start-stop-daemon.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * A rewrite of the original Debian's start-stop-daemon Perl script
  3. * in C (faster - it is executed many times during system startup).
  4. *
  5. * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
  6. * public domain. Based conceptually on start-stop-daemon.pl, by Ian
  7. * Jackson <ijackson@gnu.ai.mit.edu>. May be used and distributed
  8. * freely for any purpose. Changes by Christian Schwarz
  9. * <schwarz@monet.m.isar.de>, to make output conform to the Debian
  10. * Console Message Standard, also placed in public domain. Minor
  11. * changes by Klee Dienes <klee@debian.org>, also placed in the Public
  12. * Domain.
  13. *
  14. * Changes by Ben Collins <bcollins@debian.org>, added --chuid, --background
  15. * and --make-pidfile options, placed in public domain aswell.
  16. *
  17. * Port to OpenBSD by Sontri Tomo Huynh <huynh.29@osu.edu>
  18. * and Andreas Schuldei <andreas@schuldei.org>
  19. *
  20. * Changes by Ian Jackson: added --retry (and associated rearrangements).
  21. *
  22. * Modified for Gentoo rc-scripts by Donny Davies <woodchip@gentoo.org>:
  23. * I removed the BSD/Hurd/OtherOS stuff, added #include <stddef.h>
  24. * and stuck in a #define VERSION "1.9.18". Now it compiles without
  25. * the whole automake/config.h dance.
  26. */
  27. #ifdef HAVE_LXC
  28. #define _GNU_SOURCE
  29. #include <sched.h>
  30. #endif /* HAVE_LXC */
  31. #include <stddef.h>
  32. #define VERSION "1.9.18"
  33. #define MIN_POLL_INTERVAL 20000 /*us*/
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <stdarg.h>
  39. #include <signal.h>
  40. #include <sys/stat.h>
  41. #include <dirent.h>
  42. #include <sys/time.h>
  43. #include <sys/queue.h>
  44. #include <unistd.h>
  45. #include <getopt.h>
  46. #include <pwd.h>
  47. #include <grp.h>
  48. #include <sys/ioctl.h>
  49. #include <sys/types.h>
  50. #include <sys/termios.h>
  51. #include <fcntl.h>
  52. #include <limits.h>
  53. #include <assert.h>
  54. #include <ctype.h>
  55. #include <error.h>
  56. #include <linux/sched.h>
  57. static int testmode = 0;
  58. static int quietmode = 0;
  59. static int exitnodo = 1;
  60. static int start = 0;
  61. static int stop = 0;
  62. static int background = 0;
  63. static int mpidfile = 0;
  64. static int signal_nr = 15;
  65. static const char *signal_str = NULL;
  66. static int user_id = -1;
  67. static int runas_uid = -1;
  68. static int runas_gid = -1;
  69. static const char *userspec = NULL;
  70. static char *changeuser = NULL;
  71. static const char *changegroup = NULL;
  72. static char *changeroot = NULL;
  73. static const char *cmdname = NULL;
  74. static char *execname = NULL;
  75. static char *startas = NULL;
  76. static const char *pidfile = NULL;
  77. static char what_stop[1024];
  78. static const char *schedule_str = NULL;
  79. static const char *progname = "";
  80. static int nicelevel = 0;
  81. static struct stat exec_stat;
  82. struct pid_list {
  83. struct pid_list *next;
  84. pid_t pid;
  85. };
  86. static struct pid_list *found = NULL;
  87. static struct pid_list *killed = NULL;
  88. struct schedule_item {
  89. enum { sched_timeout, sched_signal, sched_goto, sched_forever } type;
  90. int value; /* seconds, signal no., or index into array */
  91. /* sched_forever is only seen within parse_schedule and callees */
  92. };
  93. static int schedule_length;
  94. static struct schedule_item *schedule = NULL;
  95. LIST_HEAD(namespace_head, namespace);
  96. struct namespace {
  97. LIST_ENTRY(namespace) list;
  98. char *path;
  99. int nstype;
  100. };
  101. static struct namespace_head namespace_head;
  102. static void *xmalloc(int size);
  103. static void push(struct pid_list **list, pid_t pid);
  104. static void do_help(void);
  105. static void parse_options(int argc, char * const *argv);
  106. static int pid_is_user(pid_t pid, uid_t uid);
  107. static int pid_is_cmd(pid_t pid, const char *name);
  108. static void check(pid_t pid);
  109. static void do_pidfile(const char *name);
  110. static void do_stop(int signal_nr, int quietmode,
  111. int *n_killed, int *n_notkilled, int retry_nr);
  112. static int pid_is_exec(pid_t pid, const struct stat *esb);
  113. #ifdef __GNUC__
  114. static void fatal(const char *format, ...)
  115. __attribute__((noreturn, format(printf, 1, 2)));
  116. static void badusage(const char *msg)
  117. __attribute__((noreturn));
  118. #else
  119. static void fatal(const char *format, ...);
  120. static void badusage(const char *msg);
  121. #endif
  122. /* This next part serves only to construct the TVCALC macro, which
  123. * is used for doing arithmetic on struct timeval's. It works like this:
  124. * TVCALC(result, expression);
  125. * where result is a struct timeval (and must be an lvalue) and
  126. * expression is the single expression for both components. In this
  127. * expression you can use the special values TVELEM, which when fed a
  128. * const struct timeval* gives you the relevant component, and
  129. * TVADJUST. TVADJUST is necessary when subtracting timevals, to make
  130. * it easier to renormalise. Whenver you subtract timeval elements,
  131. * you must make sure that TVADJUST is added to the result of the
  132. * subtraction (before any resulting multiplication or what have you).
  133. * TVELEM must be linear in TVADJUST.
  134. */
  135. typedef long tvselector(const struct timeval*);
  136. static long tvselector_sec(const struct timeval *tv) { return tv->tv_sec; }
  137. static long tvselector_usec(const struct timeval *tv) { return tv->tv_usec; }
  138. #define TVCALC_ELEM(result, expr, sec, adj) \
  139. { \
  140. const long TVADJUST = adj; \
  141. long (*const TVELEM)(const struct timeval*) = tvselector_##sec; \
  142. (result).tv_##sec = (expr); \
  143. }
  144. #define TVCALC(result,expr) \
  145. do { \
  146. TVCALC_ELEM(result, expr, sec, (-1)); \
  147. TVCALC_ELEM(result, expr, usec, (+1000000)); \
  148. (result).tv_sec += (result).tv_usec / 1000000; \
  149. (result).tv_usec %= 1000000; \
  150. } while(0)
  151. static void
  152. fatal(const char *format, ...)
  153. {
  154. va_list arglist;
  155. fprintf(stderr, "%s: ", progname);
  156. va_start(arglist, format);
  157. vfprintf(stderr, format, arglist);
  158. va_end(arglist);
  159. putc('\n', stderr);
  160. exit(2);
  161. }
  162. static void *
  163. xmalloc(int size)
  164. {
  165. void *ptr;
  166. ptr = malloc(size);
  167. if (ptr)
  168. return ptr;
  169. fatal("malloc(%d) failed", size);
  170. }
  171. static void
  172. xgettimeofday(struct timeval *tv)
  173. {
  174. if (gettimeofday(tv,0) != 0)
  175. fatal("gettimeofday failed: %s", strerror(errno));
  176. }
  177. static void
  178. push(struct pid_list **list, pid_t pid)
  179. {
  180. struct pid_list *p;
  181. p = xmalloc(sizeof(*p));
  182. p->next = *list;
  183. p->pid = pid;
  184. *list = p;
  185. }
  186. static void
  187. clear(struct pid_list **list)
  188. {
  189. struct pid_list *here, *next;
  190. for (here = *list; here != NULL; here = next) {
  191. next = here->next;
  192. free(here);
  193. }
  194. *list = NULL;
  195. }
  196. static char *
  197. next_dirname(const char *s)
  198. {
  199. char *cur;
  200. cur = (char *)s;
  201. if (*cur != '\0') {
  202. for (; *cur != '/'; ++cur)
  203. if (*cur == '\0')
  204. return cur;
  205. for (; *cur == '/'; ++cur)
  206. ;
  207. }
  208. return cur;
  209. }
  210. static void
  211. add_namespace(const char *path)
  212. {
  213. int nstype;
  214. char *nsdirname, *nsname, *cur;
  215. struct namespace *namespace;
  216. cur = (char *)path;
  217. nsdirname = nsname = "";
  218. while ((cur = next_dirname(cur))[0] != '\0') {
  219. nsdirname = nsname;
  220. nsname = cur;
  221. }
  222. if (!memcmp(nsdirname, "ipcns/", strlen("ipcns/")))
  223. nstype = CLONE_NEWIPC;
  224. else if (!memcmp(nsdirname, "netns/", strlen("netns/")))
  225. nstype = CLONE_NEWNET;
  226. else if (!memcmp(nsdirname, "utcns/", strlen("utcns/")))
  227. nstype = CLONE_NEWUTS;
  228. else
  229. badusage("invalid namepspace path");
  230. namespace = xmalloc(sizeof(*namespace));
  231. namespace->path = (char *)path;
  232. namespace->nstype = nstype;
  233. LIST_INSERT_HEAD(&namespace_head, namespace, list);
  234. }
  235. #ifdef HAVE_LXC
  236. static void
  237. set_namespaces()
  238. {
  239. struct namespace *namespace;
  240. int fd;
  241. LIST_FOREACH(namespace, &namespace_head, list) {
  242. if ((fd = open(namespace->path, O_RDONLY)) == -1)
  243. fatal("open namespace %s: %s", namespace->path, strerror(errno));
  244. if (setns(fd, namespace->nstype) == -1)
  245. fatal("setns %s: %s", namespace->path, strerror(errno));
  246. }
  247. }
  248. #else
  249. static void
  250. set_namespaces()
  251. {
  252. if (!LIST_EMPTY(&namespace_head))
  253. fatal("LCX namespaces not supported");
  254. }
  255. #endif
  256. static void
  257. do_help(void)
  258. {
  259. printf(
  260. "start-stop-daemon " VERSION " for Milis linux - small and fast C version written by\n"
  261. "Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
  262. "Milis Linux milisarge@gmail.com .\n"
  263. "\n"
  264. "Usage:\n"
  265. " start-stop-daemon -S|--start options ... -- arguments ...\n"
  266. " start-stop-daemon -K|--stop options ...\n"
  267. " start-stop-daemon -H|--help\n"
  268. " start-stop-daemon -V|--version\n"
  269. "\n"
  270. "Options (at least one of --exec|--pidfile|--user is required):\n"
  271. " -x|--exec <executable> program to start/check if it is running\n"
  272. " -p|--pidfile <pid-file> pid file to check\n"
  273. " -c|--chuid <name|uid[:group|gid]>\n"
  274. " change to this user/group before starting process\n"
  275. " -u|--user <username>|<uid> stop processes owned by this user\n"
  276. " -n|--name <process-name> stop processes with this name\n"
  277. " -s|--signal <signal> signal to send (default TERM)\n"
  278. " -a|--startas <pathname> program to start (default is <executable>)\n"
  279. " -N|--nicelevel <incr> add incr to the process's nice level\n"
  280. " -b|--background force the process to detach\n"
  281. " -m|--make-pidfile create the pidfile before starting\n"
  282. " -R|--retry <schedule> check whether processes die, and retry\n"
  283. " -t|--test test mode, don't do anything\n"
  284. " -o|--oknodo exit status 0 (not 1) if nothing done\n"
  285. " -q|--quiet be more quiet\n"
  286. " -v|--verbose be more verbose\n"
  287. "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
  288. " -<signal-num>|[-]<signal-name> send that signal\n"
  289. " <timeout> wait that many seconds\n"
  290. " forever repeat remainder forever\n"
  291. "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
  292. "\n"
  293. "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n"
  294. " 3 = trouble 2 = with --retry, processes wouldn't die\n");
  295. }
  296. static void
  297. badusage(const char *msg)
  298. {
  299. if (msg)
  300. fprintf(stderr, "%s: %s\n", progname, msg);
  301. fprintf(stderr, "Try `%s --help' for more information.\n", progname);
  302. exit(3);
  303. }
  304. struct sigpair {
  305. const char *name;
  306. int signal;
  307. };
  308. const struct sigpair siglist[] = {
  309. { "ABRT", SIGABRT },
  310. { "ALRM", SIGALRM },
  311. { "FPE", SIGFPE },
  312. { "HUP", SIGHUP },
  313. { "ILL", SIGILL },
  314. { "INT", SIGINT },
  315. { "KILL", SIGKILL },
  316. { "PIPE", SIGPIPE },
  317. { "QUIT", SIGQUIT },
  318. { "SEGV", SIGSEGV },
  319. { "TERM", SIGTERM },
  320. { "USR1", SIGUSR1 },
  321. { "USR2", SIGUSR2 },
  322. { "CHLD", SIGCHLD },
  323. { "CONT", SIGCONT },
  324. { "STOP", SIGSTOP },
  325. { "TSTP", SIGTSTP },
  326. { "TTIN", SIGTTIN },
  327. { "TTOU", SIGTTOU }
  328. };
  329. static int parse_integer (const char *string, int *value_r) {
  330. unsigned long ul;
  331. char *ep;
  332. if (!string[0])
  333. return -1;
  334. ul= strtoul(string,&ep,10);
  335. if (ul > INT_MAX || *ep != '\0')
  336. return -1;
  337. *value_r= ul;
  338. return 0;
  339. }
  340. static int parse_signal (const char *signal_str, int *signal_nr)
  341. {
  342. unsigned int i;
  343. if (parse_integer(signal_str, signal_nr) == 0)
  344. return 0;
  345. for (i = 0; i < sizeof (siglist) / sizeof (siglist[0]); i++) {
  346. if (strcmp (signal_str, siglist[i].name) == 0) {
  347. *signal_nr = siglist[i].signal;
  348. return 0;
  349. }
  350. }
  351. return -1;
  352. }
  353. static void
  354. parse_schedule_item(const char *string, struct schedule_item *item) {
  355. const char *after_hyph;
  356. if (!strcmp(string,"forever")) {
  357. item->type = sched_forever;
  358. } else if (isdigit(string[0])) {
  359. item->type = sched_timeout;
  360. if (parse_integer(string, &item->value) != 0)
  361. badusage("invalid timeout value in schedule");
  362. } else if ((after_hyph = string + (string[0] == '-')) &&
  363. parse_signal(after_hyph, &item->value) == 0) {
  364. item->type = sched_signal;
  365. } else {
  366. badusage("invalid schedule item (must be [-]<signal-name>, "
  367. "-<signal-number>, <timeout> or `forever'");
  368. }
  369. }
  370. static void
  371. parse_schedule(const char *schedule_str) {
  372. char item_buf[20];
  373. const char *slash;
  374. int count, repeatat;
  375. ptrdiff_t str_len;
  376. count = 0;
  377. for (slash = schedule_str; *slash; slash++)
  378. if (*slash == '/')
  379. count++;
  380. schedule_length = (count == 0) ? 4 : count+1;
  381. schedule = xmalloc(sizeof(*schedule) * schedule_length);
  382. if (count == 0) {
  383. schedule[0].type = sched_signal;
  384. schedule[0].value = signal_nr;
  385. parse_schedule_item(schedule_str, &schedule[1]);
  386. if (schedule[1].type != sched_timeout) {
  387. badusage ("--retry takes timeout, or schedule list"
  388. " of at least two items");
  389. }
  390. schedule[2].type = sched_signal;
  391. schedule[2].value = SIGKILL;
  392. schedule[3]= schedule[1];
  393. } else {
  394. count = 0;
  395. repeatat = -1;
  396. while (schedule_str != NULL) {
  397. slash = strchr(schedule_str,'/');
  398. str_len = slash ? slash - schedule_str : strlen(schedule_str);
  399. if (str_len >= (ptrdiff_t)sizeof(item_buf))
  400. badusage("invalid schedule item: far too long"
  401. " (you must delimit items with slashes)");
  402. memcpy(item_buf, schedule_str, str_len);
  403. item_buf[str_len] = 0;
  404. schedule_str = slash ? slash+1 : NULL;
  405. parse_schedule_item(item_buf, &schedule[count]);
  406. if (schedule[count].type == sched_forever) {
  407. if (repeatat >= 0)
  408. badusage("invalid schedule: `forever'"
  409. " appears more than once");
  410. repeatat = count;
  411. continue;
  412. }
  413. count++;
  414. }
  415. if (repeatat >= 0) {
  416. schedule[count].type = sched_goto;
  417. schedule[count].value = repeatat;
  418. count++;
  419. }
  420. assert(count == schedule_length);
  421. }
  422. }
  423. static void
  424. parse_options(int argc, char * const *argv)
  425. {
  426. static struct option longopts[] = {
  427. { "help", 0, NULL, 'H'},
  428. { "stop", 0, NULL, 'K'},
  429. { "start", 0, NULL, 'S'},
  430. { "version", 0, NULL, 'V'},
  431. { "startas", 1, NULL, 'a'},
  432. { "name", 1, NULL, 'n'},
  433. { "oknodo", 0, NULL, 'o'},
  434. { "pidfile", 1, NULL, 'p'},
  435. { "quiet", 0, NULL, 'q'},
  436. { "signal", 1, NULL, 's'},
  437. { "test", 0, NULL, 't'},
  438. { "user", 1, NULL, 'u'},
  439. { "chroot", 1, NULL, 'r'},
  440. { "namespace", 1, NULL, 'd'},
  441. { "verbose", 0, NULL, 'v'},
  442. { "exec", 1, NULL, 'x'},
  443. { "chuid", 1, NULL, 'c'},
  444. { "nicelevel", 1, NULL, 'N'},
  445. { "background", 0, NULL, 'b'},
  446. { "make-pidfile", 0, NULL, 'm'},
  447. { "retry", 1, NULL, 'R'},
  448. { NULL, 0, NULL, 0}
  449. };
  450. int c;
  451. for (;;) {
  452. c = getopt_long(argc, argv, "HKSVa:n:op:qr:d:s:tu:vx:c:N:bmR:",
  453. longopts, (int *) 0);
  454. if (c == -1)
  455. break;
  456. switch (c) {
  457. case 'H': /* --help */
  458. do_help();
  459. exit(0);
  460. case 'K': /* --stop */
  461. stop = 1;
  462. break;
  463. case 'S': /* --start */
  464. start = 1;
  465. break;
  466. case 'V': /* --version */
  467. printf("start-stop-daemon " VERSION "\n");
  468. exit(0);
  469. case 'a': /* --startas <pathname> */
  470. startas = optarg;
  471. break;
  472. case 'n': /* --name <process-name> */
  473. cmdname = optarg;
  474. break;
  475. case 'o': /* --oknodo */
  476. exitnodo = 0;
  477. break;
  478. case 'p': /* --pidfile <pid-file> */
  479. pidfile = optarg;
  480. break;
  481. case 'q': /* --quiet */
  482. quietmode = 1;
  483. break;
  484. case 's': /* --signal <signal> */
  485. signal_str = optarg;
  486. break;
  487. case 't': /* --test */
  488. testmode = 1;
  489. break;
  490. case 'u': /* --user <username>|<uid> */
  491. userspec = optarg;
  492. break;
  493. case 'v': /* --verbose */
  494. quietmode = -1;
  495. break;
  496. case 'x': /* --exec <executable> */
  497. execname = optarg;
  498. break;
  499. case 'c': /* --chuid <username>|<uid> */
  500. /* we copy the string just in case we need the
  501. * argument later. */
  502. changeuser = strdup(optarg);
  503. changeuser = strtok(changeuser, ":");
  504. changegroup = strtok(NULL, ":");
  505. break;
  506. case 'r': /* --chroot /new/root */
  507. changeroot = optarg;
  508. break;
  509. case 'd': /* --namespace /.../<ipcns>|<netns>|<utsns>/name */
  510. add_namespace(optarg);
  511. break;
  512. case 'N': /* --nice */
  513. nicelevel = atoi(optarg);
  514. break;
  515. case 'b': /* --background */
  516. background = 1;
  517. break;
  518. case 'm': /* --make-pidfile */
  519. mpidfile = 1;
  520. break;
  521. case 'R': /* --retry <schedule>|<timeout> */
  522. schedule_str = optarg;
  523. break;
  524. default:
  525. badusage(NULL); /* message printed by getopt */
  526. }
  527. }
  528. if (signal_str != NULL) {
  529. if (parse_signal (signal_str, &signal_nr) != 0)
  530. badusage("signal value must be numeric or name"
  531. " of signal (KILL, INTR, ...)");
  532. }
  533. if (schedule_str != NULL) {
  534. parse_schedule(schedule_str);
  535. }
  536. if (start == stop)
  537. badusage("need one of --start or --stop");
  538. if (!execname && !pidfile && !userspec && !cmdname)
  539. badusage("need at least one of --exec, --pidfile, --user or --name");
  540. if (!startas)
  541. startas = execname;
  542. if (start && !startas)
  543. badusage("--start needs --exec or --startas");
  544. if (mpidfile && pidfile == NULL)
  545. badusage("--make-pidfile is only relevant with --pidfile");
  546. if (background && !start)
  547. badusage("--background is only relevant with --start");
  548. }
  549. static int
  550. pid_is_exec(pid_t pid, const struct stat *esb)
  551. {
  552. struct stat sb;
  553. char buf[32];
  554. sprintf(buf, "/proc/%d/exe", pid);
  555. if (stat(buf, &sb) != 0)
  556. return 0;
  557. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  558. }
  559. static int
  560. pid_is_user(pid_t pid, uid_t uid)
  561. {
  562. struct stat sb;
  563. char buf[32];
  564. sprintf(buf, "/proc/%d", pid);
  565. if (stat(buf, &sb) != 0)
  566. return 0;
  567. return (sb.st_uid == uid);
  568. }
  569. static int
  570. pid_is_cmd(pid_t pid, const char *name)
  571. {
  572. char buf[32];
  573. FILE *f;
  574. int c;
  575. sprintf(buf, "/proc/%d/stat", pid);
  576. f = fopen(buf, "r");
  577. if (!f)
  578. return 0;
  579. while ((c = getc(f)) != EOF && c != '(')
  580. ;
  581. if (c != '(') {
  582. fclose(f);
  583. return 0;
  584. }
  585. /* this hopefully handles command names containing ')' */
  586. while ((c = getc(f)) != EOF && c == *name)
  587. name++;
  588. fclose(f);
  589. return (c == ')' && *name == '\0');
  590. }
  591. static void
  592. check(pid_t pid)
  593. {
  594. if (execname && !pid_is_exec(pid, &exec_stat))
  595. return;
  596. if (userspec && !pid_is_user(pid, user_id))
  597. return;
  598. if (cmdname && !pid_is_cmd(pid, cmdname))
  599. return;
  600. push(&found, pid);
  601. }
  602. static void
  603. do_pidfile(const char *name)
  604. {
  605. FILE *f;
  606. pid_t pid;
  607. f = fopen(name, "r");
  608. if (f) {
  609. if (fscanf(f, "%d", &pid) == 1)
  610. check(pid);
  611. fclose(f);
  612. } else if (errno != ENOENT)
  613. fatal("open pidfile %s: %s", name, strerror(errno));
  614. }
  615. /* WTA: this needs to be an autoconf check for /proc/pid existance.
  616. */
  617. static void
  618. do_procinit(void)
  619. {
  620. DIR *procdir;
  621. struct dirent *entry;
  622. int foundany;
  623. pid_t pid;
  624. procdir = opendir("/proc");
  625. if (!procdir)
  626. fatal("opendir /proc: %s", strerror(errno));
  627. foundany = 0;
  628. while ((entry = readdir(procdir)) != NULL) {
  629. if (sscanf(entry->d_name, "%d", &pid) != 1)
  630. continue;
  631. foundany++;
  632. check(pid);
  633. }
  634. closedir(procdir);
  635. if (!foundany)
  636. fatal("nothing in /proc - not mounted?");
  637. }
  638. static void
  639. do_findprocs(void)
  640. {
  641. clear(&found);
  642. if (pidfile)
  643. do_pidfile(pidfile);
  644. else
  645. do_procinit();
  646. }
  647. /* return 1 on failure */
  648. static void
  649. do_stop(int signal_nr, int quietmode, int *n_killed, int *n_notkilled, int retry_nr)
  650. {
  651. struct pid_list *p;
  652. do_findprocs();
  653. *n_killed = 0;
  654. *n_notkilled = 0;
  655. if (!found)
  656. return;
  657. clear(&killed);
  658. for (p = found; p; p = p->next) {
  659. if (testmode)
  660. printf("Would send signal %d to %d.\n",
  661. signal_nr, p->pid);
  662. else if (kill(p->pid, signal_nr) == 0) {
  663. push(&killed, p->pid);
  664. (*n_killed)++;
  665. } else {
  666. printf("%s: warning: failed to kill %d: %s\n",
  667. progname, p->pid, strerror(errno));
  668. (*n_notkilled)++;
  669. }
  670. }
  671. if (quietmode < 0 && killed) {
  672. printf("Stopped %s (pid", what_stop);
  673. for (p = killed; p; p = p->next)
  674. printf(" %d", p->pid);
  675. putchar(')');
  676. if (retry_nr > 0)
  677. printf(", retry #%d", retry_nr);
  678. printf(".\n");
  679. }
  680. }
  681. static void
  682. set_what_stop(const char *str)
  683. {
  684. strncpy(what_stop, str, sizeof(what_stop));
  685. what_stop[sizeof(what_stop)-1] = '\0';
  686. }
  687. static int
  688. run_stop_schedule(void)
  689. {
  690. int r, position, n_killed, n_notkilled, value, ratio, anykilled, retry_nr;
  691. struct timeval stopat, before, after, interval, maxinterval;
  692. if (testmode) {
  693. if (schedule != NULL) {
  694. printf("Ignoring --retry in test mode\n");
  695. schedule = NULL;
  696. }
  697. }
  698. if (cmdname)
  699. set_what_stop(cmdname);
  700. else if (execname)
  701. set_what_stop(execname);
  702. else if (pidfile)
  703. sprintf(what_stop, "process in pidfile `%.200s'", pidfile);
  704. else if (userspec)
  705. sprintf(what_stop, "process(es) owned by `%.200s'", userspec);
  706. else
  707. fatal("internal error, please report");
  708. anykilled = 0;
  709. retry_nr = 0;
  710. if (schedule == NULL) {
  711. do_stop(signal_nr, quietmode, &n_killed, &n_notkilled, 0);
  712. if (n_notkilled > 0 && quietmode <= 0)
  713. printf("%d pids were not killed\n", n_notkilled);
  714. if (n_killed)
  715. anykilled = 1;
  716. goto x_finished;
  717. }
  718. for (position = 0; position < schedule_length; ) {
  719. value= schedule[position].value;
  720. n_notkilled = 0;
  721. switch (schedule[position].type) {
  722. case sched_goto:
  723. position = value;
  724. continue;
  725. case sched_signal:
  726. do_stop(value, quietmode, &n_killed, &n_notkilled, retry_nr++);
  727. if (!n_killed)
  728. goto x_finished;
  729. else
  730. anykilled = 1;
  731. goto next_item;
  732. case sched_timeout:
  733. /* We want to keep polling for the processes, to see if they've exited,
  734. * or until the timeout expires.
  735. *
  736. * This is a somewhat complicated algorithm to try to ensure that we
  737. * notice reasonably quickly when all the processes have exited, but
  738. * don't spend too much CPU time polling. In particular, on a fast
  739. * machine with quick-exiting daemons we don't want to delay system
  740. * shutdown too much, whereas on a slow one, or where processes are
  741. * taking some time to exit, we want to increase the polling
  742. * interval.
  743. *
  744. * The algorithm is as follows: we measure the elapsed time it takes
  745. * to do one poll(), and wait a multiple of this time for the next
  746. * poll. However, if that would put us past the end of the timeout
  747. * period we wait only as long as the timeout period, but in any case
  748. * we always wait at least MIN_POLL_INTERVAL (20ms). The multiple
  749. * (`ratio') starts out as 2, and increases by 1 for each poll to a
  750. * maximum of 10; so we use up to between 30% and 10% of the
  751. * machine's resources (assuming a few reasonable things about system
  752. * performance).
  753. */
  754. xgettimeofday(&stopat);
  755. stopat.tv_sec += value;
  756. ratio = 1;
  757. for (;;) {
  758. xgettimeofday(&before);
  759. if (timercmp(&before,&stopat,>))
  760. goto next_item;
  761. do_stop(0, 1, &n_killed, &n_notkilled, 0);
  762. if (!n_killed)
  763. goto x_finished;
  764. xgettimeofday(&after);
  765. if (!timercmp(&after,&stopat,<))
  766. goto next_item;
  767. if (ratio < 10)
  768. ratio++;
  769. TVCALC(interval, ratio * (TVELEM(&after) - TVELEM(&before) + TVADJUST));
  770. TVCALC(maxinterval, TVELEM(&stopat) - TVELEM(&after) + TVADJUST);
  771. if (timercmp(&interval,&maxinterval,>))
  772. interval = maxinterval;
  773. if (interval.tv_sec == 0 &&
  774. interval.tv_usec <= MIN_POLL_INTERVAL)
  775. interval.tv_usec = MIN_POLL_INTERVAL;
  776. r = select(0,0,0,0,&interval);
  777. if (r < 0 && errno != EINTR)
  778. fatal("select() failed for pause: %s",
  779. strerror(errno));
  780. }
  781. default:
  782. assert(!"schedule[].type value must be valid");
  783. }
  784. next_item:
  785. position++;
  786. }
  787. if (quietmode <= 0)
  788. printf("Program %s, %d process(es), refused to die.\n",
  789. what_stop, n_killed);
  790. return 2;
  791. x_finished:
  792. if (!anykilled) {
  793. if (quietmode <= 0)
  794. printf("No %s found running; none killed.\n", what_stop);
  795. return exitnodo;
  796. } else {
  797. return 0;
  798. }
  799. }
  800. /*
  801. int main(int argc, char **argv) NONRETURNING;
  802. */
  803. int
  804. main(int argc, char **argv)
  805. {
  806. progname = argv[0];
  807. LIST_INIT(&namespace_head);
  808. parse_options(argc, argv);
  809. argc -= optind;
  810. argv += optind;
  811. if (execname && stat(execname, &exec_stat))
  812. fatal("stat %s: %s", execname, strerror(errno));
  813. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  814. struct passwd *pw;
  815. pw = getpwnam(userspec);
  816. if (!pw)
  817. fatal("user `%s' not found\n", userspec);
  818. user_id = pw->pw_uid;
  819. }
  820. if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) {
  821. struct group *gr = getgrnam(changegroup);
  822. if (!gr)
  823. fatal("group `%s' not found\n", changegroup);
  824. runas_gid = gr->gr_gid;
  825. }
  826. if (changeuser && sscanf(changeuser, "%d", &runas_uid) != 1) {
  827. struct passwd *pw = getpwnam(changeuser);
  828. if (!pw)
  829. fatal("user `%s' not found\n", changeuser);
  830. runas_uid = pw->pw_uid;
  831. if (changegroup == NULL) { /* pass the default group of this user */
  832. changegroup = ""; /* just empty */
  833. runas_gid = pw->pw_gid;
  834. }
  835. }
  836. if (stop) {
  837. int i = run_stop_schedule();
  838. exit(i);
  839. }
  840. do_findprocs();
  841. if (found) {
  842. if (quietmode <= 0)
  843. printf("%s already running.\n", execname);
  844. exit(exitnodo);
  845. }
  846. if (testmode) {
  847. printf("Would start %s ", startas);
  848. while (argc-- > 0)
  849. printf("%s ", *argv++);
  850. if (changeuser != NULL) {
  851. printf(" (as user %s[%d]", changeuser, runas_uid);
  852. if (changegroup != NULL)
  853. printf(", and group %s[%d])", changegroup, runas_gid);
  854. else
  855. printf(")");
  856. }
  857. if (changeroot != NULL)
  858. printf(" in directory %s", changeroot);
  859. if (nicelevel)
  860. printf(", and add %i to the priority", nicelevel);
  861. printf(".\n");
  862. exit(0);
  863. }
  864. if (quietmode < 0)
  865. printf("Starting %s...\n", startas);
  866. *--argv = startas;
  867. if (changeroot != NULL) {
  868. if (chdir(changeroot) < 0)
  869. fatal("Unable to chdir() to %s", changeroot);
  870. if (chroot(changeroot) < 0)
  871. fatal("Unable to chroot() to %s", changeroot);
  872. }
  873. if (changeuser != NULL) {
  874. if (setgid(runas_gid))
  875. fatal("Unable to set gid to %d", runas_gid);
  876. if (initgroups(changeuser, runas_gid))
  877. fatal("Unable to set initgroups() with gid %d", runas_gid);
  878. if (setuid(runas_uid))
  879. fatal("Unable to set uid to %s", changeuser);
  880. }
  881. if (background) { /* ok, we need to detach this process */
  882. int i, fd;
  883. if (quietmode < 0)
  884. printf("Detatching to start %s...", startas);
  885. i = fork();
  886. if (i<0) {
  887. fatal("Unable to fork.\n");
  888. }
  889. if (i) { /* parent */
  890. if (quietmode < 0)
  891. printf("done.\n");
  892. exit(0);
  893. }
  894. /* child continues here */
  895. /* now close all extra fds */
  896. for (i=getdtablesize()-1; i>=0; --i) close(i);
  897. /* change tty */
  898. fd = open("/dev/tty", O_RDWR);
  899. ioctl(fd, TIOCNOTTY, 0);
  900. close(fd);
  901. chdir("/");
  902. umask(022); /* set a default for dumb programs */
  903. setpgid(0,0); /* set the process group */
  904. fd=open("/dev/null", O_RDWR); /* stdin */
  905. dup(fd); /* stdout */
  906. dup(fd); /* stderr */
  907. }
  908. if (nicelevel) {
  909. errno = 0;
  910. if (nice(nicelevel) < 0 && errno)
  911. fatal("Unable to alter nice level by %i: %s", nicelevel,
  912. strerror(errno));
  913. }
  914. if (mpidfile && pidfile != NULL) { /* user wants _us_ to make the pidfile :) */
  915. FILE *pidf = fopen(pidfile, "w");
  916. pid_t pidt = getpid();
  917. if (pidf == NULL)
  918. fatal("Unable to open pidfile `%s' for writing: %s", pidfile,
  919. strerror(errno));
  920. fprintf(pidf, "%d\n", pidt);
  921. fclose(pidf);
  922. }
  923. set_namespaces();
  924. execv(startas, argv);
  925. fatal("Unable to start %s: %s", startas, strerror(errno));
  926. }