- chdir("/");
-
- asprintf(&scriptname, "%s/%s", confbase, name);
-
- /* Close all file descriptors */
- closelog(); /* <- this means we cannot use syslog() here anymore! */
- fcloseall();
-
- execl(scriptname, NULL);
- /* No return on success */
-
- if(errno != ENOENT) /* Ignore if the file does not exist */
- exit(-1); /* Some error while trying execl(). */
- else
- exit(0);
+ if(service) {
+ ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &description);
+ logger(DEBUG_ALWAYS, LOG_INFO, "%s service installed", identname);
+ } else {
+ service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
+ }
+
+ if(!StartService(service, 0, NULL))
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
+ else
+ logger(DEBUG_ALWAYS, LOG_INFO, "%s service started", identname);
+
+ return true;
+}
+
+static io_t stop_io;
+
+DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
+ switch(request) {
+ case SERVICE_CONTROL_INTERROGATE:
+ SetServiceStatus(statushandle, &status);
+ return NO_ERROR;
+ case SERVICE_CONTROL_STOP:
+ logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_STOP");
+ break;
+ case SERVICE_CONTROL_SHUTDOWN:
+ logger(DEBUG_ALWAYS, LOG_NOTICE, "Got %s request", "SERVICE_CONTROL_SHUTDOWN");
+ break;
+ default:
+ logger(DEBUG_ALWAYS, LOG_WARNING, "Got unexpected request %d", (int)request);
+ return ERROR_CALL_NOT_IMPLEMENTED;
+ }
+
+ status.dwWaitHint = 1000;
+ status.dwCurrentState = SERVICE_STOP_PENDING;
+ SetServiceStatus(statushandle, &status);
+ if (WSASetEvent(stop_io.event) == FALSE)
+ abort();
+ return NO_ERROR;