1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
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
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
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
23 /* vix 26jan87 [RCS has the log]
33 #define TMAX(a,b) ((a)>(b)?(a):(b))
36 static void process_crontab(char *, char *, char *,
38 cron_db *, cron_db *);
47 struct stat syscron_stat;
52 Debug(DLOAD, ("[%d] load_database()\n", getpid()))
54 /* before we start loading any data, do a stat on SPOOL_DIR
55 * so that if anything changes as of this moment (i.e., before we've
56 * cached any of the database), we'll see the changes next time.
58 if (stat(SPOOL_DIR, &statbuf) < OK) {
59 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR);
60 (void) exit(ERROR_EXIT);
63 /* track system crontab file
65 if (stat(SYSCRONTAB, &syscron_stat) < OK)
66 syscron_stat.st_mtime = 0;
68 /* if spooldir's mtime has not changed, we don't need to fiddle with
71 * Note that old_db->mtime is initialized to 0 in main(), and
72 * so is guaranteed to be different than the stat() mtime the first
73 * time this function is called.
75 if (old_db->mtime == TMAX(statbuf.st_mtime, syscron_stat.st_mtime)) {
76 Debug(DLOAD, ("[%d] spool dir mtime unch, no load needed.\n",
81 /* something's different. make a new database, moving unchanged
82 * elements from the old database, reloading elements that have
83 * actually changed. Whatever is left in the old database when
84 * we're done is chaff -- crontabs that disappeared.
86 new_db.mtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime);
87 new_db.head = new_db.tail = NULL;
89 if (syscron_stat.st_mtime) {
90 process_crontab("root", SYS_NAME,
91 SYSCRONTAB, &syscron_stat,
95 /* we used to keep this dir open all the time, for the sake of
96 * efficiency. however, we need to close it in every fork, and
97 * we fork a lot more often than the mtime of the dir changes.
99 if (!(dir = opendir(SPOOL_DIR))) {
100 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR);
101 (void) exit(ERROR_EXIT);
104 while (NULL != (dp = readdir(dir))) {
105 char fname[MAXNAMLEN+1],
106 tabname[MAXNAMLEN+1];
108 /* avoid file names beginning with ".". this is good
109 * because we would otherwise waste two guaranteed calls
110 * to getpwnam() for . and .., and also because user names
111 * starting with a period are just too nasty to consider.
113 if (dp->d_name[0] == '.')
116 (void) strncpy(fname, dp->d_name, sizeof(fname));
117 fname[sizeof(fname)-1] = '\0';
118 (void) snprintf(tabname, sizeof tabname, CRON_TAB(fname));
120 process_crontab(fname, fname, tabname,
121 &statbuf, &new_db, old_db);
125 /* if we don't do this, then when our children eventually call
126 * getpwnam() in do_command.c's child_process to verify MAILTO=,
127 * they will screw us up (and v-v).
131 /* whatever's left in the old database is now junk.
133 Debug(DLOAD, ("unlinking old database:\n"))
134 for (u = old_db->head; u != NULL; u = nu) {
135 Debug(DLOAD, ("\t%s\n", u->name))
137 unlink_user(old_db, u);
141 /* overwrite the database control block with the new one.
144 Debug(DLOAD, ("load_database is done\n"))
153 if (db->head == NULL)
171 u->prev->next = u->next;
176 u->next->prev = u->prev;
188 for (u = db->head; u != NULL; u = u->next)
189 if (!strcmp(u->name, name))
196 process_crontab(uname, fname, tabname, statbuf, new_db, old_db)
200 struct stat *statbuf;
204 struct passwd *pw = NULL;
205 int crontab_fd = OK - 1;
208 if (strcmp(fname, SYS_NAME) && !(pw = getpwnam(uname))) {
209 /* file doesn't have a user in passwd file.
211 log_it(fname, getpid(), "ORPHAN", "no passwd entry");
215 if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
216 /* crontab not accessible?
218 log_it(fname, getpid(), "CAN'T OPEN", tabname);
222 if (fstat(crontab_fd, statbuf) < OK) {
223 log_it(fname, getpid(), "FSTAT FAILED", tabname);
227 Debug(DLOAD, ("\t%s:", fname))
228 u = find_user(old_db, fname);
230 /* if crontab has not changed since we last read it
231 * in, then we can just use our existing entry.
233 if (u->mtime == statbuf->st_mtime) {
234 Debug(DLOAD, (" [no change, using old data]"))
235 unlink_user(old_db, u);
236 link_user(new_db, u);
240 /* before we fall through to the code that will reload
241 * the user, let's deallocate and unlink the user in
242 * the old database. This is more a point of memory
243 * efficiency than anything else, since all leftover
244 * users will be deleted from the old database when
245 * we finish with the crontab...
247 Debug(DLOAD, (" [delete old data]"))
248 unlink_user(old_db, u);
250 log_it(fname, getpid(), "RELOAD", tabname);
252 u = load_user(crontab_fd, pw, fname);
254 u->mtime = statbuf->st_mtime;
255 link_user(new_db, u);
259 if (crontab_fd >= OK) {
260 Debug(DLOAD, (" [done]\n"))