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
);
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
);
27 CronFile
*FileBase
= NULL
;
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
);
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 logn(LOG_DEBUG
, "Synchronizing %s\n", dpath
);
175 * Delete all database CronFiles for this directory. DeleteFile() will
176 * free *pfile and relink the *pfile pointer, or in the alternative will
177 * mark it as deleted.
180 while ((file
= *pfile
) != NULL
) {
181 if (file
->cf_Deleted
== 0 && strcmp(file
->cf_DPath
, dpath
) == 0) {
184 pfile
= &file
->cf_Next
;
189 * Since we are resynchronizing the entire directory, remove the
190 * the CRONUPDATE file.
192 asprintf(&path
, "%s/%s", dpath
, CRONUPDATE
);
197 * Scan the specified directory
199 if ((dir
= opendir(dpath
)) != NULL
) {
200 while ((den
= readdir(dir
)) != NULL
) {
201 if (strchr(den
->d_name
, '.') != NULL
)
203 if (strcmp(den
->d_name
, CRONUPDATE
) == 0)
206 SynchronizeFile(dpath
, den
->d_name
, user_override
);
207 } else if (getpwnam(den
->d_name
)) {
208 SynchronizeFile(dpath
, den
->d_name
, den
->d_name
);
210 logn(LOG_WARNING
, "ignoring %s/%s (non-existent user)\n",
217 logn(LOG_ERR
, "unable to scan directory %s\n", dpath
);
218 /* softerror, do not exit the program */
224 ReadTimestamps(const char *user
)
235 while (file
!= NULL
) {
236 if (file
->cf_Deleted
== 0 && (!user
|| strcmp(user
, file
->cf_UserName
) == 0)) {
237 line
= file
->cf_LineBase
;
238 while (line
!= NULL
) {
239 if (line
->cl_Timestamp
) {
240 if ((fi
= fopen(line
->cl_Timestamp
, "r")) != NULL
) {
241 if (fgets(buf
, sizeof(buf
), fi
) != NULL
) {
244 if (strncmp(buf
, "after ", 6) == 0) {
249 ptr
= strptime(ptr
, TIMESTAMP_FMT
, &tm
);
250 if (ptr
&& (*ptr
== 0 || *ptr
== '\n'))
251 /* strptime uses current seconds when seconds not specified? anyway, we don't get round minutes */
254 if (sec
== (time_t)-1) {
255 logn(LOG_ERR
, "unable to parse timestamp (user %s job %s)\n", file
->cf_UserName
, line
->cl_JobName
);
256 /* we continue checking other timestamps in this CronFile */
258 /* sec -= sec % 60; */
260 line
->cl_NotUntil
= sec
;
262 line
->cl_LastRan
= sec
;
263 freq
= (line
->cl_Freq
> 0) ? line
->cl_Freq
: line
->cl_Delay
;
264 /* if (line->cl_NotUntil < line->cl_LastRan + freq) */
265 line
->cl_NotUntil
= line
->cl_LastRan
+ freq
;
272 logn(LOG_NOTICE
, "no timestamp found (user %s job %s)\n", file
->cf_UserName
, line
->cl_JobName
);
273 /* write a fake timestamp file so our initial NotUntil doesn't keep being reset every hour when crond does a SynchronizeDir */
274 if ((fi
= fopen(line
->cl_Timestamp
, "w")) != NULL
) {
275 if (strftime(buf
, sizeof(buf
), TIMESTAMP_FMT
, localtime(&line
->cl_NotUntil
)))
276 if (fputs("after ", fi
) >= 0)
277 if (fputs(buf
,fi
) >= 0)
282 logn(LOG_WARNING
, "unable to write timestamp to %s (user %s %s)\n", line
->cl_Timestamp
, file
->cf_UserName
, line
->cl_Description
);
285 line
= line
->cl_Next
;
288 file
= file
->cf_Next
;
293 SynchronizeFile(const char *dpath
, const char *fileName
, const char *userName
)
306 if (strcmp(userName
, "root") == 0)
309 maxEntries
= MAXLINES
;
310 maxLines
= maxEntries
* 10;
313 * Delete any existing copy of this CronFile
316 while ((file
= *pfile
) != NULL
) {
317 if (file
->cf_Deleted
== 0 && strcmp(file
->cf_DPath
, dpath
) == 0 &&
318 strcmp(file
->cf_FileName
, fileName
) == 0
322 pfile
= &file
->cf_Next
;
326 asprintf(&path
, "%s/%s", dpath
, fileName
);
327 if ((fi
= fopen(path
, "r")) != NULL
) {
330 if (fstat(fileno(fi
), &sbuf
) == 0 && sbuf
.st_uid
== DaemonUid
) {
331 CronFile
*file
= calloc(1, sizeof(CronFile
));
333 time_t tnow
= time(NULL
);
336 file
->cf_UserName
= strdup(userName
);
337 file
->cf_FileName
= strdup(fileName
);
338 file
->cf_DPath
= strdup(dpath
);
339 pline
= &file
->cf_LineBase
;
341 while (fgets(buf
, sizeof(buf
), fi
) != NULL
&& --maxLines
) {
346 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
350 if (len
&& ptr
[len
-1] == '\n')
353 if (*ptr
== 0 || *ptr
== '#')
356 if (--maxEntries
== 0)
359 bzero(&line
, sizeof(line
));
362 logn(LOG_DEBUG
, "User %s Entry %s\n", userName
, buf
);
371 for (j
= 0; FreqAry
[j
]; ++j
) {
372 if (strncmp(ptr
, FreqAry
[j
], strlen(FreqAry
[j
])) == 0) {
377 ptr
+= strlen(FreqAry
[j
]);
390 line
.cl_Freq
= HOURLY_FREQ
;
393 line
.cl_Freq
= DAILY_FREQ
;
396 line
.cl_Freq
= WEEKLY_FREQ
;
399 line
.cl_Freq
= MONTHLY_FREQ
;
401 line
.cl_Freq
= YEARLY_FREQ
;
402 /* else line.cl_Freq will remain 0 */
406 if (!line
.cl_Freq
|| (*ptr
!= ' ' && *ptr
!= '\t')) {
407 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, buf
);
411 if (line
.cl_Delay
< 0) {
413 * delays on @daily, @hourly, etc are 1/20 of the frequency
414 * so they don't all start at once
415 * this also affects how they behave when the job returns EAGAIN
417 line
.cl_Delay
= line
.cl_Freq
/ 20;
418 line
.cl_Delay
-= line
.cl_Delay
% 60;
419 if (line
.cl_Delay
== 0)
421 /* all minutes are permitted */
427 /* days are numbered 1..31 */
433 while (*ptr
== ' ' || *ptr
== '\t')
441 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mins
, 60, 0, 1,
443 ptr
= ParseField(file
->cf_UserName
, line
.cl_Hrs
, 24, 0, 1,
445 ptr
= ParseField(file
->cf_UserName
, line
.cl_Days
, 32, 0, 1,
447 ptr
= ParseField(file
->cf_UserName
, line
.cl_Mons
, 12, -1, 1,
449 ptr
= ParseField(file
->cf_UserName
, line
.cl_Dow
, 7, 0, 31,
459 * fix days and dow - if one is not * and the other
460 * is *, the other is set to 0, and vise-versa
466 /* check for ID=... and AFTER=... and FREQ=... */
468 if (strncmp(ptr
, ID_TAG
, strlen(ID_TAG
)) == 0) {
469 if (line
.cl_JobName
) {
470 /* only assign ID_TAG once */
471 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
474 ptr
+= strlen(ID_TAG
);
476 * name = strsep(&ptr, seps):
477 * return name = ptr, and if ptr contains sep chars, overwrite first with 0 and point ptr to next char
480 asprintf(&line
.cl_Description
, "job %s", strsep(&ptr
, " \t"));
481 line
.cl_JobName
= line
.cl_Description
+ 4;
483 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, ID_TAG
, line
.cl_JobName
);
485 } else if (strncmp(ptr
, FREQ_TAG
, strlen(FREQ_TAG
)) == 0) {
487 /* only assign FREQ_TAG once */
488 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
492 ptr
+= strlen(FREQ_TAG
);
493 ptr
= ParseInterval(&line
.cl_Freq
, ptr
);
494 if (ptr
&& *ptr
== '/')
495 ptr
= ParseInterval(&line
.cl_Delay
, ++ptr
);
497 line
.cl_Delay
= line
.cl_Freq
;
499 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", userName
, base
);
500 } else if (*ptr
!= ' ' && *ptr
!= '\t') {
501 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s\n", userName
, base
);
505 } else if (strncmp(ptr
, WAIT_TAG
, strlen(WAIT_TAG
)) == 0) {
506 if (line
.cl_Waiters
) {
507 /* only assign WAIT_TAG once */
508 logn(LOG_WARNING
, "failed parsing crontab for user %s: repeated %s\n", userName
, ptr
);
513 ptr
+= strlen(WAIT_TAG
);
515 CronLine
*job
, **pjob
;
516 if (strcspn(ptr
,",") < strcspn(ptr
," \t"))
517 name
= strsep(&ptr
, ",");
520 name
= strsep(&ptr
, " \t");
522 if (!ptr
|| *ptr
== 0) {
523 /* unexpectedly this was the last token in buf; so abort */
524 logn(LOG_WARNING
, "failed parsing crontab for user %s: no command after %s%s\n", userName
, WAIT_TAG
, name
);
529 if ((w
= index(name
, '/')) != NULL
) {
531 w
= ParseInterval(&waitfor
, w
);
533 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s%s\n", userName
, WAIT_TAG
, name
);
540 /* look for a matching CronLine */
541 pjob
= &file
->cf_LineBase
;
542 while ((job
= *pjob
) != NULL
) {
543 if (job
->cl_JobName
&& strcmp(job
->cl_JobName
, name
) == 0) {
544 CronWaiter
*waiter
= malloc(sizeof(CronWaiter
));
545 CronNotifier
*notif
= malloc(sizeof(CronNotifier
));
546 waiter
->cw_Flag
= -1;
547 waiter
->cw_MaxWait
= waitfor
;
548 waiter
->cw_NotifLine
= job
;
549 waiter
->cw_Notifier
= notif
;
550 waiter
->cw_Next
= line
.cl_Waiters
; /* add to head of line.cl_Waiters */
551 line
.cl_Waiters
= waiter
;
552 notif
->cn_Waiter
= waiter
;
553 notif
->cn_Next
= job
->cl_Notifs
; /* add to head of job->cl_Notifs */
554 job
->cl_Notifs
= notif
;
557 pjob
= &job
->cl_Next
;
560 logn(LOG_WARNING
, "failed parsing crontab for user %s: unknown job %s\n", userName
, name
);
561 /* we can continue parsing this line, we just don't install any CronWaiter for the requested job */
565 } while (ptr
&& more
);
571 while (*ptr
== ' ' || *ptr
== '\t')
573 } while (!line
.cl_JobName
|| !line
.cl_Waiters
|| !line
.cl_Freq
);
575 if (line
.cl_JobName
&& (!ptr
|| *line
.cl_JobName
== 0)) {
576 /* we're aborting, or ID= was empty */
577 free(line
.cl_Description
);
578 line
.cl_Description
= NULL
;
579 line
.cl_JobName
= NULL
;
581 if (ptr
&& line
.cl_Delay
> 0 && !line
.cl_JobName
) {
582 logn(LOG_WARNING
, "failed parsing crontab for user %s: writing timestamp requires job %s to be named\n", userName
, ptr
);
586 /* couldn't parse so we abort; free any cl_Waiters */
587 if (line
.cl_Waiters
) {
588 CronWaiter
**pwaiters
, *waiters
;
589 pwaiters
= &line
.cl_Waiters
;
590 while ((waiters
= *pwaiters
) != NULL
) {
591 *pwaiters
= waiters
->cw_Next
;
592 /* leave the Notifier allocated but disabled */
593 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
599 /* now we've added any ID=... or AFTER=... */
602 * copy command string
604 line
.cl_Shell
= strdup(ptr
);
606 if (line
.cl_Delay
> 0) {
607 asprintf(&line
.cl_Timestamp
, "%s/%s.%s", TSDir
, userName
, line
.cl_JobName
);
608 line
.cl_NotUntil
= tnow
+ line
.cl_Delay
;
611 if (line
.cl_JobName
) {
613 logn(LOG_DEBUG
, " Command %s Job %s\n", line
.cl_Shell
, line
.cl_JobName
);
615 /* when cl_JobName is NULL, we point cl_Description to cl_Shell */
616 line
.cl_Description
= line
.cl_Shell
;
618 logn(LOG_DEBUG
, " Command %s\n", line
.cl_Shell
);
621 *pline
= calloc(1, sizeof(CronLine
));
622 /* copy working CronLine to newly allocated one */
625 pline
= &((*pline
)->cl_Next
);
630 file
->cf_Next
= FileBase
;
633 if (maxLines
== 0 || maxEntries
== 0)
634 logn(LOG_WARNING
, "maximum number of lines reached for user %s\n", userName
);
642 ParseInterval(int *interval
, char *ptr
)
645 if (ptr
&& *ptr
>= '0' && *ptr
<= '9' && (n
= strtol(ptr
, &ptr
, 10)) > 0)
670 ParseField(char *user
, char *ary
, int modvalue
, int off
, int onvalue
, const char **names
, char *ptr
)
679 while (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
683 * Handle numeric digit or symbol or '*'
687 n1
= 0; /* everything will be filled */
691 } else if (*ptr
>= '0' && *ptr
<= '9') {
693 n1
= strtol(ptr
, &ptr
, 10) + off
;
695 n2
= strtol(ptr
, &ptr
, 10) + off
;
700 for (i
= 0; names
[i
]; ++i
) {
701 if (strncmp(ptr
, names
[i
], strlen(names
[i
])) == 0) {
706 ptr
+= strlen(names
[i
]);
716 * handle optional range '-'
720 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
723 if (*ptr
== '-' && n2
< 0) {
729 * collapse single-value ranges, handle skipmark, and fill
730 * in the character array appropriately.
737 skip
= strtol(ptr
+ 1, &ptr
, 10);
740 * fill array, using a failsafe is the easiest way to prevent
750 n1
= (n1
+ 1) % modvalue
;
756 } while (n1
!= n2
&& --failsafe
);
759 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
770 if (*ptr
!= ' ' && *ptr
!= '\t' && *ptr
!= '\n') {
771 logn(LOG_WARNING
, "failed parsing crontab for user %s: %s\n", user
, base
);
775 while (*ptr
== ' ' || *ptr
== '\t' || *ptr
== '\n')
781 for (i
= 0; i
< modvalue
; ++i
)
783 logn(LOG_DEBUG
, "%2x ", ary
[i
]);
785 logn(LOG_DEBUG
, "%d", ary
[i
]);
786 logn(LOG_DEBUG
, "\n");
793 FixDayDow(CronLine
*line
)
799 for (i
= 0; i
< arysize(line
->cl_Dow
); ++i
) {
800 if (line
->cl_Dow
[i
] == 0) {
805 for (i
= 0; i
< arysize(line
->cl_Days
); ++i
) {
806 if (line
->cl_Days
[i
] == 0) {
810 /* change from "every Mon" to "ith Mon"
811 * 6th,7th... Dow are treated as 1st,2nd... */
812 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
813 line
->cl_Dow
[j
] &= 1 << (i
-1)%5;
816 /* change from "nth Mon" to "nth or ith Mon" */
817 for (j
= 0; j
< arysize(line
->cl_Dow
); ++j
) {
819 line
->cl_Dow
[j
] |= 1 << (i
-1)%5;
822 /* continue cycling through cl_Days */
831 memset(line
->cl_Days
, 0, sizeof(line
->cl_Days
));
833 if (daysUsed
&& !weekUsed
) {
834 memset(line
->cl_Dow
, 0, sizeof(line
->cl_Dow
));
839 * DeleteFile() - destroy a CronFile.
841 * The CronFile (*pfile) is destroyed if possible, and marked cf_Deleted
842 * if there are still active processes running on it. *pfile is relinked
846 DeleteFile(CronFile
**pfile
)
848 CronFile
*file
= *pfile
;
849 CronLine
**pline
= &file
->cf_LineBase
;
851 CronWaiter
**pwaiters
, *waiters
;
852 CronNotifier
**pnotifs
, *notifs
;
854 file
->cf_Running
= 0;
855 file
->cf_Deleted
= 1;
857 while ((line
= *pline
) != NULL
) {
858 if (line
->cl_Pid
> 0) {
859 file
->cf_Running
= 1;
860 pline
= &line
->cl_Next
;
862 *pline
= line
->cl_Next
;
863 free(line
->cl_Shell
);
865 if (line
->cl_JobName
)
866 /* this frees both cl_Description and cl_JobName
867 * if cl_JobName is NULL, Description pointed to ch_Shell, which was already freed
869 free(line
->cl_Description
);
870 if (line
->cl_Timestamp
)
871 free(line
->cl_Timestamp
);
873 pnotifs
= &line
->cl_Notifs
;
874 while ((notifs
= *pnotifs
) != NULL
) {
875 *pnotifs
= notifs
->cn_Next
;
876 if (notifs
->cn_Waiter
) {
877 notifs
->cn_Waiter
->cw_NotifLine
= NULL
;
878 notifs
->cn_Waiter
->cw_Notifier
= NULL
;
882 pwaiters
= &line
->cl_Waiters
;
883 while ((waiters
= *pwaiters
) != NULL
) {
884 *pwaiters
= waiters
->cw_Next
;
885 if (waiters
->cw_Notifier
)
886 waiters
->cw_Notifier
->cn_Waiter
= NULL
;
893 if (file
->cf_Running
== 0) {
894 *pfile
= file
->cf_Next
;
895 free(file
->cf_DPath
);
896 free(file
->cf_FileName
);
897 free(file
->cf_UserName
);
906 * determine which jobs need to be run. Under normal conditions, the
907 * period is about a minute (one scan). Worst case it will be one
912 TestJobs(time_t t1
, time_t t2
)
919 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
920 if (file
->cf_Deleted
)
922 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
923 struct CronWaiter
*waiter
;
925 if (line
->cl_Pid
== -2) {
926 /* can job stop waiting? */
928 waiter
= line
->cl_Waiters
;
929 while (waiter
!= NULL
) {
930 if (waiter
->cw_Flag
> 0) {
931 /* notifier exited unsuccessfully */
934 } else if (waiter
->cw_Flag
< 0)
935 /* still waiting, notifier hasn't run to completion */
937 waiter
= waiter
->cw_Next
;
941 logn(LOG_DEBUG
, "cancelled waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
945 logn(LOG_DEBUG
, "finished waiting: user %s %s\n", file
->cf_UserName
, line
->cl_Description
);
946 nJobs
+= ArmJob(file
, line
, 0, -1);
948 if (line->cl_NotUntil)
949 line->cl_NotUntil = t2;
957 * Find jobs > t1 and <= t2
960 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
962 struct tm
*tp
= localtime(&t
);
964 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
966 struct tm tnext
= *tp
;
968 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
969 n_wday
|= 16; /* last dow in month is always recognized as 5th */
972 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
973 if (file
->cf_Deleted
)
975 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
976 if (line
->cl_Pid
!= -1 && (line
->cl_Freq
== 0 || (line
->cl_Freq
> 0 && t2
>= line
->cl_NotUntil
))) {
977 /* (re)schedule job? */
978 if (line
->cl_Mins
[tp
->tm_min
] &&
979 line
->cl_Hrs
[tp
->tm_hour
] &&
980 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
981 line
->cl_Mons
[tp
->tm_mon
]
983 if (line
->cl_NotUntil
)
984 line
->cl_NotUntil
= t2
- t2
% 60 + line
->cl_Delay
; /* save what minute this job was scheduled/started waiting, plus cl_Delay */
985 nJobs
+= ArmJob(file
, line
, t1
, t2
);
996 * ArmJob: if t2 is (time_t)-1, we force-schedule the job without any waiting
997 * else it will wait on any of its declared notifiers who will run <= t2 + cw_MaxWait
1001 ArmJob(CronFile
*file
, CronLine
*line
, time_t t1
, time_t t2
)
1003 struct CronWaiter
*waiter
;
1004 if (line
->cl_Pid
> 0) {
1005 logn(LOG_NOTICE
, "process already running (%d): user %s %s\n",
1008 line
->cl_Description
1010 } else if (t2
== -1 && line
->cl_Pid
!= -1) {
1014 } else if (line
->cl_Pid
== 0) {
1015 /* arming a waiting job (cl_Pid == -2) without forcing has no effect */
1017 /* if we have any waiters, zero them and arm cl_Pid=-2 */
1018 waiter
= line
->cl_Waiters
;
1019 while (waiter
!= NULL
) {
1020 /* check if notifier will run <= t2 + cw_Max_Wait? */
1021 if (!waiter
->cw_NotifLine
)
1022 /* notifier deleted */
1023 waiter
->cw_Flag
= 0;
1024 else if (waiter
->cw_NotifLine
->cl_Pid
!= 0) {
1025 /* if notifier is armed, or waiting, or running, we wait for it */
1026 waiter
->cw_Flag
= -1;
1028 } else if (waiter
->cw_NotifLine
->cl_Freq
< 0) {
1029 /* arm any @noauto or @reboot jobs we're waiting on */
1030 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t2
);
1031 waiter
->cw_Flag
= -1;
1035 if (waiter
->cw_MaxWait
== 0)
1036 /* when no MaxWait interval specified, we always wait */
1037 waiter
->cw_Flag
= -1;
1038 else if (waiter
->cw_NotifLine
->cl_Freq
== 0 || (waiter
->cw_NotifLine
->cl_Freq
> 0 && t2
+ waiter
->cw_MaxWait
>= waiter
->cw_NotifLine
->cl_NotUntil
)) {
1039 /* default is don't wait */
1040 waiter
->cw_Flag
= 0;
1041 for (t
= t1
- t1
% 60; t
<= t2
; t
+= 60) {
1043 struct tm
*tp
= localtime(&t
);
1045 unsigned short n_wday
= (tp
->tm_mday
- 1)%7 + 1;
1047 struct tm tnext
= *tp
;
1049 if (mktime(&tnext
) != (time_t)-1 && tnext
.tm_mon
!= tp
->tm_mon
)
1050 n_wday
|= 16; /* last dow in month is always recognized as 5th */
1052 if (line
->cl_Mins
[tp
->tm_min
] &&
1053 line
->cl_Hrs
[tp
->tm_hour
] &&
1054 (line
->cl_Days
[tp
->tm_mday
] || (n_wday
&& line
->cl_Dow
[tp
->tm_wday
]) ) &&
1055 line
->cl_Mons
[tp
->tm_mon
]
1057 /* notifier will run soon enough, we wait for it */
1058 waiter
->cw_Flag
= -1;
1066 waiter
= waiter
->cw_Next
;
1068 if (line
->cl_Pid
== -1) {
1069 /* job is ready to run */
1072 logn(LOG_DEBUG
, "scheduled: user %s %s\n",
1074 line
->cl_Description
1077 } else if (DebugOpt
)
1078 logn(LOG_DEBUG
, "waiting: user %s %s\n",
1080 line
->cl_Description
1087 TestStartupJobs(void)
1090 time_t t1
= time(NULL
);
1094 t1
= t1
- t1
% 60 + 60;
1096 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1098 logn(LOG_DEBUG
, "TestStartup for FILE %s/%s USER %s:\n",
1099 file
->cf_DPath
, file
->cf_FileName
, file
->cf_UserName
);
1100 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1101 struct CronWaiter
*waiter
;
1103 if (line
->cl_JobName
)
1104 logn(LOG_DEBUG
, " LINE %s JOB %s\n", line
->cl_Shell
, line
->cl_JobName
);
1106 logn(LOG_DEBUG
, " LINE %s\n", line
->cl_Shell
);
1109 if (line
->cl_Freq
== -1) {
1110 /* freq is @reboot */
1113 /* if we have any waiters, reset them and arm Pid = -2 */
1114 waiter
= line
->cl_Waiters
;
1115 while (waiter
!= NULL
) {
1116 waiter
->cw_Flag
= -1;
1118 /* we only arm @noauto jobs we're waiting on, not other @reboot jobs */
1119 if (waiter
->cw_NotifLine
&& waiter
->cw_NotifLine
->cl_Freq
== -2)
1120 ArmJob(file
, waiter
->cw_NotifLine
, t1
, t1
+60);
1121 waiter
= waiter
->cw_Next
;
1123 if (line
->cl_Pid
== -1) {
1124 /* job is ready to run */
1128 logn(LOG_DEBUG
, " scheduled: %s\n", line
->cl_Description
);
1129 } else if (DebugOpt
)
1130 logn(LOG_DEBUG
, " waiting: %s\n", line
->cl_Description
);
1145 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1146 if (file
->cf_Ready
) {
1149 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1150 if (line
->cl_Pid
== -1) {
1154 logn(LOG_INFO
, "FILE %s/%s USER %s PID %3d %s\n",
1159 line
->cl_Description
1161 if (line
->cl_Pid
< 0)
1162 /* how could this happen? RunJob will leave cl_Pid set to 0 or the actual pid */
1164 else if (line
->cl_Pid
> 0)
1165 file
->cf_Running
= 1;
1173 * CheckJobs() - check for job completion
1175 * Check for job completion, return number of CronFiles still running after
1184 int nStillRunning
= 0;
1186 for (file
= FileBase
; file
; file
= file
->cf_Next
) {
1187 if (file
->cf_Running
) {
1188 file
->cf_Running
= 0;
1190 for (line
= file
->cf_LineBase
; line
; line
= line
->cl_Next
) {
1191 if (line
->cl_Pid
> 0) {
1193 int r
= waitpid(line
->cl_Pid
, &status
, WNOHANG
);
1195 if (r
< 0 || r
== line
->cl_Pid
) {
1196 if (r
> 0 && WIFEXITED(status
))
1197 status
= WEXITSTATUS(status
);
1200 EndJob(file
, line
, status
);
1202 file
->cf_Running
= 1;
1203 } else if (r
== 0) {
1204 file
->cf_Running
= 1;
1209 nStillRunning
+= file
->cf_Running
;
1211 return(nStillRunning
);