-/*
- Detach from current terminal, write pidfile, kill parent
-*/
-int detach(void)
-{
- int fd;
- pid_t pid;
-
- if(do_detach)
- {
- ppid = getpid();
-
- if((pid = fork()) < 0)
- {
- perror("fork");
- return -1;
- }
- if(pid) /* parent process */
- {
- signal(SIGTERM, parent_exit);
- sleep(600); /* wait 10 minutes */
- exit(1);
- }
- }
-
- if(write_pidfile())
- return -1;
-
- if(do_detach)
- {
- if((fd = open("/dev/tty", O_RDWR)) >= 0)
- {
- if(ioctl(fd, TIOCNOTTY, NULL))
- {
- perror("ioctl");
- return -1;
- }
- close(fd);
- }
-
- if(setsid() < 0)
- return -1;
-
- kill(ppid, SIGTERM);
- }
-
- chdir("/"); /* avoid keeping a mointpoint busy */
-
- openlog(identname, LOG_CONS | LOG_PID, LOG_DAEMON);
-
- if(debug_lvl > DEBUG_NOTHING)
- syslog(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
- VERSION, __DATE__, __TIME__, debug_lvl);
- else
- syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
-
- xalloc_fail_func = memory_full;
-
- return 0;
+/* Some functions the less gifted operating systems might lack... */
+
+#ifdef HAVE_MINGW
+extern char *identname;
+extern char *program_name;
+extern char **g_argv;
+
+static SC_HANDLE manager = NULL;
+static SC_HANDLE service = NULL;
+static SERVICE_STATUS status = {0};
+static SERVICE_STATUS_HANDLE statushandle = 0;
+
+bool install_service(void) {
+ char command[4096] = "\"";
+ char **argp;
+ bool space;
+ SERVICE_DESCRIPTION description = {"Virtual Private Network daemon"};
+
+ manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+
+ if(!manager) {
+ logger(LOG_ERR, "Could not open service manager: %s", winerror(GetLastError()));
+ return false;
+ }
+
+ if(!strchr(program_name, '\\')) {
+ GetCurrentDirectory(sizeof(command) - 1, command + 1);
+ strncat(command, "\\", sizeof(command) - strlen(command));
+ }
+
+ strncat(command, program_name, sizeof(command) - strlen(command));
+
+ strncat(command, "\"", sizeof(command) - strlen(command));
+
+ for(argp = g_argv + 1; *argp; argp++) {
+ space = strchr(*argp, ' ');
+ strncat(command, " ", sizeof(command) - strlen(command));
+
+ 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(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(LOG_INFO, "%s service installed", identname);
+ } else {
+ service = OpenService(manager, identname, SERVICE_ALL_ACCESS);
+ }
+
+ if(!StartService(service, 0, NULL)) {
+ logger(LOG_WARNING, "Could not start %s service: %s", identname, winerror(GetLastError()));
+ } else {
+ logger(LOG_INFO, "%s service started", identname);
+ }
+
+ return true;