1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
3 | Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | Some of these are taken from tinc, see the AUTHORS file. |
5 | Some of these are taken from tinc, see the AUTHORS file. |
6 | |
6 | |
|
|
7 | This file is part of GVPE. |
|
|
8 | |
7 | This program is free software; you can redistribute it and/or modify |
9 | GVPE is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
10 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2 of the License, or |
11 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
12 | (at your option) any later version. |
11 | |
13 | |
12 | This program is distributed in the hope that it will be useful, |
14 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
17 | GNU General Public License for more details. |
16 | |
18 | |
17 | You should have received a copy of the GNU General Public License |
19 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software |
20 | along with gvpe; if not, write to the Free Software |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | */ |
22 | */ |
21 | |
23 | |
22 | #include "config.h" |
24 | #include "config.h" |
23 | |
25 | |
… | |
… | |
125 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
127 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
126 | |
128 | |
127 | return 0; |
129 | return 0; |
128 | } |
130 | } |
129 | |
131 | |
130 | void run_script (const run_script_cb &cb, bool wait) |
132 | bool run_script (const run_script_cb &cb, bool wait) |
131 | { |
133 | { |
132 | int pid; |
134 | if (wait) |
|
|
135 | 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. |
133 | |
136 | |
134 | if ((pid = fork ()) == 0) |
137 | int pid = fork (); |
|
|
138 | |
|
|
139 | if (pid == 0) |
135 | { |
140 | { |
136 | char *filename; |
141 | execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0); |
137 | asprintf (&filename, "%s/%s", confbase, cb()); |
142 | exit (EXIT_FAILURE); |
138 | execl (filename, filename, (char *) 0); |
|
|
139 | exit (126); |
|
|
140 | } |
143 | } |
141 | else if (pid > 0) |
144 | else if (pid > 0) |
142 | { |
145 | { |
143 | if (wait) |
146 | if (wait) |
144 | { |
147 | { |
|
|
148 | int status; |
145 | waitpid (pid, 0, 0); |
149 | int res = waitpid (pid, &status, 0); |
146 | /* TODO: check status */ |
150 | |
|
|
151 | signal (SIGCHLD, SIG_IGN); |
|
|
152 | |
|
|
153 | if (res < 0) |
|
|
154 | { |
|
|
155 | slog (L_WARN, _("waiting for an external command failed: %s."), |
|
|
156 | strerror (errno)); |
|
|
157 | return false; |
|
|
158 | } |
|
|
159 | else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS) |
|
|
160 | { |
|
|
161 | slog (L_WARN, _("external command returned with exit status %d (%04x)."), |
|
|
162 | WEXITSTATUS (status), status); |
|
|
163 | return false; |
|
|
164 | } |
147 | } |
165 | } |
148 | } |
166 | } |
|
|
167 | else |
|
|
168 | { |
|
|
169 | slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno)); |
|
|
170 | exit (EXIT_FAILURE); |
|
|
171 | } |
|
|
172 | |
|
|
173 | return true; |
149 | } |
174 | } |
150 | |
175 | |
151 | #if ENABLE_HTTP_PROXY |
176 | #if ENABLE_HTTP_PROXY |
152 | // works like strdup |
177 | // works like strdup |
153 | u8 * |
178 | u8 * |