22 NTService::NTService()
32 fpServiceThread = NULL;
35 nStartTimeOut = 15000;
36 nStopTimeOut = 86400000;
38 nResumeTimeOut = 5000;
41 dwDesiredAccess = SERVICE_ALL_ACCESS;
42 dwServiceType = SERVICE_WIN32_OWN_PROCESS;
43 dwStartType = SERVICE_AUTO_START;
44 dwErrorControl = SERVICE_ERROR_NORMAL;
45 szLoadOrderGroup = NULL;
47 szDependencies = NULL;
59 NTService::~NTService()
61 if (ServiceName != NULL)
delete[] ServiceName;
67 BOOL NTService::GetOS()
70 memset(&osVer, 0,
sizeof(OSVERSIONINFO));
71 osVer.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
72 if (GetVersionEx(&osVer))
74 if (osVer.dwPlatformId == VER_PLATFORM_WIN32_NT)
94 fpServiceThread = (THREAD_FC)ServiceThread;
95 ServiceName =
new char[lstrlen(szInternName)+1];
96 lstrcpy(ServiceName,szInternName);
98 SERVICE_TABLE_ENTRY stb[] =
100 { (
char *)szInternName,(LPSERVICE_MAIN_FUNCTION)
ServiceMain} ,
104 return StartServiceCtrlDispatcher(stb);
119 LPCSTR szDisplayName,
120 LPCSTR szFullPath, LPCSTR szAccountName,
124 SC_HANDLE newService, scm;
126 if (!SeekStatus(szInternName,1))
129 char szFilePath[_MAX_PATH];
130 GetModuleFileName(NULL, szFilePath,
sizeof(szFilePath));
133 if (!(scm = OpenSCManager(0, 0,SC_MANAGER_CREATE_SERVICE)))
134 printf(
"Failed to install the service (Couldn't open the SCM)\n");
144 (startType == 1 ? SERVICE_AUTO_START :
145 SERVICE_DEMAND_START),
153 printf(
"Failed to install the service (Couldn't create service)\n");
156 printf(
"Service successfully installed.\n");
157 CloseServiceHandle(newService);
160 CloseServiceHandle(scm);
179 BOOL ret_value=FALSE;
180 SC_HANDLE service, scm;
182 if (!SeekStatus(szInternName,0))
188 if (!(scm = OpenSCManager(0, 0,SC_MANAGER_CREATE_SERVICE)))
190 printf(
"Failed to remove the service (Couldn't open the SCM)\n");
194 if ((service = OpenService(scm,szInternName, DELETE)))
196 if (!DeleteService(service))
197 printf(
"Failed to remove the service\n");
200 printf(
"Service successfully removed.\n");
203 CloseServiceHandle(service);
206 printf(
"Failed to remove the service (Couldn't open the service)\n");
207 CloseServiceHandle(scm);
218 SetStatus(SERVICE_STOP_PENDING,NO_ERROR, 0, 1, 60000);
220 SetStatus(SERVICE_STOPPED, NO_ERROR, 0, 1, 1000);
233 if (!(pService->hServiceStatusHandle =
234 RegisterServiceCtrlHandler(pService->ServiceName,
236 NTService::ServiceCtrlHandler)))
240 if (!pService->SetStatus(SERVICE_START_PENDING,NO_ERROR, 0, 1, 8000))
244 if (!(pService->hExitEvent = CreateEvent (0, TRUE, FALSE,0)))
247 if (!pService->SetStatus(SERVICE_START_PENDING,NO_ERROR, 0, 3,
248 pService->nStartTimeOut))
252 pService->my_argc=argc;
253 pService->my_argv=argv;
256 if (!pService->StartService())
260 WaitForSingleObject (pService->hExitEvent, INFINITE);
263 if (WaitForSingleObject (pService->hThreadHandle, INFINITE) == WAIT_TIMEOUT)
264 CloseHandle(pService->hThreadHandle);
270 pService->Exit(GetLastError());
276 void NTService::SetRunning()
279 pService->SetStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
285 pService->SetStatus(SERVICE_START_PENDING,NO_ERROR, 0, 0, timeout);
293 BOOL NTService::StartService()
296 if (!(hThreadHandle = (HANDLE) _beginthread((THREAD_FC)fpServiceThread,0,
305 void NTService::StopService()
311 SetEvent(hShutdownEvent);
314 SetEvent(hExitEvent);
319 void NTService::PauseService()
322 SuspendThread(hThreadHandle);
327 void NTService::ResumeService()
330 ResumeThread(hThreadHandle);
335 BOOL NTService::SetStatus (DWORD dwCurrentState,DWORD dwWin32ExitCode,
336 DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,
340 SERVICE_STATUS serviceStatus;
342 dwState=dwCurrentState;
344 serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
345 serviceStatus.dwCurrentState = dwCurrentState;
347 if (dwCurrentState == SERVICE_START_PENDING)
348 serviceStatus.dwControlsAccepted = 0;
350 serviceStatus.dwControlsAccepted = (SERVICE_ACCEPT_STOP |
351 SERVICE_ACCEPT_PAUSE_CONTINUE |
352 SERVICE_ACCEPT_SHUTDOWN);
355 if (dwServiceSpecificExitCode == 0)
356 serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
358 serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
360 serviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
362 serviceStatus.dwCheckPoint = dwCheckPoint;
363 serviceStatus.dwWaitHint = dwWaitHint;
366 if (!(bRet=SetServiceStatus (hServiceStatusHandle, &serviceStatus)))
374 void NTService::ServiceCtrlHandler(DWORD ctrlCode)
381 dwState=pService->dwState;
384 case SERVICE_CONTROL_SHUTDOWN:
385 case SERVICE_CONTROL_STOP:
386 dwState = SERVICE_STOP_PENDING;
387 pService->SetStatus(SERVICE_STOP_PENDING,NO_ERROR, 0, 1,
388 pService->nStopTimeOut);
389 pService->StopService();
393 pService->SetStatus(dwState, NO_ERROR,0, 0, 0);
403 void NTService::Exit(DWORD error)
406 CloseHandle(hExitEvent);
409 if (hServiceStatusHandle)
410 SetStatus(SERVICE_STOPPED, error,0, 0, 0);
421 BOOL NTService::SeekStatus(LPCSTR szInternName,
int OperationType)
423 BOOL ret_value=FALSE;
424 SC_HANDLE service, scm;
427 if (!(scm = OpenSCManager(0, 0,SC_MANAGER_CREATE_SERVICE)))
429 DWORD ret_error=GetLastError();
430 if (ret_error == ERROR_ACCESS_DENIED)
432 printf(
"Install/Remove of the Service Denied!\n");
433 if (!is_super_user())
434 printf(
"That operation should be made by an user with Administrator privileges!\n");
437 printf(
"There is a problem for to open the Service Control Manager!\n");
441 if (OperationType == 1)
444 if ((service = OpenService(scm,szInternName, SERVICE_ALL_ACCESS )))
446 LPQUERY_SERVICE_CONFIG ConfigBuf;
449 ConfigBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
450 printf(
"The service already exists!\n");
451 if (QueryServiceConfig(service,ConfigBuf,4096,&dwSize))
452 printf(
"The current server installed: %s\n",
453 ConfigBuf->lpBinaryPathName);
454 LocalFree(ConfigBuf);
455 CloseServiceHandle(service);
463 if (!(service = OpenService(scm,szInternName, SERVICE_ALL_ACCESS )))
464 printf(
"The service doesn't exist!\n");
469 memset(&ss, 0,
sizeof(ss));
470 if (QueryServiceStatus(service,&ss))
472 DWORD dwState = ss.dwCurrentState;
473 if (dwState == SERVICE_RUNNING)
474 printf(
"Failed to remove the service because the service is running\nStop the service and try again\n");
475 else if (dwState == SERVICE_STOP_PENDING)
477 Failed to remove the service because the service is in stop pending state!\n\
478 Wait 30 seconds and try again.\n\
479 If this condition persist, reboot the machine and try again\n");
483 CloseServiceHandle(service);
486 CloseServiceHandle(scm);
493 BOOL NTService::IsService(LPCSTR ServiceName)
495 BOOL ret_value=FALSE;
496 SC_HANDLE service, scm;
498 if ((scm= OpenSCManager(0, 0,SC_MANAGER_ENUMERATE_SERVICE)))
500 if ((service = OpenService(scm,ServiceName, SERVICE_QUERY_STATUS)))
503 CloseServiceHandle(service);
505 CloseServiceHandle(scm);
511 BOOL NTService::got_service_option(
char **argv,
char *service_option)
514 for (option= argv[1]; *option; option++)
515 if (!strcmp(option, service_option))
521 BOOL NTService::is_super_user()
524 UCHAR InfoBuffer[1024];
525 PTOKEN_GROUPS ptgGroups=(PTOKEN_GROUPS)InfoBuffer;
526 DWORD dwInfoBufferSize;
527 PSID psidAdministrators;
528 SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
530 BOOL ret_value=FALSE;
532 if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE,&hAccessToken ))
534 if (GetLastError() != ERROR_NO_TOKEN)
537 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hAccessToken))
541 ret_value= GetTokenInformation(hAccessToken,TokenGroups,InfoBuffer,
542 1024, &dwInfoBufferSize);
544 CloseHandle(hAccessToken);
549 if (!AllocateAndInitializeSid(&siaNtAuthority, 2,
550 SECURITY_BUILTIN_DOMAIN_RID,
551 DOMAIN_ALIAS_RID_ADMINS,
553 &psidAdministrators))
558 for (x=0;x<ptgGroups->GroupCount;x++)
560 if ( EqualSid(psidAdministrators, ptgGroups->Groups[x].Sid) )
567 FreeSid(psidAdministrators);