xref: /reactos/base/applications/sc/control.c (revision c2c66aff)
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             dwDesiredAccess = SERVICE_PAUSE_CONTINUE;
48             break;
49 
50         case SERVICE_CONTROL_CONTINUE:
51             dwDesiredAccess = SERVICE_PAUSE_CONTINUE;
52             break;
53 
54         case SERVICE_CONTROL_INTERROGATE:
55             dwDesiredAccess = SERVICE_INTERROGATE;
56             break;
57 
58         case SERVICE_CONTROL_SHUTDOWN:
59             dwDesiredAccess = 0;
60             break;
61 
62     }
63 
64     hSCManager = OpenSCManager(NULL,
65                                NULL,
66                                SC_MANAGER_CONNECT);
67     if (hSCManager == NULL)
68     {
69         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
70         bResult = FALSE;
71         goto done;
72     }
73 
74     hService = OpenService(hSCManager,
75                            ServiceName,
76                            dwDesiredAccess);
77     if (hService == NULL)
78     {
79         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
80         bResult = FALSE;
81         goto done;
82     }
83 
84     if (!ControlService(hService,
85                         Control,
86                         &Status))
87     {
88         _tprintf(_T("[SC] ControlService FAILED %lu:\n\n"), GetLastError());
89         bResult = FALSE;
90         goto done;
91     }
92 
93     /* FIXME: lazy hack ;) */
94     CopyMemory(&StatusEx, &Status, sizeof(Status));
95     StatusEx.dwProcessId = 0;
96     StatusEx.dwServiceFlags = 0;
97 
98     PrintService(ServiceName,
99                  &StatusEx,
100                  FALSE);
101 
102 done:
103     if (!bResult)
104         ReportLastError();
105 
106     if (hService)
107         CloseServiceHandle(hService);
108 
109     if (hSCManager)
110         CloseServiceHandle(hSCManager);
111 
112     return bResult;
113 }
114