move network tools
[reactos.git] / reactos / subsys / system / sc / start.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS SC utility
4 * FILE: subsys/system/sc/start.c
5 * PURPOSE: control ReactOS services
6 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
7 * REVISIONS:
8 * Ged Murphy 20/10/05 Created
9 *
10 */
11
12 #include "sc.h"
13
14 BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
15 {
16 SC_HANDLE hSc;
17 SERVICE_STATUS_PROCESS ServiceStatus, ServiceStatus2;
18 DWORD BytesNeeded;
19
20 #ifdef SCDBG
21 /* testing */
22 _tprintf(_T("service to start - %s\n\n"), ServiceName);
23 _tprintf(_T("Arguments :\n"));
24 while (*ServiceArgs)
25 {
26 printf("%s\n", *ServiceArgs);
27 ServiceArgs++;
28 }
29 #endif
30
31 /* get a handle to the service requested for starting */
32 hSc = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS);
33
34 if (hSc == NULL)
35 {
36 _tprintf(_T("openService failed\n"));
37 ReportLastError();
38 return FALSE;
39 }
40
41 /* start the service opened */
42 if (! StartService(hSc, ArgCount, ServiceArgs))
43 {
44 _tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError());
45 ReportLastError();
46 return FALSE;
47 }
48
49 if (! QueryServiceStatusEx(
50 hSc,
51 SC_STATUS_PROCESS_INFO,
52 (LPBYTE)&ServiceStatus,
53 sizeof(SERVICE_STATUS_PROCESS),
54 &BytesNeeded))
55 {
56 _tprintf(_T("QueryServiceStatusEx 1 failed\n"));
57 ReportLastError();
58 return FALSE;
59 }
60
61
62 while (ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
63 {
64 /* wait before checking status */
65 Sleep(ServiceStatus.dwWaitHint);
66
67 /* check status again */
68 if (! QueryServiceStatusEx(
69 hSc,
70 SC_STATUS_PROCESS_INFO,
71 (LPBYTE)&ServiceStatus,
72 sizeof(SERVICE_STATUS_PROCESS),
73 &BytesNeeded))
74 {
75 _tprintf(_T("QueryServiceStatusEx 2 failed\n"));
76 ReportLastError();
77 return FALSE;
78 }
79 }
80
81 QueryServiceStatusEx(hSc, SC_STATUS_PROCESS_INFO, (LPBYTE)&ServiceStatus2,
82 sizeof(SERVICE_STATUS_PROCESS), &BytesNeeded);
83
84 CloseServiceHandle(hSc);
85
86 if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
87 {
88 _tprintf(_T("\nSERVICE_NAME: %s\n"), ServiceName);
89 _tprintf(_T("\tTYPE : %lu\n"), ServiceStatus2.dwServiceType);
90 _tprintf(_T("\tSTATE : %lu\n"), ServiceStatus2.dwCurrentState);
91 _tprintf(_T("\tWIN32_EXIT_CODE : %lu\n"), ServiceStatus2.dwWin32ExitCode);
92 _tprintf(_T("\tCHECKPOINT : %lu\n"), ServiceStatus2.dwCheckPoint);
93 _tprintf(_T("\tWAIT_HINT : %lu\n"), ServiceStatus2.dwWaitHint);
94 _tprintf(_T("\tPID : %lu\n"), ServiceStatus2.dwProcessId);
95 _tprintf(_T("\tFLAGS : %lu\n"), ServiceStatus2.dwServiceFlags);
96
97 return TRUE;
98 }
99 else
100 {
101 _tprintf(_T("Failed to start %s\n"), ServiceName);
102 _tprintf(_T("Curent state: %lu\n"), ServiceStatus.dwCurrentState);
103 _tprintf(_T("Exit code: %lu\n"), ServiceStatus.dwWin32ExitCode);
104 _tprintf(_T("Service Specific exit code: %lu\n"),
105 ServiceStatus.dwServiceSpecificExitCode);
106 _tprintf(_T("Check point: %lu\n"), ServiceStatus.dwCheckPoint);
107 _tprintf(_T("Wait hint: %lu\n"), ServiceStatus.dwWaitHint);
108
109 return FALSE;
110 }
111 }