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