xref: /reactos/base/services/wmisvc/wmisvc.c (revision 4561998a)
1 /*
2  *  ReactOS Services
3  *  Copyright (C) 2015 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 /*
20  * COPYRIGHT:        See COPYING in the top level directory
21  * PROJECT:          ReactOS Services
22  * FILE:             base/services/wmisvc/wmisvc.c
23  * PURPOSE:          WMI service
24  * PROGRAMMER:       Pierre Schweitzer
25  */
26 
27 /* INCLUDES *****************************************************************/
28 
29 #define WIN32_NO_STATUS
30 #define _INC_WINDOWS
31 #define COM_NO_WINDOWS_H
32 #include <stdarg.h>
33 #include <windef.h>
34 #include <winbase.h>
35 #include <winreg.h>
36 #include <winsvc.h>
37 
38 #define NDEBUG
39 #include <debug.h>
40 
41 /* GLOBALS ******************************************************************/
42 
43 static WCHAR ServiceName[] = L"winmgmt";
44 
45 static SERVICE_STATUS_HANDLE ServiceStatusHandle;
46 static SERVICE_STATUS ServiceStatus;
47 
48 /* FUNCTIONS *****************************************************************/
49 
50 static VOID
51 UpdateServiceStatus(DWORD dwState)
52 {
53     ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
54     ServiceStatus.dwCurrentState = dwState;
55     ServiceStatus.dwControlsAccepted = 0;
56     ServiceStatus.dwWin32ExitCode = 0;
57     ServiceStatus.dwServiceSpecificExitCode = 0;
58     ServiceStatus.dwCheckPoint = 0;
59 
60     if (dwState == SERVICE_START_PENDING ||
61         dwState == SERVICE_STOP_PENDING ||
62         dwState == SERVICE_PAUSE_PENDING ||
63         dwState == SERVICE_CONTINUE_PENDING)
64         ServiceStatus.dwWaitHint = 10000;
65     else
66         ServiceStatus.dwWaitHint = 0;
67 
68     SetServiceStatus(ServiceStatusHandle,
69                      &ServiceStatus);
70 }
71 
72 static DWORD WINAPI
73 ServiceControlHandler(DWORD dwControl,
74                       DWORD dwEventType,
75                       LPVOID lpEventData,
76                       LPVOID lpContext)
77 {
78     DPRINT1("ServiceControlHandler() called\n");
79 
80     switch (dwControl)
81     {
82         case SERVICE_CONTROL_STOP:
83             DPRINT1("  SERVICE_CONTROL_STOP received\n");
84             UpdateServiceStatus(SERVICE_STOPPED);
85             return ERROR_SUCCESS;
86 
87         case SERVICE_CONTROL_PAUSE:
88             DPRINT1("  SERVICE_CONTROL_PAUSE received\n");
89             UpdateServiceStatus(SERVICE_PAUSED);
90             return ERROR_SUCCESS;
91 
92         case SERVICE_CONTROL_CONTINUE:
93             DPRINT1("  SERVICE_CONTROL_CONTINUE received\n");
94             UpdateServiceStatus(SERVICE_RUNNING);
95             return ERROR_SUCCESS;
96 
97         case SERVICE_CONTROL_INTERROGATE:
98             DPRINT1("  SERVICE_CONTROL_INTERROGATE received\n");
99             SetServiceStatus(ServiceStatusHandle,
100                              &ServiceStatus);
101             return ERROR_SUCCESS;
102 
103         case SERVICE_CONTROL_SHUTDOWN:
104             DPRINT1("  SERVICE_CONTROL_SHUTDOWN received\n");
105             UpdateServiceStatus(SERVICE_STOPPED);
106             return ERROR_SUCCESS;
107 
108         default :
109             DPRINT1("  Control %lu received\n");
110             return ERROR_CALL_NOT_IMPLEMENTED;
111     }
112 }
113 
114 VOID WINAPI
115 ServiceMain(DWORD argc, LPTSTR *argv)
116 {
117     UNREFERENCED_PARAMETER(argc);
118     UNREFERENCED_PARAMETER(argv);
119 
120     DPRINT("ServiceMain() called\n");
121 
122     ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
123                                                         ServiceControlHandler,
124                                                         NULL);
125     if (!ServiceStatusHandle)
126     {
127         DPRINT1("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
128         return;
129     }
130 
131     UpdateServiceStatus(SERVICE_RUNNING);
132 
133     do
134     {
135         Sleep(1);
136     } while (1);
137 
138     UpdateServiceStatus(SERVICE_STOPPED);
139 }
140 
141 BOOL WINAPI
142 DllMain(HINSTANCE hinstDLL,
143         DWORD fdwReason,
144         LPVOID lpvReserved)
145 {
146     switch (fdwReason)
147     {
148         case DLL_PROCESS_ATTACH:
149             DisableThreadLibraryCalls(hinstDLL);
150             break;
151 
152         case DLL_PROCESS_DETACH:
153             break;
154     }
155 
156     return TRUE;
157 }
158