--- gvpe/src/util.C 2003/10/14 15:48:15 1.8 +++ gvpe/src/util.C 2005/04/08 16:12:49 1.20 @@ -1,9 +1,12 @@ /* util.C -- process management and other utility functions + Copyright (C) 2003-2005 Marc Lehmann Some of these are taken from tinc, see the AUTHORS file. - This program is free software; you can redistribute it and/or modify + This file is part of GVPE. + + GVPE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -14,13 +17,14 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software + along with gvpe; if not, write to the Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" #include +#include #include #include @@ -30,8 +34,6 @@ #include #include -#include - #include "netcompat.h" #include "gettext.h" @@ -48,16 +50,16 @@ { int pid; - pid = check_pid (pidfilename); + pid = check_pid (conf.pidfilename); if (pid) { - fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); + fprintf (stderr, _("A gvpe daemon is already running with pid %d.\n"), pid); return 1; } /* if it's locked, write-protected, or whatever */ - if (!write_pid (pidfilename)) + if (!write_pid (conf.pidfilename)) return 1; return 0; @@ -68,11 +70,11 @@ { int pid; - pid = read_pid (pidfilename); + pid = read_pid (conf.pidfilename); if (!pid) { - fprintf (stderr, _("No other vped is running.\n")); + fprintf (stderr, _("No other gvpe daemon is running.\n")); return 1; } @@ -81,10 +83,10 @@ /* ESRCH is returned when no process with that pid is found */ if (kill (pid, signal) && errno == ESRCH) { - fprintf (stderr, _("The vped is no longer running. ")); + fprintf (stderr, _("The gvpe daemon is no longer running. ")); fprintf (stderr, _("Removing stale lock file.\n")); - remove_pid (pidfilename); + remove_pid (conf.pidfilename); } return 0; @@ -114,7 +116,7 @@ /* Now UPDATE the pid in the pidfile, because we changed it... */ - if (!write_pid (pidfilename)) + if (!write_pid (conf.pidfilename)) return -1; log_to (LOGTO_SYSLOG); @@ -122,40 +124,53 @@ else log_to (LOGTO_SYSLOG | LOGTO_STDERR); - slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); + slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); return 0; } -void -make_names (void) +bool run_script (const run_script_cb &cb, bool wait) { - if (!pidfilename) - pidfilename = LOCALSTATEDIR "/run/vped.pid"; + if (wait) + signal (SIGCHLD, SIG_DFL); // this is extremely ugly, but I did not feel like implementing a complete wait() event logic. It's easier to write this long comment to make your editor happy. - if (!confbase) - asprintf (&confbase, "%s/vpe", CONFDIR); -} - -void run_script (const run_script_cb &cb, bool wait) -{ - int pid; + int pid = fork (); - if ((pid = fork ()) == 0) + if (pid == 0) { - char *filename; - asprintf (&filename, "%s/%s", confbase, cb()); - execl (filename, filename, (char *) 0); - exit (255); + execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0); + exit (EXIT_FAILURE); } else if (pid > 0) { if (wait) { - waitpid (pid, 0, 0); - /* TODO: check status */ + int status; + int res = waitpid (pid, &status, 0); + + signal (SIGCHLD, SIG_IGN); + + if (res < 0) + { + slog (L_WARN, _("waiting for an external command failed: %s."), + strerror (errno)); + return false; + } + else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS) + { + slog (L_WARN, _("external command returned with exit status %d (%04x)."), + WEXITSTATUS (status), status); + return false; + } } } + else + { + slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno)); + exit (EXIT_FAILURE); + } + + return true; } #if ENABLE_HTTP_PROXY @@ -206,3 +221,28 @@ } #endif +void +id2mac (unsigned int id, void *m) +{ + mac &p = *(mac *)m; + + if (id) + { + p[0] = 0xfe; + p[1] = 0xfd; + p[2] = 0x80; + p[3] = 0x00; + p[4] = id >> 8; + p[5] = id; + } + else + { + p[0] = 0xff; + p[1] = 0xff; + p[2] = 0xff; + p[3] = 0xff; + p[4] = 0xff; + p[5] = 0xff; + } +} +