ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/util.C
(Generate patch)

Comparing gvpe/src/util.C (file contents):
Revision 1.20 by pcg, Fri Apr 8 16:12:49 2005 UTC vs.
Revision 1.25 by root, Tue Mar 8 17:33:31 2011 UTC

1/* 1/*
2 util.C -- process management and other utility functions 2 util.C -- process management and other utility functions
3 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> 3 Copyright (C) 2003-2011 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. 7 This file is part of GVPE.
8 8
9 GVPE is free software; you can redistribute it and/or modify 9 GVPE is free software; you can redistribute it and/or modify it
10 it under the terms of the GNU General Public License as published by 10 under the terms of the GNU General Public License as published by the
11 the Free Software Foundation; either version 2 of the License, or 11 Free Software Foundation; either version 3 of the License, or (at your
12 (at your option) any later version. 12 option) any later version.
13 13
14 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, but
15 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 GNU General Public License for more details. 17 Public License for more details.
18 18
19 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 along
20 along with gvpe; if not, write to the Free Software 20 with this program; if not, see <http://www.gnu.org/licenses/>.
21 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21
22 Additional permission under GNU GPL version 3 section 7
23
24 If you modify this Program, or any covered work, by linking or
25 combining it with the OpenSSL project's OpenSSL library (or a modified
26 version of that library), containing parts covered by the terms of the
27 OpenSSL or SSLeay licenses, the licensors of this Program grant you
28 additional permission to convey the resulting work. Corresponding
29 Source for a non-source form of such a combination shall include the
30 source code for the parts of OpenSSL used as well as that of the
31 covered work.
22*/ 32*/
23 33
24#include "config.h" 34#include "config.h"
25 35
26#include <cstdio> 36#include <cstdio>
27#include <cstdlib> 37#include <cstdlib>
28#include <cstring> 38#include <cstring>
39
40#include <queue>
29 41
30#include <errno.h> 42#include <errno.h>
31#include <signal.h> 43#include <signal.h>
32#include <sys/types.h> 44#include <sys/types.h>
33#include <sys/wait.h> 45#include <sys/wait.h>
34#include <unistd.h> 46#include <unistd.h>
35#include <time.h> 47#include <time.h>
36 48
49#if ENABLE_PTHREADS
50# include <pthread.h>
51#endif
52
37#include "netcompat.h" 53#include "netcompat.h"
38 54
39#include "gettext.h" 55#include "gettext.h"
40#include "pidfile.h" 56#include "pidfile.h"
41#include "dropin.h" 57#include "dropin.h"
122 log_to (LOGTO_SYSLOG); 138 log_to (LOGTO_SYSLOG);
123 } 139 }
124 else 140 else
125 log_to (LOGTO_SYSLOG | LOGTO_STDERR); 141 log_to (LOGTO_SYSLOG | LOGTO_STDERR);
126 142
127 slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); 143 slog (L_INFO, _("gvpe daemon %s (%s %s) starting up."), VERSION, __DATE__, __TIME__);
128 144
129 return 0; 145 return 0;
130} 146}
131 147
148/*****************************************************************************/
149
150pid_t
132bool run_script (const run_script_cb &cb, bool wait) 151run_script (const run_script_cb &cb, bool wait)
133{ 152{
153 sigset_t oldset;
154
134 if (wait) 155 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. 156 {
157 sigset_t sigchld;
158 sigemptyset (&sigchld);
159 sigaddset (&sigchld, SIGCHLD);
160 sigprocmask (SIG_BLOCK, &sigchld, &oldset);
161 }
136 162
137 int pid = fork (); 163 pid_t pid = fork ();
138 164
139 if (pid == 0) 165 if (pid == 0)
140 { 166 {
167 sigprocmask (SIG_SETMASK, &oldset, 0);
168
141 execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0); 169 execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0);
142 exit (EXIT_FAILURE); 170 exit (EXIT_FAILURE);
143 } 171 }
144 else if (pid > 0) 172 else if (pid > 0)
145 { 173 {
146 if (wait) 174 if (wait)
147 { 175 {
148 int status; 176 int status;
149 int res = waitpid (pid, &status, 0); 177 int res = waitpid (pid, &status, 0);
150 178
151 signal (SIGCHLD, SIG_IGN); 179 sigprocmask (SIG_SETMASK, &oldset, 0);
152 180
153 if (res < 0) 181 if (res < 0)
154 { 182 {
155 slog (L_WARN, _("waiting for an external command failed: %s."), 183 slog (L_WARN, _("waiting for an external command failed: %s."),
156 strerror (errno)); 184 strerror (errno));
157 return false; 185 return 0;
158 } 186 }
159 else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS) 187 else if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
160 { 188 {
161 slog (L_WARN, _("external command returned with exit status %d (%04x)."), 189 slog (L_WARN, _("external command returned with exit status %d (%04x)."),
162 WEXITSTATUS (status), status); 190 WEXITSTATUS (status), status);
163 return false; 191 return 0;
164 } 192 }
165 } 193 }
166 } 194 }
167 else 195 else
168 { 196 {
169 slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno)); 197 slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno));
170 exit (EXIT_FAILURE); 198 exit (EXIT_FAILURE);
171 } 199 }
172 200
201 return pid;
202}
203
204/*****************************************************************************/
205
206#if 0 /* not yet used */
207
208#if ENABLE_PTHREADS
209struct async_cb
210{
211 callback<void ()> work_cb;
212 callback<void ()> done_cb;
213};
214
215static ev::async async_done_w;
216static std::queue< callback<void ()> > async_q;
217
218static callback<void ()> work_cb;
219
220static void *
221async_exec (void *)
222{
223 work_cb ();
224 async_done_w.send ();
225
173 return true; 226 return 0;
174} 227}
228
229static void
230async_q_next ()
231{
232 work_cb = async_q.front (); async_q.pop ();
233
234 sigset_t fullsigset, oldsigset;
235 pthread_attr_t attr;
236 pthread_t tid;
237
238 pthread_attr_init (&attr);
239 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
240 //pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN);
241 sigfillset (&fullsigset);
242 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
243
244 if (pthread_create (&tid, &attr, async_exec, 0))
245 async_exec (0);
246
247 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
248 pthread_attr_destroy (&attr);
249}
250
251namespace {
252 void
253 async_done (ev::async &w, int revents)
254 {
255 callback<void ()> done_cb = async_q.front (); async_q.pop ();
256
257 if (async_q.empty ())
258 async_done_w.stop ();
259 else
260 async_q_next ();
261
262 done_cb ();
263 }
264};
265
266void
267async (callback<void ()> work_cb, callback<void ()> done_cb)
268{
269 bool was_empty = async_q.empty ();
270
271 async_q.push (work_cb);
272 async_q.push (done_cb);
273
274 if (was_empty)
275 {
276 async_done_w.set<async_done> ();
277 async_done_w.start ();
278 async_q_next ();
279 }
280}
281
282#else
283
284void
285async (callback<void ()> work_cb, callback<void ()> done_cb)
286{
287 work_cb ();
288 done_cb ();
289}
290
291#endif
292
293#endif
294
295/*****************************************************************************/
175 296
176#if ENABLE_HTTP_PROXY 297#if ENABLE_HTTP_PROXY
177// works like strdup 298// works like strdup
178u8 * 299u8 *
179base64_encode (const u8 *data, unsigned int len) 300base64_encode (const u8 *data, unsigned int len)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines