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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2699 Revision 2708
28#include <signal.h> 28#include <signal.h>
29#include <stddef.h> 29#include <stddef.h>
30#include <stdio.h> 30#include <stdio.h>
31#include <stdlib.h> 31#include <stdlib.h>
32#include <string.h> 32#include <string.h>
33#include <time.h>
33#include <unistd.h> 34#include <unistd.h>
34 35
35#ifdef HAVE_PAM 36#ifdef HAVE_PAM
36#include <security/pam_appl.h> 37#include <security/pam_appl.h>
37 38
86 if (schedule) 87 if (schedule)
87 free_schedulelist (&schedule); 88 free_schedulelist (&schedule);
88 89
89 if (newenv) 90 if (newenv)
90 rc_strlist_free (newenv); 91 rc_strlist_free (newenv);
92}
93
94static int get_time(struct timeval *tp)
95{
96 struct timespec ts;
97
98 if (clock_gettime (CLOCK_MONOTONIC, &ts) == -1) {
99 eerror ("clock_gettime: %s", strerror (errno));
100 return (-1);
101 }
102
103 tp->tv_sec = ts.tv_sec;
104 tp->tv_usec = ts.tv_nsec / 1000;
105 return (0);
91} 106}
92 107
93static int parse_signal (const char *sig) 108static int parse_signal (const char *sig)
94{ 109{
95 typedef struct signalpair 110 typedef struct signalpair
372 if (item->value < 1) { 387 if (item->value < 1) {
373 item = NULL; 388 item = NULL;
374 break; 389 break;
375 } 390 }
376 391
377 if (gettimeofday (&stopat, NULL) != 0) { 392 if (get_time (&stopat) != 0)
378 eerror ("%s: gettimeofday: %s", progname, strerror (errno));
379 return (0); 393 return (0);
380 }
381 394
382 stopat.tv_sec += item->value; 395 stopat.tv_sec += item->value;
383 while (1) { 396 while (1) {
384 if ((nrunning = do_stop (exec, cmd, pidfile, 397 if ((nrunning = do_stop (exec, cmd, pidfile,
385 uid, 0, true, false, true)) == 0) 398 uid, 0, true, false, true)) == 0)
394 eerror ("%s: select: %s", progname, strerror (errno)); 407 eerror ("%s: select: %s", progname, strerror (errno));
395 return (0); 408 return (0);
396 } 409 }
397 } 410 }
398 411
399 if (gettimeofday (&now, NULL) != 0) { 412 if (get_time (&now) != 0)
400 eerror ("%s: gettimeofday: %s", progname, strerror (errno));
401 return (0); 413 return (0);
402 }
403 if (timercmp (&now, &stopat, >)) 414 if (timercmp (&now, &stopat, >))
404 break; 415 break;
405 } 416 }
406 break; 417 break;
407 418
964 if (START_WAIT > 0) { 975 if (START_WAIT > 0) {
965 struct timeval stopat; 976 struct timeval stopat;
966 struct timeval now; 977 struct timeval now;
967 bool retestpid = false; 978 bool retestpid = false;
968 979
969 if (gettimeofday (&stopat, NULL) != 0) 980 if (get_time (&stopat) != 0)
970 eerrorx ("%s: gettimeofday: %s", progname, strerror (errno)); 981 exit (EXIT_FAILURE);
971 982
972 stopat.tv_usec += START_WAIT; 983 stopat.tv_usec += START_WAIT;
973 while (1) { 984 while (1) {
974 bool alive = false; 985 bool alive = false;
975 986
978 if (select (0, 0, 0, 0, &tv) < 0) { 989 if (select (0, 0, 0, 0, &tv) < 0) {
979 /* Let our signal handler handle the interupt */ 990 /* Let our signal handler handle the interupt */
980 if (errno != EINTR) 991 if (errno != EINTR)
981 eerrorx ("%s: select: %s", progname, strerror (errno)); 992 eerrorx ("%s: select: %s", progname, strerror (errno));
982 } 993 }
983
984 if (gettimeofday (&now, NULL) != 0)
985 eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
986 994
987 /* This is knarly. 995 /* This is knarly.
988 If we backgrounded then we know the exact pid. 996 If we backgrounded then we know the exact pid.
989 Otherwise if we have a pidfile then it *may* know the exact pid. 997 Otherwise if we have a pidfile then it *may* know the exact pid.
990 Failing that, we'll have to query processes. 998 Failing that, we'll have to query processes.
998 /* The pidfile may not have been written yet - give it some time */ 1006 /* The pidfile may not have been written yet - give it some time */
999 if (get_pid (pidfile, true) == -1) { 1007 if (get_pid (pidfile, true) == -1) {
1000 alive = true; 1008 alive = true;
1001 retestpid = true; 1009 retestpid = true;
1002 } else { 1010 } else {
1011 printf ("%d\n", get_pid (pidfile, true));
1003 retestpid = false; 1012 retestpid = false;
1004 if (do_stop (NULL, NULL, pidfile, uid, 0, 1013 if (do_stop (NULL, NULL, pidfile, uid, 0,
1005 true, false, true) > 0) 1014 true, false, true) > 0)
1006 alive = true; 1015 alive = true;
1007 } 1016 }
1013 } 1022 }
1014 1023
1015 if (! alive) 1024 if (! alive)
1016 eerrorx ("%s: %s died", progname, exec); 1025 eerrorx ("%s: %s died", progname, exec);
1017 1026
1027 if (get_time (&now) != 0)
1028 exit (EXIT_FAILURE);
1018 if (timercmp (&now, &stopat, >)) 1029 if (timercmp (&now, &stopat, >))
1019 break; 1030 break;
1020 } 1031 }
1021 1032
1022 if (retestpid) { 1033 if (retestpid) {
1023 if (do_stop (NULL, NULL, pidfile, uid, 0, true, 1034 if (do_stop (NULL, NULL, pidfile, uid, 0, true,

Legend:
Removed from v.2699  
changed lines
  Added in v.2708

  ViewVC Help
Powered by ViewVC 1.1.20