Merge PR #283 "[USBPORT] Transaction Translator (TT) support bringup"
[reactos.git] / base / applications / sc / control.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/sc/control.c
5 * PURPOSE: Stops, pauses and resumes a service
6 * COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "sc.h"
11
12 BOOL
13 Control(DWORD Control,
14 LPCTSTR ServiceName,
15 LPCTSTR *Args,
16 INT ArgCount)
17 {
18 SC_HANDLE hSCManager = NULL;
19 SC_HANDLE hService = NULL;
20 SERVICE_STATUS Status;
21 DWORD dwDesiredAccess = 0;
22 BOOL bResult = TRUE;
23 SERVICE_STATUS_PROCESS StatusEx;
24
25 #ifdef SCDBG
26 LPCTSTR *TmpArgs = Args;
27 INT TmpCnt = ArgCount;
28 _tprintf(_T("service to control - %s\n"), ServiceName);
29 _tprintf(_T("command - %lu\n"), Control);
30 _tprintf(_T("Arguments:\n"));
31 while (TmpCnt)
32 {
33 _tprintf(_T(" %s\n"), *TmpArgs);
34 TmpArgs++;
35 TmpCnt--;
36 }
37 _tprintf(_T("\n"));
38 #endif /* SCDBG */
39
40 switch (Control)
41 {
42 case SERVICE_CONTROL_STOP:
43 dwDesiredAccess = SERVICE_STOP;
44 break;
45
46 case SERVICE_CONTROL_PAUSE:
47 case SERVICE_CONTROL_CONTINUE:
48 case SERVICE_CONTROL_PARAMCHANGE:
49 case SERVICE_CONTROL_NETBINDADD:
50 case SERVICE_CONTROL_NETBINDREMOVE:
51 case SERVICE_CONTROL_NETBINDENABLE:
52 case SERVICE_CONTROL_NETBINDDISABLE:
53 dwDesiredAccess = SERVICE_PAUSE_CONTINUE;
54 break;
55
56 case SERVICE_CONTROL_INTERROGATE:
57 dwDesiredAccess = SERVICE_INTERROGATE;
58 break;
59
60 default:
61 if (Control >= 128 && Control <= 255)
62 dwDesiredAccess = SERVICE_USER_DEFINED_CONTROL;
63 else
64 dwDesiredAccess = 0;
65 break;
66 }
67
68 hSCManager = OpenSCManager(NULL,
69 NULL,
70 SC_MANAGER_CONNECT);
71 if (hSCManager == NULL)
72 {
73 _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
74 bResult = FALSE;
75 goto done;
76 }
77
78 hService = OpenService(hSCManager,
79 ServiceName,
80 dwDesiredAccess);
81 if (hService == NULL)
82 {
83 _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
84 bResult = FALSE;
85 goto done;
86 }
87
88 if (!ControlService(hService,
89 Control,
90 &Status))
91 {
92 _tprintf(_T("[SC] ControlService FAILED %lu:\n\n"), GetLastError());
93 bResult = FALSE;
94 goto done;
95 }
96
97 /* FIXME: lazy hack ;) */
98 CopyMemory(&StatusEx, &Status, sizeof(Status));
99 StatusEx.dwProcessId = 0;
100 StatusEx.dwServiceFlags = 0;
101
102 PrintService(ServiceName,
103 NULL,
104 &StatusEx,
105 FALSE);
106
107 done:
108 if (!bResult)
109 ReportLastError();
110
111 if (hService)
112 CloseServiceHandle(hService);
113
114 if (hSCManager)
115 CloseServiceHandle(hSCManager);
116
117 return bResult;
118 }