start-stop-daemon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. #include "config.h"
  21. #if defined(linux)
  22. # define OSLinux
  23. #elif defined(__GNU__)
  24. # define OSHURD
  25. #elif defined(__sparc__)
  26. # define OSsunos
  27. #elif defined(OPENBSD)
  28. # define OSOpenBSD
  29. #else
  30. # error Unknown architecture - cannot build start-stop-daemon
  31. #endif
  32. #if defined(OSHURD)
  33. # include <hurd.h>
  34. # include <ps.h>
  35. #endif
  36. #if defined(OSOpenBSD)
  37. #include <sys/param.h>
  38. #include <sys/user.h>
  39. #include <sys/proc.h>
  40. #include <sys/stat.h>
  41. #include <sys/sysctl.h>
  42. #include <sys/types.h>
  43. #include <err.h>
  44. #include <kvm.h>
  45. #include <limits.h>
  46. #endif
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <stdarg.h>
  52. #include <signal.h>
  53. #include <sys/stat.h>
  54. #include <dirent.h>
  55. #include <unistd.h>
  56. #include <getopt.h>
  57. #include <pwd.h>
  58. #include <grp.h>
  59. #include <sys/ioctl.h>
  60. #include <sys/types.h>
  61. #include <sys/termios.h>
  62. #include <fcntl.h>
  63. #ifdef HAVE_ERROR_H
  64. # include <error.h>
  65. #endif
  66. #ifdef HURD_IHASH_H
  67. #include <hurd/ihash.h>
  68. #endif
  69. static int testmode = 0;
  70. static int quietmode = 0;
  71. static int exitnodo = 1;
  72. static int start = 0;
  73. static int stop = 0;
  74. static int background = 0;
  75. static int mpidfile = 0;
  76. static int signal_nr = 15;
  77. static const char *signal_str = NULL;
  78. static int user_id = -1;
  79. static int runas_uid = -1;
  80. static int runas_gid = -1;
  81. static const char *userspec = NULL;
  82. static char *changeuser = NULL;
  83. static const char *changegroup = NULL;
  84. static char *changeroot = NULL;
  85. static const char *cmdname = NULL;
  86. static char *execname = NULL;
  87. static char *startas = NULL;
  88. static const char *pidfile = NULL;
  89. static const char *progname = "";
  90. static int nicelevel = 0;
  91. static struct stat exec_stat;
  92. #if defined(OSHURD)
  93. static struct proc_stat_list *procset;
  94. #endif
  95. struct pid_list {
  96. struct pid_list *next;
  97. int pid;
  98. };
  99. static struct pid_list *found = NULL;
  100. static struct pid_list *killed = NULL;
  101. static void *xmalloc(int size);
  102. static void push(struct pid_list **list, int pid);
  103. static void do_help(void);
  104. static void parse_options(int argc, char * const *argv);
  105. static int pid_is_user(int pid, int uid);
  106. static int pid_is_cmd(int pid, const char *name);
  107. static void check(int pid);
  108. static void do_pidfile(const char *name);
  109. static int do_stop(void);
  110. #if defined(OSLinux)
  111. static int pid_is_exec(int pid, const struct stat *esb);
  112. #endif
  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. static void
  123. fatal(const char *format, ...)
  124. {
  125. va_list arglist;
  126. fprintf(stderr, "%s: ", progname);
  127. va_start(arglist, format);
  128. vfprintf(stderr, format, arglist);
  129. va_end(arglist);
  130. putc('\n', stderr);
  131. exit(2);
  132. }
  133. static void *
  134. xmalloc(int size)
  135. {
  136. void *ptr;
  137. ptr = malloc(size);
  138. if (ptr)
  139. return ptr;
  140. fatal("malloc(%d) failed", size);
  141. }
  142. static void
  143. push(struct pid_list **list, int pid)
  144. {
  145. struct pid_list *p;
  146. p = xmalloc(sizeof(*p));
  147. p->next = *list;
  148. p->pid = pid;
  149. *list = p;
  150. }
  151. static void
  152. do_help(void)
  153. {
  154. printf("\
  155. start-stop-daemon for Debian GNU/Linux - small and fast C version written by\n\
  156. Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
  157. VERSION "\n\
  158. \n\
  159. Usage:
  160. start-stop-daemon -S|--start options ... -- arguments ...\n\
  161. start-stop-daemon -K|--stop options ...\n\
  162. start-stop-daemon -H|--help\n\
  163. start-stop-daemon -V|--version\n\
  164. \n\
  165. Options (at least one of --exec|--pidfile|--user is required):
  166. -x|--exec <executable> program to start/check if it is running\n\
  167. -p|--pidfile <pid-file> pid file to check\n\
  168. -c|--chuid <name|uid[:group|gid]>
  169. change to this user/group before starting process\n\
  170. -u|--user <username>|<uid> stop processes owned by this user\n\
  171. -n|--name <process-name> stop processes with this name\n\
  172. -s|--signal <signal> signal to send (default TERM)\n\
  173. -a|--startas <pathname> program to start (default is <executable>)\n\
  174. -N|--nicelevel <incr> add incr to the process's nice level\n\
  175. -b|--background force the process to detach\n\
  176. -m|--make-pidfile create the pidfile before starting\n\
  177. -t|--test test mode, don't do anything\n\
  178. -o|--oknodo exit status 0 (not 1) if nothing done\n\
  179. -q|--quiet be more quiet\n\
  180. -v|--verbose be more verbose\n\
  181. \n\
  182. Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo) 2 = trouble\n");
  183. }
  184. static void
  185. badusage(const char *msg)
  186. {
  187. if (msg)
  188. fprintf(stderr, "%s: %s\n", progname, msg);
  189. fprintf(stderr, "Try `%s --help' for more information.\n", progname);
  190. exit(2);
  191. }
  192. struct sigpair {
  193. const char *name;
  194. int signal;
  195. };
  196. const struct sigpair siglist[] = {
  197. { "ABRT", SIGABRT },
  198. { "ALRM", SIGALRM },
  199. { "FPE", SIGFPE },
  200. { "HUP", SIGHUP },
  201. { "ILL", SIGILL },
  202. { "INT", SIGINT },
  203. { "KILL", SIGKILL },
  204. { "PIPE", SIGPIPE },
  205. { "QUIT", SIGQUIT },
  206. { "SEGV", SIGSEGV },
  207. { "TERM", SIGTERM },
  208. { "USR1", SIGUSR1 },
  209. { "USR2", SIGUSR2 },
  210. { "CHLD", SIGCHLD },
  211. { "CONT", SIGCONT },
  212. { "STOP", SIGSTOP },
  213. { "TSTP", SIGTSTP },
  214. { "TTIN", SIGTTIN },
  215. { "TTOU", SIGTTOU }
  216. };
  217. static int parse_signal (const char *signal_str, int *signal_nr)
  218. {
  219. int i;
  220. for (i = 0; i < sizeof (siglist) / sizeof (siglist[0]); i++) {
  221. if (strcmp (signal_str, siglist[i].name) == 0) {
  222. *signal_nr = siglist[i].signal;
  223. return 0;
  224. }
  225. }
  226. return -1;
  227. }
  228. static void
  229. parse_options(int argc, char * const *argv)
  230. {
  231. static struct option longopts[] = {
  232. { "help", 0, NULL, 'H'},
  233. { "stop", 0, NULL, 'K'},
  234. { "start", 0, NULL, 'S'},
  235. { "version", 0, NULL, 'V'},
  236. { "startas", 1, NULL, 'a'},
  237. { "name", 1, NULL, 'n'},
  238. { "oknodo", 0, NULL, 'o'},
  239. { "pidfile", 1, NULL, 'p'},
  240. { "quiet", 0, NULL, 'q'},
  241. { "signal", 1, NULL, 's'},
  242. { "test", 0, NULL, 't'},
  243. { "user", 1, NULL, 'u'},
  244. { "chroot", 1, NULL, 'r'},
  245. { "verbose", 0, NULL, 'v'},
  246. { "exec", 1, NULL, 'x'},
  247. { "chuid", 1, NULL, 'c'},
  248. { "nicelevel", 1, NULL, 'N'},
  249. { "background", 0, NULL, 'b'},
  250. { "make-pidfile", 0, NULL, 'm'},
  251. { NULL, 0, NULL, 0}
  252. };
  253. int c;
  254. for (;;) {
  255. c = getopt_long(argc, argv, "HKSVa:n:op:qr:s:tu:vx:c:N:bm",
  256. longopts, (int *) 0);
  257. if (c == -1)
  258. break;
  259. switch (c) {
  260. case 'H': /* --help */
  261. do_help();
  262. exit(0);
  263. case 'K': /* --stop */
  264. stop = 1;
  265. break;
  266. case 'S': /* --start */
  267. start = 1;
  268. break;
  269. case 'V': /* --version */
  270. printf("start-stop-daemon " VERSION "\n");
  271. exit(0);
  272. case 'a': /* --startas <pathname> */
  273. startas = optarg;
  274. break;
  275. case 'n': /* --name <process-name> */
  276. cmdname = optarg;
  277. break;
  278. case 'o': /* --oknodo */
  279. exitnodo = 0;
  280. break;
  281. case 'p': /* --pidfile <pid-file> */
  282. pidfile = optarg;
  283. break;
  284. case 'q': /* --quiet */
  285. quietmode = 1;
  286. break;
  287. case 's': /* --signal <signal> */
  288. signal_str = optarg;
  289. break;
  290. case 't': /* --test */
  291. testmode = 1;
  292. break;
  293. case 'u': /* --user <username>|<uid> */
  294. userspec = optarg;
  295. break;
  296. case 'v': /* --verbose */
  297. quietmode = -1;
  298. break;
  299. case 'x': /* --exec <executable> */
  300. execname = optarg;
  301. break;
  302. case 'c': /* --chuid <username>|<uid> */
  303. /* we copy the string just in case we need the
  304. * argument later. */
  305. changeuser = strdup(optarg);
  306. changeuser = strtok(changeuser, ":");
  307. changegroup = strtok(NULL, ":");
  308. break;
  309. case 'r': /* --chroot /new/root */
  310. changeroot = optarg;
  311. break;
  312. case 'N': /* --nice */
  313. nicelevel = atoi(optarg);
  314. break;
  315. case 'b': /* --background */
  316. background = 1;
  317. break;
  318. case 'm': /* --make-pidfile */
  319. mpidfile = 1;
  320. break;
  321. default:
  322. badusage(NULL); /* message printed by getopt */
  323. }
  324. }
  325. if (signal_str != NULL) {
  326. if (sscanf (signal_str, "%d", &signal_nr) != 1) {
  327. if (parse_signal (signal_str, &signal_nr) != 0) {
  328. badusage ("--signal takes a numeric argument or name of signal (KILL, INTR, ...)");
  329. }
  330. }
  331. }
  332. if (start == stop)
  333. badusage("need one of --start or --stop");
  334. if (!execname && !pidfile && !userspec && !cmdname)
  335. badusage("need at least one of --exec, --pidfile, --user or --name");
  336. if (!startas)
  337. startas = execname;
  338. if (start && !startas)
  339. badusage("--start needs --exec or --startas");
  340. if (mpidfile && pidfile == NULL)
  341. badusage("--make-pidfile is only relevant with --pidfile");
  342. if (background && !start)
  343. badusage("--background is only relevant with --start");
  344. }
  345. #if defined(OSLinux)
  346. static int
  347. pid_is_exec(int pid, const struct stat *esb)
  348. {
  349. struct stat sb;
  350. char buf[32];
  351. sprintf(buf, "/proc/%d/exe", pid);
  352. if (stat(buf, &sb) != 0)
  353. return 0;
  354. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  355. }
  356. static int
  357. pid_is_user(int pid, int uid)
  358. {
  359. struct stat sb;
  360. char buf[32];
  361. sprintf(buf, "/proc/%d", pid);
  362. if (stat(buf, &sb) != 0)
  363. return 0;
  364. return (sb.st_uid == uid);
  365. }
  366. static int
  367. pid_is_cmd(int pid, const char *name)
  368. {
  369. char buf[32];
  370. FILE *f;
  371. int c;
  372. sprintf(buf, "/proc/%d/stat", pid);
  373. f = fopen(buf, "r");
  374. if (!f)
  375. return 0;
  376. while ((c = getc(f)) != EOF && c != '(')
  377. ;
  378. if (c != '(') {
  379. fclose(f);
  380. return 0;
  381. }
  382. /* this hopefully handles command names containing ')' */
  383. while ((c = getc(f)) != EOF && c == *name)
  384. name++;
  385. fclose(f);
  386. return (c == ')' && *name == '\0');
  387. }
  388. #endif /* OSLinux */
  389. #if defined(OSHURD)
  390. static int
  391. pid_is_user(int pid, int uid)
  392. {
  393. struct stat sb;
  394. char buf[32];
  395. struct proc_stat *pstat;
  396. sprintf(buf, "/proc/%d", pid);
  397. if (stat(buf, &sb) != 0)
  398. return 0;
  399. return (sb.st_uid == uid);
  400. pstat = proc_stat_list_pid_proc_stat (procset, pid);
  401. if (pstat == NULL)
  402. fatal ("Error getting process information: NULL proc_stat struct");
  403. proc_stat_set_flags (pstat, PSTAT_PID | PSTAT_OWNER_UID);
  404. return (pstat->owner_uid == uid);
  405. }
  406. static int
  407. pid_is_cmd(int pid, const char *name)
  408. {
  409. struct proc_stat *pstat;
  410. pstat = proc_stat_list_pid_proc_stat (procset, pid);
  411. if (pstat == NULL)
  412. fatal ("Error getting process information: NULL proc_stat struct");
  413. proc_stat_set_flags (pstat, PSTAT_PID | PSTAT_ARGS);
  414. return (!strcmp (name, pstat->args));
  415. }
  416. #endif /* OSHURD */
  417. static void
  418. check(int pid)
  419. {
  420. #if defined(OSLinux)
  421. if (execname && !pid_is_exec(pid, &exec_stat))
  422. #elif defined(OSHURD)
  423. /* I will try this to see if it works */
  424. if (execname && !pid_is_cmd(pid, execname))
  425. #endif
  426. return;
  427. if (userspec && !pid_is_user(pid, user_id))
  428. return;
  429. if (cmdname && !pid_is_cmd(pid, cmdname))
  430. return;
  431. push(&found, pid);
  432. }
  433. static void
  434. do_pidfile(const char *name)
  435. {
  436. FILE *f;
  437. int pid;
  438. f = fopen(name, "r");
  439. if (f) {
  440. if (fscanf(f, "%d", &pid) == 1)
  441. check(pid);
  442. fclose(f);
  443. } else if (errno != ENOENT)
  444. fatal("open pidfile %s: %s", name, strerror(errno));
  445. }
  446. /* WTA: this needs to be an autoconf check for /proc/pid existance.
  447. */
  448. #if defined(OSLinux) || defined (OSsunos)
  449. static void
  450. do_procinit(void)
  451. {
  452. DIR *procdir;
  453. struct dirent *entry;
  454. int foundany, pid;
  455. procdir = opendir("/proc");
  456. if (!procdir)
  457. fatal("opendir /proc: %s", strerror(errno));
  458. foundany = 0;
  459. while ((entry = readdir(procdir)) != NULL) {
  460. if (sscanf(entry->d_name, "%d", &pid) != 1)
  461. continue;
  462. foundany++;
  463. check(pid);
  464. }
  465. closedir(procdir);
  466. if (!foundany)
  467. fatal("nothing in /proc - not mounted?");
  468. }
  469. #endif /* OSLinux */
  470. #if defined(OSHURD)
  471. error_t
  472. check_all (void *ptr)
  473. {
  474. struct proc_stat *pstat = ptr;
  475. check (pstat->pid);
  476. return (0);
  477. }
  478. static void
  479. do_procinit(void)
  480. {
  481. struct ps_context *context;
  482. error_t err;
  483. err = ps_context_create (getproc (), &context);
  484. if (err)
  485. error (1, err, "ps_context_create");
  486. err = proc_stat_list_create (context, &procset);
  487. if (err)
  488. error (1, err, "proc_stat_list_create");
  489. err = proc_stat_list_add_all (procset, 0, 0);
  490. if (err)
  491. error (1, err, "proc_stat_list_add_all");
  492. /* Check all pids */
  493. ihash_iterate (context->procs, check_all);
  494. }
  495. #endif /* OSHURD */
  496. #if defined(OSOpenBSD)
  497. int
  498. pid_is_cmd(int pid, const char *name)
  499. {
  500. kvm_t *kd;
  501. int nentries, argv_len=0;
  502. struct kinfo_proc *kp;
  503. char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
  504. char **pid_argv_p;
  505. char *start_argv_0_p, *end_argv_0_p;
  506. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  507. if (kd == 0)
  508. errx(1, "%s", errbuf);
  509. if ((kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries)) == 0)
  510. errx(1, "%s", kvm_geterr(kd));
  511. if ( ( pid_argv_p = kvm_getargv(kd, kp, argv_len)) == 0)
  512. errx(1, "%s", kvm_geterr(kd));
  513. start_argv_0_p = *pid_argv_p;
  514. /* find and compare string */
  515. /* find end of argv[0] then copy and cut of str there. */
  516. if ( (end_argv_0_p = strchr(*pid_argv_p, ' ')) == 0 )
  517. /* There seems to be no space, so we have the command
  518. * allready in its desired form. */
  519. start_argv_0_p = *pid_argv_p;
  520. else {
  521. /* Tests indicate that this never happens, since
  522. * kvm_getargv itselfe cuts of tailing stuff. This is
  523. * not what the manpage says, however. */
  524. strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
  525. buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
  526. start_argv_0_p = buf;
  527. }
  528. if (strlen(name) != strlen(start_argv_0_p))
  529. return 0;
  530. return (strcmp(name, start_argv_0_p) == 0) ? 1 : 0;
  531. }
  532. int
  533. pid_is_user(int pid, int uid)
  534. {
  535. kvm_t *kd;
  536. int nentries; /* Value not used */
  537. uid_t proc_uid;
  538. struct kinfo_proc *kp;
  539. char errbuf[_POSIX2_LINE_MAX];
  540. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  541. if (kd == 0)
  542. errx(1, "%s", errbuf);
  543. if ((kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries)) == 0)
  544. errx(1, "%s", kvm_geterr(kd));
  545. if ( kp->kp_proc.p_cred )
  546. kvm_read(kd, (u_long)&(kp->kp_proc.p_cred->p_ruid),
  547. &proc_uid, sizeof(uid_t));
  548. else
  549. return 0;
  550. return (proc_uid == (uid_t)uid);
  551. }
  552. int
  553. pid_is_exec(int pid, const char *name)
  554. {
  555. kvm_t *kd;
  556. int nentries;
  557. struct kinfo_proc *kp;
  558. char errbuf[_POSIX2_LINE_MAX], *pidexec;
  559. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  560. if (kd == 0)
  561. errx(1, "%s", errbuf);
  562. if ((kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries)) == 0)
  563. errx(1, "%s", kvm_geterr(kd));
  564. pidexec = (&kp->kp_proc)->p_comm;
  565. if (strlen(name) != strlen(pidexec))
  566. return 0;
  567. return (strcmp(name, pidexec) == 0) ? 1 : 0;
  568. }
  569. static void
  570. do_procinit(void)
  571. {
  572. /* Nothing to do */
  573. }
  574. #endif /* OSOpenBSD */
  575. /* return 1 on failure */
  576. static int
  577. do_stop(void)
  578. {
  579. char what[2048];
  580. struct pid_list *p;
  581. int retval = 0;
  582. if (cmdname)
  583. snprintf(what, sizeof(what), "%s", cmdname);
  584. else if (execname)
  585. snprintf(what, sizeof(what), "%s", execname);
  586. else if (pidfile)
  587. snprintf(what, sizeof(what), "process in pidfile `%s'", pidfile);
  588. else if (userspec)
  589. snprintf(what, sizeof(what), "process(es) owned by `%s'", userspec);
  590. else
  591. fatal("internal error, please report");
  592. if (!found) {
  593. if (quietmode <= 0)
  594. printf("No %s found running; none killed.\n", what);
  595. exit(exitnodo);
  596. }
  597. for (p = found; p; p = p->next) {
  598. if (testmode)
  599. printf("Would send signal %d to %d.\n",
  600. signal_nr, p->pid);
  601. else if (kill(p->pid, signal_nr) == 0)
  602. push(&killed, p->pid);
  603. else {
  604. printf("%s: warning: failed to kill %d: %s\n",
  605. progname, p->pid, strerror(errno));
  606. retval += exitnodo;
  607. }
  608. }
  609. if (quietmode < 0 && killed) {
  610. printf("Stopped %s (pid", what);
  611. for (p = killed; p; p = p->next)
  612. printf(" %d", p->pid);
  613. printf(").\n");
  614. }
  615. return retval;
  616. }
  617. int
  618. main(int argc, char **argv)
  619. {
  620. progname = argv[0];
  621. parse_options(argc, argv);
  622. argc -= optind;
  623. argv += optind;
  624. if (execname && stat(execname, &exec_stat))
  625. fatal("stat %s: %s", execname, strerror(errno));
  626. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  627. struct passwd *pw;
  628. pw = getpwnam(userspec);
  629. if (!pw)
  630. fatal("user `%s' not found\n", userspec);
  631. user_id = pw->pw_uid;
  632. }
  633. if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) {
  634. struct group *gr = getgrnam(changegroup);
  635. if (!gr)
  636. fatal("group `%s' not found\n", changegroup);
  637. runas_gid = gr->gr_gid;
  638. }
  639. if (changeuser && sscanf(changeuser, "%d", &runas_uid) != 1) {
  640. struct passwd *pw = getpwnam(changeuser);
  641. if (!pw)
  642. fatal("user `%s' not found\n", changeuser);
  643. runas_uid = pw->pw_uid;
  644. if (changegroup == NULL) { /* pass the default group of this user */
  645. changegroup = ""; /* just empty */
  646. runas_gid = pw->pw_gid;
  647. }
  648. }
  649. if (pidfile)
  650. do_pidfile(pidfile);
  651. else
  652. do_procinit();
  653. if (stop) {
  654. int i = do_stop();
  655. if (i) {
  656. if (quietmode <= 0)
  657. printf("%d pids were not killed\n", i);
  658. exit(1);
  659. }
  660. exit(0);
  661. }
  662. if (found) {
  663. if (quietmode <= 0)
  664. printf("%s already running.\n", execname);
  665. exit(exitnodo);
  666. }
  667. if (testmode) {
  668. printf("Would start %s ", startas);
  669. while (argc-- > 0)
  670. printf("%s ", *argv++);
  671. if (changeuser != NULL) {
  672. printf(" (as user %s[%d]", changeuser, runas_uid);
  673. if (changegroup != NULL)
  674. printf(", and group %s[%d])", changegroup, runas_gid);
  675. else
  676. printf(")");
  677. }
  678. if (changeroot != NULL)
  679. printf(" in directory %s", changeroot);
  680. if (nicelevel)
  681. printf(", and add %i to the priority", nicelevel);
  682. printf(".\n");
  683. exit(0);
  684. }
  685. if (quietmode < 0)
  686. printf("Starting %s...\n", startas);
  687. *--argv = startas;
  688. if (changeroot != NULL) {
  689. if (chdir(changeroot) < 0)
  690. fatal("Unable to chdir() to %s", changeroot);
  691. if (chroot(changeroot) < 0)
  692. fatal("Unable to chroot() to %s", changeroot);
  693. }
  694. if (changeuser != NULL) {
  695. if (setgid(runas_gid))
  696. fatal("Unable to set gid to %d", runas_gid);
  697. if (initgroups(changeuser, runas_gid))
  698. fatal("Unable to set initgroups() with gid %d", runas_gid);
  699. if (setuid(runas_uid))
  700. fatal("Unable to set uid to %s", changeuser);
  701. }
  702. if (background) { /* ok, we need to detach this process */
  703. int i, fd;
  704. if (quietmode < 0)
  705. printf("Detatching to start %s...", startas);
  706. i = fork();
  707. if (i<0) {
  708. fatal("Unable to fork.\n");
  709. }
  710. if (i) { /* parent */
  711. if (quietmode < 0)
  712. printf("done.\n");
  713. exit(0);
  714. }
  715. /* child continues here */
  716. /* now close all extra fds */
  717. for (i=getdtablesize()-1; i>=0; --i) close(i);
  718. /* change tty */
  719. fd = open("/dev/tty", O_RDWR);
  720. ioctl(fd, TIOCNOTTY, 0);
  721. close(fd);
  722. chdir("/");
  723. umask(022); /* set a default for dumb programs */
  724. setpgid(0,0); /* set the process group */
  725. fd=open("/dev/null", O_RDWR); /* stdin */
  726. dup(fd); /* stdout */
  727. dup(fd); /* stderr */
  728. }
  729. if (nicelevel) {
  730. if (nice(nicelevel))
  731. fatal("Unable to alter nice level by %i: %s", nicelevel,
  732. strerror(errno));
  733. }
  734. if (mpidfile && pidfile != NULL) { /* user wants _us_ to make the pidfile :) */
  735. FILE *pidf = fopen(pidfile, "w");
  736. pid_t pidt = getpid();
  737. if (pidf == NULL)
  738. fatal("Unable to open pidfile `%s' for writing: %s", pidfile,
  739. strerror(errno));
  740. fprintf(pidf, "%d\n", pidt);
  741. fclose(pidf);
  742. }
  743. execv(startas, argv);
  744. fatal("Unable to start %s: %s", startas, strerror(errno));
  745. }