/* Start the service */ int start_service() { stopping = false; if (process_handle) return 0; /* Allocate a STARTUPINFO structure for a new process */ STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); /* Allocate a PROCESSINFO structure for the process */ PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); /* Get startup parameters */ char *env = 0; int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay); if (ret) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service_name, 0); return stop_service(2, true, true); } /* Launch executable with arguments */ char cmd[CMD_LENGTH]; if (_snprintf(cmd, sizeof(cmd), "\"%s\" %s", exe, flags) < 0) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0); return stop_service(2, true, true); } throttle_restart(); if (! CreateProcess(0, cmd, 0, 0, false, 0, env, dir, &si, &pi)) { unsigned long error = GetLastError(); if (error == ERROR_INVALID_PARAMETER && env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service_name, exe, NSSM_REG_ENV, 0); else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service_name, exe, error_string(error), 0); return stop_service(3, true, true); } process_handle = pi.hProcess; pid = pi.dwProcessId; if (get_process_creation_time(process_handle, &creation_time)) ZeroMemory(&creation_time, sizeof(creation_time)); /* Signal successful start */ service_status.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(service_handle, &service_status); /* Wait for a clean startup. */ if (WaitForSingleObject(process_handle, throttle_delay) == WAIT_TIMEOUT) throttle = 0; return 0; }
static DWORD handle_service_ctrl (DWORD control_code, DWORD event_type, LPVOID event_data, LPVOID context) { (void) event_type; (void) event_data; (void) context; switch (control_code) { case SERVICE_CONTROL_PRESHUTDOWN: case SERVICE_CONTROL_SHUTDOWN: case SERVICE_CONTROL_STOP: stop_service (); return NO_ERROR; case SERVICE_CONTROL_PARAMCHANGE: callbacks->on_reconfigure (callback_arg); return NO_ERROR; case SERVICE_CONTROL_INTERROGATE: update_service_status (current_state, NO_ERROR, 0, 0, 0); return NO_ERROR; } return ERROR_CALL_NOT_IMPLEMENTED; }
ERROR_CODE ran_service() { /* allocates the return value */ ERROR_CODE return_value; /* in case the service status is open */ if(service->status == STATUS_CLOSED) { /* prints a debug message */ V_DEBUG("No service to be stopped\n"); } else { /* prints a debug message */ V_DEBUG("Stopping service\n"); /* stops the service, this call should make the required changes in the service structure so that it's stopped as soon as possible */ return_value = stop_service(service); /* tests the error code for error */ if(IS_ERROR_CODE(return_value)) { /* runs the socket finish so that the proper cleanup operations are performed and then re-raises the error*/ SOCKET_FINISH(); RAISE_AGAIN(return_value); } /* prints a debug message */ V_DEBUG("Finished stopping service\n"); } /* raises no error */ RAISE_NO_ERROR; }
/* Service control handler */ unsigned long WINAPI service_control_handler(unsigned long control, unsigned long event, void *data, void *context) { switch (control) { case SERVICE_CONTROL_SHUTDOWN: case SERVICE_CONTROL_STOP: log_service_control(service_name, control, true); stop_service(0, true, true); return NO_ERROR; case SERVICE_CONTROL_CONTINUE: log_service_control(service_name, control, true); if (! throttle_timer) return ERROR_CALL_NOT_IMPLEMENTED; throttle = 0; ZeroMemory(&throttle_duetime, sizeof(throttle_duetime)); SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0); service_status.dwCurrentState = SERVICE_CONTINUE_PENDING; service_status.dwWaitHint = throttle_milliseconds() + NSSM_WAITHINT_MARGIN; log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service_name, 0); SetServiceStatus(service_handle, &service_status); return NO_ERROR; case SERVICE_CONTROL_PAUSE: /* We don't accept pause messages but it isn't possible to register only for continue messages so we have to handle this case. */ log_service_control(service_name, control, false); return ERROR_CALL_NOT_IMPLEMENTED; } /* Unknown control */ log_service_control(service_name, control, false); return ERROR_CALL_NOT_IMPLEMENTED; }
//lancement de l'analyse ring 0 void analysis::ring0analysis() { manager=NULL; service=NULL; int error=0; int status = 0; status= driverStatus(); if(status==0) { printf(" [-] Impossible to query the driver status.\n"); return; } //si pas déjà installé, installation if(status == DRIVER_NOT_INSTALLED) { //installation if(!install_driver()) { printf(" [-] Impossible to install the driver.\n"); error=2; } } //si pas démarré, démarrage if((status == DRIVER_STOPPED || status == DRIVER_NOT_INSTALLED) && !error) { if(!start_service()) { printf(" [-] Impossible to start the service.\n"); error=1; } } //si pas d'erreurs (et service lancé, donc), analyse if(!error) { ssdt(); //stop, puisque lancé if(!stop_service()) printf(" [-] Impossible to stop the service.\n"); } if(error<1) { //si installé, désinstallation if(!remove_driver()) printf(" [-] Impossible to delete the driver.\n"); } sCCleanHandles(); }
int WINAPI manage_npf_driver(LPCTSTR LogFileName, char operation) { FILE *log; DWORD ReturnValue = NO_ERROR; if (LogFileName != NULL) log = fopen(LogFileName, "a"); else log = NULL; switch(operation) { case 's': ReturnValue = start_service(log,"NPF"); break; case 'x': ReturnValue = stop_service(log,"NPF"); break; case 'u': ReturnValue = delete_service(log,"NPF"); break; case 'i': ReturnValue = create_driver_service(log,"NPF","NetGroup Packet Filter Driver","system32\\drivers\\npf.sys"); break; case 'r': (void)delete_service(log,"NPF"); Sleep(100); ReturnValue = create_driver_service(log,"NPF","NetGroup Packet Filter Driver","system32\\drivers\\npf.sys"); break; case 'a': ReturnValue = change_start_type_service(log,"NPF", SERVICE_AUTO_START); break; case 'd': ReturnValue = change_start_type_service(log,"NPF", SERVICE_DEMAND_START); break; default: ReturnValue = ERROR_INVALID_PARAMETER; break; } if (log != NULL) fclose(log); return (int)ReturnValue; }
/** * @brief * @param * @see * @remarks * @code * @endcode * @return */ scm_context::~scm_context() { if(INVALID_HANDLE_VALUE != _driver_handle) { CloseHandle(_driver_handle); _driver_handle = INVALID_HANDLE_VALUE; } if (true == _uninstall_service_on_free) { stop_service(); uninstall_service(); } }
static char * copy_filter(HWND hDlg, char *resin_home, char *isapi_script, int *iis_restart, int is_nt) { char src_name[1024]; char dst_name[1024]; FILE *src_file; FILE *dst_file; char buf[1024]; int len; *iis_restart = 0; sprintf(src_name, "%s/win32/isapi_srun.dll", resin_home); sprintf(dst_name, "%s\\isapi_srun.dll", isapi_script); dst_file = fopen(dst_name, "w+b"); if (! dst_file) { if (! is_nt) MessageBox(hDlg, TEXT("You must stop PWS for setup to copy the new iis_srun filter. You may need to press 'Remove', reboot, and then install Resin."), TEXT("Stop PWS"), MB_OK); else if (MessageBox(hDlg, TEXT("Setup needs to stop IIS to copy the ISAPI filter."), TEXT("Stop IIS?"), MB_OKCANCEL) == IDCANCEL) return 0; if (is_nt) { char *msg = stop_service(IIS_SERVICE); if (msg) return msg; *iis_restart = 1; } dst_file = fopen(dst_name, "w+b"); } if (! dst_file) return "Can't create isapi_srun.dll in IIS script directory"; src_file = fopen(src_name, "rb"); if (! src_file) { fclose(dst_file); return "Can't open isapi_srun.dll in RESIN_HOME"; } while ((len = fread(buf, 1, sizeof(buf), src_file)) > 0) { fwrite(buf, 1, len, dst_file); } fclose(src_file); fclose(dst_file); return 0; }
/** * Actual code of the service that does the work. * * @param dwArgc * @param *lpszArgv * * @author mickem * * @date 03-13-2004 * */ void _handle_start(DWORD dwArgc, LPTSTR *lpszArgv) { stop_mutex_.lock(); if (!_report_status_to_SCMgr(SERVICE_RUNNING, 0)) { stop_service(); return; } TBase::handle_startup(utf8::cvt<std::string>(name_)); stop_mutex_.lock(); print_debug(_T("Shutting down: ") + name_); TBase::handle_shutdown(utf8::cvt<std::string>(name_)); }
void ConcurrentMarkThread::stop() { { MutexLockerEx ml(Terminator_lock); _should_terminate = true; } stop_service(); { MutexLockerEx ml(Terminator_lock); while (!_has_terminated) { Terminator_lock->wait(); } } }
char * configure_iis(HWND hDlg, char *resin_home, char *isapi_script) { char filter[1024]; int is_nt = (GetVersion() & 0x80000000) == 0; sprintf(filter, "%s\\isapi_srun.dll", isapi_script); char *metabase_msg = 0; add_resin_script_permission(isapi_script); char *script = add_resin_script_metabase(hDlg, isapi_script); metabase_msg = add_resin_filter_metabase(hDlg, filter); if (metabase_msg) add_resin_filter_registry(hDlg, isapi_script); int iis_restart = 0; char *msg = copy_filter(hDlg, resin_home, isapi_script, &iis_restart, is_nt); if (! is_nt) { } else if (iis_restart) start_service(IIS_SERVICE); else if (! msg) { if (MessageBox(hDlg, TEXT("Do you want to restart IIS to use the new filter?"), TEXT("Restart IIS?"), MB_OKCANCEL) == IDCANCEL) return 0; char *msg = stop_service(IIS_SERVICE); if (! msg) start_service(IIS_SERVICE); } if (! msg) return metabase_msg; else return msg; }
char * remove_iis(HWND hDlg) { char *msg = remove_resin_filter_metabase(hDlg); if (msg) return msg; int is_nt = (GetVersion() & 0x80000000) == 0; if (is_nt) { if (MessageBox(hDlg, TEXT("Do you want to restart IIS?"), TEXT("Restart IIS?"), MB_OKCANCEL) == IDCANCEL) return 0; msg = stop_service(IIS_SERVICE); if (! msg) start_service(IIS_SERVICE); } else { MessageBox(hDlg, TEXT("You will need to restart IIS to remove Resin. You may need to reboot to see the changes."), TEXT("Restart"), MB_OK); } return msg; }
int _tmain( int argc, TCHAR *argv[] ) { // LPCTSTR service_name = _T("hindsight"); TCHAR *service_name = _T("hindsight"); if ( argc == 1 ) return run_service( service_name ); if ( lstrcmpi(argv[1], TEXT("install")) == 0 ) { return install_service( service_name ); } if ( lstrcmpi(argv[1], TEXT("start")) == 0 ) { return start_service( service_name ); } if ( lstrcmpi(argv[1], TEXT("stop")) == 0 ) { return stop_service( service_name ); } if ( lstrcmpi(argv[1], TEXT("uninstall")) == 0 ) { return uninstall_service( service_name ); } return -1; }
static void handle_event(struct uevent *uevent,struct hotplug_info *hotplug_info) { write_uevent_logcat(uevent,uevent->type); //return ; int c =uevent->action[0]; switch(c) { case 'a':{ if(!strncmp(uevent->type,"usb_interface",12)){ // see if this interface is ripe for a switching write_uevent_logcat(uevent,uevent->type); try_usb_modeswitch(uevent->vendor_id, uevent->product_id,hotplug_info->modeswitch_d); }else if(!strncmp(uevent->type,"usb-serial",10)){ //Serial Killals write_uevent_logcat(uevent,uevent->type); } else if(!strncmp(uevent->subsystem,"tty",3)){ write_uevent_logcat(uevent,uevent->type); if(!strncmp(uevent->name,"ttyUSB0",7)) property_set("ril.pppd_tty", "/dev/ttyUSB0"); if(!strncmp(uevent->name,"ttyHS4",7) ) property_set("ril.pppd_tty", "/dev/ttyHS4"); if(!strncmp(uevent->name,"ttyHS3",7)){ property_set("rild.libargs", "-d /dev/ttyHS3"); property_set("rild.libpath", "/system/lib/libtcl-ril.so"); start_service("ril-daemon"); } if(!strncmp(uevent->name,"ttyUSB2",7)) { property_set("rild.libargs", "-d /dev/ttyUSB2"); property_set("rild.libpath", "/system/lib/libhuaweigeneric-ril.so"); start_service("ril-daemon"); } }else { //write_uevent_logcat(uevent,"ignored"); } break; } case 'r':{ if(!strncmp(uevent->type,"usb_device",10)){ //Serial Killa write_uevent_logcat(uevent,uevent->type); //handle_connection_connection(); } else if(!strncmp(uevent->subsystem,"tty",3)){ write_uevent_logcat(uevent,uevent->type); if(!strncmp(uevent->name,"ttyUSB2",7)) { ALOGD("Stopping Rild"); stop_service("ril-daemon"); }else if(!strncmp(uevent->name,"ttyUSB0",7)){ system("pkill -9 ppp"); ALOGD("Killing PPP"); } } break; } case 'c':{ //write_uevent_logcat(uevent,"ignored"); } break; } }
static void handle_uevent(struct uevent *uevent) { // We need to handle the write_uevent_logcat(uevent,"DONGLED"); int c =uevent->action[0]; switch(c) { case 'a':{ if( strlen(uevent->type) == 0 ) { // if no type is specified then look for a tty subsystem if ( strlen(uevent->subsystem) == 0 ){ // handle zero length subsystem return ; // No Subsystem and No type means nothing doing }else if( !strncmp(uevent->subsystem,"tty",strlen(uevent->subsystem))){ // got a tty subsystem if(!strncmp(uevent->name,"ttyUSB0",strlen(uevent->name))){ property_set("ril.pppd_tty", "/dev/ttyUSB0"); return ; } if(!strncmp(uevent->name,"ttyHS4",strlen(uevent->name)) ){ property_set("ril.pppd_tty", "/dev/ttyHS4"); return ; } if(!strncmp(uevent->name,"ttyHS3",strlen(uevent->name))){ property_set("rild.libargs", "-d /dev/ttyHS3"); property_set("rild.libpath", "/system/lib/libtcl-ril.so"); stop_service("ril-daemon"); sleep(1); start_service("ril-daemon"); return ; } if(!strncmp(uevent->name,"ttyUSB2",strlen(uevent->name))) { property_set("rild.libargs", "-d /dev/ttyUSB2"); property_set("rild.libpath", "/system/lib/libhuaweigeneric-ril.so"); stop_service("ril-daemon"); sleep(1); start_service("ril-daemon"); return; } } return; }else{ if( !strncmp(uevent->type,"usb_device",strlen(uevent->type))) { // i'm not a massive fan of sleeping, it implies an unknown quantity // however we need to give the usb subsystem a chance to catch up // there is another option of handling the uevent->type = "usb_interface" // but that is fired multiple times where as usb_device is raised once sleep(2); // convert the vendor and product to int, this is more convient than messing around with strings int vendor = get_int_from_hexstring(uevent->vendor_id); int product = get_int_from_hexstring(uevent->product_id); process_add_usb_device_uevent(vendor,product); return ; } } } } return ; }
/* Callback function triggered when the server exits */ void CALLBACK end_service(void *arg, unsigned char why) { if (stopping) return; stopping = true; pid = (unsigned long) arg; /* Check exit code */ unsigned long exitcode = 0; char code[16]; FILETIME exit_time; GetExitCodeProcess(process_handle, &exitcode); if (exitcode == STILL_ACTIVE || get_process_exit_time(process_handle, &exit_time)) GetSystemTimeAsFileTime(&exit_time); CloseHandle(process_handle); /* Log that the service ended BEFORE logging about killing the process tree. See below for the possible values of the why argument. */ if (! why) { _snprintf(code, sizeof(code), "%d", exitcode); log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0); } /* Clean up. */ kill_process_tree(service_name, pid, exitcode, pid, &creation_time, &exit_time); /* The why argument is true if our wait timed out or false otherwise. Our wait is infinite so why will never be true when called by the system. If it is indeed true, assume we were called from stop_service() because this is a controlled shutdown, and don't take any restart action. */ if (why) return; /* What action should we take? */ int action = NSSM_EXIT_RESTART; unsigned char action_string[ACTION_LEN]; bool default_action; if (! get_exit_action(service_name, &exitcode, action_string, &default_action)) { for (int i = 0; exit_action_strings[i]; i++) { if (! _strnicmp((const char *) action_string, exit_action_strings[i], ACTION_LEN)) { action = i; break; } } } process_handle = 0; pid = 0; switch (action) { /* Try to restart the service or return failure code to service manager */ case NSSM_EXIT_RESTART: log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_RESTART, service_name, code, exit_action_strings[action], exe, 0); while (monitor_service()) { log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_RESTART_SERVICE_FAILED, exe, service_name, 0); Sleep(30000); } break; /* Do nothing, just like srvany would */ case NSSM_EXIT_IGNORE: log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_IGNORE, service_name, code, exit_action_strings[action], exe, 0); Sleep(INFINITE); break; /* Tell the service manager we are finished */ case NSSM_EXIT_REALLY: log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_REALLY, service_name, code, exit_action_strings[action], 0); stop_service(exitcode, true, default_action); break; /* Fake a crash so pre-Vista service managers will run recovery actions. */ case NSSM_EXIT_UNCLEAN: log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service_name, code, exit_action_strings[action], 0); exit(stop_service(exitcode, false, default_action)); break; } }