ReactOS 0.4.15-dev-7704-gc07eb9f
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
35static WCHAR ServiceName[] = L"Schedule";
36
39
40HANDLE Events[3] = {NULL, NULL, NULL}; // StopEvent, UpdateEvent, Timer
41
42
43/* FUNCTIONS *****************************************************************/
44
45static VOID
47{
50
51 if (dwState == SERVICE_PAUSED || dwState == SERVICE_RUNNING)
55 else
57
61
62 if (dwState == SERVICE_START_PENDING ||
63 dwState == SERVICE_STOP_PENDING ||
64 dwState == SERVICE_PAUSE_PENDING ||
65 dwState == SERVICE_CONTINUE_PENDING)
67 else
69
72}
73
74
75static 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");
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
128static
129DWORD
131{
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 */
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
166
167 /* Create the stop event */
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 */
177 if (Events[1] == NULL)
178 {
179 ERR("Could not create the update event\n");
181 return GetLastError();
182 }
183
185 if (Events[2] == NULL)
186 {
187 ERR("Could not create the timer\n");
190 return GetLastError();
191 }
192
193 return ERROR_SUCCESS;
194}
195
196
199{
200 DWORD dwWait, dwError;
201
204
205 TRACE("SchedServiceMain()\n");
206
209 NULL);
211 {
212 ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
213 return;
214 }
215
217
218 dwError = ServiceInit();
219 if (dwError != ERROR_SUCCESS)
220 {
221 ERR("Service stopped (dwError: %lu\n", dwError);
223 return;
224 }
225
227
229
230 for (;;)
231 {
232 /* Wait for the next event */
233 TRACE("Wait for next event!\n");
235 if (dwWait == WAIT_OBJECT_0)
236 {
237 TRACE("Stop event signaled!\n");
238 break;
239 }
240 else if (dwWait == WAIT_OBJECT_0 + 1)
241 {
242 TRACE("Update event signaled!\n");
243
247 }
248 else if (dwWait == WAIT_OBJECT_0 + 2)
249 {
250 TRACE("Timeout: Start the next job!\n");
251
256 }
257 }
258
259 /* Close the start and update event handles */
263
264 /* Stop the service */
266}
267
268
271 DWORD fdwReason,
273{
274 switch (fdwReason)
275 {
278 break;
279
281 break;
282 }
283
284 return TRUE;
285}
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
static int argc
Definition: ServiceArgs.c:12
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
DWORD WINAPI RpcThreadRoutine(LPVOID lpParameter)
Definition: rpcserver.c:20
LIST_ENTRY JobListHead
Definition: job.c:29
RTL_RESOURCE JobListLock
Definition: job.c:30
LONG LoadJobs(VOID)
Definition: job.c:318
VOID GetNextJobTimeout(HANDLE hTimer)
Definition: job.c:44
RTL_RESOURCE StartListLock
Definition: job.c:33
LIST_ENTRY StartListHead
Definition: job.c:32
VOID RunCurrentJobs(VOID)
Definition: job.c:122
static WCHAR ServiceName[]
Definition: schedsvc.c:35
HANDLE Events[3]
Definition: schedsvc.c:40
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Definition: schedsvc.c:270
static DWORD ServiceInit(VOID)
Definition: schedsvc.c:130
static DWORD WINAPI ServiceControlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: schedsvc.c:76
static VOID UpdateServiceStatus(DWORD dwState)
Definition: schedsvc.c:46
static SERVICE_STATUS_HANDLE ServiceStatusHandle
Definition: schedsvc.c:37
static SERVICE_STATUS ServiceStatus
Definition: schedsvc.c:38
VOID WINAPI SchedServiceMain(DWORD argc, LPTSTR *argv)
Definition: schedsvc.c:198
#define ERR(fmt,...)
Definition: debug.h:110
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define DLL_PROCESS_DETACH
Definition: compat.h:130
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
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:137
#define INFINITE
Definition: serial.h:102
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
static IN DWORD IN LPVOID lpvReserved
#define argv
Definition: mplay32.c:18
NTSYSAPI BOOLEAN NTAPI RtlAcquireResourceShared(_In_ PRTL_RESOURCE Resource, _In_ BOOLEAN Wait)
NTSYSAPI VOID NTAPI RtlInitializeResource(_In_ PRTL_RESOURCE Resource)
NTSYSAPI BOOLEAN NTAPI RtlAcquireResourceExclusive(_In_ PRTL_RESOURCE Resource, _In_ BOOLEAN Wait)
NTSYSAPI VOID NTAPI RtlReleaseResource(_In_ PRTL_RESOURCE Resource)
HANDLE hThread
Definition: wizard.c:28
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define L(x)
Definition: ntvdm.h:50
RPC_STATUS WINAPI RpcMgmtStopServerListening(RPC_BINDING_HANDLE Binding)
Definition: rpc_server.c:1596
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext)
Definition: sctrl.c:812
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
#define TRACE(s)
Definition: solgame.cpp:4
DWORD dwServiceType
Definition: winsvc.h:99
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwControlsAccepted
Definition: winsvc.h:101
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwCurrentState
Definition: winsvc.h:100
DWORD dwCheckPoint
Definition: winsvc.h:104
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
HANDLE WINAPI CreateWaitableTimerW(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL, IN BOOL bManualReset, IN LPCWSTR lpTimerName OPTIONAL)
Definition: synch.c:331
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define CreateEvent
Definition: winbase.h:3683
#define WAIT_OBJECT_0
Definition: winbase.h:406
DWORD(WINAPI * LPTHREAD_START_ROUTINE)(LPVOID)
Definition: winbase.h:729
#define WINAPI
Definition: msvc.h:6
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define SERVICE_PAUSED
Definition: winsvc.h:27
#define SERVICE_START_PENDING
Definition: winsvc.h:22
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define SERVICE_CONTROL_CONTINUE
Definition: winsvc.h:38
#define SERVICE_ACCEPT_PAUSE_CONTINUE
Definition: winsvc.h:29
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define SERVICE_PAUSE_PENDING
Definition: winsvc.h:26
#define SERVICE_CONTROL_PAUSE
Definition: winsvc.h:37
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39
#define SERVICE_CONTINUE_PENDING
Definition: winsvc.h:25
#define SERVICE_WIN32_SHARE_PROCESS
Definition: cmtypes.h:963
__wchar_t WCHAR
Definition: xmlstorage.h:180
CHAR * LPTSTR
Definition: xmlstorage.h:192