This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / cron / database.c
CommitLineData
693d8207
GR
1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
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.
12 *
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
16 * From Id: database.c,v 2.8 1994/01/15 20:43:43 vixie Exp
17 */
18
19#if !defined(lint) && !defined(LINT)
20static char rcsid[] = "$Header: $";
21#endif
22
23/* vix 26jan87 [RCS has the log]
24 */
25
26
27#include "cron.h"
28#include <fcntl.h>
29#include <sys/stat.h>
30#include <sys/file.h>
31
32
33#define TMAX(a,b) ((a)>(b)?(a):(b))
34
35
36static void process_crontab __P((char *, char *, char *,
37 struct stat *,
38 cron_db *, cron_db *));
39
40
41void
42load_database(old_db)
43 cron_db *old_db;
44{
45 DIR *dir;
46 struct stat statbuf;
47 struct stat syscron_stat;
48 DIR_T *dp;
49 cron_db new_db;
50 user *u, *nu;
51
52 Debug(DLOAD, ("[%d] load_database()\n", getpid()))
53
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.
57 */
58 if (stat(SPOOL_DIR, &statbuf) < OK) {
59 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR);
60 (void) exit(ERROR_EXIT);
61 }
62
63 /* track system crontab file
64 */
65 if (stat(SYSCRONTAB, &syscron_stat) < OK)
66 syscron_stat.st_mtime = 0;
67
68 /* if spooldir's mtime has not changed, we don't need to fiddle with
69 * the database.
70 *
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.
74 */
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",
77 getpid()))
78 return;
79 }
80
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.
85 */
86 new_db.mtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime);
87 new_db.head = new_db.tail = NULL;
88
89 if (syscron_stat.st_mtime) {
90 process_crontab("root", "*system*",
91 SYSCRONTAB, &syscron_stat,
92 &new_db, old_db);
93 }
94
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.
98 */
99 if (!(dir = opendir(SPOOL_DIR))) {
100 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR);
101 (void) exit(ERROR_EXIT);
102 }
103
104 while (NULL != (dp = readdir(dir))) {
105 char fname[MAXNAMLEN+1],
106 tabname[MAXNAMLEN+1];
107
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.
112 */
113 if (dp->d_name[0] == '.')
114 continue;
115
116 (void) strcpy(fname, dp->d_name);
117 sprintf(tabname, CRON_TAB(fname));
118
119 process_crontab(fname, fname, tabname,
120 &statbuf, &new_db, old_db);
121 }
122 closedir(dir);
123
124 /* if we don't do this, then when our children eventually call
125 * getpwnam() in do_command.c's child_process to verify MAILTO=,
126 * they will screw us up (and v-v).
127 */
128 endpwent();
129
130 /* whatever's left in the old database is now junk.
131 */
132 Debug(DLOAD, ("unlinking old database:\n"))
133 for (u = old_db->head; u != NULL; u = nu) {
134 Debug(DLOAD, ("\t%s\n", u->name))
135 nu = u->next;
136 unlink_user(old_db, u);
137 free_user(u);
138 }
139
140 /* overwrite the database control block with the new one.
141 */
142 *old_db = new_db;
143 Debug(DLOAD, ("load_database is done\n"))
144}
145
146
147void
148link_user(db, u)
149 cron_db *db;
150 user *u;
151{
152 if (db->head == NULL)
153 db->head = u;
154 if (db->tail)
155 db->tail->next = u;
156 u->prev = db->tail;
157 u->next = NULL;
158 db->tail = u;
159}
160
161
162void
163unlink_user(db, u)
164 cron_db *db;
165 user *u;
166{
167 if (u->prev == NULL)
168 db->head = u->next;
169 else
170 u->prev->next = u->next;
171
172 if (u->next == NULL)
173 db->tail = u->prev;
174 else
175 u->next->prev = u->prev;
176}
177
178
179user *
180find_user(db, name)
181 cron_db *db;
182 char *name;
183{
184 char *env_get();
185 user *u;
186
187 for (u = db->head; u != NULL; u = u->next)
188 if (!strcmp(u->name, name))
189 break;
190 return u;
191}
192
193
194static void
195process_crontab(uname, fname, tabname, statbuf, new_db, old_db)
196 char *uname;
197 char *fname;
198 char *tabname;
199 struct stat *statbuf;
200 cron_db *new_db;
201 cron_db *old_db;
202{
203 struct passwd *pw = NULL;
204 int crontab_fd = OK - 1;
205 user *u;
206
207 if (strcmp(fname, "*system*") && !(pw = getpwnam(uname))) {
208 /* file doesn't have a user in passwd file.
209 */
210 log_it(fname, getpid(), "ORPHAN", "no passwd entry");
211 goto next_crontab;
212 }
213
214 if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
215 /* crontab not accessible?
216 */
217 log_it(fname, getpid(), "CAN'T OPEN", tabname);
218 goto next_crontab;
219 }
220
221 if (fstat(crontab_fd, statbuf) < OK) {
222 log_it(fname, getpid(), "FSTAT FAILED", tabname);
223 goto next_crontab;
224 }
225
226 Debug(DLOAD, ("\t%s:", fname))
227 u = find_user(old_db, fname);
228 if (u != NULL) {
229 /* if crontab has not changed since we last read it
230 * in, then we can just use our existing entry.
231 */
232 if (u->mtime == statbuf->st_mtime) {
233 Debug(DLOAD, (" [no change, using old data]"))
234 unlink_user(old_db, u);
235 link_user(new_db, u);
236 goto next_crontab;
237 }
238
239 /* before we fall through to the code that will reload
240 * the user, let's deallocate and unlink the user in
241 * the old database. This is more a point of memory
242 * efficiency than anything else, since all leftover
243 * users will be deleted from the old database when
244 * we finish with the crontab...
245 */
246 Debug(DLOAD, (" [delete old data]"))
247 unlink_user(old_db, u);
248 free_user(u);
249 log_it(fname, getpid(), "RELOAD", tabname);
250 }
251 u = load_user(crontab_fd, pw, fname);
252 if (u != NULL) {
253 u->mtime = statbuf->st_mtime;
254 link_user(new_db, u);
255 }
256
257next_crontab:
258 if (crontab_fd >= OK) {
259 Debug(DLOAD, (" [done]\n"))
260 close(crontab_fd);
261 }
262}