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
));
309 file
->cf_UserName
= strdup(userName
);
310 file
->cf_FileName
= strdup(fileName
);
311 file
->cf_DPath
= strdup(dpath
);
312 pline
= &file
->cf_LineBase
;
314 while (fgets(buf
, sizeof(buf
), fi
) != NULL
&& --maxLines
) {
319 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
323 if (len
&& ptr
[len
-1] == '\n')
326 if (*ptr
== 0 || *ptr
== '#')
329 if (--maxEntries
== 0)
332 bzero(&line
, sizeof(line
));
335 logn(LOG_DEBUG
, "User %s Entry %s\n", userName
, buf
);
344 for (j
= 0; FreqAry
[j
]; ++j
) {
345 if (strncmp(ptr
, FreqAry
[j
], strlen(FreqAry
[j
])) == 0) {
350 ptr
+= strlen(FreqAry
[j
]);
363 line
.cl_Freq
= HOURLY_FREQ
;
366 line
.cl_Freq
= DAILY_FREQ
;
369 line
.cl_Freq
= WEEKLY_FREQ
;
372 line
.cl_Freq
= MONTHLY_FREQ
;
374 line
.cl_Freq
= YEARLY_FREQ
;
375 /* else line.cl_Freq will remain 0 */
379 if (!line
.cl_Freq
|| (*ptr
!= ' ' && *ptr
!= '\t')) {
380 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, buf
);
384 if (line
.cl_Delay
< 0) {
385 line
.cl_Delay
= line
.cl_Freq
/ 20;
386 /* all minutes are permitted */
392 /* days are numbered 1..31 */
398 while (*ptr
== ' ' || *ptr
== '\t')
406 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mins
, 60, 0, 1,
408 ptr
= ParseField(file
->cf_UserName
, line
.cl_Hrs
, 24, 0, 1,
410 ptr
= ParseField(file
->cf_UserName
, line
.cl_Days
, 32, 0, 1,
412 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mons
, 12, -1, 1,
414 ptr
= ParseField(file
->cf_UserName
, line
.cl_Dow
, 7, 0, 31,
424 * fix days and dow - if one is not * and the other
425 * is *, the other is set to 0, and vise-versa
431 /* check for ID=... and AFTER=... and FREQ=... */
433 if (strncmp(ptr
, ID_TAG
, strlen(ID_TAG
)) == 0) {
434 if (line
.cl_JobName
) {
435 /* only assign ID_TAG once */
436 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
439 ptr
+= strlen(ID_TAG
);
441 * name = strsep(&ptr, seps):
442 * return name = ptr, and if ptr contains sep chars, overwrite first with 0 and point ptr to next char
445 asprintf(&line
.cl_Description
, "job %s", strsep(&ptr
, " \t"));
446 line
.cl_JobName
= line
.cl_Description
+ 4;
448 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, ID_TAG
, line
.cl_JobName
);
450 } else if (strncmp(ptr
, FREQ_TAG
, strlen(FREQ_TAG
)) == 0) {
452 /* only assign FREQ_TAG once */
453 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
457 ptr
+= strlen(FREQ_TAG
);
458 ptr
= ParseInterval(&line
.cl_Freq
, ptr
);
459 if (ptr
&& *ptr
== '/')
460 ptr
= ParseInterval(&line
.cl_Delay
, ++ptr
);
462 line
.cl_Delay
= line
.cl_Freq
;
464 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, base
);
465 } else if (*ptr
!= ' ' && *ptr
!= '\t') {
466 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s\n", userName
, base
);
470 } else if (strncmp(ptr
, WAIT_TAG
, strlen(WAIT_TAG
)) == 0) {
471 if (line
.cl_Waiters
) {
472 /* only assign WAIT_TAG once */
473 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
478 ptr
+= strlen(WAIT_TAG
);
480 CronLine
*job
, **pjob
;
481 if (strcspn(ptr
,",") < strcspn(ptr
," \t"))
482 name
= strsep(&ptr
, ",");
485 name
= strsep(&ptr
, " \t");
487 if (!ptr
|| *ptr
== 0) {
488 /* unexpectedly this was the last token in buf; so abort */
489 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, WAIT_TAG
, name
);
494 if ((w
= index(name
, '/')) != NULL
) {
496 w
= ParseInterval(&waitfor
, w
);
498 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s%s\n", userName
, WAIT_TAG
, name
);
505 /* look for a matching CronLine */
506 pjob
= &file
->cf_LineBase
;
507 while ((job
= *pjob
) != NULL
) {
508 if (job
->cl_JobName
&& strcmp(job
->cl_JobName
, name
) == 0) {
509 CronWaiter
*waiter
= malloc(sizeof(CronWaiter
));
510 CronNotifier
*notif
= malloc(sizeof(CronNotifier
));
511 waiter
->cw_Flag
= -1;
512 waiter
->cw_MaxWait
= waitfor
;
513 waiter
->cw_NotifLine
= job
;
514 waiter
->cw_Notifier
= notif
;
515 waiter
->cw_Next
= line
.cl_Waiters
; /* add to head of line.cl_Waiters */
516 line
.cl_Waiters
= waiter
;
517 notif
->cn_Waiter
= waiter
;
518 notif
->cn_Next
= job
->cl_Notifs
; /* add to head of job->cl_Notifs */
519 job
->cl_Notifs
= notif
;
522 pjob
= &job
->cl_Next
;
525 logn(LOG_WARNING
, "failed parsing crontab for user %s: unknown job %s\n", userName
, name
);
526 /* we can continue parsing this line, we just don't install any CronWaiter for the requested job */
530 } while (ptr
&& more
);
536 while (*ptr
== ' ' || *ptr
== '\t')
538 } while (!line
.cl_JobName
|| !line
.cl_Waiters
|| !line
.cl_Freq
);
540 if (line
.cl_JobName
&& (!ptr
|| *line
.cl_JobName
== 0)) {
541 /* we're aborting, or ID= was empty */
542 free(line
.cl_Description
);
543 line
.cl_Description
= NULL
;
544 line
.cl_JobName
= NULL
;
546 if (ptr
&& line
.cl_Delay
> 0 && !line
.cl_JobName
) {
547 logn(LOG_WARNING
, "failed parsing crontab for user %s: writing timestamp requires job %s to be named\n", userName
, ptr
);
551 /* couldn't parse so we abort; free any cl_Waiters */
552 if (line
.cl_Waiters
) {
553 CronWaiter
**pwaiters
, *waiters
;
554 pwaiters
= &line
.cl_Waiters
;
555 while ((waiters
= *pwaiters
) != NULL
) {
556 *pwaiters
= waiters
->cw_Next
;
557 /* leave the Notifier allocated but disabled */
558 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
564 /* now we've added any ID=... or AFTER=... */
567 * copy command string
569 line
.cl_Shell
= strdup(ptr
);
571 if (line
.cl_Delay
> 0) {
572 asprintf(&line
.cl_Timestamp
, "%s/%s.%s", TSDir
, userName
, line
.cl_JobName
);
573 line
.cl_NotUntil
= time(NULL
) + line
.cl_Delay
;
576 if (line
.cl_JobName
) {
578 logn(LOG_DEBUG
, " Command %s Job %s\n", line
.cl_Shell
, line
.cl_JobName
);
580 /* when cl_JobName is NULL, we point cl_Description to cl_Shell */
581 line
.cl_Description
= line
.cl_Shell
;
583 logn(LOG_DEBUG
, " Command %s\n", line
.cl_Shell
);
586 *pline
= calloc(1, sizeof(CronLine
));
587 /* copy working CronLine to newly allocated one */
590 pline
= &((*pline
)->cl_Next
);
595 file
->cf_Next
= FileBase
;
598 if (maxLines
== 0 || maxEntries
== 0)
599 logn(LOG_WARNING
, "maximum number of lines reached for user %s\n", userName
);
607 ParseInterval(int *interval
, char *ptr
)
610 if (ptr
&& *ptr
>= '0' && *ptr
<= '9' && (n
= strtol(ptr
, &ptr
, 10)) > 0)
635 ParseField(char *user
, char *ary
, int modvalue
, int off
, int onvalue
, const char **names
, char *ptr
)
644 while (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
648 * Handle numeric digit or symbol or '*'
652 n1
= 0; /* everything will be filled */
656 } else if (*ptr
>= '0' && *ptr
<= '9') {
658 n1
= strtol(ptr
, &ptr
, 10) + off
;
660 n2
= strtol(ptr
, &ptr
, 10) + off
;
665 for (i
= 0; names
[i
]; ++i
) {
666 if (strncmp(ptr
, names
[i
], strlen(names
[i
])) == 0) {
671 ptr
+= strlen(names
[i
]);
681 * handle optional range '-'
685 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
688 if (*ptr
== '-' && n2
< 0) {
694 * collapse single-value ranges, handle skipmark, and fill
695 * in the character array appropriately.
702 skip
= strtol(ptr
+ 1, &ptr
, 10);
705 * fill array, using a failsafe is the easiest way to prevent
715 n1
= (n1
+ 1) % modvalue
;
721 } while (n1
!= n2
&& --failsafe
);
724 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
735 if (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
736 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
740 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
746 for (i
= 0; i
< modvalue
; ++i
)
747 logn(LOG_DEBUG
, "%2x ", ary
[i
]);
748 logn(LOG_DEBUG
, "\n");
755 FixDayDow(CronLine
*line
)
761 for (i
= 0; i
< arysize(line
->cl_Dow
); ++i
) {
762 if (line
->cl_Dow
[i
] == 0) {
767 for (i
= 0; i
< arysize(line
->cl_Days
); ++i
) {
768 if (line
->cl_Days
[i
] == 0) {
772 /* change from "every Mon" to "ith Mon"
773 * 6th,7th... Dow are treated as 1st,2nd... */
774 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
775 line
->cl_Dow
[j
] &= 1 << (i
-1)%5;
778 /* change from "nth Mon" to "nth or ith Mon" */
779 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
781 line
->cl_Dow
[j
] |= 1 << (i
-1)%5;
784 /* continue cycling through cl_Days */
793 memset(line
->cl_Days
, 0, sizeof(line
->cl_Days
));
795 if (daysUsed
&& !weekUsed
) {
796 memset(line
->cl_Dow
, 0, sizeof(line
->cl_Dow
));
801 * DeleteFile() - destroy a CronFile.
803 * The CronFile (*pfile) is destroyed if possible, and marked cf_Deleted
804 * if there are still active processes running on it. *pfile is relinked
808 DeleteFile(CronFile
**pfile
)
810 CronFile
*file
= *pfile
;
811 CronLine
**pline
= &file
->cf_LineBase
;
813 CronWaiter
**pwaiters
, *waiters
;
814 CronNotifier
**pnotifs
, *notifs
;
816 file
->cf_Running
= 0;
817 file
->cf_Deleted
= 1;
819 while ((line
= *pline
) != NULL
) {
820 if (line
->cl_Pid
> 0) {
821 file
->cf_Running
= 1;
822 pline
= &line
->cl_Next
;
824 *pline
= line
->cl_Next
;
825 free(line
->cl_Shell
);
827 if (line
->cl_JobName
)
828 /* this frees both cl_Description and cl_JobName
829 * if cl_JobName is NULL, Description pointed to ch_Shell, which was already freed
831 free(line
->cl_Description
);
832 if (line
->cl_Timestamp
)
833 free(line
->cl_Timestamp
);
835 pnotifs
= &line
->cl_Notifs
;
836 while ((notifs
= *pnotifs
) != NULL
) {
837 *pnotifs
= notifs
->cn_Next
;
838 if (notifs
->cn_Waiter
) {
839 notifs
->cn_Waiter
->cw_NotifLine
= NULL
;
840 notifs
->cn_Waiter
->cw_Notifier
= NULL
;
844 pwaiters
= &line
->cl_Waiters
;
845 while ((waiters
= *pwaiters
) != NULL
) {
846 *pwaiters
= waiters
->cw_Next
;
847 if (waiters
->cw_Notifier
)
848 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
855 if (file
->cf_Running
== 0) {
856 *pfile
= file
->cf_Next
;
857 free(file
->cf_DPath
);
858 free(file
->cf_FileName
);
859 free(file
->cf_UserName
);
868 * determine which jobs need to be run. Under normal conditions, the
869 * period is about a minute (one scan). Worst case it will be one
874 TestJobs(time_t t1
, time_t t2
)
881 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
882 if (file
->cf_Deleted
)
884 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
885 struct CronWaiter
*waiter
;
887 if (line
->cl_Pid
== -2) {
888 /* can job stop waiting? */
890 waiter
= line
->cl_Waiters
;
891 while (waiter
!= NULL
) {
892 if (waiter
->cw_Flag
> 0) {
893 /* notifier exited unsuccessfully */
896 } else if (waiter
->cw_Flag
< 0)
897 /* still waiting, notifier hasn't run to completion */
899 waiter
= waiter
->cw_Next
;
903 logn(LOG_DEBUG
, "cancelled waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
907 logn(LOG_DEBUG
, "finished waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
908 nJobs
+= ArmJob(file
, line
, 0, -1);
910 if (line->cl_NotUntil)
911 line->cl_NotUntil = t2;
919 * Find jobs > t1 and <= t2
922 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
924 struct tm
*tp
= localtime(&t
);
926 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
928 struct tm tnext
= *tp
;
930 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
931 n_wday
|= 16; /* last dow in month is always recognized as 5th */
934 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
935 if (file
->cf_Deleted
)
937 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
938 if (line
->cl_Pid
!= -1 && (line
->cl_Freq
== 0 || (line
->cl_Freq
> 0 && t2
>= line
->cl_NotUntil
))) {
939 /* (re)schedule job? */
940 if (line
->cl_Mins
[tp
->tm_min
] &&
941 line
->cl_Hrs
[tp
->tm_hour
] &&
942 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
943 line
->cl_Mons
[tp
->tm_mon
]
945 if (line
->cl_NotUntil
)
946 line
->cl_NotUntil
= t2
- t2
% 60 + line
->cl_Delay
; /* save what minute this job was scheduled/started waiting, plus cl_Delay */
947 nJobs
+= ArmJob(file
, line
, t1
, t2
);
958 * ArmJob: if t2 is (time_t)-1, we force-schedule the job without any waiting
959 * else it will wait on any of its declared notifiers who will run <= t2 + cw_MaxWait
963 ArmJob(CronFile
*file
, CronLine
*line
, time_t t1
, time_t t2
)
965 struct CronWaiter
*waiter
;
966 if (line
->cl_Pid
> 0) {
967 logn(LOG_NOTICE
, "process already running (%d): user %s %s\n",
972 } else if (t2
== -1 && line
->cl_Pid
!= -1) {
976 } else if (line
->cl_Pid
== 0) {
977 /* arming a waiting job (cl_Pid == -2) without forcing has no effect */
979 /* if we have any waiters, zero them and arm cl_Pid=-2 */
980 waiter
= line
->cl_Waiters
;
981 while (waiter
!= NULL
) {
982 /* check if notifier will run <= t2 + cw_Max_Wait? */
983 if (!waiter
->cw_NotifLine
)
984 /* notifier deleted */
986 else if (waiter
->cw_NotifLine
->cl_Pid
!= 0) {
987 /* if notifier is armed, or waiting, or running, we wait for it */
988 waiter
->cw_Flag
= -1;
990 } else if (waiter
->cw_NotifLine
->cl_Freq
< 0) {
991 /* arm any @noauto or @reboot jobs we're waiting on */
992 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t2
);
993 waiter
->cw_Flag
= -1;
997 if (waiter
->cw_MaxWait
== 0)
998 /* when no MaxWait interval specified, we always wait */
999 waiter
->cw_Flag
= -1;
1000 else if (waiter
->cw_NotifLine
->cl_Freq
== 0 || (waiter
->cw_NotifLine
->cl_Freq
> 0 && t2
+ waiter
->cw_MaxWait
>= waiter
->cw_NotifLine
->cl_NotUntil
)) {
1001 /* default is don't wait */
1002 waiter
->cw_Flag
= 0;
1003 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
1005 struct tm
*tp
= localtime(&t
);
1007 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
1009 struct tm tnext
= *tp
;
1011 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
1012 n_wday
|= 16; /* last dow in month is always recognized as 5th */
1014 if (line
->cl_Mins
[tp
->tm_min
] &&
1015 line
->cl_Hrs
[tp
->tm_hour
] &&
1016 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
1017 line
->cl_Mons
[tp
->tm_mon
]
1019 /* notifier will run soon enough, we wait for it */
1020 waiter
->cw_Flag
= -1;
1028 waiter
= waiter
->cw_Next
;
1030 if (line
->cl_Pid
== -1) {
1031 /* job is ready to run */
1034 logn(LOG_DEBUG
, "scheduled: user %s %s\n",
1036 line
->cl_Description
1039 } else if (DebugOpt
)
1040 logn(LOG_DEBUG
, "waiting: user %s %s\n",
1042 line
->cl_Description
1049 TestStartupJobs(void)
1052 time_t t1
= time(NULL
);
1056 t1
= t1
- t1
% 60 + 60;
1058 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1060 logn(LOG_DEBUG
, "TestStartup for FILE %s/%s USER %s:\n",
1061 file
->cf_DPath
, file
->cf_FileName
, file
->cf_UserName
);
1062 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1063 struct CronWaiter
*waiter
;
1065 if (line
->cl_JobName
)
1066 logn(LOG_DEBUG
, " LINE %s JOB %s\n", line
->cl_Shell
, line
->cl_JobName
);
1068 logn(LOG_DEBUG
, " LINE %s\n", line
->cl_Shell
);
1071 if (line
->cl_Freq
== -1) {
1072 /* freq is @reboot */
1075 /* if we have any waiters, reset them and arm Pid = -2 */
1076 waiter
= line
->cl_Waiters
;
1077 while (waiter
!= NULL
) {
1078 waiter
->cw_Flag
= -1;
1080 /* we only arm @noauto jobs we're waiting on, not other @reboot jobs */
1081 if (waiter
->cw_NotifLine
&& waiter
->cw_NotifLine
->cl_Freq
== -2)
1082 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t1
+60);
1083 waiter
= waiter
->cw_Next
;
1085 if (line
->cl_Pid
== -1) {
1086 /* job is ready to run */
1090 logn(LOG_DEBUG
, " scheduled: %s\n", line
->cl_Description
);
1091 } else if (DebugOpt
)
1092 logn(LOG_DEBUG
, " waiting: %s\n", line
->cl_Description
);
1107 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1108 if (file
->cf_Ready
) {
1111 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1112 if (line
->cl_Pid
== -1) {
1116 logn(LOG_INFO
, "FILE %s/%s USER %s PID %3d %s\n",
1121 line
->cl_Description
1123 if (line
->cl_Pid
< 0)
1124 /* how could this happen? RunJob will leave cl_Pid set to 0 or the actual pid */
1126 else if (line
->cl_Pid
> 0)
1127 file
->cf_Running
= 1;
1135 * CheckJobs() - check for job completion
1137 * Check for job completion, return number of CronFiles still running after
1146 int nStillRunning
= 0;
1148 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1149 if (file
->cf_Running
) {
1150 file
->cf_Running
= 0;
1152 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1153 if (line
->cl_Pid
> 0) {
1155 int r
= waitpid(line
->cl_Pid
, &status
, WNOHANG
);
1157 if (r
< 0 || r
== line
->cl_Pid
) {
1158 if (r
> 0 && WIFEXITED(status
))
1159 status
= WEXITSTATUS(status
);
1162 EndJob(file
, line
, status
);
1164 file
->cf_Running
= 1;
1165 } else if (r
== 0) {
1166 file
->cf_Running
= 1;
1171 nStillRunning
+= file
->cf_Running
;
1173 return(nStillRunning
);