aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--daemon/main.c143
1 files changed, 76 insertions, 67 deletions
diff --git a/daemon/main.c b/daemon/main.c
index 439138d..f45eac3 100644
--- a/daemon/main.c
+++ b/daemon/main.c
@@ -66,42 +66,6 @@ void display_message( FILE* fd, char* file, int line, const char* func, char* fm
}
/******************************************************************************
- * shutdown_service
- *
- * set service stop event
- * wait until event exists
- */
-static void shutdown_service( gboolean quiet )
-{
- HANDLE he;
- BOOL ret;
-
- for(;;)
- {
- he = OpenEvent( EVENT_MODIFY_STATE, FALSE, service_stop_event_name );
- if( !he )
- {
- if( !quiet )
- ERR( "cannot open event; error %lu\n", GetLastError() );
- return;
- }
- TRACE( "setting stop event\n" );
- ret = SetEvent( he );
- CloseHandle( he );
- if( !ret )
- {
- if( !quiet )
- {
- ERR( "cannot set event; error %lu\n", GetLastError() );
- return;
- }
- }
- quiet = TRUE;
- Sleep(0);
- }
-}
-
-/******************************************************************************
* service control handler
*/
static void WINAPI ServiceControlHandler( DWORD Control )
@@ -124,23 +88,12 @@ static void WINAPI ServiceControlHandler( DWORD Control )
*/
static void WINAPI winnt_ServiceMain( DWORD Argc, LPTSTR* Argv )
{
- char security_descriptor[ SECURITY_DESCRIPTOR_MIN_LENGTH ];
- SECURITY_ATTRIBUTES security_attributes;
-
- InitializeSecurityDescriptor( (PSECURITY_DESCRIPTOR) security_descriptor,
- SECURITY_DESCRIPTOR_REVISION );
- SetSecurityDescriptorDacl( (PSECURITY_DESCRIPTOR) security_descriptor,
- TRUE, (PACL) NULL, FALSE );
- security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
- security_attributes.lpSecurityDescriptor = (PSECURITY_DESCRIPTOR) security_descriptor;
- security_attributes.bInheritHandle = FALSE;
-
hss = RegisterServiceCtrlHandler( PACKAGE_NAME,
(LPHANDLER_FUNCTION)ServiceControlHandler );
if( !hss )
return;
- service_stop_event = CreateEvent( &security_attributes, TRUE, FALSE, service_stop_event_name );
+ service_stop_event = CreateEvent( NULL, TRUE, FALSE, NULL );
if( !service_stop_event )
return;
@@ -296,6 +249,77 @@ static BOOL start_service()
}
/******************************************************************************
+ * shutdown_service
+ */
+static void winnt_shutdown_service()
+{
+ SC_HANDLE hscm, hsvc;
+ int i;
+
+ hscm = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
+ if( !hscm )
+ {
+ TRACE( "OpenSCManager error %lu\n", GetLastError() );
+ return;
+ }
+
+ hsvc = OpenService( hscm, service_name, SERVICE_ALL_ACCESS );
+ if( !hsvc )
+ {
+ TRACE( "OpenService error %lu\n", GetLastError() );
+ CloseServiceHandle( hscm );
+ return;
+ }
+
+ ControlService( hsvc, SERVICE_CONTROL_STOP, &sstatus );
+ for( i = 0; i < 10; i++ )
+ {
+ Sleep( 100 );
+ if( !QueryServiceStatus( hsvc, &sstatus ) )
+ {
+ TRACE( "Cannot query service status; error %lu\n", GetLastError() );
+ break;
+ }
+ if( SERVICE_STOPPED == sstatus.dwCurrentState )
+ break;
+ }
+
+ CloseServiceHandle( hsvc );
+}
+
+static void shutdown_service( gboolean quiet )
+{
+ /* try to stop windows NT service */
+ winnt_shutdown_service();
+
+ /* set stop_event and wait for completion */
+ for(;;)
+ {
+ BOOL ret;
+ HANDLE he = OpenEvent( EVENT_MODIFY_STATE, FALSE, service_stop_event_name );
+ if( !he )
+ {
+ if( !quiet )
+ ERR( "cannot open event; error %lu\n", GetLastError() );
+ return;
+ }
+ TRACE( "setting stop event\n" );
+ ret = SetEvent( he );
+ CloseHandle( he );
+ if( !ret )
+ {
+ if( !quiet )
+ {
+ ERR( "cannot set event; error %lu\n", GetLastError() );
+ return;
+ }
+ }
+ quiet = TRUE;
+ Sleep(0);
+ }
+}
+
+/******************************************************************************
* service installer
*/
static BOOL win9x_InstallService( char* command_line )
@@ -412,7 +436,6 @@ static void win9x_RemoveService()
static void winnt_RemoveService()
{
SC_HANDLE hscm, hsvc;
- int i;
TRACE_ENTER( "\n" );
hscm = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
@@ -430,21 +453,8 @@ static void winnt_RemoveService()
return;
}
- ControlService( hsvc, SERVICE_CONTROL_STOP, &sstatus );
-
- for( i = 0; i < 10; i++ )
- {
- Sleep( 100 );
- if( !QueryServiceStatus( hsvc, &sstatus ) )
- {
- TRACE( "Cannot query service status; error %lu\n", GetLastError() );
- break;
- }
- if( SERVICE_STOPPED == sstatus.dwCurrentState )
- break;
- }
if( !DeleteService( hsvc ) )
- TRACE( "Cannot delete service; error %lu\n", GetLastError() );
+ ERR( "Cannot delete service; error %lu\n", GetLastError() );
CloseServiceHandle( hsvc );
CloseServiceHandle( hscm );
@@ -458,10 +468,6 @@ static void remove_service()
winnt_RemoveService();
else
win9x_RemoveService();
-
- /* always try to shutdown because it's possible that daemon
- is running not as service under winNT */
- shutdown_service( TRUE );
TRACE_LEAVE( "done\n" );
}
@@ -768,6 +774,8 @@ int main( int argc, char* argv[] )
{
if( install_flag || start_flag || shutdown_flag || restart_flag )
ERR( "Remove option has priority over install/start/shutdown/restart\n" );
+
+ shutdown_service( TRUE );
remove_service();
return 0;
}
@@ -775,6 +783,7 @@ int main( int argc, char* argv[] )
{
if( start_flag || shutdown_flag || restart_flag )
ERR( "Install option has priority over start/shutdown/restart\n" );
+
if( !install_service( priority ) )
return 1;
if( !start_service() )