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
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.7 2008/01/07 14:11:23 matthias Exp $
21 /* vix 26jan87 [RCS has the log]
31 #define TMAX(a,b) ((a)>(b)?(a):(b))
34 static void process_crontab(char *, char *, char *,
36 cron_db
*, cron_db
*);
40 load_database(cron_db
*old_db
)
44 struct stat syscron_stat
;
49 Debug(DLOAD
, ("[%d] load_database()\n", getpid()))
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.
55 if (stat(SPOOL_DIR
, &statbuf
) < OK
) {
56 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR
);
60 /* track system crontab file
62 if (stat(SYSCRONTAB
, &syscron_stat
) < OK
)
63 syscron_stat
.st_mtime
= 0;
65 /* if spooldir's mtime has not changed, we don't need to fiddle with
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.
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",
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.
83 new_db
.mtime
= TMAX(statbuf
.st_mtime
, syscron_stat
.st_mtime
);
84 new_db
.head
= new_db
.tail
= NULL
;
86 if (syscron_stat
.st_mtime
) {
87 process_crontab("root", SYS_NAME
,
88 SYSCRONTAB
, &syscron_stat
,
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.
96 if (!(dir
= opendir(SPOOL_DIR
))) {
97 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR
);
101 while (NULL
!= (dp
= readdir(dir
))) {
102 char fname
[NAME_MAX
+ 1], tabname
[NAME_MAX
+ 1];
104 /* avoid file names beginning with ".". this is good
105 * because we would otherwise waste two guaranteed calls
106 * to getpwnam() for . and .., and also because user names
107 * starting with a period are just too nasty to consider.
109 if (dp
->d_name
[0] == '.')
112 strlcpy(fname
, dp
->d_name
, sizeof(fname
));
113 snprintf(tabname
, sizeof tabname
, CRON_TAB(fname
));
115 process_crontab(fname
, fname
, tabname
,
116 &statbuf
, &new_db
, old_db
);
120 /* if we don't do this, then when our children eventually call
121 * getpwnam() in do_command.c's child_process to verify MAILTO=,
122 * they will screw us up (and v-v).
126 /* whatever's left in the old database is now junk.
128 Debug(DLOAD
, ("unlinking old database:\n"))
129 for (u
= old_db
->head
; u
!= NULL
; u
= nu
) {
130 Debug(DLOAD
, ("\t%s\n", u
->name
))
132 unlink_user(old_db
, u
);
136 /* overwrite the database control block with the new one.
139 Debug(DLOAD
, ("load_database is done\n"))
144 link_user(cron_db
*db
, user
*u
)
146 if (db
->head
== NULL
)
157 unlink_user(cron_db
*db
, user
*u
)
162 u
->prev
->next
= u
->next
;
167 u
->next
->prev
= u
->prev
;
172 find_user(cron_db
*db
, char *name
)
177 for (u
= db
->head
; u
!= NULL
; u
= u
->next
)
178 if (!strcmp(u
->name
, name
))
185 process_crontab(char *uname
, char *fname
, char *tabname
, struct stat
*statbuf
,
186 cron_db
*new_db
, cron_db
*old_db
)
188 struct passwd
*pw
= NULL
;
189 int crontab_fd
= OK
- 1;
192 if (strcmp(fname
, SYS_NAME
) && !(pw
= getpwnam(uname
))) {
193 /* file doesn't have a user in passwd file.
195 log_it(fname
, getpid(), "ORPHAN", "no passwd entry");
199 if ((crontab_fd
= open(tabname
, O_RDONLY
, 0)) < OK
) {
200 /* crontab not accessible?
202 log_it(fname
, getpid(), "CAN'T OPEN", tabname
);
206 if (fstat(crontab_fd
, statbuf
) < OK
) {
207 log_it(fname
, getpid(), "FSTAT FAILED", tabname
);
211 Debug(DLOAD
, ("\t%s:", fname
))
212 u
= find_user(old_db
, fname
);
214 /* if crontab has not changed since we last read it
215 * in, then we can just use our existing entry.
217 if (u
->mtime
== statbuf
->st_mtime
) {
218 Debug(DLOAD
, (" [no change, using old data]"))
219 unlink_user(old_db
, u
);
220 link_user(new_db
, u
);
224 /* before we fall through to the code that will reload
225 * the user, let's deallocate and unlink the user in
226 * the old database. This is more a point of memory
227 * efficiency than anything else, since all leftover
228 * users will be deleted from the old database when
229 * we finish with the crontab...
231 Debug(DLOAD
, (" [delete old data]"))
232 unlink_user(old_db
, u
);
234 log_it(fname
, getpid(), "RELOAD", tabname
);
236 u
= load_user(crontab_fd
, pw
, fname
);
238 u
->mtime
= statbuf
->st_mtime
;
239 link_user(new_db
, u
);
243 if (crontab_fd
>= OK
) {
244 Debug(DLOAD
, (" [done]\n"))