122696Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 362522Sbostic * Copyright (c) 1988, 1993 462522Sbostic * The Regents of the University of California. All rights reserved. 533728Sbostic * 642824Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822696Sdist 922696Sdist #ifndef lint 10*63968Seric static char sccsid[] = "@(#)clock.c 8.4 (Berkeley) 07/21/93"; 1133728Sbostic #endif /* not lint */ 1222696Sdist 137358Seric # include "sendmail.h" 1411728Seric # include <signal.h> 157358Seric 1660601Seric # ifndef sigmask 1760601Seric # define sigmask(s) (1 << ((s) - 1)) 1860601Seric # endif 1960601Seric 207358Seric /* 217684Seric ** SETEVENT -- set an event to happen at a specific time. 227684Seric ** 239373Seric ** Events are stored in a sorted list for fast processing. 249373Seric ** An event only applies to the process that set it. 259373Seric ** 267684Seric ** Parameters: 277684Seric ** intvl -- intvl until next event occurs. 287684Seric ** func -- function to call on event. 297684Seric ** arg -- argument to func on event. 307684Seric ** 317684Seric ** Returns: 327684Seric ** none. 337684Seric ** 347684Seric ** Side Effects: 357684Seric ** none. 367684Seric */ 377684Seric 3846928Sbostic static void tick(); 3946928Sbostic 407684Seric EVENT * 417684Seric setevent(intvl, func, arg) 427684Seric time_t intvl; 437684Seric int (*func)(); 447684Seric int arg; 457684Seric { 467684Seric register EVENT **evp; 477684Seric register EVENT *ev; 487684Seric auto time_t now; 497684Seric 507757Seric if (intvl <= 0) 517757Seric { 5258151Seric syserr("554 setevent: intvl=%ld\n", intvl); 539346Seric return (NULL); 547757Seric } 557757Seric 567684Seric (void) time(&now); 577684Seric 587684Seric /* search event queue for correct position */ 597684Seric for (evp = &EventQueue; (ev = *evp) != NULL; evp = &ev->ev_link) 607684Seric { 617684Seric if (ev->ev_time >= now + intvl) 627684Seric break; 637684Seric } 647684Seric 657684Seric /* insert new event */ 667684Seric ev = (EVENT *) xalloc(sizeof *ev); 677684Seric ev->ev_time = now + intvl; 687684Seric ev->ev_func = func; 697684Seric ev->ev_arg = arg; 707931Seric ev->ev_pid = getpid(); 717684Seric ev->ev_link = *evp; 727684Seric *evp = ev; 737684Seric 748063Seric if (tTd(5, 5)) 757684Seric printf("setevent: intvl=%ld, for=%ld, func=%x, arg=%d, ev=%x\n", 767684Seric intvl, now + intvl, func, arg, ev); 777684Seric 787939Seric tick(); 797684Seric return (ev); 807684Seric } 817684Seric /* 827684Seric ** CLREVENT -- remove an event from the event queue. 837684Seric ** 847684Seric ** Parameters: 857684Seric ** ev -- pointer to event to remove. 867684Seric ** 877684Seric ** Returns: 887684Seric ** none. 897684Seric ** 907684Seric ** Side Effects: 917684Seric ** arranges for event ev to not happen. 927684Seric */ 937684Seric 947684Seric clrevent(ev) 957684Seric register EVENT *ev; 967684Seric { 977684Seric register EVENT **evp; 987684Seric 998063Seric if (tTd(5, 5)) 1007684Seric printf("clrevent: ev=%x\n", ev); 1017757Seric if (ev == NULL) 1027757Seric return; 1037684Seric 1047684Seric /* find the parent event */ 1059373Seric (void) signal(SIGALRM, SIG_IGN); 1067684Seric for (evp = &EventQueue; *evp != NULL; evp = &(*evp)->ev_link) 1077684Seric { 1087684Seric if (*evp == ev) 1097684Seric break; 1107684Seric } 1117684Seric 1127684Seric /* now remove it */ 1137939Seric if (*evp != NULL) 1147939Seric { 1157939Seric *evp = ev->ev_link; 1169346Seric free((char *) ev); 1177939Seric } 1187862Seric 1197862Seric /* restore clocks and pick up anything spare */ 1207862Seric tick(); 1217684Seric } 1227684Seric /* 1237358Seric ** TICK -- take a clock tick 1247358Seric ** 1257684Seric ** Called by the alarm clock. This routine runs events as needed. 1267358Seric ** 1277358Seric ** Parameters: 1287358Seric ** none. 1297358Seric ** 1307358Seric ** Returns: 1317684Seric ** none. 1327358Seric ** 1337358Seric ** Side Effects: 1347684Seric ** calls the next function in EventQueue. 1357358Seric */ 1367358Seric 13746979Sbostic static void 1387358Seric tick() 1397358Seric { 1407889Seric register time_t now; 1417684Seric register EVENT *ev; 14224955Seric int mypid = getpid(); 14363787Seric #ifdef SIG_UNBLOCK 14463787Seric sigset_t ss; 14563787Seric #endif 1467684Seric 1479373Seric (void) signal(SIGALRM, SIG_IGN); 1489373Seric (void) alarm(0); 1497889Seric now = curtime(); 1507684Seric 1518063Seric if (tTd(5, 4)) 1527684Seric printf("tick: now=%ld\n", now); 1537684Seric 1548128Seric while ((ev = EventQueue) != NULL && 15524955Seric (ev->ev_time <= now || ev->ev_pid != mypid)) 1567684Seric { 15716141Seric int (*f)(); 15816141Seric int arg; 15916141Seric int pid; 1607862Seric 1617684Seric /* process the event on the top of the queue */ 1627684Seric ev = EventQueue; 1637684Seric EventQueue = EventQueue->ev_link; 1648063Seric if (tTd(5, 6)) 1657931Seric printf("tick: ev=%x, func=%x, arg=%d, pid=%d\n", ev, 1667931Seric ev->ev_func, ev->ev_arg, ev->ev_pid); 1677862Seric 1687862Seric /* we must be careful in here because ev_func may not return */ 1699373Seric (void) signal(SIGALRM, tick); 17063787Seric #ifdef SIG_UNBLOCK 17163787Seric /* unblock SIGALRM signal */ 17263787Seric sigemptyset(&ss); 17363787Seric sigaddset(&ss, SIGALRM); 17463787Seric sigprocmask(SIG_UNBLOCK, &ss, NULL); 17563787Seric #else 17620844Seric #ifdef SIGVTALRM 17720844Seric /* reset 4.2bsd signal mask to allow future alarms */ 17820844Seric (void) sigsetmask(sigblock(0) & ~sigmask(SIGALRM)); 17956795Seric #endif /* SIGVTALRM */ 18063787Seric #endif /* SIG_UNBLOCK */ 18120844Seric 1827862Seric f = ev->ev_func; 18316141Seric arg = ev->ev_arg; 18416141Seric pid = ev->ev_pid; 1859346Seric free((char *) ev); 18616141Seric if (pid != getpid()) 1877931Seric continue; 1887862Seric if (EventQueue != NULL) 1897862Seric { 1907862Seric if (EventQueue->ev_time > now) 19112635Seric (void) alarm((unsigned) (EventQueue->ev_time - now)); 1927862Seric else 1937889Seric (void) alarm(3); 1947862Seric } 19516141Seric (*f)(arg); 1967862Seric (void) alarm(0); 1977889Seric now = curtime(); 1987684Seric } 1999373Seric (void) signal(SIGALRM, tick); 2007889Seric if (EventQueue != NULL) 20112635Seric (void) alarm((unsigned) (EventQueue->ev_time - now)); 2027358Seric } 2037690Seric /* 2047690Seric ** SLEEP -- a version of sleep that works with this stuff 2057690Seric ** 2067690Seric ** Because sleep uses the alarm facility, I must reimplement 2077690Seric ** it here. 2087690Seric ** 2097690Seric ** Parameters: 2107690Seric ** intvl -- time to sleep. 2117690Seric ** 2127690Seric ** Returns: 2137690Seric ** none. 2147690Seric ** 2157690Seric ** Side Effects: 2167690Seric ** waits for intvl time. However, other events can 2177690Seric ** be run during that interval. 2187690Seric */ 2197690Seric 2207690Seric static bool SleepDone; 22163937Seric static int endsleep(); 2227690Seric 223*63968Seric #ifndef SLEEP_T 224*63968Seric # define SLEEP_T unsigned int 225*63968Seric #endif 226*63968Seric 227*63968Seric SLEEP_T 2287690Seric sleep(intvl) 22925615Seric unsigned int intvl; 2307690Seric { 2317757Seric if (intvl == 0) 2327757Seric return; 2337690Seric SleepDone = FALSE; 23412635Seric (void) setevent((time_t) intvl, endsleep, 0); 2357690Seric while (!SleepDone) 2367690Seric pause(); 2377690Seric } 2387690Seric 2397690Seric static 2407690Seric endsleep() 2417690Seric { 2427690Seric SleepDone = TRUE; 2437690Seric } 244