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);
29 SERVICE_STATUS_HANDLE gSvcStatusHandle;
30 SERVICE_STATUS gSvcStatus;
35 yaz_sc_t yaz_sc_create(const char *service_name, const char *display_name)
37 yaz_sc_t s = xmalloc(sizeof(*s));
39 s->service_name = service_name ? xstrdup(service_name) : 0;
40 s->display_name = display_name ? xstrdup(display_name) : 0;
48 s->gSvcStatusHandle = 0;
54 static void parse_args(yaz_sc_t s, int *argc_p, char ***argv_p)
57 const char *log_file = 0;
60 /* now look for the service arguments */
62 for (i = 1; i < *argc_p; i++)
64 const char *opt = (*argv_p)[i];
65 if (!strcmp(opt, "-install"))
71 else if (!strcmp(opt, "-installa"))
78 else if (!strcmp(opt, "-remove"))
84 else if (!strcmp(opt, "-run") && i < *argc_p-1)
87 const char *dir = (*argv_p)[i+1];
95 for (; i < *argc_p; i++)
96 (*argv_p)[i] = (*argv_p)[i + skip_opt];
98 /* we must have a YAZ log file to work with */
99 for (i = 1; i < *argc_p; i++)
101 const char *opt = (*argv_p)[i];
102 if (opt[0] == '-' && opt[1] == 'l')
110 else if (i < *argc_p - 1)
112 log_file = (*argv_p)[i+1];
119 yaz_log_init_file(log_file);
124 yaz_log(YLOG_FATAL, "Must specify -l logfile for service to install");
129 { /* remove -l logfile for a running service */
130 for (; i < *argc_p; i++)
131 (*argv_p)[i] = (*argv_p)[i + skip_opt];
135 VOID sc_ReportSvcStatus(yaz_sc_t s,
136 DWORD dwCurrentState,
137 DWORD dwWin32ExitCode,
140 if (s->gSvcStatusHandle)
142 static DWORD dwCheckPoint = 1;
144 // Fill in the SERVICE_STATUS structure.
146 s->gSvcStatus.dwCurrentState = dwCurrentState;
147 s->gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
148 s->gSvcStatus.dwWaitHint = dwWaitHint;
150 if (dwCurrentState == SERVICE_START_PENDING)
151 s->gSvcStatus.dwControlsAccepted = 0;
153 s->gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
155 if ( (dwCurrentState == SERVICE_RUNNING) ||
156 (dwCurrentState == SERVICE_STOPPED) )
157 s->gSvcStatus.dwCheckPoint = 0;
159 s->gSvcStatus.dwCheckPoint = dwCheckPoint++;
161 // Report the status of the service to the SCM.
162 SetServiceStatus(s->gSvcStatusHandle, &s->gSvcStatus );
166 static yaz_sc_t global_sc = 0;
168 VOID WINAPI sc_SvcCtrlHandler(DWORD dwCtrl)
172 case SERVICE_CONTROL_STOP:
173 yaz_log(YLOG_LOG, "Service %s to stop", global_sc->service_name);
174 sc_ReportSvcStatus(global_sc, SERVICE_STOP_PENDING, NO_ERROR, 0);
175 global_sc->sc_stop(global_sc);
176 sc_ReportSvcStatus(global_sc, SERVICE_STOPPED, NO_ERROR, 0);
178 case SERVICE_CONTROL_INTERROGATE:
185 static void WINAPI sc_service_main(DWORD argc, char **argv)
187 yaz_sc_t s = global_sc;
190 yaz_log(YLOG_LOG, "Service %s starting", s->service_name);
192 s->gSvcStatusHandle = RegisterServiceCtrlHandler(
193 s->service_name, sc_SvcCtrlHandler);
195 if (!s->gSvcStatusHandle)
197 yaz_log(YLOG_FATAL|YLOG_ERRNO, "RegisterServiceCtrlHandler");
201 s->gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
202 s->gSvcStatus.dwServiceSpecificExitCode = 0;
204 sc_ReportSvcStatus(s, SERVICE_START_PENDING, NO_ERROR, 3000);
206 ret_code = s->sc_main(s, argc, argv);
208 sc_ReportSvcStatus(s, SERVICE_STOPPED,
209 ret_code ? ERROR_SERVICE_SPECIFIC_ERROR : NO_ERROR, ret_code);
213 void yaz_sc_running(yaz_sc_t s)
216 sc_ReportSvcStatus(s, SERVICE_RUNNING, NO_ERROR, 0);
220 int yaz_sc_program(yaz_sc_t s, int argc, char **argv,
221 int (*sc_main)(yaz_sc_t s, int argc, char **argv),
222 void (*sc_stop)(yaz_sc_t s))
225 s->sc_main = sc_main;
226 s->sc_stop = sc_stop;
228 parse_args(s, &argc, &argv);
230 if (s->install_flag || s->remove_flag)
232 SC_HANDLE manager = OpenSCManager(NULL /* machine */,
234 SC_MANAGER_ALL_ACCESS);
237 yaz_log(YLOG_FATAL|YLOG_ERRNO, "OpenSCManager failed");
242 SC_HANDLE schService = 0;
245 WRBUF w = wrbuf_alloc();
246 char cwdstr[_MAX_PATH];
248 if (!_getcwd(cwdstr, sizeof(cwdstr)))
249 strcpy (cwdstr, ".");
251 if (GetModuleFileName(NULL, szPath, 2048) == 0)
253 yaz_log(YLOG_FATAL, "GetModuleFileName failed");
256 wrbuf_puts(w, szPath);
257 for (i = 1; i < argc; i++)
260 wrbuf_puts(w, argv[i]);
262 wrbuf_puts(w, " -run \"");
263 wrbuf_puts(w, cwdstr);
265 yaz_log(YLOG_LOG, "path: %s", wrbuf_cstr(w));
269 manager, /* SCM database */
270 TEXT(s->service_name), /* name of service */
271 TEXT(s->display_name), /* service name to display */
272 SERVICE_ALL_ACCESS, /* desired access */
273 SERVICE_WIN32_OWN_PROCESS, /* service type */
275 SERVICE_AUTO_START : SERVICE_DEMAND_START, /* start type */
276 SERVICE_ERROR_NORMAL, /* error control type */
277 wrbuf_cstr(w), /* path to service's binary */
278 NULL, /* no load ordering group */
279 NULL, /* no tag identifier */
280 NULL, /* no dependencies */
281 NULL, /* LocalSystem account */
282 NULL); /* no password */
283 if (schService == NULL)
285 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be installed",
287 CloseServiceHandle(manager);
290 yaz_log(YLOG_LOG, "Installed service %s", s->service_name);
291 CloseServiceHandle(schService);
294 else if (s->remove_flag)
296 SC_HANDLE schService = 0;
297 SERVICE_STATUS serviceStatus;
299 schService = OpenService(manager, TEXT(s->service_name), SERVICE_ALL_ACCESS);
300 if (schService == NULL)
302 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be opened",
304 CloseServiceHandle(manager);
307 /* try to stop the service */
308 if (ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus))
310 yaz_log(YLOG_LOG, "Service %s being stopped", s->service_name);
312 while (QueryServiceStatus(schService, &serviceStatus))
314 if (serviceStatus.dwCurrentState != SERVICE_STOP_PENDING)
318 if (serviceStatus.dwCurrentState != SERVICE_STOPPED)
319 yaz_log(YLOG_LOG|YLOG_FATAL, "Service failed to stop");
321 if (DeleteService(schService))
322 yaz_log(YLOG_LOG, "Service %s removed", s->service_name);
324 yaz_log(YLOG_FATAL, "Service %s could not be removed", s->service_name);
325 CloseServiceHandle(schService);
327 CloseServiceHandle(manager);
333 SERVICE_TABLE_ENTRY dt[2];
335 dt[0].lpServiceName = s->service_name;
336 dt[0].lpServiceProc = sc_service_main;
337 dt[1].lpServiceName = 0;
338 dt[1].lpServiceProc = 0;
340 if (!StartServiceCtrlDispatcher(dt))
342 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Service %s could not be controlled",
348 /* run the program standalone (with no service) */
349 return s->sc_main(s, argc, argv);
352 void yaz_sc_destroy(yaz_sc_t *s)
354 xfree((*s)->service_name);
355 xfree((*s)->display_name);
363 * indent-tabs-mode: nil
365 * vim: shiftwidth=4 tabstop=8 expandtab