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.
34 #define VERSION "1.9.18"
36 #define MIN_POLL_INTERVAL 20000 /*us*/
47 #include <sys/queue.h>
52 #include <sys/ioctl.h>
53 #include <sys/types.h>
60 #include <linux/sched.h>
63 static int testmode
= 0;
64 static int quietmode
= 0;
65 static int exitnodo
= 1;
68 static int background
= 0;
69 static int mpidfile
= 0;
70 static int signal_nr
= 15;
71 static const char *signal_str
= NULL
;
72 static int user_id
= -1;
73 static int runas_uid
= -1;
74 static int runas_gid
= -1;
75 static const char *userspec
= NULL
;
76 static char *changeuser
= NULL
;
77 static const char *changegroup
= NULL
;
78 static char *changeroot
= NULL
;
79 static const char *cmdname
= NULL
;
80 static char *execname
= NULL
;
81 static char *startas
= NULL
;
82 static const char *pidfile
= NULL
;
83 static char what_stop
[1024];
84 static const char *schedule_str
= NULL
;
85 static const char *progname
= "";
86 static int nicelevel
= 0;
88 static struct stat exec_stat
;
91 struct pid_list
*next
;
95 static struct pid_list
*found
= NULL
;
96 static struct pid_list
*killed
= NULL
;
98 struct schedule_item
{
99 enum { sched_timeout
, sched_signal
, sched_goto
, sched_forever
} type
;
100 int value
; /* seconds, signal no., or index into array */
101 /* sched_forever is only seen within parse_schedule and callees */
104 static int schedule_length
;
105 static struct schedule_item
*schedule
= NULL
;
107 LIST_HEAD(namespace_head
, namespace);
110 LIST_ENTRY(namespace) list
;
115 static struct namespace_head namespace_head
;
117 static void *xmalloc(int size
);
118 static void push(struct pid_list
**list
, pid_t pid
);
119 static void do_help(void);
120 static void parse_options(int argc
, char * const *argv
);
121 static int pid_is_user(pid_t pid
, uid_t uid
);
122 static int pid_is_cmd(pid_t pid
, const char *name
);
123 static void check(pid_t pid
);
124 static void do_pidfile(const char *name
);
125 static void do_stop(int signal_nr
, int quietmode
,
126 int *n_killed
, int *n_notkilled
, int retry_nr
);
127 static int pid_is_exec(pid_t pid
, const struct stat
*esb
);
130 static void fatal(const char *format
, ...)
131 __attribute__((noreturn
, format(printf
, 1, 2)));
132 static void badusage(const char *msg
)
133 __attribute__((noreturn
));
135 static void fatal(const char *format
, ...);
136 static void badusage(const char *msg
);
139 /* This next part serves only to construct the TVCALC macro, which
140 * is used for doing arithmetic on struct timeval's. It works like this:
141 * TVCALC(result, expression);
142 * where result is a struct timeval (and must be an lvalue) and
143 * expression is the single expression for both components. In this
144 * expression you can use the special values TVELEM, which when fed a
145 * const struct timeval* gives you the relevant component, and
146 * TVADJUST. TVADJUST is necessary when subtracting timevals, to make
147 * it easier to renormalise. Whenver you subtract timeval elements,
148 * you must make sure that TVADJUST is added to the result of the
149 * subtraction (before any resulting multiplication or what have you).
150 * TVELEM must be linear in TVADJUST.
152 typedef long tvselector(const struct timeval
*);
153 static long tvselector_sec(const struct timeval
*tv
) { return tv
->tv_sec
; }
154 static long tvselector_usec(const struct timeval
*tv
) { return tv
->tv_usec
; }
155 #define TVCALC_ELEM(result, expr, sec, adj) \
157 const long TVADJUST = adj; \
158 long (*const TVELEM)(const struct timeval*) = tvselector_##sec; \
159 (result).tv_##sec = (expr); \
161 #define TVCALC(result,expr) \
163 TVCALC_ELEM(result, expr, sec, (-1)); \
164 TVCALC_ELEM(result, expr, usec, (+1000000)); \
165 (result).tv_sec += (result).tv_usec / 1000000; \
166 (result).tv_usec %= 1000000; \
171 fatal(const char *format
, ...)
175 fprintf(stderr
, "%s: ", progname
);
176 va_start(arglist
, format
);
177 vfprintf(stderr
, format
, arglist
);
192 fatal("malloc(%d) failed", size
);
196 xgettimeofday(struct timeval
*tv
)
198 if (gettimeofday(tv
,0) != 0)
199 fatal("gettimeofday failed: %s", strerror(errno
));
203 push(struct pid_list
**list
, pid_t pid
)
207 p
= xmalloc(sizeof(*p
));
214 clear(struct pid_list
**list
)
216 struct pid_list
*here
, *next
;
218 for (here
= *list
; here
!= NULL
; here
= next
) {
228 next_dirname(const char *s
)
232 cur
= (const char *)s
;
235 for (; *cur
!= '/'; ++cur
)
239 for (; *cur
== '/'; ++cur
)
247 add_namespace(const char *path
)
250 const char *nsdirname
, *nsname
, *cur
;
251 struct namespace *namespace;
253 cur
= (const char *)path
;
254 nsdirname
= nsname
= "";
256 while ((cur
= next_dirname(cur
))[0] != '\0') {
261 if (!memcmp(nsdirname
, "ipcns/", strlen("ipcns/")))
262 nstype
= CLONE_NEWIPC
;
263 else if (!memcmp(nsdirname
, "netns/", strlen("netns/")))
264 nstype
= CLONE_NEWNET
;
265 else if (!memcmp(nsdirname
, "utcns/", strlen("utcns/")))
266 nstype
= CLONE_NEWUTS
;
268 badusage("invalid namepspace path");
270 namespace = xmalloc(sizeof(*namespace));
271 namespace->path
= (const char *)path
;
272 namespace->nstype
= nstype
;
273 LIST_INSERT_HEAD(&namespace_head
, namespace, list
);
281 struct namespace *namespace;
284 LIST_FOREACH(namespace, &namespace_head
, list
) {
285 if ((fd
= open(namespace->path
, O_RDONLY
)) == -1)
286 fatal("open namespace %s: %s", namespace->path
, strerror(errno
));
287 if (setns(fd
, namespace->nstype
) == -1)
288 fatal("setns %s: %s", namespace->path
, strerror(errno
));
295 if (!LIST_EMPTY(&namespace_head
))
296 fatal("LCX namespaces not supported");
304 "start-stop-daemon " VERSION
" for Debian - small and fast C version written by\n"
305 "Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
308 " start-stop-daemon -S|--start options ... -- arguments ...\n"
309 " start-stop-daemon -K|--stop options ...\n"
310 " start-stop-daemon -H|--help\n"
311 " start-stop-daemon -V|--version\n"
313 "Options (at least one of --exec|--pidfile|--user is required):\n"
314 " -x|--exec <executable> program to start/check if it is running\n"
315 " -p|--pidfile <pid-file> pid file to check\n"
316 " -c|--chuid <name|uid[:group|gid]>\n"
317 " change to this user/group before starting process\n"
318 " -u|--user <username>|<uid> stop processes owned by this user\n"
319 " -n|--name <process-name> stop processes with this name\n"
320 " -s|--signal <signal> signal to send (default TERM)\n"
321 " -a|--startas <pathname> program to start (default is <executable>)\n"
322 " -N|--nicelevel <incr> add incr to the process's nice level\n"
323 " -b|--background force the process to detach\n"
324 " -m|--make-pidfile create the pidfile before starting\n"
325 " -R|--retry <schedule> check whether processes die, and retry\n"
326 " -t|--test test mode, don't do anything\n"
327 " -o|--oknodo exit status 0 (not 1) if nothing done\n"
328 " -q|--quiet be more quiet\n"
329 " -v|--verbose be more verbose\n"
330 "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
331 " -<signal-num>|[-]<signal-name> send that signal\n"
332 " <timeout> wait that many seconds\n"
333 " forever repeat remainder forever\n"
334 "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
336 "Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo)\n"
337 " 3 = trouble 2 = with --retry, processes wouldn't die\n");
342 badusage(const char *msg
)
345 fprintf(stderr
, "%s: %s\n", progname
, msg
);
346 fprintf(stderr
, "Try `%s --help' for more information.\n", progname
);
355 const struct sigpair siglist
[] = {
377 static int parse_integer (const char *string
, int *value_r
) {
384 ul
= strtoul(string
,&ep
,10);
385 if (ul
> INT_MAX
|| *ep
!= '\0')
392 static int parse_signal (const char *signal_str
, int *signal_nr
)
396 if (parse_integer(signal_str
, signal_nr
) == 0)
399 for (i
= 0; i
< sizeof (siglist
) / sizeof (siglist
[0]); i
++) {
400 if (strcmp (signal_str
, siglist
[i
].name
) == 0) {
401 *signal_nr
= siglist
[i
].signal
;
409 parse_schedule_item(const char *string
, struct schedule_item
*item
) {
410 const char *after_hyph
;
412 if (!strcmp(string
,"forever")) {
413 item
->type
= sched_forever
;
414 } else if (isdigit(string
[0])) {
415 item
->type
= sched_timeout
;
416 if (parse_integer(string
, &item
->value
) != 0)
417 badusage("invalid timeout value in schedule");
418 } else if ((after_hyph
= string
+ (string
[0] == '-')) &&
419 parse_signal(after_hyph
, &item
->value
) == 0) {
420 item
->type
= sched_signal
;
422 badusage("invalid schedule item (must be [-]<signal-name>, "
423 "-<signal-number>, <timeout> or `forever'");
428 parse_schedule(const char *schedule_str
) {
435 for (slash
= schedule_str
; *slash
; slash
++)
439 schedule_length
= (count
== 0) ? 4 : count
+1;
440 schedule
= xmalloc(sizeof(*schedule
) * schedule_length
);
443 schedule
[0].type
= sched_signal
;
444 schedule
[0].value
= signal_nr
;
445 parse_schedule_item(schedule_str
, &schedule
[1]);
446 if (schedule
[1].type
!= sched_timeout
) {
447 badusage ("--retry takes timeout, or schedule list"
448 " of at least two items");
450 schedule
[2].type
= sched_signal
;
451 schedule
[2].value
= SIGKILL
;
452 schedule
[3]= schedule
[1];
456 while (schedule_str
!= NULL
) {
457 slash
= strchr(schedule_str
,'/');
458 str_len
= slash
? slash
- schedule_str
: (ptrdiff_t)strlen(schedule_str
);
459 if (str_len
>= (ptrdiff_t)sizeof(item_buf
))
460 badusage("invalid schedule item: far too long"
461 " (you must delimit items with slashes)");
462 memcpy(item_buf
, schedule_str
, str_len
);
463 item_buf
[str_len
] = 0;
464 schedule_str
= slash
? slash
+1 : NULL
;
466 parse_schedule_item(item_buf
, &schedule
[count
]);
467 if (schedule
[count
].type
== sched_forever
) {
469 badusage("invalid schedule: `forever'"
470 " appears more than once");
477 schedule
[count
].type
= sched_goto
;
478 schedule
[count
].value
= repeatat
;
481 assert(count
== schedule_length
);
486 parse_options(int argc
, char * const *argv
)
488 static struct option longopts
[] = {
489 { "help", 0, NULL
, 'H'},
490 { "stop", 0, NULL
, 'K'},
491 { "start", 0, NULL
, 'S'},
492 { "version", 0, NULL
, 'V'},
493 { "startas", 1, NULL
, 'a'},
494 { "name", 1, NULL
, 'n'},
495 { "oknodo", 0, NULL
, 'o'},
496 { "pidfile", 1, NULL
, 'p'},
497 { "quiet", 0, NULL
, 'q'},
498 { "signal", 1, NULL
, 's'},
499 { "test", 0, NULL
, 't'},
500 { "user", 1, NULL
, 'u'},
501 { "chroot", 1, NULL
, 'r'},
502 { "namespace", 1, NULL
, 'd'},
503 { "verbose", 0, NULL
, 'v'},
504 { "exec", 1, NULL
, 'x'},
505 { "chuid", 1, NULL
, 'c'},
506 { "nicelevel", 1, NULL
, 'N'},
507 { "background", 0, NULL
, 'b'},
508 { "make-pidfile", 0, NULL
, 'm'},
509 { "retry", 1, NULL
, 'R'},
515 c
= getopt_long(argc
, argv
, "HKSVa:n:op:qr:d:s:tu:vx:c:N:bmR:",
516 longopts
, (int *) 0);
520 case 'H': /* --help */
523 case 'K': /* --stop */
526 case 'S': /* --start */
529 case 'V': /* --version */
530 printf("start-stop-daemon " VERSION
"\n");
532 case 'a': /* --startas <pathname> */
535 case 'n': /* --name <process-name> */
538 case 'o': /* --oknodo */
541 case 'p': /* --pidfile <pid-file> */
544 case 'q': /* --quiet */
547 case 's': /* --signal <signal> */
550 case 't': /* --test */
553 case 'u': /* --user <username>|<uid> */
556 case 'v': /* --verbose */
559 case 'x': /* --exec <executable> */
562 case 'c': /* --chuid <username>|<uid> */
563 /* we copy the string just in case we need the
565 changeuser
= strdup(optarg
);
566 changeuser
= strtok(changeuser
, ":");
567 changegroup
= strtok(NULL
, ":");
569 case 'r': /* --chroot /new/root */
572 case 'd': /* --namespace /.../<ipcns>|<netns>|<utsns>/name */
574 add_namespace(optarg
);
577 case 'N': /* --nice */
578 nicelevel
= atoi(optarg
);
580 case 'b': /* --background */
583 case 'm': /* --make-pidfile */
586 case 'R': /* --retry <schedule>|<timeout> */
587 schedule_str
= optarg
;
590 badusage(NULL
); /* message printed by getopt */
594 if (signal_str
!= NULL
) {
595 if (parse_signal (signal_str
, &signal_nr
) != 0)
596 badusage("signal value must be numeric or name"
597 " of signal (KILL, INTR, ...)");
600 if (schedule_str
!= NULL
) {
601 parse_schedule(schedule_str
);
605 badusage("need one of --start or --stop");
607 if (!execname
&& !pidfile
&& !userspec
&& !cmdname
)
608 badusage("need at least one of --exec, --pidfile, --user or --name");
613 if (start
&& !startas
)
614 badusage("--start needs --exec or --startas");
616 if (mpidfile
&& pidfile
== NULL
)
617 badusage("--make-pidfile is only relevant with --pidfile");
619 if (background
&& !start
)
620 badusage("--background is only relevant with --start");
625 pid_is_exec(pid_t pid
, const struct stat
*esb
)
630 sprintf(buf
, "/proc/%d/exe", pid
);
631 if (stat(buf
, &sb
) != 0)
633 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
638 pid_is_user(pid_t pid
, uid_t uid
)
643 sprintf(buf
, "/proc/%d", pid
);
644 if (stat(buf
, &sb
) != 0)
646 return (sb
.st_uid
== uid
);
651 pid_is_cmd(pid_t pid
, const char *name
)
657 sprintf(buf
, "/proc/%d/stat", pid
);
661 while ((c
= getc(f
)) != EOF
&& c
!= '(')
667 /* this hopefully handles command names containing ')' */
668 while ((c
= getc(f
)) != EOF
&& c
== *name
)
671 return (c
== ')' && *name
== '\0');
678 if (execname
&& !pid_is_exec(pid
, &exec_stat
))
680 if (userspec
&& !pid_is_user(pid
, user_id
))
682 if (cmdname
&& !pid_is_cmd(pid
, cmdname
))
688 do_pidfile(const char *name
)
693 f
= fopen(name
, "r");
695 if (fscanf(f
, "%d", &pid
) == 1)
698 } else if (errno
!= ENOENT
)
699 fatal("open pidfile %s: %s", name
, strerror(errno
));
703 /* WTA: this needs to be an autoconf check for /proc/pid existance.
709 struct dirent
*entry
;
713 procdir
= opendir("/proc");
715 fatal("opendir /proc: %s", strerror(errno
));
718 while ((entry
= readdir(procdir
)) != NULL
) {
719 if (sscanf(entry
->d_name
, "%d", &pid
) != 1)
726 fatal("nothing in /proc - not mounted?");
740 /* return 1 on failure */
742 do_stop(int signal_nr
, int quietmode
, int *n_killed
, int *n_notkilled
, int retry_nr
)
756 for (p
= found
; p
; p
= p
->next
) {
758 printf("Would send signal %d to %d.\n",
760 else if (kill(p
->pid
, signal_nr
) == 0) {
761 push(&killed
, p
->pid
);
764 printf("%s: warning: failed to kill %d: %s\n",
765 progname
, p
->pid
, strerror(errno
));
769 if (quietmode
< 0 && killed
) {
770 printf("Stopped %s (pid", what_stop
);
771 for (p
= killed
; p
; p
= p
->next
)
772 printf(" %d", p
->pid
);
775 printf(", retry #%d", retry_nr
);
782 set_what_stop(const char *str
)
784 strncpy(what_stop
, str
, sizeof(what_stop
));
785 what_stop
[sizeof(what_stop
)-1] = '\0';
789 run_stop_schedule(void)
791 int r
, position
, n_killed
, n_notkilled
, value
, ratio
, anykilled
, retry_nr
;
792 struct timeval stopat
, before
, after
, interval
, maxinterval
;
795 if (schedule
!= NULL
) {
796 printf("Ignoring --retry in test mode\n");
802 set_what_stop(cmdname
);
804 set_what_stop(execname
);
806 sprintf(what_stop
, "process in pidfile `%.200s'", pidfile
);
808 sprintf(what_stop
, "process(es) owned by `%.200s'", userspec
);
810 fatal("internal error, please report");
816 if (schedule
== NULL
) {
817 do_stop(signal_nr
, quietmode
, &n_killed
, &n_notkilled
, 0);
818 if (n_notkilled
> 0 && quietmode
<= 0)
819 printf("%d pids were not killed\n", n_notkilled
);
825 for (position
= 0; position
< schedule_length
; ) {
826 value
= schedule
[position
].value
;
829 switch (schedule
[position
].type
) {
836 do_stop(value
, quietmode
, &n_killed
, &n_notkilled
, retry_nr
++);
844 /* We want to keep polling for the processes, to see if they've exited,
845 * or until the timeout expires.
847 * This is a somewhat complicated algorithm to try to ensure that we
848 * notice reasonably quickly when all the processes have exited, but
849 * don't spend too much CPU time polling. In particular, on a fast
850 * machine with quick-exiting daemons we don't want to delay system
851 * shutdown too much, whereas on a slow one, or where processes are
852 * taking some time to exit, we want to increase the polling
855 * The algorithm is as follows: we measure the elapsed time it takes
856 * to do one poll(), and wait a multiple of this time for the next
857 * poll. However, if that would put us past the end of the timeout
858 * period we wait only as long as the timeout period, but in any case
859 * we always wait at least MIN_POLL_INTERVAL (20ms). The multiple
860 * (`ratio') starts out as 2, and increases by 1 for each poll to a
861 * maximum of 10; so we use up to between 30% and 10% of the
862 * machine's resources (assuming a few reasonable things about system
865 xgettimeofday(&stopat
);
866 stopat
.tv_sec
+= value
;
869 xgettimeofday(&before
);
870 if (timercmp(&before
,&stopat
,>))
873 do_stop(0, 1, &n_killed
, &n_notkilled
, 0);
877 xgettimeofday(&after
);
879 if (!timercmp(&after
,&stopat
,<))
885 TVCALC(interval
, ratio
* (TVELEM(&after
) - TVELEM(&before
) + TVADJUST
));
886 TVCALC(maxinterval
, TVELEM(&stopat
) - TVELEM(&after
) + TVADJUST
);
888 if (timercmp(&interval
,&maxinterval
,>))
889 interval
= maxinterval
;
891 if (interval
.tv_sec
== 0 &&
892 interval
.tv_usec
<= MIN_POLL_INTERVAL
)
893 interval
.tv_usec
= MIN_POLL_INTERVAL
;
895 r
= select(0,0,0,0,&interval
);
896 if (r
< 0 && errno
!= EINTR
)
897 fatal("select() failed for pause: %s",
902 assert(!"schedule[].type value must be valid");
911 printf("Program %s, %d process(es), refused to die.\n",
912 what_stop
, n_killed
);
919 printf("No %s found running; none killed.\n", what_stop
);
927 int main(int argc, char **argv) NONRETURNING;
931 main(int argc
, char **argv
)
935 LIST_INIT(&namespace_head
);
937 parse_options(argc
, argv
);
941 if (execname
&& stat(execname
, &exec_stat
))
942 fatal("stat %s: %s", execname
, strerror(errno
));
944 if (userspec
&& sscanf(userspec
, "%d", &user_id
) != 1) {
947 pw
= getpwnam(userspec
);
949 fatal("user `%s' not found\n", userspec
);
951 user_id
= pw
->pw_uid
;
954 if (changegroup
&& sscanf(changegroup
, "%d", &runas_gid
) != 1) {
955 struct group
*gr
= getgrnam(changegroup
);
957 fatal("group `%s' not found\n", changegroup
);
958 runas_gid
= gr
->gr_gid
;
960 if (changeuser
&& sscanf(changeuser
, "%d", &runas_uid
) != 1) {
961 struct passwd
*pw
= getpwnam(changeuser
);
963 fatal("user `%s' not found\n", changeuser
);
964 runas_uid
= pw
->pw_uid
;
965 if (changegroup
== NULL
) { /* pass the default group of this user */
966 changegroup
= ""; /* just empty */
967 runas_gid
= pw
->pw_gid
;
972 int i
= run_stop_schedule();
980 printf("%s already running.\n", execname
);
984 printf("Would start %s ", startas
);
986 printf("%s ", *argv
++);
987 if (changeuser
!= NULL
) {
988 printf(" (as user %s[%d]", changeuser
, runas_uid
);
989 if (changegroup
!= NULL
)
990 printf(", and group %s[%d])", changegroup
, runas_gid
);
994 if (changeroot
!= NULL
)
995 printf(" in directory %s", changeroot
);
997 printf(", and add %i to the priority", nicelevel
);
1002 printf("Starting %s...\n", startas
);
1004 if (changeroot
!= NULL
) {
1005 if (chdir(changeroot
) < 0)
1006 fatal("Unable to chdir() to %s", changeroot
);
1007 if (chroot(changeroot
) < 0)
1008 fatal("Unable to chroot() to %s", changeroot
);
1010 if (changeuser
!= NULL
) {
1011 if (setgid(runas_gid
))
1012 fatal("Unable to set gid to %d", runas_gid
);
1013 if (initgroups(changeuser
, runas_gid
))
1014 fatal("Unable to set initgroups() with gid %d", runas_gid
);
1015 if (setuid(runas_uid
))
1016 fatal("Unable to set uid to %s", changeuser
);
1019 if (background
) { /* ok, we need to detach this process */
1022 printf("Detatching to start %s...", startas
);
1025 fatal("Unable to fork.\n");
1027 if (i
) { /* parent */
1032 /* child continues here */
1033 /* now close all extra fds */
1034 for (i
=getdtablesize()-1; i
>=0; --i
) close(i
);
1036 fd
= open("/dev/tty", O_RDWR
);
1037 ioctl(fd
, TIOCNOTTY
, 0);
1040 umask(022); /* set a default for dumb programs */
1041 setpgid(0,0); /* set the process group */
1042 fd
=open("/dev/null", O_RDWR
); /* stdin */
1043 dup(fd
); /* stdout */
1044 dup(fd
); /* stderr */
1048 if (nice(nicelevel
) < 0 && errno
)
1049 fatal("Unable to alter nice level by %i: %s", nicelevel
,
1052 if (mpidfile
&& pidfile
!= NULL
) { /* user wants _us_ to make the pidfile :) */
1053 FILE *pidf
= fopen(pidfile
, "w");
1054 pid_t pidt
= getpid();
1056 fatal("Unable to open pidfile `%s' for writing: %s", pidfile
,
1058 fprintf(pidf
, "%d\n", pidt
);
1062 execv(startas
, argv
);
1063 fatal("Unable to start %s: %s", startas
, strerror(errno
));