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 | |
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 | |
6 | This program is free software; you can redistribute it and/or modify |
7 | 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 | it under the terms of the GNU General Public License as published by |
… | |
… | |
19 | */ |
20 | */ |
20 | |
21 | |
21 | #include "config.h" |
22 | #include "config.h" |
22 | |
23 | |
23 | #include <cstdio> |
24 | #include <cstdio> |
|
|
25 | #include <cstdlib> |
24 | #include <cstring> |
26 | #include <cstring> |
25 | |
27 | |
26 | #include <errno.h> |
28 | #include <errno.h> |
27 | #include <signal.h> |
29 | #include <signal.h> |
28 | #include <sys/types.h> |
30 | #include <sys/types.h> |
… | |
… | |
44 | int |
46 | int |
45 | write_pidfile (void) |
47 | write_pidfile (void) |
46 | { |
48 | { |
47 | int pid; |
49 | int pid; |
48 | |
50 | |
49 | pid = check_pid (pidfilename); |
51 | pid = check_pid (conf.pidfilename); |
50 | |
52 | |
51 | if (pid) |
53 | if (pid) |
52 | { |
54 | { |
53 | fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); |
55 | fprintf (stderr, _("A vped is already running with pid %d.\n"), pid); |
54 | return 1; |
56 | return 1; |
55 | } |
57 | } |
56 | |
58 | |
57 | /* if it's locked, write-protected, or whatever */ |
59 | /* if it's locked, write-protected, or whatever */ |
58 | if (!write_pid (pidfilename)) |
60 | if (!write_pid (conf.pidfilename)) |
59 | return 1; |
61 | return 1; |
60 | |
62 | |
61 | return 0; |
63 | return 0; |
62 | } |
64 | } |
63 | |
65 | |
64 | int |
66 | int |
65 | kill_other (int signal) |
67 | kill_other (int signal) |
66 | { |
68 | { |
67 | int pid; |
69 | int pid; |
68 | |
70 | |
69 | pid = read_pid (pidfilename); |
71 | pid = read_pid (conf.pidfilename); |
70 | |
72 | |
71 | if (!pid) |
73 | if (!pid) |
72 | { |
74 | { |
73 | fprintf (stderr, _("No other vped is running.\n")); |
75 | fprintf (stderr, _("No other vped is running.\n")); |
74 | return 1; |
76 | return 1; |
… | |
… | |
80 | if (kill (pid, signal) && errno == ESRCH) |
82 | if (kill (pid, signal) && errno == ESRCH) |
81 | { |
83 | { |
82 | fprintf (stderr, _("The vped is no longer running. ")); |
84 | fprintf (stderr, _("The vped is no longer running. ")); |
83 | |
85 | |
84 | fprintf (stderr, _("Removing stale lock file.\n")); |
86 | fprintf (stderr, _("Removing stale lock file.\n")); |
85 | remove_pid (pidfilename); |
87 | remove_pid (conf.pidfilename); |
86 | } |
88 | } |
87 | |
89 | |
88 | return 0; |
90 | return 0; |
89 | } |
91 | } |
90 | |
92 | |
… | |
… | |
110 | return -1; |
112 | return -1; |
111 | } |
113 | } |
112 | |
114 | |
113 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
115 | /* Now UPDATE the pid in the pidfile, because we changed it... */ |
114 | |
116 | |
115 | if (!write_pid (pidfilename)) |
117 | if (!write_pid (conf.pidfilename)) |
116 | return -1; |
118 | return -1; |
117 | |
119 | |
118 | log_to (LOGTO_SYSLOG); |
120 | log_to (LOGTO_SYSLOG); |
119 | } |
121 | } |
120 | else |
122 | else |
121 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
123 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
122 | |
124 | |
123 | slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
125 | slog (L_INFO, _("vped %s (%s %s) starting"), VERSION, __DATE__, __TIME__); |
124 | |
126 | |
125 | return 0; |
127 | 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 | } |
128 | } |
137 | |
129 | |
138 | void run_script (const run_script_cb &cb, bool wait) |
130 | void run_script (const run_script_cb &cb, bool wait) |
139 | { |
131 | { |
140 | int pid; |
132 | int pid; |
… | |
… | |
142 | if ((pid = fork ()) == 0) |
134 | if ((pid = fork ()) == 0) |
143 | { |
135 | { |
144 | char *filename; |
136 | char *filename; |
145 | asprintf (&filename, "%s/%s", confbase, cb()); |
137 | asprintf (&filename, "%s/%s", confbase, cb()); |
146 | execl (filename, filename, (char *) 0); |
138 | execl (filename, filename, (char *) 0); |
147 | exit (255); |
139 | exit (126); |
148 | } |
140 | } |
149 | else if (pid > 0) |
141 | else if (pid > 0) |
150 | { |
142 | { |
151 | if (wait) |
143 | if (wait) |
152 | { |
144 | { |