Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / libexec / cron / database.c
blobfcec0c6885f3f42acd13a7d9d26eece2d544bb8f
1 /* Copyright 1988,1990,1993 by Paul Vixie
2 * All rights reserved
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
11 * user.
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 $";
20 #endif
22 /* vix 26jan87 [RCS has the log]
26 #include "cron.h"
27 #include "externs.h"
28 #include <pwd.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <sys/file.h>
34 #define TMAX(a,b) ((a)>(b)?(a):(b))
37 static void process_crontab __P((char *, char *, char *,
38 struct stat *,
39 cron_db *, cron_db *));
42 void
43 load_database(old_db)
44 cron_db *old_db;
46 DIR *dir;
47 struct stat statbuf;
48 struct stat syscron_stat;
49 DIR_T *dp;
50 cron_db new_db;
51 user *u, *nu;
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",
80 getpid()))
81 return;
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] == '.')
116 continue;
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);
124 closedir(dir);
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).
130 endpwent();
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)))
137 nu = u->next;
138 unlink_user(old_db, u);
139 free_user(u);
142 /* overwrite the database control block with the new one.
144 Debug(DLOAD, ("installing new database\n"))
145 *old_db = new_db;
146 Debug(DLOAD, ("load_database is done\n"))
150 void
151 link_user(db, u)
152 cron_db *db;
153 user *u;
155 if (db->head == NULL)
156 db->head = u;
157 if (db->tail)
158 db->tail->next = u;
159 u->prev = db->tail;
160 u->next = NULL;
161 db->tail = u;
165 void
166 unlink_user(db, u)
167 cron_db *db;
168 user *u;
170 if (u->prev == NULL)
171 db->head = u->next;
172 else
173 u->prev->next = u->next;
175 if (u->next == NULL)
176 db->tail = u->prev;
177 else
178 u->next->prev = u->prev;
182 user *
183 find_user(db, name)
184 cron_db *db;
185 char *name;
187 char *env_get();
188 user *u;
190 for (u = db->head; u != NULL; u = u->next)
191 if (!strcmp(env_get("LOGNAME", u->envp), name))
192 break;
193 return u;
197 static void
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 */
201 char *tabname;
202 struct stat *statbuf;
203 cron_db *new_db;
204 cron_db *old_db;
206 struct passwd *pw;
207 int crontab_fd = OK - 1;
208 user *u;
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");
214 goto next_crontab;
217 if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
218 /* crontab not accessible?
220 log_it(fname, getpid(), "CAN'T OPEN", tabname);
221 goto next_crontab;
224 if (fstat(crontab_fd, statbuf) < OK) {
225 log_it(fname, getpid(), "FSTAT FAILED", tabname);
226 goto next_crontab;
229 Debug(DLOAD, ("\t%s:", fname))
230 u = find_user(old_db, fname);
231 if (u != NULL) {
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);
241 goto next_crontab;
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);
253 free_user(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 */
257 if (u != NULL) {
258 u->mtime = statbuf->st_mtime;
259 link_user(new_db, u);
262 next_crontab:
263 if (crontab_fd >= OK) {
264 Debug(DLOAD, (" [done]\n"))
265 close(crontab_fd);