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
void 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(const char *user
, 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
;
101 asprintf(&path
, "%s/%s", dpath
, CRONUPDATE
);
102 if ((fi
= fopen(path
, "r")) != NULL
) {
104 logn(LOG_INFO
, "reading %s/%s\n", dpath
, CRONUPDATE
);
105 while (fgets(buf
, sizeof(buf
), fi
) != NULL
) {
107 * if buf has only sep chars, return NULL and point ptok at buf's terminating 0
108 * else return pointer to first non-sep of buf and
109 * if there's a following sep, overwrite it to 0 and point ptok to next char
110 * else point ptok at buf's terminating 0
112 fname
= strtok_r(buf
, " \t\n", &ptok
);
115 SynchronizeFile(dpath
, fname
, user_override
);
116 else if (!getpwnam(fname
))
117 logn(LOG_WARNING
, "ignoring %s/%s (non-existent user)\n", dpath
, fname
);
118 else if (*ptok
== 0 || *ptok
== '\n') {
119 SynchronizeFile(dpath
, fname
, fname
);
120 ReadTimestamps(fname
, 1);
122 /* if fname is followed by whitespace, we prod any following jobs */
123 CronFile
*file
= FileBase
;
125 if (strcmp(file
->cf_UserName
, fname
) == 0)
127 file
= file
->cf_Next
;
130 logn(LOG_WARNING
, "unable to prod for user %s: no crontab\n", fname
);
133 /* calling strtok(ptok...) then strtok(NULL) is equiv to calling strtok_r(NULL,..&ptok) */
134 while ((job
= strtok(ptok
, " \t\n")) != NULL
) {
141 line
= file
->cf_LineBase
;
143 if (line
->cl_JobName
&& strcmp(line
->cl_JobName
, job
) == 0)
145 line
= line
->cl_Next
;
148 ArmJob(file
, line
, t1
, force
);
150 logn(LOG_WARNING
, "unable to prod for user %s: unknown job %s\n", fname
, job
);
151 /* we can continue parsing this line, we just don't install any CronWaiter for the requested job */
163 SynchronizeDir(const char *dpath
, const char *user_override
, int initial_scan
)
172 * Delete all database CronFiles for this directory. DeleteFile() will
173 * free *pfile and relink the *pfile pointer, or in the alternative will
174 * mark it as deleted.
177 while ((file
= *pfile
) != NULL
) {
178 if (file
->cf_Deleted
== 0 && strcmp(file
->cf_DPath
, dpath
) == 0) {
181 pfile
= &file
->cf_Next
;
186 * Since we are resynchronizing the entire directory, remove the
187 * the CRONUPDATE file.
189 asprintf(&path
, "%s/%s", dpath
, CRONUPDATE
);
194 * Scan the specified directory
196 if ((dir
= opendir(dpath
)) != NULL
) {
197 while ((den
= readdir(dir
)) != NULL
) {
198 if (strchr(den
->d_name
, '.') != NULL
)
200 if (strcmp(den
->d_name
, CRONUPDATE
) == 0)
203 SynchronizeFile(dpath
, den
->d_name
, user_override
);
204 } else if (getpwnam(den
->d_name
)) {
205 SynchronizeFile(dpath
, den
->d_name
, den
->d_name
);
207 logn(LOG_WARNING
, "ignoring %s/%s (non-existent user)\n",
214 logn(LOG_ERR
, "unable to scan directory %s\n", dpath
);
215 /* softerror, do not exit the program */
221 ReadTimestamps(const char *user
, int initial_scan
)
232 while (file
!= NULL
) {
233 if (file
->cf_Deleted
== 0 && (!user
|| strcmp(user
, file
->cf_UserName
) == 0)) {
234 line
= file
->cf_LineBase
;
235 while (line
!= NULL
) {
236 if (line
->cl_Timestamp
) {
237 if ((fi
= fopen(line
->cl_Timestamp
, "r")) != NULL
) {
238 if (fgets(buf
, sizeof(buf
), fi
) != NULL
) {
240 ptr
= strptime(buf
, TIMESTAMP_FMT
, &tm
);
241 if (ptr
&& (*ptr
== 0 || *ptr
== '\n'))
243 if (sec
== (time_t)-1) {
244 logn(LOG_WARNING
, "unable to parse timestamp (user %s job %s)\n", file
->cf_UserName
, line
->cl_JobName
);
245 /* we continue checking other timestamps in this CronFile */
247 line
->cl_LastRan
= sec
;
248 freq
= (line
->cl_Freq
> 0) ? line
->cl_Freq
: line
->cl_Delay
;
249 if (line
->cl_NotUntil
< line
->cl_LastRan
+ freq
)
250 line
->cl_NotUntil
= line
->cl_LastRan
+ freq
;
256 logn(LOG_NOTICE
, "no timestamp found (user %s job %s)\n", file
->cf_UserName
, line
->cl_JobName
);
257 /* softerror, do not exit the program */
260 line
= line
->cl_Next
;
263 file
= file
->cf_Next
;
268 SynchronizeFile(const char *dpath
, const char *fileName
, const char *userName
)
281 if (strcmp(userName
, "root") == 0)
284 maxEntries
= MAXLINES
;
285 maxLines
= maxEntries
* 10;
288 * Delete any existing copy of this CronFile
291 while ((file
= *pfile
) != NULL
) {
292 if (file
->cf_Deleted
== 0 && strcmp(file
->cf_DPath
, dpath
) == 0 &&
293 strcmp(file
->cf_FileName
, fileName
) == 0
297 pfile
= &file
->cf_Next
;
301 asprintf(&path
, "%s/%s", dpath
, fileName
);
302 if ((fi
= fopen(path
, "r")) != NULL
) {
305 if (fstat(fileno(fi
), &sbuf
) == 0 && sbuf
.st_uid
== DaemonUid
) {
306 CronFile
*file
= calloc(1, sizeof(CronFile
));
308 time_t tnow
= time(NULL
);
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) {
387 * delays on @daily, @hourly, etc are 1/20 of the frequency
388 * so they don't all start at once
389 * this also affects how they behave when the job returns EAGAIN
391 line
.cl_Delay
= line
.cl_Freq
/ 20;
392 /* all minutes are permitted */
398 /* days are numbered 1..31 */
404 while (*ptr
== ' ' || *ptr
== '\t')
412 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mins
, 60, 0, 1,
414 ptr
= ParseField(file
->cf_UserName
, line
.cl_Hrs
, 24, 0, 1,
416 ptr
= ParseField(file
->cf_UserName
, line
.cl_Days
, 32, 0, 1,
418 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mons
, 12, -1, 1,
420 ptr
= ParseField(file
->cf_UserName
, line
.cl_Dow
, 7, 0, 31,
430 * fix days and dow - if one is not * and the other
431 * is *, the other is set to 0, and vise-versa
437 /* check for ID=... and AFTER=... and FREQ=... */
439 if (strncmp(ptr
, ID_TAG
, strlen(ID_TAG
)) == 0) {
440 if (line
.cl_JobName
) {
441 /* only assign ID_TAG once */
442 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
445 ptr
+= strlen(ID_TAG
);
447 * name = strsep(&ptr, seps):
448 * return name = ptr, and if ptr contains sep chars, overwrite first with 0 and point ptr to next char
451 asprintf(&line
.cl_Description
, "job %s", strsep(&ptr
, " \t"));
452 line
.cl_JobName
= line
.cl_Description
+ 4;
454 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, ID_TAG
, line
.cl_JobName
);
456 } else if (strncmp(ptr
, FREQ_TAG
, strlen(FREQ_TAG
)) == 0) {
458 /* only assign FREQ_TAG once */
459 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
463 ptr
+= strlen(FREQ_TAG
);
464 ptr
= ParseInterval(&line
.cl_Freq
, ptr
);
465 if (ptr
&& *ptr
== '/')
466 ptr
= ParseInterval(&line
.cl_Delay
, ++ptr
);
468 line
.cl_Delay
= line
.cl_Freq
;
470 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, base
);
471 } else if (*ptr
!= ' ' && *ptr
!= '\t') {
472 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s\n", userName
, base
);
476 } else if (strncmp(ptr
, WAIT_TAG
, strlen(WAIT_TAG
)) == 0) {
477 if (line
.cl_Waiters
) {
478 /* only assign WAIT_TAG once */
479 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
484 ptr
+= strlen(WAIT_TAG
);
486 CronLine
*job
, **pjob
;
487 if (strcspn(ptr
,",") < strcspn(ptr
," \t"))
488 name
= strsep(&ptr
, ",");
491 name
= strsep(&ptr
, " \t");
493 if (!ptr
|| *ptr
== 0) {
494 /* unexpectedly this was the last token in buf; so abort */
495 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, WAIT_TAG
, name
);
500 if ((w
= index(name
, '/')) != NULL
) {
502 w
= ParseInterval(&waitfor
, w
);
504 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s%s\n", userName
, WAIT_TAG
, name
);
511 /* look for a matching CronLine */
512 pjob
= &file
->cf_LineBase
;
513 while ((job
= *pjob
) != NULL
) {
514 if (job
->cl_JobName
&& strcmp(job
->cl_JobName
, name
) == 0) {
515 CronWaiter
*waiter
= malloc(sizeof(CronWaiter
));
516 CronNotifier
*notif
= malloc(sizeof(CronNotifier
));
517 waiter
->cw_Flag
= -1;
518 waiter
->cw_MaxWait
= waitfor
;
519 waiter
->cw_NotifLine
= job
;
520 waiter
->cw_Notifier
= notif
;
521 waiter
->cw_Next
= line
.cl_Waiters
; /* add to head of line.cl_Waiters */
522 line
.cl_Waiters
= waiter
;
523 notif
->cn_Waiter
= waiter
;
524 notif
->cn_Next
= job
->cl_Notifs
; /* add to head of job->cl_Notifs */
525 job
->cl_Notifs
= notif
;
528 pjob
= &job
->cl_Next
;
531 logn(LOG_WARNING
, "failed parsing crontab for user %s: unknown job %s\n", userName
, name
);
532 /* we can continue parsing this line, we just don't install any CronWaiter for the requested job */
536 } while (ptr
&& more
);
542 while (*ptr
== ' ' || *ptr
== '\t')
544 } while (!line
.cl_JobName
|| !line
.cl_Waiters
|| !line
.cl_Freq
);
546 if (line
.cl_JobName
&& (!ptr
|| *line
.cl_JobName
== 0)) {
547 /* we're aborting, or ID= was empty */
548 free(line
.cl_Description
);
549 line
.cl_Description
= NULL
;
550 line
.cl_JobName
= NULL
;
552 if (ptr
&& line
.cl_Delay
> 0 && !line
.cl_JobName
) {
553 logn(LOG_WARNING
, "failed parsing crontab for user %s: writing timestamp requires job %s to be named\n", userName
, ptr
);
557 /* couldn't parse so we abort; free any cl_Waiters */
558 if (line
.cl_Waiters
) {
559 CronWaiter
**pwaiters
, *waiters
;
560 pwaiters
= &line
.cl_Waiters
;
561 while ((waiters
= *pwaiters
) != NULL
) {
562 *pwaiters
= waiters
->cw_Next
;
563 /* leave the Notifier allocated but disabled */
564 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
570 /* now we've added any ID=... or AFTER=... */
573 * copy command string
575 line
.cl_Shell
= strdup(ptr
);
577 if (line
.cl_Delay
> 0) {
578 asprintf(&line
.cl_Timestamp
, "%s/%s.%s", TSDir
, userName
, line
.cl_JobName
);
579 line
.cl_NotUntil
= tnow
+ line
.cl_Delay
;
582 if (line
.cl_JobName
) {
584 logn(LOG_DEBUG
, " Command %s Job %s\n", line
.cl_Shell
, line
.cl_JobName
);
586 /* when cl_JobName is NULL, we point cl_Description to cl_Shell */
587 line
.cl_Description
= line
.cl_Shell
;
589 logn(LOG_DEBUG
, " Command %s\n", line
.cl_Shell
);
592 *pline
= calloc(1, sizeof(CronLine
));
593 /* copy working CronLine to newly allocated one */
596 pline
= &((*pline
)->cl_Next
);
601 file
->cf_Next
= FileBase
;
604 if (maxLines
== 0 || maxEntries
== 0)
605 logn(LOG_WARNING
, "maximum number of lines reached for user %s\n", userName
);
613 ParseInterval(int *interval
, char *ptr
)
616 if (ptr
&& *ptr
>= '0' && *ptr
<= '9' && (n
= strtol(ptr
, &ptr
, 10)) > 0)
641 ParseField(char *user
, char *ary
, int modvalue
, int off
, int onvalue
, const char **names
, char *ptr
)
650 while (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
654 * Handle numeric digit or symbol or '*'
658 n1
= 0; /* everything will be filled */
662 } else if (*ptr
>= '0' && *ptr
<= '9') {
664 n1
= strtol(ptr
, &ptr
, 10) + off
;
666 n2
= strtol(ptr
, &ptr
, 10) + off
;
671 for (i
= 0; names
[i
]; ++i
) {
672 if (strncmp(ptr
, names
[i
], strlen(names
[i
])) == 0) {
677 ptr
+= strlen(names
[i
]);
687 * handle optional range '-'
691 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
694 if (*ptr
== '-' && n2
< 0) {
700 * collapse single-value ranges, handle skipmark, and fill
701 * in the character array appropriately.
708 skip
= strtol(ptr
+ 1, &ptr
, 10);
711 * fill array, using a failsafe is the easiest way to prevent
721 n1
= (n1
+ 1) % modvalue
;
727 } while (n1
!= n2
&& --failsafe
);
730 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
741 if (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
742 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
746 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
752 for (i
= 0; i
< modvalue
; ++i
)
753 logn(LOG_DEBUG
, "%2x ", ary
[i
]);
754 logn(LOG_DEBUG
, "\n");
761 FixDayDow(CronLine
*line
)
767 for (i
= 0; i
< arysize(line
->cl_Dow
); ++i
) {
768 if (line
->cl_Dow
[i
] == 0) {
773 for (i
= 0; i
< arysize(line
->cl_Days
); ++i
) {
774 if (line
->cl_Days
[i
] == 0) {
778 /* change from "every Mon" to "ith Mon"
779 * 6th,7th... Dow are treated as 1st,2nd... */
780 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
781 line
->cl_Dow
[j
] &= 1 << (i
-1)%5;
784 /* change from "nth Mon" to "nth or ith Mon" */
785 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
787 line
->cl_Dow
[j
] |= 1 << (i
-1)%5;
790 /* continue cycling through cl_Days */
799 memset(line
->cl_Days
, 0, sizeof(line
->cl_Days
));
801 if (daysUsed
&& !weekUsed
) {
802 memset(line
->cl_Dow
, 0, sizeof(line
->cl_Dow
));
807 * DeleteFile() - destroy a CronFile.
809 * The CronFile (*pfile) is destroyed if possible, and marked cf_Deleted
810 * if there are still active processes running on it. *pfile is relinked
814 DeleteFile(CronFile
**pfile
)
816 CronFile
*file
= *pfile
;
817 CronLine
**pline
= &file
->cf_LineBase
;
819 CronWaiter
**pwaiters
, *waiters
;
820 CronNotifier
**pnotifs
, *notifs
;
822 file
->cf_Running
= 0;
823 file
->cf_Deleted
= 1;
825 while ((line
= *pline
) != NULL
) {
826 if (line
->cl_Pid
> 0) {
827 file
->cf_Running
= 1;
828 pline
= &line
->cl_Next
;
830 *pline
= line
->cl_Next
;
831 free(line
->cl_Shell
);
833 if (line
->cl_JobName
)
834 /* this frees both cl_Description and cl_JobName
835 * if cl_JobName is NULL, Description pointed to ch_Shell, which was already freed
837 free(line
->cl_Description
);
838 if (line
->cl_Timestamp
)
839 free(line
->cl_Timestamp
);
841 pnotifs
= &line
->cl_Notifs
;
842 while ((notifs
= *pnotifs
) != NULL
) {
843 *pnotifs
= notifs
->cn_Next
;
844 if (notifs
->cn_Waiter
) {
845 notifs
->cn_Waiter
->cw_NotifLine
= NULL
;
846 notifs
->cn_Waiter
->cw_Notifier
= NULL
;
850 pwaiters
= &line
->cl_Waiters
;
851 while ((waiters
= *pwaiters
) != NULL
) {
852 *pwaiters
= waiters
->cw_Next
;
853 if (waiters
->cw_Notifier
)
854 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
861 if (file
->cf_Running
== 0) {
862 *pfile
= file
->cf_Next
;
863 free(file
->cf_DPath
);
864 free(file
->cf_FileName
);
865 free(file
->cf_UserName
);
874 * determine which jobs need to be run. Under normal conditions, the
875 * period is about a minute (one scan). Worst case it will be one
880 TestJobs(time_t t1
, time_t t2
)
887 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
888 if (file
->cf_Deleted
)
890 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
891 struct CronWaiter
*waiter
;
893 if (line
->cl_Pid
== -2) {
894 /* can job stop waiting? */
896 waiter
= line
->cl_Waiters
;
897 while (waiter
!= NULL
) {
898 if (waiter
->cw_Flag
> 0) {
899 /* notifier exited unsuccessfully */
902 } else if (waiter
->cw_Flag
< 0)
903 /* still waiting, notifier hasn't run to completion */
905 waiter
= waiter
->cw_Next
;
909 logn(LOG_DEBUG
, "cancelled waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
913 logn(LOG_DEBUG
, "finished waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
914 nJobs
+= ArmJob(file
, line
, 0, -1);
916 if (line->cl_NotUntil)
917 line->cl_NotUntil = t2;
925 * Find jobs > t1 and <= t2
928 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
930 struct tm
*tp
= localtime(&t
);
932 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
934 struct tm tnext
= *tp
;
936 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
937 n_wday
|= 16; /* last dow in month is always recognized as 5th */
940 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
941 if (file
->cf_Deleted
)
943 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
944 if (line
->cl_Pid
!= -1 && (line
->cl_Freq
== 0 || (line
->cl_Freq
> 0 && t2
>= line
->cl_NotUntil
))) {
945 /* (re)schedule job? */
946 if (line
->cl_Mins
[tp
->tm_min
] &&
947 line
->cl_Hrs
[tp
->tm_hour
] &&
948 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
949 line
->cl_Mons
[tp
->tm_mon
]
951 if (line
->cl_NotUntil
)
952 line
->cl_NotUntil
= t2
- t2
% 60 + line
->cl_Delay
; /* save what minute this job was scheduled/started waiting, plus cl_Delay */
953 nJobs
+= ArmJob(file
, line
, t1
, t2
);
964 * ArmJob: if t2 is (time_t)-1, we force-schedule the job without any waiting
965 * else it will wait on any of its declared notifiers who will run <= t2 + cw_MaxWait
969 ArmJob(CronFile
*file
, CronLine
*line
, time_t t1
, time_t t2
)
971 struct CronWaiter
*waiter
;
972 if (line
->cl_Pid
> 0) {
973 logn(LOG_NOTICE
, "process already running (%d): user %s %s\n",
978 } else if (t2
== -1 && line
->cl_Pid
!= -1) {
982 } else if (line
->cl_Pid
== 0) {
983 /* arming a waiting job (cl_Pid == -2) without forcing has no effect */
985 /* if we have any waiters, zero them and arm cl_Pid=-2 */
986 waiter
= line
->cl_Waiters
;
987 while (waiter
!= NULL
) {
988 /* check if notifier will run <= t2 + cw_Max_Wait? */
989 if (!waiter
->cw_NotifLine
)
990 /* notifier deleted */
992 else if (waiter
->cw_NotifLine
->cl_Pid
!= 0) {
993 /* if notifier is armed, or waiting, or running, we wait for it */
994 waiter
->cw_Flag
= -1;
996 } else if (waiter
->cw_NotifLine
->cl_Freq
< 0) {
997 /* arm any @noauto or @reboot jobs we're waiting on */
998 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t2
);
999 waiter
->cw_Flag
= -1;
1003 if (waiter
->cw_MaxWait
== 0)
1004 /* when no MaxWait interval specified, we always wait */
1005 waiter
->cw_Flag
= -1;
1006 else if (waiter
->cw_NotifLine
->cl_Freq
== 0 || (waiter
->cw_NotifLine
->cl_Freq
> 0 && t2
+ waiter
->cw_MaxWait
>= waiter
->cw_NotifLine
->cl_NotUntil
)) {
1007 /* default is don't wait */
1008 waiter
->cw_Flag
= 0;
1009 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
1011 struct tm
*tp
= localtime(&t
);
1013 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
1015 struct tm tnext
= *tp
;
1017 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
1018 n_wday
|= 16; /* last dow in month is always recognized as 5th */
1020 if (line
->cl_Mins
[tp
->tm_min
] &&
1021 line
->cl_Hrs
[tp
->tm_hour
] &&
1022 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
1023 line
->cl_Mons
[tp
->tm_mon
]
1025 /* notifier will run soon enough, we wait for it */
1026 waiter
->cw_Flag
= -1;
1034 waiter
= waiter
->cw_Next
;
1036 if (line
->cl_Pid
== -1) {
1037 /* job is ready to run */
1040 logn(LOG_DEBUG
, "scheduled: user %s %s\n",
1042 line
->cl_Description
1045 } else if (DebugOpt
)
1046 logn(LOG_DEBUG
, "waiting: user %s %s\n",
1048 line
->cl_Description
1055 TestStartupJobs(void)
1058 time_t t1
= time(NULL
);
1062 t1
= t1
- t1
% 60 + 60;
1064 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1066 logn(LOG_DEBUG
, "TestStartup for FILE %s/%s USER %s:\n",
1067 file
->cf_DPath
, file
->cf_FileName
, file
->cf_UserName
);
1068 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1069 struct CronWaiter
*waiter
;
1071 if (line
->cl_JobName
)
1072 logn(LOG_DEBUG
, " LINE %s JOB %s\n", line
->cl_Shell
, line
->cl_JobName
);
1074 logn(LOG_DEBUG
, " LINE %s\n", line
->cl_Shell
);
1077 if (line
->cl_Freq
== -1) {
1078 /* freq is @reboot */
1081 /* if we have any waiters, reset them and arm Pid = -2 */
1082 waiter
= line
->cl_Waiters
;
1083 while (waiter
!= NULL
) {
1084 waiter
->cw_Flag
= -1;
1086 /* we only arm @noauto jobs we're waiting on, not other @reboot jobs */
1087 if (waiter
->cw_NotifLine
&& waiter
->cw_NotifLine
->cl_Freq
== -2)
1088 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t1
+60);
1089 waiter
= waiter
->cw_Next
;
1091 if (line
->cl_Pid
== -1) {
1092 /* job is ready to run */
1096 logn(LOG_DEBUG
, " scheduled: %s\n", line
->cl_Description
);
1097 } else if (DebugOpt
)
1098 logn(LOG_DEBUG
, " waiting: %s\n", line
->cl_Description
);
1113 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1114 if (file
->cf_Ready
) {
1117 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1118 if (line
->cl_Pid
== -1) {
1122 logn(LOG_INFO
, "FILE %s/%s USER %s PID %3d %s\n",
1127 line
->cl_Description
1129 if (line
->cl_Pid
< 0)
1130 /* how could this happen? RunJob will leave cl_Pid set to 0 or the actual pid */
1132 else if (line
->cl_Pid
> 0)
1133 file
->cf_Running
= 1;
1141 * CheckJobs() - check for job completion
1143 * Check for job completion, return number of CronFiles still running after
1152 int nStillRunning
= 0;
1154 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1155 if (file
->cf_Running
) {
1156 file
->cf_Running
= 0;
1158 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1159 if (line
->cl_Pid
> 0) {
1161 int r
= waitpid(line
->cl_Pid
, &status
, WNOHANG
);
1163 if (r
< 0 || r
== line
->cl_Pid
) {
1164 if (r
> 0 && WIFEXITED(status
))
1165 status
= WEXITSTATUS(status
);
1169 logn(LOG_DEBUG
, "user %s %s finished with %d\n", file
->cf_UserName
, line
->cl_Description
, status
);
1170 EndJob(file
, line
, status
);
1172 file
->cf_Running
= 1;
1173 } else if (r
== 0) {
1174 file
->cf_Running
= 1;
1179 nStillRunning
+= file
->cf_Running
;
1181 return(nStillRunning
);