xref: /dragonfly/usr.sbin/cron/cron/cron.h (revision 1b11ea06)
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  * $FreeBSD: src/usr.sbin/cron/cron/cron.h,v 1.17 2007/06/17 17:25:53 yar Exp $
21  *
22  * vix 14nov88 [rest of log is in RCS]
23  * vix 14jan87 [0 or 7 can be sunday; thanks, mwm@berkeley]
24  * vix 30dec86 [written]
25  */
26 
27 /* reorder these #include's at your peril */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include "compat.h"
32 
33 #include <bitstring.h>
34 #include <ctype.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <pwd.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <time.h>
41 #include <sys/wait.h>
42 
43 #include "pathnames.h"
44 #include "config.h"
45 #include "externs.h"
46 
47 	/* these are really immutable, and are
48 	 *   defined for symbolic convenience only
49 	 * TRUE, FALSE, and ERR must be distinct
50 	 * ERR must be < OK.
51 	 */
52 #define TRUE		1
53 #define FALSE		0
54 	/* system calls return this on success */
55 #define OK		0
56 	/*   or this on error */
57 #define ERR		(-1)
58 
59 	/* turn this on to get '-x' code */
60 #ifndef DEBUGGING
61 #define DEBUGGING	FALSE
62 #endif
63 
64 #define READ_PIPE	0	/* which end of a pipe pair do you read? */
65 #define WRITE_PIPE	1	/*   or write to? */
66 #define STDIN		0	/* what is stdin's file descriptor? */
67 #define STDOUT		1	/*   stdout's? */
68 #define STDERR		2	/*   stderr's? */
69 #define ERROR_EXIT	1	/* exit() with this will scare the shell */
70 #define	OK_EXIT		0	/* exit() with this is considered 'normal' */
71 #define	MAX_FNAME	100	/* max length of internally generated fn */
72 #define	MAX_COMMAND	1000	/* max length of internally generated cmd */
73 #define	MAX_ENVSTR	1000	/* max length of envvar=value\0 strings */
74 #define	MAX_TEMPSTR	100	/* obvious */
75 #define	MAX_UNAME	20	/* max length of username, should be overkill */
76 #define	ROOT_UID	0	/* don't change this, it really must be root */
77 #define	ROOT_USER	"root"	/* ditto */
78 #define	SYS_NAME	"*system*" /* magic owner name for system crontab */
79 
80 				/* NOTE: these correspond to DebugFlagNames,
81 				 *	defined below.
82 				 */
83 #define	DEXT		0x0001	/* extend flag for other debug masks */
84 #define	DSCH		0x0002	/* scheduling debug mask */
85 #define	DPROC		0x0004	/* process control debug mask */
86 #define	DPARS		0x0008	/* parsing debug mask */
87 #define	DLOAD		0x0010	/* database loading debug mask */
88 #define	DMISC		0x0020	/* misc debug mask */
89 #define	DTEST		0x0040	/* test mode: don't execute any commands */
90 #define	DBIT		0x0080	/* bit twiddling shown (long) */
91 
92 #define	CRON_TAB(u)	"%s/%s", SPOOL_DIR, u
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_MINUTE	0
125 #define	LAST_MINUTE	59
126 #define	MINUTE_COUNT	(LAST_MINUTE - FIRST_MINUTE + 1)
127 
128 #define	FIRST_HOUR	0
129 #define	LAST_HOUR	23
130 #define	HOUR_COUNT	(LAST_HOUR - FIRST_HOUR + 1)
131 
132 #define	FIRST_DOM	1
133 #define	LAST_DOM	31
134 #define	DOM_COUNT	(LAST_DOM - FIRST_DOM + 1)
135 
136 #define	FIRST_MONTH	1
137 #define	LAST_MONTH	12
138 #define	MONTH_COUNT	(LAST_MONTH - FIRST_MONTH + 1)
139 
140 /* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */
141 #define	FIRST_DOW	0
142 #define	LAST_DOW	7
143 #define	DOW_COUNT	(LAST_DOW - FIRST_DOW + 1)
144 
145 #ifdef LOGIN_CAP
146 /* see init.c */
147 #define RESOURCE_RC "daemon"
148 #endif
149 
150 			/* each user's crontab will be held as a list of
151 			 * the following structure.
152 			 *
153 			 * These are the cron commands.
154 			 */
155 
156 typedef	struct _entry {
157 	struct _entry	*next;
158 	uid_t		uid;
159 	gid_t		gid;
160 #ifdef LOGIN_CAP
161 	char            *class;
162 #endif
163 	char		**envp;
164 	char		*cmd;
165 	bitstr_t	bit_decl(minute, MINUTE_COUNT);
166 	bitstr_t	bit_decl(hour,   HOUR_COUNT);
167 	bitstr_t	bit_decl(dom,    DOM_COUNT);
168 	bitstr_t	bit_decl(month,  MONTH_COUNT);
169 	bitstr_t	bit_decl(dow,    DOW_COUNT);
170 	int		flags;
171 #define	DOM_STAR	0x01
172 #define	DOW_STAR	0x02
173 #define	WHEN_REBOOT	0x04
174 #define	RUN_AT	0x08
175 #define	NOT_UNTIL	0x10
176 	time_t	lastrun;
177 } entry;
178 
179 			/* the crontab database will be a list of the
180 			 * following structure, one element per user
181 			 * plus one for the system.
182 			 *
183 			 * These are the crontabs.
184 			 */
185 
186 typedef	struct _user {
187 	struct _user	*next, *prev;	/* links */
188 	char		*name;
189 	time_t		mtime;		/* last modtime of crontab */
190 	entry		*crontab;	/* this person's crontab */
191 } user;
192 
193 typedef	struct _cron_db {
194 	user		*head, *tail;	/* links */
195 	time_t		mtime;		/* last modtime on spooldir */
196 } cron_db;
197 
198 
199 void		set_cron_uid(void),
200 		set_cron_cwd(void),
201 		load_database(cron_db *),
202 		open_logfile(void),
203 		sigpipe_func(void),
204 		job_add(entry *, user *),
205 		do_command(entry *, user *),
206 		link_user(cron_db *, user *),
207 		unlink_user(cron_db *, user *),
208 		free_user(user *),
209 		env_free(char **),
210 		unget_char(int, FILE *),
211 		free_entry(entry *),
212 		acquire_daemonlock(int),
213 		skip_comments(FILE *),
214 		log_it(char *, int, char *, char *),
215 		log_close(void);
216 
217 int		job_runqueue(void),
218 		set_debug_flags(char *),
219 		get_char(FILE *),
220 		get_string(char *, int, FILE *, char *),
221 		swap_uids(void),
222 		load_env(char *, FILE *),
223 		cron_pclose(FILE *),
224 		strcmp_until(char *, char *, int),
225 		allowed(char *),
226 		strdtb(char *);
227 
228 char		*env_get(char *, char **),
229 		*arpadate(time_t *),
230 		*mkprints(unsigned char *, unsigned int),
231 		*first_word(char *, char *),
232 		**env_init(void),
233 		**env_copy(char **),
234 		**env_set(char **, char *);
235 
236 user		*load_user(int, struct passwd *, char *),
237 		*find_user(cron_db *, char *);
238 
239 entry		*load_entry(FILE *, void (*)(),
240 				 struct passwd *, char **);
241 
242 FILE		*cron_popen(char *, char *, entry *);
243 
244 
245 				/* in the C tradition, we only create
246 				 * variables for the main program, just
247 				 * extern them elsewhere.
248 				 */
249 
250 #ifdef MAIN_PROGRAM
251 # if !defined(LINT) && !defined(lint)
252 char	*copyright[] = {
253 		"@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie",
254 		"@(#) All rights reserved"
255 	};
256 # endif
257 
258 char	*MonthNames[] = {
259 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
260 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
261 		NULL
262 	};
263 
264 char	*DowNames[] = {
265 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
266 		NULL
267 	};
268 
269 char	*ProgramName;
270 int	LineNumber;
271 unsigned Jitter,
272 	RootJitter;
273 time_t	TargetTime;
274 
275 # if DEBUGGING
276 int	DebugFlags;
277 char	*DebugFlagNames[] = {	/* sync with #defines */
278 		"ext", "sch", "proc", "pars", "load", "misc", "test", "bit",
279 		NULL		/* NULL must be last element */
280 	};
281 # endif /* DEBUGGING */
282 #else /*MAIN_PROGRAM*/
283 extern	char	*copyright[],
284 		*MonthNames[],
285 		*DowNames[],
286 		*ProgramName;
287 extern	int	LineNumber;
288 extern unsigned	Jitter,
289 		RootJitter;
290 extern	time_t	TargetTime;
291 # if DEBUGGING
292 extern	int	DebugFlags;
293 extern	char	*DebugFlagNames[];
294 # endif /* DEBUGGING */
295 #endif /*MAIN_PROGRAM*/
296