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).
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
14 * Changes by Ben Collins <bcollins@debian.org>, added --chuid, --background
15 * and --make-pidfile options, placed in public domain aswell.
17 * Port to OpenBSD by Sontri Tomo Huynh <huynh.29@osu.edu>
18 * and Andreas Schuldei <andreas@schuldei.org>
20 * Changes by Ian Jackson: added --retry (and associated rearrangements).
22 * Modified for Gentoo rc-scripts by Donny Davies <woodchip@gentoo.org>:
23 * I removed the BSD/Hurd/OtherOS stuff, added #include <stddef.h>
24 * and stuck in a #define VERSION "1.9.18". Now it compiles without
25 * the whole automake/config.h dance.
39 #define VERSION "1.9.18"
41 #define MIN_POLL_INTERVAL 20000 /*us*/
52 #include <sys/queue.h>
57 #include <sys/ioctl.h>
58 #include <sys/types.h>
65 #include <linux/sched.h>
68 static int testmode
= 0;
69 static int quietmode
= 0;
70 static int exitnodo
= 1;
73 static int background
= 0;
74 static int mpidfile
= 0;
75 static int signal_nr
= 15;
76 static const char *signal_str
= NULL
;
77 static int user_id
= -1;
78 static int runas_uid
= -1;
79 static int runas_gid
= -1;
80 static const char *userspec
= NULL
;
81 static char *changeuser
= NULL
;
82 static const char *changegroup
= NULL
;
83 static char *changeroot
= NULL
;
84 static const char *cmdname
= NULL
;
85 static char *execname
= NULL
;
86 static char *startas
= NULL
;
87 static const char *pidfile
= NULL
;
88 static char what_stop
[1024];
89 static const char *schedule_str
= NULL
;
90 static const char *progname
= "";
91 static int nicelevel
= 0;
93 static struct stat exec_stat
;
96 struct pid_list
*next
;
100 static struct pid_list
*found
= NULL
;
101 static struct pid_list
*killed
= NULL
;
103 struct schedule_item
{
104 enum { sched_timeout
, sched_signal
, sched_goto
, sched_forever
} type
;
105 int value
; /* seconds, signal no., or index into array */
106 /* sched_forever is only seen within parse_schedule and callees */
109 static int schedule_length
;
110 static struct schedule_item
*schedule
= NULL
;
112 LIST_HEAD(namespace_head
, namespace);
116 LIST_ENTRY(namespace) list
;
121 static struct namespace_head namespace_head
;
123 static void *xmalloc(int size
);
124 static void push(struct pid_list
**list
, pid_t pid
);
125 static void do_help(void);
126 static void parse_options(int argc
, char *const *argv
);
127 static int pid_is_user(pid_t pid
, uid_t uid
);
128 static int pid_is_cmd(pid_t pid
, const char *name
);
129 static void check(pid_t pid
);
130 static void do_pidfile(const char *name
);
131 static void do_stop(int signal_nr
, int quietmode
, int *n_killed
,
132 int *n_notkilled
, int retry_nr
);
133 static int pid_is_exec(pid_t pid
, const struct stat
*esb
);
136 static void fatal(const char *format
, ...)
137 __attribute__((noreturn
, format(printf
, 1, 2)));
138 static void badusage(const char *msg
) __attribute__((noreturn
));
140 static void fatal(const char *format
, ...);
141 static void badusage(const char *msg
);
144 /* This next part serves only to construct the TVCALC macro, which
145 * is used for doing arithmetic on struct timeval's. It works like this:
146 * TVCALC(result, expression);
147 * where result is a struct timeval (and must be an lvalue) and
148 * expression is the single expression for both components. In this
149 * expression you can use the special values TVELEM, which when fed a
150 * const struct timeval* gives you the relevant component, and
151 * TVADJUST. TVADJUST is necessary when subtracting timevals, to make
152 * it easier to renormalise. Whenver you subtract timeval elements,
153 * you must make sure that TVADJUST is added to the result of the
154 * subtraction (before any resulting multiplication or what have you).
155 * TVELEM must be linear in TVADJUST.
157 typedef long tvselector(const struct timeval
*);
158 static long tvselector_sec(const struct timeval
*tv
)
162 static long tvselector_usec(const struct timeval
*tv
)
166 #define TVCALC_ELEM(result, expr, sec, adj) \
168 const long TVADJUST = adj; \
169 long (*const TVELEM)(const struct timeval *) = \
171 (result).tv_##sec = (expr); \
173 #define TVCALC(result, expr) \
175 TVCALC_ELEM(result, expr, sec, (-1)); \
176 TVCALC_ELEM(result, expr, usec, (+1000000)); \
177 (result).tv_sec += (result).tv_usec / 1000000; \
178 (result).tv_usec %= 1000000; \
182 static void fatal(const char *format
, ...)
186 fprintf(stderr
, "%s: ", progname
);
187 va_start(arglist
, format
);
188 vfprintf(stderr
, format
, arglist
);
195 static void *xmalloc(int size
)
202 fatal("malloc(%d) failed", size
);
205 static void xgettimeofday(struct timeval
*tv
)
207 if (gettimeofday(tv
, 0) != 0)
208 fatal("gettimeofday failed: %s", strerror(errno
));
211 static void push(struct pid_list
**list
, pid_t pid
)
215 p
= xmalloc(sizeof(*p
));
221 static void clear(struct pid_list
**list
)
223 struct pid_list
*here
, *next
;
225 for (here
= *list
; here
!= NULL
; here
= next
) {
234 static const char *next_dirname(const char *s
)
241 for (; *cur
!= '/'; ++cur
)
245 for (; *cur
== '/'; ++cur
)
252 static void add_namespace(const char *path
)
255 const char *nsdirname
, *nsname
, *cur
;
256 struct namespace *namespace;
259 nsdirname
= nsname
= "";
261 while ((cur
= next_dirname(cur
))[0] != '\0') {
266 if (!strncmp(nsdirname
, "ipcns/", strlen("ipcns/")))
267 nstype
= CLONE_NEWIPC
;
268 else if (!strncmp(nsdirname
, "netns/", strlen("netns/")))
269 nstype
= CLONE_NEWNET
;
270 else if (!strncmp(nsdirname
, "utcns/", strlen("utcns/")))
271 nstype
= CLONE_NEWUTS
;
273 badusage("invalid namepspace path");
275 namespace = xmalloc(sizeof(*namespace));
276 namespace->path
= path
;
277 namespace->nstype
= nstype
;
278 LIST_INSERT_HEAD(&namespace_head
, namespace, list
);
283 static void set_namespaces(void)
285 struct namespace *namespace;
288 LIST_FOREACH (namespace, &namespace_head
, list
) {
289 if ((fd
= open(namespace->path
, O_RDONLY
)) == -1)
290 fatal("open namespace %s: %s", namespace->path
,
292 if (setns(fd
, namespace->nstype
) == -1)
293 fatal("setns %s: %s", namespace->path
, strerror(errno
));
297 static void set_namespaces(void)
299 if (!LIST_EMPTY(&namespace_head
))
300 fatal("LCX namespaces not supported");
304 static void do_help(void)
306 printf("start-stop-daemon " VERSION
307 " for Debian - small and fast C version written by\n"
308 "Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
311 " start-stop-daemon -S|--start options ... -- arguments ...\n"
312 " start-stop-daemon -K|--stop options ...\n"
313 " start-stop-daemon -H|--help\n"
314 " start-stop-daemon -V|--version\n"
316 "Options (at least one of --exec|--pidfile|--user is required):\n"
317 " -x|--exec <executable> program to start/check if it is running\n"
318 " -p|--pidfile <pid-file> pid file to check\n"
319 " -c|--chuid <name|uid[:group|gid]>\n"
320 " change to this user/group before starting process\n"
321 " -u|--user <username>|<uid> stop processes owned by this user\n"
322 " -n|--name <process-name> stop processes with this name\n"
323 " -s|--signal <signal> signal to send (default TERM)\n"
324 " -a|--startas <pathname> program to start (default is <executable>)\n"
325 " -N|--nicelevel <incr> add incr to the process's nice level\n"
326 " -b|--background force the process to detach\n"
327 " -m|--make-pidfile create the pidfile before starting\n"
328 " -R|--retry <schedule> check whether processes die, and retry\n"
329 " -t|--test test mode, don't do anything\n"
330 " -o|--oknodo exit status 0 (not 1) if nothing done\n"
331 " -q|--quiet be more quiet\n"
332 " -v|--verbose be more verbose\n"
333 "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
334 " -<signal-num>|[-]<signal-name> send that signal\n"
335 " <timeout> wait that many seconds\n"
336 " forever repeat remainder forever\n"
337 "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
339 "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n"
340 " 3 = trouble 2 = with --retry, processes wouldn't die\n");
344 static void badusage(const char *msg
)
347 fprintf(stderr
, "%s: %s\n", progname
, msg
);
348 fprintf(stderr
, "Try `%s --help' for more information.\n", progname
);
357 const struct sigpair siglist
[] = {
358 {"ABRT", SIGABRT
}, {"ALRM", SIGALRM
}, {"FPE", SIGFPE
},
359 {"HUP", SIGHUP
}, {"ILL", SIGILL
}, {"INT", SIGINT
},
360 {"KILL", SIGKILL
}, {"PIPE", SIGPIPE
}, {"QUIT", SIGQUIT
},
361 {"SEGV", SIGSEGV
}, {"TERM", SIGTERM
}, {"USR1", SIGUSR1
},
362 {"USR2", SIGUSR2
}, {"CHLD", SIGCHLD
}, {"CONT", SIGCONT
},
363 {"STOP", SIGSTOP
}, {"TSTP", SIGTSTP
}, {"TTIN", SIGTTIN
},
366 static int parse_integer(const char *string
, int *value_r
)
374 ul
= strtoul(string
, &ep
, 10);
375 if (ul
> INT_MAX
|| *ep
!= '\0')
382 static int parse_signal(const char *signal_str
, int *signal_nr
)
386 if (parse_integer(signal_str
, signal_nr
) == 0)
389 for (i
= 0; i
< sizeof(siglist
) / sizeof(siglist
[0]); i
++) {
390 if (strcmp(signal_str
, siglist
[i
].name
) == 0) {
391 *signal_nr
= siglist
[i
].signal
;
398 static void parse_schedule_item(const char *string
, struct schedule_item
*item
)
400 const char *after_hyph
;
402 if (!strcmp(string
, "forever")) {
403 item
->type
= sched_forever
;
404 } else if (isdigit((unsigned char)string
[0])) {
405 item
->type
= sched_timeout
;
406 if (parse_integer(string
, &item
->value
) != 0)
407 badusage("invalid timeout value in schedule");
408 } else if ((after_hyph
= string
+ (string
[0] == '-'))
409 && parse_signal(after_hyph
, &item
->value
) == 0) {
410 item
->type
= sched_signal
;
413 "invalid schedule item (must be [-]<signal-name>, -<signal-number>, <timeout> or `forever'");
417 static void parse_schedule(const char *schedule_str
)
425 for (slash
= schedule_str
; *slash
; slash
++)
429 schedule_length
= (count
== 0) ? 4 : count
+ 1;
430 schedule
= xmalloc(sizeof(*schedule
) * schedule_length
);
433 schedule
[0].type
= sched_signal
;
434 schedule
[0].value
= signal_nr
;
435 parse_schedule_item(schedule_str
, &schedule
[1]);
436 if (schedule
[1].type
!= sched_timeout
) {
438 "--retry takes timeout, or schedule list of at least two items");
440 schedule
[2].type
= sched_signal
;
441 schedule
[2].value
= SIGKILL
;
442 schedule
[3] = schedule
[1];
446 while (schedule_str
!= NULL
) {
447 slash
= strchr(schedule_str
, '/');
448 str_len
= slash
? slash
- schedule_str
449 : (ptrdiff_t)strlen(schedule_str
);
450 if (str_len
>= (ptrdiff_t)sizeof(item_buf
))
452 "invalid schedule item: far too long (you must delimit items with slashes)");
453 memcpy(item_buf
, schedule_str
, str_len
);
454 item_buf
[str_len
] = 0;
455 schedule_str
= slash
? slash
+ 1 : NULL
;
457 parse_schedule_item(item_buf
, &schedule
[count
]);
458 if (schedule
[count
].type
== sched_forever
) {
461 "invalid schedule: `forever' appears more than once");
468 schedule
[count
].type
= sched_goto
;
469 schedule
[count
].value
= repeatat
;
472 assert(count
== schedule_length
);
476 static void parse_options(int argc
, char *const *argv
)
478 static struct option longopts
[] = {
479 {"help", 0, NULL
, 'H'}, {"stop", 0, NULL
, 'K'},
480 {"start", 0, NULL
, 'S'}, {"version", 0, NULL
, 'V'},
481 {"startas", 1, NULL
, 'a'}, {"name", 1, NULL
, 'n'},
482 {"oknodo", 0, NULL
, 'o'}, {"pidfile", 1, NULL
, 'p'},
483 {"quiet", 0, NULL
, 'q'}, {"signal", 1, NULL
, 's'},
484 {"test", 0, NULL
, 't'}, {"user", 1, NULL
, 'u'},
485 {"chroot", 1, NULL
, 'r'}, {"namespace", 1, NULL
, 'd'},
486 {"verbose", 0, NULL
, 'v'}, {"exec", 1, NULL
, 'x'},
487 {"chuid", 1, NULL
, 'c'}, {"nicelevel", 1, NULL
, 'N'},
488 {"background", 0, NULL
, 'b'}, {"make-pidfile", 0, NULL
, 'm'},
489 {"retry", 1, NULL
, 'R'}, {NULL
, 0, NULL
, 0}};
493 c
= getopt_long(argc
, argv
,
494 "HKSVa:n:op:qr:d:s:tu:vx:c:N:bmR:", longopts
,
499 case 'H': /* --help */
502 case 'K': /* --stop */
505 case 'S': /* --start */
508 case 'V': /* --version */
509 printf("start-stop-daemon " VERSION
"\n");
511 case 'a': /* --startas <pathname> */
514 case 'n': /* --name <process-name> */
517 case 'o': /* --oknodo */
520 case 'p': /* --pidfile <pid-file> */
523 case 'q': /* --quiet */
526 case 's': /* --signal <signal> */
529 case 't': /* --test */
532 case 'u': /* --user <username>|<uid> */
535 case 'v': /* --verbose */
538 case 'x': /* --exec <executable> */
541 case 'c': /* --chuid <username>|<uid> */
542 changeuser
= strtok(optarg
, ":");
543 changegroup
= strtok(NULL
, ":");
545 case 'r': /* --chroot /new/root */
548 case 'd': /* --namespace /.../<ipcns>|<netns>|<utsns>/name */
550 add_namespace(optarg
);
553 case 'N': /* --nice */
554 nicelevel
= atoi(optarg
);
556 case 'b': /* --background */
559 case 'm': /* --make-pidfile */
562 case 'R': /* --retry <schedule>|<timeout> */
563 schedule_str
= optarg
;
566 badusage(NULL
); /* message printed by getopt */
570 if (signal_str
!= NULL
) {
571 if (parse_signal(signal_str
, &signal_nr
) != 0)
573 "signal value must be numeric or name of signal (KILL, INTR, ...)");
576 if (schedule_str
!= NULL
) {
577 parse_schedule(schedule_str
);
581 badusage("need one of --start or --stop");
583 if (!execname
&& !pidfile
&& !userspec
&& !cmdname
)
585 "need at least one of --exec, --pidfile, --user or --name");
590 if (start
&& !startas
)
591 badusage("--start needs --exec or --startas");
593 if (mpidfile
&& pidfile
== NULL
)
594 badusage("--make-pidfile is only relevant with --pidfile");
596 if (background
&& !start
)
597 badusage("--background is only relevant with --start");
600 static int pid_is_exec(pid_t pid
, const struct stat
*esb
)
605 snprintf(buf
, sizeof(buf
), "/proc/%ld/exe", (long)pid
);
606 if (stat(buf
, &sb
) != 0)
608 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
612 static int pid_is_user(pid_t pid
, uid_t uid
)
617 snprintf(buf
, sizeof(buf
), "/proc/%ld", (long)pid
);
618 if (stat(buf
, &sb
) != 0)
620 return (sb
.st_uid
== uid
);
624 static int pid_is_cmd(pid_t pid
, const char *name
)
630 snprintf(buf
, sizeof(buf
), "/proc/%ld/stat", (long)pid
);
634 while ((c
= getc(f
)) != EOF
&& c
!= '(')
640 /* this hopefully handles command names containing ')' */
641 while ((c
= getc(f
)) != EOF
&& c
== *name
)
644 return (c
== ')' && *name
== '\0');
648 static void check(pid_t pid
)
650 if (execname
&& !pid_is_exec(pid
, &exec_stat
))
652 if (userspec
&& !pid_is_user(pid
, user_id
))
654 if (cmdname
&& !pid_is_cmd(pid
, cmdname
))
659 static void do_pidfile(const char *name
)
664 f
= fopen(name
, "r");
666 if (fscanf(f
, "%ld", &pid
) == 1)
669 } else if (errno
!= ENOENT
)
670 fatal("open pidfile %s: %s", name
, strerror(errno
));
673 /* WTA: this needs to be an autoconf check for /proc/pid existance.
675 static void do_procinit(void)
678 struct dirent
*entry
;
682 procdir
= opendir("/proc");
684 fatal("opendir /proc: %s", strerror(errno
));
687 while ((entry
= readdir(procdir
)) != NULL
) {
688 if (sscanf(entry
->d_name
, "%ld", &pid
) != 1)
695 fatal("nothing in /proc - not mounted?");
698 static void do_findprocs(void)
708 /* return 1 on failure */
709 static void do_stop(int signal_nr
, int quietmode
, int *n_killed
,
710 int *n_notkilled
, int retry_nr
)
724 for (p
= found
; p
; p
= p
->next
) {
726 printf("Would send signal %d to %ld.\n", signal_nr
,
728 else if (kill(p
->pid
, signal_nr
) == 0) {
729 push(&killed
, p
->pid
);
732 printf("%s: warning: failed to kill %ld: %s\n",
733 progname
, (long)p
->pid
, strerror(errno
));
737 if (quietmode
< 0 && killed
) {
738 printf("Stopped %s (pid", what_stop
);
739 for (p
= killed
; p
; p
= p
->next
)
740 printf(" %ld", (long)p
->pid
);
743 printf(", retry #%d", retry_nr
);
749 static void set_what_stop(const char *str
)
751 strncpy(what_stop
, str
, sizeof(what_stop
));
752 what_stop
[sizeof(what_stop
) - 1] = '\0';
755 static int run_stop_schedule(void)
757 int r
, position
, n_killed
, n_notkilled
, value
, ratio
, anykilled
,
759 struct timeval stopat
, before
, after
, interval
, maxinterval
;
762 if (schedule
!= NULL
) {
763 printf("Ignoring --retry in test mode\n");
769 set_what_stop(cmdname
);
771 set_what_stop(execname
);
773 sprintf(what_stop
, "process in pidfile `%.200s'", pidfile
);
775 sprintf(what_stop
, "process(es) owned by `%.200s'", userspec
);
777 fatal("internal error, please report");
783 if (schedule
== NULL
) {
784 do_stop(signal_nr
, quietmode
, &n_killed
, &n_notkilled
, 0);
785 if (n_notkilled
> 0 && quietmode
<= 0)
786 printf("%d pids were not killed\n", n_notkilled
);
792 for (position
= 0; position
< schedule_length
;) {
793 value
= schedule
[position
].value
;
796 switch (schedule
[position
].type
) {
803 do_stop(value
, quietmode
, &n_killed
, &n_notkilled
,
812 /* We want to keep polling for the processes, to see if
814 * or until the timeout expires.
816 * This is a somewhat complicated algorithm to try to
818 * notice reasonably quickly when all the processes have
820 * don't spend too much CPU time polling. In
821 * particular, on a fast
822 * machine with quick-exiting daemons we don't want to
824 * shutdown too much, whereas on a slow one, or where
826 * taking some time to exit, we want to increase the
830 * The algorithm is as follows: we measure the elapsed
832 * to do one poll(), and wait a multiple of this time
834 * poll. However, if that would put us past the end of
836 * period we wait only as long as the timeout period,
838 * we always wait at least MIN_POLL_INTERVAL (20ms).
840 * (`ratio') starts out as 2, and increases by 1 for
842 * maximum of 10; so we use up to between 30% and 10% of
844 * machine's resources (assuming a few reasonable things
848 xgettimeofday(&stopat
);
849 stopat
.tv_sec
+= value
;
852 xgettimeofday(&before
);
853 if (timercmp(&before
, &stopat
, >))
856 do_stop(0, 1, &n_killed
, &n_notkilled
, 0);
860 xgettimeofday(&after
);
862 if (!timercmp(&after
, &stopat
, <))
869 ratio
* (TVELEM(&after
) - TVELEM(&before
)
872 TVELEM(&stopat
) - TVELEM(&after
)
875 if (timercmp(&interval
, &maxinterval
, >))
876 interval
= maxinterval
;
878 if (interval
.tv_sec
== 0
879 && interval
.tv_usec
<= MIN_POLL_INTERVAL
)
880 interval
.tv_usec
= MIN_POLL_INTERVAL
;
882 r
= select(0, 0, 0, 0, &interval
);
883 if (r
< 0 && errno
!= EINTR
)
884 fatal("select() failed for pause: %s",
889 assert(!"schedule[].type value must be valid");
897 printf("Program %s, %d process(es), refused to die.\n",
898 what_stop
, n_killed
);
905 printf("No %s found running; none killed.\n",
914 int main(int argc, char **argv) NONRETURNING;
917 int main(int argc
, char **argv
)
921 LIST_INIT(&namespace_head
);
923 parse_options(argc
, argv
);
927 if (execname
&& stat(execname
, &exec_stat
))
928 fatal("stat %s: %s", execname
, strerror(errno
));
930 if (userspec
&& sscanf(userspec
, "%d", &user_id
) != 1) {
933 pw
= getpwnam(userspec
);
935 fatal("user `%s' not found\n", userspec
);
937 user_id
= pw
->pw_uid
;
940 if (changegroup
&& sscanf(changegroup
, "%d", &runas_gid
) != 1) {
941 struct group
*gr
= getgrnam(changegroup
);
943 fatal("group `%s' not found\n", changegroup
);
944 runas_gid
= gr
->gr_gid
;
946 if (changeuser
&& sscanf(changeuser
, "%d", &runas_uid
) != 1) {
947 struct passwd
*pw
= getpwnam(changeuser
);
949 fatal("user `%s' not found\n", changeuser
);
950 runas_uid
= pw
->pw_uid
;
952 == NULL
) { /* pass the default group of this user */
953 changegroup
= ""; /* just empty */
954 runas_gid
= pw
->pw_gid
;
959 int i
= run_stop_schedule();
967 printf("%s already running.\n", execname
);
971 printf("Would start %s ", startas
);
973 printf("%s ", *argv
++);
974 if (changeuser
!= NULL
) {
975 printf(" (as user %s[%d]", changeuser
, runas_uid
);
976 if (changegroup
!= NULL
)
977 printf(", and group %s[%d])", changegroup
,
982 if (changeroot
!= NULL
)
983 printf(" in directory %s", changeroot
);
985 printf(", and add %i to the priority", nicelevel
);
990 printf("Starting %s...\n", startas
);
992 if (changeroot
!= NULL
) {
993 if (chdir(changeroot
) < 0)
994 fatal("Unable to chdir() to %s", changeroot
);
995 if (chroot(changeroot
) < 0)
996 fatal("Unable to chroot() to %s", changeroot
);
998 if (changeuser
!= NULL
) {
999 if (setgid(runas_gid
))
1000 fatal("Unable to set gid to %d", runas_gid
);
1001 if (initgroups(changeuser
, runas_gid
))
1002 fatal("Unable to set initgroups() with gid %d",
1004 if (setuid(runas_uid
))
1005 fatal("Unable to set uid to %s", changeuser
);
1008 if (background
) { /* ok, we need to detach this process */
1011 printf("Detaching to start %s...", startas
);
1014 fatal("Unable to fork.\n");
1016 if (i
) { /* parent */
1021 /* child continues here */
1022 /* now close all extra fds */
1023 for (i
= getdtablesize() - 1; i
>= 0; --i
)
1026 fd
= open("/dev/tty", O_RDWR
);
1028 if (ioctl(fd
, TIOCNOTTY
, 0) < 0)
1029 printf("ioctl TIOCNOTTY failed: %s\n",
1034 umask(022); /* set a default for dumb programs */
1035 setpgid(0, 0); /* set the process group */
1036 fd
= open("/dev/null", O_RDWR
); /* stdin */
1038 dup(fd
); /* stdout */
1039 dup(fd
); /* stderr */
1044 if (nice(nicelevel
) < 0 && errno
)
1045 fatal("Unable to alter nice level by %i: %s", nicelevel
,
1049 && pidfile
!= NULL
) { /* user wants _us_ to make the pidfile :) */
1050 FILE *pidf
= fopen(pidfile
, "w");
1051 pid_t pidt
= getpid();
1053 fatal("Unable to open pidfile `%s' for writing: %s",
1054 pidfile
, strerror(errno
));
1055 fprintf(pidf
, "%ld\n", (long)pidt
);
1059 execv(startas
, argv
);
1060 fatal("Unable to start %s: %s", startas
, strerror(errno
));