start-stop-daemon.c 13 KB

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