[SCHEDSVC]
[reactos.git] / reactos / base / services / schedsvc / schedsvc.c
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
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 /* FUNCTIONS *****************************************************************/
41
42 static VOID
43 UpdateServiceStatus(DWORD dwState)
44 {
45 ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
46 ServiceStatus.dwCurrentState = dwState;
47 ServiceStatus.dwControlsAccepted = 0;
48 ServiceStatus.dwWin32ExitCode = 0;
49 ServiceStatus.dwServiceSpecificExitCode = 0;
50 ServiceStatus.dwCheckPoint = 0;
51
52 if (dwState == SERVICE_START_PENDING ||
53 dwState == SERVICE_STOP_PENDING ||
54 dwState == SERVICE_PAUSE_PENDING ||
55 dwState == SERVICE_CONTINUE_PENDING)
56 ServiceStatus.dwWaitHint = 10000;
57 else
58 ServiceStatus.dwWaitHint = 0;
59
60 SetServiceStatus(ServiceStatusHandle,
61 &ServiceStatus);
62 }
63
64 static DWORD WINAPI
65 ServiceControlHandler(DWORD dwControl,
66 DWORD dwEventType,
67 LPVOID lpEventData,
68 LPVOID lpContext)
69 {
70 TRACE("ServiceControlHandler() called\n");
71
72 switch (dwControl)
73 {
74 case SERVICE_CONTROL_STOP:
75 TRACE(" SERVICE_CONTROL_STOP received\n");
76 UpdateServiceStatus(SERVICE_STOP_PENDING);
77 /* Stop listening to incoming RPC messages */
78 RpcMgmtStopServerListening(NULL);
79 UpdateServiceStatus(SERVICE_STOPPED);
80 return ERROR_SUCCESS;
81
82 case SERVICE_CONTROL_PAUSE:
83 TRACE(" SERVICE_CONTROL_PAUSE received\n");
84 UpdateServiceStatus(SERVICE_PAUSED);
85 return ERROR_SUCCESS;
86
87 case SERVICE_CONTROL_CONTINUE:
88 TRACE(" SERVICE_CONTROL_CONTINUE received\n");
89 UpdateServiceStatus(SERVICE_RUNNING);
90 return ERROR_SUCCESS;
91
92 case SERVICE_CONTROL_INTERROGATE:
93 TRACE(" SERVICE_CONTROL_INTERROGATE received\n");
94 SetServiceStatus(ServiceStatusHandle,
95 &ServiceStatus);
96 return ERROR_SUCCESS;
97
98 case SERVICE_CONTROL_SHUTDOWN:
99 TRACE(" SERVICE_CONTROL_SHUTDOWN received\n");
100 UpdateServiceStatus(SERVICE_STOP_PENDING);
101 UpdateServiceStatus(SERVICE_STOPPED);
102 return ERROR_SUCCESS;
103
104 default :
105 TRACE(" Control %lu received\n", dwControl);
106 return ERROR_CALL_NOT_IMPLEMENTED;
107 }
108 }
109
110
111 static
112 DWORD
113 ServiceInit(VOID)
114 {
115 HANDLE hThread;
116
117 hThread = CreateThread(NULL,
118 0,
119 (LPTHREAD_START_ROUTINE)RpcThreadRoutine,
120 NULL,
121 0,
122 NULL);
123
124 if (!hThread)
125 {
126 ERR("Can't create PortThread\n");
127 return GetLastError();
128 }
129 else
130 CloseHandle(hThread);
131
132 return ERROR_SUCCESS;
133 }
134
135
136 VOID WINAPI
137 SchedServiceMain(DWORD argc, LPTSTR *argv)
138 {
139 DWORD dwError;
140
141 UNREFERENCED_PARAMETER(argc);
142 UNREFERENCED_PARAMETER(argv);
143
144 TRACE("SchedServiceMain() called\n");
145
146 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
147 ServiceControlHandler,
148 NULL);
149 if (!ServiceStatusHandle)
150 {
151 ERR("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
152 return;
153 }
154
155 UpdateServiceStatus(SERVICE_START_PENDING);
156
157 dwError = ServiceInit();
158 if (dwError != ERROR_SUCCESS)
159 {
160 ERR("Service stopped (dwError: %lu\n", dwError);
161 UpdateServiceStatus(SERVICE_STOPPED);
162 return;
163 }
164
165 UpdateServiceStatus(SERVICE_RUNNING);
166 }
167
168
169 BOOL WINAPI
170 DllMain(HINSTANCE hinstDLL,
171 DWORD fdwReason,
172 LPVOID lpvReserved)
173 {
174 switch (fdwReason)
175 {
176 case DLL_PROCESS_ATTACH:
177 DisableThreadLibraryCalls(hinstDLL);
178 break;
179
180 case DLL_PROCESS_DETACH:
181 break;
182 }
183
184 return TRUE;
185 }