1 /* Copyright 1988,1990,1993 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 char rcsid
[] = "$Id: database.c,v 1.1 1994/01/05 20:40:14 jtc Exp $";
22 /* vix 26jan87 [RCS has the log]
34 #define TMAX(a,b) ((a)>(b)?(a):(b))
37 static void process_crontab
__P((char *, char *, char *,
39 cron_db
*, cron_db
*));
48 struct stat syscron_stat
;
53 Debug(DLOAD
, ("[%d] load_database()\n", getpid()))
55 /* before we start loading any data, do a stat on SPOOL_DIR
56 * so that if anything changes as of this moment (i.e., before we've
57 * cached any of the database), we'll see the changes next time.
59 if (stat(SPOOL_DIR
, &statbuf
) < OK
) {
60 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR
);
61 (void) exit(ERROR_EXIT
);
64 /* track system crontab file
66 if (stat(SYSCRONTAB
, &syscron_stat
) < OK
)
67 syscron_stat
.st_mtime
= 0;
69 /* if spooldir's mtime has not changed, we don't need to fiddle with
70 * the database. Note that if /etc/passwd changes (like, someone's
71 * UID/GID/HOME/SHELL, we won't see it. Maybe we should
72 * keep an mtime for the passwd file? HINT
74 * Note that old_db->mtime is initialized to 0 in main(), and
75 * so is guaranteed to be different than the stat() mtime the first
76 * time this function is called.
78 if (old_db
->mtime
== TMAX(statbuf
.st_mtime
, syscron_stat
.st_mtime
)) {
79 Debug(DLOAD
, ("[%d] spool dir mtime unch, no load needed.\n",
84 /* we used to keep this dir open all the time, for the sake of
85 * efficiency. however, we need to close it in every fork, and
86 * we fork a lot more often than the mtime of the dir changes.
88 if (!(dir
= opendir(SPOOL_DIR
))) {
89 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR
);
90 (void) exit(ERROR_EXIT
);
93 /* something's different. make a new database, moving unchanged
94 * elements from the old database, reloading elements that have
95 * actually changed. Whatever is left in the old database when
96 * we're done is chaff -- crontabs that disappeared.
98 new_db
.mtime
= TMAX(statbuf
.st_mtime
, syscron_stat
.st_mtime
);
99 new_db
.head
= new_db
.tail
= NULL
;
101 if (syscron_stat
.st_mtime
) {
102 process_crontab("root", "*root*", SYSCRONTAB
,
103 &syscron_stat
, &new_db
, old_db
);
106 while (NULL
!= (dp
= readdir(dir
))) {
107 char fname
[MAXNAMLEN
+1],
108 tabname
[MAXNAMLEN
+1];
110 /* avoid file names beginning with ".". this is good
111 * because we would otherwise waste two guaranteed calls
112 * to getpwnam() for . and .., and also because user names
113 * starting with a period are just too nasty to consider.
115 if (dp
->d_name
[0] == '.')
118 (void) strcpy(fname
, dp
->d_name
);
119 sprintf(tabname
, CRON_TAB(fname
));
121 process_crontab(fname
, fname
, tabname
,
122 &statbuf
, &new_db
, old_db
);
126 /* if we don't do this, then when our children eventually call
127 * getpwnam() in do_command.c's child_process to verify MAILTO=,
128 * they will screw us up (and v-v).
132 /* whatever's left in the old database is now junk.
134 Debug(DLOAD
, ("unlinking old database:\n"))
135 for (u
= old_db
->head
; u
!= NULL
; u
= nu
) {
136 Debug(DLOAD
, ("\t%s\n", env_get("LOGNAME", u
->envp
)))
138 unlink_user(old_db
, u
);
142 /* overwrite the database control block with the new one.
144 Debug(DLOAD
, ("installing new database\n"))
146 Debug(DLOAD
, ("load_database is done\n"))
155 if (db
->head
== NULL
)
173 u
->prev
->next
= u
->next
;
178 u
->next
->prev
= u
->prev
;
190 for (u
= db
->head
; u
!= NULL
; u
= u
->next
)
191 if (!strcmp(env_get("LOGNAME", u
->envp
), name
))
198 process_crontab(uname
, fname
, tabname
, statbuf
, new_db
, old_db
)
199 char *uname
; /* these are the same except for */
200 char *fname
; /* *root* processing /etc/crontab */
202 struct stat
*statbuf
;
207 int crontab_fd
= OK
- 1;
210 if (NULL
== (pw
= getpwnam(uname
))) {
211 /* file doesn't have a user in passwd file.
213 log_it(fname
, getpid(), "ORPHAN", "no passwd entry");
217 if ((crontab_fd
= open(tabname
, O_RDONLY
, 0)) < OK
) {
218 /* crontab not accessible?
220 log_it(fname
, getpid(), "CAN'T OPEN", tabname
);
224 if (fstat(crontab_fd
, statbuf
) < OK
) {
225 log_it(fname
, getpid(), "FSTAT FAILED", tabname
);
229 Debug(DLOAD
, ("\t%s:", fname
))
230 u
= find_user(old_db
, fname
);
232 /* if crontab has not changed since we last read it
233 * in, then we can just use our existing entry.
234 * XXX - note that we do not check for changes in the
235 * passwd entry (uid, home dir, etc).
237 if (u
->mtime
== statbuf
->st_mtime
) {
238 Debug(DLOAD
, (" [no change, using old data]"))
239 unlink_user(old_db
, u
);
240 link_user(new_db
, u
);
244 /* before we fall through to the code that will reload
245 * the user, let's deallocate and unlink the user in
246 * the old database. This is more a point of memory
247 * efficiency than anything else, since all leftover
248 * users will be deleted from the old database when
249 * we finish with the crontab...
251 Debug(DLOAD
, (" [delete old data]"))
252 unlink_user(old_db
, u
);
255 u
= load_user(crontab_fd
, pw
->pw_name
, pw
->pw_uid
, pw
->pw_gid
,
256 pw
->pw_dir
, (!strcmp(fname
, "*root*"))); /* XXX cookie */
258 u
->mtime
= statbuf
->st_mtime
;
259 link_user(new_db
, u
);
263 if (crontab_fd
>= OK
) {
264 Debug(DLOAD
, (" [done]\n"))