ReactOS  0.4.11-dev-946-g431643b
schedsvc.c
Go to the documentation of this file.
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 
32 
33 /* GLOBALS ******************************************************************/
34 
35 static WCHAR ServiceName[] = L"Schedule";
36 
39 
40 HANDLE Events[2] = {NULL, NULL}; // StopEvent, UpdateEvent
41 
42 
43 /* FUNCTIONS *****************************************************************/
44 
45 static VOID
47 {
49  ServiceStatus.dwCurrentState = dwState;
50 
51  if (dwState == SERVICE_PAUSED || dwState == SERVICE_RUNNING)
52  ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
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 
71  &ServiceStatus);
72 }
73 
74 
75 static DWORD WINAPI
77  DWORD dwEventType,
78  LPVOID lpEventData,
79  LPVOID lpContext)
80 {
81  TRACE("ServiceControlHandler()\n");
82 
83  switch (dwControl)
84  {
87  TRACE(" SERVICE_CONTROL_STOP/SERVICE_CONTROL_SHUTDOWN received\n");
89  /* Stop listening to incoming RPC messages */
91  if (Events[0] != NULL)
92  SetEvent(Events[0]);
93  return ERROR_SUCCESS;
94 
96  TRACE(" SERVICE_CONTROL_PAUSE received\n");
98  return ERROR_SUCCESS;
99 
101  TRACE(" SERVICE_CONTROL_CONTINUE received\n");
103  return ERROR_SUCCESS;
104 
106  TRACE(" SERVICE_CONTROL_INTERROGATE received\n");
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);
124  }
125 }
126 
127 
128 static
129 DWORD
131 {
132  HANDLE hThread;
133  DWORD dwError;
134 
135  /* Initialize the job list */
137 
138  /* Initialize the job list lock */
140 
141  /* Initialize the start list */
143 
144  /* Initialize the start list lock */
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,
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
190 {
191  DWORD dwWait, dwTimeout, dwError;
192 
195 
196  TRACE("SchedServiceMain()\n");
197 
200  NULL);
201  if (!ServiceStatusHandle)
202  {
203  ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
204  return;
205  }
206 
208 
209  dwError = ServiceInit();
210  if (dwError != ERROR_SUCCESS)
211  {
212  ERR("Service stopped (dwError: %lu\n", dwError);
214  return;
215  }
216 
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 
236  dwTimeout = GetNextJobTimeout();
238  }
239  else if (dwWait == WAIT_TIMEOUT)
240  {
241  TRACE("Timeout: Start the next job!\n");
242 
244  RunNextJob();
245  dwTimeout = GetNextJobTimeout();
247  }
248  }
249 
250  /* Close the start and update event handles */
251  CloseHandle(Events[0]);
252  CloseHandle(Events[1]);
253 
254  /* Stop the service */
256 }
257 
258 
259 BOOL WINAPI
261  DWORD fdwReason,
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 }
#define CreateEvent
Definition: winbase.h:3562
RTL_RESOURCE JobListLock
Definition: job.c:30
static int argc
Definition: ServiceArgs.c:12
DWORD(WINAPI * LPTHREAD_START_ROUTINE)(LPVOID)
Definition: winbase.h:707
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
RPC_STATUS WINAPI RpcMgmtStopServerListening(RPC_BINDING_HANDLE Binding)
Definition: rpc_server.c:1599
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD dwCurrentState
Definition: winsvc.h:100
#define SERVICE_PAUSE_PENDING
Definition: winsvc.h:26
VOID WINAPI SchedServiceMain(DWORD argc, LPTSTR *argv)
Definition: schedsvc.c:189
VOID RunNextJob(VOID)
Definition: job.c:121
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define SERVICE_PAUSED
Definition: winsvc.h:27
NTSYSAPI BOOLEAN NTAPI RtlAcquireResourceShared(_In_ PRTL_RESOURCE Resource, _In_ BOOLEAN Wait)
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define SERVICE_START_PENDING
Definition: winsvc.h:22
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
static SERVICE_STATUS ServiceStatus
Definition: schedsvc.c:38
static WCHAR ServiceName[]
Definition: schedsvc.c:35
LIST_ENTRY StartListHead
Definition: job.c:32
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
NTSYSAPI VOID NTAPI RtlReleaseResource(_In_ PRTL_RESOURCE Resource)
LIST_ENTRY JobListHead
Definition: job.c:29
static char ** argv
Definition: ServiceArgs.c:11
CHAR * LPTSTR
Definition: xmlstorage.h:192
static IN DWORD IN LPVOID lpvReserved
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:957
DWORD dwCheckPoint
Definition: winsvc.h:104
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_RUNNING
Definition: winsvc.h:24
DWORD WINAPI RpcThreadRoutine(LPVOID lpParameter)
Definition: rpc.c:26
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:112
smooth NULL
Definition: ftsmooth.c:416
DWORD GetNextJobTimeout(VOID)
Definition: job.c:39
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
#define DLL_PROCESS_DETACH
Definition: compat.h:119
HANDLE Events[2]
Definition: schedsvc.c:40
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
NTSYSAPI VOID NTAPI RtlInitializeResource(_In_ PRTL_RESOURCE Resource)
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define TRACE(s)
Definition: solgame.cpp:4
unsigned int BOOL
Definition: ntddk_ex.h:94
#define WAIT_OBJECT_0
Definition: winbase.h:387
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwWin32ExitCode
Definition: winsvc.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwServiceType
Definition: winsvc.h:99
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Definition: schedsvc.c:260
static SERVICE_STATUS_HANDLE ServiceStatusHandle
Definition: schedsvc.c:37
#define WAIT_TIMEOUT
Definition: dderror.h:14
RTL_RESOURCE StartListLock
Definition: job.c:33
#define SERVICE_CONTROL_PAUSE
Definition: winsvc.h:37
static const WCHAR L[]
Definition: oid.c:1087
WINE_DEFAULT_DEBUG_CHANNEL(schedsvc)
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext)
Definition: sctrl.c:773
#define WINAPI
Definition: msvc.h:20
#define ERR(fmt,...)
Definition: debug.h:109
_In_ void _In_ PCCERT_CONTEXT _In_opt_ LPFILETIME _In_ DWORD _In_ DWORD dwTimeout
Definition: wincrypt.h:6079
DWORD dwControlsAccepted
Definition: winsvc.h:101
#define SERVICE_CONTINUE_PENDING
Definition: winsvc.h:25
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define SERVICE_CONTROL_CONTINUE
Definition: winsvc.h:38
HANDLE hThread
Definition: wizard.c:27
static DWORD WINAPI ServiceControlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: schedsvc.c:76
static DWORD ServiceInit(VOID)
Definition: schedsvc.c:130
LONG LoadJobs(VOID)
Definition: job.c:315
static VOID UpdateServiceStatus(DWORD dwState)
Definition: schedsvc.c:46
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
NTSYSAPI BOOLEAN NTAPI RtlAcquireResourceExclusive(_In_ PRTL_RESOURCE Resource, _In_ BOOLEAN Wait)