1 /* $NetBSD: database.c,v 1.6 2002/11/16 04:34:13 itojun Exp $ */
3 /* Copyright 1988,1990,1993,1994 by Paul Vixie
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
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)
23 static char rcsid
[] = "Id: database.c,v 2.8 1994/01/15 20:43:43 vixie Exp";
25 __RCSID("$NetBSD: database.c,v 1.6 2002/11/16 04:34:13 itojun Exp $");
29 /* vix 26jan87 [RCS has the log]
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
*);
48 load_database(cron_db
*old_db
)
52 struct stat syscron_stat
;
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
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",
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
,
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] == '.')
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
);
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).
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
))
140 unlink_user(old_db
, u
);
144 /* overwrite the database control block with the new one.
147 Debug(DLOAD
, ("load_database is done\n"))
152 link_user(cron_db
*db
, user
*u
)
154 if (db
->head
== NULL
)
165 unlink_user(cron_db
*db
, user
*u
)
170 u
->prev
->next
= u
->next
;
175 u
->next
->prev
= u
->prev
;
180 find_user(cron_db
*db
, const char *name
)
184 for (u
= db
->head
; u
!= NULL
; u
= u
->next
)
185 if (!strcmp(u
->name
, name
))
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;
200 /* must be set to something.
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");
210 if ((crontab_fd
= open(tabname
, O_RDONLY
, 0)) < OK
) {
211 /* crontab not accessible?
213 log_it(fname
, getpid(), "CAN'T OPEN", tabname
);
217 if (fstat(crontab_fd
, statbuf
) < OK
) {
218 log_it(fname
, getpid(), "FSTAT FAILED", tabname
);
222 Debug(DLOAD
, ("\t%s:", fname
))
223 u
= find_user(old_db
, fname
);
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
);
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
);
245 log_it(fname
, getpid(), "RELOAD", tabname
);
247 u
= load_user(crontab_fd
, pw
, fname
);
249 u
->mtime
= statbuf
->st_mtime
;
250 link_user(new_db
, u
);
254 if (crontab_fd
>= OK
) {
255 Debug(DLOAD
, (" [done]\n"))