diff options
Diffstat (limited to '')
-rw-r--r-- | daemon/main.c | 160 |
1 files changed, 134 insertions, 26 deletions
diff --git a/daemon/main.c b/daemon/main.c index ee91238..9b5aa29 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -186,31 +186,35 @@ static BOOL open_run_key( PHKEY hk ) } /****************************************************************************** - * service installer + * start service */ -static BOOL win9x_InstallService( char* command_line ) +static BOOL win9x_StartService() { - BOOL ret; + BOOL ret = FALSE; HKEY hk; + LONG status; + DWORD type, size; + char command_line[ MAX_PATH ]; STARTUPINFO si; PROCESS_INFORMATION pi; - TRACE_ENTER( "command_line=%s\n", command_line ); - ret = open_run_key( &hk ); - if( !ret ) + TRACE_ENTER( "\n" ); + if( !open_run_key( &hk ) ) + goto done; + + size = sizeof(command_line); + status = RegQueryValueEx( hk, service_name, NULL, &type, command_line, &size ); + RegCloseKey( hk ); + if( status != ERROR_SUCCESS ) { - TRACE_LEAVE( "done\n" ); - return FALSE; + ERR( "Cannot get registry value; error %lu\n", GetLastError() ); + goto done; } - - if( RegSetValueEx( hk, service_name, 0, REG_SZ, command_line, strlen( command_line ) + 1 ) ) + if( type != REG_SZ ) { - ERR( "Cannot set registry value; error %lu\n", GetLastError() ); - ret = FALSE; + ERR( "Invalid type of registry value\n" ); + goto done; } - RegCloseKey( hk ); - if( !ret ) - return FALSE; memset( &si, 0, sizeof(si ) ); si.cb = sizeof(si); @@ -224,8 +228,82 @@ static BOOL win9x_InstallService( char* command_line ) CloseHandle( pi.hThread ); CloseHandle( pi.hProcess ); + +done: + TRACE_LEAVE( "done; ret=%d\n", ret ); + return ret; +} + +static BOOL winnt_StartService() +{ + SC_HANDLE hscm, hsvc; + BOOL ret; + + TRACE_ENTER( "\n" ); + hscm = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); + if( !hscm ) + { + ERR( "Cannot open service control manager; error %lu\n", GetLastError() ); + return FALSE; + } + hsvc = OpenService( hscm, service_name, SERVICE_ALL_ACCESS ); + if( !hsvc ) + { + ERR( "Cannot open service %s; error %lu\n", service_name, GetLastError() ); + ret = FALSE; + } + else + { + ret = StartService( hsvc, 0, NULL ); + if( !ret ) + ERR( "Cannot start service; error %lu\n", GetLastError() ); + CloseServiceHandle( hsvc ); + } + CloseServiceHandle( hscm ); TRACE_LEAVE( "done\n" ); - return TRUE; + return ret; +} + +static BOOL start_service() +{ + BOOL ret; + + TRACE_ENTER( "\n" ); + + if( VER_PLATFORM_WIN32_NT == vi.dwPlatformId ) + ret = winnt_StartService(); + else + ret = win9x_StartService(); + + TRACE_LEAVE( "done; ret=%d\n", ret ); + return ret; +} + +/****************************************************************************** + * service installer + */ +static BOOL win9x_InstallService( char* command_line ) +{ + BOOL ret; + HKEY hk; + + TRACE_ENTER( "command_line=%s\n", command_line ); + ret = open_run_key( &hk ); + if( !ret ) + { + TRACE_LEAVE( "done\n" ); + return FALSE; + } + + if( RegSetValueEx( hk, service_name, 0, REG_SZ, command_line, strlen( command_line ) + 1 ) ) + { + ERR( "Cannot set registry value; error %lu\n", GetLastError() ); + ret = FALSE; + } + RegCloseKey( hk ); + + TRACE_LEAVE( "done; ret=%d\n", ret ); + return ret; } static BOOL winnt_InstallService( char* command_line ) @@ -251,10 +329,8 @@ static BOOL winnt_InstallService( char* command_line ) } else { - ret = StartService( hsvc, 0, NULL ); - if( !ret ) - ERR( "Cannot start service; error %lu\n", GetLastError() ); CloseServiceHandle( hsvc ); + ret = TRUE; } CloseServiceHandle( hscm ); TRACE_LEAVE( "done\n" ); @@ -466,7 +542,9 @@ int main( int argc, char* argv[] ) static int install_flag = 0; static int remove_flag = 0; static int service_flag = 0; + static int start_flag = 0; static int shutdown_flag = 0; + static int restart_flag = 0; char *instance_name = NULL; char *priority = NULL; int getopt_failure = 0; @@ -513,7 +591,9 @@ int main( int argc, char* argv[] ) { "install", no_argument, &install_flag, 1 }, { "remove", no_argument, &remove_flag, 1 }, { "service", no_argument, &service_flag, 1 }, + { "start", no_argument, &start_flag, 1 }, { "shutdown", no_argument, &shutdown_flag,1 }, + { "restart", no_argument, &restart_flag, 1 }, { "instance", required_argument, NULL, 'I'}, { "priority", required_argument, NULL, 'p'}, { 0, 0, 0, 0 } @@ -642,33 +722,61 @@ int main( int argc, char* argv[] ) "\t\tincreases verbosity\n" ); printf( "-i, --install\tinstall and start service\n" ); printf( "-r, --remove\tstop and remove service\n" ); + printf( "--start\t\tstart installed daemon\n" ); printf( "-s, --shutdown\tsend shutdown signal to the daemon\n" ); + printf( "--restart\trestart daemon\n" ); printf( "-I, --instance\tset instance name in the case of multiple daemons\n" ); printf( "-p, --priority\tset priority class; value may be 'normal' (default),\n" - "\t\t\t'high', or 'highest'\n" ); + "\t\t'high', or 'highest'\n" ); printf( "-h, --help\tdisplay this message\n" ); printf( "--version\tdisplay version information\n" ); return 0; } - /* install/remove/shutdown */ + /* install/remove/start/shutdown/restart */ if( remove_flag ) { - if( install_flag || shutdown_flag ) - ERR( "Remove option has priority over install/shutdown\n" ); + if( install_flag || start_flag || shutdown_flag || restart_flag ) + ERR( "Remove option has priority over install/start/shutdown/restart\n" ); remove_service(); return 0; } if( install_flag ) { - if( shutdown_flag ) - ERR( "Install option has priority over shutdown\n" ); - install_service( priority ); + 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() ) + return 1; + return 0; + } + if( start_flag ) + { + if( shutdown_flag || restart_flag ) + { + ERR( "Please specify only one action\n" ); + return 1; + } + if( !start_service() ) + return 1; return 0; } if( shutdown_flag ) { + if( restart_flag ) + { + ERR( "Please specify only one action\n" ); + return 1; + } + shutdown_service( FALSE ); + return 0; + } + if( restart_flag ) + { shutdown_service( FALSE ); + if( !start_service() ) + return 1; return 0; } |