Gracefully exit through the main thread.
[tinc] / src / process.c
index 09fd63e..d88ff97 100644 (file)
@@ -40,7 +40,9 @@ extern char *identname;
 extern char **g_argv;
 extern bool use_logfile;
 
+#ifndef HAVE_MINGW
 sigset_t emptysigset;
+#endif
 
 static void memory_full(int size) {
        logger(LOG_ERR, "Memory exhausted (couldn't allocate %d bytes), exitting.", size);
@@ -164,7 +166,7 @@ DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) {
                        return ERROR_CALL_NOT_IMPLEMENTED;
        }
 
-       event_loopexit(NULL);
+       running = false;
        status.dwWaitHint = 30000; 
        status.dwCurrentState = SERVICE_STOP_PENDING; 
        SetServiceStatus(statushandle, &status);
@@ -261,7 +263,7 @@ bool detach(void) {
 bool execute_script(const char *name, char **envp) {
 #ifdef HAVE_SYSTEM
        int status, len;
-       char *scriptname, *p;
+       char *scriptname;
        int i;
 
 #ifndef HAVE_MINGW
@@ -302,7 +304,7 @@ bool execute_script(const char *name, char **envp) {
        for(i = 0; envp[i]; i++) {
                char *e = strchr(envp[i], '=');
                if(e) {
-                       p = alloca(e - envp[i] + 1);
+                       char p[e - envp[i] + 1];
                        strncpy(p, envp[i], e - envp[i]);
                        p[e - envp[i]] = '\0';
                        putenv(p);
@@ -340,6 +342,14 @@ bool execute_script(const char *name, char **envp) {
 */
 
 #ifndef HAVE_MINGW
+static RETSIGTYPE sigterm_handler(int a) {
+       logger(LOG_NOTICE, "Got %s signal", "TERM");
+       if(running)
+               running = false;
+       else
+               exit(1);
+}
+
 static RETSIGTYPE fatal_signal_square(int a) {
        logger(LOG_ERR, "Got another fatal signal %d (%s): not restarting.", a,
                   strsignal(a));
@@ -380,11 +390,15 @@ static struct {
        int signal;
        void (*handler)(int);
 } sighandlers[] = {
+       {SIGTERM, sigterm_handler},
+       {SIGQUIT, sigterm_handler},
+       {SIGINT, sigterm_handler},
        {SIGSEGV, fatal_signal_handler},
        {SIGBUS, fatal_signal_handler},
        {SIGILL, fatal_signal_handler},
        {SIGPIPE, ignore_signal_handler},
        {SIGCHLD, ignore_signal_handler},
+       {SIGALRM, ignore_signal_handler},
        {0, NULL}
 };
 #endif