]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/cron/cron/database.c
dts: Update our copy to be in sync with Linux 5.7
[FreeBSD/FreeBSD.git] / usr.sbin / cron / cron / database.c
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  */
17
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
20   "$FreeBSD$";
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
36 static  void            process_crontab(char *, char *, char *,
37                                              struct stat *,
38                                              cron_db *, cron_db *);
39
40
41 void
42 load_database(old_db)
43         cron_db         *old_db;
44 {
45         DIR             *dir;
46         struct stat     statbuf;
47         struct stat     syscron_stat, st;
48         time_t          maxmtime;
49         DIR_T           *dp;
50         cron_db         new_db;
51         user            *u, *nu;
52         struct {
53                 const char *name;
54                 struct stat st;
55         } syscrontabs [] = {
56                 { SYSCRONTABS },
57                 { LOCALSYSCRONTABS }
58         };
59         int i, ret;
60
61         Debug(DLOAD, ("[%d] load_database()\n", getpid()))
62
63         /* before we start loading any data, do a stat on SPOOL_DIR
64          * so that if anything changes as of this moment (i.e., before we've
65          * cached any of the database), we'll see the changes next time.
66          */
67         if (stat(SPOOL_DIR, &statbuf) < OK) {
68                 log_it("CRON", getpid(), "STAT FAILED", SPOOL_DIR);
69                 (void) exit(ERROR_EXIT);
70         }
71
72         /* track system crontab file
73          */
74         if (stat(SYSCRONTAB, &syscron_stat) < OK)
75                 syscron_stat.st_mtime = 0;
76
77         maxmtime = TMAX(statbuf.st_mtime, syscron_stat.st_mtime);
78
79         for (i = 0; i < nitems(syscrontabs); i++) {
80                 if (stat(syscrontabs[i].name, &syscrontabs[i].st) != -1) {
81                         maxmtime = TMAX(syscrontabs[i].st.st_mtime, maxmtime);
82                         /* Traverse into directory */
83                         if (!(dir = opendir(syscrontabs[i].name)))
84                                 continue;
85                         while (NULL != (dp = readdir(dir))) {
86                                 if (dp->d_name[0] == '.')
87                                         continue;
88                                 ret = fstatat(dirfd(dir), dp->d_name, &st, 0);
89                                 if (ret != 0 || !S_ISREG(st.st_mode))
90                                         continue;
91                                 maxmtime = TMAX(st.st_mtime, maxmtime);
92                         }
93                         closedir(dir);
94                 } else {
95                         syscrontabs[i].st.st_mtime = 0;
96                 }
97         }
98
99         /* if spooldir's mtime has not changed, we don't need to fiddle with
100          * the database.
101          *
102          * Note that old_db->mtime is initialized to 0 in main(), and
103          * so is guaranteed to be different than the stat() mtime the first
104          * time this function is called.
105          */
106         if (old_db->mtime == maxmtime) {
107                 Debug(DLOAD, ("[%d] spool dir mtime unch, no load needed.\n",
108                               getpid()))
109                 return;
110         }
111
112         /* something's different.  make a new database, moving unchanged
113          * elements from the old database, reloading elements that have
114          * actually changed.  Whatever is left in the old database when
115          * we're done is chaff -- crontabs that disappeared.
116          */
117         new_db.mtime = maxmtime;
118         new_db.head = new_db.tail = NULL;
119
120         if (syscron_stat.st_mtime) {
121                 process_crontab("root", SYS_NAME,
122                                 SYSCRONTAB, &syscron_stat,
123                                 &new_db, old_db);
124         }
125
126         for (i = 0; i < nitems(syscrontabs); i++) {
127                 char tabname[MAXPATHLEN];
128                 if (syscrontabs[i].st.st_mtime == 0)
129                         continue;
130                 if (!(dir = opendir(syscrontabs[i].name))) {
131                         log_it("CRON", getpid(), "OPENDIR FAILED",
132                             syscrontabs[i].name);
133                         (void) exit(ERROR_EXIT);
134                 }
135
136                 while (NULL != (dp = readdir(dir))) {
137                         if (dp->d_name[0] == '.')
138                                 continue;
139                         if (fstatat(dirfd(dir), dp->d_name, &st, 0) == 0 &&
140                             !S_ISREG(st.st_mode))
141                                 continue;
142                         snprintf(tabname, sizeof(tabname), "%s/%s",
143                             syscrontabs[i].name, dp->d_name);
144                         process_crontab("root", SYS_NAME, tabname,
145                             &syscrontabs[i].st, &new_db, old_db);
146                 }
147                 closedir(dir);
148         }
149
150         /* we used to keep this dir open all the time, for the sake of
151          * efficiency.  however, we need to close it in every fork, and
152          * we fork a lot more often than the mtime of the dir changes.
153          */
154         if (!(dir = opendir(SPOOL_DIR))) {
155                 log_it("CRON", getpid(), "OPENDIR FAILED", SPOOL_DIR);
156                 (void) exit(ERROR_EXIT);
157         }
158
159         while (NULL != (dp = readdir(dir))) {
160                 char    fname[MAXNAMLEN+1],
161                         tabname[MAXNAMLEN+1];
162
163                 /* avoid file names beginning with ".".  this is good
164                  * because we would otherwise waste two guaranteed calls
165                  * to getpwnam() for . and .., and also because user names
166                  * starting with a period are just too nasty to consider.
167                  */
168                 if (dp->d_name[0] == '.')
169                         continue;
170
171                 (void) strncpy(fname, dp->d_name, sizeof(fname));
172                 fname[sizeof(fname)-1] = '\0';
173                 (void) snprintf(tabname, sizeof tabname, CRON_TAB(fname));
174
175                 process_crontab(fname, fname, tabname,
176                                 &statbuf, &new_db, old_db);
177         }
178         closedir(dir);
179
180         /* if we don't do this, then when our children eventually call
181          * getpwnam() in do_command.c's child_process to verify MAILTO=,
182          * they will screw us up (and v-v).
183          */
184         endpwent();
185
186         /* whatever's left in the old database is now junk.
187          */
188         Debug(DLOAD, ("unlinking old database:\n"))
189         for (u = old_db->head;  u != NULL;  u = nu) {
190                 Debug(DLOAD, ("\t%s\n", u->name))
191                 nu = u->next;
192                 unlink_user(old_db, u);
193                 free_user(u);
194         }
195
196         /* overwrite the database control block with the new one.
197          */
198         *old_db = new_db;
199         Debug(DLOAD, ("load_database is done\n"))
200 }
201
202
203 void
204 link_user(db, u)
205         cron_db *db;
206         user    *u;
207 {
208         if (db->head == NULL)
209                 db->head = u;
210         if (db->tail)
211                 db->tail->next = u;
212         u->prev = db->tail;
213         u->next = NULL;
214         db->tail = u;
215 }
216
217
218 void
219 unlink_user(db, u)
220         cron_db *db;
221         user    *u;
222 {
223         if (u->prev == NULL)
224                 db->head = u->next;
225         else
226                 u->prev->next = u->next;
227
228         if (u->next == NULL)
229                 db->tail = u->prev;
230         else
231                 u->next->prev = u->prev;
232 }
233
234
235 user *
236 find_user(db, name)
237         cron_db *db;
238         char    *name;
239 {
240         char    *env_get();
241         user    *u;
242
243         for (u = db->head;  u != NULL;  u = u->next)
244                 if (!strcmp(u->name, name))
245                         break;
246         return u;
247 }
248
249
250 static void
251 process_crontab(uname, fname, tabname, statbuf, new_db, old_db)
252         char            *uname;
253         char            *fname;
254         char            *tabname;
255         struct stat     *statbuf;
256         cron_db         *new_db;
257         cron_db         *old_db;
258 {
259         struct passwd   *pw = NULL;
260         int             crontab_fd = OK - 1;
261         user            *u;
262         entry           *e;
263         time_t          now;
264
265         if (strcmp(fname, SYS_NAME) && !(pw = getpwnam(uname))) {
266                 /* file doesn't have a user in passwd file.
267                  */
268                 log_it(fname, getpid(), "ORPHAN", "no passwd entry");
269                 goto next_crontab;
270         }
271
272         if ((crontab_fd = open(tabname, O_RDONLY, 0)) < OK) {
273                 /* crontab not accessible?
274                  */
275                 log_it(fname, getpid(), "CAN'T OPEN", tabname);
276                 goto next_crontab;
277         }
278
279         if (fstat(crontab_fd, statbuf) < OK) {
280                 log_it(fname, getpid(), "FSTAT FAILED", tabname);
281                 goto next_crontab;
282         }
283
284         Debug(DLOAD, ("\t%s:", fname))
285         u = find_user(old_db, fname);
286         if (u != NULL) {
287                 /* if crontab has not changed since we last read it
288                  * in, then we can just use our existing entry.
289                  */
290                 if (u->mtime == statbuf->st_mtime) {
291                         Debug(DLOAD, (" [no change, using old data]"))
292                         unlink_user(old_db, u);
293                         link_user(new_db, u);
294                         goto next_crontab;
295                 }
296
297                 /* before we fall through to the code that will reload
298                  * the user, let's deallocate and unlink the user in
299                  * the old database.  This is more a point of memory
300                  * efficiency than anything else, since all leftover
301                  * users will be deleted from the old database when
302                  * we finish with the crontab...
303                  */
304                 Debug(DLOAD, (" [delete old data]"))
305                 unlink_user(old_db, u);
306                 free_user(u);
307                 log_it(fname, getpid(), "RELOAD", tabname);
308         }
309         u = load_user(crontab_fd, pw, fname);
310         if (u != NULL) {
311                 u->mtime = statbuf->st_mtime;
312                 /*
313                  * TargetTime == 0 when we're initially populating the database,
314                  * and TargetTime > 0 any time after that (i.e. we're reloading
315                  * cron.d/ files because they've been created/modified).  In the
316                  * latter case, we should check for any interval jobs and run
317                  * them 'n' seconds from the time the job was loaded/reloaded.
318                  * Otherwise, they will not be run until cron is restarted.
319                  */
320                 if (TargetTime != 0) {
321                         now = time(NULL);
322                         for (e = u->crontab; e != NULL; e = e->next) {
323                                 if ((e->flags & INTERVAL) != 0)
324                                         e->lastexit = now;
325                         }
326                 }
327                 link_user(new_db, u);
328         }
329
330 next_crontab:
331         if (crontab_fd >= OK) {
332                 Debug(DLOAD, (" [done]\n"))
333                 close(crontab_fd);
334         }
335 }