3 * Copyright (C) 2015 ReactOS Team
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.
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.
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.
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>
27 /* INCLUDES *****************************************************************/
31 WINE_DEFAULT_DEBUG_CHANNEL(schedsvc
);
33 /* GLOBALS ******************************************************************/
35 static WCHAR ServiceName
[] = L
"Schedule";
37 static SERVICE_STATUS_HANDLE ServiceStatusHandle
;
38 static SERVICE_STATUS ServiceStatus
;
40 static BOOL bStopService
= FALSE
;
42 /* FUNCTIONS *****************************************************************/
45 UpdateServiceStatus(DWORD dwState
)
47 ServiceStatus
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
48 ServiceStatus
.dwCurrentState
= dwState
;
50 if (dwState
== SERVICE_PAUSED
|| dwState
== SERVICE_RUNNING
)
51 ServiceStatus
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
|
52 SERVICE_ACCEPT_SHUTDOWN
|
53 SERVICE_ACCEPT_PAUSE_CONTINUE
;
55 ServiceStatus
.dwControlsAccepted
= 0;
57 ServiceStatus
.dwWin32ExitCode
= 0;
58 ServiceStatus
.dwServiceSpecificExitCode
= 0;
59 ServiceStatus
.dwCheckPoint
= 0;
61 if (dwState
== SERVICE_START_PENDING
||
62 dwState
== SERVICE_STOP_PENDING
||
63 dwState
== SERVICE_PAUSE_PENDING
||
64 dwState
== SERVICE_CONTINUE_PENDING
)
65 ServiceStatus
.dwWaitHint
= 10000;
67 ServiceStatus
.dwWaitHint
= 0;
69 SetServiceStatus(ServiceStatusHandle
,
75 ServiceControlHandler(DWORD dwControl
,
80 TRACE("ServiceControlHandler() called\n");
84 case SERVICE_CONTROL_STOP
:
85 case SERVICE_CONTROL_SHUTDOWN
:
86 TRACE(" SERVICE_CONTROL_STOP/SERVICE_CONTROL_SHUTDOWN received\n");
87 UpdateServiceStatus(SERVICE_STOP_PENDING
);
88 /* Stop listening to incoming RPC messages */
89 RpcMgmtStopServerListening(NULL
);
93 case SERVICE_CONTROL_PAUSE
:
94 TRACE(" SERVICE_CONTROL_PAUSE received\n");
95 UpdateServiceStatus(SERVICE_PAUSED
);
98 case SERVICE_CONTROL_CONTINUE
:
99 TRACE(" SERVICE_CONTROL_CONTINUE received\n");
100 UpdateServiceStatus(SERVICE_RUNNING
);
101 return ERROR_SUCCESS
;
103 case SERVICE_CONTROL_INTERROGATE
:
104 TRACE(" SERVICE_CONTROL_INTERROGATE received\n");
105 SetServiceStatus(ServiceStatusHandle
,
107 return ERROR_SUCCESS
;
110 TRACE(" Control %lu received\n", dwControl
);
111 return ERROR_CALL_NOT_IMPLEMENTED
;
118 ServiceInit(PHANDLE phEvent
)
123 /* Initialize the job list */
124 InitializeListHead(&JobListHead
);
126 /* Initialize the job list lock */
127 RtlInitializeResource(&JobListLock
);
129 /* Initialize the start list */
130 InitializeListHead(&StartListHead
);
132 /* Initialize the start list lock */
133 RtlInitializeResource(&StartListLock
);
135 /* Load stored jobs from the registry */
136 dwError
= LoadJobs();
137 if (dwError
!= ERROR_SUCCESS
)
140 /* Start the RPC thread */
141 hThread
= CreateThread(NULL
,
143 (LPTHREAD_START_ROUTINE
)RpcThreadRoutine
,
149 ERR("Could not create the RPC thread\n");
150 return GetLastError();
153 CloseHandle(hThread
);
155 /* Create the scheduler event */
156 *phEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
157 if (*phEvent
== NULL
)
159 ERR("Could not create the scheduler event\n");
160 return GetLastError();
163 return ERROR_SUCCESS
;
168 SchedServiceMain(DWORD argc
, LPTSTR
*argv
)
170 HANDLE hEvent
= NULL
;
173 UNREFERENCED_PARAMETER(argc
);
174 UNREFERENCED_PARAMETER(argv
);
176 TRACE("SchedServiceMain() called\n");
178 ServiceStatusHandle
= RegisterServiceCtrlHandlerExW(ServiceName
,
179 ServiceControlHandler
,
181 if (!ServiceStatusHandle
)
183 ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
187 UpdateServiceStatus(SERVICE_START_PENDING
);
189 dwError
= ServiceInit(&hEvent
);
190 if (dwError
!= ERROR_SUCCESS
)
192 ERR("Service stopped (dwError: %lu\n", dwError
);
193 UpdateServiceStatus(SERVICE_STOPPED
);
197 UpdateServiceStatus(SERVICE_RUNNING
);
201 /* Leave the loop, if the service has to be stopped */
205 /* Wait for the next timeout */
206 WaitForSingleObject(hEvent
, 5000);
207 TRACE("Service running!\n");
210 /* Close the scheduler event handle */
213 /* Stop the service */
214 UpdateServiceStatus(SERVICE_STOPPED
);
219 DllMain(HINSTANCE hinstDLL
,
225 case DLL_PROCESS_ATTACH
:
226 DisableThreadLibraryCalls(hinstDLL
);
229 case DLL_PROCESS_DETACH
: