1 /* timer.c --- daemon to provide a tagged interval timer service
3 This little daemon runs forever waiting for signals. SIGIO (or
4 SIGUSR1) causes it to read an event spec from stdin; that is, a
5 date followed by colon followed by an event label. SIGALRM causes
6 it to check its queue for events attached to the current second; if
7 one is found, its label is written to stdout. SIGTERM causes it to
8 terminate, printing a list of pending events.
10 This program is intended to be used with the lisp package called
11 timer.el. It was written anonymously in 1990. This version was
12 documented and rewritten for portability by esr@snark.thyrsus.com,
17 #include <fcntl.h> /* FASYNC */
18 #include <sys/types.h> /* time_t */
20 #include <../src/config.h>
27 /* Perhaps this is correct unconditionally. */
33 extern char *sys_errlist
[], *malloc ();
34 extern time_t time ();
37 * The field separator for input. This character shouldn't be legal in a date,
38 * and should be printable so event strings are readable by people. Was
39 * originally ';', then got changed to bogus `\001'.
48 int events_size
; /* How many slots have we allocated? */
49 int num_events
; /* How many are actually scheduled? */
50 struct event
*events
; /* events[0 .. num_events-1] are the
53 char *pname
; /* programme name for error messages */
55 /* Accepts a string of two fields separated by FS.
56 First field is string for get_date, saying when to wake-up.
57 Second field is a token to identify the request. */
62 extern time_t get_date ();
63 extern char *strcpy ();
66 static struct event
*ep
;
68 /* check entry format */
69 for (p
= str
; *p
&& *p
!= FS
; p
++)
73 fprintf (stderr
, "%s: bad input format: %s\n", pname
, str
);
78 /* allocate an event slot */
79 ep
= events
+ num_events
;
81 /* If the event array is full, stretch it. After stretching, we know
82 that ep will be pointing to an available event spot. */
83 if (ep
== events
+ events_size
)
85 int old_size
= events_size
;
88 events
= ((struct event
*)
89 realloc (events
, events_size
* sizeof (struct event
)));
92 fprintf (stderr
, "%s: virtual memory exhausted.\n", pname
);
94 /* Should timer exit now? Well, we've still got other
95 events in the queue, and more memory might become
96 available in the future, so we'll just toss this event.
97 This will screw up whoever scheduled the event, but
98 maybe someone else will survive. */
102 while (old_size
< events_size
)
103 events
[old_size
++].token
= NULL
;
106 /* Don't allow users to schedule events in past time. */
107 ep
->reply_at
= get_date (str
, NULL
);
108 if (ep
->reply_at
- time (&now
) < 0)
110 fprintf (stderr
, "%s: bad time spec: %s%c%s\n", pname
, str
, FS
, p
);
114 /* save the event description */
115 ep
->token
= (char *) malloc ((unsigned) strlen (p
) + 1);
118 fprintf (stderr
, "%s: malloc %s: %s%c%s\n",
119 pname
, sys_errlist
[errno
], str
, FS
, p
);
123 strcpy (ep
->token
, p
);
130 time_t now
, tdiff
, waitfor
= -1;
131 register struct event
*ep
;
133 /* If an alarm timer runs out while this function is executing,
134 it could get called recursively. This would be bad, because
135 it's not re-entrant. So we must try to suspend the signal. */
136 #if 0 /* This function isn't right for BSD. Fix it later. */
140 now
= time ((time_t *) NULL
);
142 for (ep
= events
; ep
< events
+ num_events
; ep
++)
143 /* Are any events ready to fire? */
144 if (ep
->reply_at
<= now
)
146 fputs (ep
->token
, stdout
);
151 /* We now have a hole in the event array; fill it with the last
153 ep
->token
= events
[num_events
- 1].token
;
154 ep
->reply_at
= events
[num_events
- 1].reply_at
;
157 /* We ought to scan this event again. */
162 /* next timeout should be the soonest of any remaining */
163 if ((tdiff
= ep
->reply_at
- now
) < waitfor
|| waitfor
< 0)
164 waitfor
= (long)tdiff
;
167 /* If there are no more events, we needn't bother setting an alarm. */
171 #if 0 /* This function isn't right for BSD. */
183 /* In principle the itimer should be disabled on entry to this
184 function, but it really doesn't make any important difference
188 buf
= (char *) malloc (buf_size
);
190 /* Read a line from standard input, expanding buf if it is too short
199 buf
= (char *) realloc (buf
, buf_size
);
201 /* If we're out of memory, toss this event. */
206 while (c
!= '\n' && c
!= EOF
);
225 /* Register the event. */
229 /* Who knows what this interrupted, or if it said "now"? */
236 /* dispatch on incoming signal, then restore it */
249 fprintf (stderr
, "Events still queued:\n");
250 for (ep
= events
; ep
< events
+ num_events
; ep
++)
251 fprintf (stderr
, "%d = %ld @ %s\n",
252 ep
- events
, ep
->reply_at
, ep
->token
);
257 /* required on older UNIXes; harmless on newer ones */
258 signal (sig
, sigcatch
);
267 for (pname
= argv
[0] + strlen (argv
[0]);
268 *pname
!= '/' && pname
!= argv
[0];
274 events
= ((struct event
*) malloc (events_size
* sizeof (*events
)));
277 signal (SIGIO
, sigcatch
);
278 signal (SIGALRM
, sigcatch
);
279 signal (SIGTERM
, sigcatch
);
282 if (fcntl (0, F_SETOWN
, getpid ()) == -1)
284 fprintf (stderr
, "%s: can't set ownership of stdin\n", pname
);
285 fprintf (stderr
, "%s\n", sys_errlist
[errno
]);
288 if (fcntl (0, F_SETFL
, fcntl (0, F_GETFL
, 0) | FASYNC
) == -1)
290 fprintf (stderr
, "%s: can't request asynchronous I/O on stdin\n", pname
);
291 fprintf (stderr
, "%s\n", sys_errlist
[errno
]);
296 /* In case Emacs sent some input before we set up
297 the handling of SIGIO, read it now. */
304 /* timer.c ends here */