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