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

  ViewVC Help
Powered by ViewVC 1.1.20