-/*
- Detach from current terminal, write pidfile, kill parent
-*/
-int detach(void)
-{
-cp
- setup_signals();
-
- /* First check if we can open a fresh new pidfile */
-
- if(write_pidfile())
- return -1;
-
- /* If we succeeded in doing that, detach */
-
- if(do_detach)
- {
- if(daemon(0, 0) < 0)
- {
- fprintf(stderr, _("Couldn't detach from terminal: %m"));
- return -1;
+ if(space) {
+ strncat(command, "\"", sizeof(command) - strlen(command));
+ }
+
+ strncat(command, *argp, sizeof(command) - strlen(command));
+
+ if(space) {
+ strncat(command, "\"", sizeof(command) - strlen(command));
+ }
+ }
+
+ service = CreateService(manager, identname, identname,
+ SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
+ command, NULL, NULL, NULL, NULL, NULL);
+
+ if(!service) {
+ DWORD lasterror = GetLastError();
+ logger(DEBUG_ALWAYS, LOG_ERR, "Could not create %s service: %s", identname, winerror(lasterror));
+
+ if(lasterror != ERROR_SERVICE_EXISTS) {
+ return false;
+ }
+ }
+
+ 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);