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#include <sys/resource.h>
60
61
62#define WANT_PIDFILE 1
63#include "libbb.h"
64
65struct pid_list {
66 struct pid_list *next;
67 pid_t pid;
68};
69
70enum {
71 CTX_STOP = (1 << 0),
72 CTX_START = (1 << 1),
73 OPT_BACKGROUND = (1 << 2),
74 OPT_QUIET = (1 << 3),
75 OPT_TEST = (1 << 4),
76 OPT_MAKEPID = (1 << 5),
77 OPT_a = (1 << 6),
78 OPT_n = (1 << 7),
79 OPT_s = (1 << 8),
80 OPT_u = (1 << 9),
81 OPT_c = (1 << 10),
82 OPT_x = (1 << 11),
83 OPT_p = (1 << 12),
84 OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
85 OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
86 OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY,
87};
88#define QUIET (option_mask32 & OPT_QUIET)
89#define TEST (option_mask32 & OPT_TEST)
90
91struct globals {
92 struct pid_list *found_procs;
93 char *userspec;
94 char *cmdname;
95 char *execname;
96 char *pidfile;
97 char *execname_cmpbuf;
98 unsigned execname_sizeof;
99 int user_id;
100 smallint signal_nr;
101} FIX_ALIASING;
102#define G (*(struct globals*)&bb_common_bufsiz1)
103#define userspec (G.userspec )
104#define cmdname (G.cmdname )
105#define execname (G.execname )
106#define pidfile (G.pidfile )
107#define user_id (G.user_id )
108#define signal_nr (G.signal_nr )
109#define INIT_G() do { \
110 user_id = -1; \
111 signal_nr = 15; \
112} while (0)
113
114#ifdef OLDER_VERSION_OF_X
115
116
117
118
119static int pid_is_exec(pid_t pid)
120{
121 struct stat st;
122 char buf[sizeof("/proc/%u/exe") + sizeof(int)*3];
123
124 sprintf(buf, "/proc/%u/exe", (unsigned)pid);
125 if (stat(buf, &st) < 0)
126 return 0;
127 if (st.st_dev == execstat.st_dev
128 && st.st_ino == execstat.st_ino)
129 return 1;
130 return 0;
131}
132#endif
133
134static int pid_is_exec(pid_t pid)
135{
136 ssize_t bytes;
137 char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
138
139 sprintf(buf, "/proc/%u/cmdline", (unsigned)pid);
140 bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof);
141 if (bytes > 0) {
142 G.execname_cmpbuf[bytes] = '\0';
143 return strcmp(execname, G.execname_cmpbuf) == 0;
144 }
145 return 0;
146}
147
148static int pid_is_name(pid_t pid)
149{
150
151 char buf[32];
152 char *p, *pe;
153
154 sprintf(buf, "/proc/%u/stat", (unsigned)pid);
155 if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
156 return 0;
157 buf[sizeof(buf) - 1] = '\0';
158 p = strchr(buf, '(');
159 if (!p)
160 return 0;
161 pe = strrchr(++p, ')');
162 if (!pe)
163 return 0;
164 *pe = '\0';
165
166
167
168 if (strlen(p) >= COMM_LEN - 1)
169 return 0;
170 return strcmp(p, cmdname) == 0;
171}
172
173static int pid_is_user(int pid)
174{
175 struct stat sb;
176 char buf[sizeof("/proc/") + sizeof(int)*3];
177
178 sprintf(buf, "/proc/%u", (unsigned)pid);
179 if (stat(buf, &sb) != 0)
180 return 0;
181 return (sb.st_uid == (uid_t)user_id);
182}
183
184static void check(int pid)
185{
186 struct pid_list *p;
187
188 if (execname && !pid_is_exec(pid)) {
189 return;
190 }
191 if (cmdname && !pid_is_name(pid)) {
192 return;
193 }
194 if (userspec && !pid_is_user(pid)) {
195 return;
196 }
197 p = xmalloc(sizeof(*p));
198 p->next = G.found_procs;
199 p->pid = pid;
200 G.found_procs = p;
201}
202
203static void do_pidfile(void)
204{
205 FILE *f;
206 unsigned pid;
207
208 f = fopen_for_read(pidfile);
209 if (f) {
210 if (fscanf(f, "%u", &pid) == 1)
211 check(pid);
212 fclose(f);
213 } else if (errno != ENOENT)
214 bb_perror_msg_and_die("open pidfile %s", pidfile);
215}
216
217static void do_procinit(void)
218{
219 DIR *procdir;
220 struct dirent *entry;
221 int pid;
222
223 if (pidfile) {
224 do_pidfile();
225 return;
226 }
227
228 procdir = xopendir("/proc");
229
230 pid = 0;
231 while (1) {
232 errno = 0;
233 entry = readdir(procdir);
234
235
236 if (errno)
237 continue;
238 if (!entry)
239 break;
240 pid = bb_strtou(entry->d_name, NULL, 10);
241 if (errno)
242 continue;
243 check(pid);
244 }
245 closedir(procdir);
246 if (!pid)
247 bb_error_msg_and_die("nothing in /proc - not mounted?");
248}
249
250static int do_stop(void)
251{
252 char *what;
253 struct pid_list *p;
254 int killed = 0;
255
256 if (cmdname) {
257 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
258 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
259 } else if (execname) {
260 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
261 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
262 } else if (pidfile) {
263 what = xasprintf("process in pidfile '%s'", pidfile);
264 } else if (userspec) {
265 what = xasprintf("process(es) owned by '%s'", userspec);
266 } else {
267 bb_error_msg_and_die("internal error, please report");
268 }
269
270 if (!G.found_procs) {
271 if (!QUIET)
272 printf("no %s found; none killed\n", what);
273 killed = -1;
274 goto ret;
275 }
276 for (p = G.found_procs; p; p = p->next) {
277 if (TEST || kill(p->pid, signal_nr) == 0) {
278 killed++;
279 } else {
280 p->pid = 0;
281 bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
282 }
283 }
284 if (!QUIET && killed) {
285 printf("stopped %s (pid", what);
286 for (p = G.found_procs; p; p = p->next)
287 if (p->pid)
288 printf(" %u", (unsigned)p->pid);
289 puts(")");
290 }
291 ret:
292 if (ENABLE_FEATURE_CLEAN_UP)
293 free(what);
294 return killed;
295}
296
297#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
298static const char start_stop_daemon_longopts[] ALIGN1 =
299 "stop\0" No_argument "K"
300 "start\0" No_argument "S"
301 "background\0" No_argument "b"
302 "quiet\0" No_argument "q"
303 "test\0" No_argument "t"
304 "make-pidfile\0" No_argument "m"
305#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
306 "oknodo\0" No_argument "o"
307 "verbose\0" No_argument "v"
308 "nicelevel\0" Required_argument "N"
309#endif
310 "startas\0" Required_argument "a"
311 "name\0" Required_argument "n"
312 "signal\0" Required_argument "s"
313 "user\0" Required_argument "u"
314 "chuid\0" Required_argument "c"
315 "exec\0" Required_argument "x"
316 "pidfile\0" Required_argument "p"
317#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
318 "retry\0" Required_argument "R"
319#endif
320 ;
321#endif
322
323int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
324int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
325{
326 unsigned opt;
327 char *signame;
328 char *startas;
329 char *chuid;
330#ifdef OLDER_VERSION_OF_X
331 struct stat execstat;
332#endif
333#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
334
335
336 char *opt_N;
337#endif
338
339 INIT_G();
340
341#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
342 applet_long_options = start_stop_daemon_longopts;
343#endif
344
345
346
347
348
349
350 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
351 IF_FEATURE_START_STOP_DAEMON_FANCY("q-v");
352 opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:"
353 IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
354 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
355 IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
356
357 IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
358 );
359
360 if (opt & OPT_s) {
361 signal_nr = get_signum(signame);
362 if (signal_nr < 0) bb_show_usage();
363 }
364
365 if (!(opt & OPT_a))
366 startas = execname;
367 if (!execname)
368 execname = startas;
369 if (execname) {
370 G.execname_sizeof = strlen(execname) + 1;
371 G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1);
372 }
373
374
375
376
377
378
379 argv += optind;
380
381 if (userspec) {
382 user_id = bb_strtou(userspec, NULL, 10);
383 if (errno)
384 user_id = xuname2uid(userspec);
385 }
386
387 do_procinit();
388
389 if (opt & CTX_STOP) {
390 int i = do_stop();
391 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
392 }
393
394 if (G.found_procs) {
395 if (!QUIET)
396 printf("%s is already running\n%u\n", execname, (unsigned)G.found_procs->pid);
397 return !(opt & OPT_OKNODO);
398 }
399
400#ifdef OLDER_VERSION_OF_X
401 if (execname)
402 xstat(execname, &execstat);
403#endif
404
405 *--argv = startas;
406 if (opt & OPT_BACKGROUND) {
407#if BB_MMU
408 bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
409
410
411#else
412 pid_t pid = xvfork();
413 if (pid != 0) {
414
415
416
417 _exit(EXIT_SUCCESS);
418 }
419
420 setsid();
421
422
423 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
424 + DAEMON_CLOSE_EXTRA_FDS
425 + DAEMON_ONLY_SANITIZE,
426 NULL );
427#endif
428 }
429 if (opt & OPT_MAKEPID) {
430
431 write_pidfile(pidfile);
432 }
433 if (opt & OPT_c) {
434 struct bb_uidgid_t ugid = { -1, -1 };
435 parse_chown_usergroup_or_die(&ugid, chuid);
436 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
437 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
438 }
439#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
440 if (opt & OPT_NICELEVEL) {
441
442 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
443 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
444 bb_perror_msg_and_die("setpriority(%d)", prio);
445 }
446 }
447#endif
448 execvp(startas, argv);
449 bb_perror_msg_and_die("can't execute '%s'", startas);
450}
451