657d6d786bd5354f4e8df7e2dfb052bf39e77fb6
[reactos.git] / reactos / subsys / system / sc / sc.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS SC utility
4 * FILE: subsys/system/sc/sc.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 SC_HANDLE hSCManager;
15
16 DWORD ReportLastError(VOID)
17 {
18 LPVOID lpMsgBuf;
19 DWORD RetVal;
20
21 DWORD ErrorCode = GetLastError();
22 if (ErrorCode != ERROR_SUCCESS)
23 {
24 if ((RetVal = FormatMessage(
25 FORMAT_MESSAGE_ALLOCATE_BUFFER |
26 FORMAT_MESSAGE_FROM_SYSTEM |
27 FORMAT_MESSAGE_IGNORE_INSERTS,
28 NULL,
29 ErrorCode,
30 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
31 (LPTSTR) &lpMsgBuf,
32 0,
33 NULL )))
34 {
35 _tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
36
37 LocalFree(lpMsgBuf);
38 /* return number of TCHAR's stored in output buffer
39 * excluding '\0' - as FormatMessage does*/
40 return RetVal;
41 }
42 }
43 return 0;
44 }
45
46
47 INT ScControl(LPTSTR MachineName, // remote machine name
48 LPCTSTR Command, // sc command
49 LPCTSTR ServiceName, // name of service
50 LPCTSTR *ServiceArgs, // any options
51 DWORD ArgCount) // argument counter
52 {
53 /* count trailing arguments */
54 ArgCount -= 3;
55
56 if (MachineName)
57 {
58 _tprintf(_T("Remote service control is not yet implemented\n"));
59 return 2;
60 }
61
62 hSCManager = OpenSCManager(MachineName, NULL, SC_MANAGER_ALL_ACCESS);
63 if (hSCManager == NULL)
64 {
65 _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
66 ReportLastError();
67 return -1;
68 }
69
70 /* emurate command */
71 if (_tcsicmp(Command, _T("query")) == 0)
72 Query(ServiceName, ServiceArgs, FALSE);
73
74 else if (_tcsicmp(Command, _T("queryex")) == 0)
75 Query(ServiceName, ServiceArgs, TRUE);
76
77 else if (_tcsicmp(Command, _T("start")) == 0)
78 {
79 if (ServiceName)
80 Start(ServiceName, ServiceArgs, ArgCount);
81 else
82 StartUsage();
83 }
84 else if (_tcsicmp(Command, _T("pause")) == 0)
85 {
86 if (ServiceName)
87 Control(SERVICE_CONTROL_PAUSE, ServiceName, ServiceArgs);
88 else
89 PauseUsage();
90 }
91 else if (_tcsicmp(Command, _T("interrogate")) == 0)
92 {
93 if (ServiceName)
94 Control(SERVICE_CONTROL_INTERROGATE, ServiceName, ServiceArgs);
95 else
96 InterrogateUsage();
97 }
98 else if (_tcsicmp(Command, _T("stop")) == 0)
99 {
100 if (ServiceName)
101 Control(SERVICE_CONTROL_STOP, ServiceName, ServiceArgs);
102 else
103 StopUsage();
104 }
105 else if (_tcsicmp(Command, _T("continue")) == 0)
106 {
107 if (ServiceName)
108 Control(SERVICE_CONTROL_CONTINUE, ServiceName, ServiceArgs);
109 else
110 ContinueUsage();
111 }
112 else if (_tcsicmp(Command, _T("delete")) == 0)
113 {
114 if (ServiceName)
115 Delete(ServiceName);
116 else
117 DeleteUsage();
118 }
119 else if (_tcsicmp(Command, _T("create")) == 0)
120 {
121 if (*ServiceArgs)
122 Create(ServiceName, ServiceArgs);
123 else
124 CreateUsage();
125 }
126 else if (_tcsicmp(Command, _T("control")) == 0)
127 {
128 if (ServiceName)
129 Control((DWORD)NULL, ServiceName, ServiceArgs);
130 else
131 ContinueUsage();
132 }
133 return 0;
134 }
135
136
137 int _tmain(int argc, LPCTSTR argv[])
138 {
139 LPTSTR MachineName = NULL; // remote machine
140 LPCTSTR Command = NULL; // sc command
141 LPCTSTR ServiceName = NULL; // Name of service
142
143 if (argc < 2)
144 return MainUsage();
145
146 /* get server name */
147 if ((argv[1][0] == '\\') && (argv[1][1] == '\\'))
148 {
149 if (argc < 3)
150 return MainUsage();
151
152 _tcscpy(MachineName, argv[1]);
153 Command = argv[2];
154 if (argc > 3)
155 ServiceName = argv[3];
156 return ScControl(MachineName, Command, ServiceName, &argv[4], argc);
157 }
158 else
159 {
160 Command = argv[1];
161 if (argc > 2)
162 ServiceName = argv[2];
163 return ScControl(MachineName, Command, ServiceName, &argv[3], argc);
164 }
165
166 return MainUsage();
167 }