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.7 (Berkeley) 10/21/93"; 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(); 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(); 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(); 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 ** none. 129 ** 130 ** Returns: 131 ** none. 132 ** 133 ** Side Effects: 134 ** calls the next function in EventQueue. 135 */ 136 137 static void 138 tick() 139 { 140 register time_t now; 141 register EVENT *ev; 142 int mypid = getpid(); 143 int olderrno = errno; 144 #ifdef SIG_UNBLOCK 145 sigset_t ss; 146 #endif 147 148 (void) setsignal(SIGALRM, SIG_IGN); 149 (void) alarm(0); 150 now = curtime(); 151 152 if (tTd(5, 4)) 153 printf("tick: now=%ld\n", now); 154 155 while ((ev = EventQueue) != NULL && 156 (ev->ev_time <= now || ev->ev_pid != mypid)) 157 { 158 int (*f)(); 159 int arg; 160 int pid; 161 162 /* process the event on the top of the queue */ 163 ev = EventQueue; 164 EventQueue = EventQueue->ev_link; 165 if (tTd(5, 6)) 166 printf("tick: ev=%x, func=%x, arg=%d, pid=%d\n", ev, 167 ev->ev_func, ev->ev_arg, ev->ev_pid); 168 169 /* we must be careful in here because ev_func may not return */ 170 f = ev->ev_func; 171 arg = ev->ev_arg; 172 pid = ev->ev_pid; 173 free((char *) ev); 174 if (pid != getpid()) 175 continue; 176 if (EventQueue != NULL) 177 { 178 if (EventQueue->ev_time > now) 179 (void) alarm((unsigned) (EventQueue->ev_time - now)); 180 else 181 (void) alarm(3); 182 } 183 184 /* restore signals so that we can take ticks while in ev_func */ 185 (void) setsignal(SIGALRM, tick); 186 #ifdef SIG_UNBLOCK 187 /* unblock SIGALRM signal */ 188 sigemptyset(&ss); 189 sigaddset(&ss, SIGALRM); 190 sigprocmask(SIG_UNBLOCK, &ss, NULL); 191 #else 192 #ifdef SIGVTALRM 193 /* reset 4.2bsd signal mask to allow future alarms */ 194 (void) sigsetmask(sigblock(0) & ~sigmask(SIGALRM)); 195 #endif /* SIGVTALRM */ 196 #endif /* SIG_UNBLOCK */ 197 198 /* call ev_func */ 199 errno = olderrno; 200 (*f)(arg); 201 (void) alarm(0); 202 now = curtime(); 203 } 204 (void) setsignal(SIGALRM, tick); 205 if (EventQueue != NULL) 206 (void) alarm((unsigned) (EventQueue->ev_time - now)); 207 errno = olderrno; 208 } 209 /* 210 ** SLEEP -- a version of sleep that works with this stuff 211 ** 212 ** Because sleep uses the alarm facility, I must reimplement 213 ** it here. 214 ** 215 ** Parameters: 216 ** intvl -- time to sleep. 217 ** 218 ** Returns: 219 ** none. 220 ** 221 ** Side Effects: 222 ** waits for intvl time. However, other events can 223 ** be run during that interval. 224 */ 225 226 static bool SleepDone; 227 static int endsleep(); 228 229 #ifndef SLEEP_T 230 # define SLEEP_T unsigned int 231 #endif 232 233 SLEEP_T 234 sleep(intvl) 235 unsigned int intvl; 236 { 237 if (intvl == 0) 238 return; 239 SleepDone = FALSE; 240 (void) setevent((time_t) intvl, endsleep, 0); 241 while (!SleepDone) 242 pause(); 243 } 244 245 static 246 endsleep() 247 { 248 SleepDone = TRUE; 249 } 250