1 /* timer.c --- daemon to provide a tagged interval timer service
3 This little daemon runs forever waiting for commands to schedule events.
5 it to check its queue for events attached to the current second; if
6 one is found, its label is written to stdout. SIGTERM causes it to
7 terminate, printing a list of pending events.
9 This program is intended to be used with the lisp package called
10 timer.el. The first such program was written anonymously in 1990.
11 This version was documented and rewritten for portability by
12 esr@snark.thyrsus.com, Aug 7 1992. */
17 #include <sys/types.h> /* time_t */
19 #include <../src/config.h>
23 /* Perhaps this is correct unconditionally. */
27 /* I agree with the comment above, this probably should be unconditional (it
28 * is already unconditional in a couple of other files in this directory),
29 * but in the spirit of minimizing the effects of my port, I am making it
30 * conditional on _CX_UX.
37 extern char *strerror ();
38 extern time_t time ();
41 * The field separator for input. This character shouldn't occur in dates,
42 * and should be printable so event strings are readable by people.
51 int events_size
; /* How many slots have we allocated? */
52 int num_events
; /* How many are actually scheduled? */
53 struct event
*events
; /* events[0 .. num_events-1] are the
56 char *pname
; /* program name for error messages */
58 /* This buffer is used for reading commands.
59 We make it longer when necessary, but we never free it. */
61 /* This is the allocated size of buf. */
64 /* Non-zero means don't handle an alarm now;
65 instead, just set alarm_deferred if an alarm happens.
66 We set this around parts of the program that call malloc and free. */
69 /* Non-zero if an alarm came in during the reading of a command. */
72 /* Schedule one event, and arrange an alarm for it.
73 STR is a string of two fields separated by FS.
74 First field is string for get_date, saying when to wake-up.
75 Second field is a token to identify the request. */
81 extern time_t get_date ();
82 extern char *strcpy ();
85 static struct event
*ep
;
87 /* check entry format */
88 for (p
= str
; *p
&& *p
!= FS
; p
++)
92 fprintf (stderr
, "%s: bad input format: %s\n", pname
, str
);
97 /* allocate an event slot */
98 ep
= events
+ num_events
;
100 /* If the event array is full, stretch it. After stretching, we know
101 that ep will be pointing to an available event spot. */
102 if (ep
== events
+ events_size
)
104 int old_size
= events_size
;
107 events
= ((struct event
*)
108 realloc (events
, events_size
* sizeof (struct event
)));
111 fprintf (stderr
, "%s: virtual memory exhausted.\n", pname
);
112 /* Since there is so much virtual memory, and running out
113 almost surely means something is very very wrong,
114 it is best to exit rather than continue. */
118 while (old_size
< events_size
)
119 events
[old_size
++].token
= NULL
;
122 /* Don't allow users to schedule events in past time. */
123 ep
->reply_at
= get_date (str
, NULL
);
124 if (ep
->reply_at
- time (&now
) < 0)
126 fprintf (stderr
, "%s: bad time spec: %s%c%s\n", pname
, str
, FS
, p
);
130 /* save the event description */
131 ep
->token
= (char *) malloc ((unsigned) strlen (p
) + 1);
134 fprintf (stderr
, "%s: malloc %s: %s%c%s\n",
135 pname
, strerror (errno
), str
, FS
, p
);
139 strcpy (ep
->token
, p
);
143 /* Print the notification for the alarmed event just arrived if any,
144 and schedule an alarm for the next event if any. */
149 time_t now
, tdiff
, waitfor
= -1;
150 register struct event
*ep
;
152 /* Inhibit interference with alarms while changing global vars. */
156 now
= time ((time_t *) NULL
);
158 for (ep
= events
; ep
< events
+ num_events
; ep
++)
159 /* Are any events ready to fire? */
160 if (ep
->reply_at
<= now
)
162 fputs (ep
->token
, stdout
);
167 /* We now have a hole in the event array; fill it with the last
169 ep
->token
= events
[num_events
- 1].token
;
170 ep
->reply_at
= events
[num_events
- 1].reply_at
;
173 /* We ought to scan this event again. */
178 /* next timeout should be the soonest of any remaining */
179 if ((tdiff
= ep
->reply_at
- now
) < waitfor
|| waitfor
< 0)
180 waitfor
= (long)tdiff
;
183 /* If there are no more events, we needn't bother setting an alarm. */
187 /* Now check if there was another alarm
188 while we were handling an explicit request. */
195 /* Read one command from command from standard input
196 and schedule the event for it. */
203 /* In principle the itimer should be disabled on entry to this
204 function, but it really doesn't make any important difference
210 buf
= (char *) malloc (buf_size
);
213 /* Read a line from standard input, expanding buf if it is too short
225 buf
= (char *) realloc (buf
, buf_size
);
232 /* Read one character into c. */
235 nread
= read (fileno (stdin
), &c
, 1);
237 /* Retry after transient error. */
249 /* Report serious errors. */
272 /* Register the event. */
281 /* Handle incoming signal SIG. */
289 /* required on older UNIXes; harmless on newer ones */
290 signal (sig
, sigcatch
);
301 fprintf (stderr
, "Events still queued:\n");
302 for (ep
= events
; ep
< events
+ num_events
; ep
++)
303 fprintf (stderr
, "%d = %ld @ %s\n",
304 ep
- events
, ep
->reply_at
, ep
->token
);
316 for (pname
= argv
[0] + strlen (argv
[0]);
317 *pname
!= '/' && pname
!= argv
[0];
323 events
= ((struct event
*) malloc (events_size
* sizeof (*events
)));
326 signal (SIGALRM
, sigcatch
);
327 signal (SIGTERM
, sigcatch
);
329 /* Loop reading commands from standard input
330 and scheduling alarms accordingly.
331 The alarms are handled asynchronously, while we wait for commands. */
336 #ifndef HAVE_STRERROR
341 extern char *sys_errlist
[];
344 if (errnum
>= 0 && errnum
< sys_nerr
)
345 return sys_errlist
[errnum
];
346 return (char *) "Unknown error";
349 #endif /* ! HAVE_STRERROR */
357 val
= (long *) malloc (size
);
361 fprintf (stderr
, "timer: virtual memory exceeded\n");
368 /* timer.c ends here */