Daemons and Services


A UNIX daemon is a process that runs in the background and does not requirea user interface. A service application is the equivalent on Windows. Normally, a daemon is started when the system is booted and runs without supervision until the system is shut down. Similarly, a Windows service can be started at boot time and run until system shutdown. However, the service control manager (SCM) controls all services, so to convert daemon code to run on Windows, you mustadd code to interface with the SCM.

Unless the main() function of the daemon is extremely simple, the best strategyis to rename it to something else, such as service_main() . Then create a new main that contains code to install, uninstall, and run the service depending on command-line arguments.

To install the program as a service, the program must call OpenSCManager to geta handle to the SCM. It must then call CreateService, passing the SCM handle and several arguments, including the service name, display name , service type, path to the executable, and identity the service uses to run. After the service is installed, the administrative tools services applet can be used to examine and modify many these values.

Uninstalling the service is similar to installing it. Call OpenSCManager to get a handle to the SCM, and then call OpenService to get a handle to the service. If itis running, the service should be stopped by calling the ControlService function. Finally, call DeleteService , passing the service handle, and close the handlesto clean up.

Running the daemon as a service is also fairly straightforward. The new main function sets up a SERVICE_TABLE_ENTRY structure that contains a name and a pointer to the service main, which is the old main function renamed service_main . This structure is passed to StartServiceCtrlDispatcher , which does not return until the service stops. Note that more than one service entry can be in the service entry structure, so a single executable can support more than one service. The definitions are the same as a main() function; the arguments to the service main function are supplied by the SCM and can be set by the services applet or the CreateService call.

The service main function needs new code to call the function SetServiceStatus , which keeps the SCM informed of the service s status during startup. If the SCM does not receive status updates within a specified time period, it assumes thatthe service has stopped running and logs an error. The SCM must also be given the address of a service control function that it uses to inform the service when it should stop and for other requests . Call RegisterServiceCtrlHandler or RegisterServiceCtrlHandlerEx to set this address. When the service is fully initialized , it should call SetServiceStatus with the SERVICE_RUNNING status to complete the startup sequence.

For sample service programs and details of the service functions, see the MSDN Web site ( http://www.msdn.microsoft.com/ ).




UNIX Application Migration Guide
Unix Application Migration Guide (Patterns & Practices)
ISBN: 0735618380
EAN: 2147483647
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net