start-stop-daemon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. #include "config.h"
  14. #define _GNU_SOURCE
  15. #ifdef linux
  16. #define OSLinux
  17. #endif
  18. #ifdef __GNU__
  19. #define OSHURD
  20. #endif
  21. #ifdef HAVE_HURH_H
  22. #include <hurd.h>
  23. #endif
  24. #ifdef HAVE_PS_H
  25. #include <ps.h>
  26. #endif
  27. #include <errno.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <signal.h>
  33. #include <sys/stat.h>
  34. #include <dirent.h>
  35. #include <unistd.h>
  36. #include <getopt.h>
  37. #include <pwd.h>
  38. #ifdef HAVE_ERROR_H
  39. #include <error.h>
  40. #endif
  41. #ifdef HURD_IHASH_H
  42. #include <hurd/ihash.h>
  43. #endif
  44. static int testmode = 0;
  45. static int quietmode = 0;
  46. static int exitnodo = 1;
  47. static int start = 0;
  48. static int stop = 0;
  49. static int signal_nr = 15;
  50. static const char *signal_str = NULL;
  51. static int user_id = -1;
  52. static const char *userspec = NULL;
  53. static const char *cmdname = NULL;
  54. static char *execname = NULL;
  55. static char *startas = NULL;
  56. static const char *pidfile = NULL;
  57. static const char *progname = "";
  58. static struct stat exec_stat;
  59. #if defined(OSHURD)
  60. static struct ps_context *context;
  61. static struct proc_stat_list *procset;
  62. #endif
  63. struct pid_list {
  64. struct pid_list *next;
  65. int pid;
  66. };
  67. static struct pid_list *found = NULL;
  68. static struct pid_list *killed = NULL;
  69. static void *xmalloc(int size);
  70. static void push(struct pid_list **list, int pid);
  71. static void do_help(void);
  72. static void parse_options(int argc, char * const *argv);
  73. static int pid_is_user(int pid, int uid);
  74. static int pid_is_cmd(int pid, const char *name);
  75. static void check(int pid);
  76. static void do_pidfile(const char *name);
  77. static void do_stop(void);
  78. #if defined(OSLinux)
  79. static int pid_is_exec(int pid, const struct stat *esb);
  80. #endif
  81. static void do_psinit(void);
  82. #ifdef __GNUC__
  83. static void fatal(const char *format, ...)
  84. __attribute__((noreturn, format(printf, 1, 2)));
  85. static void badusage(const char *msg)
  86. __attribute__((noreturn));
  87. #else
  88. static void fatal(const char *format, ...);
  89. static void badusage(const char *msg);
  90. #endif
  91. static void
  92. fatal(const char *format, ...)
  93. {
  94. va_list arglist;
  95. fprintf(stderr, "%s: ", progname);
  96. va_start(arglist, format);
  97. vfprintf(stderr, format, arglist);
  98. va_end(arglist);
  99. putc('\n', stderr);
  100. exit(2);
  101. }
  102. static void *
  103. xmalloc(int size)
  104. {
  105. void *ptr;
  106. ptr = malloc(size);
  107. if (ptr)
  108. return ptr;
  109. fatal("malloc(%d) failed", size);
  110. }
  111. static void
  112. push(struct pid_list **list, int pid)
  113. {
  114. struct pid_list *p;
  115. p = xmalloc(sizeof(*p));
  116. p->next = *list;
  117. p->pid = pid;
  118. *list = p;
  119. }
  120. static void
  121. do_help(void)
  122. {
  123. printf("\
  124. start-stop-daemon for Debian Linux - small and fast C version written by\n\
  125. Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
  126. VERSION "\n\
  127. \n\
  128. Usage:
  129. start-stop-daemon -S|--start options ... -- arguments ...\n\
  130. start-stop-daemon -K|--stop options ...\n\
  131. start-stop-daemon -H|--help\n\
  132. start-stop-daemon -V|--version\n\
  133. \n\
  134. Options (at least one of --exec|--pidfile|--user is required):
  135. -x|--exec <executable> program to start/check if it is running\n\
  136. -p|--pidfile <pid-file> pid file to check\n\
  137. -u|--user <username>|<uid> stop processes owned by this user\n\
  138. -n|--name <process-name> stop processes with this name\n\
  139. -s|--signal <signal> signal to send (default TERM)\n\
  140. -a|--startas <pathname> program to start (default is <executable>)\n\
  141. -t|--test test mode, don't do anything\n\
  142. -o|--oknodo exit status 0 (not 1) if nothing done\n\
  143. -q|--quiet be more quiet\n\
  144. -v|--verbose be more verbose\n\
  145. \n\
  146. Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo) 2 = trouble\n");
  147. }
  148. static void
  149. badusage(const char *msg)
  150. {
  151. if (msg)
  152. fprintf(stderr, "%s: %s\n", progname, msg);
  153. fprintf(stderr, "Try `%s --help' for more information.\n", progname);
  154. exit(2);
  155. }
  156. struct sigpair {
  157. const char *name;
  158. int signal;
  159. };
  160. const struct sigpair siglist[] = {
  161. { "ABRT", SIGABRT },
  162. { "ALRM", SIGALRM },
  163. { "FPE", SIGFPE },
  164. { "HUP", SIGHUP },
  165. { "ILL", SIGILL },
  166. { "INT", SIGINT },
  167. { "KILL", SIGKILL },
  168. { "PIPE", SIGPIPE },
  169. { "QUIT", SIGQUIT },
  170. { "SEGV", SIGSEGV },
  171. { "TERM", SIGTERM },
  172. { "USR1", SIGUSR1 },
  173. { "USR2", SIGUSR2 },
  174. { "CHLD", SIGCHLD },
  175. { "CONT", SIGCONT },
  176. { "STOP", SIGSTOP },
  177. { "TSTP", SIGTSTP },
  178. { "TTIN", SIGTTIN },
  179. { "TTOU", SIGTTOU }
  180. };
  181. static int parse_signal (const char *signal_str, int *signal_nr)
  182. {
  183. int i;
  184. for (i = 0; i < sizeof (siglist) / sizeof (siglist[0]); i++) {
  185. if (strcmp (signal_str, siglist[i].name) == 0) {
  186. *signal_nr = siglist[i].signal;
  187. return 0;
  188. }
  189. }
  190. return -1;
  191. }
  192. static void
  193. parse_options(int argc, char * const *argv)
  194. {
  195. static struct option longopts[] = {
  196. { "help", 0, NULL, 'H'},
  197. { "stop", 0, NULL, 'K'},
  198. { "start", 0, NULL, 'S'},
  199. { "version", 0, NULL, 'V'},
  200. { "startas", 1, NULL, 'a'},
  201. { "name", 1, NULL, 'n'},
  202. { "oknodo", 0, NULL, 'o'},
  203. { "pidfile", 1, NULL, 'p'},
  204. { "quiet", 0, NULL, 'q'},
  205. { "signal", 1, NULL, 's'},
  206. { "test", 0, NULL, 't'},
  207. { "user", 1, NULL, 'u'},
  208. { "verbose", 0, NULL, 'v'},
  209. { "exec", 1, NULL, 'x'},
  210. { NULL, 0, NULL, 0}
  211. };
  212. int c;
  213. for (;;) {
  214. c = getopt_long(argc, argv, "HKSVa:n:op:qs:tu:vx:",
  215. longopts, (int *) 0);
  216. if (c == -1)
  217. break;
  218. switch (c) {
  219. case 'H': /* --help */
  220. do_help();
  221. exit(0);
  222. case 'K': /* --stop */
  223. stop = 1;
  224. break;
  225. case 'S': /* --start */
  226. start = 1;
  227. break;
  228. case 'V': /* --version */
  229. printf("start-stop-daemon " VERSION "\n");
  230. exit(0);
  231. case 'a': /* --startas <pathname> */
  232. startas = optarg;
  233. break;
  234. case 'n': /* --name <process-name> */
  235. cmdname = optarg;
  236. break;
  237. case 'o': /* --oknodo */
  238. exitnodo = 0;
  239. break;
  240. case 'p': /* --pidfile <pid-file> */
  241. pidfile = optarg;
  242. break;
  243. case 'q': /* --quiet */
  244. quietmode = 1;
  245. break;
  246. case 's': /* --signal <signal> */
  247. signal_str = optarg;
  248. break;
  249. case 't': /* --test */
  250. testmode = 1;
  251. break;
  252. case 'u': /* --user <username>|<uid> */
  253. userspec = optarg;
  254. break;
  255. case 'v': /* --verbose */
  256. quietmode = -1;
  257. break;
  258. case 'x': /* --exec <executable> */
  259. execname = optarg;
  260. break;
  261. default:
  262. badusage(NULL); /* message printed by getopt */
  263. }
  264. }
  265. if (signal_str != NULL) {
  266. if (sscanf (signal_str, "%d", &signal_nr) != 1) {
  267. if (parse_signal (signal_str, &signal_nr) != 0) {
  268. badusage ("--signal takes a numeric argument or name of signal (KILL, INTR, ...)");
  269. }
  270. }
  271. }
  272. if (start == stop)
  273. badusage("need one of --start or --stop");
  274. if (!execname && !pidfile && !userspec)
  275. badusage("need at least one of --exec, --pidfile or --user");
  276. if (!startas)
  277. startas = execname;
  278. if (start && !startas)
  279. badusage("--start needs --exec or --startas");
  280. }
  281. #if defined(OSLinux)
  282. static int
  283. pid_is_exec(int pid, const struct stat *esb)
  284. {
  285. struct stat sb;
  286. char buf[32];
  287. sprintf(buf, "/proc/%d/exe", pid);
  288. if (stat(buf, &sb) != 0)
  289. return 0;
  290. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  291. }
  292. static int
  293. pid_is_user(int pid, int uid)
  294. {
  295. struct stat sb;
  296. char buf[32];
  297. sprintf(buf, "/proc/%d", pid);
  298. if (stat(buf, &sb) != 0)
  299. return 0;
  300. return (sb.st_uid == uid);
  301. }
  302. static int
  303. pid_is_cmd(int pid, const char *name)
  304. {
  305. char buf[32];
  306. FILE *f;
  307. int c;
  308. sprintf(buf, "/proc/%d/stat", pid);
  309. f = fopen(buf, "r");
  310. if (!f)
  311. return 0;
  312. while ((c = getc(f)) != EOF && c != '(')
  313. ;
  314. if (c != '(') {
  315. fclose(f);
  316. return 0;
  317. }
  318. /* this hopefully handles command names containing ')' */
  319. while ((c = getc(f)) != EOF && c == *name)
  320. name++;
  321. fclose(f);
  322. return (c == ')' && *name == '\0');
  323. }
  324. #endif /* OSLinux */
  325. #if defined(OSHURD)
  326. static int
  327. pid_is_user(int pid, int uid)
  328. {
  329. struct stat sb;
  330. char buf[32];
  331. struct proc_stat *pstat;
  332. sprintf(buf, "/proc/%d", pid);
  333. if (stat(buf, &sb) != 0)
  334. return 0;
  335. return (sb.st_uid == uid);
  336. pstat = proc_stat_list_pid_proc_stat (procset, pid);
  337. if (pstat == NULL)
  338. fatal ("Error getting process information: NULL proc_stat struct");
  339. proc_stat_set_flags (pstat, PSTAT_PID | PSTAT_OWNER_UID);
  340. return (pstat->owner_uid == uid);
  341. }
  342. static int
  343. pid_is_cmd(int pid, const char *name)
  344. {
  345. struct proc_stat *pstat;
  346. pstat = proc_stat_list_pid_proc_stat (procset, pid);
  347. if (pstat == NULL)
  348. fatal ("Error getting process information: NULL proc_stat struct");
  349. proc_stat_set_flags (pstat, PSTAT_PID | PSTAT_ARGS);
  350. return (!strcmp (name, pstat->args));
  351. }
  352. #endif /* OSHURD */
  353. static void
  354. check(int pid)
  355. {
  356. #if defined(OSLinux)
  357. if (execname && !pid_is_exec(pid, &exec_stat))
  358. #elif defined(OSHURD)
  359. /* I will try this to see if it works */
  360. if (execname && !pid_is_cmd(pid, execname))
  361. #endif
  362. return;
  363. if (userspec && !pid_is_user(pid, user_id))
  364. return;
  365. if (cmdname && !pid_is_cmd(pid, cmdname))
  366. return;
  367. push(&found, pid);
  368. }
  369. static void
  370. do_pidfile(const char *name)
  371. {
  372. FILE *f;
  373. int pid;
  374. f = fopen(name, "r");
  375. if (f) {
  376. if (fscanf(f, "%d", &pid) == 1)
  377. check(pid);
  378. fclose(f);
  379. }
  380. }
  381. #if defined(OSLinux)
  382. static void
  383. do_procinit(void)
  384. {
  385. DIR *procdir;
  386. struct dirent *entry;
  387. int foundany, pid;
  388. procdir = opendir("/proc");
  389. if (!procdir)
  390. fatal("opendir /proc: %s", strerror(errno));
  391. foundany = 0;
  392. while ((entry = readdir(procdir)) != NULL) {
  393. if (sscanf(entry->d_name, "%d", &pid) != 1)
  394. continue;
  395. foundany++;
  396. check(pid);
  397. }
  398. closedir(procdir);
  399. if (!foundany)
  400. fatal("nothing in /proc - not mounted?");
  401. }
  402. #endif /* OSLinux */
  403. #if defined(OSHURD)
  404. error_t
  405. check_all (void *ptr)
  406. {
  407. struct proc_stat *pstat = ptr;
  408. check (pstat->pid);
  409. return (0);
  410. }
  411. static void
  412. do_psinit(void)
  413. error_t err;
  414. err = ps_context_create (getproc (), &context);
  415. if (err)
  416. error (1, err, "ps_context_create");
  417. err = proc_stat_list_create (context, &procset);
  418. if (err)
  419. error (1, err, "proc_stat_list_create");
  420. err = proc_stat_list_add_all (procset, 0, 0);
  421. if (err)
  422. error (1, err, "proc_stat_list_add_all");
  423. /* Check all pids */
  424. ihash_iterate (context->procs, check_all);
  425. }
  426. #endif /* OSHURD */
  427. static void
  428. do_stop(void)
  429. {
  430. char what[1024];
  431. struct pid_list *p;
  432. if (cmdname)
  433. strcpy(what, cmdname);
  434. else if (execname)
  435. strcpy(what, execname);
  436. else if (pidfile)
  437. sprintf(what, "process in pidfile `%s'", pidfile);
  438. else if (userspec)
  439. sprintf(what, "process(es) owned by `%s'", userspec);
  440. else
  441. fatal("internal error, please report");
  442. if (!found) {
  443. if (quietmode <= 0)
  444. printf("No %s found running; none killed.\n", what);
  445. exit(exitnodo);
  446. }
  447. for (p = found; p; p = p->next) {
  448. if (testmode)
  449. printf("Would send signal %d to %d.\n",
  450. signal_nr, p->pid);
  451. else if (kill(p->pid, signal_nr) == 0)
  452. push(&killed, p->pid);
  453. else
  454. printf("%s: warning: failed to kill %d: %s\n",
  455. progname, p->pid, strerror(errno));
  456. }
  457. if (quietmode < 0 && killed) {
  458. printf("Stopped %s (pid", what);
  459. for (p = killed; p; p = p->next)
  460. printf(" %d", p->pid);
  461. printf(").\n");
  462. }
  463. }
  464. int
  465. main(int argc, char **argv)
  466. {
  467. progname = argv[0];
  468. parse_options(argc, argv);
  469. argc -= optind;
  470. argv += optind;
  471. if (execname && stat(execname, &exec_stat))
  472. fatal("stat %s: %s", execname, strerror(errno));
  473. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  474. struct passwd *pw;
  475. pw = getpwnam(userspec);
  476. if (!pw)
  477. fatal("user `%s' not found\n", userspec);
  478. user_id = pw->pw_uid;
  479. }
  480. if (pidfile)
  481. do_pidfile(pidfile);
  482. else
  483. do_procinit();
  484. if (stop) {
  485. do_stop();
  486. exit(0);
  487. }
  488. if (found) {
  489. if (quietmode <= 0)
  490. printf("%s already running.\n", execname);
  491. exit(exitnodo);
  492. }
  493. if (testmode) {
  494. printf("Would start %s ", startas);
  495. while (argc-- > 0)
  496. printf("%s ", *argv++);
  497. printf(".\n");
  498. exit(0);
  499. }
  500. if (quietmode < 0)
  501. printf("Starting %s...\n", startas);
  502. *--argv = startas;
  503. execv(startas, argv);
  504. fatal("Unable to start %s: %s", startas, strerror(errno));
  505. }