1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
|
/* tab.c - process crontabs and create in-core crontab data
* Author: Kees J. Bot
* 7 Dec 1996
* Changes:
* 17 Jul 2000 by Philip Homburg
* - Tab_reschedule() rewritten (and fixed).
*/
#define nil ((void*)0)
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include "misc.h"
#include "tab.h"
static int nextbit(bitmap_t map, int bit)
/* Return the next bit set in 'map' from 'bit' onwards, cyclic. */
{
for (;;) {
bit= (bit+1) & 63;
if (bit_isset(map, bit)) break;
}
return bit;
}
void tab_reschedule(cronjob_t *job)
/* Reschedule one job. Compute the next time to run the job in job->rtime.
*/
{
struct tm prevtm, nexttm, tmptm;
time_t nodst_rtime, dst_rtime;
/* AT jobs are only run once. */
if (job->atjob) { job->rtime= NEVER; return; }
/* Was the job scheduled late last time? */
if (job->late) job->rtime= now;
prevtm= *localtime(&job->rtime);
prevtm.tm_sec= 0;
nexttm= prevtm;
nexttm.tm_min++; /* Minimal increment */
for (;;)
{
if (nexttm.tm_min > 59)
{
nexttm.tm_min= 0;
nexttm.tm_hour++;
}
if (nexttm.tm_hour > 23)
{
nexttm.tm_min= 0;
nexttm.tm_hour= 0;
nexttm.tm_mday++;
}
if (nexttm.tm_mday > 31)
{
nexttm.tm_hour= nexttm.tm_min= 0;
nexttm.tm_mday= 1;
nexttm.tm_mon++;
}
if (nexttm.tm_mon >= 12)
{
nexttm.tm_hour= nexttm.tm_min= 0;
nexttm.tm_mday= 1;
nexttm.tm_mon= 0;
nexttm.tm_year++;
}
/* Verify tm_year. A crontab entry cannot restrict tm_year
* directly. However, certain dates (such as Feb, 29th) do
* not occur every year. We limit the difference between
* nexttm.tm_year and prevtm.tm_year to detect impossible dates
* (e.g, Feb, 31st). In theory every date occurs within a
* period of 4 years. However, some years at the end of a
* century are not leap years (e.g, the year 2100). An extra
* factor of 2 should be enough.
*/
if (nexttm.tm_year-prevtm.tm_year > 2*4)
{
job->rtime= NEVER;
return; /* Impossible combination */
}
if (!job->do_wday)
{
/* Verify the mon and mday fields. If do_wday and
* do_mday are both true we have to merge both
* schedules. This is done after the call to mktime.
*/
if (!bit_isset(job->mon, nexttm.tm_mon))
{
/* Clear other fields */
nexttm.tm_mday= 1;
nexttm.tm_hour= nexttm.tm_min= 0;
nexttm.tm_mon++;
continue;
}
/* Verify mday */
if (!bit_isset(job->mday, nexttm.tm_mday))
{
/* Clear other fields */
nexttm.tm_hour= nexttm.tm_min= 0;
nexttm.tm_mday++;
continue;
}
}
/* Verify hour */
if (!bit_isset(job->hour, nexttm.tm_hour))
{
/* Clear tm_min field */
nexttm.tm_min= 0;
nexttm.tm_hour++;
continue;
}
/* Verify min */
if (!bit_isset(job->min, nexttm.tm_min))
{
nexttm.tm_min++;
continue;
}
/* Verify that we don't have any problem with DST. Try
* tm_isdst=0 first. */
tmptm= nexttm;
tmptm.tm_isdst= 0;
#if 0
fprintf(stderr,
"tab_reschedule: trying %04d-%02d-%02d %02d:%02d:%02d isdst=0\n",
1900+nexttm.tm_year, nexttm.tm_mon+1,
nexttm.tm_mday, nexttm.tm_hour,
nexttm.tm_min, nexttm.tm_sec);
#endif
nodst_rtime= job->rtime= mktime(&tmptm);
if (job->rtime == -1) {
/* This should not happen. */
cronlog(LOG_ERR,
"mktime failed for %04d-%02d-%02d %02d:%02d:%02d",
1900+nexttm.tm_year, nexttm.tm_mon+1,
nexttm.tm_mday, nexttm.tm_hour,
nexttm.tm_min, nexttm.tm_sec);
job->rtime= NEVER;
return;
}
tmptm= *localtime(&job->rtime);
if (tmptm.tm_hour != nexttm.tm_hour ||
tmptm.tm_min != nexttm.tm_min)
{
assert(tmptm.tm_isdst);
tmptm= nexttm;
tmptm.tm_isdst= 1;
#if 0
fprintf(stderr,
"tab_reschedule: trying %04d-%02d-%02d %02d:%02d:%02d isdst=1\n",
1900+nexttm.tm_year, nexttm.tm_mon+1,
nexttm.tm_mday, nexttm.tm_hour,
nexttm.tm_min, nexttm.tm_sec);
#endif
dst_rtime= job->rtime= mktime(&tmptm);
if (job->rtime == -1) {
/* This should not happen. */
cronlog(LOG_ERR,
"mktime failed for %04d-%02d-%02d %02d:%02d:%02d\n",
1900+nexttm.tm_year, nexttm.tm_mon+1,
nexttm.tm_mday, nexttm.tm_hour,
nexttm.tm_min, nexttm.tm_sec);
job->rtime= NEVER;
return;
}
tmptm= *localtime(&job->rtime);
if (tmptm.tm_hour != nexttm.tm_hour ||
tmptm.tm_min != nexttm.tm_min)
{
assert(!tmptm.tm_isdst);
/* We have a problem. This time neither
* exists with DST nor without DST.
* Use the latest time, which should be
* nodst_rtime.
*/
assert(nodst_rtime > dst_rtime);
job->rtime= nodst_rtime;
#if 0
fprintf(stderr,
"During DST trans. %04d-%02d-%02d %02d:%02d:%02d\n",
1900+nexttm.tm_year, nexttm.tm_mon+1,
nexttm.tm_mday, nexttm.tm_hour,
nexttm.tm_min, nexttm.tm_sec);
#endif
}
}
/* Verify this the combination (tm_year, tm_mon, tm_mday). */
if (tmptm.tm_mday != nexttm.tm_mday ||
tmptm.tm_mon != nexttm.tm_mon ||
tmptm.tm_year != nexttm.tm_year)
{
/* Wrong day */
#if 0
fprintf(stderr, "Wrong day\n");
#endif
nexttm.tm_hour= nexttm.tm_min= 0;
nexttm.tm_mday++;
continue;
}
/* Check tm_wday */
if (job->do_wday && bit_isset(job->wday, tmptm.tm_wday))
{
/* OK, wday matched */
break;
}
/* Check tm_mday */
if (job->do_mday && bit_isset(job->mon, tmptm.tm_mon) &&
bit_isset(job->mday, tmptm.tm_mday))
{
/* OK, mon and mday matched */
break;
}
if (!job->do_wday && !job->do_mday)
{
/* No need to match wday and mday */
break;
}
/* Wrong day */
#if 0
fprintf(stderr, "Wrong mon+mday or wday\n");
#endif
nexttm.tm_hour= nexttm.tm_min= 0;
nexttm.tm_mday++;
}
#if 0
fprintf(stderr, "Using %04d-%02d-%02d %02d:%02d:%02d \n",
1900+nexttm.tm_year, nexttm.tm_mon+1, nexttm.tm_mday,
nexttm.tm_hour, nexttm.tm_min, nexttm.tm_sec);
tmptm= *localtime(&job->rtime);
fprintf(stderr, "Act. %04d-%02d-%02d %02d:%02d:%02d isdst=%d\n",
1900+tmptm.tm_year, tmptm.tm_mon+1, tmptm.tm_mday,
tmptm.tm_hour, tmptm.tm_min, tmptm.tm_sec,
tmptm.tm_isdst);
#endif
/* Is job issuing lagging behind with the progress of time? */
job->late= (job->rtime < now);
/* The result is in job->rtime. */
if (job->rtime < next) next= job->rtime;
}
#define isdigit(c) ((unsigned) ((c) - '0') < 10)
static char *get_token(char **ptr)
/* Return a pointer to the next token in a string. Move *ptr to the end of
* the token, and return a pointer to the start. If *ptr == start of token
* then we're stuck against a newline or end of string.
*/
{
char *start, *p;
p= *ptr;
while (*p == ' ' || *p == '\t') p++;
start= p;
while (*p != ' ' && *p != '\t' && *p != '\n' && *p != 0) p++;
*ptr= p;
return start;
}
static int range_parse(char *file, char *data, bitmap_t map,
int min, int max, int *wildcard)
/* Parse a comma separated series of 'n', 'n-m' or 'n:m' numbers. 'n'
* includes number 'n' in the bit map, 'n-m' includes all numbers between
* 'n' and 'm' inclusive, and 'n:m' includes 'n+k*m' for any k if in range.
* Numbers must fall between 'min' and 'max'. A '*' means all numbers. A
* '?' is allowed as a synonym for the current minute, which only makes sense
* in the minute field, i.e. max must be 59. Return true iff parsed ok.
*/
{
char *p;
int end;
int n, m;
/* Clear all bits. */
for (n= 0; n < 8; n++) map[n]= 0;
p= data;
while (*p != ' ' && *p != '\t' && *p != '\n' && *p != 0) p++;
end= *p;
*p= 0;
p= data;
if (*p == 0) {
cronlog(LOG_ERR, "%s: not enough time fields\n", file);
return 0;
}
/* Is it a '*'? */
if (p[0] == '*' && p[1] == 0) {
for (n= min; n <= max; n++) bit_set(map, n);
p[1]= end;
*wildcard= 1;
return 1;
}
*wildcard= 0;
/* Parse a comma separated series of numbers or ranges. */
for (;;) {
if (*p == '?' && max == 59 && p[1] != '-') {
n= localtime(&now)->tm_min;
p++;
} else {
if (!isdigit(*p)) goto syntax;
n= 0;
do {
n= 10 * n + (*p++ - '0');
if (n > max) goto range;
} while (isdigit(*p));
}
if (n < min) goto range;
if (*p == '-') { /* A range of the form 'n-m'? */
p++;
if (!isdigit(*p)) goto syntax;
m= 0;
do {
m= 10 * m + (*p++ - '0');
if (m > max) goto range;
} while (isdigit(*p));
if (m < n) goto range;
do {
bit_set(map, n);
} while (++n <= m);
} else
if (*p == ':') { /* A repeat of the form 'n:m'? */
p++;
if (!isdigit(*p)) goto syntax;
m= 0;
do {
m= 10 * m + (*p++ - '0');
if (m > (max-min+1)) goto range;
} while (isdigit(*p));
if (m == 0) goto range;
while (n >= min) n-= m;
while ((n+= m) <= max) bit_set(map, n);
} else {
/* Simply a number */
bit_set(map, n);
}
if (*p == 0) break;
if (*p++ != ',') goto syntax;
}
*p= end;
return 1;
syntax:
cronlog(LOG_ERR, "%s: field '%s': bad syntax for a %d-%d time field\n",
file, data, min, max);
return 0;
range:
cronlog(LOG_ERR,
"%s: field '%s': values out of the %d-%d allowed range\n",
file, data, min, max);
return 0;
}
void tab_parse(char *file, char *user)
/* Parse a crontab file and add its data to the tables. Handle errors by
* yourself. Table is owned by 'user' if non-null.
*/
{
crontab_t **atab, *tab;
cronjob_t **ajob, *job;
int fd;
struct stat st;
char *p, *q;
size_t n;
ssize_t r;
int ok, wc;
for (atab= &crontabs; (tab= *atab) != nil; atab= &tab->next) {
if (strcmp(file, tab->file) == 0) break;
}
/* Try to open the file. */
if ((fd= open(file, O_RDONLY)) < 0 || fstat(fd, &st) < 0) {
if (errno != ENOENT) {
cronlog(LOG_ERR, "%s: %s\n", file, strerror(errno));
}
if (fd != -1) close(fd);
return;
}
/* Forget it if the file is awfully big. */
if (st.st_size > TAB_MAX) {
cronlog(LOG_ERR, "%s: %lu bytes is bigger than my %lu limit\n",
file,
(unsigned long) st.st_size,
(unsigned long) TAB_MAX);
return;
}
/* If the file is the same as before then don't bother. */
if (tab != nil && st.st_mtime == tab->mtime) {
close(fd);
tab->current= 1;
return;
}
/* Create a new table structure. */
tab= allocate(sizeof(*tab));
tab->file= allocate((strlen(file) + 1) * sizeof(tab->file[0]));
strcpy(tab->file, file);
tab->user= nil;
if (user != nil) {
tab->user= allocate((strlen(user) + 1) * sizeof(tab->user[0]));
strcpy(tab->user, user);
}
tab->data= allocate((st.st_size + 1) * sizeof(tab->data[0]));
tab->jobs= nil;
tab->mtime= st.st_mtime;
tab->current= 0;
tab->next= *atab;
*atab= tab;
/* Pull a new table in core. */
n= 0;
while (n < st.st_size) {
if ((r = read(fd, tab->data + n, st.st_size - n)) < 0) {
cronlog(LOG_CRIT, "%s: %s", file, strerror(errno));
close(fd);
return;
}
if (r == 0) break;
n+= r;
}
close(fd);
tab->data[n]= 0;
if (strlen(tab->data) < n) {
cronlog(LOG_ERR, "%s contains a null character\n", file);
return;
}
/* Parse the file. */
ajob= &tab->jobs;
p= tab->data;
ok= 1;
while (ok && *p != 0) {
q= get_token(&p);
if (*q == '#' || q == p) {
/* Comment or empty. */
while (*p != 0 && *p++ != '\n') {}
continue;
}
/* One new job coming up. */
*ajob= job= allocate(sizeof(*job));
*(ajob= &job->next)= nil;
job->tab= tab;
if (!range_parse(file, q, job->min, 0, 59, &wc)) {
ok= 0;
break;
}
q= get_token(&p);
if (!range_parse(file, q, job->hour, 0, 23, &wc)) {
ok= 0;
break;
}
q= get_token(&p);
if (!range_parse(file, q, job->mday, 1, 31, &wc)) {
ok= 0;
break;
}
job->do_mday= !wc;
q= get_token(&p);
if (!range_parse(file, q, job->mon, 1, 12, &wc)) {
ok= 0;
break;
}
job->do_mday |= !wc;
q= get_token(&p);
if (!range_parse(file, q, job->wday, 0, 7, &wc)) {
ok= 0;
break;
}
job->do_wday= !wc;
/* 7 is Sunday, but 0 is a common mistake because it is in the
* tm_wday range. We allow and even prefer it internally.
*/
if (bit_isset(job->wday, 7)) {
bit_clr(job->wday, 7);
bit_set(job->wday, 0);
}
/* The month range is 1-12, but tm_mon likes 0-11. */
job->mon[0] >>= 1;
if (bit_isset(job->mon, 8)) bit_set(job->mon, 7);
job->mon[1] >>= 1;
/* Scan for options. */
job->user= nil;
while (q= get_token(&p), *q == '-') {
q++;
if (q[0] == '-' && q+1 == p) {
/* -- */
q= get_token(&p);
break;
}
while (q < p) switch (*q++) {
case 'u':
if (q == p) q= get_token(&p);
if (q == p) goto usage;
memmove(q-1, q, p-q); /* gross... */
p[-1]= 0;
job->user= q-1;
q= p;
break;
default:
usage:
cronlog(LOG_ERR,
"%s: bad option -%c, good options are: -u username\n",
file, q[-1]);
ok= 0;
goto endtab;
}
}
/* A crontab owned by a user can only do things as that user. */
if (tab->user != nil) job->user= tab->user;
/* Inspect the first character of the command. */
job->cmd= q;
if (q == p || *q == '#') {
/* Rest of the line is empty, i.e. the commands are on
* the following lines indented by one tab.
*/
while (*p != 0 && *p++ != '\n') {}
if (*p++ != '\t') {
cronlog(LOG_ERR,
"%s: contains an empty command\n",
file);
ok= 0;
goto endtab;
}
while (*p != 0) {
if ((*q = *p++) == '\n') {
if (*p != '\t') break;
p++;
}
q++;
}
} else {
/* The command is on this line. Alas we must now be
* backwards compatible and change %'s to newlines.
*/
p= q;
while (*p != 0) {
if ((*q = *p++) == '\n') break;
if (*q == '%') *q= '\n';
q++;
}
}
*q= 0;
job->rtime= now;
job->late= 0; /* It is on time. */
job->atjob= 0; /* True cron job. */
job->pid= IDLE_PID; /* Not running yet. */
tab_reschedule(job); /* Compute next time to run. */
}
endtab:
if (ok) tab->current= 1;
}
void tab_find_atjob(char *atdir)
/* Find the first to be executed AT job and kludge up an crontab job for it.
* We set tab->file to "atdir/jobname", tab->data to "atdir/past/jobname",
* and job->cmd to "jobname".
*/
{
DIR *spool;
struct dirent *entry;
time_t t0, t1;
struct tm tmnow, tmt1;
static char template[] = "96.365.1546.00";
char firstjob[sizeof(template)];
int i;
crontab_t *tab;
cronjob_t *job;
if ((spool= opendir(atdir)) == nil) return;
tmnow= *localtime(&now);
t0= NEVER;
while ((entry= readdir(spool)) != nil) {
/* Check if the name fits the template. */
for (i= 0; template[i] != 0; i++) {
if (template[i] == '.') {
if (entry->d_name[i] != '.') break;
} else {
if (!isdigit(entry->d_name[i])) break;
}
}
if (template[i] != 0 || entry->d_name[i] != 0) continue;
/* Convert the name to a time. Careful with the century. */
memset(&tmt1, 0, sizeof(tmt1));
tmt1.tm_year= atoi(entry->d_name+0);
while (tmt1.tm_year < tmnow.tm_year-10) tmt1.tm_year+= 100;
tmt1.tm_mday= 1+atoi(entry->d_name+3);
tmt1.tm_min= atoi(entry->d_name+7);
tmt1.tm_hour= tmt1.tm_min / 100;
tmt1.tm_min%= 100;
tmt1.tm_isdst= -1;
if ((t1= mktime(&tmt1)) == -1) {
/* Illegal time? Try in winter time. */
tmt1.tm_isdst= 0;
if ((t1= mktime(&tmt1)) == -1) continue;
}
if (t1 < t0) {
t0= t1;
strcpy(firstjob, entry->d_name);
}
}
closedir(spool);
if (t0 == NEVER) return; /* AT job spool is empty. */
/* Create new table and job structures. */
tab= allocate(sizeof(*tab));
tab->file= allocate((strlen(atdir) + 1 + sizeof(template))
* sizeof(tab->file[0]));
strcpy(tab->file, atdir);
strcat(tab->file, "/");
strcat(tab->file, firstjob);
tab->data= allocate((strlen(atdir) + 6 + sizeof(template))
* sizeof(tab->data[0]));
strcpy(tab->data, atdir);
strcat(tab->data, "/past/");
strcat(tab->data, firstjob);
tab->user= nil;
tab->mtime= 0;
tab->current= 1;
tab->next= crontabs;
crontabs= tab;
tab->jobs= job= allocate(sizeof(*job));
job->next= nil;
job->tab= tab;
job->user= nil;
job->cmd= tab->data + strlen(atdir) + 6;
job->rtime= t0;
job->late= 0;
job->atjob= 1; /* One AT job disguised as a cron job. */
job->pid= IDLE_PID;
if (job->rtime < next) next= job->rtime;
}
void tab_purge(void)
/* Remove table data that is no longer current. E.g. a crontab got removed.
*/
{
crontab_t **atab, *tab;
cronjob_t *job;
atab= &crontabs;
while ((tab= *atab) != nil) {
if (tab->current) {
/* Table is fine. */
tab->current= 0;
atab= &tab->next;
} else {
/* Table is not, or no longer valid; delete. */
*atab= tab->next;
while ((job= tab->jobs) != nil) {
tab->jobs= job->next;
deallocate(job);
}
deallocate(tab->data);
deallocate(tab->file);
deallocate(tab->user);
deallocate(tab);
}
}
}
static cronjob_t *reap_or_find(pid_t pid)
/* Find a finished job or search for the next one to run. */
{
crontab_t *tab;
cronjob_t *job;
cronjob_t *nextjob;
nextjob= nil;
next= NEVER;
for (tab= crontabs; tab != nil; tab= tab->next) {
for (job= tab->jobs; job != nil; job= job->next) {
if (job->pid == pid) {
job->pid= IDLE_PID;
tab_reschedule(job);
}
if (job->pid != IDLE_PID) continue;
if (job->rtime < next) next= job->rtime;
if (job->rtime <= now) nextjob= job;
}
}
return nextjob;
}
void tab_reap_job(pid_t pid)
/* A job has finished. Try to find it among the crontab data and reschedule
* it. Recompute time next to run a job.
*/
{
(void) reap_or_find(pid);
}
cronjob_t *tab_nextjob(void)
/* Find a job that should be run now. If none are found return null.
* Update 'next'.
*/
{
return reap_or_find(NO_PID);
}
static void pr_map(FILE *fp, bitmap_t map)
{
int last_bit= -1, bit;
char *sep;
sep= "";
for (bit= 0; bit < 64; bit++) {
if (bit_isset(map, bit)) {
if (last_bit == -1) last_bit= bit;
} else {
if (last_bit != -1) {
fprintf(fp, "%s%d", sep, last_bit);
if (last_bit != bit-1) {
fprintf(fp, "-%d", bit-1);
}
last_bit= -1;
sep= ",";
}
}
}
}
void tab_print(FILE *fp)
/* Print out a stored crontab file for debugging purposes. */
{
crontab_t *tab;
cronjob_t *job;
char *p;
struct tm tm;
for (tab= crontabs; tab != nil; tab= tab->next) {
fprintf(fp, "tab->file = \"%s\"\n", tab->file);
fprintf(fp, "tab->user = \"%s\"\n",
tab->user == nil ? "(root)" : tab->user);
fprintf(fp, "tab->mtime = %s", ctime(&tab->mtime));
for (job= tab->jobs; job != nil; job= job->next) {
if (job->atjob) {
fprintf(fp, "AT job");
} else {
pr_map(fp, job->min); fputc(' ', fp);
pr_map(fp, job->hour); fputc(' ', fp);
pr_map(fp, job->mday); fputc(' ', fp);
pr_map(fp, job->mon); fputc(' ', fp);
pr_map(fp, job->wday);
}
if (job->user != nil && job->user != tab->user) {
fprintf(fp, " -u %s", job->user);
}
fprintf(fp, "\n\t");
for (p= job->cmd; *p != 0; p++) {
fputc(*p, fp);
if (*p == '\n') fputc('\t', fp);
}
fputc('\n', fp);
tm= *localtime(&job->rtime);
fprintf(fp, " rtime = %.24s%s\n", asctime(&tm),
tm.tm_isdst ? " (DST)" : "");
if (job->pid != IDLE_PID) {
fprintf(fp, " pid = %ld\n", (long) job->pid);
}
}
}
}
/*
* $PchId: tab.c,v 1.5 2000/07/25 22:07:51 philip Exp $
*/
|