xref: /original-bsd/usr.sbin/sendmail/src/clock.c (revision c3e32dec)
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.1 (Berkeley) 06/07/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <signal.h>
15 
16 # ifndef sigmask
17 #  define sigmask(s)	(1 << ((s) - 1))
18 # endif
19 
20 /*
21 **  SETEVENT -- set an event to happen at a specific time.
22 **
23 **	Events are stored in a sorted list for fast processing.
24 **	An event only applies to the process that set it.
25 **
26 **	Parameters:
27 **		intvl -- intvl until next event occurs.
28 **		func -- function to call on event.
29 **		arg -- argument to func on event.
30 **
31 **	Returns:
32 **		none.
33 **
34 **	Side Effects:
35 **		none.
36 */
37 
38 static void tick();
39 
40 EVENT *
41 setevent(intvl, func, arg)
42 	time_t intvl;
43 	int (*func)();
44 	int arg;
45 {
46 	register EVENT **evp;
47 	register EVENT *ev;
48 	auto time_t now;
49 
50 	if (intvl <= 0)
51 	{
52 		syserr("554 setevent: intvl=%ld\n", intvl);
53 		return (NULL);
54 	}
55 
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) signal(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 
144 	(void) signal(SIGALRM, SIG_IGN);
145 	(void) alarm(0);
146 	now = curtime();
147 
148 	if (tTd(5, 4))
149 		printf("tick: now=%ld\n", now);
150 
151 	while ((ev = EventQueue) != NULL &&
152 	       (ev->ev_time <= now || ev->ev_pid != mypid))
153 	{
154 		int (*f)();
155 		int arg;
156 		int pid;
157 
158 		/* process the event on the top of the queue */
159 		ev = EventQueue;
160 		EventQueue = EventQueue->ev_link;
161 		if (tTd(5, 6))
162 			printf("tick: ev=%x, func=%x, arg=%d, pid=%d\n", ev,
163 				ev->ev_func, ev->ev_arg, ev->ev_pid);
164 
165 		/* we must be careful in here because ev_func may not return */
166 		(void) signal(SIGALRM, tick);
167 #ifdef SIGVTALRM
168 		/* reset 4.2bsd signal mask to allow future alarms */
169 		(void) sigsetmask(sigblock(0) & ~sigmask(SIGALRM));
170 #endif /* SIGVTALRM */
171 
172 		f = ev->ev_func;
173 		arg = ev->ev_arg;
174 		pid = ev->ev_pid;
175 		free((char *) ev);
176 		if (pid != getpid())
177 			continue;
178 		if (EventQueue != NULL)
179 		{
180 			if (EventQueue->ev_time > now)
181 				(void) alarm((unsigned) (EventQueue->ev_time - now));
182 			else
183 				(void) alarm(3);
184 		}
185 		(*f)(arg);
186 		(void) alarm(0);
187 		now = curtime();
188 	}
189 	(void) signal(SIGALRM, tick);
190 	if (EventQueue != NULL)
191 		(void) alarm((unsigned) (EventQueue->ev_time - now));
192 }
193 /*
194 **  SLEEP -- a version of sleep that works with this stuff
195 **
196 **	Because sleep uses the alarm facility, I must reimplement
197 **	it here.
198 **
199 **	Parameters:
200 **		intvl -- time to sleep.
201 **
202 **	Returns:
203 **		none.
204 **
205 **	Side Effects:
206 **		waits for intvl time.  However, other events can
207 **		be run during that interval.
208 */
209 
210 static bool	SleepDone;
211 
212 unsigned int
213 sleep(intvl)
214 	unsigned int intvl;
215 {
216 	static int endsleep();
217 
218 	if (intvl == 0)
219 		return;
220 	SleepDone = FALSE;
221 	(void) setevent((time_t) intvl, endsleep, 0);
222 	while (!SleepDone)
223 		pause();
224 }
225 
226 static
227 endsleep()
228 {
229 	SleepDone = TRUE;
230 }
231