Conditionalize an #include so libcam can build the file.
[dragonfly.git] / usr.sbin / cron / cron / database.c
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  * $FreeBSD: src/usr.sbin/cron/cron/database.c,v 1.8 1999/08/28 01:15:50 peter Exp $
18  * $DragonFly: src/usr.sbin/cron/cron/database.c,v 1.5 2004/12/18 22:48:03 swildner Exp $
19  */
20
21 /* vix 26jan87 [RCS has the log]
22  */
23
24
25 #include "cron.h"
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/file.h>
29
30
31 #define TMAX(a,b) ((a)>(b)?(a):(b))
32
33
34 static  void            process_crontab(char *, char *, char *,
35                                              struct stat *,
36                                              cron_db *, cron_db *);
37
38
39 void
40 load_database(cron_db *old_db)
41 {
42         DIR             *dir;
43         struct stat     statbuf;
44         struct stat     syscron_stat;
45         DIR_T           *dp;
46         cron_db         new_db;
47         user            *u, *nu;
48
49         Debug(DLOAD, ("[%d] load_database()\n", getpid()))
50
51         /* before we start loading any data, do a stat on SPOOL_DIR
52          * so that if anything changes as of this moment (i.e., before we've
53          * cached any of the database), we'll see the changes next time.
54          */
55         if (stat(SPOOL_DIR, &statbuf) < OK) {
56                 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR);
57                 exit(ERROR_EXIT);
58         }
59
60         /* track system crontab file
61          */
62         if (stat(SYSCRONTAB, &syscron_stat) < OK)
63                 syscron_stat.st_mtime = 0;
64
65         /* if spooldir's mtime has not changed, we don't need to fiddle with
66          * the database.
67          *
68          * Note that old_db->mtime is initialized to 0 in main(), and
69          * so is guaranteed to be different than the stat() mtime the first
70          * time this function is called.
71          */
72         if (old_db->mtime == TMAX(statbuf.st_mtime, syscron_stat.st_mtime)) {
73                 Debug(DLOAD, ("[%d] spool dir mtime unch, no load needed.\n",
74                               getpid()))
75                 return;
76         }
77
78         /* something's different.  make a new database, moving unchanged
79          * elements from the old database, reloading elements that have
80          * actually changed.  Whatever is left in the old database when
81          * we're done is chaff -- crontabs that disappeared.
82          */
83         new_db.mtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime);
84         new_db.head = new_db.tail = NULL;
85
86         if (syscron_stat.st_mtime) {
87                 process_crontab("root", "*system*",
88                                 SYSCRONTAB, &syscron_stat,
89                                 &new_db, old_db);
90         }
91
92         /* we used to keep this dir open all the time, for the sake of
93          * efficiency.  however, we need to close it in every fork, and
94          * we fork a lot more often than the mtime of the dir changes.
95          */
96         if (!(dir = opendir(SPOOL_DIR))) {
97                 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR);
98                 exit(ERROR_EXIT);
99         }
100
101         while (NULL != (dp = readdir(dir))) {
102                 char    fname[MAXNAMLEN+1],
103                         tabname[MAXNAMLEN+1];
104
105                 /* avoid file names beginning with ".".  this is good
106                  * because we would otherwise waste two guaranteed calls
107                  * to getpwnam() for . and .., and also because user names
108                  * starting with a period are just too nasty to consider.
109                  */
110                 if (dp->d_name[0] == '.')
111                         continue;
112
113                 strncpy(fname, dp->d_name, sizeof(fname));
114                 fname[sizeof(fname)-1] = '\0';
115                 snprintf(tabname, sizeof tabname, CRON_TAB(fname));
116
117                 process_crontab(fname, fname, tabname,
118                                 &statbuf, &new_db, old_db);
119         }
120         closedir(dir);
121
122         /* if we don't do this, then when our children eventually call
123          * getpwnam() in do_command.c's child_process to verify MAILTO=,
124          * they will screw us up (and v-v).
125          */
126         endpwent();
127
128         /* whatever's left in the old database is now junk.
129          */
130         Debug(DLOAD, ("unlinking old database:\n"))
131         for (u = old_db->head;  u != NULL;  u = nu) {
132                 Debug(DLOAD, ("\t%s\n", u->name))
133                 nu = u->next;
134                 unlink_user(old_db, u);
135                 free_user(u);
136         }
137
138         /* overwrite the database control block with the new one.
139          */
140         *old_db = new_db;
141         Debug(DLOAD, ("load_database is done\n"))
142 }
143
144
145 void
146 link_user(cron_db *db, user *u)
147 {
148         if (db->head == NULL)
149                 db->head = u;
150         if (db->tail)
151                 db->tail->next = u;
152         u->prev = db->tail;
153         u->next = NULL;
154         db->tail = u;
155 }
156
157
158 void
159 unlink_user(cron_db *db, user *u)
160 {
161         if (u->prev == NULL)
162                 db->head = u->next;
163         else
164                 u->prev->next = u->next;
165
166         if (u->next == NULL)
167                 db->tail = u->prev;
168         else
169                 u->next->prev = u->prev;
170 }
171
172
173 user *
174 find_user(cron_db *db, char *name)
175 {
176         char    *env_get();
177         user    *u;
178
179         for (u = db->head;  u != NULL;  u = u->next)
180                 if (!strcmp(u->name, name))
181                         break;
182         return u;
183 }
184
185
186 static void
187 process_crontab(char *uname, char *fname, char *tabname, struct stat *statbuf,
188                 cron_db *new_db, cron_db *old_db)
189 {
190         struct passwd   *pw = NULL;
191         int             crontab_fd = OK - 1;
192         user            *u;
193
194         if (strcmp(fname, "*system*") && !(pw = getpwnam(uname))) {
195                 /* file doesn't have a user in passwd file.
196                  */
197                 log_it(fname, getpid(), "ORPHAN", "no passwd entry");
198                 goto next_crontab;
199         }
200
201         if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
202                 /* crontab not accessible?
203                  */
204                 log_it(fname, getpid(), "CAN'T OPEN", tabname);
205                 goto next_crontab;
206         }
207
208         if (fstat(crontab_fd, statbuf) < OK) {
209                 log_it(fname, getpid(), "FSTAT FAILED", tabname);
210                 goto next_crontab;
211         }
212
213         Debug(DLOAD, ("\t%s:", fname))
214         u = find_user(old_db, fname);
215         if (u != NULL) {
216                 /* if crontab has not changed since we last read it
217                  * in, then we can just use our existing entry.
218                  */
219                 if (u->mtime == statbuf->st_mtime) {
220                         Debug(DLOAD, (" [no change, using old data]"))
221                         unlink_user(old_db, u);
222                         link_user(new_db, u);
223                         goto next_crontab;
224                 }
225
226                 /* before we fall through to the code that will reload
227                  * the user, let's deallocate and unlink the user in
228                  * the old database.  This is more a point of memory
229                  * efficiency than anything else, since all leftover
230                  * users will be deleted from the old database when
231                  * we finish with the crontab...
232                  */
233                 Debug(DLOAD, (" [delete old data]"))
234                 unlink_user(old_db, u);
235                 free_user(u);
236                 log_it(fname, getpid(), "RELOAD", tabname);
237         }
238         u = load_user(crontab_fd, pw, fname);
239         if (u != NULL) {
240                 u->mtime = statbuf->st_mtime;
241                 link_user(new_db, u);
242         }
243
244 next_crontab:
245         if (crontab_fd >= OK) {
246                 Debug(DLOAD, (" [done]\n"))
247                 close(crontab_fd);
248         }
249 }