fixed warnings when compiled with -Wwrite-strings
[reactos.git] / reactos / subsys / system / sc / query.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS SC utility
4 * FILE: subsys/system/sc/query.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 /* local function decs */
15 VOID PrintService(BOOL bExtended);
16 BOOL EnumServices(DWORD ServiceType, DWORD ServiceState);
17
18 /* global variables */
19 static ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
20
21
22 BOOL Query(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, BOOL bExtended)
23 {
24
25 if (! ServiceName)
26 {
27 /* display all running services and drivers */
28 _tprintf(_T("No service name, displaying all services\n")); // test
29
30 /* get default values */
31 EnumServices(SERVICE_WIN32, SERVICE_ACTIVE);
32
33 /* print default values */
34 PrintService(bExtended);
35 }
36 else if (_tcsicmp(ServiceName, _T("type=")) == 0)
37 {
38 LPCTSTR Type = *ServiceArgs;
39
40 _tprintf(_T("got type\narg = %s\n"), Type); // test
41 if (_tcsicmp(Type, _T("driver")) == 0)
42 EnumServices(SERVICE_DRIVER, SERVICE_STATE_ALL);
43 else if (_tcsicmp(Type, _T("service")) == 0)
44 EnumServices(SERVICE_WIN32, SERVICE_STATE_ALL);
45 else if (_tcsicmp(Type, _T("all")) == 0)
46 EnumServices(SERVICE_DRIVER|SERVICE_WIN32, SERVICE_STATE_ALL);
47 else
48 {
49 _tprintf(_T("\nERROR following \"type=\"!\n"));
50 _tprintf(_T("Must be \"driver\" or \"service\" or \"all\"\n"));
51 }
52
53 PrintService(bExtended);
54 }
55 else if(_tcsicmp(ServiceName, _T("state=")) == 0)
56 {
57 LPCTSTR State = *ServiceArgs;
58
59 if (_tcsicmp(State, _T("active")) == 0)
60 EnumServices(SERVICE_DRIVER|SERVICE_WIN32, SERVICE_ACTIVE);
61 else if (_tcsicmp(State, _T("inactive")) == 0)
62 EnumServices(SERVICE_DRIVER|SERVICE_WIN32, SERVICE_INACTIVE);
63 else if (_tcsicmp(State, _T("all")) == 0)
64 EnumServices(SERVICE_DRIVER|SERVICE_WIN32, SERVICE_STATE_ALL);
65 else
66 {
67 _tprintf(_T("\nERROR following \"state=\"!\n"));
68 _tprintf(_T("Must be \"active\" or \"inactive\" or \"all\"\n"));
69 }
70
71 PrintService(bExtended);
72 }
73 /*
74 else if(_tcsicmp(ServiceName, _T("bufsize=")))
75
76 else if(_tcsicmp(ServiceName, _T("ri=")))
77
78 else if(_tcsicmp(ServiceName, _T("group=")))
79 */
80 else
81 {
82 /* print only the service requested */
83 printf("Service name %s\n", ServiceName); // test
84
85 /* get default values */
86 EnumServices(SERVICE_WIN32, SERVICE_ACTIVE);
87
88 /* print default values */
89 PrintService(bExtended);
90 }
91
92 return TRUE;
93
94 }
95
96
97 BOOL EnumServices(DWORD ServiceType, DWORD ServiceState)
98 {
99 //SC_HANDLE hSc;
100 DWORD BufSize = 0;
101 DWORD BytesNeeded = 0;
102 DWORD NumServices = 0;
103 DWORD ResumeHandle = 0;
104
105 // hSc = OpenService(hSCManager, ServiceName, SERVICE_QUERY_STATUS);
106
107 /* determine required buffer size */
108 if (! EnumServicesStatusEx(hSCManager,
109 SC_ENUM_PROCESS_INFO,
110 ServiceType,
111 ServiceState,
112 (LPBYTE)pServiceStatus,
113 BufSize,
114 &BytesNeeded,
115 &NumServices,
116 &ResumeHandle,
117 0))
118 {
119 /* Call function again if required size was returned */
120 if (GetLastError() == ERROR_MORE_DATA)
121 {
122 /* reserve memory for service info array */
123 pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) malloc(BytesNeeded);
124
125 /* fill array with service info */
126 if (! EnumServicesStatusEx(hSCManager,
127 SC_ENUM_PROCESS_INFO,
128 SERVICE_DRIVER | SERVICE_WIN32,
129 SERVICE_STATE_ALL,
130 (LPBYTE)pServiceStatus,
131 BufSize,
132 &BytesNeeded,
133 &NumServices,
134 &ResumeHandle,
135 0))
136 {
137 _tprintf(_T("Second call to EnumServicesStatusEx failed : "));
138 ReportLastError();
139 return FALSE;
140 }
141 }
142 else /* exit on failure */
143 {
144 _tprintf(_T("First call to EnumServicesStatusEx failed : "));
145 ReportLastError();
146 return FALSE;
147 }
148 }
149
150 return TRUE;
151 }
152
153
154 VOID
155 PrintService(BOOL bExtended)
156 {
157 _tprintf(_T("SERVICE_NAME: %s\n"), pServiceStatus->lpServiceName);
158 _tprintf(_T("DISPLAY_NAME: %s\n"), pServiceStatus->lpDisplayName);
159 _tprintf(_T("TYPE : %lu\n"),
160 pServiceStatus->ServiceStatusProcess.dwServiceType);
161 _tprintf(_T("STATE : %lu\n"),
162 pServiceStatus->ServiceStatusProcess.dwCurrentState);
163 // (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
164 _tprintf(_T("WIN32_EXIT_CODE : %lu \n"),
165 pServiceStatus->ServiceStatusProcess.dwWin32ExitCode);
166 _tprintf(_T("SERVICE_EXIT_CODE : %lu \n"),
167 pServiceStatus->ServiceStatusProcess.dwServiceSpecificExitCode);
168 _tprintf(_T("CHECKPOINT : %lu\n"),
169 pServiceStatus->ServiceStatusProcess.dwCheckPoint);
170 _tprintf(_T("WAIT_HINT : %lu\n"),
171 pServiceStatus->ServiceStatusProcess.dwWaitHint);
172 if (bExtended)
173 {
174 _tprintf(_T("PID : %lu\n"),
175 pServiceStatus->ServiceStatusProcess.dwProcessId);
176 _tprintf(_T("FLAGS : %lu\n"),
177 pServiceStatus->ServiceStatusProcess.dwServiceFlags);
178 }
179 }