xref: /freebsd-13-stable/usr.sbin/cron/cron/cron.h (revision 17da660ad5b3b9cd90e164dd4dbb9beaa7203054)
1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2  * All rights reserved
3  *
4  * Distribute freely, except: don't remove my name from the source or
5  * documentation (don't take credit for my work), mark your changes (don't
6  * get me blamed for your possible bugs), don't alter or remove this
7  * notice.  May be sold if buildable source is provided to buyer.  No
8  * warrantee of any kind, express or implied, is included with this
9  * software; use at your own risk, responsibility for damages (if any) to
10  * anyone resulting from the use of this software rests entirely with the
11  * user.
12  *
13  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14  * I'll try to keep a version up to date.  I can be reached as follows:
15  * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
16  */
17 
18 /* cron.h - header for vixie's cron
19  *
20  * vix 14nov88 [rest of log is in RCS]
21  * vix 14jan87 [0 or 7 can be sunday; thanks, mwm@berkeley]
22  * vix 30dec86 [written]
23  */
24 
25 /* reorder these #include's at your peril */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include "compat.h"
30 
31 #include <bitstring.h>
32 #include <ctype.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <libutil.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <time.h>
40 #include <sys/wait.h>
41 
42 #include "pathnames.h"
43 #include "config.h"
44 #include "externs.h"
45 
46 	/* these are really immutable, and are
47 	 *   defined for symbolic convenience only
48 	 * TRUE, FALSE, and ERR must be distinct
49 	 * ERR must be < OK.
50 	 */
51 #define TRUE		1
52 #define FALSE		0
53 	/* system calls return this on success */
54 #define OK		0
55 	/*   or this on error */
56 #define ERR		(-1)
57 
58 	/* turn this on to get '-x' code */
59 #ifndef DEBUGGING
60 #define DEBUGGING	FALSE
61 #endif
62 
63 #define READ_PIPE	0	/* which end of a pipe pair do you read? */
64 #define WRITE_PIPE	1	/*   or write to? */
65 #define STDIN		0	/* what is stdin's file descriptor? */
66 #define STDOUT		1	/*   stdout's? */
67 #define STDERR		2	/*   stderr's? */
68 #define ERROR_EXIT	1	/* exit() with this will scare the shell */
69 #define	OK_EXIT		0	/* exit() with this is considered 'normal' */
70 #define	MAX_FNAME	100	/* max length of internally generated fn */
71 #define	MAX_COMMAND	1000	/* max length of internally generated cmd */
72 #define	MAX_ENVSTR	1000	/* max length of envvar=value\0 strings */
73 #define	MAX_TEMPSTR	100	/* obvious */
74 #define	ROOT_UID	0	/* don't change this, it really must be root */
75 #define	ROOT_USER	"root"	/* ditto */
76 #define	SYS_NAME	"*system*" /* magic owner name for system crontab */
77 
78 				/* NOTE: these correspond to DebugFlagNames,
79 				 *	defined below.
80 				 */
81 #define	DEXT		0x0001	/* extend flag for other debug masks */
82 #define	DSCH		0x0002	/* scheduling debug mask */
83 #define	DPROC		0x0004	/* process control debug mask */
84 #define	DPARS		0x0008	/* parsing debug mask */
85 #define	DLOAD		0x0010	/* database loading debug mask */
86 #define	DMISC		0x0020	/* misc debug mask */
87 #define	DTEST		0x0040	/* test mode: don't execute any commands */
88 #define	DBIT		0x0080	/* bit twiddling shown (long) */
89 
90 #define	CRON_TAB(u)	"%s/%s", SPOOL_DIR, u
91 #define	REG		register
92 #define	PPC_NULL	((char **)NULL)
93 
94 #ifndef MAXHOSTNAMELEN
95 #define MAXHOSTNAMELEN 256
96 #endif
97 
98 #define	Skip_Blanks(c, f) \
99 			while (c == '\t' || c == ' ') \
100 				c = get_char(f);
101 
102 #define	Skip_Nonblanks(c, f) \
103 			while (c!='\t' && c!=' ' && c!='\n' && c != EOF) \
104 				c = get_char(f);
105 
106 #define	Skip_Line(c, f) \
107 			do {c = get_char(f);} while (c != '\n' && c != EOF);
108 
109 #if DEBUGGING
110 # define Debug(mask, message) \
111 			if ( (DebugFlags & (mask) ) == (mask) ) \
112 				printf message;
113 #else /* !DEBUGGING */
114 # define Debug(mask, message) \
115 			;
116 #endif /* DEBUGGING */
117 
118 #define	MkLower(ch)	(isupper(ch) ? tolower(ch) : ch)
119 #define	MkUpper(ch)	(islower(ch) ? toupper(ch) : ch)
120 #define	Set_LineNum(ln)	{Debug(DPARS|DEXT,("linenum=%d\n",ln)); \
121 			 LineNumber = ln; \
122 			}
123 
124 #define	FIRST_SECOND	0
125 #define	LAST_SECOND	59
126 #define	SECOND_COUNT	(LAST_SECOND - FIRST_SECOND + 1)
127 
128 #define	FIRST_MINUTE	0
129 #define	LAST_MINUTE	59
130 #define	MINUTE_COUNT	(LAST_MINUTE - FIRST_MINUTE + 1)
131 
132 #define	FIRST_HOUR	0
133 #define	LAST_HOUR	23
134 #define	HOUR_COUNT	(LAST_HOUR - FIRST_HOUR + 1)
135 
136 #define	FIRST_DOM	1
137 #define	LAST_DOM	31
138 #define	DOM_COUNT	(LAST_DOM - FIRST_DOM + 1)
139 
140 #define	FIRST_MONTH	1
141 #define	LAST_MONTH	12
142 #define	MONTH_COUNT	(LAST_MONTH - FIRST_MONTH + 1)
143 
144 /* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */
145 #define	FIRST_DOW	0
146 #define	LAST_DOW	7
147 #define	DOW_COUNT	(LAST_DOW - FIRST_DOW + 1)
148 
149 #ifdef LOGIN_CAP
150 /* see init.c */
151 #define RESOURCE_RC "daemon"
152 #endif
153 
154 			/* each user's crontab will be held as a list of
155 			 * the following structure.
156 			 *
157 			 * These are the cron commands.
158 			 */
159 
160 typedef	struct _entry {
161 	struct _entry	*next;
162 	uid_t		uid;
163 	gid_t		gid;
164 #ifdef LOGIN_CAP
165 	char            *class;
166 #endif
167 	char		**envp;
168 	char		*cmd;
169 	union {
170 		struct {
171 			bitstr_t	bit_decl(second, SECOND_COUNT);
172 			bitstr_t	bit_decl(minute, MINUTE_COUNT);
173 			bitstr_t	bit_decl(hour,   HOUR_COUNT);
174 			bitstr_t	bit_decl(dom,    DOM_COUNT);
175 			bitstr_t	bit_decl(month,  MONTH_COUNT);
176 			bitstr_t	bit_decl(dow,    DOW_COUNT);
177 		};
178 		struct {
179 			time_t	lastexit;
180 			time_t	interval;
181 			pid_t	child;
182 		};
183 	};
184 	int		flags;
185 #define	DOM_STAR	0x01
186 #define	DOW_STAR	0x02
187 #define	WHEN_REBOOT	0x04
188 #define	RUN_AT		0x08
189 #define	NOT_UNTIL	0x10
190 #define	SEC_RES		0x20
191 #define	INTERVAL	0x40
192 #define	DONT_LOG	0x80
193 #define	MAIL_WHEN_ERR	0x100
194 	time_t	lastrun;
195 } entry;
196 
197 			/* the crontab database will be a list of the
198 			 * following structure, one element per user
199 			 * plus one for the system.
200 			 *
201 			 * These are the crontabs.
202 			 */
203 
204 typedef	struct _user {
205 	struct _user	*next, *prev;	/* links */
206 	char		*name;
207 	time_t		mtime;		/* last modtime of crontab */
208 	entry		*crontab;	/* this person's crontab */
209 } user;
210 
211 typedef	struct _cron_db {
212 	user		*head, *tail;	/* links */
213 	time_t		mtime;		/* last modtime on spooldir */
214 } cron_db;
215 
216 
217 void		set_cron_uid(void),
218 		set_cron_cwd(void),
219 		load_database(cron_db *),
220 		open_logfile(void),
221 		sigpipe_func(void),
222 		job_add(entry *, user *),
223 		do_command(entry *, user *),
224 		link_user(cron_db *, user *),
225 		unlink_user(cron_db *, user *),
226 		free_user(user *),
227 		env_free(char **),
228 		unget_char(int, FILE *),
229 		free_entry(entry *),
230 		skip_comments(FILE *),
231 		log_it(char *, int, char *, const char *),
232 		log_close(void);
233 
234 int		job_runqueue(void),
235 		set_debug_flags(char *),
236 		get_char(FILE *),
237 		get_string(char *, int, FILE *, char *),
238 		swap_uids(void),
239 		swap_uids_back(void),
240 		load_env(char *, FILE *),
241 		cron_pclose(FILE *),
242 		strcmp_until(char *, char *, int),
243 		allowed(char *),
244 		strdtb(char *);
245 
246 char		*env_get(char *, char **),
247 		*arpadate(time_t *),
248 		*mkprints(unsigned char *, unsigned int),
249 		*first_word(char *, char *),
250 		**env_init(void),
251 		**env_copy(char **),
252 		**env_set(char **, char *);
253 
254 user		*load_user(int, struct passwd *, char *),
255 		*find_user(cron_db *, char *);
256 
257 entry		*load_entry(FILE *, void (*)(char *),
258 				 struct passwd *, char **);
259 
260 FILE		*cron_popen(char *, char *, entry *, PID_T *);
261 
262 
263 				/* in the C tradition, we only create
264 				 * variables for the main program, just
265 				 * extern them elsewhere.
266 				 */
267 
268 #ifdef MAIN_PROGRAM
269 # if !defined(LINT) && !defined(lint)
270 char	*copyright[] = {
271 		"@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie",
272 		"@(#) All rights reserved"
273 	};
274 # endif
275 
276 char	*MonthNames[] = {
277 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
278 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
279 		NULL
280 	};
281 
282 char	*DowNames[] = {
283 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
284 		NULL
285 	};
286 
287 char	*ProgramName,
288 	*defmailto;
289 int	LineNumber;
290 unsigned Jitter,
291 	RootJitter;
292 time_t	TargetTime;
293 
294 # if DEBUGGING
295 int	DebugFlags;
296 char	*DebugFlagNames[] = {	/* sync with #defines */
297 		"ext", "sch", "proc", "pars", "load", "misc", "test", "bit",
298 		NULL		/* NULL must be last element */
299 	};
300 # endif /* DEBUGGING */
301 #else /*MAIN_PROGRAM*/
302 extern	char	*copyright[],
303 		*MonthNames[],
304 		*DowNames[],
305 		*ProgramName,
306 		*defmailto;
307 extern	int	LineNumber;
308 extern unsigned	Jitter,
309 		RootJitter;
310 extern	time_t	TargetTime;
311 extern struct pidfh *pfh;
312 # if DEBUGGING
313 extern	int	DebugFlags;
314 extern	char	*DebugFlagNames[];
315 # endif /* DEBUGGING */
316 #endif /*MAIN_PROGRAM*/
317