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