1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
3 | |
3 | |
4 | Most of these are taken from tinc, see the AUTHORS file. |
4 | Some of these are taken from tinc, see the AUTHORS file. |
5 | |
5 | |
6 | This program is free software; you can redistribute it and/or modify |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
9 | (at your option) any later version. |
… | |
… | |
24 | #include <cstring> |
24 | #include <cstring> |
25 | |
25 | |
26 | #include <errno.h> |
26 | #include <errno.h> |
27 | #include <signal.h> |
27 | #include <signal.h> |
28 | #include <sys/types.h> |
28 | #include <sys/types.h> |
|
|
29 | #include <sys/wait.h> |
29 | #include <unistd.h> |
30 | #include <unistd.h> |
30 | #include <time.h> |
31 | #include <time.h> |
31 | #include <sys/socket.h> |
32 | #include <sys/socket.h> |
32 | #include <netinet/in.h> |
33 | #include <netinet/in.h> |
33 | #include <arpa/inet.h> |
34 | #include <arpa/inet.h> |
34 | |
35 | |
|
|
36 | |
35 | #include <sys/mman.h> |
37 | #include <sys/mman.h> |
36 | |
|
|
37 | #include <openssl/rand.h> |
|
|
38 | #include <openssl/rsa.h> |
|
|
39 | #include <openssl/pem.h> |
|
|
40 | #include <openssl/evp.h> |
|
|
41 | |
38 | |
42 | #include "gettext.h" |
39 | #include "gettext.h" |
43 | #include "pidfile.h" |
40 | #include "pidfile.h" |
44 | #include "dropin.h" |
41 | #include "dropin.h" |
45 | |
42 | |
46 | #include "global.h" |
43 | #include "global.h" |
47 | #include "conf.h" |
44 | #include "conf.h" |
|
|
45 | #include "util.h" |
48 | #include "slog.h" |
46 | #include "slog.h" |
49 | #include "protocol.h" |
|
|
50 | |
47 | |
51 | int |
48 | int |
52 | write_pidfile (void) |
49 | write_pidfile (void) |
53 | { |
50 | { |
54 | int pid; |
51 | int pid; |
… | |
… | |
140 | |
137 | |
141 | if (!confbase) |
138 | if (!confbase) |
142 | asprintf (&confbase, "%s/vpe", CONFDIR); |
139 | asprintf (&confbase, "%s/vpe", CONFDIR); |
143 | } |
140 | } |
144 | |
141 | |
145 | sockinfo::operator const char *() |
142 | void run_script (const run_script_cb &cb, bool wait) |
146 | { |
143 | { |
147 | static char hostport[15 + 1 + 5 + 1]; |
144 | int pid; |
148 | in_addr ia = { host }; |
|
|
149 | |
145 | |
150 | sprintf (hostport, "%.15s:%d", inet_ntoa (ia), ntohs (port) & 0xffff); |
146 | if ((pid = fork ()) == 0) |
151 | |
147 | { |
152 | return hostport; |
148 | char *filename; |
|
|
149 | asprintf (&filename, "%s/%s", confbase, cb()); |
|
|
150 | execl (filename, filename, (char *) 0); |
|
|
151 | exit (255); |
|
|
152 | } |
|
|
153 | else if (pid > 0) |
|
|
154 | { |
|
|
155 | if (wait) |
|
|
156 | { |
|
|
157 | waitpid (pid, 0, 0); |
|
|
158 | /* TODO: check status */ |
|
|
159 | } |
|
|
160 | } |
153 | } |
161 | } |
154 | |
|
|