start-stop-daemon.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. #include "config.h"
  23. #if defined(linux) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
  24. # define OSLinux
  25. #elif defined(__GNU__)
  26. # define OSHURD
  27. #elif defined(__sparc__)
  28. # define OSsunos
  29. #elif defined(OPENBSD) || defined(__OpenBSD__)
  30. # define OSOpenBSD
  31. #elif defined(hpux)
  32. # define OShpux
  33. #elif defined(__FreeBSD__)
  34. # define OSFreeBSD
  35. #elif defined(__NetBSD__)
  36. # define OSNetBSD
  37. #else
  38. # error Unknown architecture - cannot build start-stop-daemon
  39. #endif
  40. #define MIN_POLL_INTERVAL 20000 /*us*/
  41. #if defined(OSHURD)
  42. #include <hurd.h>
  43. #include <ps.h>
  44. #endif
  45. #if defined(OSOpenBSD) || defined(OSFreeBSD) || defined(OSNetBSD)
  46. #include <sys/param.h>
  47. #include <sys/proc.h>
  48. #include <sys/stat.h>
  49. #include <sys/types.h>
  50. #include <err.h>
  51. #include <limits.h>
  52. #endif
  53. #ifdef HAVE_KVM_H
  54. #include <kvm.h>
  55. #include <sys/sysctl.h>
  56. #include <sys/user.h>
  57. #endif
  58. #if defined(OShpux)
  59. #include <sys/param.h>
  60. #include <sys/pstat.h>
  61. #endif
  62. #include <errno.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <stdarg.h>
  67. #include <signal.h>
  68. #include <sys/stat.h>
  69. #include <dirent.h>
  70. #include <sys/time.h>
  71. #include <unistd.h>
  72. #include <getopt.h>
  73. #include <pwd.h>
  74. #include <grp.h>
  75. #include <sys/ioctl.h>
  76. #include <sys/types.h>
  77. #include <sys/termios.h>
  78. #include <fcntl.h>
  79. #include <limits.h>
  80. #include <assert.h>
  81. #include <ctype.h>
  82. #ifdef HAVE_SYS_CDEFS_H
  83. #include <sys/cdefs.h>
  84. #endif
  85. #ifdef HAVE_STDDEF_H
  86. #include <stddef.h>
  87. #endif
  88. #ifdef HAVE_ERROR_H
  89. #include <error.h>
  90. #endif
  91. #if HAVE_C_ATTRIBUTE
  92. # define CONSTANT __attribute__((constant))
  93. # define PRINTFFORMAT(si, tc) __attribute__((format(printf, si, tc)))
  94. # define NONRETURNING __attribute__((noreturn))
  95. # define UNUSED __attribute__((unused))
  96. #else
  97. # define CONSTANT
  98. # define PRINTFFORMAT(si, tc)
  99. # define NONRETURNING
  100. # define UNUSED
  101. #endif
  102. static int testmode = 0;
  103. static int quietmode = 0;
  104. static int exitnodo = 1;
  105. static int start = 0;
  106. static int stop = 0;
  107. static int background = 0;
  108. static int mpidfile = 0;
  109. static int signal_nr = 15;
  110. static int user_id = -1;
  111. static int runas_uid = -1;
  112. static int runas_gid = -1;
  113. static const char *userspec = NULL;
  114. static char *changeuser = NULL;
  115. static const char *changegroup = NULL;
  116. static char *changeroot = NULL;
  117. static const char *changedir = "/";
  118. static const char *cmdname = NULL;
  119. static char *execname = NULL;
  120. static char *startas = NULL;
  121. static const char *pidfile = NULL;
  122. static char what_stop[1024];
  123. static const char *progname = "";
  124. static int nicelevel = 0;
  125. static int umask_value = -1;
  126. static struct stat exec_stat;
  127. #if defined(OSHURD)
  128. static struct proc_stat_list *procset = NULL;
  129. #endif
  130. struct pid_list {
  131. struct pid_list *next;
  132. pid_t pid;
  133. };
  134. static struct pid_list *found = NULL;
  135. static struct pid_list *killed = NULL;
  136. struct schedule_item {
  137. enum {
  138. sched_timeout,
  139. sched_signal,
  140. sched_goto,
  141. sched_forever /* Only seen within parse_schedule and callees */
  142. } type;
  143. int value; /* Seconds, signal no., or index into array */
  144. };
  145. static int schedule_length;
  146. static struct schedule_item *schedule = NULL;
  147. static void *xmalloc(int size);
  148. static void push(struct pid_list **list, pid_t pid);
  149. static void do_help(void);
  150. static void parse_options(int argc, char * const *argv);
  151. static int pid_is_user(pid_t pid, uid_t uid);
  152. static int pid_is_cmd(pid_t pid, const char *name);
  153. static void check(pid_t pid);
  154. static void do_pidfile(const char *name);
  155. static void do_stop(int signal_nr, int quietmode,
  156. int *n_killed, int *n_notkilled, int retry_nr);
  157. #if defined(OSLinux) || defined(OShpux)
  158. static int pid_is_exec(pid_t pid, const struct stat *esb);
  159. #endif
  160. static void fatal(const char *format, ...)
  161. NONRETURNING PRINTFFORMAT(1, 2);
  162. static void badusage(const char *msg)
  163. NONRETURNING;
  164. static void
  165. fatal(const char *format, ...)
  166. {
  167. va_list arglist;
  168. fprintf(stderr, "%s: ", progname);
  169. va_start(arglist, format);
  170. vfprintf(stderr, format, arglist);
  171. va_end(arglist);
  172. fprintf(stderr, " (%s)\n", strerror (errno));
  173. exit(2);
  174. }
  175. static void *
  176. xmalloc(int size)
  177. {
  178. void *ptr;
  179. ptr = malloc(size);
  180. if (ptr)
  181. return ptr;
  182. fatal("malloc(%d) failed", size);
  183. }
  184. static void
  185. xgettimeofday(struct timeval *tv)
  186. {
  187. if (gettimeofday(tv, NULL) != 0)
  188. fatal("gettimeofday failed: %s", strerror(errno));
  189. }
  190. static void
  191. tmul(struct timeval *a, int b)
  192. {
  193. a->tv_sec *= b;
  194. a->tv_usec *= b;
  195. a->tv_sec = a->tv_sec + a->tv_usec / 1000000;
  196. a->tv_usec %= 1000000;
  197. }
  198. static long
  199. get_open_fd_max(void)
  200. {
  201. #ifdef HAVE_GETDTABLESIZE
  202. return getdtablesize();
  203. #else
  204. return sysconf(_SC_OPEN_MAX);
  205. #endif
  206. }
  207. static void
  208. daemonize(void)
  209. {
  210. pid_t pid;
  211. if (quietmode < 0)
  212. printf("Detaching to start %s...", startas);
  213. pid = fork();
  214. if (pid < 0)
  215. fatal("Unable to do first fork.\n");
  216. else if (pid) /* Parent */
  217. _exit(0);
  218. /* Create a new session */
  219. #ifdef HAVE_SETSID
  220. setsid();
  221. #else
  222. setpgid(0, 0);
  223. #endif
  224. pid = fork();
  225. if (pid < 0)
  226. fatal("Unable to do second fork.\n");
  227. else if (pid) /* Parent */
  228. _exit(0);
  229. if (quietmode < 0)
  230. printf("done.\n");
  231. }
  232. static void
  233. push(struct pid_list **list, pid_t pid)
  234. {
  235. struct pid_list *p;
  236. p = xmalloc(sizeof(*p));
  237. p->next = *list;
  238. p->pid = pid;
  239. *list = p;
  240. }
  241. static void
  242. clear(struct pid_list **list)
  243. {
  244. struct pid_list *here, *next;
  245. for (here = *list; here != NULL; here = next) {
  246. next = here->next;
  247. free(here);
  248. }
  249. *list = NULL;
  250. }
  251. static void
  252. do_help(void)
  253. {
  254. printf(
  255. "Usage: start-stop-daemon [<option> ...] <command>\n"
  256. "\n"
  257. "Commands:\n"
  258. " -S|--start -- <argument> ... start a program and pass <arguments> to it\n"
  259. " -K|--stop stop a program\n"
  260. " -H|--help print help information\n"
  261. " -V|--version print version\n"
  262. "\n"
  263. "Matching options (at least one is required):\n"
  264. " -p|--pidfile <pid-file> pid file to check\n"
  265. " -x|--exec <executable> program to start/check if it is running\n"
  266. " -n|--name <process-name> process name to check\n"
  267. " -u|--user <username|uid> process owner to check\n"
  268. "\n"
  269. "Options:\n"
  270. " -g|--group <group|gid> run process as this group\n"
  271. " -c|--chuid <name|uid[:group|gid]>\n"
  272. " change to this user/group before starting\n"
  273. " process\n"
  274. " -s|--signal <signal> signal to send (default TERM)\n"
  275. " -a|--startas <pathname> program to start (default is <executable>)\n"
  276. " -r|--chroot <directory> chroot to <directory> before starting\n"
  277. " -d|--chdir <directory> change to <directory> (default is /)\n"
  278. " -N|--nicelevel <incr> add incr to the process's nice level\n"
  279. " -k|--umask <mask> change the umask to <mask> before starting\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. "\n"
  288. "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
  289. " -<signal-num>|[-]<signal-name> send that signal\n"
  290. " <timeout> wait that many seconds\n"
  291. " forever repeat remainder forever\n"
  292. "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
  293. "\n"
  294. "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n"
  295. " 3 = trouble 2 = with --retry, processes wouldn't die\n");
  296. }
  297. static void
  298. do_version(void)
  299. {
  300. printf("start-stop-daemon %s for Debian\n\n", VERSION);
  301. printf("Written by Marek Michalkiewicz, public domain.\n");
  302. }
  303. static void
  304. badusage(const char *msg)
  305. {
  306. if (msg)
  307. fprintf(stderr, "%s: %s\n", progname, msg);
  308. fprintf(stderr, "Try `%s --help' for more information.\n", progname);
  309. exit(3);
  310. }
  311. struct sigpair {
  312. const char *name;
  313. int signal;
  314. };
  315. static const struct sigpair siglist[] = {
  316. { "ABRT", SIGABRT },
  317. { "ALRM", SIGALRM },
  318. { "FPE", SIGFPE },
  319. { "HUP", SIGHUP },
  320. { "ILL", SIGILL },
  321. { "INT", SIGINT },
  322. { "KILL", SIGKILL },
  323. { "PIPE", SIGPIPE },
  324. { "QUIT", SIGQUIT },
  325. { "SEGV", SIGSEGV },
  326. { "TERM", SIGTERM },
  327. { "USR1", SIGUSR1 },
  328. { "USR2", SIGUSR2 },
  329. { "CHLD", SIGCHLD },
  330. { "CONT", SIGCONT },
  331. { "STOP", SIGSTOP },
  332. { "TSTP", SIGTSTP },
  333. { "TTIN", SIGTTIN },
  334. { "TTOU", SIGTTOU }
  335. };
  336. static int
  337. parse_integer(const char *string, int *value_r)
  338. {
  339. unsigned long ul;
  340. char *ep;
  341. if (!string[0])
  342. return -1;
  343. ul = strtoul(string, &ep, 10);
  344. if (ul > INT_MAX || *ep != '\0')
  345. return -1;
  346. *value_r = ul;
  347. return 0;
  348. }
  349. static int
  350. parse_signal(const char *signal_str, int *signal_nr)
  351. {
  352. unsigned int i;
  353. if (parse_integer(signal_str, signal_nr) == 0)
  354. return 0;
  355. for (i = 0; i < sizeof(siglist) / sizeof(siglist[0]); i++) {
  356. if (strcmp(signal_str, siglist[i].name) == 0) {
  357. *signal_nr = siglist[i].signal;
  358. return 0;
  359. }
  360. }
  361. return -1;
  362. }
  363. static int
  364. parse_umask(const char *string, int *value_r)
  365. {
  366. if (!string[0])
  367. return -1;
  368. errno = 0;
  369. *value_r = strtoul(string, NULL, 0);
  370. if (errno)
  371. return -1;
  372. else
  373. return 0;
  374. }
  375. static void
  376. parse_schedule_item(const char *string, struct schedule_item *item)
  377. {
  378. const char *after_hyph;
  379. if (!strcmp(string, "forever")) {
  380. item->type = sched_forever;
  381. } else if (isdigit(string[0])) {
  382. item->type = sched_timeout;
  383. if (parse_integer(string, &item->value) != 0)
  384. badusage("invalid timeout value in schedule");
  385. } else if ((after_hyph = string + (string[0] == '-')) &&
  386. parse_signal(after_hyph, &item->value) == 0) {
  387. item->type = sched_signal;
  388. } else {
  389. badusage("invalid schedule item (must be [-]<signal-name>, "
  390. "-<signal-number>, <timeout> or `forever'");
  391. }
  392. }
  393. static void
  394. parse_schedule(const char *schedule_str)
  395. {
  396. char item_buf[20];
  397. const char *slash;
  398. int count, repeatat;
  399. size_t str_len;
  400. count = 0;
  401. for (slash = schedule_str; *slash; slash++)
  402. if (*slash == '/')
  403. count++;
  404. schedule_length = (count == 0) ? 4 : count + 1;
  405. schedule = xmalloc(sizeof(*schedule) * schedule_length);
  406. if (count == 0) {
  407. schedule[0].type = sched_signal;
  408. schedule[0].value = signal_nr;
  409. parse_schedule_item(schedule_str, &schedule[1]);
  410. if (schedule[1].type != sched_timeout) {
  411. badusage ("--retry takes timeout, or schedule list"
  412. " of at least two items");
  413. }
  414. schedule[2].type = sched_signal;
  415. schedule[2].value = SIGKILL;
  416. schedule[3] = schedule[1];
  417. } else {
  418. count = 0;
  419. repeatat = -1;
  420. while (schedule_str != NULL) {
  421. slash = strchr(schedule_str, '/');
  422. str_len = slash ? (size_t)(slash - schedule_str) : strlen(schedule_str);
  423. if (str_len >= sizeof(item_buf))
  424. badusage("invalid schedule item: far too long"
  425. " (you must delimit items with slashes)");
  426. memcpy(item_buf, schedule_str, str_len);
  427. item_buf[str_len] = 0;
  428. schedule_str = slash ? slash + 1 : NULL;
  429. parse_schedule_item(item_buf, &schedule[count]);
  430. if (schedule[count].type == sched_forever) {
  431. if (repeatat >= 0)
  432. badusage("invalid schedule: `forever'"
  433. " appears more than once");
  434. repeatat = count;
  435. continue;
  436. }
  437. count++;
  438. }
  439. if (repeatat >= 0) {
  440. schedule[count].type = sched_goto;
  441. schedule[count].value = repeatat;
  442. count++;
  443. }
  444. assert(count == schedule_length);
  445. }
  446. }
  447. static void
  448. parse_options(int argc, char * const *argv)
  449. {
  450. static struct option longopts[] = {
  451. { "help", 0, NULL, 'H'},
  452. { "stop", 0, NULL, 'K'},
  453. { "start", 0, NULL, 'S'},
  454. { "version", 0, NULL, 'V'},
  455. { "startas", 1, NULL, 'a'},
  456. { "name", 1, NULL, 'n'},
  457. { "oknodo", 0, NULL, 'o'},
  458. { "pidfile", 1, NULL, 'p'},
  459. { "quiet", 0, NULL, 'q'},
  460. { "signal", 1, NULL, 's'},
  461. { "test", 0, NULL, 't'},
  462. { "user", 1, NULL, 'u'},
  463. { "group", 1, NULL, 'g'},
  464. { "chroot", 1, NULL, 'r'},
  465. { "verbose", 0, NULL, 'v'},
  466. { "exec", 1, NULL, 'x'},
  467. { "chuid", 1, NULL, 'c'},
  468. { "nicelevel", 1, NULL, 'N'},
  469. { "umask", 1, NULL, 'k'},
  470. { "background", 0, NULL, 'b'},
  471. { "make-pidfile", 0, NULL, 'm'},
  472. { "retry", 1, NULL, 'R'},
  473. { "chdir", 1, NULL, 'd'},
  474. { NULL, 0, NULL, 0 }
  475. };
  476. const char *umask_str = NULL;
  477. const char *signal_str = NULL;
  478. const char *schedule_str = NULL;
  479. int c;
  480. for (;;) {
  481. c = getopt_long(argc, argv,
  482. "HKSVa:n:op:qr:s:tu:vx:c:N:k:bmR:g:d:",
  483. longopts, NULL);
  484. if (c == -1)
  485. break;
  486. switch (c) {
  487. case 'H': /* --help */
  488. do_help();
  489. exit(0);
  490. case 'K': /* --stop */
  491. stop = 1;
  492. break;
  493. case 'S': /* --start */
  494. start = 1;
  495. break;
  496. case 'V': /* --version */
  497. do_version();
  498. exit(0);
  499. case 'a': /* --startas <pathname> */
  500. startas = optarg;
  501. break;
  502. case 'n': /* --name <process-name> */
  503. cmdname = optarg;
  504. break;
  505. case 'o': /* --oknodo */
  506. exitnodo = 0;
  507. break;
  508. case 'p': /* --pidfile <pid-file> */
  509. pidfile = optarg;
  510. break;
  511. case 'q': /* --quiet */
  512. quietmode = 1;
  513. break;
  514. case 's': /* --signal <signal> */
  515. signal_str = optarg;
  516. break;
  517. case 't': /* --test */
  518. testmode = 1;
  519. break;
  520. case 'u': /* --user <username>|<uid> */
  521. userspec = optarg;
  522. break;
  523. case 'v': /* --verbose */
  524. quietmode = -1;
  525. break;
  526. case 'x': /* --exec <executable> */
  527. execname = optarg;
  528. break;
  529. case 'c': /* --chuid <username>|<uid> */
  530. /* we copy the string just in case we need the
  531. * argument later. */
  532. changeuser = strdup(optarg);
  533. changeuser = strtok(changeuser, ":");
  534. changegroup = strtok(NULL, ":");
  535. break;
  536. case 'g': /* --group <group>|<gid> */
  537. changegroup = optarg;
  538. break;
  539. case 'r': /* --chroot /new/root */
  540. changeroot = optarg;
  541. break;
  542. case 'N': /* --nice */
  543. nicelevel = atoi(optarg);
  544. break;
  545. case 'k': /* --umask <mask> */
  546. umask_str = optarg;
  547. break;
  548. case 'b': /* --background */
  549. background = 1;
  550. break;
  551. case 'm': /* --make-pidfile */
  552. mpidfile = 1;
  553. break;
  554. case 'R': /* --retry <schedule>|<timeout> */
  555. schedule_str = optarg;
  556. break;
  557. case 'd': /* --chdir /new/dir */
  558. changedir = optarg;
  559. break;
  560. default:
  561. badusage(NULL); /* message printed by getopt */
  562. }
  563. }
  564. if (signal_str != NULL) {
  565. if (parse_signal(signal_str, &signal_nr) != 0)
  566. badusage("signal value must be numeric or name"
  567. " of signal (KILL, INT, ...)");
  568. }
  569. if (schedule_str != NULL) {
  570. parse_schedule(schedule_str);
  571. }
  572. if (umask_str != NULL) {
  573. if (parse_umask(umask_str, &umask_value) != 0)
  574. badusage("umask value must be a positive number");
  575. }
  576. if (start == stop)
  577. badusage("need one of --start or --stop");
  578. if (!execname && !pidfile && !userspec && !cmdname)
  579. badusage("need at least one of --exec, --pidfile, --user or --name");
  580. if (!startas)
  581. startas = execname;
  582. if (start && !startas)
  583. badusage("--start needs --exec or --startas");
  584. if (mpidfile && pidfile == NULL)
  585. badusage("--make-pidfile requires --pidfile");
  586. if (background && !start)
  587. badusage("--background is only relevant with --start");
  588. }
  589. #if defined(OSLinux)
  590. static int
  591. pid_is_exec(pid_t pid, const struct stat *esb)
  592. {
  593. char lname[32];
  594. char lcontents[_POSIX_PATH_MAX];
  595. const char deleted[] = " (deleted)";
  596. int nread;
  597. struct stat sb;
  598. sprintf(lname, "/proc/%d/exe", pid);
  599. nread = readlink(lname, lcontents, sizeof(lcontents));
  600. if (nread == -1)
  601. return 0;
  602. lcontents[nread] = '\0';
  603. if (strcmp(lcontents + nread - strlen(deleted), deleted) == 0)
  604. lcontents[nread - strlen(deleted)] = '\0';
  605. if (stat(lcontents, &sb) != 0)
  606. return 0;
  607. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  608. }
  609. static int
  610. pid_is_user(pid_t pid, uid_t uid)
  611. {
  612. struct stat sb;
  613. char buf[32];
  614. sprintf(buf, "/proc/%d", pid);
  615. if (stat(buf, &sb) != 0)
  616. return 0;
  617. return (sb.st_uid == uid);
  618. }
  619. static int
  620. pid_is_cmd(pid_t pid, const char *name)
  621. {
  622. char buf[32];
  623. FILE *f;
  624. int c;
  625. sprintf(buf, "/proc/%d/stat", pid);
  626. f = fopen(buf, "r");
  627. if (!f)
  628. return 0;
  629. while ((c = getc(f)) != EOF && c != '(')
  630. ;
  631. if (c != '(') {
  632. fclose(f);
  633. return 0;
  634. }
  635. /* this hopefully handles command names containing ')' */
  636. while ((c = getc(f)) != EOF && c == *name)
  637. name++;
  638. fclose(f);
  639. return (c == ')' && *name == '\0');
  640. }
  641. #endif /* OSLinux */
  642. #if defined(OSHURD)
  643. static void
  644. init_procset(void)
  645. {
  646. struct ps_context *context;
  647. error_t err;
  648. err = ps_context_create(getproc(), &context);
  649. if (err)
  650. error(1, err, "ps_context_create");
  651. err = proc_stat_list_create(context, &procset);
  652. if (err)
  653. error(1, err, "proc_stat_list_create");
  654. err = proc_stat_list_add_all(procset, 0, 0);
  655. if (err)
  656. error(1, err, "proc_stat_list_add_all");
  657. }
  658. static struct proc_stat *
  659. get_proc_stat (pid_t pid, ps_flags_t flags)
  660. {
  661. struct proc_stat *ps;
  662. ps_flags_t wanted_flags = PSTAT_PID | flags;
  663. if (!procset)
  664. init_procset();
  665. ps = proc_stat_list_pid_proc_stat(procset, pid);
  666. if (!ps)
  667. return NULL;
  668. if (proc_stat_set_flags(ps, wanted_flags))
  669. return NULL;
  670. if ((proc_stat_flags(ps) & wanted_flags) != wanted_flags)
  671. return NULL;
  672. return ps;
  673. }
  674. static int
  675. pid_is_user(pid_t pid, uid_t uid)
  676. {
  677. struct proc_stat *ps;
  678. ps = get_proc_stat(pid, PSTAT_OWNER_UID);
  679. return ps && proc_stat_owner_uid(ps) == uid;
  680. }
  681. static int
  682. pid_is_cmd(pid_t pid, const char *name)
  683. {
  684. struct proc_stat *ps;
  685. ps = get_proc_stat(pid, PSTAT_ARGS);
  686. return ps && !strcmp(proc_stat_args(ps), name);
  687. }
  688. static int
  689. pid_is_running(pid_t pid)
  690. {
  691. return get_proc_stat(pid, 0) != NULL;
  692. }
  693. #else /* !OSHURD */
  694. static int
  695. pid_is_running(pid_t pid)
  696. {
  697. struct stat sb;
  698. char buf[32];
  699. sprintf(buf, "/proc/%d", pid);
  700. if (stat(buf, &sb) != 0) {
  701. if (errno != ENOENT)
  702. fatal("Error stating %s: %s", buf, strerror(errno));
  703. return 0;
  704. }
  705. return 1;
  706. }
  707. #endif /* OSHURD */
  708. static void
  709. check(pid_t pid)
  710. {
  711. #if defined(OSLinux) || defined(OShpux)
  712. if (execname && !pid_is_exec(pid, &exec_stat))
  713. #elif defined(OSHURD) || defined(OSFreeBSD) || defined(OSNetBSD)
  714. /* Let's try this to see if it works */
  715. if (execname && !pid_is_cmd(pid, execname))
  716. #endif
  717. return;
  718. if (userspec && !pid_is_user(pid, user_id))
  719. return;
  720. if (cmdname && !pid_is_cmd(pid, cmdname))
  721. return;
  722. if (start && !pid_is_running(pid))
  723. return;
  724. push(&found, pid);
  725. }
  726. static void
  727. do_pidfile(const char *name)
  728. {
  729. FILE *f;
  730. static pid_t pid = 0;
  731. if (pid) {
  732. check(pid);
  733. return;
  734. }
  735. f = fopen(name, "r");
  736. if (f) {
  737. if (fscanf(f, "%d", &pid) == 1)
  738. check(pid);
  739. fclose(f);
  740. } else if (errno != ENOENT)
  741. fatal("open pidfile %s: %s", name, strerror(errno));
  742. }
  743. #if defined(OSLinux) || defined (OSsunos)
  744. static void
  745. do_procinit(void)
  746. {
  747. DIR *procdir;
  748. struct dirent *entry;
  749. int foundany;
  750. pid_t pid;
  751. procdir = opendir("/proc");
  752. if (!procdir)
  753. fatal("opendir /proc: %s", strerror(errno));
  754. foundany = 0;
  755. while ((entry = readdir(procdir)) != NULL) {
  756. if (sscanf(entry->d_name, "%d", &pid) != 1)
  757. continue;
  758. foundany++;
  759. check(pid);
  760. }
  761. closedir(procdir);
  762. if (!foundany)
  763. fatal("nothing in /proc - not mounted?");
  764. }
  765. #endif /* OSLinux */
  766. #if defined(OSHURD)
  767. static int
  768. check_proc_stat(struct proc_stat *ps)
  769. {
  770. check(ps->pid);
  771. return 0;
  772. }
  773. static void
  774. do_procinit(void)
  775. {
  776. if (!procset)
  777. init_procset();
  778. proc_stat_list_for_each(procset, check_proc_stat);
  779. }
  780. #endif /* OSHURD */
  781. #ifdef HAVE_KVM_H
  782. static int
  783. pid_is_cmd(pid_t pid, const char *name)
  784. {
  785. kvm_t *kd;
  786. int nentries, argv_len = 0;
  787. struct kinfo_proc *kp;
  788. char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
  789. char **pid_argv_p;
  790. char *start_argv_0_p, *end_argv_0_p;
  791. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  792. if (kd == NULL)
  793. errx(1, "%s", errbuf);
  794. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  795. if (kp == NULL)
  796. errx(1, "%s", kvm_geterr(kd));
  797. pid_argv_p = kvm_getargv(kd, kp, argv_len);
  798. if (pid_argv_p == NULL)
  799. errx(1, "%s", kvm_geterr(kd));
  800. start_argv_0_p = *pid_argv_p;
  801. /* Find and compare string */
  802. /* Find end of argv[0] then copy and cut of str there. */
  803. end_argv_0_p = strchr(*pid_argv_p, ' ');
  804. if (end_argv_0_p == NULL)
  805. /* There seems to be no space, so we have the command
  806. * allready in its desired form. */
  807. start_argv_0_p = *pid_argv_p;
  808. else {
  809. /* Tests indicate that this never happens, since
  810. * kvm_getargv itself cuts of tailing stuff. This is
  811. * not what the manpage says, however. */
  812. strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
  813. buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
  814. start_argv_0_p = buf;
  815. }
  816. if (strlen(name) != strlen(start_argv_0_p))
  817. return 0;
  818. return (strcmp(name, start_argv_0_p) == 0) ? 1 : 0;
  819. }
  820. static int
  821. pid_is_user(pid_t pid, uid_t uid)
  822. {
  823. kvm_t *kd;
  824. int nentries; /* Value not used */
  825. uid_t proc_uid;
  826. struct kinfo_proc *kp;
  827. char errbuf[_POSIX2_LINE_MAX];
  828. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  829. if (kd == NULL)
  830. errx(1, "%s", errbuf);
  831. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  832. if (kp == NULL)
  833. errx(1, "%s", kvm_geterr(kd));
  834. if (kp->kp_proc.p_cred)
  835. kvm_read(kd, (u_long)&(kp->kp_proc.p_cred->p_ruid),
  836. &proc_uid, sizeof(uid_t));
  837. else
  838. return 0;
  839. return (proc_uid == (uid_t)uid);
  840. }
  841. static int
  842. pid_is_exec(pid_t pid, const char *name)
  843. {
  844. kvm_t *kd;
  845. int nentries;
  846. struct kinfo_proc *kp;
  847. char errbuf[_POSIX2_LINE_MAX], *pidexec;
  848. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  849. if (kd == NULL)
  850. errx(1, "%s", errbuf);
  851. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  852. if (kp == NULL)
  853. errx(1, "%s", kvm_geterr(kd));
  854. pidexec = (&kp->kp_proc)->p_comm;
  855. if (strlen(name) != strlen(pidexec))
  856. return 0;
  857. return (strcmp(name, pidexec) == 0) ? 1 : 0;
  858. }
  859. static void
  860. do_procinit(void)
  861. {
  862. /* Nothing to do */
  863. }
  864. #endif /* OSOpenBSD */
  865. #if defined(OShpux)
  866. static int
  867. pid_is_user(pid_t pid, uid_t uid)
  868. {
  869. struct pst_status pst;
  870. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  871. return 0;
  872. return ((uid_t)pst.pst_uid == uid);
  873. }
  874. static int
  875. pid_is_cmd(pid_t pid, const char *name)
  876. {
  877. struct pst_status pst;
  878. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  879. return 0;
  880. return (strcmp(pst.pst_ucomm, name) == 0);
  881. }
  882. static int
  883. pid_is_exec(pid_t pid, const struct stat *esb)
  884. {
  885. struct pst_status pst;
  886. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  887. return 0;
  888. return ((dev_t)pst.pst_text.psf_fsid.psfs_id == esb->st_dev
  889. && (ino_t)pst.pst_text.psf_fileid == esb->st_ino);
  890. }
  891. static void
  892. do_procinit(void)
  893. {
  894. struct pst_status pst[10];
  895. int i, count;
  896. int idx = 0;
  897. while ((count = pstat_getproc(pst, sizeof(pst[0]), 10, idx)) > 0) {
  898. for (i = 0; i < count; i++)
  899. check(pst[i].pst_pid);
  900. idx = pst[count - 1].pst_idx + 1;
  901. }
  902. }
  903. #endif /* OShpux */
  904. static void
  905. do_findprocs(void)
  906. {
  907. clear(&found);
  908. if (pidfile)
  909. do_pidfile(pidfile);
  910. else
  911. do_procinit();
  912. }
  913. /* return 1 on failure */
  914. static void
  915. do_stop(int signal_nr, int quietmode, int *n_killed, int *n_notkilled, int retry_nr)
  916. {
  917. struct pid_list *p;
  918. do_findprocs();
  919. *n_killed = 0;
  920. *n_notkilled = 0;
  921. if (!found)
  922. return;
  923. clear(&killed);
  924. for (p = found; p; p = p->next) {
  925. if (testmode) {
  926. if (quietmode <= 0)
  927. printf("Would send signal %d to %d.\n",
  928. signal_nr, p->pid);
  929. (*n_killed)++;
  930. } else if (kill(p->pid, signal_nr) == 0) {
  931. push(&killed, p->pid);
  932. (*n_killed)++;
  933. } else {
  934. if (signal_nr)
  935. printf("%s: warning: failed to kill %d: %s\n",
  936. progname, p->pid, strerror(errno));
  937. (*n_notkilled)++;
  938. }
  939. }
  940. if (quietmode < 0 && killed) {
  941. printf("Stopped %s (pid", what_stop);
  942. for (p = killed; p; p = p->next)
  943. printf(" %d", p->pid);
  944. putchar(')');
  945. if (retry_nr > 0)
  946. printf(", retry #%d", retry_nr);
  947. printf(".\n");
  948. }
  949. }
  950. static void
  951. set_what_stop(const char *str)
  952. {
  953. strncpy(what_stop, str, sizeof(what_stop));
  954. what_stop[sizeof(what_stop) - 1] = '\0';
  955. }
  956. static int
  957. run_stop_schedule(void)
  958. {
  959. int r, position, n_killed, n_notkilled, value, ratio, anykilled, retry_nr;
  960. struct timeval stopat, before, after, interval, maxinterval;
  961. if (testmode) {
  962. if (schedule != NULL) {
  963. if (quietmode <= 0)
  964. printf("Ignoring --retry in test mode\n");
  965. schedule = NULL;
  966. }
  967. }
  968. if (cmdname)
  969. set_what_stop(cmdname);
  970. else if (execname)
  971. set_what_stop(execname);
  972. else if (pidfile)
  973. sprintf(what_stop, "process in pidfile `%.200s'", pidfile);
  974. else if (userspec)
  975. sprintf(what_stop, "process(es) owned by `%.200s'", userspec);
  976. else
  977. fatal("internal error, please report");
  978. anykilled = 0;
  979. retry_nr = 0;
  980. if (schedule == NULL) {
  981. do_stop(signal_nr, quietmode, &n_killed, &n_notkilled, 0);
  982. if (n_notkilled > 0 && quietmode <= 0)
  983. printf("%d pids were not killed\n", n_notkilled);
  984. if (n_killed)
  985. anykilled = 1;
  986. goto x_finished;
  987. }
  988. for (position = 0; position < schedule_length; ) {
  989. value= schedule[position].value;
  990. n_notkilled = 0;
  991. switch (schedule[position].type) {
  992. case sched_goto:
  993. position = value;
  994. continue;
  995. case sched_signal:
  996. do_stop(value, quietmode, &n_killed, &n_notkilled, retry_nr++);
  997. if (!n_killed)
  998. goto x_finished;
  999. else
  1000. anykilled = 1;
  1001. goto next_item;
  1002. case sched_timeout:
  1003. /* We want to keep polling for the processes, to see if they've exited,
  1004. * or until the timeout expires.
  1005. *
  1006. * This is a somewhat complicated algorithm to try to ensure that we
  1007. * notice reasonably quickly when all the processes have exited, but
  1008. * don't spend too much CPU time polling. In particular, on a fast
  1009. * machine with quick-exiting daemons we don't want to delay system
  1010. * shutdown too much, whereas on a slow one, or where processes are
  1011. * taking some time to exit, we want to increase the polling
  1012. * interval.
  1013. *
  1014. * The algorithm is as follows: we measure the elapsed time it takes
  1015. * to do one poll(), and wait a multiple of this time for the next
  1016. * poll. However, if that would put us past the end of the timeout
  1017. * period we wait only as long as the timeout period, but in any case
  1018. * we always wait at least MIN_POLL_INTERVAL (20ms). The multiple
  1019. * (`ratio') starts out as 2, and increases by 1 for each poll to a
  1020. * maximum of 10; so we use up to between 30% and 10% of the
  1021. * machine's resources (assuming a few reasonable things about system
  1022. * performance).
  1023. */
  1024. xgettimeofday(&stopat);
  1025. stopat.tv_sec += value;
  1026. ratio = 1;
  1027. for (;;) {
  1028. xgettimeofday(&before);
  1029. if (timercmp(&before, &stopat, >))
  1030. goto next_item;
  1031. do_stop(0, 1, &n_killed, &n_notkilled, 0);
  1032. if (!n_killed)
  1033. goto x_finished;
  1034. xgettimeofday(&after);
  1035. if (!timercmp(&after, &stopat, <))
  1036. goto next_item;
  1037. if (ratio < 10)
  1038. ratio++;
  1039. timersub(&stopat, &after, &maxinterval);
  1040. timersub(&after, &before, &interval);
  1041. tmul(&interval, ratio);
  1042. if (interval.tv_sec < 0 || interval.tv_usec < 0)
  1043. interval.tv_sec = interval.tv_usec = 0;
  1044. if (timercmp(&interval, &maxinterval, >))
  1045. interval = maxinterval;
  1046. if (interval.tv_sec == 0 &&
  1047. interval.tv_usec <= MIN_POLL_INTERVAL)
  1048. interval.tv_usec = MIN_POLL_INTERVAL;
  1049. r = select(0, NULL, NULL, NULL, &interval);
  1050. if (r < 0 && errno != EINTR)
  1051. fatal("select() failed for pause: %s",
  1052. strerror(errno));
  1053. }
  1054. default:
  1055. assert(!"schedule[].type value must be valid");
  1056. }
  1057. next_item:
  1058. position++;
  1059. }
  1060. if (quietmode <= 0)
  1061. printf("Program %s, %d process(es), refused to die.\n",
  1062. what_stop, n_killed);
  1063. return 2;
  1064. x_finished:
  1065. if (!anykilled) {
  1066. if (quietmode <= 0)
  1067. printf("No %s found running; none killed.\n", what_stop);
  1068. return exitnodo;
  1069. } else {
  1070. return 0;
  1071. }
  1072. }
  1073. int
  1074. main(int argc, char **argv)
  1075. {
  1076. int devnull_fd = -1;
  1077. gid_t rgid;
  1078. uid_t ruid;
  1079. #ifdef HAVE_TIOCNOTTY
  1080. int tty_fd = -1;
  1081. #endif
  1082. progname = argv[0];
  1083. parse_options(argc, argv);
  1084. argc -= optind;
  1085. argv += optind;
  1086. if (execname) {
  1087. char *fullexecname;
  1088. if (changeroot) {
  1089. int fullexecname_len = strlen(changeroot) + 1 +
  1090. strlen(execname) + 1;
  1091. fullexecname = xmalloc(fullexecname_len);
  1092. snprintf(fullexecname, fullexecname_len, "%s/%s",
  1093. changeroot, execname);
  1094. } else
  1095. fullexecname = execname;
  1096. if (stat(fullexecname, &exec_stat))
  1097. fatal("stat %s: %s", fullexecname, strerror(errno));
  1098. if (fullexecname != execname)
  1099. free(fullexecname);
  1100. }
  1101. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  1102. struct passwd *pw;
  1103. pw = getpwnam(userspec);
  1104. if (!pw)
  1105. fatal("user `%s' not found\n", userspec);
  1106. user_id = pw->pw_uid;
  1107. }
  1108. if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) {
  1109. struct group *gr = getgrnam(changegroup);
  1110. if (!gr)
  1111. fatal("group `%s' not found\n", changegroup);
  1112. runas_gid = gr->gr_gid;
  1113. }
  1114. if (changeuser && sscanf(changeuser, "%d", &runas_uid) != 1) {
  1115. struct passwd *pw = getpwnam(changeuser);
  1116. if (!pw)
  1117. fatal("user `%s' not found\n", changeuser);
  1118. runas_uid = pw->pw_uid;
  1119. if (changegroup == NULL) {
  1120. /* Pass the default group of this user */
  1121. changegroup = ""; /* Just empty */
  1122. runas_gid = pw->pw_gid;
  1123. }
  1124. if (access(pw->pw_dir, F_OK) == 0)
  1125. setenv("HOME", pw->pw_dir, 1);
  1126. }
  1127. if (stop) {
  1128. int i = run_stop_schedule();
  1129. exit(i);
  1130. }
  1131. do_findprocs();
  1132. if (found) {
  1133. if (quietmode <= 0)
  1134. printf("%s already running.\n", execname ? execname : "process");
  1135. exit(exitnodo);
  1136. }
  1137. if (testmode && quietmode <= 0) {
  1138. printf("Would start %s ", startas);
  1139. while (argc-- > 0)
  1140. printf("%s ", *argv++);
  1141. if (changeuser != NULL) {
  1142. printf(" (as user %s[%d]", changeuser, runas_uid);
  1143. if (changegroup != NULL)
  1144. printf(", and group %s[%d])", changegroup, runas_gid);
  1145. else
  1146. printf(")");
  1147. }
  1148. if (changeroot != NULL)
  1149. printf(" in directory %s", changeroot);
  1150. if (nicelevel)
  1151. printf(", and add %i to the priority", nicelevel);
  1152. printf(".\n");
  1153. }
  1154. if (testmode)
  1155. exit(0);
  1156. if (quietmode < 0)
  1157. printf("Starting %s...\n", startas);
  1158. *--argv = startas;
  1159. if (background) { /* ok, we need to detach this process */
  1160. daemonize();
  1161. #ifdef HAVE_TIOCNOTTY
  1162. tty_fd=open("/dev/tty", O_RDWR);
  1163. #endif
  1164. devnull_fd=open("/dev/null", O_RDWR);
  1165. }
  1166. if (nicelevel) {
  1167. errno = 0;
  1168. if ((nice(nicelevel) == -1) && (errno != 0))
  1169. fatal("Unable to alter nice level by %i: %s",
  1170. nicelevel, strerror(errno));
  1171. }
  1172. if (umask_value >= 0)
  1173. umask(umask_value);
  1174. if (mpidfile && pidfile != NULL) {
  1175. /* User wants _us_ to make the pidfile :) */
  1176. FILE *pidf = fopen(pidfile, "w");
  1177. pid_t pidt = getpid();
  1178. if (pidf == NULL)
  1179. fatal("Unable to open pidfile `%s' for writing: %s",
  1180. pidfile, strerror(errno));
  1181. fprintf(pidf, "%d\n", pidt);
  1182. fclose(pidf);
  1183. }
  1184. if (changeroot != NULL) {
  1185. if (chdir(changeroot) < 0)
  1186. fatal("Unable to chdir() to %s", changeroot);
  1187. if (chroot(changeroot) < 0)
  1188. fatal("Unable to chroot() to %s", changeroot);
  1189. }
  1190. if (chdir(changedir) < 0)
  1191. fatal("Unable to chdir() to %s", changedir);
  1192. rgid = getgid();
  1193. ruid = getuid();
  1194. if (changegroup != NULL) {
  1195. if (rgid != (gid_t)runas_gid)
  1196. if (setgid(runas_gid))
  1197. fatal("Unable to set gid to %d", runas_gid);
  1198. }
  1199. if (changeuser != NULL) {
  1200. /* We assume that if our real user and group are the same as
  1201. * the ones we should switch to, the supplementary groups
  1202. * will be already in place. */
  1203. if (rgid != (gid_t)runas_gid || ruid != (uid_t)runas_uid)
  1204. if (initgroups(changeuser, runas_gid))
  1205. fatal("Unable to set initgroups() with gid %d",
  1206. runas_gid);
  1207. if (ruid != (uid_t)runas_uid)
  1208. if (setuid(runas_uid))
  1209. fatal("Unable to set uid to %s", changeuser);
  1210. }
  1211. if (background) {
  1212. /* Continue background setup */
  1213. int i;
  1214. #ifdef HAVE_TIOCNOTTY
  1215. /* Change tty */
  1216. ioctl(tty_fd, TIOCNOTTY, 0);
  1217. close(tty_fd);
  1218. #endif
  1219. if (umask_value < 0)
  1220. umask(022); /* Set a default for dumb programs */
  1221. dup2(devnull_fd, 0); /* stdin */
  1222. dup2(devnull_fd, 1); /* stdout */
  1223. dup2(devnull_fd, 2); /* stderr */
  1224. /* Now close all extra fds */
  1225. for (i = get_open_fd_max() - 1; i >= 3; --i)
  1226. close(i);
  1227. }
  1228. execv(startas, argv);
  1229. fatal("Unable to start %s: %s", startas, strerror(errno));
  1230. }