1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
14 #include <yaz/xmalloc.h>
17 #include <yaz/wrbuf.h>
26 int (*sc_main)(yaz_sc_t s, int argc, char **argv);
27 void (*sc_stop)(yaz_sc_t s);
31 SERVICE_STATUS_HANDLE gSvcStatusHandle;
32 SERVICE_STATUS gSvcStatus;
37 yaz_sc_t yaz_sc_create(const char *service_name, const char *display_name)
39 yaz_sc_t s = xmalloc(sizeof(*s));
41 s->service_name = service_name ? xstrdup(service_name) : 0;
42 s->display_name = display_name ? xstrdup(display_name) : 0;
50 s->gSvcStatusHandle = 0;
56 static void parse_args(yaz_sc_t s, int *argc_p, char ***argv_p)
59 const char *log_file = 0;
62 /* now look for the service arguments */
64 for (i = 1; i < *argc_p; i++)
66 const char *opt = (*argv_p)[i];
67 if (!strcmp(opt, "-install"))
73 else if (!strcmp(opt, "-installa"))
80 else if (!strcmp(opt, "-remove"))
86 else if (!strcmp(opt, "-run") && i < *argc_p-1)
89 const char *dir = (*argv_p)[i+1];
96 *argc_p = *argc_p - skip_opt;
97 for (; i < *argc_p; i++)
98 (*argv_p)[i] = (*argv_p)[i + skip_opt];
100 /* now look for the service arguments */
101 /* we must have a YAZ log file to work with */
103 for (i = 1; i < *argc_p; i++)
105 const char *opt = (*argv_p)[i];
106 if (opt[0] == '-' && opt[1] == 'l')
114 else if (i < *argc_p - 1)
116 log_file = (*argv_p)[i+1];
123 yaz_log_init_file(log_file);
128 yaz_log(YLOG_FATAL, "Must specify -l logfile for service to install");
133 { /* remove -l logfile for a running service */
134 *argc_p = *argc_p - skip_opt;
135 for (; i < *argc_p; i++)
136 (*argv_p)[i] = (*argv_p)[i + skip_opt];
141 VOID sc_ReportSvcStatus(yaz_sc_t s,
142 DWORD dwCurrentState,
143 DWORD dwWin32ExitCode,
146 if (s->gSvcStatusHandle)
148 static DWORD dwCheckPoint = 1;
150 // Fill in the SERVICE_STATUS structure.
152 s->gSvcStatus.dwCurrentState = dwCurrentState;
153 s->gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
154 s->gSvcStatus.dwWaitHint = dwWaitHint;
156 if (dwCurrentState == SERVICE_START_PENDING)
157 s->gSvcStatus.dwControlsAccepted = 0;
159 s->gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
161 if ( (dwCurrentState == SERVICE_RUNNING) ||
162 (dwCurrentState == SERVICE_STOPPED) )
163 s->gSvcStatus.dwCheckPoint = 0;
165 s->gSvcStatus.dwCheckPoint = dwCheckPoint++;
167 // Report the status of the service to the SCM.
168 SetServiceStatus(s->gSvcStatusHandle, &s->gSvcStatus );
172 static yaz_sc_t global_sc = 0;
174 VOID WINAPI sc_SvcCtrlHandler(DWORD dwCtrl)
178 case SERVICE_CONTROL_STOP:
179 yaz_log(YLOG_LOG, "Service %s to stop", global_sc->service_name);
180 sc_ReportSvcStatus(global_sc, SERVICE_STOP_PENDING, NO_ERROR, 0);
181 global_sc->sc_stop(global_sc);
182 sc_ReportSvcStatus(global_sc, SERVICE_STOPPED, NO_ERROR, 0);
184 case SERVICE_CONTROL_INTERROGATE:
191 static void WINAPI sc_service_main(DWORD argc, char **argv)
193 yaz_sc_t s = global_sc;
196 yaz_log(YLOG_LOG, "Service %s starting", s->service_name);
198 s->gSvcStatusHandle = RegisterServiceCtrlHandler(
199 s->service_name, sc_SvcCtrlHandler);
201 if (!s->gSvcStatusHandle)
203 yaz_log(YLOG_FATAL|YLOG_ERRNO, "RegisterServiceCtrlHandler");
207 s->gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
208 s->gSvcStatus.dwServiceSpecificExitCode = 0;
210 sc_ReportSvcStatus(s, SERVICE_START_PENDING, NO_ERROR, 3000);
212 ret_code = s->sc_main(s, s->argc, s->argv);
214 sc_ReportSvcStatus(s, SERVICE_STOPPED,
215 ret_code ? ERROR_SERVICE_SPECIFIC_ERROR : NO_ERROR, ret_code);
219 void yaz_sc_running(yaz_sc_t s)
222 sc_ReportSvcStatus(s, SERVICE_RUNNING, NO_ERROR, 0);
226 int yaz_sc_program(yaz_sc_t s, int argc, char **argv,
227 int (*sc_main)(yaz_sc_t s, int argc, char **argv),
228 void (*sc_stop)(yaz_sc_t s))
231 s->sc_main = sc_main;
232 s->sc_stop = sc_stop;
234 parse_args(s, &argc, &argv);
236 if (s->install_flag || s->remove_flag)
238 SC_HANDLE manager = OpenSCManager(NULL /* machine */,
240 SC_MANAGER_ALL_ACCESS);
243 yaz_log(YLOG_FATAL|YLOG_ERRNO, "OpenSCManager failed");
248 SC_HANDLE schService = 0;
251 WRBUF w = wrbuf_alloc();
252 char cwdstr[_MAX_PATH];
254 if (!_getcwd(cwdstr, sizeof(cwdstr)))
255 strcpy (cwdstr, ".");
257 if (GetModuleFileName(NULL, szPath, 2048) == 0)
259 yaz_log(YLOG_FATAL, "GetModuleFileName failed");
262 wrbuf_puts(w, szPath);
263 for (i = 1; i < argc; i++)
266 wrbuf_puts(w, argv[i]);
268 wrbuf_puts(w, " -run \"");
269 wrbuf_puts(w, cwdstr);
271 yaz_log(YLOG_LOG, "path: %s", wrbuf_cstr(w));
275 manager, /* SCM database */
276 TEXT(s->service_name), /* name of service */
277 TEXT(s->display_name), /* service name to display */
278 SERVICE_ALL_ACCESS, /* desired access */
279 SERVICE_WIN32_OWN_PROCESS, /* service type */
281 SERVICE_AUTO_START : SERVICE_DEMAND_START, /* start type */
282 SERVICE_ERROR_NORMAL, /* error control type */
283 wrbuf_cstr(w), /* path to service's binary */
284 NULL, /* no load ordering group */
285 NULL, /* no tag identifier */
286 NULL, /* no dependencies */
287 NULL, /* LocalSystem account */
288 NULL); /* no password */
289 if (schService == NULL)
291 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be installed",
293 CloseServiceHandle(manager);
296 yaz_log(YLOG_LOG, "Installed service %s", s->service_name);
297 CloseServiceHandle(schService);
300 else if (s->remove_flag)
302 SC_HANDLE schService = 0;
303 SERVICE_STATUS serviceStatus;
305 schService = OpenService(manager, TEXT(s->service_name), SERVICE_ALL_ACCESS);
306 if (schService == NULL)
308 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be opened",
310 CloseServiceHandle(manager);
313 /* try to stop the service */
314 if (ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus))
316 yaz_log(YLOG_LOG, "Service %s being stopped", s->service_name);
318 while (QueryServiceStatus(schService, &serviceStatus))
320 if (serviceStatus.dwCurrentState != SERVICE_STOP_PENDING)
324 if (serviceStatus.dwCurrentState != SERVICE_STOPPED)
325 yaz_log(YLOG_LOG|YLOG_FATAL, "Service failed to stop");
327 if (DeleteService(schService))
328 yaz_log(YLOG_LOG, "Service %s removed", s->service_name);
330 yaz_log(YLOG_FATAL, "Service %s could not be removed", s->service_name);
331 CloseServiceHandle(schService);
333 CloseServiceHandle(manager);
339 SERVICE_TABLE_ENTRY dt[2];
341 dt[0].lpServiceName = s->service_name;
342 dt[0].lpServiceProc = sc_service_main;
343 dt[1].lpServiceName = 0;
344 dt[1].lpServiceProc = 0;
348 if (!StartServiceCtrlDispatcher(dt))
350 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be controlled",
356 /* run the program standalone (with no service) */
357 return s->sc_main(s, argc, argv);
360 void yaz_sc_destroy(yaz_sc_t *s)
362 xfree((*s)->service_name);
363 xfree((*s)->display_name);
371 * indent-tabs-mode: nil
373 * vim: shiftwidth=4 tabstop=8 expandtab