start-stop-daemon.c 33 KB

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