📄 smpd_service.c
字号:
_tprintf(TEXT("%s installed, but failed to start:\n%s.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME), smpd_get_last_error_text(szErr, 256) ); fflush(stdout); CloseServiceHandle(schService); } else { _tprintf(TEXT("CreateService failed:\n%s\n"), smpd_get_last_error_text(szErr, 256)); fflush(stdout); } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); }}/* FUNCTION: smpd_remove_service(BOOL bErrorOnNotInstalled) PURPOSE: Stops and removes the service PARAMETERS: none RETURN VALUE: none COMMENTS:*/SMPD_BOOL smpd_remove_service(SMPD_BOOL bErrorOnNotInstalled){ SMPD_BOOL bRetVal = SMPD_FALSE; SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szErr[256]; schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS ); if ( schSCManager ) { schService = OpenService(schSCManager, TEXT(SMPD_SERVICE_NAME), SERVICE_ALL_ACCESS); if (schService) { /* try to stop the service */ if ( ControlService( schService, SERVICE_CONTROL_STOP, &smpd_process.ssStatus ) ) { _tprintf(TEXT("Stopping %s."), TEXT(SMPD_SERVICE_DISPLAY_NAME)); fflush(stdout); Sleep( 1000 ); while( QueryServiceStatus( schService, &smpd_process.ssStatus ) ) { if ( smpd_process.ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); fflush(stdout); Sleep( 250 ); } else break; } if ( smpd_process.ssStatus.dwCurrentState == SERVICE_STOPPED ) { _tprintf(TEXT("\n%s stopped.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME) ); fflush(stdout); } else { _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME) ); fflush(stdout); } } /* Delete the registry entries for the service. */ RegDeleteKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MPICH\\SMPD"); /* now remove the service */ if (DeleteService(schService)) { _tprintf(TEXT("%s removed.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME) ); fflush(stdout); bRetVal = SMPD_TRUE; } else { _tprintf(TEXT("DeleteService failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); } CloseServiceHandle(schService); } else { if (bErrorOnNotInstalled) { _tprintf(TEXT("OpenService failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); } else { bRetVal = SMPD_TRUE; } } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); } return bRetVal;}/* FUNCTION: smpd_stop_service() PURPOSE: Stops the service PARAMETERS: none RETURN VALUE: none COMMENTS:*/void smpd_stop_service(){ SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szErr[256]; schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if ( schSCManager ) { schService = OpenService(schSCManager, TEXT(SMPD_SERVICE_NAME), SERVICE_ALL_ACCESS); if (schService) { /* try to stop the service */ if ( ControlService( schService, SERVICE_CONTROL_STOP, &smpd_process.ssStatus ) ) { _tprintf(TEXT("Stopping %s."), TEXT(SMPD_SERVICE_DISPLAY_NAME)); fflush(stdout); Sleep( 1000 ); while( QueryServiceStatus( schService, &smpd_process.ssStatus ) ) { if ( smpd_process.ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); fflush(stdout); Sleep( 250 ); } else break; } if ( smpd_process.ssStatus.dwCurrentState == SERVICE_STOPPED ) { _tprintf(TEXT("\n%s stopped.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME) ); fflush(stdout); } else { _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME) ); fflush(stdout); } } CloseServiceHandle(schService); } else { _tprintf(TEXT("OpenService failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); }}/* FUNCTION: smpd_start_service() PURPOSE: Starts the service PARAMETERS: none RETURN VALUE: none COMMENTS:*/void smpd_start_service(){ SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szErr[256]; schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if ( schSCManager ) { schService = OpenService(schSCManager, TEXT(SMPD_SERVICE_NAME), SERVICE_ALL_ACCESS); if ( schService ) { /* Start the service */ if (StartService(schService, 0, NULL)) { _tprintf(TEXT("%s started.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME) ); fflush(stdout); } else { _tprintf(TEXT("%s failed to start.\n%s.\n"), TEXT(SMPD_SERVICE_DISPLAY_NAME), smpd_get_last_error_text(szErr, 256) ); fflush(stdout); } CloseServiceHandle(schService); } else { _tprintf(TEXT("OpenService failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), smpd_get_last_error_text(szErr,256)); fflush(stdout); }}/* FUNCTION: smpd_get_last_error_text PURPOSE: copies error message text to string PARAMETERS: lpszBuf - destination buffer dwSize - size of buffer RETURN VALUE: destination buffer COMMENTS:*/static LPTSTR smpd_get_last_error_text( LPTSTR lpszBuf, DWORD dwSize ){ DWORD dwRet; LPTSTR lpszTemp = NULL; dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, GetLastError(), LANG_NEUTRAL, (LPTSTR)&lpszTemp, 0, NULL ); /* supplied buffer is not long enough */ if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) ) lpszBuf[0] = TEXT('\0'); else { lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); /* remove cr and newline character */ _stprintf( lpszBuf, TEXT("%s (error %d)"), lpszTemp, GetLastError() ); } if ( lpszTemp ) LocalFree((HLOCAL) lpszTemp ); return lpszBuf;}/* A bomb thread can be used to guarantee that the service will exit when a stop command is processed */void smpd_bomb_thread(){ if (WaitForSingleObject(smpd_process.hBombDiffuseEvent, (DWORD)10000) == WAIT_TIMEOUT) { smpd_dbg_printf("smpd_bomb_thread timed out, exiting.\n"); ExitProcess((UINT)-1); }}/* FUNCTION: smpd_service_stop PURPOSE: Stops the service PARAMETERS: none RETURN VALUE: none COMMENTS: If a ServiceStop procedure is going to take longer than 3 seconds to execute, it should spawn a thread to execute the stop code, and return. Otherwise, the ServiceControlManager will believe that the service has stopped responding.*/ void smpd_service_stop(){ MPIDU_Sock_set_t set; MPIDU_Sock_t sock; MPIDU_Sock_event_t event; char host[SMPD_MAX_HOST_LENGTH]; int iter; DWORD dwThreadID; int result; for (iter=0; iter<10; iter++) { smpd_process.hBombThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)smpd_bomb_thread, NULL, 0, &dwThreadID); if (smpd_process.hBombThread != NULL) break; Sleep(250); } /* stop the main thread */ smpd_process.service_stop = SMPD_TRUE; smpd_get_hostname(host, SMPD_MAX_HOST_LENGTH); result = MPIDU_Sock_create_set(&set); if (result != MPI_SUCCESS) { smpd_err_printf("MPIDU_Sock_create_set failed,\nsock error: %s\n", get_sock_error_string(result)); SetEvent(smpd_process.hBombDiffuseEvent); WaitForSingleObject(smpd_process.hBombThread, (DWORD)3000); CloseHandle(smpd_process.hBombThread); ExitProcess((UINT)-1); } result = MPIDU_Sock_post_connect(set, NULL, host, smpd_process.port, &sock); if (result != MPI_SUCCESS) { smpd_err_printf("Unable to connect to '%s:%d',\nsock error: %s\n", smpd_process.host_list->host, smpd_process.port, get_sock_error_string(result)); SetEvent(smpd_process.hBombDiffuseEvent); WaitForSingleObject(smpd_process.hBombThread, (DWORD)3000); CloseHandle(smpd_process.hBombThread); ExitProcess((UINT)-1); } result = MPIDU_Sock_wait(set, MPIDU_SOCK_INFINITE_TIME, &event); if (result != MPI_SUCCESS) { smpd_err_printf("Unable to connect to '%s:%d',\nsock error: %s\n", smpd_process.host_list->host, smpd_process.port, get_sock_error_string(result)); SetEvent(smpd_process.hBombDiffuseEvent); WaitForSingleObject(smpd_process.hBombThread, (DWORD)3000); CloseHandle(smpd_process.hBombThread); ExitProcess((UINT)-1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -