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