5 * Copyright 1994-1998 Matthew Dillon (dillon@backplane.com)
6 * Copyright 2009-2011 James Pryor <profjim@jimpryor.net>
7 * May be distributed under the GNU General Public License
12 * 0. gcc defaults to _BSD_SOURCE and _POSIX_SOURCE
13 * 1. need _POSIX_SOURCE or _XOPEN_SOURCE for getopt, fileno, sigaction
14 * 2. need _XOPEN_SOURCE for strptime
15 * 3. need _BSD_SOURCE for setenv, mk{d,s}temp, [v]snprintf, initgroups, strsep, strdup, setre{u,g}id, gethostname, perror
16 * 4. use concat.c instead of requiring asprintf / _GNU_SOURCE
19 #define _XOPEN_SOURCE 1
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
26 #include <sys/resource.h>
47 #define Prototype extern
48 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
51 #define SCRONTABS "/etc/cron.d"
54 #define CRONTABS "/var/spool/cron/crontabs"
57 #define CRONSTAMPS "/var/spool/cron/cronstamps"
60 #define LOG_IDENT "crond"
63 #define TIMESTAMP_FMT "%b %e %H:%M:%S"
67 #define LOG_LEVEL LOG_NOTICE
70 #define CRONSTAMP_FMT "%Y-%m-%d %H:%M"
73 #define CRONUPDATE "cron.update"
80 #define SENDMAIL "/usr/sbin/sendmail"
83 #define SENDMAIL_ARGS "-t", "-oem", "-i"
86 #define PATH_VI "/usr/bin/vi" /* location of vi */
93 #define WAIT_TAG "AFTER="
96 #define FREQ_TAG "FREQ="
99 #define HOURLY_FREQ 60 * 60
100 #define DAILY_FREQ 24 * HOURLY_FREQ
101 #define WEEKLY_FREQ 7 * DAILY_FREQ
102 #define MONTHLY_FREQ 30 * DAILY_FREQ
103 #define YEARLY_FREQ 365 * DAILY_FREQ
105 #define LOGHEADER TIMESTAMP_FMT " %%s " LOG_IDENT ": "
106 #define LOCALE_LOGHEADER "%c %%s " LOG_IDENT ": "
109 #define MAXOPEN 256 /* close fds < this limit */
110 #define MAXLINES 256 /* max lines in non-root crontabs */
111 #define SMALL_BUFFER 256
112 #define RW_BUFFER 1024
113 #define LOG_BUFFER 2048 /* max size of log line */
118 typedef struct CronFile
{
119 struct CronFile
*cf_Next
;
120 struct CronLine
*cf_LineBase
;
121 char *cf_DPath
; /* Directory path to cronfile */
122 char *cf_FileName
; /* Name of cronfile */
123 char *cf_UserName
; /* username to execute jobs as */
124 int cf_Ready
; /* bool: one or more jobs ready */
125 int cf_Running
; /* bool: one or more jobs running */
126 int cf_Deleted
; /* marked for deletion, ignore */
129 typedef struct CronLine
{
130 struct CronLine
*cl_Next
;
131 char *cl_Shell
; /* shell command */
132 char *cl_Description
; /* either "<cl_Shell>" or "job <cl_JobName>" */
133 char *cl_JobName
; /* job name, if any */
134 char *cl_Timestamp
; /* path to timestamp file, if cl_Freq defined */
135 struct CronWaiter
*cl_Waiters
;
136 struct CronNotifier
*cl_Notifs
;
137 int cl_Freq
; /* 0 (use arrays), minutes, -1 (noauto), -2 (startup) */
138 int cl_Delay
; /* defaults to cl_Freq or hourly */
141 int cl_Pid
; /* running pid, 0, or armed (-1), or waiting (-2) */
142 int cl_MailFlag
; /* running pid is for mail */
143 int cl_MailPos
; /* 'empty file' size */
144 char cl_Mins
[60]; /* 0-59 */
145 char cl_Hrs
[24]; /* 0-23 */
146 char cl_Days
[32]; /* 1-31 */
147 char cl_Mons
[12]; /* 0-11 */
148 char cl_Dow
[7]; /* 0-6, beginning sunday */
151 typedef struct CronWaiter
{
152 struct CronWaiter
*cw_Next
;
153 struct CronNotifier
*cw_Notifier
;
154 struct CronLine
*cw_NotifLine
;
159 typedef struct CronNotifier
{
160 struct CronNotifier
*cn_Next
;
161 struct CronWaiter
*cn_Waiter
;