1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
3 | Copyright (C) 2003-2008 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 | |
… | |
… | |
35 | |
35 | |
36 | #include <cstdio> |
36 | #include <cstdio> |
37 | #include <cstdlib> |
37 | #include <cstdlib> |
38 | #include <cstring> |
38 | #include <cstring> |
39 | |
39 | |
|
|
40 | #include <queue> |
|
|
41 | |
40 | #include <errno.h> |
42 | #include <errno.h> |
41 | #include <signal.h> |
43 | #include <signal.h> |
42 | #include <sys/types.h> |
44 | #include <sys/types.h> |
43 | #include <sys/wait.h> |
45 | #include <sys/wait.h> |
44 | #include <unistd.h> |
46 | #include <unistd.h> |
45 | #include <time.h> |
47 | #include <time.h> |
46 | |
48 | |
|
|
49 | #if ENABLE_PTHREADS |
|
|
50 | # include <pthread.h> |
|
|
51 | #endif |
|
|
52 | |
47 | #include "netcompat.h" |
53 | #include "netcompat.h" |
48 | |
54 | |
49 | #include "gettext.h" |
55 | #include "gettext.h" |
50 | #include "pidfile.h" |
56 | #include "pidfile.h" |
51 | #include "dropin.h" |
57 | #include "dropin.h" |
… | |
… | |
136 | |
142 | |
137 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting up."), VERSION, __DATE__, __TIME__); |
143 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting up."), VERSION, __DATE__, __TIME__); |
138 | |
144 | |
139 | return 0; |
145 | return 0; |
140 | } |
146 | } |
|
|
147 | |
|
|
148 | /*****************************************************************************/ |
141 | |
149 | |
142 | pid_t |
150 | pid_t |
143 | run_script (const run_script_cb &cb, bool wait) |
151 | run_script (const run_script_cb &cb, bool wait) |
144 | { |
152 | { |
145 | sigset_t oldset; |
153 | sigset_t oldset; |
… | |
… | |
191 | } |
199 | } |
192 | |
200 | |
193 | return pid; |
201 | return pid; |
194 | } |
202 | } |
195 | |
203 | |
|
|
204 | /*****************************************************************************/ |
|
|
205 | |
|
|
206 | #if 0 /* not yet used */ |
|
|
207 | |
|
|
208 | #if ENABLE_PTHREADS |
|
|
209 | struct async_cb |
|
|
210 | { |
|
|
211 | callback<void ()> work_cb; |
|
|
212 | callback<void ()> done_cb; |
|
|
213 | }; |
|
|
214 | |
|
|
215 | static ev::async async_done_w; |
|
|
216 | static std::queue< callback<void ()> > async_q; |
|
|
217 | |
|
|
218 | static callback<void ()> work_cb; |
|
|
219 | |
|
|
220 | static void * |
|
|
221 | async_exec (void *) |
|
|
222 | { |
|
|
223 | work_cb (); |
|
|
224 | async_done_w.send (); |
|
|
225 | |
|
|
226 | return 0; |
|
|
227 | } |
|
|
228 | |
|
|
229 | static void |
|
|
230 | async_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 | |
|
|
251 | namespace { |
|
|
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 | |
|
|
266 | void |
|
|
267 | async (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 | |
|
|
284 | void |
|
|
285 | async (callback<void ()> work_cb, callback<void ()> done_cb) |
|
|
286 | { |
|
|
287 | work_cb (); |
|
|
288 | done_cb (); |
|
|
289 | } |
|
|
290 | |
|
|
291 | #endif |
|
|
292 | |
|
|
293 | #endif |
|
|
294 | |
|
|
295 | /*****************************************************************************/ |
|
|
296 | |
196 | #if ENABLE_HTTP_PROXY |
297 | #if ENABLE_HTTP_PROXY |
197 | // works like strdup |
298 | // works like strdup |
198 | u8 * |
299 | u8 * |
199 | base64_encode (const u8 *data, unsigned int len) |
300 | base64_encode (const u8 *data, unsigned int len) |
200 | { |
301 | { |