1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Windows Service Control
21 #include <yaz/xmalloc.h>
24 #include <yaz/wrbuf.h>
33 int (*sc_main)(yaz_sc_t s, int argc, char **argv);
34 void (*sc_stop)(yaz_sc_t s);
38 SERVICE_STATUS_HANDLE gSvcStatusHandle;
39 SERVICE_STATUS gSvcStatus;
44 yaz_sc_t yaz_sc_create(const char *service_name, const char *display_name)
46 yaz_sc_t s = (yaz_sc_t) xmalloc(sizeof(*s));
48 s->service_name = service_name ? xstrdup(service_name) : 0;
49 s->display_name = display_name ? xstrdup(display_name) : 0;
57 s->gSvcStatusHandle = 0;
63 static void parse_args(yaz_sc_t s, int *argc_p, char ***argv_p)
66 const char *log_file = 0;
69 /* now look for the service arguments */
71 for (i = 1; i < *argc_p; i++)
73 const char *opt = (*argv_p)[i];
74 if (!strcmp(opt, "-install"))
80 else if (!strcmp(opt, "-installa"))
87 else if (!strcmp(opt, "-remove"))
93 else if (!strcmp(opt, "-run") && i < *argc_p-1)
96 const char *dir = (*argv_p)[i+1];
103 *argc_p = *argc_p - skip_opt;
104 for (; i < *argc_p; i++)
105 (*argv_p)[i] = (*argv_p)[i + skip_opt];
107 /* now look for the service arguments */
108 /* we must have a YAZ log file to work with */
110 for (i = 1; i < *argc_p; i++)
112 const char *opt = (*argv_p)[i];
113 if (opt[0] == '-' && opt[1] == 'l')
121 else if (i < *argc_p - 1)
123 log_file = (*argv_p)[i+1];
130 yaz_log_init_file(log_file);
135 yaz_log(YLOG_FATAL, "Must specify -l logfile for service to install");
140 { /* remove -l logfile for a running service */
141 *argc_p = *argc_p - skip_opt;
142 for (; i < *argc_p; i++)
143 (*argv_p)[i] = (*argv_p)[i + skip_opt];
148 VOID sc_ReportSvcStatus(yaz_sc_t s,
149 DWORD dwCurrentState,
150 DWORD dwWin32ExitCode,
153 if (s->gSvcStatusHandle)
155 static DWORD dwCheckPoint = 1;
157 // Fill in the SERVICE_STATUS structure.
159 s->gSvcStatus.dwCurrentState = dwCurrentState;
160 s->gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
161 s->gSvcStatus.dwWaitHint = dwWaitHint;
163 if (dwCurrentState == SERVICE_START_PENDING)
164 s->gSvcStatus.dwControlsAccepted = 0;
166 s->gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
168 if ( (dwCurrentState == SERVICE_RUNNING) ||
169 (dwCurrentState == SERVICE_STOPPED) )
170 s->gSvcStatus.dwCheckPoint = 0;
172 s->gSvcStatus.dwCheckPoint = dwCheckPoint++;
174 // Report the status of the service to the SCM.
175 SetServiceStatus(s->gSvcStatusHandle, &s->gSvcStatus );
179 static yaz_sc_t global_sc = 0;
181 VOID WINAPI sc_SvcCtrlHandler(DWORD dwCtrl)
185 case SERVICE_CONTROL_STOP:
186 yaz_log(YLOG_LOG, "Service %s to stop", global_sc->service_name);
187 sc_ReportSvcStatus(global_sc, SERVICE_STOP_PENDING, NO_ERROR, 0);
188 global_sc->sc_stop(global_sc);
189 sc_ReportSvcStatus(global_sc, SERVICE_STOPPED, NO_ERROR, 0);
191 case SERVICE_CONTROL_INTERROGATE:
198 static void WINAPI sc_service_main(DWORD argc, char **argv)
200 yaz_sc_t s = global_sc;
203 yaz_log(YLOG_LOG, "Service %s starting", s->service_name);
205 s->gSvcStatusHandle = RegisterServiceCtrlHandler(
206 s->service_name, sc_SvcCtrlHandler);
208 if (!s->gSvcStatusHandle)
210 yaz_log(YLOG_FATAL|YLOG_ERRNO, "RegisterServiceCtrlHandler");
214 s->gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
215 s->gSvcStatus.dwServiceSpecificExitCode = 0;
217 sc_ReportSvcStatus(s, SERVICE_START_PENDING, NO_ERROR, 3000);
219 ret_code = s->sc_main(s, s->argc, s->argv);
221 sc_ReportSvcStatus(s, SERVICE_STOPPED,
222 ret_code ? ERROR_SERVICE_SPECIFIC_ERROR : NO_ERROR, ret_code);
226 void yaz_sc_running(yaz_sc_t s)
229 sc_ReportSvcStatus(s, SERVICE_RUNNING, NO_ERROR, 0);
233 int yaz_sc_program(yaz_sc_t s, int argc, char **argv,
234 int (*sc_main)(yaz_sc_t s, int argc, char **argv),
235 void (*sc_stop)(yaz_sc_t s))
238 s->sc_main = sc_main;
239 s->sc_stop = sc_stop;
241 parse_args(s, &argc, &argv);
243 if (s->install_flag || s->remove_flag)
245 SC_HANDLE manager = OpenSCManager(NULL /* machine */,
247 SC_MANAGER_ALL_ACCESS);
250 yaz_log(YLOG_FATAL|YLOG_ERRNO, "OpenSCManager failed");
255 SC_HANDLE schService = 0;
258 WRBUF w = wrbuf_alloc();
259 char cwdstr[_MAX_PATH];
261 if (!_getcwd(cwdstr, sizeof(cwdstr)))
262 strcpy (cwdstr, ".");
264 if (GetModuleFileName(NULL, szPath, 2048) == 0)
266 yaz_log(YLOG_FATAL, "GetModuleFileName failed");
269 wrbuf_puts(w, szPath);
270 for (i = 1; i < argc; i++)
273 if (strchr(argv[i], ' '))
275 wrbuf_puts(w, argv[i]);
276 if (strchr(argv[i], ' '))
279 wrbuf_puts(w, " -run \"");
280 wrbuf_puts(w, cwdstr);
282 yaz_log(YLOG_LOG, "path: %s", wrbuf_cstr(w));
286 manager, /* SCM database */
287 TEXT(s->service_name), /* name of service */
288 TEXT(s->display_name), /* service name to display */
289 SERVICE_ALL_ACCESS, /* desired access */
290 SERVICE_WIN32_OWN_PROCESS, /* service type */
292 SERVICE_AUTO_START : SERVICE_DEMAND_START, /* start type */
293 SERVICE_ERROR_NORMAL, /* error control type */
294 wrbuf_cstr(w), /* path to service's binary */
295 NULL, /* no load ordering group */
296 NULL, /* no tag identifier */
297 NULL, /* no dependencies */
298 NULL, /* LocalSystem account */
299 NULL); /* no password */
300 if (schService == NULL)
302 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be installed",
304 CloseServiceHandle(manager);
307 yaz_log(YLOG_LOG, "Installed service %s", s->service_name);
308 CloseServiceHandle(schService);
311 else if (s->remove_flag)
313 SC_HANDLE schService = 0;
314 SERVICE_STATUS serviceStatus;
316 schService = OpenService(manager, TEXT(s->service_name), SERVICE_ALL_ACCESS);
317 if (schService == NULL)
319 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be opened",
321 CloseServiceHandle(manager);
324 /* try to stop the service */
325 if (ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus))
327 yaz_log(YLOG_LOG, "Service %s being stopped", s->service_name);
329 while (QueryServiceStatus(schService, &serviceStatus))
331 if (serviceStatus.dwCurrentState != SERVICE_STOP_PENDING)
335 if (serviceStatus.dwCurrentState != SERVICE_STOPPED)
336 yaz_log(YLOG_LOG|YLOG_FATAL, "Service failed to stop");
338 if (DeleteService(schService))
339 yaz_log(YLOG_LOG, "Service %s removed", s->service_name);
341 yaz_log(YLOG_FATAL, "Service %s could not be removed", s->service_name);
342 CloseServiceHandle(schService);
344 CloseServiceHandle(manager);
350 SERVICE_TABLE_ENTRY dt[2];
352 dt[0].lpServiceName = s->service_name;
353 dt[0].lpServiceProc = sc_service_main;
354 dt[1].lpServiceName = 0;
355 dt[1].lpServiceProc = 0;
359 if (!StartServiceCtrlDispatcher(dt))
361 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be controlled",
367 /* run the program standalone (with no service) */
368 return s->sc_main(s, argc, argv);
371 void yaz_sc_destroy(yaz_sc_t *s)
373 xfree((*s)->service_name);
374 xfree((*s)->display_name);
382 * c-file-style: "Stroustrup"
383 * indent-tabs-mode: nil
385 * vim: shiftwidth=4 tabstop=8 expandtab