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