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[3] = {NULL, NULL, NULL}; // StopEvent, UpdateEvent, Timer 41 42 43 /* FUNCTIONS *****************************************************************/ 44 45 static VOID 46 UpdateServiceStatus(DWORD dwState) 47 { 48 ServiceStatus.dwServiceType = SERVICE_WIN32_SHARE_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 Events[2] = CreateWaitableTimerW(NULL, FALSE, NULL); 185 if (Events[2] == NULL) 186 { 187 ERR("Could not create the timer\n"); 188 CloseHandle(Events[1]); 189 CloseHandle(Events[0]); 190 return GetLastError(); 191 } 192 193 return ERROR_SUCCESS; 194 } 195 196 197 VOID WINAPI 198 SchedServiceMain(DWORD argc, LPTSTR *argv) 199 { 200 DWORD dwWait, dwError; 201 202 UNREFERENCED_PARAMETER(argc); 203 UNREFERENCED_PARAMETER(argv); 204 205 TRACE("SchedServiceMain()\n"); 206 207 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName, 208 ServiceControlHandler, 209 NULL); 210 if (!ServiceStatusHandle) 211 { 212 ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError()); 213 return; 214 } 215 216 UpdateServiceStatus(SERVICE_START_PENDING); 217 218 dwError = ServiceInit(); 219 if (dwError != ERROR_SUCCESS) 220 { 221 ERR("Service stopped (dwError: %lu\n", dwError); 222 UpdateServiceStatus(SERVICE_STOPPED); 223 return; 224 } 225 226 UpdateServiceStatus(SERVICE_RUNNING); 227 228 GetNextJobTimeout(Events[2]); 229 230 for (;;) 231 { 232 /* Wait for the next event */ 233 TRACE("Wait for next event!\n"); 234 dwWait = WaitForMultipleObjects(3, Events, FALSE, INFINITE); 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 244 RtlAcquireResourceShared(&JobListLock, TRUE); 245 GetNextJobTimeout(Events[2]); 246 RtlReleaseResource(&JobListLock); 247 } 248 else if (dwWait == WAIT_OBJECT_0 + 2) 249 { 250 TRACE("Timeout: Start the next job!\n"); 251 252 RtlAcquireResourceExclusive(&JobListLock, TRUE); 253 RunCurrentJobs(); 254 GetNextJobTimeout(Events[2]); 255 RtlReleaseResource(&JobListLock); 256 } 257 } 258 259 /* Close the start and update event handles */ 260 CloseHandle(Events[0]); 261 CloseHandle(Events[1]); 262 CloseHandle(Events[2]); 263 264 /* Stop the service */ 265 UpdateServiceStatus(SERVICE_STOPPED); 266 } 267 268 269 BOOL WINAPI 270 DllMain(HINSTANCE hinstDLL, 271 DWORD fdwReason, 272 LPVOID lpvReserved) 273 { 274 switch (fdwReason) 275 { 276 case DLL_PROCESS_ATTACH: 277 DisableThreadLibraryCalls(hinstDLL); 278 break; 279 280 case DLL_PROCESS_DETACH: 281 break; 282 } 283 284 return TRUE; 285 } 286