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> |
… | |
… | |
30 | #include <unistd.h> |
34 | #include <unistd.h> |
31 | #include <time.h> |
35 | #include <time.h> |
32 | |
36 | |
33 | #include "netcompat.h" |
37 | #include "netcompat.h" |
34 | |
38 | |
35 | #include "gettext.h" |
|
|
36 | #include "pidfile.h" |
39 | #include "pidfile.h" |
37 | #include "dropin.h" |
40 | #include "dropin.h" |
38 | |
41 | |
39 | #include "global.h" |
42 | #include "global.h" |
40 | #include "conf.h" |
43 | #include "conf.h" |
… | |
… | |
44 | int |
47 | int |
45 | write_pidfile (void) |
48 | write_pidfile (void) |
46 | { |
49 | { |
47 | int pid; |
50 | int pid; |
48 | |
51 | |
49 | pid = check_pid (pidfilename); |
52 | pid = check_pid (conf.pidfilename); |
50 | |
53 | |
51 | if (pid) |
54 | if (pid) |
52 | { |
55 | { |
53 | 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); |
54 | return 1; |
57 | return 1; |
55 | } |
58 | } |
56 | |
59 | |
57 | /* if it's locked, write-protected, or whatever */ |
60 | /* if it's locked, write-protected, or whatever */ |
58 | if (!write_pid (pidfilename)) |
61 | if (!write_pid (conf.pidfilename)) |
59 | return 1; |
62 | return 1; |
60 | |
63 | |
61 | return 0; |
64 | return 0; |
62 | } |
65 | } |
63 | |
66 | |
64 | int |
67 | int |
65 | kill_other (int signal) |
68 | kill_other (int signal) |
66 | { |
69 | { |
67 | int pid; |
70 | int pid; |
68 | |
71 | |
69 | pid = read_pid (pidfilename); |
72 | pid = read_pid (conf.pidfilename); |
70 | |
73 | |
71 | if (!pid) |
74 | if (!pid) |
72 | { |
75 | { |
73 | fprintf (stderr, _("No other vped is running.\n")); |
76 | fprintf (stderr, _("No other gvpe daemon is running.\n")); |
74 | return 1; |
77 | return 1; |
75 | } |
78 | } |
76 | |
79 | |
77 | errno = 0; /* No error, sometimes errno is only changed on error */ |
80 | errno = 0; /* No error, sometimes errno is only changed on error */ |
78 | |
81 | |
79 | /* ESRCH is returned when no process with that pid is found */ |
82 | /* ESRCH is returned when no process with that pid is found */ |
80 | if (kill (pid, signal) && errno == ESRCH) |
83 | if (kill (pid, signal) && errno == ESRCH) |
81 | { |
84 | { |
82 | fprintf (stderr, _("The vped is no longer running. ")); |
85 | fprintf (stderr, _("The gvpe daemon is no longer running. ")); |
83 | |
86 | |
84 | fprintf (stderr, _("Removing stale lock file.\n")); |
87 | fprintf (stderr, _("Removing stale lock file.\n")); |
85 | remove_pid (pidfilename); |
88 | remove_pid (conf.pidfilename); |
86 | } |
89 | } |
87 | |
90 | |
88 | return 0; |
91 | return 0; |
89 | } |
92 | } |
90 | |
93 | |
… | |
… | |
110 | return -1; |
113 | return -1; |
111 | } |
114 | } |
112 | |
115 | |
113 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
116 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
114 | |
117 | |
115 | if (!write_pid (pidfilename)) |
118 | if (!write_pid (conf.pidfilename)) |
116 | return -1; |
119 | return -1; |
117 | |
120 | |
118 | log_to (LOGTO_SYSLOG); |
121 | log_to (LOGTO_SYSLOG); |
119 | } |
122 | } |
120 | else |
123 | else |
121 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
124 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
122 | |
125 | |
123 | slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
126 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
124 | |
127 | |
125 | return 0; |
128 | 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 | } |
129 | } |
137 | |
130 | |
138 | void run_script (const run_script_cb &cb, bool wait) |
131 | void run_script (const run_script_cb &cb, bool wait) |
139 | { |
132 | { |
140 | int pid; |
133 | int pid; |
… | |
… | |
142 | if ((pid = fork ()) == 0) |
135 | if ((pid = fork ()) == 0) |
143 | { |
136 | { |
144 | char *filename; |
137 | char *filename; |
145 | asprintf (&filename, "%s/%s", confbase, cb()); |
138 | asprintf (&filename, "%s/%s", confbase, cb()); |
146 | execl (filename, filename, (char *) 0); |
139 | execl (filename, filename, (char *) 0); |
147 | exit (255); |
140 | exit (126); |
148 | } |
141 | } |
149 | else if (pid > 0) |
142 | else if (pid > 0) |
150 | { |
143 | { |
151 | if (wait) |
144 | if (wait) |
152 | { |
145 | { |