X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fprocess.c;h=36b214615490e6f3f1299a6fa3e5ec10f28a4066;hp=326d3ff915f3e28328740da9464a08a6b3749311;hb=0e945413315c9d15a3eb013fa3731dd978a8c7b8;hpb=5db596c6844169f1eb5f804b72abe99d067aaa5a diff --git a/src/process.c b/src/process.c index 326d3ff9..36b21461 100644 --- a/src/process.c +++ b/src/process.c @@ -17,54 +17,37 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: process.c,v 1.1.2.54 2003/07/12 17:41:46 guus Exp $ + $Id: process.c,v 1.1.2.58 2003/07/28 22:06:09 guus Exp $ */ -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include +#include "system.h" #include "conf.h" -#include "process.h" -#include "subnet.h" -#include "device.h" #include "connection.h" #include "device.h" +#include "edge.h" #include "logger.h" - -#include "system.h" +#include "node.h" +#include "pidfile.h" +#include "process.h" +#include "subnet.h" +#include "utils.h" +#include "xalloc.h" /* If zero, don't detach from the terminal. */ -int do_detach = 1; +bool do_detach = true; +bool sighup = false; +bool sigalrm = false; extern char *identname; extern char *pidfilename; extern char **g_argv; -extern int use_logfile; +extern bool use_logfile; sigset_t emptysigset; static int saved_debug_level = -1; -extern int sighup; -extern int sigalrm; -extern int do_purge; - static void memory_full(int size) { logger(LOG_ERR, _("Memory exhausted (couldn't allocate %d bytes), exitting."), size); @@ -101,14 +84,15 @@ void cleanup_and_exit(int c) logger(LOG_NOTICE, _("Terminating")); - closelog(); + closelogger(); exit(c); } +#ifndef HAVE_MINGW /* check for an existing tinc for this net, and write pid to pidfile */ -static int write_pidfile(void) +static bool write_pidfile(void) { int pid; @@ -122,21 +106,23 @@ static int write_pidfile(void) netname, pid); else fprintf(stderr, _("A tincd is already running with pid %d.\n"), pid); - return 1; + return false; } /* if it's locked, write-protected, or whatever */ if(!write_pid(pidfilename)) - return 1; + return false; - return 0; + return true; } +#endif /* kill older tincd for this net */ -int kill_other(int signal) +bool kill_other(int signal) { +#ifndef HAVE_MINGW int pid; cp(); @@ -149,7 +135,7 @@ int kill_other(int signal) netname); else fprintf(stderr, _("No other tincd is running.\n")); - return 1; + return false; } errno = 0; /* No error, sometimes errno is only changed on error */ @@ -165,14 +151,15 @@ int kill_other(int signal) fprintf(stderr, _("Removing stale lock file.\n")); remove_pid(pidfilename); } +#endif - return 0; + return true; } /* Detach from current terminal, write pidfile, kill parent */ -int detach(void) +bool detach(void) { cp(); @@ -180,25 +167,29 @@ int detach(void) /* First check if we can open a fresh new pidfile */ - if(write_pidfile()) - return -1; +#ifndef HAVE_MINGW + if(!write_pidfile()) + return false; +#endif /* If we succeeded in doing that, detach */ - closelog(); + closelogger(); +#ifdef HAVE_FORK if(do_detach) { - if(daemon(0, 0) < 0) { + if(daemon(0, 0)) { fprintf(stderr, _("Couldn't detach from terminal: %s"), strerror(errno)); - return -1; + return false; } /* Now UPDATE the pid in the pidfile, because we changed it... */ if(!write_pid(pidfilename)) - return -1; + return false; } +#endif openlogger(identname, use_logfile?LOGMODE_FILE:(do_detach?LOGMODE_SYSLOG:LOGMODE_STDERR)); @@ -207,9 +198,10 @@ int detach(void) xalloc_fail_func = memory_full; - return 0; + return true; } +#ifdef HAVE_FORK /* Execute the program name, with sane environment. */ @@ -241,12 +233,14 @@ static void _execute_script(const char *scriptname, char **envp) strerror(save_errno)); exit(save_errno); } +#endif /* Fork and execute the program pointed to by name. */ -int execute_script(const char *name, char **envp) +bool execute_script(const char *name, char **envp) { +#ifdef HAVE_FORK pid_t pid; int status; struct stat s; @@ -259,14 +253,14 @@ int execute_script(const char *name, char **envp) /* First check if there is a script */ if(stat(scriptname, &s)) - return 0; + return true; pid = fork(); if(pid < 0) { logger(LOG_ERR, _("System call `%s' failed: %s"), "fork", strerror(errno)); - return -1; + return false; } if(pid) { @@ -279,31 +273,34 @@ int execute_script(const char *name, char **envp) if(WEXITSTATUS(status)) { logger(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status)); - return -1; + return false; } else - return 0; + return true; } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */ logger(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"), pid, name, WTERMSIG(status), strsignal(WTERMSIG(status))); - return -1; + return false; } else { /* Something strange happened */ logger(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name); - return -1; + return false; } } else if (errno != EINTR) { logger(LOG_ERR, _("System call `%s' failed: %s"), "waitpid", strerror(errno)); - return -1; + return false; } /* Why do we get EINTR? */ - return 0; + return true; } /* Child here */ _execute_script(scriptname, envp); +#else + return true; +#endif } @@ -311,6 +308,7 @@ int execute_script(const char *name, char **envp) Signal handlers. */ +#ifndef HAVE_MINGW static RETSIGTYPE sigterm_handler(int a) { logger(LOG_NOTICE, _("Got TERM signal")); @@ -359,7 +357,7 @@ static RETSIGTYPE fatal_signal_handler(int a) static RETSIGTYPE sighup_handler(int a) { logger(LOG_NOTICE, _("Got HUP signal")); - sighup = 1; + sighup = true; } static RETSIGTYPE sigint_handler(int a) @@ -381,7 +379,7 @@ static RETSIGTYPE sigint_handler(int a) static RETSIGTYPE sigalrm_handler(int a) { logger(LOG_NOTICE, _("Got ALRM signal")); - sigalrm = 1; + sigalrm = true; } static RETSIGTYPE sigusr1_handler(int a) @@ -399,8 +397,7 @@ static RETSIGTYPE sigusr2_handler(int a) static RETSIGTYPE sigwinch_handler(int a) { - extern int do_purge; - do_purge = 1; + do_purge = true; } static RETSIGTYPE unexpected_signal_handler(int a) @@ -418,25 +415,26 @@ static struct { int signal; void (*handler)(int); } sighandlers[] = { - { - SIGHUP, sighup_handler}, { - SIGTERM, sigterm_handler}, { - SIGQUIT, sigquit_handler}, { - SIGSEGV, fatal_signal_handler}, { - SIGBUS, fatal_signal_handler}, { - SIGILL, fatal_signal_handler}, { - SIGPIPE, ignore_signal_handler}, { - SIGINT, sigint_handler}, { - SIGUSR1, sigusr1_handler}, { - SIGUSR2, sigusr2_handler}, { - SIGCHLD, ignore_signal_handler}, { - SIGALRM, sigalrm_handler}, { - SIGWINCH, sigwinch_handler}, { - 0, NULL} + {SIGHUP, sighup_handler}, + {SIGTERM, sigterm_handler}, + {SIGQUIT, sigquit_handler}, + {SIGSEGV, fatal_signal_handler}, + {SIGBUS, fatal_signal_handler}, + {SIGILL, fatal_signal_handler}, + {SIGPIPE, ignore_signal_handler}, + {SIGINT, sigint_handler}, + {SIGUSR1, sigusr1_handler}, + {SIGUSR2, sigusr2_handler}, + {SIGCHLD, ignore_signal_handler}, + {SIGALRM, sigalrm_handler}, + {SIGWINCH, sigwinch_handler}, + {0, NULL} }; +#endif void setup_signals(void) { +#ifndef HAVE_MINGW int i; struct sigaction act; @@ -468,4 +466,5 @@ void setup_signals(void) sighandlers[i].signal, strsignal(sighandlers[i].signal), strerror(errno)); } +#endif }