1 |
/* |
2 |
util.C -- process management and other utility functions |
3 |
Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
Some of these are taken from tinc, see the AUTHORS file. |
6 |
|
7 |
This file is part of GVPE. |
8 |
|
9 |
GVPE is free software; you can redistribute it and/or modify |
10 |
it under the terms of the GNU General Public License as published by |
11 |
the Free Software Foundation; either version 2 of the License, or |
12 |
(at your option) any later version. |
13 |
|
14 |
This program is distributed in the hope that it will be useful, |
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
GNU General Public License for more details. |
18 |
|
19 |
You should have received a copy of the GNU General Public License |
20 |
along with gvpe; if not, write to the Free Software |
21 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 |
*/ |
23 |
|
24 |
#include "config.h" |
25 |
|
26 |
#include <cstdio> |
27 |
#include <cstdlib> |
28 |
#include <cstring> |
29 |
|
30 |
#include <errno.h> |
31 |
#include <signal.h> |
32 |
#include <sys/types.h> |
33 |
#include <sys/wait.h> |
34 |
#include <unistd.h> |
35 |
#include <time.h> |
36 |
|
37 |
#include "netcompat.h" |
38 |
|
39 |
#include "gettext.h" |
40 |
#include "pidfile.h" |
41 |
#include "dropin.h" |
42 |
|
43 |
#include "global.h" |
44 |
#include "conf.h" |
45 |
#include "util.h" |
46 |
#include "slog.h" |
47 |
|
48 |
int |
49 |
write_pidfile (void) |
50 |
{ |
51 |
int pid; |
52 |
|
53 |
pid = check_pid (conf.pidfilename); |
54 |
|
55 |
if (pid) |
56 |
{ |
57 |
fprintf (stderr, _("A gvpe daemon is already running with pid %d.\n"), pid); |
58 |
return 1; |
59 |
} |
60 |
|
61 |
/* if it's locked, write-protected, or whatever */ |
62 |
if (!write_pid (conf.pidfilename)) |
63 |
return 1; |
64 |
|
65 |
return 0; |
66 |
} |
67 |
|
68 |
int |
69 |
kill_other (int signal) |
70 |
{ |
71 |
int pid; |
72 |
|
73 |
pid = read_pid (conf.pidfilename); |
74 |
|
75 |
if (!pid) |
76 |
{ |
77 |
fprintf (stderr, _("No other gvpe daemon is running.\n")); |
78 |
return 1; |
79 |
} |
80 |
|
81 |
errno = 0; /* No error, sometimes errno is only changed on error */ |
82 |
|
83 |
/* ESRCH is returned when no process with that pid is found */ |
84 |
if (kill (pid, signal) && errno == ESRCH) |
85 |
{ |
86 |
fprintf (stderr, _("The gvpe daemon is no longer running. ")); |
87 |
|
88 |
fprintf (stderr, _("Removing stale lock file.\n")); |
89 |
remove_pid (conf.pidfilename); |
90 |
} |
91 |
|
92 |
return 0; |
93 |
} |
94 |
|
95 |
int |
96 |
detach (int do_detach) |
97 |
{ |
98 |
/* First check if we can open a fresh new pidfile */ |
99 |
|
100 |
if (write_pidfile ()) |
101 |
return -1; |
102 |
|
103 |
/* If we succeeded in doing that, detach */ |
104 |
|
105 |
log_to (0); |
106 |
|
107 |
if (do_detach) |
108 |
{ |
109 |
if (daemon (0, 0) < 0) |
110 |
{ |
111 |
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
112 |
|
113 |
slog (L_ERR, _("couldn't detach from terminal: %s"), strerror (errno)); |
114 |
return -1; |
115 |
} |
116 |
|
117 |
/* Now UPDATE the pid in the pidfile, because we changed it... */ |
118 |
|
119 |
if (!write_pid (conf.pidfilename)) |
120 |
return -1; |
121 |
|
122 |
log_to (LOGTO_SYSLOG); |
123 |
} |
124 |
else |
125 |
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
126 |
|
127 |
slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
128 |
|
129 |
return 0; |
130 |
} |
131 |
|
132 |
bool run_script (const run_script_cb &cb, bool wait) |
133 |
{ |
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. |
136 |
|
137 |
int pid = fork (); |
138 |
|
139 |
if (pid == 0) |
140 |
{ |
141 |
execl ("/bin/sh", "/bin/sh", "-c", cb (), (char *) 0); |
142 |
exit (EXIT_FAILURE); |
143 |
} |
144 |
else if (pid > 0) |
145 |
{ |
146 |
if (wait) |
147 |
{ |
148 |
int status; |
149 |
int res = waitpid (pid, &status, 0); |
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 |
} |
165 |
} |
166 |
} |
167 |
else |
168 |
{ |
169 |
slog (L_ERR, _("unable to fork, exiting: %s"), strerror (errno)); |
170 |
exit (EXIT_FAILURE); |
171 |
} |
172 |
|
173 |
return true; |
174 |
} |
175 |
|
176 |
#if ENABLE_HTTP_PROXY |
177 |
// works like strdup |
178 |
u8 * |
179 |
base64_encode (const u8 *data, unsigned int len) |
180 |
{ |
181 |
const static char base64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
182 |
|
183 |
unsigned int t, i; |
184 |
const u8 *end = data + len; |
185 |
u8 *res = new u8 [4 * ((len + 2) / 3) + 1]; |
186 |
u8 *out = res; |
187 |
|
188 |
while (data <= end - 3) |
189 |
{ |
190 |
t = (((data[0] << 8) | data[1]) << 8) | data[2]; |
191 |
data += 3; |
192 |
|
193 |
*out++ = base64[(t >> 18) & 0x3f]; |
194 |
*out++ = base64[(t >> 12) & 0x3f]; |
195 |
*out++ = base64[(t >> 6) & 0x3f]; |
196 |
*out++ = base64[(t ) & 0x3f]; |
197 |
} |
198 |
|
199 |
for (t = 0, i = 0; data < end; i++) |
200 |
t = (t << 8) | *data++; |
201 |
|
202 |
switch (i) |
203 |
{ |
204 |
case 2: |
205 |
*out++ = base64[(t >> 10) & 0x3f]; |
206 |
*out++ = base64[(t >> 4) & 0x3f]; |
207 |
*out++ = base64[(t << 2) & 0x3f]; |
208 |
*out++ = '='; |
209 |
break; |
210 |
case 1: |
211 |
*out++ = base64[(t >> 2) & 0x3f]; |
212 |
*out++ = base64[(t << 4) & 0x3f]; |
213 |
*out++ = '='; |
214 |
*out++ = '='; |
215 |
break; |
216 |
} |
217 |
|
218 |
*out++ = 0; |
219 |
|
220 |
return res; |
221 |
} |
222 |
#endif |
223 |
|
224 |
void |
225 |
id2mac (unsigned int id, void *m) |
226 |
{ |
227 |
mac &p = *(mac *)m; |
228 |
|
229 |
if (id) |
230 |
{ |
231 |
p[0] = 0xfe; |
232 |
p[1] = 0xfd; |
233 |
p[2] = 0x80; |
234 |
p[3] = 0x00; |
235 |
p[4] = id >> 8; |
236 |
p[5] = id; |
237 |
} |
238 |
else |
239 |
{ |
240 |
p[0] = 0xff; |
241 |
p[1] = 0xff; |
242 |
p[2] = 0xff; |
243 |
p[3] = 0xff; |
244 |
p[4] = 0xff; |
245 |
p[5] = 0xff; |
246 |
} |
247 |
} |
248 |
|