xref: /reactos/base/services/schedsvc/schedsvc.c (revision 7eead935)
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/schedsvc/schedsvc.c
23  * PURPOSE:          Scheduling service
24  * PROGRAMMER:       Eric Kohl <eric.kohl@reactos.org>
25  */
26 
27 /* INCLUDES *****************************************************************/
28 
29 #include "precomp.h"
30 
31 WINE_DEFAULT_DEBUG_CHANNEL(schedsvc);
32 
33 /* GLOBALS ******************************************************************/
34 
35 static WCHAR ServiceName[] = L"Schedule";
36 
37 static SERVICE_STATUS_HANDLE ServiceStatusHandle;
38 static SERVICE_STATUS ServiceStatus;
39 
40 HANDLE Events[2] = {NULL, NULL}; // StopEvent, UpdateEvent
41 
42 
43 /* FUNCTIONS *****************************************************************/
44 
45 static VOID
46 UpdateServiceStatus(DWORD dwState)
47 {
48     ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
49     ServiceStatus.dwCurrentState = dwState;
50 
51     if (dwState == SERVICE_PAUSED || dwState == SERVICE_RUNNING)
52         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
53                                            SERVICE_ACCEPT_SHUTDOWN |
54                                            SERVICE_ACCEPT_PAUSE_CONTINUE;
55     else
56         ServiceStatus.dwControlsAccepted = 0;
57 
58     ServiceStatus.dwWin32ExitCode = 0;
59     ServiceStatus.dwServiceSpecificExitCode = 0;
60     ServiceStatus.dwCheckPoint = 0;
61 
62     if (dwState == SERVICE_START_PENDING ||
63         dwState == SERVICE_STOP_PENDING ||
64         dwState == SERVICE_PAUSE_PENDING ||
65         dwState == SERVICE_CONTINUE_PENDING)
66         ServiceStatus.dwWaitHint = 10000;
67     else
68         ServiceStatus.dwWaitHint = 0;
69 
70     SetServiceStatus(ServiceStatusHandle,
71                      &ServiceStatus);
72 }
73 
74 
75 static DWORD WINAPI
76 ServiceControlHandler(DWORD dwControl,
77                       DWORD dwEventType,
78                       LPVOID lpEventData,
79                       LPVOID lpContext)
80 {
81     TRACE("ServiceControlHandler()\n");
82 
83     switch (dwControl)
84     {
85         case SERVICE_CONTROL_STOP:
86         case SERVICE_CONTROL_SHUTDOWN:
87             TRACE("  SERVICE_CONTROL_STOP/SERVICE_CONTROL_SHUTDOWN received\n");
88             UpdateServiceStatus(SERVICE_STOP_PENDING);
89             /* Stop listening to incoming RPC messages */
90             RpcMgmtStopServerListening(NULL);
91             if (Events[0] != NULL)
92                 SetEvent(Events[0]);
93             return ERROR_SUCCESS;
94 
95         case SERVICE_CONTROL_PAUSE:
96             TRACE("  SERVICE_CONTROL_PAUSE received\n");
97             UpdateServiceStatus(SERVICE_PAUSED);
98             return ERROR_SUCCESS;
99 
100         case SERVICE_CONTROL_CONTINUE:
101             TRACE("  SERVICE_CONTROL_CONTINUE received\n");
102             UpdateServiceStatus(SERVICE_RUNNING);
103             return ERROR_SUCCESS;
104 
105         case SERVICE_CONTROL_INTERROGATE:
106             TRACE("  SERVICE_CONTROL_INTERROGATE received\n");
107             SetServiceStatus(ServiceStatusHandle,
108                              &ServiceStatus);
109             return ERROR_SUCCESS;
110 
111 #if 0
112         case 128:
113             TRACE("  Start Shell control received\n");
114             return ERROR_SUCCESS;
115 
116         case 129:
117             TRACE("  Logoff control received\n");
118             return ERROR_SUCCESS;
119 #endif
120 
121         default:
122             TRACE("  Control %lu received\n", dwControl);
123             return ERROR_CALL_NOT_IMPLEMENTED;
124     }
125 }
126 
127 
128 static
129 DWORD
130 ServiceInit(VOID)
131 {
132     HANDLE hThread;
133     DWORD dwError;
134 
135     /* Initialize the job list */
136     InitializeListHead(&JobListHead);
137 
138     /* Initialize the job list lock */
139     RtlInitializeResource(&JobListLock);
140 
141     /* Initialize the start list */
142     InitializeListHead(&StartListHead);
143 
144     /* Initialize the start list lock */
145     RtlInitializeResource(&StartListLock);
146 
147     /* Load stored jobs from the registry */
148     dwError = LoadJobs();
149     if (dwError != ERROR_SUCCESS)
150         return dwError;
151 
152     /* Start the RPC thread */
153     hThread = CreateThread(NULL,
154                            0,
155                            (LPTHREAD_START_ROUTINE)RpcThreadRoutine,
156                            NULL,
157                            0,
158                            NULL);
159     if (!hThread)
160     {
161         ERR("Could not create the RPC thread\n");
162         return GetLastError();
163     }
164 
165     CloseHandle(hThread);
166 
167     /* Create the stop event */
168     Events[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
169     if (Events[0] == NULL)
170     {
171         ERR("Could not create the stop event\n");
172         return GetLastError();
173     }
174 
175     /* Create the update event */
176     Events[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
177     if (Events[1] == NULL)
178     {
179         ERR("Could not create the update event\n");
180         CloseHandle(Events[0]);
181         return GetLastError();
182     }
183 
184     return ERROR_SUCCESS;
185 }
186 
187 
188 VOID WINAPI
189 SchedServiceMain(DWORD argc, LPTSTR *argv)
190 {
191     DWORD dwWait, dwTimeout, dwError;
192 
193     UNREFERENCED_PARAMETER(argc);
194     UNREFERENCED_PARAMETER(argv);
195 
196     TRACE("SchedServiceMain()\n");
197 
198     ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
199                                                         ServiceControlHandler,
200                                                         NULL);
201     if (!ServiceStatusHandle)
202     {
203         ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
204         return;
205     }
206 
207     UpdateServiceStatus(SERVICE_START_PENDING);
208 
209     dwError = ServiceInit();
210     if (dwError != ERROR_SUCCESS)
211     {
212         ERR("Service stopped (dwError: %lu\n", dwError);
213         UpdateServiceStatus(SERVICE_STOPPED);
214         return;
215     }
216 
217     UpdateServiceStatus(SERVICE_RUNNING);
218 
219     dwTimeout = GetNextJobTimeout();
220 
221     for (;;)
222     {
223         /* Wait for the next event */
224         TRACE("Wait for next event!\n");
225         dwWait = WaitForMultipleObjects(2, Events, FALSE, dwTimeout);
226         if (dwWait == WAIT_OBJECT_0)
227         {
228             TRACE("Stop event signaled!\n");
229             break;
230         }
231         else if (dwWait == WAIT_OBJECT_0 + 1)
232         {
233             TRACE("Update event signaled!\n");
234 
235             RtlAcquireResourceShared(&JobListLock, TRUE);
236             dwTimeout = GetNextJobTimeout();
237             RtlReleaseResource(&JobListLock);
238         }
239         else if (dwWait == WAIT_TIMEOUT)
240         {
241             TRACE("Timeout: Start the next job!\n");
242 
243             RtlAcquireResourceExclusive(&JobListLock, TRUE);
244             RunNextJob();
245             dwTimeout = GetNextJobTimeout();
246             RtlReleaseResource(&JobListLock);
247         }
248     }
249 
250     /* Close the start and update event handles */
251     CloseHandle(Events[0]);
252     CloseHandle(Events[1]);
253 
254     /* Stop the service */
255     UpdateServiceStatus(SERVICE_STOPPED);
256 }
257 
258 
259 BOOL WINAPI
260 DllMain(HINSTANCE hinstDLL,
261         DWORD fdwReason,
262         LPVOID lpvReserved)
263 {
264     switch (fdwReason)
265     {
266         case DLL_PROCESS_ATTACH:
267             DisableThreadLibraryCalls(hinstDLL);
268             break;
269 
270         case DLL_PROCESS_DETACH:
271             break;
272     }
273 
274     return TRUE;
275 }
276