1 // SPDX-License-Identifier: NONE
3 * A rewrite of the original Debian's start-stop-daemon Perl script
4 * in C (faster - it is executed many times during system startup).
6 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
7 * public domain. Based conceptually on start-stop-daemon.pl, by Ian
8 * Jackson <ijackson@gnu.ai.mit.edu>. May be used and distributed
9 * freely for any purpose. Changes by Christian Schwarz
10 * <schwarz@monet.m.isar.de>, to make output conform to the Debian
11 * Console Message Standard, also placed in public domain. Minor
12 * changes by Klee Dienes <klee@debian.org>, also placed in the Public
15 * Changes by Ben Collins <bcollins@debian.org>, added --chuid, --background
16 * and --make-pidfile options, placed in public domain aswell.
18 * Port to OpenBSD by Sontri Tomo Huynh <huynh.29@osu.edu>
19 * and Andreas Schuldei <andreas@schuldei.org>
21 * Changes by Ian Jackson: added --retry (and associated rearrangements).
23 * Modified for Gentoo rc-scripts by Donny Davies <woodchip@gentoo.org>:
24 * I removed the BSD/Hurd/OtherOS stuff, added #include <stddef.h>
25 * and stuck in a #define VERSION "1.9.18". Now it compiles without
26 * the whole automake/config.h dance.
40 #define VERSION "1.9.18"
42 #define MIN_POLL_INTERVAL 20000 /*us*/
53 #include <sys/queue.h>
58 #include <sys/ioctl.h>
59 #include <sys/types.h>
66 #include <linux/sched.h>
69 static int testmode
= 0;
70 static int quietmode
= 0;
71 static int exitnodo
= 1;
74 static int background
= 0;
75 static int mpidfile
= 0;
76 static int signal_nr
= 15;
77 static const char *signal_str
= NULL
;
78 static int user_id
= -1;
79 static int runas_uid
= -1;
80 static int runas_gid
= -1;
81 static const char *userspec
= NULL
;
82 static char *changeuser
= NULL
;
83 static const char *changegroup
= NULL
;
84 static char *changeroot
= NULL
;
85 static const char *cmdname
= NULL
;
86 static char *execname
= NULL
;
87 static char *startas
= NULL
;
88 static const char *pidfile
= NULL
;
89 static char what_stop
[1024];
90 static const char *schedule_str
= NULL
;
91 static const char *progname
= "";
92 static int nicelevel
= 0;
94 static struct stat exec_stat
;
97 struct pid_list
*next
;
101 static struct pid_list
*found
= NULL
;
102 static struct pid_list
*killed
= NULL
;
104 struct schedule_item
{
105 enum { sched_timeout
, sched_signal
, sched_goto
, sched_forever
} type
;
106 int value
; /* seconds, signal no., or index into array */
107 /* sched_forever is only seen within parse_schedule and callees */
110 static int schedule_length
;
111 static struct schedule_item
*schedule
= NULL
;
113 LIST_HEAD(namespace_head
, namespace);
117 LIST_ENTRY(namespace) list
;
122 static struct namespace_head namespace_head
;
124 static void *xmalloc(int size
);
125 static void push(struct pid_list
**list
, pid_t pid
);
126 static void do_help(void);
127 static void parse_options(int argc
, char *const *argv
);
128 static int pid_is_user(pid_t pid
, uid_t uid
);
129 static int pid_is_cmd(pid_t pid
, const char *name
);
130 static void check(pid_t pid
);
131 static void do_pidfile(const char *name
);
132 static void do_stop(int signal_nr
, int quietmode
, int *n_killed
,
133 int *n_notkilled
, int retry_nr
);
134 static int pid_is_exec(pid_t pid
, const struct stat
*esb
);
137 static void fatal(const char *format
, ...)
138 __attribute__((noreturn
, format(printf
, 1, 2)));
139 static void badusage(const char *msg
) __attribute__((noreturn
));
141 static void fatal(const char *format
, ...);
142 static void badusage(const char *msg
);
145 /* This next part serves only to construct the TVCALC macro, which
146 * is used for doing arithmetic on struct timeval's. It works like this:
147 * TVCALC(result, expression);
148 * where result is a struct timeval (and must be an lvalue) and
149 * expression is the single expression for both components. In this
150 * expression you can use the special values TVELEM, which when fed a
151 * const struct timeval* gives you the relevant component, and
152 * TVADJUST. TVADJUST is necessary when subtracting timevals, to make
153 * it easier to renormalise. Whenver you subtract timeval elements,
154 * you must make sure that TVADJUST is added to the result of the
155 * subtraction (before any resulting multiplication or what have you).
156 * TVELEM must be linear in TVADJUST.
158 typedef long tvselector(const struct timeval
*);
159 static long tvselector_sec(const struct timeval
*tv
)
163 static long tvselector_usec(const struct timeval
*tv
)
167 #define TVCALC_ELEM(result, expr, sec, adj) \
169 const long TVADJUST = adj; \
170 long (*const TVELEM)(const struct timeval *) = \
172 (result).tv_##sec = (expr); \
174 #define TVCALC(result, expr) \
176 TVCALC_ELEM(result, expr, sec, (-1)); \
177 TVCALC_ELEM(result, expr, usec, (+1000000)); \
178 (result).tv_sec += (result).tv_usec / 1000000; \
179 (result).tv_usec %= 1000000; \
183 static void fatal(const char *format
, ...)
187 fprintf(stderr
, "%s: ", progname
);
188 va_start(arglist
, format
);
189 vfprintf(stderr
, format
, arglist
);
196 static void *xmalloc(int size
)
203 fatal("malloc(%d) failed", size
);
206 static void xgettimeofday(struct timeval
*tv
)
208 if (gettimeofday(tv
, 0) != 0)
209 fatal("gettimeofday failed: %s", strerror(errno
));
212 static void push(struct pid_list
**list
, pid_t pid
)
216 p
= xmalloc(sizeof(*p
));
222 static void clear(struct pid_list
**list
)
224 struct pid_list
*here
, *next
;
226 for (here
= *list
; here
!= NULL
; here
= next
) {
235 static const char *next_dirname(const char *s
)
242 for (; *cur
!= '/'; ++cur
)
246 for (; *cur
== '/'; ++cur
)
253 static void add_namespace(const char *path
)
256 const char *nsdirname
, *nsname
, *cur
;
257 struct namespace *namespace;
260 nsdirname
= nsname
= "";
262 while ((cur
= next_dirname(cur
))[0] != '\0') {
267 if (!strncmp(nsdirname
, "ipcns/", strlen("ipcns/")))
268 nstype
= CLONE_NEWIPC
;
269 else if (!strncmp(nsdirname
, "netns/", strlen("netns/")))
270 nstype
= CLONE_NEWNET
;
271 else if (!strncmp(nsdirname
, "utcns/", strlen("utcns/")))
272 nstype
= CLONE_NEWUTS
;
274 badusage("invalid namepspace path");
276 namespace = xmalloc(sizeof(*namespace));
277 namespace->path
= path
;
278 namespace->nstype
= nstype
;
279 LIST_INSERT_HEAD(&namespace_head
, namespace, list
);
284 static void set_namespaces(void)
286 struct namespace *namespace;
289 LIST_FOREACH (namespace, &namespace_head
, list
) {
290 if ((fd
= open(namespace->path
, O_RDONLY
)) == -1)
291 fatal("open namespace %s: %s", namespace->path
,
293 if (setns(fd
, namespace->nstype
) == -1)
294 fatal("setns %s: %s", namespace->path
, strerror(errno
));
298 static void set_namespaces(void)
300 if (!LIST_EMPTY(&namespace_head
))
301 fatal("LCX namespaces not supported");
305 static void do_help(void)
307 printf("start-stop-daemon " VERSION
308 " for Debian - small and fast C version written by\n"
309 "Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
312 " start-stop-daemon -S|--start options ... -- arguments ...\n"
313 " start-stop-daemon -K|--stop options ...\n"
314 " start-stop-daemon -H|--help\n"
315 " start-stop-daemon -V|--version\n"
317 "Options (at least one of --exec|--pidfile|--user is required):\n"
318 " -x|--exec <executable> program to start/check if it is running\n"
319 " -p|--pidfile <pid-file> pid file to check\n"
320 " -c|--chuid <name|uid[:group|gid]>\n"
321 " change to this user/group before starting process\n"
322 " -u|--user <username>|<uid> stop processes owned by this user\n"
323 " -n|--name <process-name> stop processes with this name\n"
324 " -s|--signal <signal> signal to send (default TERM)\n"
325 " -a|--startas <pathname> program to start (default is <executable>)\n"
326 " -N|--nicelevel <incr> add incr to the process's nice level\n"
327 " -b|--background force the process to detach\n"
328 " -m|--make-pidfile create the pidfile before starting\n"
329 " -R|--retry <schedule> check whether processes die, and retry\n"
330 " -t|--test test mode, don't do anything\n"
331 " -o|--oknodo exit status 0 (not 1) if nothing done\n"
332 " -q|--quiet be more quiet\n"
333 " -v|--verbose be more verbose\n"
334 "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
335 " -<signal-num>|[-]<signal-name> send that signal\n"
336 " <timeout> wait that many seconds\n"
337 " forever repeat remainder forever\n"
338 "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
340 "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n"
341 " 3 = trouble 2 = with --retry, processes wouldn't die\n");
345 static void badusage(const char *msg
)
348 fprintf(stderr
, "%s: %s\n", progname
, msg
);
349 fprintf(stderr
, "Try `%s --help' for more information.\n", progname
);
358 const struct sigpair siglist
[] = {
359 {"ABRT", SIGABRT
}, {"ALRM", SIGALRM
}, {"FPE", SIGFPE
},
360 {"HUP", SIGHUP
}, {"ILL", SIGILL
}, {"INT", SIGINT
},
361 {"KILL", SIGKILL
}, {"PIPE", SIGPIPE
}, {"QUIT", SIGQUIT
},
362 {"SEGV", SIGSEGV
}, {"TERM", SIGTERM
}, {"USR1", SIGUSR1
},
363 {"USR2", SIGUSR2
}, {"CHLD", SIGCHLD
}, {"CONT", SIGCONT
},
364 {"STOP", SIGSTOP
}, {"TSTP", SIGTSTP
}, {"TTIN", SIGTTIN
},
367 static int parse_integer(const char *string
, int *value_r
)
375 ul
= strtoul(string
, &ep
, 10);
376 if (ul
> INT_MAX
|| *ep
!= '\0')
383 static int parse_signal(const char *signal_str
, int *signal_nr
)
387 if (parse_integer(signal_str
, signal_nr
) == 0)
390 for (i
= 0; i
< sizeof(siglist
) / sizeof(siglist
[0]); i
++) {
391 if (strcmp(signal_str
, siglist
[i
].name
) == 0) {
392 *signal_nr
= siglist
[i
].signal
;
399 static void parse_schedule_item(const char *string
, struct schedule_item
*item
)
401 const char *after_hyph
;
403 if (!strcmp(string
, "forever")) {
404 item
->type
= sched_forever
;
405 } else if (isdigit((unsigned char)string
[0])) {
406 item
->type
= sched_timeout
;
407 if (parse_integer(string
, &item
->value
) != 0)
408 badusage("invalid timeout value in schedule");
409 } else if ((after_hyph
= string
+ (string
[0] == '-'))
410 && parse_signal(after_hyph
, &item
->value
) == 0) {
411 item
->type
= sched_signal
;
414 "invalid schedule item (must be [-]<signal-name>, -<signal-number>, <timeout> or `forever'");
418 static void parse_schedule(const char *schedule_str
)
426 for (slash
= schedule_str
; *slash
; slash
++)
430 schedule_length
= (count
== 0) ? 4 : count
+ 1;
431 schedule
= xmalloc(sizeof(*schedule
) * schedule_length
);
434 schedule
[0].type
= sched_signal
;
435 schedule
[0].value
= signal_nr
;
436 parse_schedule_item(schedule_str
, &schedule
[1]);
437 if (schedule
[1].type
!= sched_timeout
) {
439 "--retry takes timeout, or schedule list of at least two items");
441 schedule
[2].type
= sched_signal
;
442 schedule
[2].value
= SIGKILL
;
443 schedule
[3] = schedule
[1];
447 while (schedule_str
!= NULL
) {
448 slash
= strchr(schedule_str
, '/');
449 str_len
= slash
? slash
- schedule_str
450 : (ptrdiff_t)strlen(schedule_str
);
451 if (str_len
>= (ptrdiff_t)sizeof(item_buf
))
453 "invalid schedule item: far too long (you must delimit items with slashes)");
454 memcpy(item_buf
, schedule_str
, str_len
);
455 item_buf
[str_len
] = 0;
456 schedule_str
= slash
? slash
+ 1 : NULL
;
458 parse_schedule_item(item_buf
, &schedule
[count
]);
459 if (schedule
[count
].type
== sched_forever
) {
462 "invalid schedule: `forever' appears more than once");
469 schedule
[count
].type
= sched_goto
;
470 schedule
[count
].value
= repeatat
;
473 assert(count
== schedule_length
);
477 static void parse_options(int argc
, char *const *argv
)
479 static struct option longopts
[] = {
480 {"help", 0, NULL
, 'H'}, {"stop", 0, NULL
, 'K'},
481 {"start", 0, NULL
, 'S'}, {"version", 0, NULL
, 'V'},
482 {"startas", 1, NULL
, 'a'}, {"name", 1, NULL
, 'n'},
483 {"oknodo", 0, NULL
, 'o'}, {"pidfile", 1, NULL
, 'p'},
484 {"quiet", 0, NULL
, 'q'}, {"signal", 1, NULL
, 's'},
485 {"test", 0, NULL
, 't'}, {"user", 1, NULL
, 'u'},
486 {"chroot", 1, NULL
, 'r'}, {"namespace", 1, NULL
, 'd'},
487 {"verbose", 0, NULL
, 'v'}, {"exec", 1, NULL
, 'x'},
488 {"chuid", 1, NULL
, 'c'}, {"nicelevel", 1, NULL
, 'N'},
489 {"background", 0, NULL
, 'b'}, {"make-pidfile", 0, NULL
, 'm'},
490 {"retry", 1, NULL
, 'R'}, {NULL
, 0, NULL
, 0}};
494 c
= getopt_long(argc
, argv
,
495 "HKSVa:n:op:qr:d:s:tu:vx:c:N:bmR:", longopts
,
500 case 'H': /* --help */
503 case 'K': /* --stop */
506 case 'S': /* --start */
509 case 'V': /* --version */
510 printf("start-stop-daemon " VERSION
"\n");
512 case 'a': /* --startas <pathname> */
515 case 'n': /* --name <process-name> */
518 case 'o': /* --oknodo */
521 case 'p': /* --pidfile <pid-file> */
524 case 'q': /* --quiet */
527 case 's': /* --signal <signal> */
530 case 't': /* --test */
533 case 'u': /* --user <username>|<uid> */
536 case 'v': /* --verbose */
539 case 'x': /* --exec <executable> */
542 case 'c': /* --chuid <username>|<uid> */
543 changeuser
= strtok(optarg
, ":");
544 changegroup
= strtok(NULL
, ":");
546 case 'r': /* --chroot /new/root */
549 case 'd': /* --namespace /.../<ipcns>|<netns>|<utsns>/name */
551 add_namespace(optarg
);
554 case 'N': /* --nice */
555 nicelevel
= atoi(optarg
);
557 case 'b': /* --background */
560 case 'm': /* --make-pidfile */
563 case 'R': /* --retry <schedule>|<timeout> */
564 schedule_str
= optarg
;
567 badusage(NULL
); /* message printed by getopt */
571 if (signal_str
!= NULL
) {
572 if (parse_signal(signal_str
, &signal_nr
) != 0)
574 "signal value must be numeric or name of signal (KILL, INTR, ...)");
577 if (schedule_str
!= NULL
) {
578 parse_schedule(schedule_str
);
582 badusage("need one of --start or --stop");
584 if (!execname
&& !pidfile
&& !userspec
&& !cmdname
)
586 "need at least one of --exec, --pidfile, --user or --name");
591 if (start
&& !startas
)
592 badusage("--start needs --exec or --startas");
594 if (mpidfile
&& pidfile
== NULL
)
595 badusage("--make-pidfile is only relevant with --pidfile");
597 if (background
&& !start
)
598 badusage("--background is only relevant with --start");
601 static int pid_is_exec(pid_t pid
, const struct stat
*esb
)
606 snprintf(buf
, sizeof(buf
), "/proc/%ld/exe", (long)pid
);
607 if (stat(buf
, &sb
) != 0)
609 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
613 static int pid_is_user(pid_t pid
, uid_t uid
)
618 snprintf(buf
, sizeof(buf
), "/proc/%ld", (long)pid
);
619 if (stat(buf
, &sb
) != 0)
621 return (sb
.st_uid
== uid
);
625 static int pid_is_cmd(pid_t pid
, const char *name
)
631 snprintf(buf
, sizeof(buf
), "/proc/%ld/stat", (long)pid
);
635 while ((c
= getc(f
)) != EOF
&& c
!= '(')
641 /* this hopefully handles command names containing ')' */
642 while ((c
= getc(f
)) != EOF
&& c
== *name
)
645 return (c
== ')' && *name
== '\0');
649 static void check(pid_t pid
)
651 if (execname
&& !pid_is_exec(pid
, &exec_stat
))
653 if (userspec
&& !pid_is_user(pid
, user_id
))
655 if (cmdname
&& !pid_is_cmd(pid
, cmdname
))
660 static void do_pidfile(const char *name
)
665 f
= fopen(name
, "r");
667 if (fscanf(f
, "%ld", &pid
) == 1)
670 } else if (errno
!= ENOENT
)
671 fatal("open pidfile %s: %s", name
, strerror(errno
));
674 /* WTA: this needs to be an autoconf check for /proc/pid existance.
676 static void do_procinit(void)
679 struct dirent
*entry
;
683 procdir
= opendir("/proc");
685 fatal("opendir /proc: %s", strerror(errno
));
688 while ((entry
= readdir(procdir
)) != NULL
) {
689 if (sscanf(entry
->d_name
, "%ld", &pid
) != 1)
696 fatal("nothing in /proc - not mounted?");
699 static void do_findprocs(void)
709 /* return 1 on failure */
710 static void do_stop(int signal_nr
, int quietmode
, int *n_killed
,
711 int *n_notkilled
, int retry_nr
)
725 for (p
= found
; p
; p
= p
->next
) {
727 printf("Would send signal %d to %ld.\n", signal_nr
,
729 else if (kill(p
->pid
, signal_nr
) == 0) {
730 push(&killed
, p
->pid
);
733 printf("%s: warning: failed to kill %ld: %s\n",
734 progname
, (long)p
->pid
, strerror(errno
));
738 if (quietmode
< 0 && killed
) {
739 printf("Stopped %s (pid", what_stop
);
740 for (p
= killed
; p
; p
= p
->next
)
741 printf(" %ld", (long)p
->pid
);
744 printf(", retry #%d", retry_nr
);
750 static void set_what_stop(const char *str
)
752 strncpy(what_stop
, str
, sizeof(what_stop
));
753 what_stop
[sizeof(what_stop
) - 1] = '\0';
756 static int run_stop_schedule(void)
758 int r
, position
, n_killed
, n_notkilled
, value
, ratio
, anykilled
,
760 struct timeval stopat
, before
, after
, interval
, maxinterval
;
763 if (schedule
!= NULL
) {
764 printf("Ignoring --retry in test mode\n");
770 set_what_stop(cmdname
);
772 set_what_stop(execname
);
774 sprintf(what_stop
, "process in pidfile `%.200s'", pidfile
);
776 sprintf(what_stop
, "process(es) owned by `%.200s'", userspec
);
778 fatal("internal error, please report");
784 if (schedule
== NULL
) {
785 do_stop(signal_nr
, quietmode
, &n_killed
, &n_notkilled
, 0);
786 if (n_notkilled
> 0 && quietmode
<= 0)
787 printf("%d pids were not killed\n", n_notkilled
);
793 for (position
= 0; position
< schedule_length
;) {
794 value
= schedule
[position
].value
;
797 switch (schedule
[position
].type
) {
804 do_stop(value
, quietmode
, &n_killed
, &n_notkilled
,
813 /* We want to keep polling for the processes, to see if
815 * or until the timeout expires.
817 * This is a somewhat complicated algorithm to try to
819 * notice reasonably quickly when all the processes have
821 * don't spend too much CPU time polling. In
822 * particular, on a fast
823 * machine with quick-exiting daemons we don't want to
825 * shutdown too much, whereas on a slow one, or where
827 * taking some time to exit, we want to increase the
831 * The algorithm is as follows: we measure the elapsed
833 * to do one poll(), and wait a multiple of this time
835 * poll. However, if that would put us past the end of
837 * period we wait only as long as the timeout period,
839 * we always wait at least MIN_POLL_INTERVAL (20ms).
841 * (`ratio') starts out as 2, and increases by 1 for
843 * maximum of 10; so we use up to between 30% and 10% of
845 * machine's resources (assuming a few reasonable things
849 xgettimeofday(&stopat
);
850 stopat
.tv_sec
+= value
;
853 xgettimeofday(&before
);
854 if (timercmp(&before
, &stopat
, >))
857 do_stop(0, 1, &n_killed
, &n_notkilled
, 0);
861 xgettimeofday(&after
);
863 if (!timercmp(&after
, &stopat
, <))
870 ratio
* (TVELEM(&after
) - TVELEM(&before
)
873 TVELEM(&stopat
) - TVELEM(&after
)
876 if (timercmp(&interval
, &maxinterval
, >))
877 interval
= maxinterval
;
879 if (interval
.tv_sec
== 0
880 && interval
.tv_usec
<= MIN_POLL_INTERVAL
)
881 interval
.tv_usec
= MIN_POLL_INTERVAL
;
883 r
= select(0, 0, 0, 0, &interval
);
884 if (r
< 0 && errno
!= EINTR
)
885 fatal("select() failed for pause: %s",
890 assert(!"schedule[].type value must be valid");
898 printf("Program %s, %d process(es), refused to die.\n",
899 what_stop
, n_killed
);
906 printf("No %s found running; none killed.\n",
915 int main(int argc, char **argv) NONRETURNING;
918 int main(int argc
, char **argv
)
922 LIST_INIT(&namespace_head
);
924 parse_options(argc
, argv
);
928 if (execname
&& stat(execname
, &exec_stat
))
929 fatal("stat %s: %s", execname
, strerror(errno
));
931 if (userspec
&& sscanf(userspec
, "%d", &user_id
) != 1) {
934 pw
= getpwnam(userspec
);
936 fatal("user `%s' not found\n", userspec
);
938 user_id
= pw
->pw_uid
;
941 if (changegroup
&& sscanf(changegroup
, "%d", &runas_gid
) != 1) {
942 struct group
*gr
= getgrnam(changegroup
);
944 fatal("group `%s' not found\n", changegroup
);
945 runas_gid
= gr
->gr_gid
;
947 if (changeuser
&& sscanf(changeuser
, "%d", &runas_uid
) != 1) {
948 struct passwd
*pw
= getpwnam(changeuser
);
950 fatal("user `%s' not found\n", changeuser
);
951 runas_uid
= pw
->pw_uid
;
953 == NULL
) { /* pass the default group of this user */
954 changegroup
= ""; /* just empty */
955 runas_gid
= pw
->pw_gid
;
960 int i
= run_stop_schedule();
968 printf("%s already running.\n", execname
);
972 printf("Would start %s ", startas
);
974 printf("%s ", *argv
++);
975 if (changeuser
!= NULL
) {
976 printf(" (as user %s[%d]", changeuser
, runas_uid
);
977 if (changegroup
!= NULL
)
978 printf(", and group %s[%d])", changegroup
,
983 if (changeroot
!= NULL
)
984 printf(" in directory %s", changeroot
);
986 printf(", and add %i to the priority", nicelevel
);
991 printf("Starting %s...\n", startas
);
993 if (changeroot
!= NULL
) {
994 if (chdir(changeroot
) < 0)
995 fatal("Unable to chdir() to %s", changeroot
);
996 if (chroot(changeroot
) < 0)
997 fatal("Unable to chroot() to %s", changeroot
);
999 if (changeuser
!= NULL
) {
1000 if (setgid(runas_gid
))
1001 fatal("Unable to set gid to %d", runas_gid
);
1002 if (initgroups(changeuser
, runas_gid
))
1003 fatal("Unable to set initgroups() with gid %d",
1005 if (setuid(runas_uid
))
1006 fatal("Unable to set uid to %s", changeuser
);
1009 if (background
) { /* ok, we need to detach this process */
1012 printf("Detaching to start %s...", startas
);
1015 fatal("Unable to fork.\n");
1017 if (i
) { /* parent */
1022 /* child continues here */
1023 /* now close all extra fds */
1024 for (i
= getdtablesize() - 1; i
>= 0; --i
)
1027 fd
= open("/dev/tty", O_RDWR
);
1029 if (ioctl(fd
, TIOCNOTTY
, 0) < 0)
1030 printf("ioctl TIOCNOTTY failed: %s\n",
1035 umask(022); /* set a default for dumb programs */
1036 setpgid(0, 0); /* set the process group */
1037 fd
= open("/dev/null", O_RDWR
); /* stdin */
1039 dup(fd
); /* stdout */
1040 dup(fd
); /* stderr */
1045 if (nice(nicelevel
) < 0 && errno
)
1046 fatal("Unable to alter nice level by %i: %s", nicelevel
,
1050 && pidfile
!= NULL
) { /* user wants _us_ to make the pidfile :) */
1051 FILE *pidf
= fopen(pidfile
, "w");
1052 pid_t pidt
= getpid();
1054 fatal("Unable to open pidfile `%s' for writing: %s",
1055 pidfile
, strerror(errno
));
1056 fprintf(pidf
, "%ld\n", (long)pidt
);
1060 execv(startas
, argv
);
1061 fatal("Unable to start %s: %s", startas
, strerror(errno
));