Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / cron / database.c
blob440ac3e7116f2a2ddda23fd330a0c7f231f1e4f3
1 /* $NetBSD: database.c,v 1.6 2002/11/16 04:34:13 itojun Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * All rights reserved
6 * Distribute freely, except: don't remove my name from the source or
7 * documentation (don't take credit for my work), mark your changes (don't
8 * get me blamed for your possible bugs), don't alter or remove this
9 * notice. May be sold if buildable source is provided to buyer. No
10 * warrantee of any kind, express or implied, is included with this
11 * software; use at your own risk, responsibility for damages (if any) to
12 * anyone resulting from the use of this software rests entirely with the
13 * user.
15 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
16 * I'll try to keep a version up to date. I can be reached as follows:
17 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
20 #include <sys/cdefs.h>
21 #if !defined(lint) && !defined(LINT)
22 #if 0
23 static char rcsid[] = "Id: database.c,v 2.8 1994/01/15 20:43:43 vixie Exp";
24 #else
25 __RCSID("$NetBSD: database.c,v 1.6 2002/11/16 04:34:13 itojun Exp $");
26 #endif
27 #endif
29 /* vix 26jan87 [RCS has the log]
33 #include "cron.h"
34 #include <fcntl.h>
35 #include <sys/stat.h>
36 #include <sys/file.h>
39 #define TMAX(a,b) ((a)>(b)?(a):(b))
42 static void process_crontab(const char *, const char *,
43 const char *, struct stat *,
44 cron_db *, cron_db *);
47 void
48 load_database(cron_db *old_db)
50 DIR *dir;
51 struct stat statbuf;
52 struct stat syscron_stat;
53 DIR_T *dp;
54 cron_db new_db;
55 user *u, *nu;
57 Debug(DLOAD, ("[%d] load_database()\n", getpid()))
59 /* before we start loading any data, do a stat on SPOOL_DIR
60 * so that if anything changes as of this moment (i.e., before we've
61 * cached any of the database), we'll see the changes next time.
63 if (stat(SPOOL_DIR, &statbuf) < OK) {
64 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR);
65 (void) exit(ERROR_EXIT);
68 /* track system crontab file
70 if (stat(SYSCRONTAB, &syscron_stat) < OK)
71 syscron_stat.st_mtime = 0;
73 /* if spooldir's mtime has not changed, we don't need to fiddle with
74 * the database.
76 * Note that old_db->mtime is initialized to 0 in main(), and
77 * so is guaranteed to be different than the stat() mtime the first
78 * time this function is called.
80 if (old_db->mtime == TMAX(statbuf.st_mtime, syscron_stat.st_mtime)) {
81 Debug(DLOAD, ("[%d] spool dir mtime unch, no load needed.\n",
82 getpid()))
83 return;
86 /* something's different. make a new database, moving unchanged
87 * elements from the old database, reloading elements that have
88 * actually changed. Whatever is left in the old database when
89 * we're done is chaff -- crontabs that disappeared.
91 new_db.mtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime);
92 new_db.head = new_db.tail = NULL;
94 if (syscron_stat.st_mtime) {
95 process_crontab("root", NULL, SYSCRONTAB, &syscron_stat,
96 &new_db, old_db);
99 /* we used to keep this dir open all the time, for the sake of
100 * efficiency. however, we need to close it in every fork, and
101 * we fork a lot more often than the mtime of the dir changes.
103 if (!(dir = opendir(SPOOL_DIR))) {
104 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR);
105 (void) exit(ERROR_EXIT);
108 while (NULL != (dp = readdir(dir))) {
109 char fname[MAXNAMLEN+1],
110 tabname[MAXNAMLEN+1];
112 /* avoid file names beginning with ".". this is good
113 * because we would otherwise waste two guaranteed calls
114 * to getpwnam() for . and .., and also because user names
115 * starting with a period are just too nasty to consider.
117 if (dp->d_name[0] == '.')
118 continue;
120 (void) strlcpy(fname, dp->d_name, sizeof(fname));
121 snprintf(tabname, sizeof(tabname), CRON_TAB(fname));
123 process_crontab(fname, fname, tabname,
124 &statbuf, &new_db, old_db);
126 closedir(dir);
128 /* if we don't do this, then when our children eventually call
129 * getpwnam() in do_command.c's child_process to verify MAILTO=,
130 * they will screw us up (and v-v).
132 endpwent();
134 /* whatever's left in the old database is now junk.
136 Debug(DLOAD, ("unlinking old database:\n"))
137 for (u = old_db->head; u != NULL; u = nu) {
138 Debug(DLOAD, ("\t%s\n", u->name))
139 nu = u->next;
140 unlink_user(old_db, u);
141 free_user(u);
144 /* overwrite the database control block with the new one.
146 *old_db = new_db;
147 Debug(DLOAD, ("load_database is done\n"))
151 void
152 link_user(cron_db *db, user *u)
154 if (db->head == NULL)
155 db->head = u;
156 if (db->tail)
157 db->tail->next = u;
158 u->prev = db->tail;
159 u->next = NULL;
160 db->tail = u;
164 void
165 unlink_user(cron_db *db, user *u)
167 if (u->prev == NULL)
168 db->head = u->next;
169 else
170 u->prev->next = u->next;
172 if (u->next == NULL)
173 db->tail = u->prev;
174 else
175 u->next->prev = u->prev;
179 user *
180 find_user(cron_db *db, const char *name)
182 user *u;
184 for (u = db->head; u != NULL; u = u->next)
185 if (!strcmp(u->name, name))
186 break;
187 return u;
191 static void
192 process_crontab(const char *uname, const char *fname, const char *tabname,
193 struct stat *statbuf, cron_db *new_db, cron_db *old_db)
195 struct passwd *pw = NULL;
196 int crontab_fd = OK - 1;
197 user *u;
199 if (fname == NULL) {
200 /* must be set to something.
202 fname = "*system*";
203 } else if ((pw = getpwnam(uname)) == NULL) {
204 /* file doesn't have a user in passwd file.
206 log_it(fname, getpid(), "ORPHAN", "no passwd entry");
207 goto next_crontab;
210 if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
211 /* crontab not accessible?
213 log_it(fname, getpid(), "CAN'T OPEN", tabname);
214 goto next_crontab;
217 if (fstat(crontab_fd, statbuf) < OK) {
218 log_it(fname, getpid(), "FSTAT FAILED", tabname);
219 goto next_crontab;
222 Debug(DLOAD, ("\t%s:", fname))
223 u = find_user(old_db, fname);
224 if (u != NULL) {
225 /* if crontab has not changed since we last read it
226 * in, then we can just use our existing entry.
228 if (u->mtime == statbuf->st_mtime) {
229 Debug(DLOAD, (" [no change, using old data]"))
230 unlink_user(old_db, u);
231 link_user(new_db, u);
232 goto next_crontab;
235 /* before we fall through to the code that will reload
236 * the user, let's deallocate and unlink the user in
237 * the old database. This is more a point of memory
238 * efficiency than anything else, since all leftover
239 * users will be deleted from the old database when
240 * we finish with the crontab...
242 Debug(DLOAD, (" [delete old data]"))
243 unlink_user(old_db, u);
244 free_user(u);
245 log_it(fname, getpid(), "RELOAD", tabname);
247 u = load_user(crontab_fd, pw, fname);
248 if (u != NULL) {
249 u->mtime = statbuf->st_mtime;
250 link_user(new_db, u);
253 next_crontab:
254 if (crontab_fd >= OK) {
255 Debug(DLOAD, (" [done]\n"))
256 close(crontab_fd);