xref: /csrg-svn/usr.sbin/sendmail/src/clock.c (revision 60601)
122696Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
642824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822696Sdist 
922696Sdist #ifndef lint
10*60601Seric static char sccsid[] = "@(#)clock.c	6.4 (Berkeley) 05/30/93";
1133728Sbostic #endif /* not lint */
1222696Sdist 
137358Seric # include "sendmail.h"
1411728Seric # include <signal.h>
157358Seric 
16*60601Seric # ifndef sigmask
17*60601Seric #  define sigmask(s)	(1 << ((s) - 1))
18*60601Seric # endif
19*60601Seric 
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();
1437684Seric 
1449373Seric 	(void) signal(SIGALRM, SIG_IGN);
1459373Seric 	(void) alarm(0);
1467889Seric 	now = curtime();
1477684Seric 
1488063Seric 	if (tTd(5, 4))
1497684Seric 		printf("tick: now=%ld\n", now);
1507684Seric 
1518128Seric 	while ((ev = EventQueue) != NULL &&
15224955Seric 	       (ev->ev_time <= now || ev->ev_pid != mypid))
1537684Seric 	{
15416141Seric 		int (*f)();
15516141Seric 		int arg;
15616141Seric 		int pid;
1577862Seric 
1587684Seric 		/* process the event on the top of the queue */
1597684Seric 		ev = EventQueue;
1607684Seric 		EventQueue = EventQueue->ev_link;
1618063Seric 		if (tTd(5, 6))
1627931Seric 			printf("tick: ev=%x, func=%x, arg=%d, pid=%d\n", ev,
1637931Seric 				ev->ev_func, ev->ev_arg, ev->ev_pid);
1647862Seric 
1657862Seric 		/* we must be careful in here because ev_func may not return */
1669373Seric 		(void) signal(SIGALRM, tick);
16720844Seric #ifdef SIGVTALRM
16820844Seric 		/* reset 4.2bsd signal mask to allow future alarms */
16920844Seric 		(void) sigsetmask(sigblock(0) & ~sigmask(SIGALRM));
17056795Seric #endif /* SIGVTALRM */
17120844Seric 
1727862Seric 		f = ev->ev_func;
17316141Seric 		arg = ev->ev_arg;
17416141Seric 		pid = ev->ev_pid;
1759346Seric 		free((char *) ev);
17616141Seric 		if (pid != getpid())
1777931Seric 			continue;
1787862Seric 		if (EventQueue != NULL)
1797862Seric 		{
1807862Seric 			if (EventQueue->ev_time > now)
18112635Seric 				(void) alarm((unsigned) (EventQueue->ev_time - now));
1827862Seric 			else
1837889Seric 				(void) alarm(3);
1847862Seric 		}
18516141Seric 		(*f)(arg);
1867862Seric 		(void) alarm(0);
1877889Seric 		now = curtime();
1887684Seric 	}
1899373Seric 	(void) signal(SIGALRM, tick);
1907889Seric 	if (EventQueue != NULL)
19112635Seric 		(void) alarm((unsigned) (EventQueue->ev_time - now));
1927358Seric }
1937690Seric /*
1947690Seric **  SLEEP -- a version of sleep that works with this stuff
1957690Seric **
1967690Seric **	Because sleep uses the alarm facility, I must reimplement
1977690Seric **	it here.
1987690Seric **
1997690Seric **	Parameters:
2007690Seric **		intvl -- time to sleep.
2017690Seric **
2027690Seric **	Returns:
2037690Seric **		none.
2047690Seric **
2057690Seric **	Side Effects:
2067690Seric **		waits for intvl time.  However, other events can
2077690Seric **		be run during that interval.
2087690Seric */
2097690Seric 
2107690Seric static bool	SleepDone;
2117690Seric 
21258332Seric unsigned int
2137690Seric sleep(intvl)
21425615Seric 	unsigned int intvl;
2157690Seric {
21646928Sbostic 	static int endsleep();
2177690Seric 
2187757Seric 	if (intvl == 0)
2197757Seric 		return;
2207690Seric 	SleepDone = FALSE;
22112635Seric 	(void) setevent((time_t) intvl, endsleep, 0);
2227690Seric 	while (!SleepDone)
2237690Seric 		pause();
2247690Seric }
2257690Seric 
2267690Seric static
2277690Seric endsleep()
2287690Seric {
2297690Seric 	SleepDone = TRUE;
2307690Seric }
231