1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static char rcsid[] = "$Id: entry.c,v 2.12 1994/01/17 03:20:37 vixie Exp $";
22 /* vix 26jan87 [RCS'd; rest of log is in RCS file]
23 * vix 01jan87 [added line-level error recovery]
24 * vix 31dec86 [added /step to the from-to range, per bob@acornrc]
25 * vix 30dec86 [written]
33 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
34 e_cmd, e_timespec, e_username
37 static char get_list __P((bitstr_t *, int, int, char *[], int, FILE *)),
38 get_range __P((bitstr_t *, int, int, char *[], int, FILE *)),
39 get_number __P((int *, int, char *[], int, FILE *));
40 static int set_element __P((bitstr_t *, int, int, int));
42 static char *ecodes[] =
66 /* return NULL if eof or syntax error occurs;
67 * otherwise return a pointer to a new entry.
70 load_entry(file, error_func, pw, envp)
76 /* this function reads one crontab entry -- the next -- from a file.
77 * it skips any leading blank lines, ignores comments, and returns
78 * EOF if for any reason the entry can't be read and parsed.
80 * the entry is also parsed here.
84 * minutes hours doms months dows cmd\n
85 * system crontab (/etc/crontab):
86 * minutes hours doms months dows USERNAME cmd\n
89 ecode_e ecode = e_none;
92 char cmd[MAX_COMMAND];
93 char envstr[MAX_ENVSTR];
95 Debug(DPARS, ("load_entry()...about to eat comments\n"))
103 /* ch is now the first useful character of a useful line.
104 * it may be an @special or it may be the first character
105 * of a list of minutes.
108 e = (entry *) calloc(sizeof(entry), sizeof(char));
111 /* all of these should be flagged and load-limited; i.e.,
112 * instead of @hourly meaning "0 * * * *" it should mean
113 * "close to the front of every hour but not 'til the
114 * system load is low". Problems are: how do you know
115 * what "low" means? (save me from /etc/cron.conf!) and:
116 * how to guarantee low variance (how low is low?), which
117 * means how to we run roughly every hour -- seems like
118 * we need to keep a history or let the first hour set
119 * the schedule, which means we aren't load-limited
120 * anymore. too much for my overloaded brain. (vix, jan90)
123 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
124 if (!strcmp("reboot", cmd)) {
125 e->flags |= WHEN_REBOOT;
126 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
127 bit_set(e->minute, 0);
130 bit_set(e->month, 0);
131 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
132 } else if (!strcmp("monthly", cmd)) {
133 bit_set(e->minute, 0);
136 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
137 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
138 } else if (!strcmp("weekly", cmd)) {
139 bit_set(e->minute, 0);
141 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
142 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
144 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
145 bit_set(e->minute, 0);
147 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
148 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
149 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
150 } else if (!strcmp("hourly", cmd)) {
151 bit_set(e->minute, 0);
152 bit_set(e->hour, (LAST_HOUR-FIRST_HOUR+1));
153 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
154 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
155 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
161 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
163 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
173 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
180 /* DOM (days of month)
184 e->flags |= DOM_STAR;
185 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
195 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
196 MonthNames, ch, file);
202 /* DOW (days of week)
206 e->flags |= DOW_STAR;
207 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
215 /* make sundays equivilent */
216 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
221 /* ch is the first character of a command, or a username */
222 unget_char(ch, file);
225 char *username = cmd; /* temp buffer */
227 Debug(DPARS, ("load_entry()...about to parse username\n"))
228 ch = get_string(username, MAX_COMMAND, file, " \t");
230 Debug(DPARS, ("load_entry()...got %s\n",username))
236 pw = getpwnam(username);
241 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",e->uid,e->gid))
247 /* copy and fix up environment. some variables are just defaults and
248 * others are overrides.
250 e->envp = env_copy(envp);
251 if (!env_get("SHELL", e->envp)) {
252 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
253 e->envp = env_set(e->envp, envstr);
255 if (!env_get("HOME", e->envp)) {
256 sprintf(envstr, "HOME=%s", pw->pw_dir);
257 e->envp = env_set(e->envp, envstr);
259 if (!env_get("PATH", e->envp)) {
260 sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
261 e->envp = env_set(e->envp, envstr);
263 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
264 e->envp = env_set(e->envp, envstr);
266 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
267 e->envp = env_set(e->envp, envstr);
270 Debug(DPARS, ("load_entry()...about to parse command\n"))
272 /* Everything up to the next \n or EOF is part of the command...
273 * too bad we don't know in advance how long it will be, since we
274 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
275 * XXX - should use realloc().
277 ch = get_string(cmd, MAX_COMMAND, file, "\n");
279 /* a file without a \n before the EOF is rude, so we'll complain...
286 /* got the command in the 'cmd' string; save it in *e.
288 e->cmd = strdup(cmd);
290 Debug(DPARS, ("load_entry()...returning successfully\n"))
292 /* success, fini, return pointer to the entry we just created...
298 if (ecode != e_none && error_func)
299 (*error_func)(ecodes[(int)ecode]);
300 while (ch != EOF && ch != '\n')
307 get_list(bits, low, high, names, ch, file)
308 bitstr_t *bits; /* one bit per flag, default=FALSE */
309 int low, high; /* bounds, impl. offset for bitstr */
310 char *names[]; /* NULL or *[] of names for these elements */
311 int ch; /* current character being processed */
312 FILE *file; /* file being read */
316 /* we know that we point to a non-blank character here;
317 * must do a Skip_Blanks before we exit, so that the
318 * next call (or the code that picks up the cmd) can
319 * assume the same thing.
322 Debug(DPARS|DEXT, ("get_list()...entered\n"))
324 /* list = range {"," range}
327 /* clear the bit string, since the default is 'off'.
329 bit_nclear(bits, 0, (high-low+1));
331 /* process all ranges
335 ch = get_range(bits, low, high, names, ch, file);
342 /* exiting. skip to some blanks, then skip over the blanks.
344 Skip_Nonblanks(ch, file)
345 Skip_Blanks(ch, file)
347 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
354 get_range(bits, low, high, names, ch, file)
355 bitstr_t *bits; /* one bit per flag, default=FALSE */
356 int low, high; /* bounds, impl. offset for bitstr */
357 char *names[]; /* NULL or names of elements */
358 int ch; /* current character being processed */
359 FILE *file; /* file being read */
361 /* range = number | number "-" number [ "/" number ]
365 auto int num1, num2, num3;
367 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
370 /* '*' means "first-last" but can still be modified by /step
378 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
382 /* not a range, it's a single number.
384 if (EOF == set_element(bits, low, high, num1))
394 /* get the number following the dash
396 ch = get_number(&num2, low, names, ch, file);
402 /* check for step size
411 /* get the step size -- note: we don't pass the
412 * names here, because the number is not an
413 * element id, it's a step size. 'low' is
414 * sent as a 0 since there is no offset either.
416 ch = get_number(&num3, 0, PPC_NULL, ch, file);
420 /* no step. default==1.
425 /* range. set all elements from num1 to num2, stepping
426 * by num3. (the step is a downward-compatible extension
427 * proposed conceptually by bob@acornrc, syntactically
428 * designed then implmented by paul vixie).
430 for (i = num1; i <= num2; i += num3)
431 if (EOF == set_element(bits, low, high, i))
439 get_number(numptr, low, names, ch, file)
440 int *numptr; /* where does the result go? */
441 int low; /* offset applied to result if symbolic enum used */
442 char *names[]; /* symbolic names, if any, for enums */
443 int ch; /* current character */
444 FILE *file; /* source */
446 char temp[MAX_TEMPSTR], *pc;
447 int len, i, all_digits;
449 /* collect alphanumerics into our fixed-size temp array
454 while (isalnum(ch)) {
455 if (++len >= MAX_TEMPSTR)
467 /* try to find the name in the name list
470 for (i = 0; names[i] != NULL; i++) {
472 ("get_num, compare(%s,%s)\n", names[i], temp))
473 if (!strcasecmp(names[i], temp)) {
480 /* no name list specified, or there is one and our string isn't
481 * in it. either way: if it's all digits, use its magnitude.
482 * otherwise, it's an error.
485 *numptr = atoi(temp);
494 set_element(bits, low, high, number)
495 bitstr_t *bits; /* one bit per flag, default=FALSE */
500 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
502 if (number < low || number > high)
505 bit_set(bits, (number-low));