--- gvpe/src/util.C 2005/03/18 01:53:05 1.17 +++ gvpe/src/util.C 2008/08/07 19:07:03 1.23 @@ -1,24 +1,34 @@ /* util.C -- process management and other utility functions - Copyright (C) 2003-2005 Marc Lehmann + Copyright (C) 2003-2008 Marc Lehmann Some of these are taken from tinc, see the AUTHORS file. 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. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with gvpe; if not, write to the Free Software - Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + 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 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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, see . + + Additional permission under GNU GPL version 3 section 7 + + If you modify this Program, or any covered work, by linking or + combining it with the OpenSSL project's OpenSSL library (or a modified + version of that library), containing parts covered by the terms of the + OpenSSL or SSLeay licenses, the licensors of this Program grant you + additional permission to convey the resulting work. Corresponding + Source for a non-source form of such a combination shall include the + source code for the parts of OpenSSL used as well as that of the + covered work. */ #include "config.h" @@ -36,6 +46,7 @@ #include "netcompat.h" +#include "gettext.h" #include "pidfile.h" #include "dropin.h" @@ -128,25 +139,58 @@ return 0; } -void run_script (const run_script_cb &cb, bool wait) +pid_t +run_script (const run_script_cb &cb, bool wait) { - int pid; + sigset_t oldset; + + if (wait) + { + sigset_t sigchld; + sigemptyset (&sigchld); + sigaddset (&sigchld, SIGCHLD); + sigprocmask (SIG_BLOCK, &sigchld, &oldset); + } + + pid_t pid = fork (); - if ((pid = fork ()) == 0) + if (pid == 0) { - char *filename; - asprintf (&filename, "%s/%s", confbase, cb()); - execl (filename, filename, (char *) 0); - exit (126); + sigprocmask (SIG_SETMASK, &oldset, 0); + + 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); + + sigprocmask (SIG_SETMASK, &oldset, 0); + + if (res < 0) + { + slog (L_WARN, _("waiting for an external command failed: %s."), + strerror (errno)); + return 0; + } + else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS) + { + slog (L_WARN, _("external command returned with exit status %d (%04x)."), + WEXITSTATUS (status), status); + return 0; + } } } + else + { + slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno)); + exit (EXIT_FAILURE); + } + + return pid; } #if ENABLE_HTTP_PROXY