/[baselayout]/trunk/src/start-stop-daemon.c
Gentoo

Contents of /trunk/src/start-stop-daemon.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2587 - (show annotations) (download) (as text)
Fri Apr 13 09:21:16 2007 UTC (11 years, 5 months ago) by uberlord
File MIME type: text/x-csrc
File size: 23283 byte(s)
s-s-d now allows --signal to be used without --stop and --oknodo. man page updated accordingly
1 /*
2 start-stop-daemon
3 Starts, stops, tests and signals daemons
4 Copyright 2007 Gentoo Foundation
5 Released under the GPLv2
6
7 This is essentially a ground up re-write of Debians
8 start-stop-daemon for cleaner code and to integrate into our RC
9 system so we can monitor daemons a little.
10 */
11
12 #define POLL_INTERVAL 20000
13 #define START_WAIT 100000
14
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/resource.h>
18 #include <sys/stat.h>
19 #include <sys/termios.h>
20 #include <sys/time.h>
21 #include <sys/wait.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <getopt.h>
26 #include <grp.h>
27 #include <pwd.h>
28 #include <signal.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #ifdef HAVE_PAM
36 #include <security/pam_appl.h>
37
38 /* We are not supporting authentication conversations */
39 static struct pam_conv conv = { NULL, NULL} ;
40 #endif
41
42 #include "einfo.h"
43 #include "rc.h"
44 #include "rc-misc.h"
45 #include "strlist.h"
46
47 typedef struct schedulelist
48 {
49 enum
50 {
51 schedule_timeout,
52 schedule_signal,
53 schedule_goto,
54 schedule_forever
55 } type;
56 int value;
57 struct schedulelist *gotolist;
58 struct schedulelist *next;
59 } schedulelist_t;
60 static schedulelist_t *schedule;
61
62 static char *progname;
63 static char *changeuser;
64 static char **newenv;
65
66 extern char **environ;
67
68 static void free_schedulelist (schedulelist_t **list)
69 {
70 schedulelist_t *here;
71 schedulelist_t *next;
72
73 for (here = *list; here; here = next) {
74 next = here->next;
75 free (here);
76 }
77
78 *list = NULL;
79 }
80
81 static void cleanup (void)
82 {
83 if (changeuser)
84 free (changeuser);
85
86 if (schedule)
87 free_schedulelist (&schedule);
88
89 if (newenv)
90 rc_strlist_free (newenv);
91 }
92
93 static int parse_signal (const char *sig)
94 {
95 typedef struct signalpair
96 {
97 const char *name;
98 int signal;
99 } signalpair_t;
100
101 static const signalpair_t signallist[] = {
102 { "ABRT", SIGABRT },
103 { "ALRM", SIGALRM },
104 { "FPE", SIGFPE },
105 { "HUP", SIGHUP },
106 { "ILL", SIGILL },
107 { "INT", SIGINT },
108 { "KILL", SIGKILL },
109 { "PIPE", SIGPIPE },
110 { "QUIT", SIGQUIT },
111 { "SEGV", SIGSEGV },
112 { "TERM", SIGTERM },
113 { "USR1", SIGUSR1 },
114 { "USR2", SIGUSR2 },
115 { "CHLD", SIGCHLD },
116 { "CONT", SIGCONT },
117 { "STOP", SIGSTOP },
118 { "TSTP", SIGTSTP },
119 { "TTIN", SIGTTIN },
120 { "TTOU", SIGTTOU }
121 };
122
123 unsigned int i = 0;
124 char *s;
125
126 if (! sig || strlen (sig) == 0)
127 return (-1);
128
129 if (sscanf (sig, "%u", &i) == 1) {
130 if (i > 0 && i < sizeof (signallist) / sizeof (signallist[0]))
131 return (i);
132 eerrorx ("%s: `%s' is not a valid signal", progname, sig);
133 }
134
135 if (strncmp (sig, "SIG", 3) == 0)
136 s = (char *) sig + 3;
137 else
138 s = NULL;
139
140 for (i = 0; i < sizeof (signallist) / sizeof (signallist[0]); i++)
141 if (strcmp (sig, signallist[i].name) == 0 ||
142 (s && strcmp (s, signallist[i].name) == 0))
143 return (signallist[i].signal);
144
145 eerrorx ("%s: `%s' is not a valid signal", progname, sig);
146 }
147
148 static void parse_schedule_item (schedulelist_t *item, const char *string)
149 {
150 const char *after_hyph;
151 int sig;
152
153 if (strcmp (string,"forever") == 0)
154 item->type = schedule_forever;
155 else if (isdigit (string[0])) {
156 item->type = schedule_timeout;
157 errno = 0;
158 if (sscanf (string, "%d", &item->value) != 1)
159 eerrorx ("%s: invalid timeout value in schedule `%s'", progname,
160 string);
161 } else if ((after_hyph = string + (string[0] == '-')) &&
162 ((sig = parse_signal (after_hyph)) != -1))
163 {
164 item->type = schedule_signal;
165 item->value = (int) sig;
166 }
167 else
168 eerrorx ("%s: invalid schedule item `%s'", progname, string);
169 }
170
171 static void parse_schedule (const char *string, int default_signal)
172 {
173 char buffer[20];
174 const char *slash;
175 int count = 0;
176 schedulelist_t *repeatat = NULL;
177 ptrdiff_t len;
178 schedulelist_t *next;
179
180 if (string)
181 for (slash = string; *slash; slash++)
182 if (*slash == '/')
183 count++;
184
185 if (schedule)
186 free_schedulelist (&schedule);
187
188 schedule = rc_xmalloc (sizeof (schedulelist_t));
189 schedule->gotolist = NULL;
190
191 if (count == 0) {
192 schedule->type = schedule_signal;
193 schedule->value = default_signal;
194 schedule->next = rc_xmalloc (sizeof (schedulelist_t));
195 next = schedule->next;
196 next->type = schedule_timeout;
197 next->gotolist = NULL;
198 if (string) {
199 if (sscanf (string, "%d", &next->value) != 1)
200 eerrorx ("%s: invalid timeout value in schedule", progname);
201 }
202 else
203 next->value = 5;
204 next->next = NULL;
205
206 return;
207 }
208
209 next = schedule;
210 while (string != NULL) {
211 if ((slash = strchr (string, '/')))
212 len = slash - string;
213 else
214 len = strlen (string);
215
216 if (len >= (ptrdiff_t) sizeof (buffer))
217 eerrorx ("%s: invalid schedule item, far too long", progname);
218
219 memcpy (buffer, string, len);
220 buffer[len] = 0;
221 string = slash ? slash + 1 : NULL;
222
223 parse_schedule_item (next, buffer);
224 if (next->type == schedule_forever) {
225 if (repeatat)
226 eerrorx ("%s: invalid schedule, `forever' appears more than once",
227 progname);
228
229 repeatat = next;
230 continue;
231 }
232
233 if (string) {
234 next->next = rc_xmalloc (sizeof (schedulelist_t));
235 next = next->next;
236 next->gotolist = NULL;
237 }
238 }
239
240 if (repeatat) {
241 next->next = rc_xmalloc (sizeof (schedulelist_t));
242 next = next->next;
243 next->type = schedule_goto;
244 next->value = 0;
245 next->gotolist = repeatat;
246 }
247
248 next->next = NULL;
249 return;
250 }
251
252 static pid_t get_pid (const char *pidfile, bool quiet)
253 {
254 FILE *fp;
255 pid_t pid;
256
257 if (! pidfile)
258 return (-1);
259
260 if ((fp = fopen (pidfile, "r")) == NULL) {
261 if (! quiet)
262 eerror ("%s: fopen `%s': %s", progname, pidfile, strerror (errno));
263 return (-1);
264 }
265
266 if (fscanf (fp, "%d", &pid) != 1) {
267 if (! quiet)
268 eerror ("%s: no pid found in `%s'", progname, pidfile);
269 fclose (fp);
270 return (-1);
271 }
272 fclose (fp);
273
274 return (pid);
275 }
276
277 /* return number of processed killed, -1 on error */
278 static int do_stop (const char *exec, const char *cmd,
279 const char *pidfile, uid_t uid,int sig,
280 bool quiet, bool verbose, bool test)
281 {
282 pid_t *pids;
283 bool killed;
284 int nkilled = 0;
285 pid_t pid = 0;
286 int i;
287
288 if (pidfile)
289 if ((pid = get_pid (pidfile, quiet)) == -1)
290 return (quiet ? 0 : -1);
291
292 if ((pids = rc_find_pids (exec, cmd, uid, pid)) == NULL)
293 return (0);
294
295 for (i = 0; pids[i]; i++) {
296 if (test) {
297 if (! quiet)
298 einfo ("Would send signal %d to PID %d", sig, pids[i]);
299 nkilled++;
300 continue;
301 }
302
303 if (verbose)
304 ebegin ("Sending signal %d to PID %d", sig, pids[i]);
305 errno = 0;
306 killed = (kill (pids[i], sig) == 0 || errno == ESRCH ? true : false);
307 if (! killed) {
308 if (! quiet)
309 eerror ("%s: failed to send signal %d to PID %d: %s",
310 progname, sig, pids[i], strerror (errno));
311 if (verbose)
312 eend (1, NULL);
313 nkilled = -1;
314 } else {
315 if (verbose)
316 eend (0, NULL);
317 if (nkilled != -1)
318 nkilled++;
319 }
320 }
321
322 free (pids);
323 return (nkilled);
324 }
325
326 static int run_stop_schedule (const char *exec, const char *cmd,
327 const char *pidfile, uid_t uid,
328 bool quiet, bool verbose, bool test)
329 {
330 schedulelist_t *item = schedule;
331 int nkilled = 0;
332 int tkilled = 0;
333 int nrunning = 0;
334 struct timeval tv;
335 struct timeval now;
336 struct timeval stopat;
337
338 if (verbose) {
339 if (pidfile)
340 einfo ("Will stop PID in pidfile `%s'", pidfile);
341 if (uid)
342 einfo ("Will stop processes owned by UID %d", uid);
343 if (exec)
344 einfo ("Will stop processes of `%s'", exec);
345 if (cmd)
346 einfo ("Will stop processes called `%s'", cmd);
347 }
348
349 while (item) {
350 switch (item->type) {
351 case schedule_goto:
352 item = item->gotolist;
353 continue;
354
355 case schedule_signal:
356 nrunning = 0;
357 nkilled = do_stop (exec, cmd, pidfile, uid, item->value,
358 quiet, verbose, test);
359 if (nkilled == 0) {
360 if (tkilled == 0) {
361 if (! quiet)
362 eerror ("%s: no matching processes found", progname);
363 }
364 return (tkilled);
365 }
366 else if (nkilled == -1)
367 return (0);
368
369 tkilled += nkilled;
370 break;
371 case schedule_timeout:
372 if (item->value < 1) {
373 item = NULL;
374 break;
375 }
376
377 if (gettimeofday (&stopat, NULL) != 0) {
378 eerror ("%s: gettimeofday: %s", progname, strerror (errno));
379 return (0);
380 }
381
382 stopat.tv_sec += item->value;
383 while (1) {
384 if ((nrunning = do_stop (exec, cmd, pidfile,
385 uid, 0, true, false, true)) == 0)
386 return (true);
387
388 tv.tv_sec = 0;
389 tv.tv_usec = POLL_INTERVAL;
390 if (select (0, 0, 0, 0, &tv) < 0) {
391 if (errno == EINTR)
392 eerror ("%s: caught an interupt", progname);
393 else
394 eerror ("%s: select: %s", progname, strerror (errno));
395 return (0);
396 }
397
398 if (gettimeofday (&now, NULL) != 0) {
399 eerror ("%s: gettimeofday: %s", progname, strerror (errno));
400 return (0);
401 }
402 if (timercmp (&now, &stopat, >))
403 break;
404 }
405 break;
406
407 default:
408 eerror ("%s: invalid schedule item `%d'", progname, item->type);
409 return (0);
410 }
411
412 if (item)
413 item = item->next;
414 }
415
416 if (test || (tkilled > 0 && nrunning == 0))
417 return (nkilled);
418
419 if (! quiet) {
420 if (nrunning == 1)
421 eerror ("%s: %d process refused to stop", progname, nrunning);
422 else
423 eerror ("%s: %d process(es) refused to stop", progname, nrunning);
424 }
425
426 return (-nrunning);
427 }
428
429 static void handle_signal (int sig)
430 {
431 int pid;
432 int status;
433 int serrno = errno;
434 char signame[10] = { '\0' };
435
436 switch (sig) {
437 case SIGINT:
438 if (! signame[0])
439 snprintf (signame, sizeof (signame), "SIGINT");
440 case SIGTERM:
441 if (! signame[0])
442 snprintf (signame, sizeof (signame), "SIGTERM");
443 case SIGQUIT:
444 if (! signame[0])
445 snprintf (signame, sizeof (signame), "SIGQUIT");
446 eerrorx ("%s: caught %s, aborting", progname, signame);
447
448 case SIGCHLD:
449 while (1) {
450 if ((pid = waitpid (-1, &status, WNOHANG)) < 0) {
451 if (errno != ECHILD)
452 eerror ("%s: waitpid: %s", progname, strerror (errno));
453 break;
454 }
455 }
456 break;
457
458 default:
459 eerror ("%s: caught unknown signal %d", progname, sig);
460 }
461
462 /* Restore errno */
463 errno = serrno;
464 }
465
466 int main (int argc, char **argv)
467 {
468 int devnull_fd = -1;
469
470 #ifdef TIOCNOTTY
471 int tty_fd = -1;
472 #endif
473 #ifdef HAVE_PAM
474 pam_handle_t *pamh = NULL;
475 int pamr;
476 #endif
477
478 static struct option longopts[] = {
479 { "stop", 0, NULL, 'K'},
480 { "nicelevel", 1, NULL, 'N'},
481 { "retry", 1, NULL, 'R'},
482 { "start", 0, NULL, 'S'},
483 { "background", 0, NULL, 'b'},
484 { "chuid", 1, NULL, 'c'},
485 { "chdir", 1, NULL, 'd'},
486 { "group", 1, NULL, 'g'},
487 { "make-pidfile", 0, NULL, 'm'},
488 { "name", 1, NULL, 'n'},
489 { "oknodo", 0, NULL, 'o'},
490 { "pidfile", 1, NULL, 'p'},
491 { "quiet", 0, NULL, 'q'},
492 { "signal", 1, NULL, 's'},
493 { "test", 0, NULL, 't'},
494 { "user", 1, NULL, 'u'},
495 { "chroot", 1, NULL, 'r'},
496 { "verbose", 0, NULL, 'v'},
497 { "exec", 1, NULL, 'x'},
498 { "stdout", 1, NULL, '1'},
499 { "stderr", 1, NULL, '2'},
500 { NULL, 0, NULL, 0}
501 };
502 int c;
503 bool start = false;
504 bool stop = false;
505 bool oknodo = false;
506 bool test = false;
507 bool quiet = false;
508 bool verbose = false;
509 char *exec = NULL;
510 char *cmd = NULL;
511 char *pidfile = NULL;
512 int sig = SIGTERM;
513 uid_t uid = 0;
514 int nicelevel = 0;
515 bool background = false;
516 bool makepidfile = false;
517 uid_t ch_uid = 0;
518 gid_t ch_gid = 0;
519 char *ch_root = NULL;
520 char *ch_dir = NULL;
521 int tid = 0;
522 char *redirect_stderr = NULL;
523 char *redirect_stdout = NULL;
524 int stdout_fd;
525 int stderr_fd;
526 pid_t pid;
527 struct timeval tv;
528 int i;
529 char *svcname = getenv ("SVCNAME");
530 char *env;
531
532 progname = argv[0];
533 atexit (cleanup);
534
535 signal (SIGINT, handle_signal);
536 signal (SIGQUIT, handle_signal);
537 signal (SIGTERM, handle_signal);
538
539 while ((c = getopt_long (argc, argv,
540 "KN:R:Sbc:d:g:mn:op:qs:tu:r:vx:1:2:",
541 longopts, (int *) 0)) != -1)
542 switch (c) {
543 case 'K': /* --stop */
544 stop = true;
545 break;
546
547 case 'N': /* --nice */
548 if (sscanf (optarg, "%d", &nicelevel) != 1)
549 eerrorx ("%s: invalid nice level `%s'", progname, optarg);
550 break;
551
552 case 'R': /* --retry <schedule>|<timeout> */
553 parse_schedule (optarg, sig);
554 break;
555
556 case 'S': /* --start */
557 start = true;
558 break;
559
560 case 'b': /* --background */
561 background = true;
562 break;
563
564 case 'c': /* --chuid <username>|<uid> */
565 {
566 char *p = optarg;
567 char *cu = strsep (&p, ":");
568 struct passwd *pw = NULL;
569
570 changeuser = strdup (cu);
571 if (sscanf (cu, "%d", &tid) != 1)
572 pw = getpwnam (cu);
573 else
574 pw = getpwuid (tid);
575
576 if (! pw)
577 eerrorx ("%s: user `%s' not found", progname, cu);
578 ch_uid = pw->pw_uid;
579 if (! ch_gid)
580 ch_gid = pw->pw_gid;
581
582 if (p) {
583 struct group *gr = NULL;
584 char *cg = strsep (&p, ":");
585
586 if (sscanf (cg, "%d", &tid) != 1)
587 gr = getgrnam (cg);
588 else
589 gr = getgrgid (tid);
590
591 if (! gr)
592 eerrorx ("%s: group `%s' not found", progname, cg);
593 ch_gid = gr->gr_gid;
594 }
595 }
596 break;
597
598 case 'd': /* --chdir /new/dir */
599 ch_dir = optarg;
600 break;
601
602 case 'g': /* --group <group>|<gid> */
603 {
604 struct group *gr = getgrnam (optarg);
605
606 if (sscanf (optarg, "%d", &tid) != 1)
607 gr = getgrnam (optarg);
608 else
609 gr = getgrgid (tid);
610
611 if (! gr)
612 eerrorx ("%s: group `%s' not found", progname, optarg);
613 ch_gid = gr->gr_gid;
614 }
615 break;
616
617 case 'm': /* --make-pidfile */
618 makepidfile = true;
619 break;
620
621 case 'n': /* --name <process-name> */
622 cmd = optarg;
623 break;
624
625 case 'o': /* --oknodo */
626 oknodo = true;
627 break;
628
629 case 'p': /* --pidfile <pid-file> */
630 pidfile = optarg;
631 break;
632
633 case 'q': /* --quiet */
634 quiet = true;
635 break;
636
637 case 's': /* --signal <signal> */
638 sig = parse_signal (optarg);
639 break;
640
641 case 't': /* --test */
642 test = true;
643 break;
644
645 case 'u': /* --user <username>|<uid> */
646 if (sscanf (optarg, "%d", &tid) != 1) {
647 struct passwd *pw = getpwnam (optarg);
648 if (! pw)
649 eerrorx ("%s: user `%s' not found", progname, optarg);
650 uid = pw->pw_uid;
651 } else
652 uid = tid;
653 break;
654
655 case 'r': /* --chroot /new/root */
656 ch_root = optarg;
657 break;
658
659 case 'v': /* --verbose */
660 verbose = true;
661 break;
662
663 case 'x': /* --exec <executable> */
664 exec = optarg;
665 break;
666
667 case '1': /* --stdout /path/to/stdout.lgfile */
668 redirect_stdout = optarg;
669 break;
670
671 case '2': /* --stderr /path/to/stderr.logfile */
672 redirect_stderr = optarg;
673 break;
674
675 default:
676 exit (EXIT_FAILURE);
677 }
678
679 /* Respect RC as well as how we are called */
680 if (rc_is_env ("RC_QUIET", "yes") && ! verbose)
681 quiet = true;
682
683 /* Allow start-stop-daemon --signal HUP --exec /usr/sbin/dnsmasq
684 * instead of forcing --stop --oknodo as well */
685 if (! start && ! stop)
686 if (sig != SIGINT &&
687 sig != SIGTERM &&
688 sig != SIGQUIT &&
689 sig != SIGKILL)
690 {
691 oknodo = true;
692 stop = true;
693 }
694
695 if (start == stop)
696 eerrorx ("%s: need one of --start or --stop", progname);
697
698 if (start && ! exec)
699 eerrorx ("%s: --start needs --exec", progname);
700
701 if (stop && ! exec && ! pidfile && ! cmd && ! uid)
702 eerrorx ("%s: --stop needs --exec, --pidfile, --name or --user", progname);
703
704 if (makepidfile && ! pidfile)
705 eerrorx ("%s: --make-pidfile is only relevant with --pidfile", progname);
706
707 if (background && ! start)
708 eerrorx ("%s: --background is only relevant with --start", progname);
709
710 if ((redirect_stdout || redirect_stderr) && ! background)
711 eerrorx ("%s: --stdout and --stderr are only relevant with --background",
712 progname);
713
714 argc -= optind;
715 argv += optind;
716
717 /* Validate that the binary rc_exists if we are starting */
718 if (exec && start) {
719 char *tmp;
720 if (ch_root)
721 tmp = rc_strcatpaths (ch_root, exec, (char *) NULL);
722 else
723 tmp = exec;
724 if (! rc_is_file (tmp)) {
725 eerror ("%s: %s does not exist", progname, tmp);
726 if (ch_root)
727 free (tmp);
728 exit (EXIT_FAILURE);
729 }
730 if (ch_root)
731 free (tmp);
732 }
733
734 if (stop) {
735 int result;
736
737 if (! schedule) {
738 if (test || oknodo)
739 parse_schedule ("0", sig);
740 else
741 parse_schedule (NULL, sig);
742 }
743
744 result = run_stop_schedule (exec, cmd, pidfile, uid, quiet, verbose, test);
745 if (test || oknodo)
746 return (result > 0 ? EXIT_SUCCESS : EXIT_FAILURE);
747 if (result < 1)
748 exit (result == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
749
750 if (pidfile && rc_is_file (pidfile))
751 unlink (pidfile);
752
753 if (svcname)
754 rc_set_service_daemon (svcname, exec, cmd, pidfile, false);
755
756 exit (EXIT_SUCCESS);
757 }
758
759 if (do_stop (exec, cmd, pidfile, uid, 0, true, false, true) > 0)
760 eerrorx ("%s: %s is already running", progname, exec);
761
762 if (test) {
763 if (quiet)
764 exit (EXIT_SUCCESS);
765
766 einfon ("Would start %s", exec);
767 while (argc-- > 0)
768 printf("%s ", *argv++);
769 printf ("\n");
770 eindent ();
771 if (ch_uid != 0)
772 einfo ("as user %d", ch_uid);
773 if (ch_gid != 0)
774 einfo ("as group %d", ch_gid);
775 if (ch_root)
776 einfo ("in root `%s'", ch_root);
777 if (ch_dir)
778 einfo ("in dir `%s'", ch_dir);
779 if (nicelevel != 0)
780 einfo ("with a priority of %d", nicelevel);
781 eoutdent ();
782 exit (EXIT_SUCCESS);
783 }
784
785 /* Ensure this is unset, so if the daemon does /etc/init.d/foo
786 Then we filter the environment accordingly */
787 unsetenv ("RC_SOFTLEVEL");
788
789 if (verbose) {
790 ebegin ("Detaching to start `%s'", exec);
791 eindent ();
792 }
793
794 if (background)
795 signal (SIGCHLD, handle_signal);
796
797 *--argv = exec;
798 if ((pid = fork ()) == -1)
799 eerrorx ("%s: fork: %s", progname, strerror (errno));
800
801 /* Child process - lets go! */
802 if (pid == 0) {
803 pid_t mypid = getpid ();
804
805 #ifdef TIOCNOTTY
806 tty_fd = open("/dev/tty", O_RDWR);
807 #endif
808
809 devnull_fd = open("/dev/null", O_RDWR);
810
811 if (nicelevel) {
812 if (setpriority (PRIO_PROCESS, mypid, nicelevel) == -1)
813 eerrorx ("%s: setpritory %d: %s", progname, nicelevel,
814 strerror(errno));
815 }
816
817 if (ch_root && chroot (ch_root) < 0)
818 eerrorx ("%s: chroot `%s': %s", progname, ch_root, strerror (errno));
819
820 if (ch_dir && chdir (ch_dir) < 0)
821 eerrorx ("%s: chdir `%s': %s", progname, ch_dir, strerror (errno));
822
823 if (makepidfile && pidfile) {
824 FILE *fp = fopen (pidfile, "w");
825 if (! fp)
826 eerrorx ("%s: fopen `%s': %s", progname, pidfile, strerror
827 (errno));
828 fprintf (fp, "%d\n", mypid);
829 fclose (fp);
830 }
831
832 #ifdef HAVE_PAM
833 if (changeuser != NULL)
834 pamr = pam_start ("start-stop-daemon", changeuser, &conv, &pamh);
835 else
836 pamr = pam_start ("start-stop-daemon", "nobody", &conv, &pamh);
837
838 if (pamr == PAM_SUCCESS)
839 pamr = pam_authenticate (pamh, PAM_SILENT);
840 if (pamr == PAM_SUCCESS)
841 pamr = pam_acct_mgmt (pamh, PAM_SILENT);
842 if (pamr == PAM_SUCCESS)
843 pamr = pam_open_session (pamh, PAM_SILENT);
844 if (pamr != PAM_SUCCESS)
845 eerrorx ("%s: pam error: %s", progname, pam_strerror(pamh, pamr));
846 #endif
847
848 if (ch_gid && setgid (ch_gid))
849 eerrorx ("%s: unable to set groupid to %d", progname, ch_gid);
850 if (changeuser && initgroups (changeuser, ch_gid))
851 eerrorx ("%s: initgroups (%s, %d)", progname, changeuser, ch_gid);
852 if (ch_uid && setuid (ch_uid))
853 eerrorx ("%s: unable to set userid to %d", progname, ch_uid);
854 else {
855 struct passwd *passwd = getpwuid (ch_uid);
856 if (passwd) {
857 unsetenv ("HOME");
858 if (passwd->pw_dir)
859 setenv ("HOME", passwd->pw_dir, 1);
860 unsetenv ("USER");
861 if (passwd->pw_name)
862 setenv ("USER", passwd->pw_name, 1);
863 }
864 }
865
866 /* Close any fd's to the passwd database */
867 endpwent ();
868
869 #ifdef TIOCNOTTY
870 ioctl(tty_fd, TIOCNOTTY, 0);
871 close(tty_fd);
872 #endif
873
874 /* Clean the environment of any RC_ variables */
875 STRLIST_FOREACH (environ, env, i)
876 if (env && strncmp (env, "RC_", 3) != 0) {
877 /* For the path character, remove the rcscript bin dir from it */
878 if (strncmp (env, "PATH=" RC_LIBDIR "bin:",
879 strlen ("PATH=" RC_LIBDIR "bin:")) == 0)
880 {
881 char *path = env;
882 char *newpath;
883 int len;
884 path += strlen ("PATH=" RC_LIBDIR "bin:");
885 len = sizeof (char *) * strlen (path) + 6;
886 newpath = rc_xmalloc (len);
887 snprintf (newpath, len, "PATH=%s", path);
888 newenv = rc_strlist_add (newenv, newpath);
889 free (newpath);
890 } else
891 newenv = rc_strlist_add (newenv, env);
892 }
893
894 umask (022);
895
896 stdout_fd = devnull_fd;
897 stderr_fd = devnull_fd;
898 if (redirect_stdout) {
899 if ((stdout_fd = open (redirect_stdout, O_WRONLY | O_CREAT | O_APPEND,
900 S_IRUSR | S_IWUSR)) == -1)
901 eerrorx ("%s: unable to open the logfile for stdout `%s': %s",
902 progname, redirect_stdout, strerror (errno));
903 }
904 if (redirect_stderr) {
905 if ((stderr_fd = open (redirect_stderr, O_WRONLY | O_CREAT | O_APPEND,
906 S_IRUSR | S_IWUSR)) == -1)
907 eerrorx ("%s: unable to open the logfile for stderr `%s': %s",
908 progname, redirect_stderr, strerror (errno));
909 }
910
911 if (background) {
912 /* Hmmm, some daemons may need stdin? */
913 dup2 (devnull_fd, STDIN_FILENO);
914 dup2 (stdout_fd, STDOUT_FILENO);
915 dup2 (stderr_fd, STDERR_FILENO);
916 }
917
918 for (i = getdtablesize () - 1; i >= 3; --i)
919 close(i);
920
921 setsid ();
922
923 execve (exec, argv, newenv);
924 #ifdef HAVE_PAM
925 if (pamr == PAM_SUCCESS)
926 pam_close_session (pamh, PAM_SILENT);
927 #endif
928 eerrorx ("%s: failed to exec `%s': %s", progname, exec, strerror (errno));
929 }
930
931 /* Parent process */
932 if (! background) {
933 /* As we're not backgrounding the process, wait for our pid to return */
934 int status = 0;
935 int savepid = pid;
936
937 errno = 0;
938 do {
939 pid = waitpid (savepid, &status, 0);
940 if (pid < 1) {
941 eerror ("waitpid %d: %s", savepid, strerror (errno));
942 return (-1);
943 }
944 } while (! WIFEXITED (status) && ! WIFSIGNALED (status));
945
946 if (! WIFEXITED (status) || WEXITSTATUS (status) != 0) {
947 if (! quiet)
948 eerrorx ("%s: failed to started `%s'", progname, exec);
949 exit (EXIT_FAILURE);
950 }
951
952 pid = savepid;
953 }
954
955 /* Wait a little bit and check that process is still running
956 We do this as some badly written daemons fork and then barf */
957 if (START_WAIT > 0) {
958 struct timeval stopat;
959 struct timeval now;
960
961 if (gettimeofday (&stopat, NULL) != 0)
962 eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
963
964 stopat.tv_usec += START_WAIT;
965 while (1) {
966 bool alive = false;
967
968 tv.tv_sec = 0;
969 tv.tv_usec = POLL_INTERVAL;
970 if (select (0, 0, 0, 0, &tv) < 0) {
971 /* Let our signal handler handle the interupt */
972 if (errno != EINTR)
973 eerrorx ("%s: select: %s", progname, strerror (errno));
974 }
975
976 if (gettimeofday (&now, NULL) != 0)
977 eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
978
979 /* This is knarly.
980 If we backgrounded then we know the exact pid.
981 Otherwise if we have a pidfile then it *may* know the exact pid.
982 Failing that, we'll have to query processes.
983 We sleep first as some programs like ntp like to fork, and write
984 their pidfile a LONG time later. */
985 if (background) {
986 if (kill (pid, 0) == 0)
987 alive = true;
988 } else {
989 if (pidfile && rc_exists (pidfile)) {
990 if (do_stop (NULL, NULL, pidfile, uid, 0, true, false, true) > 0)
991 alive = true;
992 } else {
993 if (do_stop (exec, cmd, NULL, uid, 0, true, false, true) > 0)
994 alive = true;
995 }
996 }
997
998 if (! alive)
999 eerrorx ("%s: %s died", progname, exec);
1000
1001 if (timercmp (&now, &stopat, >))
1002 break;
1003 }
1004 }
1005
1006 if (svcname)
1007 rc_set_service_daemon (svcname, exec, cmd, pidfile, true);
1008
1009 exit (EXIT_SUCCESS);
1010 }

  ViewVC Help
Powered by ViewVC 1.1.20