]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/cron/cron/cron.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / cron / cron / cron.h
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$
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 <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 MAX_UNAME       20      /* max length of username, should be overkill */
77 #define ROOT_UID        0       /* don't change this, it really must be root */
78 #define ROOT_USER       "root"  /* ditto */
79 #define SYS_NAME        "*system*" /* magic owner name for system crontab */
80
81                                 /* NOTE: these correspond to DebugFlagNames,
82                                  *      defined below.
83                                  */
84 #define DEXT            0x0001  /* extend flag for other debug masks */
85 #define DSCH            0x0002  /* scheduling debug mask */
86 #define DPROC           0x0004  /* process control debug mask */
87 #define DPARS           0x0008  /* parsing debug mask */
88 #define DLOAD           0x0010  /* database loading debug mask */
89 #define DMISC           0x0020  /* misc debug mask */
90 #define DTEST           0x0040  /* test mode: don't execute any commands */
91 #define DBIT            0x0080  /* bit twiddling shown (long) */
92
93 #define CRON_TAB(u)     "%s/%s", SPOOL_DIR, u
94 #define REG             register
95 #define PPC_NULL        ((char **)NULL)
96
97 #ifndef MAXHOSTNAMELEN
98 #define MAXHOSTNAMELEN 256
99 #endif
100
101 #define Skip_Blanks(c, f) \
102                         while (c == '\t' || c == ' ') \
103                                 c = get_char(f);
104
105 #define Skip_Nonblanks(c, f) \
106                         while (c!='\t' && c!=' ' && c!='\n' && c != EOF) \
107                                 c = get_char(f);
108
109 #define Skip_Line(c, f) \
110                         do {c = get_char(f);} while (c != '\n' && c != EOF);
111
112 #if DEBUGGING
113 # define Debug(mask, message) \
114                         if ( (DebugFlags & (mask) ) == (mask) ) \
115                                 printf message;
116 #else /* !DEBUGGING */
117 # define Debug(mask, message) \
118                         ;
119 #endif /* DEBUGGING */
120
121 #define MkLower(ch)     (isupper(ch) ? tolower(ch) : ch)
122 #define MkUpper(ch)     (islower(ch) ? toupper(ch) : ch)
123 #define Set_LineNum(ln) {Debug(DPARS|DEXT,("linenum=%d\n",ln)); \
124                          LineNumber = ln; \
125                         }
126
127 #define FIRST_MINUTE    0
128 #define LAST_MINUTE     59
129 #define MINUTE_COUNT    (LAST_MINUTE - FIRST_MINUTE + 1)
130
131 #define FIRST_HOUR      0
132 #define LAST_HOUR       23
133 #define HOUR_COUNT      (LAST_HOUR - FIRST_HOUR + 1)
134
135 #define FIRST_DOM       1
136 #define LAST_DOM        31
137 #define DOM_COUNT       (LAST_DOM - FIRST_DOM + 1)
138
139 #define FIRST_MONTH     1
140 #define LAST_MONTH      12
141 #define MONTH_COUNT     (LAST_MONTH - FIRST_MONTH + 1)
142
143 /* note on DOW: 0 and 7 are both Sunday, for compatibility reasons. */
144 #define FIRST_DOW       0
145 #define LAST_DOW        7
146 #define DOW_COUNT       (LAST_DOW - FIRST_DOW + 1)
147
148 #ifdef LOGIN_CAP
149 /* see init.c */
150 #define RESOURCE_RC "daemon"
151 #endif
152
153                         /* each user's crontab will be held as a list of
154                          * the following structure.
155                          *
156                          * These are the cron commands.
157                          */
158
159 typedef struct _entry {
160         struct _entry   *next;
161         uid_t           uid;
162         gid_t           gid;
163 #ifdef LOGIN_CAP
164         char            *class;
165 #endif
166         char            **envp;
167         char            *cmd;
168         bitstr_t        bit_decl(minute, MINUTE_COUNT);
169         bitstr_t        bit_decl(hour,   HOUR_COUNT);
170         bitstr_t        bit_decl(dom,    DOM_COUNT);
171         bitstr_t        bit_decl(month,  MONTH_COUNT);
172         bitstr_t        bit_decl(dow,    DOW_COUNT);
173         int             flags;
174 #define DOM_STAR        0x01
175 #define DOW_STAR        0x02
176 #define WHEN_REBOOT     0x04
177 #define RUN_AT  0x08
178 #define NOT_UNTIL       0x10
179         time_t  lastrun;
180 } entry;
181
182                         /* the crontab database will be a list of the
183                          * following structure, one element per user
184                          * plus one for the system.
185                          *
186                          * These are the crontabs.
187                          */
188
189 typedef struct _user {
190         struct _user    *next, *prev;   /* links */
191         char            *name;
192         time_t          mtime;          /* last modtime of crontab */
193         entry           *crontab;       /* this person's crontab */
194 } user;
195
196 typedef struct _cron_db {
197         user            *head, *tail;   /* links */
198         time_t          mtime;          /* last modtime on spooldir */
199 } cron_db;
200
201
202 void            set_cron_uid(void),
203                 set_cron_cwd(void),
204                 load_database(cron_db *),
205                 open_logfile(void),
206                 sigpipe_func(void),
207                 job_add(entry *, user *),
208                 do_command(entry *, user *),
209                 link_user(cron_db *, user *),
210                 unlink_user(cron_db *, user *),
211                 free_user(user *),
212                 env_free(char **),
213                 unget_char(int, FILE *),
214                 free_entry(entry *),
215                 skip_comments(FILE *),
216                 log_it(char *, int, char *, char *),
217                 log_close(void);
218
219 int             job_runqueue(void),
220                 set_debug_flags(char *),
221                 get_char(FILE *),
222                 get_string(char *, int, FILE *, char *),
223                 swap_uids(void),
224                 swap_uids_back(void),
225                 load_env(char *, FILE *),
226                 cron_pclose(FILE *),
227                 strcmp_until(char *, char *, int),
228                 allowed(char *),
229                 strdtb(char *);
230
231 char            *env_get(char *, char **),
232                 *arpadate(time_t *),
233                 *mkprints(unsigned char *, unsigned int),
234                 *first_word(char *, char *),
235                 **env_init(void),
236                 **env_copy(char **),
237                 **env_set(char **, char *);
238
239 user            *load_user(int, struct passwd *, char *),
240                 *find_user(cron_db *, char *);
241
242 entry           *load_entry(FILE *, void (*)(char *),
243                                  struct passwd *, char **);
244
245 FILE            *cron_popen(char *, char *, entry *);
246
247
248                                 /* in the C tradition, we only create
249                                  * variables for the main program, just
250                                  * extern them elsewhere.
251                                  */
252
253 #ifdef MAIN_PROGRAM
254 # if !defined(LINT) && !defined(lint)
255 char    *copyright[] = {
256                 "@(#) Copyright 1988,1989,1990,1993,1994 by Paul Vixie",
257                 "@(#) All rights reserved"
258         };
259 # endif
260
261 char    *MonthNames[] = {
262                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
263                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
264                 NULL
265         };
266
267 char    *DowNames[] = {
268                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
269                 NULL
270         };
271
272 char    *ProgramName,
273         *defmailto;
274 int     LineNumber;
275 unsigned Jitter,
276         RootJitter;
277 time_t  TargetTime;
278
279 # if DEBUGGING
280 int     DebugFlags;
281 char    *DebugFlagNames[] = {   /* sync with #defines */
282                 "ext", "sch", "proc", "pars", "load", "misc", "test", "bit",
283                 NULL            /* NULL must be last element */
284         };
285 # endif /* DEBUGGING */
286 #else /*MAIN_PROGRAM*/
287 extern  char    *copyright[],
288                 *MonthNames[],
289                 *DowNames[],
290                 *ProgramName,
291                 *defmailto;
292 extern  int     LineNumber;
293 extern unsigned Jitter,
294                 RootJitter;
295 extern  time_t  TargetTime;
296 extern struct pidfh *pfh;
297 # if DEBUGGING
298 extern  int     DebugFlags;
299 extern  char    *DebugFlagNames[];
300 # endif /* DEBUGGING */
301 #endif /*MAIN_PROGRAM*/