5 * Copyright 1994 Matthew Dillon (dillon@apollo.backplane.com)
6 * Copyright 2009 James Pryor <profjim@jimpryor.net>
7 * May be distributed under the GNU General Public License
12 Prototype
int CheckUpdates(const char *dpath
, const char *user_override
, time_t t1
, time_t t2
);
13 Prototype
void SynchronizeDir(const char *dpath
, const char *user_override
, int initial_scan
);
14 Prototype
void ReadTimestamps(int initial_scan
);
15 Prototype
int TestJobs(time_t t1
, time_t t2
);
16 Prototype
int TestStartupJobs(void);
17 Prototype
int ArmJob(CronFile
*file
, CronLine
*line
, time_t t1
, time_t t2
);
18 Prototype
void RunJobs(void);
19 Prototype
int CheckJobs(void);
21 void SynchronizeFile(const char *dpath
, const char *fname
, const char *uname
);
22 void DeleteFile(CronFile
**pfile
);
23 char *ParseInterval(int *interval
, char *ptr
);
24 char *ParseField(char *userName
, char *ary
, int modvalue
, int off
, int onvalue
, const char **names
, char *ptr
);
25 void FixDayDow(CronLine
*line
);
29 const char *DowAry
[] = {
48 const char *MonAry
[] = {
77 const char *FreqAry
[] = {
89 * Check the cron.update file in the specified directory. If user_override
90 * is NULL then the files in the directory belong to the user whose name is
91 * the file, otherwise they belong to the user_override user.
94 CheckUpdates(const char *dpath
, const char *user_override
, time_t t1
, time_t t2
)
98 char *fname
, *ptok
, *job
;
102 logn(LOG_DEBUG
, "CheckUpdates on %s/%s\n", dpath
, CRONUPDATE
);
104 asprintf(&path
, "%s/%s", dpath
, CRONUPDATE
);
105 if ((fi
= fopen(path
, "r")) != NULL
) {
107 while (fgets(buf
, sizeof(buf
), fi
) != NULL
) {
109 * if buf has only sep chars, return NULL and point ptok at buf's terminating 0
110 * else return pointer to first non-sep of buf and
111 * if there's a following sep, overwrite it to 0 and point ptok to next char
112 * else point ptok at buf's terminating 0
114 fname
= strtok_r(buf
, " \t\n", &ptok
);
117 SynchronizeFile(dpath
, fname
, user_override
);
118 else if (!getpwnam(fname
))
119 logn(LOG_WARNING
, "ignoring %s/%s (non-existent user)\n", dpath
, fname
);
120 else if (*ptok
== 0 || *ptok
== '\n')
121 SynchronizeFile(dpath
, fname
, fname
);
123 /* if fname is followed by whitespace, we prod any following jobs */
124 CronFile
*file
= FileBase
;
126 if (strcmp(file
->cf_UserName
, fname
) == 0)
128 file
= file
->cf_Next
;
131 logn(LOG_WARNING
, "unable to prod for user %s: no crontab\n", fname
);
134 /* calling strtok(ptok...) then strtok(NULL) is equiv to calling strtok_r(NULL,..&ptok) */
135 while ((job
= strtok(ptok
, " \t\n")) != NULL
) {
142 line
= file
->cf_LineBase
;
144 if (line
->cl_JobName
&& strcmp(line
->cl_JobName
, job
) == 0)
146 line
= line
->cl_Next
;
149 ArmJob(file
, line
, t1
, force
);
151 logn(LOG_WARNING
, "unable to prod for user %s: unknown job %s\n", fname
, job
);
152 /* we can continue parsing this line, we just don't install any CronWaiter for the requested job */
165 SynchronizeDir(const char *dpath
, const char *user_override
, int initial_scan
)
174 * Delete all database CronFiles for this directory. DeleteFile() will
175 * free *pfile and relink the *pfile pointer, or in the alternative will
176 * mark it as deleted.
179 while ((file
= *pfile
) != NULL
) {
180 if (file
->cf_Deleted
== 0 && strcmp(file
->cf_DPath
, dpath
) == 0) {
183 pfile
= &file
->cf_Next
;
188 * Since we are resynchronizing the entire directory, remove the
189 * the CRONUPDATE file.
191 asprintf(&path
, "%s/%s", dpath
, CRONUPDATE
);
196 * Scan the specified directory
198 if ((dir
= opendir(dpath
)) != NULL
) {
199 while ((den
= readdir(dir
)) != NULL
) {
200 if (strchr(den
->d_name
, '.') != NULL
)
202 if (strcmp(den
->d_name
, CRONUPDATE
) == 0)
205 SynchronizeFile(dpath
, den
->d_name
, user_override
);
206 } else if (getpwnam(den
->d_name
)) {
207 SynchronizeFile(dpath
, den
->d_name
, den
->d_name
);
209 logn(LOG_WARNING
, "ignoring %s/%s (non-existent user)\n",
216 logn(LOG_ERR
, "unable to scan directory %s\n", dpath
);
217 /* softerror, do not exit the program */
223 ReadTimestamps(int initial_scan
)
234 while (file
!= NULL
) {
235 if (file
->cf_Deleted
== 0) {
236 line
= file
->cf_LineBase
;
237 while (line
!= NULL
) {
238 if (line
->cl_Timestamp
) {
239 if ((fi
= fopen(line
->cl_Timestamp
, "r")) != NULL
) {
240 if (fgets(buf
, sizeof(buf
), fi
) != NULL
) {
242 ptr
= strptime(buf
, TIMESTAMP_FMT
, &tm
);
243 if (ptr
&& (*ptr
== 0 || *ptr
== '\n'))
245 if (sec
== (time_t)-1) {
246 logn(LOG_WARNING
, "unable to parse timestamp (user %s job %s)\n", file
->cf_UserName
, line
->cl_JobName
);
247 /* we continue checking other timestamps in this CronFile */
249 line
->cl_LastRan
= sec
;
250 freq
= (line
->cl_Freq
> 0) ? line
->cl_Freq
: line
->cl_Delay
;
251 if (line
->cl_NotUntil
< line
->cl_LastRan
+ freq
)
252 line
->cl_NotUntil
= line
->cl_LastRan
+ freq
;
256 } else if (initial_scan
) {
257 logn(LOG_NOTICE
, "no timestamp found (user %s job %s)\n", file
->cf_UserName
, line
->cl_JobName
);
258 /* softerror, do not exit the program */
261 line
= line
->cl_Next
;
264 file
= file
->cf_Next
;
269 SynchronizeFile(const char *dpath
, const char *fileName
, const char *userName
)
282 if (strcmp(userName
, "root") == 0)
285 maxEntries
= MAXLINES
;
286 maxLines
= maxEntries
* 10;
289 * Delete any existing copy of this CronFile
292 while ((file
= *pfile
) != NULL
) {
293 if (file
->cf_Deleted
== 0 && strcmp(file
->cf_DPath
, dpath
) == 0 &&
294 strcmp(file
->cf_FileName
, fileName
) == 0
298 pfile
= &file
->cf_Next
;
302 asprintf(&path
, "%s/%s", dpath
, fileName
);
303 if ((fi
= fopen(path
, "r")) != NULL
) {
306 if (fstat(fileno(fi
), &sbuf
) == 0 && sbuf
.st_uid
== DaemonUid
) {
307 CronFile
*file
= calloc(1, sizeof(CronFile
));
310 file
->cf_UserName
= strdup(userName
);
311 file
->cf_FileName
= strdup(fileName
);
312 file
->cf_DPath
= strdup(dpath
);
313 pline
= &file
->cf_LineBase
;
315 while (fgets(buf
, sizeof(buf
), fi
) != NULL
&& --maxLines
) {
320 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
324 if (len
&& ptr
[len
-1] == '\n')
327 if (*ptr
== 0 || *ptr
== '#')
330 if (--maxEntries
== 0)
333 bzero(&line
, sizeof(line
));
336 logn(LOG_DEBUG
, "User %s Entry %s\n", userName
, buf
);
345 for (j
= 0; FreqAry
[j
]; ++j
) {
346 if (strncmp(ptr
, FreqAry
[j
], strlen(FreqAry
[j
])) == 0) {
351 ptr
+= strlen(FreqAry
[j
]);
364 line
.cl_Freq
= HOURLY_FREQ
;
367 line
.cl_Freq
= DAILY_FREQ
;
370 line
.cl_Freq
= WEEKLY_FREQ
;
373 line
.cl_Freq
= MONTHLY_FREQ
;
375 line
.cl_Freq
= YEARLY_FREQ
;
376 /* else line.cl_Freq will remain 0 */
380 if (!line
.cl_Freq
|| (*ptr
!= ' ' && *ptr
!= '\t')) {
381 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, buf
);
385 if (line
.cl_Delay
< 0) {
386 line
.cl_Delay
= line
.cl_Freq
/ 20;
387 /* all minutes are permitted */
393 /* days are numbered 1..31 */
399 while (*ptr
== ' ' || *ptr
== '\t')
407 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mins
, 60, 0, 1,
409 ptr
= ParseField(file
->cf_UserName
, line
.cl_Hrs
, 24, 0, 1,
411 ptr
= ParseField(file
->cf_UserName
, line
.cl_Days
, 32, 0, 1,
413 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mons
, 12, -1, 1,
415 ptr
= ParseField(file
->cf_UserName
, line
.cl_Dow
, 7, 0, 31,
425 * fix days and dow - if one is not * and the other
426 * is *, the other is set to 0, and vise-versa
432 /* check for ID=... and AFTER=... and FREQ=... */
434 if (strncmp(ptr
, ID_TAG
, strlen(ID_TAG
)) == 0) {
435 if (line
.cl_JobName
) {
436 /* only assign ID_TAG once */
437 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
440 ptr
+= strlen(ID_TAG
);
442 * name = strsep(&ptr, seps):
443 * return name = ptr, and if ptr contains sep chars, overwrite first with 0 and point ptr to next char
446 asprintf(&line
.cl_Description
, "job %s", strsep(&ptr
, " \t"));
447 line
.cl_JobName
= line
.cl_Description
+ 4;
449 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, ID_TAG
, line
.cl_JobName
);
451 } else if (strncmp(ptr
, FREQ_TAG
, strlen(FREQ_TAG
)) == 0) {
453 /* only assign FREQ_TAG once */
454 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
458 ptr
+= strlen(FREQ_TAG
);
459 ptr
= ParseInterval(&line
.cl_Freq
, ptr
);
460 if (ptr
&& *ptr
== '/')
461 ptr
= ParseInterval(&line
.cl_Delay
, ++ptr
);
463 line
.cl_Delay
= line
.cl_Freq
;
465 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, base
);
466 } else if (*ptr
!= ' ' && *ptr
!= '\t') {
467 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s\n", userName
, base
);
471 } else if (strncmp(ptr
, WAIT_TAG
, strlen(WAIT_TAG
)) == 0) {
472 if (line
.cl_Waiters
) {
473 /* only assign WAIT_TAG once */
474 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
479 ptr
+= strlen(WAIT_TAG
);
481 CronLine
*job
, **pjob
;
482 if (strcspn(ptr
,",") < strcspn(ptr
," \t"))
483 name
= strsep(&ptr
, ",");
486 name
= strsep(&ptr
, " \t");
488 if (!ptr
|| *ptr
== 0) {
489 /* unexpectedly this was the last token in buf; so abort */
490 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, WAIT_TAG
, name
);
495 if ((w
= index(name
, '/')) != NULL
) {
497 w
= ParseInterval(&waitfor
, w
);
499 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s%s\n", userName
, WAIT_TAG
, name
);
506 /* look for a matching CronLine */
507 pjob
= &file
->cf_LineBase
;
508 while ((job
= *pjob
) != NULL
) {
509 if (job
->cl_JobName
&& strcmp(job
->cl_JobName
, name
) == 0) {
510 CronWaiter
*waiter
= malloc(sizeof(CronWaiter
));
511 CronNotifier
*notif
= malloc(sizeof(CronNotifier
));
512 waiter
->cw_Flag
= -1;
513 waiter
->cw_MaxWait
= waitfor
;
514 waiter
->cw_NotifLine
= job
;
515 waiter
->cw_Notifier
= notif
;
516 waiter
->cw_Next
= line
.cl_Waiters
; /* add to head of line.cl_Waiters */
517 line
.cl_Waiters
= waiter
;
518 notif
->cn_Waiter
= waiter
;
519 notif
->cn_Next
= job
->cl_Notifs
; /* add to head of job->cl_Notifs */
520 job
->cl_Notifs
= notif
;
523 pjob
= &job
->cl_Next
;
526 logn(LOG_WARNING
, "failed parsing crontab for user %s: unknown job %s\n", userName
, name
);
527 /* we can continue parsing this line, we just don't install any CronWaiter for the requested job */
531 } while (ptr
&& more
);
537 while (*ptr
== ' ' || *ptr
== '\t')
539 } while (!line
.cl_JobName
|| !line
.cl_Waiters
|| !line
.cl_Freq
);
541 if (line
.cl_JobName
&& (!ptr
|| *line
.cl_JobName
== 0)) {
542 /* we're aborting, or ID= was empty */
543 free(line
.cl_Description
);
544 line
.cl_Description
= NULL
;
545 line
.cl_JobName
= NULL
;
547 if (ptr
&& line
.cl_Delay
> 0 && !line
.cl_JobName
) {
548 logn(LOG_WARNING
, "failed parsing crontab for user %s: writing timestamp requires job %s to be named\n", userName
, ptr
);
552 /* couldn't parse so we abort; free any cl_Waiters */
553 if (line
.cl_Waiters
) {
554 CronWaiter
**pwaiters
, *waiters
;
555 pwaiters
= &line
.cl_Waiters
;
556 while ((waiters
= *pwaiters
) != NULL
) {
557 *pwaiters
= waiters
->cw_Next
;
558 /* leave the Notifier allocated but disabled */
559 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
565 /* now we've added any ID=... or AFTER=... */
568 * copy command string
570 line
.cl_Shell
= strdup(ptr
);
572 if (line
.cl_Delay
> 0) {
573 asprintf(&line
.cl_Timestamp
, "%s/%s.%s", TSDir
, userName
, line
.cl_JobName
);
574 line
.cl_NotUntil
= time(NULL
) + line
.cl_Delay
;
577 if (line
.cl_JobName
) {
579 logn(LOG_DEBUG
, " Command %s Job %s\n", line
.cl_Shell
, line
.cl_JobName
);
581 /* when cl_JobName is NULL, we point cl_Description to cl_Shell */
582 line
.cl_Description
= line
.cl_Shell
;
584 logn(LOG_DEBUG
, " Command %s\n", line
.cl_Shell
);
587 *pline
= calloc(1, sizeof(CronLine
));
588 /* copy working CronLine to newly allocated one */
591 pline
= &((*pline
)->cl_Next
);
596 file
->cf_Next
= FileBase
;
599 if (maxLines
== 0 || maxEntries
== 0)
600 logn(LOG_WARNING
, "maximum number of lines reached for user %s\n", userName
);
608 ParseInterval(int *interval
, char *ptr
)
611 if (ptr
&& *ptr
>= '0' && *ptr
<= '9' && (n
= strtol(ptr
, &ptr
, 10)) > 0)
636 ParseField(char *user
, char *ary
, int modvalue
, int off
, int onvalue
, const char **names
, char *ptr
)
645 while (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
649 * Handle numeric digit or symbol or '*'
653 n1
= 0; /* everything will be filled */
657 } else if (*ptr
>= '0' && *ptr
<= '9') {
659 n1
= strtol(ptr
, &ptr
, 10) + off
;
661 n2
= strtol(ptr
, &ptr
, 10) + off
;
666 for (i
= 0; names
[i
]; ++i
) {
667 if (strncmp(ptr
, names
[i
], strlen(names
[i
])) == 0) {
672 ptr
+= strlen(names
[i
]);
682 * handle optional range '-'
686 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
689 if (*ptr
== '-' && n2
< 0) {
695 * collapse single-value ranges, handle skipmark, and fill
696 * in the character array appropriately.
703 skip
= strtol(ptr
+ 1, &ptr
, 10);
706 * fill array, using a failsafe is the easiest way to prevent
716 n1
= (n1
+ 1) % modvalue
;
722 } while (n1
!= n2
&& --failsafe
);
725 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
736 if (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
737 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
741 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
747 for (i
= 0; i
< modvalue
; ++i
)
748 logn(LOG_DEBUG
, "%2x ", ary
[i
]);
749 logn(LOG_DEBUG
, "\n");
756 FixDayDow(CronLine
*line
)
762 for (i
= 0; i
< arysize(line
->cl_Dow
); ++i
) {
763 if (line
->cl_Dow
[i
] == 0) {
768 for (i
= 0; i
< arysize(line
->cl_Days
); ++i
) {
769 if (line
->cl_Days
[i
] == 0) {
773 /* change from "every Mon" to "ith Mon"
774 * 6th,7th... Dow are treated as 1st,2nd... */
775 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
776 line
->cl_Dow
[j
] &= 1 << (i
-1)%5;
779 /* change from "nth Mon" to "nth or ith Mon" */
780 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
782 line
->cl_Dow
[j
] |= 1 << (i
-1)%5;
785 /* continue cycling through cl_Days */
794 memset(line
->cl_Days
, 0, sizeof(line
->cl_Days
));
796 if (daysUsed
&& !weekUsed
) {
797 memset(line
->cl_Dow
, 0, sizeof(line
->cl_Dow
));
802 * DeleteFile() - destroy a CronFile.
804 * The CronFile (*pfile) is destroyed if possible, and marked cf_Deleted
805 * if there are still active processes running on it. *pfile is relinked
809 DeleteFile(CronFile
**pfile
)
811 CronFile
*file
= *pfile
;
812 CronLine
**pline
= &file
->cf_LineBase
;
814 CronWaiter
**pwaiters
, *waiters
;
815 CronNotifier
**pnotifs
, *notifs
;
817 file
->cf_Running
= 0;
818 file
->cf_Deleted
= 1;
820 while ((line
= *pline
) != NULL
) {
821 if (line
->cl_Pid
> 0) {
822 file
->cf_Running
= 1;
823 pline
= &line
->cl_Next
;
825 *pline
= line
->cl_Next
;
826 free(line
->cl_Shell
);
828 if (line
->cl_JobName
)
829 /* this frees both cl_Description and cl_JobName
830 * if cl_JobName is NULL, Description pointed to ch_Shell, which was already freed
832 free(line
->cl_Description
);
833 if (line
->cl_Timestamp
)
834 free(line
->cl_Timestamp
);
836 pnotifs
= &line
->cl_Notifs
;
837 while ((notifs
= *pnotifs
) != NULL
) {
838 *pnotifs
= notifs
->cn_Next
;
839 if (notifs
->cn_Waiter
) {
840 notifs
->cn_Waiter
->cw_NotifLine
= NULL
;
841 notifs
->cn_Waiter
->cw_Notifier
= NULL
;
845 pwaiters
= &line
->cl_Waiters
;
846 while ((waiters
= *pwaiters
) != NULL
) {
847 *pwaiters
= waiters
->cw_Next
;
848 if (waiters
->cw_Notifier
)
849 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
856 if (file
->cf_Running
== 0) {
857 *pfile
= file
->cf_Next
;
858 free(file
->cf_DPath
);
859 free(file
->cf_FileName
);
860 free(file
->cf_UserName
);
869 * determine which jobs need to be run. Under normal conditions, the
870 * period is about a minute (one scan). Worst case it will be one
875 TestJobs(time_t t1
, time_t t2
)
882 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
883 if (file
->cf_Deleted
)
885 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
886 struct CronWaiter
*waiter
;
888 if (line
->cl_Pid
== -2) {
889 /* can job stop waiting? */
891 waiter
= line
->cl_Waiters
;
892 while (waiter
!= NULL
) {
893 if (waiter
->cw_Flag
> 0) {
894 /* notifier exited unsuccessfully */
897 } else if (waiter
->cw_Flag
< 0)
898 /* still waiting, notifier hasn't run to completion */
900 waiter
= waiter
->cw_Next
;
904 logn(LOG_DEBUG
, "cancelled waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
908 logn(LOG_DEBUG
, "finished waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
909 nJobs
+= ArmJob(file
, line
, 0, -1);
911 if (line->cl_NotUntil)
912 line->cl_NotUntil = t2;
920 * Find jobs > t1 and <= t2
923 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
925 struct tm
*tp
= localtime(&t
);
927 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
929 struct tm tnext
= *tp
;
931 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
932 n_wday
|= 16; /* last dow in month is always recognized as 5th */
935 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
936 if (file
->cf_Deleted
)
938 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
939 if (line
->cl_Pid
!= -1 && (line
->cl_Freq
== 0 || (line
->cl_Freq
> 0 && t2
>= line
->cl_NotUntil
))) {
940 /* (re)schedule job? */
941 if (line
->cl_Mins
[tp
->tm_min
] &&
942 line
->cl_Hrs
[tp
->tm_hour
] &&
943 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
944 line
->cl_Mons
[tp
->tm_mon
]
946 if (line
->cl_NotUntil
)
947 line
->cl_NotUntil
= t2
- t2
% 60; /* save what minute this job was scheduled/started waiting */
948 nJobs
+= ArmJob(file
, line
, t1
, t2
);
959 * ArmJob: if t2 is (time_t)-1, we force-schedule the job without any waiting
960 * else it will wait on any of its declared notifiers who will run <= t2 + cw_MaxWait
964 ArmJob(CronFile
*file
, CronLine
*line
, time_t t1
, time_t t2
)
966 struct CronWaiter
*waiter
;
967 if (line
->cl_Pid
> 0) {
968 logn(LOG_NOTICE
, "process already running (%d): user %s %s\n",
973 } else if (t2
== -1 && line
->cl_Pid
!= -1) {
977 } else if (line
->cl_Pid
== 0) {
978 /* arming a waiting job (cl_Pid == -2) without forcing has no effect */
980 /* if we have any waiters, zero them and arm cl_Pid=-2 */
981 waiter
= line
->cl_Waiters
;
982 while (waiter
!= NULL
) {
983 /* check if notifier will run <= t2 + cw_Max_Wait? */
984 if (!waiter
->cw_NotifLine
)
985 /* notifier deleted */
987 else if (waiter
->cw_NotifLine
->cl_Pid
!= 0) {
988 /* if notifier is armed, or waiting, or running, we wait for it */
989 waiter
->cw_Flag
= -1;
991 } else if (waiter
->cw_NotifLine
->cl_Freq
< 0) {
992 /* arm any @noauto or @reboot jobs we're waiting on */
993 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t2
);
994 waiter
->cw_Flag
= -1;
998 if (waiter
->cw_MaxWait
== 0)
999 /* when no MaxWait interval specified, we always wait */
1000 waiter
->cw_Flag
= -1;
1001 else if (waiter
->cw_NotifLine
->cl_Freq
== 0 || (waiter
->cw_NotifLine
->cl_Freq
> 0 && t2
+ waiter
->cw_MaxWait
>= waiter
->cw_NotifLine
->cl_NotUntil
)) {
1002 /* default is don't wait */
1003 waiter
->cw_Flag
= 0;
1004 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
1006 struct tm
*tp
= localtime(&t
);
1008 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
1010 struct tm tnext
= *tp
;
1012 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
1013 n_wday
|= 16; /* last dow in month is always recognized as 5th */
1015 if (line
->cl_Mins
[tp
->tm_min
] &&
1016 line
->cl_Hrs
[tp
->tm_hour
] &&
1017 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
1018 line
->cl_Mons
[tp
->tm_mon
]
1020 /* notifier will run soon enough, we wait for it */
1021 waiter
->cw_Flag
= -1;
1029 waiter
= waiter
->cw_Next
;
1031 if (line
->cl_Pid
== -1) {
1032 /* job is ready to run */
1035 logn(LOG_DEBUG
, "scheduled: user %s %s\n",
1037 line
->cl_Description
1040 } else if (DebugOpt
)
1041 logn(LOG_DEBUG
, "waiting: user %s %s\n",
1043 line
->cl_Description
1050 TestStartupJobs(void)
1053 time_t t1
= time(NULL
);
1057 t1
= t1
- t1
% 60 + 60;
1059 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1061 logn(LOG_DEBUG
, "TestStartup for FILE %s/%s USER %s:\n",
1062 file
->cf_DPath
, file
->cf_FileName
, file
->cf_UserName
);
1063 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1064 struct CronWaiter
*waiter
;
1066 if (line
->cl_JobName
)
1067 logn(LOG_DEBUG
, " LINE %s JOB %s\n", line
->cl_Shell
, line
->cl_JobName
);
1069 logn(LOG_DEBUG
, " LINE %s\n", line
->cl_Shell
);
1072 if (line
->cl_Freq
== -1) {
1073 /* freq is @reboot */
1076 /* if we have any waiters, reset them and arm Pid = -2 */
1077 waiter
= line
->cl_Waiters
;
1078 while (waiter
!= NULL
) {
1079 waiter
->cw_Flag
= -1;
1081 /* we only arm @noauto jobs we're waiting on, not other @reboot jobs */
1082 if (waiter
->cw_NotifLine
&& waiter
->cw_NotifLine
->cl_Freq
== -2)
1083 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t1
+60);
1084 waiter
= waiter
->cw_Next
;
1086 if (line
->cl_Pid
== -1) {
1087 /* job is ready to run */
1091 logn(LOG_DEBUG
, " scheduled: %s\n", line
->cl_Description
);
1092 } else if (DebugOpt
)
1093 logn(LOG_DEBUG
, " waiting: %s\n", line
->cl_Description
);
1108 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1109 if (file
->cf_Ready
) {
1112 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1113 if (line
->cl_Pid
== -1) {
1117 logn(LOG_INFO
, "FILE %s/%s USER %s PID %3d %s\n",
1122 line
->cl_Description
1124 if (line
->cl_Pid
< 0)
1125 /* how could this happen? RunJob will leave cl_Pid set to 0 or the actual pid */
1127 else if (line
->cl_Pid
> 0)
1128 file
->cf_Running
= 1;
1136 * CheckJobs() - check for job completion
1138 * Check for job completion, return number of CronFiles still running after
1147 int nStillRunning
= 0;
1149 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1150 if (file
->cf_Running
) {
1151 file
->cf_Running
= 0;
1153 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1154 if (line
->cl_Pid
> 0) {
1156 int r
= waitpid(line
->cl_Pid
, &status
, WNOHANG
);
1158 if (r
< 0 || r
== line
->cl_Pid
) {
1159 if (r
> 0 && WIFEXITED(status
))
1160 status
= WEXITSTATUS(status
);
1163 EndJob(file
, line
, status
);
1165 file
->cf_Running
= 1;
1166 } else if (r
== 0) {
1167 file
->cf_Running
= 1;
1172 nStillRunning
+= file
->cf_Running
;
1174 return(nStillRunning
);