1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)clock.c 8.9 (Berkeley) 12/28/94"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 15 # ifndef sigmask 16 # define sigmask(s) (1 << ((s) - 1)) 17 # endif 18 19 /* 20 ** SETEVENT -- set an event to happen at a specific time. 21 ** 22 ** Events are stored in a sorted list for fast processing. 23 ** An event only applies to the process that set it. 24 ** 25 ** Parameters: 26 ** intvl -- intvl until next event occurs. 27 ** func -- function to call on event. 28 ** arg -- argument to func on event. 29 ** 30 ** Returns: 31 ** none. 32 ** 33 ** Side Effects: 34 ** none. 35 */ 36 37 static void tick __P((int)); 38 39 EVENT * 40 setevent(intvl, func, arg) 41 time_t intvl; 42 int (*func)(); 43 int arg; 44 { 45 register EVENT **evp; 46 register EVENT *ev; 47 auto time_t now; 48 49 if (intvl <= 0) 50 { 51 syserr("554 setevent: intvl=%ld\n", intvl); 52 return (NULL); 53 } 54 55 (void) setsignal(SIGALRM, SIG_IGN); 56 (void) time(&now); 57 58 /* search event queue for correct position */ 59 for (evp = &EventQueue; (ev = *evp) != NULL; evp = &ev->ev_link) 60 { 61 if (ev->ev_time >= now + intvl) 62 break; 63 } 64 65 /* insert new event */ 66 ev = (EVENT *) xalloc(sizeof *ev); 67 ev->ev_time = now + intvl; 68 ev->ev_func = func; 69 ev->ev_arg = arg; 70 ev->ev_pid = getpid(); 71 ev->ev_link = *evp; 72 *evp = ev; 73 74 if (tTd(5, 5)) 75 printf("setevent: intvl=%ld, for=%ld, func=%x, arg=%d, ev=%x\n", 76 intvl, now + intvl, func, arg, ev); 77 78 tick(0); 79 return (ev); 80 } 81 /* 82 ** CLREVENT -- remove an event from the event queue. 83 ** 84 ** Parameters: 85 ** ev -- pointer to event to remove. 86 ** 87 ** Returns: 88 ** none. 89 ** 90 ** Side Effects: 91 ** arranges for event ev to not happen. 92 */ 93 94 clrevent(ev) 95 register EVENT *ev; 96 { 97 register EVENT **evp; 98 99 if (tTd(5, 5)) 100 printf("clrevent: ev=%x\n", ev); 101 if (ev == NULL) 102 return; 103 104 /* find the parent event */ 105 (void) setsignal(SIGALRM, SIG_IGN); 106 for (evp = &EventQueue; *evp != NULL; evp = &(*evp)->ev_link) 107 { 108 if (*evp == ev) 109 break; 110 } 111 112 /* now remove it */ 113 if (*evp != NULL) 114 { 115 *evp = ev->ev_link; 116 free((char *) ev); 117 } 118 119 /* restore clocks and pick up anything spare */ 120 tick(0); 121 } 122 /* 123 ** TICK -- take a clock tick 124 ** 125 ** Called by the alarm clock. This routine runs events as needed. 126 ** 127 ** Parameters: 128 ** One that is ignored; for compatibility with signal handlers. 129 ** 130 ** Returns: 131 ** none. 132 ** 133 ** Side Effects: 134 ** calls the next function in EventQueue. 135 */ 136 137 static void 138 tick(arg) 139 int arg; 140 { 141 register time_t now; 142 register EVENT *ev; 143 int mypid = getpid(); 144 int olderrno = errno; 145 #ifdef SIG_UNBLOCK 146 sigset_t ss; 147 #endif 148 149 (void) setsignal(SIGALRM, SIG_IGN); 150 (void) alarm(0); 151 now = curtime(); 152 153 if (tTd(5, 4)) 154 printf("tick: now=%ld\n", now); 155 156 while ((ev = EventQueue) != NULL && 157 (ev->ev_time <= now || ev->ev_pid != mypid)) 158 { 159 int (*f)(); 160 int arg; 161 int pid; 162 163 /* process the event on the top of the queue */ 164 ev = EventQueue; 165 EventQueue = EventQueue->ev_link; 166 if (tTd(5, 6)) 167 printf("tick: ev=%x, func=%x, arg=%d, pid=%d\n", ev, 168 ev->ev_func, ev->ev_arg, ev->ev_pid); 169 170 /* we must be careful in here because ev_func may not return */ 171 f = ev->ev_func; 172 arg = ev->ev_arg; 173 pid = ev->ev_pid; 174 free((char *) ev); 175 if (pid != getpid()) 176 continue; 177 if (EventQueue != NULL) 178 { 179 if (EventQueue->ev_time > now) 180 (void) alarm((unsigned) (EventQueue->ev_time - now)); 181 else 182 (void) alarm(3); 183 } 184 185 /* restore signals so that we can take ticks while in ev_func */ 186 (void) setsignal(SIGALRM, tick); 187 #ifdef SIG_UNBLOCK 188 /* unblock SIGALRM signal */ 189 sigemptyset(&ss); 190 sigaddset(&ss, SIGALRM); 191 sigprocmask(SIG_UNBLOCK, &ss, NULL); 192 #else 193 #if HASSIGSETMASK 194 /* reset 4.2bsd signal mask to allow future alarms */ 195 (void) sigsetmask(sigblock(0) & ~sigmask(SIGALRM)); 196 #endif /* HASSIGSETMASK */ 197 #endif /* SIG_UNBLOCK */ 198 199 /* call ev_func */ 200 errno = olderrno; 201 (*f)(arg); 202 (void) alarm(0); 203 now = curtime(); 204 } 205 (void) setsignal(SIGALRM, tick); 206 if (EventQueue != NULL) 207 (void) alarm((unsigned) (EventQueue->ev_time - now)); 208 errno = olderrno; 209 } 210 /* 211 ** SLEEP -- a version of sleep that works with this stuff 212 ** 213 ** Because sleep uses the alarm facility, I must reimplement 214 ** it here. 215 ** 216 ** Parameters: 217 ** intvl -- time to sleep. 218 ** 219 ** Returns: 220 ** none. 221 ** 222 ** Side Effects: 223 ** waits for intvl time. However, other events can 224 ** be run during that interval. 225 */ 226 227 static bool SleepDone; 228 static int endsleep(); 229 230 #ifndef SLEEP_T 231 # define SLEEP_T unsigned int 232 #endif 233 234 SLEEP_T 235 sleep(intvl) 236 unsigned int intvl; 237 { 238 if (intvl == 0) 239 return; 240 SleepDone = FALSE; 241 (void) setevent((time_t) intvl, endsleep, 0); 242 while (!SleepDone) 243 pause(); 244 } 245 246 static 247 endsleep() 248 { 249 SleepDone = TRUE; 250 } 251