1 | /* |
1 | /* |
2 | util.C -- process management and other utility functions |
2 | util.C -- process management and other utility functions |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | Some of these are taken from tinc, see the AUTHORS file. |
5 | Some of these are taken from tinc, see the AUTHORS file. |
6 | |
6 | |
|
|
7 | This file is part of GVPE. |
|
|
8 | |
7 | This program is free software; you can redistribute it and/or modify |
9 | GVPE is free software; you can redistribute it and/or modify |
8 | 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 |
9 | the Free Software Foundation; either version 2 of the License, or |
11 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
12 | (at your option) any later version. |
11 | |
13 | |
12 | 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, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
17 | GNU General Public License for more details. |
16 | |
18 | |
17 | 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 |
18 | along with this program; if not, write to the Free Software |
20 | along with gvpe; if not, write to the Free Software |
19 | 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 |
20 | */ |
22 | */ |
21 | |
23 | |
22 | #include "config.h" |
24 | #include "config.h" |
23 | |
25 | |
24 | #include <cstdio> |
26 | #include <cstdio> |
|
|
27 | #include <cstdlib> |
25 | #include <cstring> |
28 | #include <cstring> |
26 | |
29 | |
27 | #include <errno.h> |
30 | #include <errno.h> |
28 | #include <signal.h> |
31 | #include <signal.h> |
29 | #include <sys/types.h> |
32 | #include <sys/types.h> |
… | |
… | |
45 | int |
48 | int |
46 | write_pidfile (void) |
49 | write_pidfile (void) |
47 | { |
50 | { |
48 | int pid; |
51 | int pid; |
49 | |
52 | |
50 | pid = check_pid (pidfilename); |
53 | pid = check_pid (conf.pidfilename); |
51 | |
54 | |
52 | if (pid) |
55 | if (pid) |
53 | { |
56 | { |
54 | fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); |
57 | fprintf (stderr, _("A gvpe daemon is already running with pid %d.\n"), pid); |
55 | return 1; |
58 | return 1; |
56 | } |
59 | } |
57 | |
60 | |
58 | /* if it's locked, write-protected, or whatever */ |
61 | /* if it's locked, write-protected, or whatever */ |
59 | if (!write_pid (pidfilename)) |
62 | if (!write_pid (conf.pidfilename)) |
60 | return 1; |
63 | return 1; |
61 | |
64 | |
62 | return 0; |
65 | return 0; |
63 | } |
66 | } |
64 | |
67 | |
65 | int |
68 | int |
66 | kill_other (int signal) |
69 | kill_other (int signal) |
67 | { |
70 | { |
68 | int pid; |
71 | int pid; |
69 | |
72 | |
70 | pid = read_pid (pidfilename); |
73 | pid = read_pid (conf.pidfilename); |
71 | |
74 | |
72 | if (!pid) |
75 | if (!pid) |
73 | { |
76 | { |
74 | fprintf (stderr, _("No other vped is running.\n")); |
77 | fprintf (stderr, _("No other gvpe daemon is running.\n")); |
75 | return 1; |
78 | return 1; |
76 | } |
79 | } |
77 | |
80 | |
78 | errno = 0; /* No error, sometimes errno is only changed on error */ |
81 | errno = 0; /* No error, sometimes errno is only changed on error */ |
79 | |
82 | |
80 | /* ESRCH is returned when no process with that pid is found */ |
83 | /* ESRCH is returned when no process with that pid is found */ |
81 | if (kill (pid, signal) && errno == ESRCH) |
84 | if (kill (pid, signal) && errno == ESRCH) |
82 | { |
85 | { |
83 | fprintf (stderr, _("The vped is no longer running. ")); |
86 | fprintf (stderr, _("The gvpe daemon is no longer running. ")); |
84 | |
87 | |
85 | fprintf (stderr, _("Removing stale lock file.\n")); |
88 | fprintf (stderr, _("Removing stale lock file.\n")); |
86 | remove_pid (pidfilename); |
89 | remove_pid (conf.pidfilename); |
87 | } |
90 | } |
88 | |
91 | |
89 | return 0; |
92 | return 0; |
90 | } |
93 | } |
91 | |
94 | |
… | |
… | |
111 | return -1; |
114 | return -1; |
112 | } |
115 | } |
113 | |
116 | |
114 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
117 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
115 | |
118 | |
116 | if (!write_pid (pidfilename)) |
119 | if (!write_pid (conf.pidfilename)) |
117 | return -1; |
120 | return -1; |
118 | |
121 | |
119 | log_to (LOGTO_SYSLOG); |
122 | log_to (LOGTO_SYSLOG); |
120 | } |
123 | } |
121 | else |
124 | else |
122 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
125 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
123 | |
126 | |
124 | slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
127 | slog (L_INFO, _("gvpe daemon %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
125 | |
128 | |
126 | return 0; |
129 | 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 | } |
130 | } |
138 | |
131 | |
139 | void run_script (const run_script_cb &cb, bool wait) |
132 | void run_script (const run_script_cb &cb, bool wait) |
140 | { |
133 | { |
141 | int pid; |
134 | int pid; |
… | |
… | |
143 | if ((pid = fork ()) == 0) |
136 | if ((pid = fork ()) == 0) |
144 | { |
137 | { |
145 | char *filename; |
138 | char *filename; |
146 | asprintf (&filename, "%s/%s", confbase, cb()); |
139 | asprintf (&filename, "%s/%s", confbase, cb()); |
147 | execl (filename, filename, (char *) 0); |
140 | execl (filename, filename, (char *) 0); |
148 | exit (255); |
141 | exit (126); |
149 | } |
142 | } |
150 | else if (pid > 0) |
143 | else if (pid > 0) |
151 | { |
144 | { |
152 | if (wait) |
145 | if (wait) |
153 | { |
146 | { |