forked from dcantrell/vpncwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.c
253 lines (210 loc) · 6.82 KB
/
proc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* proc.c
* Process control functions for vpncwatch
* Author: David Cantrell <[email protected]>
*
* Adapted from vpnc-watch.py by Gary Benson <[email protected]>
* (Python is TOO BIG for a 16M OpenWRT router.)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include "vpncwatch.h"
/* Implements which(1) as a function */
char *which(char *cmd) {
char *dir = NULL, *path = NULL, *ret = NULL;
assert(cmd != NULL);
/* did the user specify the full path to cmd by chance? */
if (access(cmd, X_OK) == 0) {
return cmd;
}
/* strtok() modifies our original, so we need to make a copy of the
* PATH environment variable since strtok() will trample it for our
* process
*/
if (getenv("PATH") == NULL) {
syslog(LOG_ERR, "missing PATH in environment table");
return NULL;
} else {
path = strdup(getenv("PATH"));
}
/* check each directory in the path with our command */
dir = strtok(path, ":");
while (dir != NULL) {
if (ret != NULL) {
free(ret);
ret = NULL;
}
if (asprintf(&ret, "%s/%s", dir, cmd) == -1) {
syslog(LOG_ERR, "memory allocation: %s:%d", __func__, __LINE__);
abort();
}
if (access(ret, X_OK) == 0) {
return ret;
} else {
dir = strtok(NULL, ":");
}
}
syslog(LOG_ERR, "%s not found in PATH", cmd);
return NULL;
}
/* Return PID of specified command. This function assumes that only one */
/* process is running by that name. */
pid_t pidof(char *cmd) {
pid_t ret = 0;
char *realcmd = NULL, *realproc = NULL, *realwatch = NULL, *procpath = NULL;
char cmdbuf[PATH_MAX];
char procbuf[PATH_MAX];
char watchbuf[PATH_MAX];
char *ep = NULL;
DIR *dp = NULL;
struct dirent *de = NULL;
assert(cmd != NULL);
if ((realcmd = realpath(which(cmd), cmdbuf)) == NULL) {
syslog(LOG_ERR, "realpath failure on realcmd: %s", strerror(errno));
return -1;
}
if ((realwatch = realpath(which("vpncwatch"), watchbuf)) == NULL) {
syslog(LOG_ERR, "realpath failure on realwatch: %s", strerror(errno));
return -1;
}
if ((dp = opendir(PROCDIR)) == NULL) {
syslog(LOG_ERR, "opendir failure on %s: %s", PROCDIR, strerror(errno));
return -1;
}
while ((de = readdir(dp)) != NULL) {
/* normally, we'd check d_type to see if it's DT_DIR before doing
* stuff, but uClibc does not set d_type, so we just go anyway if
* we can perform an strtol() and get something larger than 0.
*/
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
continue;
}
if (procpath != NULL) {
free(procpath);
}
if (asprintf(&procpath, "%s/%s/exe", PROCDIR, de->d_name) == -1) {
syslog(LOG_ERR, "memory allocation: %s:%d", __func__, __LINE__);
abort();
}
if ((realproc = realpath(procpath, procbuf)) == NULL) {
/* exe not found, which probably means it's a kernel process */
continue;
}
if ((strstr(realproc, realcmd) != NULL) &&
(strstr(realproc, realwatch) == NULL)) {
errno = 0;
ret = strtol(de->d_name, &ep, 10);
if (((ret == LONG_MIN || ret == LONG_MAX) && (errno == ERANGE)) ||
((ret == 0) && (errno == EINVAL))) {
syslog(LOG_ERR, "unable to convert %s to int", de->d_name);
break;
}
syslog(LOG_ERR, "found process %s (%d)", de->d_name, ret);
break;
}
}
if (closedir(dp)) {
syslog(LOG_ERR, "closedir failure on %s: %s", PROCDIR, strerror(errno));
return -1;
}
free(procpath);
return ret;
}
/* Check for a /proc entry for the given PID */
int is_running(int pid) {
char *pidpath = NULL;
if (asprintf(&pidpath, "%s/%d/exe", PROCDIR, pid) == -1) {
syslog(LOG_ERR, "memory allocation: %s:%d", __func__, __LINE__);
abort();
}
if (access(pidpath, R_OK) == -1) {
return 0;
} else {
return 1;
}
}
/* start the command */
pid_t start_cmd(char *cmd, char *cmdpath, char **argv) {
int status = 0;
int cmdpid = 0;
pid_t pid = 0;
syslog(LOG_NOTICE, "starting %s", cmdpath);
if ((pid = fork()) == 0) {
if (execv(cmdpath, argv) == -1) {
syslog(LOG_ERR, "execv failure in %s: %s", __func__, strerror(errno));
exit(EXIT_FAILURE);
}
} else if (pid == -1) {
syslog(LOG_ERR, "fork failure in %s: %s", __func__, strerror(errno));
return -1;
} else {
if (waitpid(pid, &status, 0) == -1) {
syslog(LOG_ERR, "waitpid failure in %s: %s", __func__, strerror(errno));
return -1;
}
if (WIFSIGNALED(status)) {
syslog(LOG_ERR, "%s killed with signal %d", cmd, WTERMSIG(status));
return -1;
}
if (!WIFEXITED(status)) {
syslog(LOG_ERR, "waitpid returned %d", status);
return -1;
}
if ((cmdpid = pidof(cmd)) == -1) {
syslog(LOG_ERR, "%s is not running", cmd);
} else {
syslog(LOG_NOTICE, "%s started", cmd);
}
return cmdpid;
}
/* never reached */
return -1;
}
/* stop the command */
void stop_cmd(char *cmd, int cmdpid) {
int s, i;
syslog(LOG_NOTICE, "stopping %s", cmd);
/* try SIGTERM, then SIGKILL */
for (s = SIGTERM; s >= SIGKILL; s -= (SIGTERM - SIGKILL)) {
if (!kill(cmdpid, s)) {
return;
}
for (i = 0; i < 30; i++) {
if (!is_running(cmdpid)) {
break;
}
sleep(1);
}
if (!is_running(cmdpid)) {
return;
}
}
if (is_running(cmdpid)) {
syslog(LOG_ERR, "%s (%d) did not die!", cmd, cmdpid);
}
return;
}