start-stop-daemon.c 33 KB

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