Strict Standards: Declaration of action_plugin_safefnrecode::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /mnt/data/www/nbee/lib/plugins/safefnrecode/action.php on line 14

Strict Standards: Declaration of action_plugin_popularity::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /mnt/data/www/nbee/lib/plugins/popularity/action.php on line 57

Warning: Cannot modify header information - headers already sent by (output started at /mnt/data/www/nbee/lib/plugins/safefnrecode/action.php:14) in /mnt/data/www/nbee/inc/auth.php on line 352

Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open($maxcols = NULL, $numrows = NULL, $pos = NULL) in /mnt/data/www/nbee/inc/parser/metadata.php on line 24

Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close($pos = NULL) in /mnt/data/www/nbee/inc/parser/metadata.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at /mnt/data/www/nbee/lib/plugins/safefnrecode/action.php:14) in /mnt/data/www/nbee/inc/actions.php on line 180
nbsvchelper [NetBee]
 

nbsvchelper

Library that allows your program to run as a daemon (UNIX), service (Win32) and in console mode


This code makes simpler the creation of programs that run in different modes. This makes the debugging easier, because the program can be launched (and debugged) in console mode being reasonable certain that it will work in daemon/service mode as well.

The nbsvchelper library is distributed with the NetBee Developer's Pack.

How to use this code

In order to use this code, you have to modify your project as follows:

  • include include/nbsvchelper.h in your project (and add this include to your main file)
  • add the static library lib/nbsvchelper.lib when linking your program

Then, you have to create a new file that contains the following code:

    #include "nbsvchelper.h"

    void main(int argc, char %%**%%argv)
    {
       SvcHelperSetHandlers(main_startup, main_cleanup);
       SvcHelperMain(argc, argv);
    }

where:

SvcHelperSetHandler
It allows registering the functions that implement the startup and cleanup code, which must reside in the user's code. This call makes the SvcHelper DLL aware of the functions that must be called in order to initialize (and cleanup) user code. This function does not have any return value.

SvcHelperMain
It is a call to the function, inside the SvcHelper DLL, that manages the program startup. This call is needed because the main() function is called only in case the program is launched with the command line. For instance, if the program is launched as a service, the startup function is different and it is defined inside the DLL itself. This function does not have any return value.

main_startup
It is a pointer to the function (implemented by the user) that contains the code that starts the program. For example, it can create child threads, and such. This function represents the entry point in your program such as the standard main() function and it receives the same parameters (argc, argv) that were used to invoke the program. However, it does not have any return value.

main_cleanup
It is a pointer to the function (implemented by the user) which contains the code that cleans the program before exiting. This function does not have any parameter, nor any return values.

The prototype that implements the main_startup() function must be blocking. If this function returns, the daemon/service will stop working. Creation of any child threads and such is under the responsability of this function.

Prototypes for startup and cleanup functions are defined in the Snbsvchelper.h header file.

Command line parameters

In order to launch the program as a service, the program must be launched with the -d switch. In case the -d switch is omitted, the program is launched in console mode. Please note that this switch must be the first one present in the command line.

Command line parameters are left untouched when they are passed to the main_startup() function. Therefore, the -d switch should be ignored in the called program.

Supported signals/actions

Currently, this code is responsive to the following actions:

  • start service command (either through the control panel, or through the net start servicename command) (Win32 Service)
    This command allows starting a service which has been previously installed. When this command is received, the main_startup() handler is called.
  • stop service command (either through the control panel, or through the net stop servicename command) (Win32 Service) or kill -TERM (UNIX Daemon, UNIX Console)
    This command allows stopping a service which has been previously installed. When this command is received, the main_cleanup() handler is called.
  • kill -HUP command (UNIX Daemon, UNIX Console)
    This signal allows stopping a service and restarting it again. Internally, are called the main_cleanup() and then the main_startup().
  • Control+C command (UNIX Console, Windows Console), or kill -INT command (UNIX Console)
    This signal allows stopping a program running in console mode. Internally, the main_cleanup() function is called.

Installing a service

The current code does provide the capability to install and manage (start, stop, uninstall) the service. This is in order to keep the code simpler and to avoid the use of serveral command line option in order to accomplish these tasks.

In case these functions are required, you can use the program nbsvcinstaller, which is part of the NetBee distribution.

Sample

A sample is provided in the sample folder, under the nbsvchelper main folder. This can be found in the NetBee Developer's Pack.

Note for Windows users

Windows do not defines the getopt() function, which is available on UNIX for command-line parameter parsing. In case you are in a Windows environment, the nbsvchelper library exports also the getopt() function.

Compiling SvcHelper

This program is rather simple; therefore no documentation related to its internals is provided. In any case, the code is commented rather well and you can probably find what you need by digging into the code.

 
docs/nbsvchelper.txt · Last modified: 2007/11/07 03:49 by admin     Back to top