958
|
1 /*
|
|
2 * timer.c --- daemon to provide a tagged interval timer service
|
|
3 *
|
|
4 * This little daemon runs forever waiting for signals. SIGIO (or SIGUSR1)
|
|
5 * causes it to read an event spec from stdin; that is, a date followed by
|
|
6 * colon followed by an event label. SIGALRM causes it to check its queue
|
|
7 * for events attached to the current second; if one is found, its label
|
|
8 * is written to stdout. SIGTERM causes it to terminate, printing a list
|
|
9 * of pending events.
|
|
10 *
|
|
11 * This program is intended to be used with the lisp package called timer.el.
|
|
12 * It was written anonymously in 1990. This version was documented and
|
|
13 * rewritten for portability by esr@snark,thyrsus.com, Aug 7 1992.
|
|
14 */
|
45
|
15 #include <stdio.h>
|
|
16 #include <signal.h>
|
|
17 #include <fcntl.h> /* FASYNC */
|
958
|
18 #include <sys/types.h> /* time_t */
|
|
19
|
|
20 #include "../src/config.h"
|
|
21 #ifdef USG
|
|
22 #undef SIGIO
|
|
23 #define SIGIO SIGUSR1
|
45
|
24 #endif
|
|
25
|
|
26 extern int errno;
|
|
27 extern char *sys_errlist[], *malloc();
|
|
28 extern time_t time();
|
|
29
|
|
30 #define MAXEVENTS 256
|
958
|
31
|
|
32 /*
|
|
33 * The field separator for input. This character shouldn't be legal in a date,
|
|
34 * and should be printable so event strings are readable by people. Was
|
|
35 * originally ';', then got changed to bogus `\001'.
|
|
36 */
|
|
37 #define FS '@'
|
45
|
38
|
958
|
39 struct event
|
|
40 {
|
|
41 char *token;
|
|
42 time_t reply_at;
|
|
43 }
|
|
44 events[MAXEVENTS];
|
45
|
45
|
|
46 char *pname; /* programme name for error messages */
|
|
47
|
958
|
48 /* Accepts a string of two fields seperated by FS.
|
45
|
49 * First field is string for getdate, saying when to wake-up.
|
|
50 * Second field is a token to identify the request.
|
|
51 */
|
958
|
52 void schedule(str)
|
|
53 char *str;
|
45
|
54 {
|
958
|
55 extern time_t getdate();
|
|
56 extern char *strcpy();
|
|
57 time_t now;
|
|
58 register char *p;
|
|
59 static struct event *ep;
|
|
60
|
|
61 #ifdef DEBUG
|
|
62 (void) fprintf(stderr, "Timer sees: %s", str);
|
|
63 #endif /* DEBUG */
|
45
|
64
|
958
|
65 /* check entry format */
|
|
66 for(p = str; *p && *p != FS; p++)
|
|
67 continue;
|
|
68 if (!*p)
|
|
69 {
|
|
70 (void)fprintf(stderr, "%s: bad input format: %s", pname, str);
|
|
71 return;
|
|
72 }
|
|
73 *p++ = 0;
|
45
|
74
|
958
|
75 /* allocate an event slot */
|
|
76 for(ep = events; ep < events + MAXEVENTS; ep++)
|
|
77 if (ep->token == (char *)NULL)
|
|
78 break;
|
|
79 if (ep == events + MAXEVENTS)
|
|
80 (void) fprintf(stderr, "%s: too many events: %s", pname, str);
|
|
81
|
|
82 /* don't allow users to schedule events in past time */
|
|
83 else if ((ep->reply_at = get_date(str, NULL)) - time(&now) < 0)
|
|
84 (void)fprintf(stderr, "%s: bad time spec: %s%c%s", pname, str, FS, p);
|
45
|
85
|
958
|
86 /* save the event description */
|
|
87 else if ((ep->token = malloc((unsigned)strlen(p) + 1)) == NULL)
|
|
88 (void)fprintf(stderr, "%s: malloc %s: %s%c%s",
|
|
89 pname, sys_errlist[errno], str, FS, p);
|
|
90 else
|
|
91 {
|
|
92 (void)strcpy(ep->token, p);
|
45
|
93
|
958
|
94 #ifdef DEBUG
|
|
95 (void) fprintf(stderr,
|
|
96 "New event: %ld: %s", ep->reply_at, ep->token);
|
|
97 #endif /* DEBUG */
|
|
98 }
|
45
|
99 }
|
|
100
|
|
101 void
|
|
102 notify()
|
958
|
103 {
|
|
104 time_t now, tdiff, waitfor = -1;
|
|
105 register struct event *ep;
|
45
|
106
|
958
|
107 now = time((time_t *)NULL);
|
45
|
108
|
958
|
109 for(ep = events; ep < events + MAXEVENTS; ep++)
|
|
110 if (ep->token)
|
|
111 {
|
|
112 /* any events ready to fire? */
|
|
113 if (ep->reply_at <= now)
|
|
114 {
|
|
115 #ifdef DEBUG
|
|
116 (void) fprintf(stderr,
|
|
117 "Event %d firing: %ld @ %s",
|
|
118 (ep - events), ep->reply_at, ep->token);
|
|
119 #endif /* DEBUG */
|
|
120 (void)fputs(ep->token, stdout);
|
|
121 free(ep->token);
|
|
122 ep->token = (char *)NULL;
|
|
123 }
|
|
124 else
|
|
125 {
|
|
126 #ifdef DEBUG
|
|
127 (void) fprintf(stderr,
|
|
128 "Event %d still waiting: %ld @ %s",
|
|
129 (ep - events), ep->reply_at, ep->token);
|
|
130 #endif /* DEBUG */
|
45
|
131
|
958
|
132 /* next timeout should be the soonest of any remaining */
|
|
133 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0)
|
|
134 waitfor = (long)tdiff;
|
|
135 }
|
|
136 }
|
|
137
|
|
138 /* If there's no more events, SIGIO should be next wake-up */
|
|
139 if (waitfor != -1)
|
|
140 {
|
|
141 #ifdef DEBUG
|
|
142 (void) fprintf(stderr,
|
|
143 "Setting %d-second alarm\n", waitfor);
|
|
144 #endif /* DEBUG */
|
|
145 (void)alarm(waitfor);
|
45
|
146 }
|
|
147 }
|
|
148
|
|
149 void
|
|
150 getevent()
|
958
|
151 {
|
|
152 extern char *fgets();
|
|
153 struct event *ep;
|
|
154 char buf[BUFSIZ];
|
45
|
155
|
958
|
156 /* in principle the itimer should be disabled on entry to this function,
|
|
157 but it really doesn't make any important difference if it isn't */
|
|
158
|
|
159 if (fgets(buf, sizeof(buf), stdin) == NULL)
|
|
160 exit(0);
|
45
|
161
|
958
|
162 /* register the event */
|
|
163 schedule(buf);
|
45
|
164
|
958
|
165 /* Who knows what this interrupted, or if it said "now"? */
|
|
166 notify();
|
|
167 }
|
45
|
168
|
958
|
169 void
|
|
170 sigcatch(sig)
|
|
171 /* dispatch on incoming signal, then restore it */
|
|
172 {
|
|
173 struct event *ep;
|
45
|
174
|
958
|
175 switch(sig)
|
|
176 {
|
|
177 case SIGALRM:
|
|
178 #ifdef DEBUG
|
|
179 (void) fprintf(stderr, "Alarm signal received\n");
|
|
180 #endif /* DEBUG */
|
|
181 notify();
|
|
182 break;
|
|
183 case SIGIO:
|
|
184 getevent();
|
|
185 break;
|
|
186 case SIGTERM:
|
|
187 (void) fprintf(stderr, "Events still queued:\n");
|
|
188 for (ep = events; ep < events + MAXEVENTS; ep++)
|
|
189 if (ep->token)
|
|
190 (void) fprintf(stderr, "%d = %ld @ %s",
|
|
191 ep - events, ep->reply_at, ep->token);
|
|
192 exit(0);
|
|
193 break;
|
|
194 }
|
45
|
195
|
958
|
196 /* required on older UNIXes; harmless on newer ones */
|
|
197 (void) signal(sig, sigcatch);
|
45
|
198 }
|
958
|
199
|
45
|
200 /*ARGSUSED*/
|
|
201 int
|
|
202 main(argc, argv)
|
|
203 int argc;
|
|
204 char **argv;
|
|
205 {
|
|
206 for (pname = argv[0] + strlen(argv[0]); *pname != '/' && pname != argv[0];
|
|
207 pname--);
|
|
208 if (*pname == '/') pname++;
|
|
209
|
958
|
210 (void)signal(SIGIO, sigcatch);
|
|
211 (void)signal(SIGALRM, sigcatch);
|
|
212 (void)signal(SIGTERM, sigcatch);
|
|
213
|
|
214 #ifndef USG
|
45
|
215 (void)fcntl(0, F_SETFL, FASYNC);
|
958
|
216 #endif /* USG */
|
45
|
217
|
|
218 while (1) pause();
|
|
219 }
|
958
|
220
|
|
221 /* timer.c ends here */
|