ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/util.C
Revision: 1.11
Committed: Thu Oct 16 02:41:21 2003 UTC (20 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: poll-based-iom, VPE_1_2, VPE_1_4
Changes since 1.10: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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