start-stop-daemon.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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 /*us*/
  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(OSLinux)
  580. static int
  581. pid_is_exec(pid_t pid, const struct stat *esb)
  582. {
  583. char lname[32];
  584. char lcontents[_POSIX_PATH_MAX];
  585. const char deleted[] = " (deleted)";
  586. int nread;
  587. struct stat sb;
  588. sprintf(lname, "/proc/%d/exe", pid);
  589. nread = readlink(lname, lcontents, sizeof(lcontents));
  590. if (nread == -1)
  591. return 0;
  592. lcontents[nread] = '\0';
  593. if (strcmp(lcontents + nread - strlen(deleted), deleted) == 0)
  594. lcontents[nread - strlen(deleted)] = '\0';
  595. if (stat(lcontents, &sb) != 0)
  596. return 0;
  597. return (sb.st_dev == esb->st_dev && sb.st_ino == esb->st_ino);
  598. }
  599. static int
  600. pid_is_user(pid_t pid, uid_t uid)
  601. {
  602. struct stat sb;
  603. char buf[32];
  604. sprintf(buf, "/proc/%d", pid);
  605. if (stat(buf, &sb) != 0)
  606. return 0;
  607. return (sb.st_uid == uid);
  608. }
  609. static int
  610. pid_is_cmd(pid_t pid, const char *name)
  611. {
  612. char buf[32];
  613. FILE *f;
  614. int c;
  615. sprintf(buf, "/proc/%d/stat", pid);
  616. f = fopen(buf, "r");
  617. if (!f)
  618. return 0;
  619. while ((c = getc(f)) != EOF && c != '(')
  620. ;
  621. if (c != '(') {
  622. fclose(f);
  623. return 0;
  624. }
  625. /* this hopefully handles command names containing ')' */
  626. while ((c = getc(f)) != EOF && c == *name)
  627. name++;
  628. fclose(f);
  629. return (c == ')' && *name == '\0');
  630. }
  631. #endif /* OSLinux */
  632. #if defined(OSHURD)
  633. static void
  634. init_procset(void)
  635. {
  636. struct ps_context *context;
  637. error_t err;
  638. err = ps_context_create(getproc(), &context);
  639. if (err)
  640. error(1, err, "ps_context_create");
  641. err = proc_stat_list_create(context, &procset);
  642. if (err)
  643. error(1, err, "proc_stat_list_create");
  644. err = proc_stat_list_add_all(procset, 0, 0);
  645. if (err)
  646. error(1, err, "proc_stat_list_add_all");
  647. }
  648. static struct proc_stat *
  649. get_proc_stat (pid_t pid, ps_flags_t flags)
  650. {
  651. struct proc_stat *ps;
  652. ps_flags_t wanted_flags = PSTAT_PID | flags;
  653. if (!procset)
  654. init_procset();
  655. ps = proc_stat_list_pid_proc_stat(procset, pid);
  656. if (!ps)
  657. return NULL;
  658. if (proc_stat_set_flags(ps, wanted_flags))
  659. return NULL;
  660. if ((proc_stat_flags(ps) & wanted_flags) != wanted_flags)
  661. return NULL;
  662. return ps;
  663. }
  664. static int
  665. pid_is_user(pid_t pid, uid_t uid)
  666. {
  667. struct proc_stat *ps;
  668. ps = get_proc_stat(pid, PSTAT_OWNER_UID);
  669. return ps && proc_stat_owner_uid(ps) == uid;
  670. }
  671. static int
  672. pid_is_cmd(pid_t pid, const char *name)
  673. {
  674. struct proc_stat *ps;
  675. ps = get_proc_stat(pid, PSTAT_ARGS);
  676. return ps && !strcmp(proc_stat_args(ps), name);
  677. }
  678. static int
  679. pid_is_running(pid_t pid)
  680. {
  681. return get_proc_stat(pid, 0) != NULL;
  682. }
  683. #else /* !OSHURD */
  684. static int
  685. pid_is_running(pid_t pid)
  686. {
  687. struct stat sb;
  688. char buf[32];
  689. sprintf(buf, "/proc/%d", pid);
  690. if (stat(buf, &sb) != 0) {
  691. if (errno != ENOENT)
  692. fatal("Error stating %s: %s", buf, strerror(errno));
  693. return 0;
  694. }
  695. return 1;
  696. }
  697. #endif /* OSHURD */
  698. static void
  699. check(pid_t pid)
  700. {
  701. #if defined(OSLinux) || defined(OShpux)
  702. if (execname && !pid_is_exec(pid, &exec_stat))
  703. return;
  704. #elif defined(OSHURD) || defined(OSFreeBSD) || defined(OSNetBSD)
  705. /* Let's try this to see if it works */
  706. if (execname && !pid_is_cmd(pid, execname))
  707. return;
  708. #endif
  709. if (userspec && !pid_is_user(pid, user_id))
  710. return;
  711. if (cmdname && !pid_is_cmd(pid, cmdname))
  712. return;
  713. if (start && !pid_is_running(pid))
  714. return;
  715. push(&found, pid);
  716. }
  717. static void
  718. do_pidfile(const char *name)
  719. {
  720. FILE *f;
  721. static pid_t pid = 0;
  722. if (pid) {
  723. check(pid);
  724. return;
  725. }
  726. f = fopen(name, "r");
  727. if (f) {
  728. if (fscanf(f, "%d", &pid) == 1)
  729. check(pid);
  730. fclose(f);
  731. } else if (errno != ENOENT)
  732. fatal("open pidfile %s: %s", name, strerror(errno));
  733. }
  734. #if defined(OSLinux) || defined (OSsunos)
  735. static void
  736. do_procinit(void)
  737. {
  738. DIR *procdir;
  739. struct dirent *entry;
  740. int foundany;
  741. pid_t pid;
  742. procdir = opendir("/proc");
  743. if (!procdir)
  744. fatal("opendir /proc: %s", strerror(errno));
  745. foundany = 0;
  746. while ((entry = readdir(procdir)) != NULL) {
  747. if (sscanf(entry->d_name, "%d", &pid) != 1)
  748. continue;
  749. foundany++;
  750. check(pid);
  751. }
  752. closedir(procdir);
  753. if (!foundany)
  754. fatal("nothing in /proc - not mounted?");
  755. }
  756. #endif /* OSLinux */
  757. #if defined(OSHURD)
  758. static int
  759. check_proc_stat(struct proc_stat *ps)
  760. {
  761. check(ps->pid);
  762. return 0;
  763. }
  764. static void
  765. do_procinit(void)
  766. {
  767. if (!procset)
  768. init_procset();
  769. proc_stat_list_for_each(procset, check_proc_stat);
  770. }
  771. #endif /* OSHURD */
  772. #ifdef HAVE_KVM_H
  773. static int
  774. pid_is_cmd(pid_t pid, const char *name)
  775. {
  776. kvm_t *kd;
  777. int nentries, argv_len = 0;
  778. struct kinfo_proc *kp;
  779. char errbuf[_POSIX2_LINE_MAX], buf[_POSIX2_LINE_MAX];
  780. char **pid_argv_p;
  781. char *start_argv_0_p, *end_argv_0_p;
  782. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  783. if (kd == NULL)
  784. errx(1, "%s", errbuf);
  785. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  786. if (kp == NULL)
  787. errx(1, "%s", kvm_geterr(kd));
  788. pid_argv_p = kvm_getargv(kd, kp, argv_len);
  789. if (pid_argv_p == NULL)
  790. errx(1, "%s", kvm_geterr(kd));
  791. start_argv_0_p = *pid_argv_p;
  792. /* Find and compare string */
  793. /* Find end of argv[0] then copy and cut of str there. */
  794. end_argv_0_p = strchr(*pid_argv_p, ' ');
  795. if (end_argv_0_p == NULL)
  796. /* There seems to be no space, so we have the command
  797. * allready in its desired form. */
  798. start_argv_0_p = *pid_argv_p;
  799. else {
  800. /* Tests indicate that this never happens, since
  801. * kvm_getargv itself cuts of tailing stuff. This is
  802. * not what the manpage says, however. */
  803. strncpy(buf, *pid_argv_p, (end_argv_0_p - start_argv_0_p));
  804. buf[(end_argv_0_p - start_argv_0_p) + 1] = '\0';
  805. start_argv_0_p = buf;
  806. }
  807. if (strlen(name) != strlen(start_argv_0_p))
  808. return 0;
  809. return (strcmp(name, start_argv_0_p) == 0) ? 1 : 0;
  810. }
  811. static int
  812. pid_is_user(pid_t pid, uid_t uid)
  813. {
  814. kvm_t *kd;
  815. int nentries; /* Value not used */
  816. uid_t proc_uid;
  817. struct kinfo_proc *kp;
  818. char errbuf[_POSIX2_LINE_MAX];
  819. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  820. if (kd == NULL)
  821. errx(1, "%s", errbuf);
  822. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  823. if (kp == NULL)
  824. errx(1, "%s", kvm_geterr(kd));
  825. if (kp->kp_proc.p_cred)
  826. kvm_read(kd, (u_long)&(kp->kp_proc.p_cred->p_ruid),
  827. &proc_uid, sizeof(uid_t));
  828. else
  829. return 0;
  830. return (proc_uid == (uid_t)uid);
  831. }
  832. static int
  833. pid_is_exec(pid_t pid, const char *name)
  834. {
  835. kvm_t *kd;
  836. int nentries;
  837. struct kinfo_proc *kp;
  838. char errbuf[_POSIX2_LINE_MAX], *pidexec;
  839. kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
  840. if (kd == NULL)
  841. errx(1, "%s", errbuf);
  842. kp = kvm_getprocs(kd, KERN_PROC_PID, pid, &nentries);
  843. if (kp == NULL)
  844. errx(1, "%s", kvm_geterr(kd));
  845. pidexec = (&kp->kp_proc)->p_comm;
  846. if (strlen(name) != strlen(pidexec))
  847. return 0;
  848. return (strcmp(name, pidexec) == 0) ? 1 : 0;
  849. }
  850. static void
  851. do_procinit(void)
  852. {
  853. /* Nothing to do */
  854. }
  855. #endif /* OSOpenBSD */
  856. #if defined(OShpux)
  857. static int
  858. pid_is_user(pid_t pid, uid_t uid)
  859. {
  860. struct pst_status pst;
  861. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  862. return 0;
  863. return ((uid_t)pst.pst_uid == uid);
  864. }
  865. static int
  866. pid_is_cmd(pid_t pid, const char *name)
  867. {
  868. struct pst_status pst;
  869. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  870. return 0;
  871. return (strcmp(pst.pst_ucomm, name) == 0);
  872. }
  873. static int
  874. pid_is_exec(pid_t pid, const struct stat *esb)
  875. {
  876. struct pst_status pst;
  877. if (pstat_getproc(&pst, sizeof(pst), (size_t)0, (int)pid) < 0)
  878. return 0;
  879. return ((dev_t)pst.pst_text.psf_fsid.psfs_id == esb->st_dev
  880. && (ino_t)pst.pst_text.psf_fileid == esb->st_ino);
  881. }
  882. static void
  883. do_procinit(void)
  884. {
  885. struct pst_status pst[10];
  886. int i, count;
  887. int idx = 0;
  888. while ((count = pstat_getproc(pst, sizeof(pst[0]), 10, idx)) > 0) {
  889. for (i = 0; i < count; i++)
  890. check(pst[i].pst_pid);
  891. idx = pst[count - 1].pst_idx + 1;
  892. }
  893. }
  894. #endif /* OShpux */
  895. static void
  896. do_findprocs(void)
  897. {
  898. clear(&found);
  899. if (pidfile)
  900. do_pidfile(pidfile);
  901. else
  902. do_procinit();
  903. }
  904. /* return 1 on failure */
  905. static void
  906. do_stop(int signal_nr, int quietmode, int *n_killed, int *n_notkilled, int retry_nr)
  907. {
  908. struct pid_list *p;
  909. do_findprocs();
  910. *n_killed = 0;
  911. *n_notkilled = 0;
  912. if (!found)
  913. return;
  914. clear(&killed);
  915. for (p = found; p; p = p->next) {
  916. if (testmode) {
  917. if (quietmode <= 0)
  918. printf("Would send signal %d to %d.\n",
  919. signal_nr, p->pid);
  920. (*n_killed)++;
  921. } else if (kill(p->pid, signal_nr) == 0) {
  922. push(&killed, p->pid);
  923. (*n_killed)++;
  924. } else {
  925. if (signal_nr)
  926. printf("%s: warning: failed to kill %d: %s\n",
  927. progname, p->pid, strerror(errno));
  928. (*n_notkilled)++;
  929. }
  930. }
  931. if (quietmode < 0 && killed) {
  932. printf("Stopped %s (pid", what_stop);
  933. for (p = killed; p; p = p->next)
  934. printf(" %d", p->pid);
  935. putchar(')');
  936. if (retry_nr > 0)
  937. printf(", retry #%d", retry_nr);
  938. printf(".\n");
  939. }
  940. }
  941. static void
  942. set_what_stop(const char *str)
  943. {
  944. strncpy(what_stop, str, sizeof(what_stop));
  945. what_stop[sizeof(what_stop) - 1] = '\0';
  946. }
  947. static int
  948. run_stop_schedule(void)
  949. {
  950. int r, position, n_killed, n_notkilled, value, ratio, anykilled, retry_nr;
  951. struct timeval stopat, before, after, interval, maxinterval;
  952. if (testmode) {
  953. if (schedule != NULL) {
  954. if (quietmode <= 0)
  955. printf("Ignoring --retry in test mode\n");
  956. schedule = NULL;
  957. }
  958. }
  959. if (cmdname)
  960. set_what_stop(cmdname);
  961. else if (execname)
  962. set_what_stop(execname);
  963. else if (pidfile)
  964. sprintf(what_stop, "process in pidfile `%.200s'", pidfile);
  965. else if (userspec)
  966. sprintf(what_stop, "process(es) owned by `%.200s'", userspec);
  967. else
  968. fatal("internal error, please report");
  969. anykilled = 0;
  970. retry_nr = 0;
  971. if (schedule == NULL) {
  972. do_stop(signal_nr, quietmode, &n_killed, &n_notkilled, 0);
  973. if (n_notkilled > 0 && quietmode <= 0)
  974. printf("%d pids were not killed\n", n_notkilled);
  975. if (n_killed)
  976. anykilled = 1;
  977. goto x_finished;
  978. }
  979. for (position = 0; position < schedule_length; ) {
  980. value= schedule[position].value;
  981. n_notkilled = 0;
  982. switch (schedule[position].type) {
  983. case sched_goto:
  984. position = value;
  985. continue;
  986. case sched_signal:
  987. do_stop(value, quietmode, &n_killed, &n_notkilled, retry_nr++);
  988. if (!n_killed)
  989. goto x_finished;
  990. else
  991. anykilled = 1;
  992. goto next_item;
  993. case sched_timeout:
  994. /* We want to keep polling for the processes, to see if they've exited,
  995. * or until the timeout expires.
  996. *
  997. * This is a somewhat complicated algorithm to try to ensure that we
  998. * notice reasonably quickly when all the processes have exited, but
  999. * don't spend too much CPU time polling. In particular, on a fast
  1000. * machine with quick-exiting daemons we don't want to delay system
  1001. * shutdown too much, whereas on a slow one, or where processes are
  1002. * taking some time to exit, we want to increase the polling
  1003. * interval.
  1004. *
  1005. * The algorithm is as follows: we measure the elapsed time it takes
  1006. * to do one poll(), and wait a multiple of this time for the next
  1007. * poll. However, if that would put us past the end of the timeout
  1008. * period we wait only as long as the timeout period, but in any case
  1009. * we always wait at least MIN_POLL_INTERVAL (20ms). The multiple
  1010. * (`ratio') starts out as 2, and increases by 1 for each poll to a
  1011. * maximum of 10; so we use up to between 30% and 10% of the
  1012. * machine's resources (assuming a few reasonable things about system
  1013. * performance).
  1014. */
  1015. xgettimeofday(&stopat);
  1016. stopat.tv_sec += value;
  1017. ratio = 1;
  1018. for (;;) {
  1019. xgettimeofday(&before);
  1020. if (timercmp(&before, &stopat, >))
  1021. goto next_item;
  1022. do_stop(0, 1, &n_killed, &n_notkilled, 0);
  1023. if (!n_killed)
  1024. goto x_finished;
  1025. xgettimeofday(&after);
  1026. if (!timercmp(&after, &stopat, <))
  1027. goto next_item;
  1028. if (ratio < 10)
  1029. ratio++;
  1030. timersub(&stopat, &after, &maxinterval);
  1031. timersub(&after, &before, &interval);
  1032. tmul(&interval, ratio);
  1033. if (interval.tv_sec < 0 || interval.tv_usec < 0)
  1034. interval.tv_sec = interval.tv_usec = 0;
  1035. if (timercmp(&interval, &maxinterval, >))
  1036. interval = maxinterval;
  1037. if (interval.tv_sec == 0 &&
  1038. interval.tv_usec <= MIN_POLL_INTERVAL)
  1039. interval.tv_usec = MIN_POLL_INTERVAL;
  1040. r = select(0, NULL, NULL, NULL, &interval);
  1041. if (r < 0 && errno != EINTR)
  1042. fatal("select() failed for pause: %s",
  1043. strerror(errno));
  1044. }
  1045. default:
  1046. assert(!"schedule[].type value must be valid");
  1047. }
  1048. next_item:
  1049. position++;
  1050. }
  1051. if (quietmode <= 0)
  1052. printf("Program %s, %d process(es), refused to die.\n",
  1053. what_stop, n_killed);
  1054. return 2;
  1055. x_finished:
  1056. if (!anykilled) {
  1057. if (quietmode <= 0)
  1058. printf("No %s found running; none killed.\n", what_stop);
  1059. return exitnodo;
  1060. } else {
  1061. return 0;
  1062. }
  1063. }
  1064. int
  1065. main(int argc, char **argv)
  1066. {
  1067. int devnull_fd = -1;
  1068. gid_t rgid;
  1069. uid_t ruid;
  1070. #ifdef HAVE_TIOCNOTTY
  1071. int tty_fd = -1;
  1072. #endif
  1073. progname = argv[0];
  1074. parse_options(argc, argv);
  1075. argc -= optind;
  1076. argv += optind;
  1077. if (execname) {
  1078. char *fullexecname;
  1079. if (changeroot) {
  1080. int fullexecname_len = strlen(changeroot) + 1 +
  1081. strlen(execname) + 1;
  1082. fullexecname = xmalloc(fullexecname_len);
  1083. snprintf(fullexecname, fullexecname_len, "%s/%s",
  1084. changeroot, execname);
  1085. } else
  1086. fullexecname = execname;
  1087. if (stat(fullexecname, &exec_stat))
  1088. fatal("stat %s: %s", fullexecname, strerror(errno));
  1089. if (fullexecname != execname)
  1090. free(fullexecname);
  1091. }
  1092. if (userspec && sscanf(userspec, "%d", &user_id) != 1) {
  1093. struct passwd *pw;
  1094. pw = getpwnam(userspec);
  1095. if (!pw)
  1096. fatal("user `%s' not found\n", userspec);
  1097. user_id = pw->pw_uid;
  1098. }
  1099. if (changegroup && sscanf(changegroup, "%d", &runas_gid) != 1) {
  1100. struct group *gr = getgrnam(changegroup);
  1101. if (!gr)
  1102. fatal("group `%s' not found\n", changegroup);
  1103. runas_gid = gr->gr_gid;
  1104. }
  1105. if (changeuser && sscanf(changeuser, "%d", &runas_uid) != 1) {
  1106. struct passwd *pw = getpwnam(changeuser);
  1107. if (!pw)
  1108. fatal("user `%s' not found\n", changeuser);
  1109. runas_uid = pw->pw_uid;
  1110. if (changegroup == NULL) {
  1111. /* Pass the default group of this user */
  1112. changegroup = ""; /* Just empty */
  1113. runas_gid = pw->pw_gid;
  1114. }
  1115. if (access(pw->pw_dir, F_OK) == 0)
  1116. setenv("HOME", pw->pw_dir, 1);
  1117. }
  1118. if (stop) {
  1119. int i = run_stop_schedule();
  1120. exit(i);
  1121. }
  1122. do_findprocs();
  1123. if (found) {
  1124. if (quietmode <= 0)
  1125. printf("%s already running.\n", execname ? execname : "process");
  1126. exit(exitnodo);
  1127. }
  1128. if (testmode && quietmode <= 0) {
  1129. printf("Would start %s ", startas);
  1130. while (argc-- > 0)
  1131. printf("%s ", *argv++);
  1132. if (changeuser != NULL) {
  1133. printf(" (as user %s[%d]", changeuser, runas_uid);
  1134. if (changegroup != NULL)
  1135. printf(", and group %s[%d])", changegroup, runas_gid);
  1136. else
  1137. printf(")");
  1138. }
  1139. if (changeroot != NULL)
  1140. printf(" in directory %s", changeroot);
  1141. if (nicelevel)
  1142. printf(", and add %i to the priority", nicelevel);
  1143. printf(".\n");
  1144. }
  1145. if (testmode)
  1146. exit(0);
  1147. if (quietmode < 0)
  1148. printf("Starting %s...\n", startas);
  1149. *--argv = startas;
  1150. if (background) { /* ok, we need to detach this process */
  1151. daemonize();
  1152. #ifdef HAVE_TIOCNOTTY
  1153. tty_fd=open("/dev/tty", O_RDWR);
  1154. #endif
  1155. devnull_fd=open("/dev/null", O_RDWR);
  1156. }
  1157. if (nicelevel) {
  1158. errno = 0;
  1159. if ((nice(nicelevel) == -1) && (errno != 0))
  1160. fatal("Unable to alter nice level by %i: %s",
  1161. nicelevel, strerror(errno));
  1162. }
  1163. if (umask_value >= 0)
  1164. umask(umask_value);
  1165. if (mpidfile && pidfile != NULL) {
  1166. /* User wants _us_ to make the pidfile :) */
  1167. FILE *pidf = fopen(pidfile, "w");
  1168. pid_t pidt = getpid();
  1169. if (pidf == NULL)
  1170. fatal("Unable to open pidfile `%s' for writing: %s",
  1171. pidfile, strerror(errno));
  1172. fprintf(pidf, "%d\n", pidt);
  1173. fclose(pidf);
  1174. }
  1175. if (changeroot != NULL) {
  1176. if (chdir(changeroot) < 0)
  1177. fatal("Unable to chdir() to %s", changeroot);
  1178. if (chroot(changeroot) < 0)
  1179. fatal("Unable to chroot() to %s", changeroot);
  1180. }
  1181. if (chdir(changedir) < 0)
  1182. fatal("Unable to chdir() to %s", changedir);
  1183. rgid = getgid();
  1184. ruid = getuid();
  1185. if (changegroup != NULL) {
  1186. if (rgid != (gid_t)runas_gid)
  1187. if (setgid(runas_gid))
  1188. fatal("Unable to set gid to %d", runas_gid);
  1189. }
  1190. if (changeuser != NULL) {
  1191. /* We assume that if our real user and group are the same as
  1192. * the ones we should switch to, the supplementary groups
  1193. * will be already in place. */
  1194. if (rgid != (gid_t)runas_gid || ruid != (uid_t)runas_uid)
  1195. if (initgroups(changeuser, runas_gid))
  1196. fatal("Unable to set initgroups() with gid %d",
  1197. runas_gid);
  1198. if (ruid != (uid_t)runas_uid)
  1199. if (setuid(runas_uid))
  1200. fatal("Unable to set uid to %s", changeuser);
  1201. }
  1202. if (background) {
  1203. /* Continue background setup */
  1204. int i;
  1205. #ifdef HAVE_TIOCNOTTY
  1206. /* Change tty */
  1207. ioctl(tty_fd, TIOCNOTTY, 0);
  1208. close(tty_fd);
  1209. #endif
  1210. if (umask_value < 0)
  1211. umask(022); /* Set a default for dumb programs */
  1212. dup2(devnull_fd, 0); /* stdin */
  1213. dup2(devnull_fd, 1); /* stdout */
  1214. dup2(devnull_fd, 2); /* stderr */
  1215. /* Now close all extra fds */
  1216. for (i = get_open_fd_max() - 1; i >= 3; --i)
  1217. close(i);
  1218. }
  1219. execv(startas, argv);
  1220. fatal("Unable to start %s: %s", startas, strerror(errno));
  1221. }