1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
|
|
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
4 | Some of these are taken from tinc, see the AUTHORS file. |
5 | Some of these are taken from tinc, see the AUTHORS file. |
5 | |
6 | |
|
|
7 | This file is part of GVPE. |
|
|
8 | |
6 | This program is free software; you can redistribute it and/or modify |
9 | GVPE is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
10 | 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 |
11 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
12 | (at your option) any later version. |
10 | |
13 | |
11 | This program is distributed in the hope that it will be useful, |
14 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
17 | GNU General Public License for more details. |
15 | |
18 | |
16 | You should have received a copy of the GNU General Public License |
19 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software |
20 | along with gvpe; if not, write to the Free Software |
18 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | */ |
22 | */ |
20 | |
23 | |
21 | #include "config.h" |
24 | #include "config.h" |
22 | |
25 | |
23 | #include <cstdio> |
26 | #include <cstdio> |
|
|
27 | #include <cstdlib> |
24 | #include <cstring> |
28 | #include <cstring> |
25 | |
29 | |
26 | #include <errno.h> |
30 | #include <errno.h> |
27 | #include <signal.h> |
31 | #include <signal.h> |
28 | #include <sys/types.h> |
32 | #include <sys/types.h> |
29 | #include <sys/wait.h> |
33 | #include <sys/wait.h> |
30 | #include <unistd.h> |
34 | #include <unistd.h> |
31 | #include <time.h> |
35 | #include <time.h> |
32 | |
36 | |
33 | #include <sys/mman.h> |
|
|
34 | |
|
|
35 | #include "netcompat.h" |
37 | #include "netcompat.h" |
36 | |
38 | |
37 | #include "gettext.h" |
|
|
38 | #include "pidfile.h" |
39 | #include "pidfile.h" |
39 | #include "dropin.h" |
40 | #include "dropin.h" |
40 | |
41 | |
41 | #include "global.h" |
42 | #include "global.h" |
42 | #include "conf.h" |
43 | #include "conf.h" |
… | |
… | |
46 | int |
47 | int |
47 | write_pidfile (void) |
48 | write_pidfile (void) |
48 | { |
49 | { |
49 | int pid; |
50 | int pid; |
50 | |
51 | |
51 | pid = check_pid (pidfilename); |
52 | pid = check_pid (conf.pidfilename); |
52 | |
53 | |
53 | if (pid) |
54 | if (pid) |
54 | { |
55 | { |
55 | fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); |
56 | fprintf (stderr, _("A gvpe daemon is already running with pid %d.\n"), pid); |
56 | return 1; |
57 | return 1; |
57 | } |
58 | } |
58 | |
59 | |
59 | /* if it's locked, write-protected, or whatever */ |
60 | /* if it's locked, write-protected, or whatever */ |
60 | if (!write_pid (pidfilename)) |
61 | if (!write_pid (conf.pidfilename)) |
61 | return 1; |
62 | return 1; |
62 | |
63 | |
63 | return 0; |
64 | return 0; |
64 | } |
65 | } |
65 | |
66 | |
66 | int |
67 | int |
67 | kill_other (int signal) |
68 | kill_other (int signal) |
68 | { |
69 | { |
69 | int pid; |
70 | int pid; |
70 | |
71 | |
71 | pid = read_pid (pidfilename); |
72 | pid = read_pid (conf.pidfilename); |
72 | |
73 | |
73 | if (!pid) |
74 | if (!pid) |
74 | { |
75 | { |
75 | fprintf (stderr, _("No other vped is running.\n")); |
76 | fprintf (stderr, _("No other gvpe daemon is running.\n")); |
76 | return 1; |
77 | return 1; |
77 | } |
78 | } |
78 | |
79 | |
79 | errno = 0; /* No error, sometimes errno is only changed on error */ |
80 | errno = 0; /* No error, sometimes errno is only changed on error */ |
80 | |
81 | |
81 | /* ESRCH is returned when no process with that pid is found */ |
82 | /* ESRCH is returned when no process with that pid is found */ |
82 | if (kill (pid, signal) && errno == ESRCH) |
83 | if (kill (pid, signal) && errno == ESRCH) |
83 | { |
84 | { |
84 | fprintf (stderr, _("The vped is no longer running. ")); |
85 | fprintf (stderr, _("The gvpe daemon is no longer running. ")); |
85 | |
86 | |
86 | fprintf (stderr, _("Removing stale lock file.\n")); |
87 | fprintf (stderr, _("Removing stale lock file.\n")); |
87 | remove_pid (pidfilename); |
88 | remove_pid (conf.pidfilename); |
88 | } |
89 | } |
89 | |
90 | |
90 | return 0; |
91 | return 0; |
91 | } |
92 | } |
92 | |
93 | |
… | |
… | |
112 | return -1; |
113 | return -1; |
113 | } |
114 | } |
114 | |
115 | |
115 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
116 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
116 | |
117 | |
117 | if (!write_pid (pidfilename)) |
118 | if (!write_pid (conf.pidfilename)) |
118 | return -1; |
119 | return -1; |
119 | |
120 | |
120 | log_to (LOGTO_SYSLOG); |
121 | log_to (LOGTO_SYSLOG); |
121 | } |
122 | } |
122 | else |
123 | else |
123 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
124 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
124 | |
125 | |
125 | slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
126 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
126 | |
127 | |
127 | return 0; |
128 | return 0; |
128 | } |
|
|
129 | |
|
|
130 | void |
|
|
131 | make_names (void) |
|
|
132 | { |
|
|
133 | if (!pidfilename) |
|
|
134 | pidfilename = LOCALSTATEDIR "/run/vped.pid"; |
|
|
135 | |
|
|
136 | if (!confbase) |
|
|
137 | asprintf (&confbase, "%s/vpe", CONFDIR); |
|
|
138 | } |
129 | } |
139 | |
130 | |
140 | void run_script (const run_script_cb &cb, bool wait) |
131 | void run_script (const run_script_cb &cb, bool wait) |
141 | { |
132 | { |
142 | int pid; |
133 | int pid; |
… | |
… | |
144 | if ((pid = fork ()) == 0) |
135 | if ((pid = fork ()) == 0) |
145 | { |
136 | { |
146 | char *filename; |
137 | char *filename; |
147 | asprintf (&filename, "%s/%s", confbase, cb()); |
138 | asprintf (&filename, "%s/%s", confbase, cb()); |
148 | execl (filename, filename, (char *) 0); |
139 | execl (filename, filename, (char *) 0); |
149 | exit (255); |
140 | exit (126); |
150 | } |
141 | } |
151 | else if (pid > 0) |
142 | else if (pid > 0) |
152 | { |
143 | { |
153 | if (wait) |
144 | if (wait) |
154 | { |
145 | { |
… | |
… | |
204 | |
195 | |
205 | return res; |
196 | return res; |
206 | } |
197 | } |
207 | #endif |
198 | #endif |
208 | |
199 | |
|
|
200 | void |
|
|
201 | id2mac (unsigned int id, void *m) |
|
|
202 | { |
|
|
203 | mac &p = *(mac *)m; |
|
|
204 | |
|
|
205 | if (id) |
|
|
206 | { |
|
|
207 | p[0] = 0xfe; |
|
|
208 | p[1] = 0xfd; |
|
|
209 | p[2] = 0x80; |
|
|
210 | p[3] = 0x00; |
|
|
211 | p[4] = id >> 8; |
|
|
212 | p[5] = id; |
|
|
213 | } |
|
|
214 | else |
|
|
215 | { |
|
|
216 | p[0] = 0xff; |
|
|
217 | p[1] = 0xff; |
|
|
218 | p[2] = 0xff; |
|
|
219 | p[3] = 0xff; |
|
|
220 | p[4] = 0xff; |
|
|
221 | p[5] = 0xff; |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |