/[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 2676 - (show annotations) (download) (as text)
Wed May 2 12:33:56 2007 UTC (11 years, 1 month ago) by uberlord
File MIME type: text/x-csrc
File size: 23455 byte(s)
    SSD_NICELEVEL can now affect the nicelevel for daemons started
    by start-stop-daemon, #175397.
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
399 if (gettimeofday (&now, NULL) != 0) {
400 eerror ("%s: gettimeofday: %s", progname, strerror (errno));
401 return (0);
402 }
403 if (timercmp (&now, &stopat, >))
404 break;
405 }
406 break;
407
408 default:
409 eerror ("%s: invalid schedule item `%d'", progname, item->type);
410 return (0);
411 }
412
413 if (item)
414 item = item->next;
415 }
416
417 if (test || (tkilled > 0 && nrunning == 0))
418 return (nkilled);
419
420 if (! quiet) {
421 if (nrunning == 1)
422 eerror ("%s: %d process refused to stop", progname, nrunning);
423 else
424 eerror ("%s: %d process(es) refused to stop", progname, nrunning);
425 }
426
427 return (-nrunning);
428 }
429
430 static void handle_signal (int sig)
431 {
432 int pid;
433 int status;
434 int serrno = errno;
435 char signame[10] = { '\0' };
436
437 switch (sig) {
438 case SIGINT:
439 if (! signame[0])
440 snprintf (signame, sizeof (signame), "SIGINT");
441 case SIGTERM:
442 if (! signame[0])
443 snprintf (signame, sizeof (signame), "SIGTERM");
444 case SIGQUIT:
445 if (! signame[0])
446 snprintf (signame, sizeof (signame), "SIGQUIT");
447 eerrorx ("%s: caught %s, aborting", progname, signame);
448
449 case SIGCHLD:
450 while (1) {
451 if ((pid = waitpid (-1, &status, WNOHANG)) < 0) {
452 if (errno != ECHILD)
453 eerror ("%s: waitpid: %s", progname, strerror (errno));
454 break;
455 }
456 }
457 break;
458
459 default:
460 eerror ("%s: caught unknown signal %d", progname, sig);
461 }
462
463 /* Restore errno */
464 errno = serrno;
465 }
466
467 int main (int argc, char **argv)
468 {
469 int devnull_fd = -1;
470
471 #ifdef TIOCNOTTY
472 int tty_fd = -1;
473 #endif
474 #ifdef HAVE_PAM
475 pam_handle_t *pamh = NULL;
476 int pamr;
477 #endif
478
479 static struct option longopts[] = {
480 { "stop", 0, NULL, 'K'},
481 { "nicelevel", 1, NULL, 'N'},
482 { "retry", 1, NULL, 'R'},
483 { "start", 0, NULL, 'S'},
484 { "background", 0, NULL, 'b'},
485 { "chuid", 1, NULL, 'c'},
486 { "chdir", 1, NULL, 'd'},
487 { "group", 1, NULL, 'g'},
488 { "make-pidfile", 0, NULL, 'm'},
489 { "name", 1, NULL, 'n'},
490 { "oknodo", 0, NULL, 'o'},
491 { "pidfile", 1, NULL, 'p'},
492 { "quiet", 0, NULL, 'q'},
493 { "signal", 1, NULL, 's'},
494 { "test", 0, NULL, 't'},
495 { "user", 1, NULL, 'u'},
496 { "chroot", 1, NULL, 'r'},
497 { "verbose", 0, NULL, 'v'},
498 { "exec", 1, NULL, 'x'},
499 { "stdout", 1, NULL, '1'},
500 { "stderr", 1, NULL, '2'},
501 { NULL, 0, NULL, 0}
502 };
503 int c;
504 bool start = false;
505 bool stop = false;
506 bool oknodo = false;
507 bool test = false;
508 bool quiet = false;
509 bool verbose = false;
510 char *exec = NULL;
511 char *cmd = NULL;
512 char *pidfile = NULL;
513 int sig = SIGTERM;
514 uid_t uid = 0;
515 int nicelevel = 0;
516 bool background = false;
517 bool makepidfile = false;
518 uid_t ch_uid = 0;
519 gid_t ch_gid = 0;
520 char *ch_root = NULL;
521 char *ch_dir = NULL;
522 int tid = 0;
523 char *redirect_stderr = NULL;
524 char *redirect_stdout = NULL;
525 int stdout_fd;
526 int stderr_fd;
527 pid_t pid;
528 struct timeval tv;
529 int i;
530 char *svcname = getenv ("SVCNAME");
531 char *env;
532
533 progname = argv[0];
534 atexit (cleanup);
535
536 signal (SIGINT, handle_signal);
537 signal (SIGQUIT, handle_signal);
538 signal (SIGTERM, handle_signal);
539
540 if ((env = getenv ("SSD_NICELEVEL")))
541 if (sscanf (env, "%d", &nicelevel) != 1)
542 eerror ("%s: invalid nice level `%s' (SSD_NICELEVEL)", progname, env);
543
544 while ((c = getopt_long (argc, argv,
545 "KN:R:Sbc:d:g:mn:op:qs:tu:r:vx:1:2:",
546 longopts, (int *) 0)) != -1)
547 switch (c) {
548 case 'K': /* --stop */
549 stop = true;
550 break;
551
552 case 'N': /* --nice */
553 if (sscanf (optarg, "%d", &nicelevel) != 1)
554 eerrorx ("%s: invalid nice level `%s'", progname, optarg);
555 break;
556
557 case 'R': /* --retry <schedule>|<timeout> */
558 parse_schedule (optarg, sig);
559 break;
560
561 case 'S': /* --start */
562 start = true;
563 break;
564
565 case 'b': /* --background */
566 background = true;
567 break;
568
569 case 'c': /* --chuid <username>|<uid> */
570 {
571 char *p = optarg;
572 char *cu = strsep (&p, ":");
573 struct passwd *pw = NULL;
574
575 changeuser = rc_xstrdup (cu);
576 if (sscanf (cu, "%d", &tid) != 1)
577 pw = getpwnam (cu);
578 else
579 pw = getpwuid (tid);
580
581 if (! pw)
582 eerrorx ("%s: user `%s' not found", progname, cu);
583 ch_uid = pw->pw_uid;
584 if (! ch_gid)
585 ch_gid = pw->pw_gid;
586
587 if (p) {
588 struct group *gr = NULL;
589 char *cg = strsep (&p, ":");
590
591 if (sscanf (cg, "%d", &tid) != 1)
592 gr = getgrnam (cg);
593 else
594 gr = getgrgid (tid);
595
596 if (! gr)
597 eerrorx ("%s: group `%s' not found", progname, cg);
598 ch_gid = gr->gr_gid;
599 }
600 }
601 break;
602
603 case 'd': /* --chdir /new/dir */
604 ch_dir = optarg;
605 break;
606
607 case 'g': /* --group <group>|<gid> */
608 {
609 struct group *gr = getgrnam (optarg);
610
611 if (sscanf (optarg, "%d", &tid) != 1)
612 gr = getgrnam (optarg);
613 else
614 gr = getgrgid (tid);
615
616 if (! gr)
617 eerrorx ("%s: group `%s' not found", progname, optarg);
618 ch_gid = gr->gr_gid;
619 }
620 break;
621
622 case 'm': /* --make-pidfile */
623 makepidfile = true;
624 break;
625
626 case 'n': /* --name <process-name> */
627 cmd = optarg;
628 break;
629
630 case 'o': /* --oknodo */
631 oknodo = true;
632 break;
633
634 case 'p': /* --pidfile <pid-file> */
635 pidfile = optarg;
636 break;
637
638 case 'q': /* --quiet */
639 quiet = true;
640 break;
641
642 case 's': /* --signal <signal> */
643 sig = parse_signal (optarg);
644 break;
645
646 case 't': /* --test */
647 test = true;
648 break;
649
650 case 'u': /* --user <username>|<uid> */
651 if (sscanf (optarg, "%d", &tid) != 1) {
652 struct passwd *pw = getpwnam (optarg);
653 if (! pw)
654 eerrorx ("%s: user `%s' not found", progname, optarg);
655 uid = pw->pw_uid;
656 } else
657 uid = tid;
658 break;
659
660 case 'r': /* --chroot /new/root */
661 ch_root = optarg;
662 break;
663
664 case 'v': /* --verbose */
665 verbose = true;
666 break;
667
668 case 'x': /* --exec <executable> */
669 exec = optarg;
670 break;
671
672 case '1': /* --stdout /path/to/stdout.lgfile */
673 redirect_stdout = optarg;
674 break;
675
676 case '2': /* --stderr /path/to/stderr.logfile */
677 redirect_stderr = optarg;
678 break;
679
680 default:
681 exit (EXIT_FAILURE);
682 }
683
684 /* Respect RC as well as how we are called */
685 if (rc_is_env ("RC_QUIET", "yes") && ! verbose)
686 quiet = true;
687
688 /* Allow start-stop-daemon --signal HUP --exec /usr/sbin/dnsmasq
689 * instead of forcing --stop --oknodo as well */
690 if (! start && ! stop)
691 if (sig != SIGINT &&
692 sig != SIGTERM &&
693 sig != SIGQUIT &&
694 sig != SIGKILL)
695 {
696 oknodo = true;
697 stop = true;
698 }
699
700 if (start == stop)
701 eerrorx ("%s: need one of --start or --stop", progname);
702
703 if (start && ! exec)
704 eerrorx ("%s: --start needs --exec", progname);
705
706 if (stop && ! exec && ! pidfile && ! cmd && ! uid)
707 eerrorx ("%s: --stop needs --exec, --pidfile, --name or --user", progname);
708
709 if (makepidfile && ! pidfile)
710 eerrorx ("%s: --make-pidfile is only relevant with --pidfile", progname);
711
712 if (background && ! start)
713 eerrorx ("%s: --background is only relevant with --start", progname);
714
715 if ((redirect_stdout || redirect_stderr) && ! background)
716 eerrorx ("%s: --stdout and --stderr are only relevant with --background",
717 progname);
718
719 argc -= optind;
720 argv += optind;
721
722 /* Validate that the binary rc_exists if we are starting */
723 if (exec && start) {
724 char *tmp;
725 if (ch_root)
726 tmp = rc_strcatpaths (ch_root, exec, (char *) NULL);
727 else
728 tmp = exec;
729 if (! rc_is_file (tmp)) {
730 eerror ("%s: %s does not exist", progname, tmp);
731 if (ch_root)
732 free (tmp);
733 exit (EXIT_FAILURE);
734 }
735 if (ch_root)
736 free (tmp);
737 }
738
739 if (stop) {
740 int result;
741
742 if (! schedule) {
743 if (test || oknodo)
744 parse_schedule ("0", sig);
745 else
746 parse_schedule (NULL, sig);
747 }
748
749 result = run_stop_schedule (exec, cmd, pidfile, uid, quiet, verbose, test);
750 if (test || oknodo)
751 return (result > 0 ? EXIT_SUCCESS : EXIT_FAILURE);
752 if (result < 1)
753 exit (result == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
754
755 if (pidfile && rc_is_file (pidfile))
756 unlink (pidfile);
757
758 if (svcname)
759 rc_set_service_daemon (svcname, exec, cmd, pidfile, false);
760
761 exit (EXIT_SUCCESS);
762 }
763
764 if (do_stop (exec, cmd, pidfile, uid, 0, true, false, true) > 0)
765 eerrorx ("%s: %s is already running", progname, exec);
766
767 if (test) {
768 if (quiet)
769 exit (EXIT_SUCCESS);
770
771 einfon ("Would start %s", exec);
772 while (argc-- > 0)
773 printf("%s ", *argv++);
774 printf ("\n");
775 eindent ();
776 if (ch_uid != 0)
777 einfo ("as user %d", ch_uid);
778 if (ch_gid != 0)
779 einfo ("as group %d", ch_gid);
780 if (ch_root)
781 einfo ("in root `%s'", ch_root);
782 if (ch_dir)
783 einfo ("in dir `%s'", ch_dir);
784 if (nicelevel != 0)
785 einfo ("with a priority of %d", nicelevel);
786 eoutdent ();
787 exit (EXIT_SUCCESS);
788 }
789
790 /* Ensure this is unset, so if the daemon does /etc/init.d/foo
791 Then we filter the environment accordingly */
792 unsetenv ("RC_SOFTLEVEL");
793
794 if (verbose) {
795 ebegin ("Detaching to start `%s'", exec);
796 eindent ();
797 }
798
799 if (background)
800 signal (SIGCHLD, handle_signal);
801
802 *--argv = exec;
803 if ((pid = fork ()) == -1)
804 eerrorx ("%s: fork: %s", progname, strerror (errno));
805
806 /* Child process - lets go! */
807 if (pid == 0) {
808 pid_t mypid = getpid ();
809
810 #ifdef TIOCNOTTY
811 tty_fd = open("/dev/tty", O_RDWR);
812 #endif
813
814 devnull_fd = open("/dev/null", O_RDWR);
815
816 if (nicelevel) {
817 if (setpriority (PRIO_PROCESS, mypid, nicelevel) == -1)
818 eerrorx ("%s: setpritory %d: %s", progname, nicelevel,
819 strerror(errno));
820 }
821
822 if (ch_root && chroot (ch_root) < 0)
823 eerrorx ("%s: chroot `%s': %s", progname, ch_root, strerror (errno));
824
825 if (ch_dir && chdir (ch_dir) < 0)
826 eerrorx ("%s: chdir `%s': %s", progname, ch_dir, strerror (errno));
827
828 if (makepidfile && pidfile) {
829 FILE *fp = fopen (pidfile, "w");
830 if (! fp)
831 eerrorx ("%s: fopen `%s': %s", progname, pidfile, strerror
832 (errno));
833 fprintf (fp, "%d\n", mypid);
834 fclose (fp);
835 }
836
837 #ifdef HAVE_PAM
838 if (changeuser != NULL)
839 pamr = pam_start ("start-stop-daemon", changeuser, &conv, &pamh);
840 else
841 pamr = pam_start ("start-stop-daemon", "nobody", &conv, &pamh);
842
843 if (pamr == PAM_SUCCESS)
844 pamr = pam_authenticate (pamh, PAM_SILENT);
845 if (pamr == PAM_SUCCESS)
846 pamr = pam_acct_mgmt (pamh, PAM_SILENT);
847 if (pamr == PAM_SUCCESS)
848 pamr = pam_open_session (pamh, PAM_SILENT);
849 if (pamr != PAM_SUCCESS)
850 eerrorx ("%s: pam error: %s", progname, pam_strerror(pamh, pamr));
851 #endif
852
853 if (ch_gid && setgid (ch_gid))
854 eerrorx ("%s: unable to set groupid to %d", progname, ch_gid);
855 if (changeuser && initgroups (changeuser, ch_gid))
856 eerrorx ("%s: initgroups (%s, %d)", progname, changeuser, ch_gid);
857 if (ch_uid && setuid (ch_uid))
858 eerrorx ("%s: unable to set userid to %d", progname, ch_uid);
859 else {
860 struct passwd *passwd = getpwuid (ch_uid);
861 if (passwd) {
862 unsetenv ("HOME");
863 if (passwd->pw_dir)
864 setenv ("HOME", passwd->pw_dir, 1);
865 unsetenv ("USER");
866 if (passwd->pw_name)
867 setenv ("USER", passwd->pw_name, 1);
868 }
869 }
870
871 /* Close any fd's to the passwd database */
872 endpwent ();
873
874 #ifdef TIOCNOTTY
875 ioctl(tty_fd, TIOCNOTTY, 0);
876 close(tty_fd);
877 #endif
878
879 /* Clean the environment of any RC_ variables */
880 STRLIST_FOREACH (environ, env, i)
881 if (env && strncmp (env, "RC_", 3) != 0) {
882 /* For the path character, remove the rcscript bin dir from it */
883 if (strncmp (env, "PATH=" RC_LIBDIR "bin:",
884 strlen ("PATH=" RC_LIBDIR "bin:")) == 0)
885 {
886 char *path = env;
887 char *newpath;
888 int len;
889 path += strlen ("PATH=" RC_LIBDIR "bin:");
890 len = sizeof (char *) * strlen (path) + 6;
891 newpath = rc_xmalloc (len);
892 snprintf (newpath, len, "PATH=%s", path);
893 newenv = rc_strlist_add (newenv, newpath);
894 free (newpath);
895 } else
896 newenv = rc_strlist_add (newenv, env);
897 }
898
899 umask (022);
900
901 stdout_fd = devnull_fd;
902 stderr_fd = devnull_fd;
903 if (redirect_stdout) {
904 if ((stdout_fd = open (redirect_stdout, O_WRONLY | O_CREAT | O_APPEND,
905 S_IRUSR | S_IWUSR)) == -1)
906 eerrorx ("%s: unable to open the logfile for stdout `%s': %s",
907 progname, redirect_stdout, strerror (errno));
908 }
909 if (redirect_stderr) {
910 if ((stderr_fd = open (redirect_stderr, O_WRONLY | O_CREAT | O_APPEND,
911 S_IRUSR | S_IWUSR)) == -1)
912 eerrorx ("%s: unable to open the logfile for stderr `%s': %s",
913 progname, redirect_stderr, strerror (errno));
914 }
915
916 if (background) {
917 /* Hmmm, some daemons may need stdin? */
918 dup2 (devnull_fd, STDIN_FILENO);
919 dup2 (stdout_fd, STDOUT_FILENO);
920 dup2 (stderr_fd, STDERR_FILENO);
921 }
922
923 for (i = getdtablesize () - 1; i >= 3; --i)
924 close(i);
925
926 setsid ();
927
928 execve (exec, argv, newenv);
929 #ifdef HAVE_PAM
930 if (pamr == PAM_SUCCESS)
931 pam_close_session (pamh, PAM_SILENT);
932 #endif
933 eerrorx ("%s: failed to exec `%s': %s", progname, exec, strerror (errno));
934 }
935
936 /* Parent process */
937 if (! background) {
938 /* As we're not backgrounding the process, wait for our pid to return */
939 int status = 0;
940 int savepid = pid;
941
942 errno = 0;
943 do {
944 pid = waitpid (savepid, &status, 0);
945 if (pid < 1) {
946 eerror ("waitpid %d: %s", savepid, strerror (errno));
947 return (-1);
948 }
949 } while (! WIFEXITED (status) && ! WIFSIGNALED (status));
950
951 if (! WIFEXITED (status) || WEXITSTATUS (status) != 0) {
952 if (! quiet)
953 eerrorx ("%s: failed to started `%s'", progname, exec);
954 exit (EXIT_FAILURE);
955 }
956
957 pid = savepid;
958 }
959
960 /* Wait a little bit and check that process is still running
961 We do this as some badly written daemons fork and then barf */
962 if (START_WAIT > 0) {
963 struct timeval stopat;
964 struct timeval now;
965
966 if (gettimeofday (&stopat, NULL) != 0)
967 eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
968
969 stopat.tv_usec += START_WAIT;
970 while (1) {
971 bool alive = false;
972
973 tv.tv_sec = 0;
974 tv.tv_usec = POLL_INTERVAL;
975 if (select (0, 0, 0, 0, &tv) < 0) {
976 /* Let our signal handler handle the interupt */
977 if (errno != EINTR)
978 eerrorx ("%s: select: %s", progname, strerror (errno));
979 }
980
981 if (gettimeofday (&now, NULL) != 0)
982 eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
983
984 /* This is knarly.
985 If we backgrounded then we know the exact pid.
986 Otherwise if we have a pidfile then it *may* know the exact pid.
987 Failing that, we'll have to query processes.
988 We sleep first as some programs like ntp like to fork, and write
989 their pidfile a LONG time later. */
990 if (background) {
991 if (kill (pid, 0) == 0)
992 alive = true;
993 } else {
994 if (pidfile && rc_exists (pidfile)) {
995 if (do_stop (NULL, NULL, pidfile, uid, 0, true, false, true) > 0)
996 alive = true;
997 } else {
998 if (do_stop (exec, cmd, NULL, uid, 0, true, false, true) > 0)
999 alive = true;
1000 }
1001 }
1002
1003 if (! alive)
1004 eerrorx ("%s: %s died", progname, exec);
1005
1006 if (timercmp (&now, &stopat, >))
1007 break;
1008 }
1009 }
1010
1011 if (svcname)
1012 rc_set_service_daemon (svcname, exec, cmd, pidfile, true);
1013
1014 exit (EXIT_SUCCESS);
1015 }

  ViewVC Help
Powered by ViewVC 1.1.20