Cleanups:
[tinc] / src / process.c
index 0b17d7a..3e07552 100644 (file)
@@ -1,7 +1,7 @@
 /*
     process.c -- process management functions
-    Copyright (C) 1999-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
-                  2000-2002 Guus Sliepen <guus@sliepen.warande.net>
+    Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
+                  2000-2002 Guus Sliepen <guus@sliepen.eu.org>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
     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.38 2002/03/24 16:22:59 guus Exp $
+    $Id: process.c,v 1.1.2.45 2002/09/09 19:39:59 guus Exp $
 */
 
 #include "config.h"
@@ -91,7 +91,7 @@ int fcloseall(void)
 */
 void cleanup_and_exit(int c)
 {
-cp
+  cp();
   close_network_connections();
 
   if(debug_lvl > DEBUG_NOTHING)
@@ -109,8 +109,10 @@ cp
 int write_pidfile(void)
 {
   int pid;
-cp
-  if((pid = check_pid(pidfilename)))
+  cp();
+  pid = check_pid(pidfilename);
+
+  if(pid)
     {
       if(netname)
        fprintf(stderr, _("A tincd is already running for net `%s' with pid %d.\n"),
@@ -123,7 +125,7 @@ cp
   /* if it's locked, write-protected, or whatever */
   if(!write_pid(pidfilename))
     return 1;
-cp
+  cp();
   return 0;
 }
 
@@ -133,8 +135,10 @@ cp
 int kill_other(int signal)
 {
   int pid;
-cp
-  if(!(pid = read_pid(pidfilename)))
+  cp();
+  pid = read_pid(pidfilename);
+
+  if(!pid)
     {
       if(netname)
        fprintf(stderr, _("No other tincd is running for net `%s'.\n"), netname);
@@ -155,7 +159,7 @@ cp
       fprintf(stderr, _("Removing stale lock file.\n"));
       remove_pid(pidfilename);
     }
-cp
+  cp();
   return 0;
 }
 
@@ -164,7 +168,7 @@ cp
 */
 int detach(void)
 {
-cp
+  cp();
   setup_signals();
 
   /* First check if we can open a fresh new pidfile */
@@ -199,7 +203,7 @@ cp
     syslog(LOG_NOTICE, _("tincd %s starting"), VERSION);
 
   xalloc_fail_func = memory_full;
-cp
+  cp();
   return 0;
 }
 
@@ -207,68 +211,48 @@ cp
   Execute the program name, with sane environment.  All output will be
   redirected to syslog.
 */
-void _execute_script(const char *name)  __attribute__ ((noreturn));
-void _execute_script(const char *name)
+void _execute_script(const char *scriptname, char **envp)  __attribute__ ((noreturn));
+void _execute_script(const char *scriptname, char **envp)
 {
-  char *scriptname;
   char *s;
-cp
-#ifdef HAVE_UNSETENV
-  unsetenv("NETNAME");
-  unsetenv("DEVICE");
-  unsetenv("INTERFACE");
-#endif
-
-  if(netname)
-    {
-      asprintf(&s, "NETNAME=%s", netname);
-      putenv(s);       /* Don't free s! see man 3 putenv */
-    }
-
-  if(device)
-    {
-      asprintf(&s, "DEVICE=%s", device);
-      putenv(s);       /* Don't free s! see man 3 putenv */
-    }
-
-  if(interface)
-    {
-      asprintf(&s, "INTERFACE=%s", interface);
-      putenv(s);       /* Don't free s! see man 3 putenv */
-    }
-
+  cp();
+  while(*envp)
+    putenv(*envp++);
+      
   chdir("/");
   
-  asprintf(&scriptname, "%s/%s", confbase, name);
-
   /* Close all file descriptors */
   closelog();          /* <- this means we cannot use syslog() here anymore! */
   fcloseall();
 
   execl(scriptname, NULL);
   /* No return on success */
-  
-  if(errno != ENOENT)  /* Ignore if the file does not exist */
-    exit(1);           /* Some error while trying execl(). */
-  else
-    exit(0);
+
+  openlog("tinc", LOG_CONS | LOG_PID, LOG_DAEMON);
+  syslog(LOG_ERR, _("Could not execute `%s': %s"), scriptname, strerror(errno));
+  exit(errno);
 }
 
 /*
   Fork and execute the program pointed to by name.
 */
-int execute_script(const char *name)
+int execute_script(const char *name, char **envp)
 {
   pid_t pid;
   int status;
   struct stat s;
-cp
+  char *scriptname;
+  cp();
+  asprintf(&scriptname, "%s/%s", confbase, name);
+
   /* First check if there is a script */
 
-  if(stat(name, &s))
+  if(stat(scriptname, &s))
     return 0;
 
-  if((pid = fork()) < 0)
+  pid = fork();
+  
+  if(pid < 0)
     {
       syslog(LOG_ERR, _("System call `%s' failed: %s"), "fork", strerror(errno));
       return -1;
@@ -279,6 +263,8 @@ cp
       if(debug_lvl >= DEBUG_STATUS)
         syslog(LOG_INFO, _("Executing script %s"), name);
 
+      free(scriptname);
+
       if(waitpid(pid, &status, 0) == pid)
         {
           if(WIFEXITED(status))                /* Child exited by itself */
@@ -309,10 +295,10 @@ cp
           return -1;
         }
     }
-cp
+  cp();
   /* Child here */
 
-  _execute_script(name);
+  _execute_script(scriptname, envp);
 }