start-stop-daemon.c 32 KB

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