X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fprocess.c;h=5bb7eb3df6da69bdc34c069ec1f9c704642b335e;hb=5a7c6546a46bdcc97cf73a9aef206d2a83bb1eb4;hp=98f4d33a811d0e9c3d6edd8d1a2a3d3413a7d29b;hpb=e57daac63b6f703af8e7c8209ef61a4d3b2180c3;p=tinc diff --git a/src/process.c b/src/process.c index 98f4d33a..5bb7eb3d 100644 --- a/src/process.c +++ b/src/process.c @@ -34,6 +34,7 @@ #include "subnet.h" #include "utils.h" #include "xalloc.h" +#include "version.h" /* If zero, don't detach from the terminal. */ bool do_detach = true; @@ -41,6 +42,7 @@ bool sigalrm = false; extern char **g_argv; extern bool use_logfile; +extern bool use_syslog; /* Some functions the less gifted operating systems might lack... */ @@ -60,12 +62,9 @@ static bool install_service(void) { 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)); + HMODULE module = GetModuleHandle(NULL); + GetModuleFileName(module, command + 1, sizeof command - 1); + command[sizeof command - 1] = 0; strncat(command, "\"", sizeof command - strlen(command)); @@ -108,7 +107,7 @@ static bool install_service(void) { return true; } -static io_t stop_io; +io_t stop_io; DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) { switch(request) { @@ -134,17 +133,9 @@ DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) { return NO_ERROR; } -static void stop_handler(void *data, int flags) { - event_exit(); -} - VOID WINAPI run_service(DWORD argc, LPTSTR* argv) { extern int main2(int argc, char **argv); - io_add_event(&stop_io, stop_handler, NULL, WSACreateEvent()); - if (stop_io.event == FALSE) - abort(); - status.dwServiceType = SERVICE_WIN32; status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; status.dwWin32ExitCode = 0; @@ -171,9 +162,6 @@ VOID WINAPI run_service(DWORD argc, LPTSTR* argv) { SetServiceStatus(statushandle, &status); } - if (WSACloseEvent(stop_io.event) == FALSE) - abort(); - io_del(&stop_io); return; } @@ -199,6 +187,8 @@ bool init_service(void) { Detach from current terminal */ bool detach(void) { + logmode_t logmode; + #ifndef HAVE_MINGW signal(SIGPIPE, SIG_IGN); signal(SIGUSR1, SIG_IGN); @@ -210,7 +200,7 @@ bool detach(void) { if(do_detach) { #ifndef HAVE_MINGW - if(daemon(0, 0)) { + if(daemon(1, 0)) { logger(DEBUG_ALWAYS, LOG_ERR, "Couldn't detach from terminal: %s", strerror(errno)); return false; } @@ -220,12 +210,17 @@ bool detach(void) { #endif } - openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR)); + if(use_logfile) + logmode = LOGMODE_FILE; + else if(use_syslog || do_detach) + logmode = LOGMODE_SYSLOG; + else + logmode = LOGMODE_STDERR; + + openlogger(identname, logmode); logger(DEBUG_ALWAYS, LOG_NOTICE, "tincd %s (%s %s) starting, debug level %d", - VERSION, __DATE__, __TIME__, debug_level); + BUILD_VERSION, BUILD_DATE, BUILD_TIME, debug_level); return true; } - -