xref: /lighttpd1.4/src/stat_cache.c (revision b598bb53)
1 #include "first.h"
2 
3 #include "stat_cache.h"
4 #include "log.h"
5 #include "fdevent.h"
6 #include "etag.h"
7 #include "algo_splaytree.h"
8 
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 
18 #if defined(HAVE_SYS_XATTR_H)
19 # include <sys/xattr.h>
20 #elif defined(HAVE_ATTR_ATTRIBUTES_H)
21 # include <attr/attributes.h>
22 #endif
23 
24 #ifdef HAVE_SYS_EXTATTR_H
25 # include <sys/extattr.h>
26 #endif
27 
28 #ifndef HAVE_LSTAT
29 #define lstat stat
30 #ifndef S_ISLNK
31 #define S_ISLNK(mode) (0)
32 #endif
33 #endif
34 
35 /*
36  * stat-cache
37  *
38  * - a splay-tree is used as we can use the caching effect of it
39  */
40 
41 enum {
42   STAT_CACHE_ENGINE_SIMPLE  = 0  /*(default)*/
43  ,STAT_CACHE_ENGINE_NONE    = 1
44  ,STAT_CACHE_ENGINE_FAM     = 2  /* same as STAT_CACHE_ENGINE_INOTIFY */
45  ,STAT_CACHE_ENGINE_INOTIFY = 2  /* same as STAT_CACHE_ENGINE_FAM */
46  ,STAT_CACHE_ENGINE_KQUEUE  = 2  /* same as STAT_CACHE_ENGINE_FAM */
47 };
48 
49 struct stat_cache_fam;  /* declaration */
50 
51 typedef struct stat_cache {
52 	int stat_cache_engine;
53 	splay_tree *files; /* nodes of tree are (stat_cache_entry *) */
54 	struct stat_cache_fam *scf;
55 } stat_cache;
56 
57 static stat_cache sc;
58 
59 
60 static void * stat_cache_sptree_find(splay_tree ** const sptree,
61                                      const char * const name,
62                                      uint32_t len)
63 {
64     const int ndx = splaytree_djbhash(name, len);
65     *sptree = splaytree_splay(*sptree, ndx);
66     return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
67 }
68 
69 
70 #if defined(HAVE_SYS_INOTIFY_H) \
71  || (defined(HAVE_SYS_EVENT_H) && defined(HAVE_KQUEUE))
72 #ifndef HAVE_FAM_H
73 #define HAVE_FAM_H
74 #endif
75 #endif
76 
77 #ifdef HAVE_FAM_H
78 
79 /* monitor changes in directories using FAM
80  *
81  * This implementation employing FAM monitors directories as they are used,
82  * and maintains a reference count for cache use within stat_cache.c.
83  * A periodic job runs in lighttpd every 32 seconds, expiring entries unused
84  * in last 64 seconds out of the cache and cancelling FAM monitoring.  Items
85  * within the cache are checked against the filesystem upon use if last stat()
86  * was greater than or equal to 16 seconds ago.
87  *
88  * This implementation does not monitor every directory in a tree, and therefore
89  * the cache may get out-of-sync with the filesystem.  Delays in receiving and
90  * processing events from FAM might also lead to stale cache entries.
91  *
92  * For many websites, a large number of files are seldom, if ever, modified,
93  * and a common practice with images is to create a new file with a new name
94  * when a new version is needed, in order for client browsers and CDNs to better
95  * cache the content.  Given this, most use will see little difference in
96  * performance between server.stat-cache-engine = "fam" and "simple" (default).
97  * The default server.stat-cache-engine = "simple" calls stat() on a target once
98  * per second, and reuses that information until the next second.  For use where
99  * changes must be immediately visible, server.stat-cache-engine = "disable"
100  * should be used.
101  *
102  * When considering use of server.stat-cache-engine = "fam", there are a few
103  * additional limitations for this cache implementation using FAM.
104  * - symlinks to files located outside of the current directory do not result
105  *   in changes to that file being monitored (unless that file is in a directory
106  *   which is monitored as a result of a different request).  symlinks can be
107  *   chained and can be circular.  This implementation *does not* readlink() or
108  *   realpath() to resolve the chains to find and monitor the ultimate target
109  *   directory.  While symlinks to files located outside the current directory
110  *   are not monitored, symlinks to directories *are* monitored, though chains
111  *   of symlinks to directories do not result in monitoring of the directories
112  *   containing intermediate symlinks to the target directory.
113  * - directory rename of a directory which is not currently being monitored will
114  *   result in stale information in the cache if there is a subdirectory that is
115  *   being monitored.
116  * Even though lighttpd will not receive FAM events in the above cases, lighttpd
117  * does re-validate the information in the cache upon use if the cache entry has
118  * not been checked in 16 seconds, so that is the upper limit for use of stale
119  * data.
120  *
121  * Use of server.stat-cache-engine = "fam" is discouraged for extremely volatile
122  * directories such as temporary directories (e.g. /tmp and maybe /var/tmp) due
123  * to the overhead of processing the additional noise generated from changes.
124  * Related, server.stat-cache-engine = "fam" is not recommended on trees of
125  * untrusted files where a malicious user could generate an excess of change
126  * events.
127  *
128  * Internal note: lighttpd walks the caches to prune trees in stat_cache when an
129  * event is received for a directory (or symlink to a directory) which has been
130  * deleted or renamed.  The splaytree data structure is suboptimal for frequent
131  * changes of large directories trees where there have been a large number of
132  * different files recently accessed and part of the stat_cache.
133  */
134 
135 #if defined(HAVE_SYS_INOTIFY_H) \
136  && !(defined(HAVE_SYS_EVENT_H) && defined(HAVE_KQUEUE))
137 
138 #include <sys/inotify.h>
139 
140 /*(translate FAM API to inotify; this is specific to stat_cache.c use of FAM)*/
141 #define fam fd /*(translate struct stat_cache_fam scf->fam -> scf->fd)*/
142 typedef int FAMRequest; /*(fr)*/
143 #define FAMClose(fd) \
144         close(*(fd))
145 #define FAMCancelMonitor(fd, wd) \
146         inotify_rm_watch(*(fd), *(wd))
147 #define fam_watch_mask ( IN_ATTRIB | IN_CREATE | IN_DELETE | IN_DELETE_SELF \
148                        | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM \
149                        | IN_EXCL_UNLINK | IN_ONLYDIR )
150                      /*(note: follows symlinks; not providing IN_DONT_FOLLOW)*/
151 #define FAMMonitorDirectory(fd, fn, wd, userData) \
152         ((*(wd) = inotify_add_watch(*(fd), (fn), (fam_watch_mask))) < 0)
153 typedef enum FAMCodes { /*(copied from fam.h to define arbitrary enum values)*/
154     FAMChanged=1,
155     FAMDeleted=2,
156     FAMCreated=5,
157     FAMMoved=6,
158 } FAMCodes;
159 
160 #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
161 #undef HAVE_SYS_INOTIFY_H
162 
163 #include <sys/event.h>
164 #include <sys/time.h>
165 
166 /*(translate FAM API to inotify; this is specific to stat_cache.c use of FAM)*/
167 #define fam fd /*(translate struct stat_cache_fam scf->fam -> scf->fd)*/
168 typedef int FAMRequest; /*(fr)*/
169 #define FAMClose(fd) \
170         (-1 != (*(fd)) ? close(*(fd)) : 0)
171 static int FAMCancelMonitor (const int * const fd, int * const wd)
172 {
173     if (-1 == *fd) return 0;
174     if (-1 == *wd) return 0;
175     struct timespec t0 = { 0, 0 };
176     struct kevent kev;
177     EV_SET(&kev, *wd, EVFILT_VNODE, EV_DELETE, 0, 0, 0);
178     int rc = kevent(*fd, &kev, 1, NULL, 0, &t0);
179     close(*wd);
180     *wd = -1;
181     return rc;
182 }
183 static int FAMMonitorDirectory (int * const fd, char * const fn, int * const wd, void * const userData)
184 {
185     *wd = fdevent_open_dirname(fn, 1); /*(note: follows symlinks)*/
186     if (-1 == *wd) return -1;
187     struct timespec t0 = { 0, 0 };
188     struct kevent kev;
189     unsigned short kev_flags = EV_ADD | EV_ENABLE | EV_CLEAR;
190     unsigned int kev_fflags = NOTE_ATTRIB | NOTE_EXTEND | NOTE_LINK | NOTE_WRITE
191                             | NOTE_DELETE | NOTE_REVOKE | NOTE_RENAME;
192     EV_SET(&kev, *wd, EVFILT_VNODE, kev_flags, kev_fflags, 0, userData);
193     return kevent(*fd, &kev, 1, NULL, 0, &t0);
194 }
195 typedef enum FAMCodes { /*(copied from fam.h to define arbitrary enum values)*/
196     FAMChanged=1,
197     FAMDeleted=2,
198     FAMCreated=5,
199     FAMMoved=6,
200 } FAMCodes;
201 
202 #else
203 
204 #include <fam.h>
205 
206 #ifdef HAVE_FAMNOEXISTS
207 #ifndef LIGHTTPD_STATIC
208 #include <dlfcn.h>
209 #endif
210 #endif
211 
212 #endif
213 
214 typedef struct fam_dir_entry {
215 	buffer *name;
216 	int refcnt;
217 	FAMRequest req;
218 	time_t stat_ts;
219 	dev_t st_dev;
220 	ino_t st_ino;
221 	struct fam_dir_entry *fam_parent;
222 } fam_dir_entry;
223 
224 typedef struct stat_cache_fam {
225 	splay_tree *dirs; /* indexed by path; node data is fam_dir_entry */
226   #ifdef HAVE_SYS_INOTIFY_H
227 	splay_tree *wds;  /* indexed by inotify watch descriptor */
228   #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
229   #else
230 	FAMConnection fam;
231   #endif
232 	log_error_st *errh;
233 	fdevents *ev;
234 	fdnode *fdn;
235 	int fd;
236 } stat_cache_fam;
237 
238 static fam_dir_entry * fam_dir_entry_init(const char *name, size_t len)
239 {
240     fam_dir_entry * const fam_dir = calloc(1, sizeof(*fam_dir));
241     force_assert(NULL != fam_dir);
242 
243     fam_dir->name = buffer_init();
244     buffer_copy_string_len(fam_dir->name, name, len);
245     fam_dir->refcnt = 0;
246   #if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
247     fam_dir->req = -1;
248   #endif
249 
250     return fam_dir;
251 }
252 
253 static void fam_dir_entry_free(fam_dir_entry *fam_dir)
254 {
255     if (!fam_dir) return;
256     /*(fam_dir->parent might be invalid pointer here; ignore)*/
257     buffer_free(fam_dir->name);
258   #if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
259     if (-1 != fam_dir->req)
260         close(fam_dir->req);
261   #endif
262     free(fam_dir);
263 }
264 
265 static void fam_dir_invalidate_node(fam_dir_entry *fam_dir)
266 {
267     fam_dir->stat_ts = 0;
268     if (fam_dir->fam_parent) {
269         --fam_dir->fam_parent->refcnt;
270         fam_dir->fam_parent = NULL;
271     }
272 }
273 
274 /*
275  * walk though splay_tree and collect contents of dir tree.
276  * remove tagged entries in a second loop
277  */
278 
279 static void fam_dir_tag_refcnt(splay_tree *t, int *keys, int *ndx)
280 {
281     if (*ndx == 512) return; /*(must match num array entries in keys[])*/
282     if (t->left)  fam_dir_tag_refcnt(t->left,  keys, ndx);
283     if (t->right) fam_dir_tag_refcnt(t->right, keys, ndx);
284     if (*ndx == 512) return; /*(must match num array entries in keys[])*/
285 
286     fam_dir_entry * const fam_dir = t->data;
287     if (0 == fam_dir->refcnt) {
288         fam_dir_invalidate_node(fam_dir);
289         keys[(*ndx)++] = t->key;
290     }
291 }
292 
293 __attribute_noinline__
294 static void fam_dir_periodic_cleanup() {
295     stat_cache_fam * const scf = sc.scf;
296     int max_ndx, i;
297     int keys[512]; /* 2k size on stack */
298   #if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
299     struct kevent kevl[512]; /* 32k size on stack to batch kevent EV_DELETE */
300   #endif
301     do {
302         if (!scf->dirs) break;
303         max_ndx = 0;
304         fam_dir_tag_refcnt(scf->dirs, keys, &max_ndx);
305         for (i = 0; i < max_ndx; ++i) {
306             const int ndx = keys[i];
307             splay_tree *node = scf->dirs = splaytree_splay(scf->dirs, ndx);
308             if (node && node->key == ndx) {
309                 fam_dir_entry *fam_dir = node->data;
310                 scf->dirs = splaytree_delete(scf->dirs, ndx);
311               #ifdef HAVE_SYS_INOTIFY_H
312                 scf->wds = splaytree_delete(scf->wds, fam_dir->req);
313               #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
314                 /* batch process kevent removal; defer cancel */
315                 EV_SET(kevl+i, fam_dir->req, EVFILT_VNODE, EV_DELETE, 0, 0, 0);
316                 fam_dir->req = -1; /*(make FAMCancelMonitor() a no-op)*/
317               #endif
318                 FAMCancelMonitor(&scf->fam, &fam_dir->req);
319                 fam_dir_entry_free(fam_dir);
320             }
321         }
322       #if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
323         /* batch process: kevent() to submit EV_DELETE, then close dir fds */
324         if (0 == max_ndx) break;
325         struct timespec t0 = { 0, 0 };
326         kevent(scf->fd, kevl, max_ndx, NULL, 0, &t0);
327         for (i = 0; i < max_ndx; ++i)
328             close((int)kevl[i].ident);
329       #endif
330     } while (max_ndx == sizeof(keys)/sizeof(int));
331 }
332 
333 static void fam_dir_invalidate_tree(splay_tree *t, const char *name, size_t len)
334 {
335   #ifdef __clang_analyzer__
336     force_assert(name);
337   #endif
338     /*force_assert(t);*/
339     if (t->left)  fam_dir_invalidate_tree(t->left,  name, len);
340     if (t->right) fam_dir_invalidate_tree(t->right, name, len);
341 
342     fam_dir_entry * const fam_dir = t->data;
343   #ifdef __clang_analyzer__
344     force_assert(fam_dir);
345   #endif
346     buffer *b = fam_dir->name;
347     size_t blen = buffer_string_length(b);
348     if (blen > len && b->ptr[len] == '/' && 0 == memcmp(b->ptr, name, len))
349         fam_dir_invalidate_node(fam_dir);
350 }
351 
352 /* declarations */
353 static void stat_cache_delete_tree(const char *name, uint32_t len);
354 static void stat_cache_invalidate_dir_tree(const char *name, size_t len);
355 static void stat_cache_handle_fdevent_fn(stat_cache_fam * const scf, fam_dir_entry * const fam_dir, const char * const fn, const uint32_t fnlen, int code);
356 
357 static void stat_cache_handle_fdevent_in(stat_cache_fam *scf)
358 {
359   #ifdef HAVE_SYS_INOTIFY_H
360     /*(inotify pads in->len to align struct following in->name[])*/
361     char buf[4096]
362       __attribute__ ((__aligned__(__alignof__(struct inotify_event))));
363     int rd;
364     do {
365         rd = (int)read(scf->fd, buf, sizeof(buf));
366         if (rd <= 0) {
367             if (-1 == rd && errno != EINTR && errno != EAGAIN) {
368                 log_perror(scf->errh, __FILE__, __LINE__, "inotify error");
369                 /* TODO: could flush cache, close scf->fd, and re-open inotify*/
370             }
371             break;
372         }
373         for (int i = 0; i < rd; ) {
374             struct inotify_event * const in =
375               (struct inotify_event *)((uintptr_t)buf + i);
376             uint32_t len = in->len;
377             if (len > sizeof(buf)) break; /*(should not happen)*/
378             i += sizeof(struct inotify_event) + len;
379             if (i > rd) break; /*(should not happen (partial record))*/
380             if (in->mask & IN_CREATE)
381                 continue; /*(see comment below for FAMCreated)*/
382             if (in->mask & IN_Q_OVERFLOW) {
383                 log_error(scf->errh, __FILE__, __LINE__,
384                           "inotify queue overflow");
385                 continue;
386             }
387             /* ignore events which may have been pending for
388              * paths recently cancelled via FAMCancelMonitor() */
389             scf->wds = splaytree_splay(scf->wds, in->wd);
390             if (!scf->wds || scf->wds->key != in->wd)
391                 continue;
392             fam_dir_entry *fam_dir = scf->wds->data;
393             if (NULL == fam_dir)        /*(should not happen)*/
394                 continue;
395             if (fam_dir->req != in->wd) /*(should not happen)*/
396                 continue;
397             /*(specific to use here in stat_cache.c)*/
398             int code = 0;
399             if (in->mask & (IN_ATTRIB | IN_MODIFY))
400                 code = FAMChanged;
401             else if (in->mask & (IN_DELETE | IN_DELETE_SELF | IN_UNMOUNT))
402                 code = FAMDeleted;
403             else if (in->mask & (IN_MOVE_SELF | IN_MOVED_FROM))
404                 code = FAMMoved;
405 
406             if (len) {
407                 do { --len; } while (len && in->name[len-1] == '\0');
408             }
409             stat_cache_handle_fdevent_fn(scf, fam_dir, in->name, len, code);
410         }
411     } while (rd + sizeof(struct inotify_event) + NAME_MAX + 1 > sizeof(buf));
412   #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
413     struct kevent kevl[256];
414     struct timespec t0 = { 0, 0 };
415     int n;
416     do {
417         n = kevent(scf->fd, NULL, 0, kevl, sizeof(kevl)/sizeof(*kevl), &t0);
418         if (n <= 0) break;
419         for (int i = 0; i < n; ++i) {
420             const struct kevent * const kev = kevl+i;
421             /* ignore events which may have been pending for
422              * paths recently cancelled via FAMCancelMonitor() */
423             int ndx = (int)(intptr_t)kev->udata;
424             scf->dirs = splaytree_splay(scf->dirs, ndx);
425             if (!scf->dirs || scf->dirs->key != ndx)
426                 continue;
427             fam_dir_entry *fam_dir = scf->dirs->data;
428             if (fam_dir->req != (int)kev->ident)
429                 continue;
430             /*(specific to use here in stat_cache.c)*/
431             /* note: stat_cache only monitors on directories,
432              *       so events here are only on directories
433              * note: changes are treated as FAMDeleted since
434              *       it is unknown which file in dir was changed
435              *       This is not efficient, but this stat_cache mechanism also
436              *       should not be used on frequently modified directories. */
437             int code = 0;
438             if (kev->fflags & (NOTE_WRITE|NOTE_ATTRIB|NOTE_EXTEND|NOTE_LINK))
439                 code = FAMDeleted; /*(not FAMChanged; see comment above)*/
440             else if (kev->fflags & (NOTE_DELETE|NOTE_REVOKE))
441                 code = FAMDeleted;
442             else if (kev->fflags & NOTE_RENAME)
443                 code = FAMMoved;
444             if (kev->flags & EV_ERROR) /*(not expected; treat as FAMDeleted)*/
445                 code = FAMDeleted;
446             stat_cache_handle_fdevent_fn(scf, fam_dir, NULL, 0, code);
447         }
448     } while (n == sizeof(kevl)/sizeof(*kevl));
449   #else
450     for (int i = 0, ndx; i || (i = FAMPending(&scf->fam)) > 0; --i) {
451         FAMEvent fe;
452         if (FAMNextEvent(&scf->fam, &fe) < 0) break;
453 
454         /* ignore events which may have been pending for
455          * paths recently cancelled via FAMCancelMonitor() */
456         ndx = (int)(intptr_t)fe.userdata;
457         scf->dirs = splaytree_splay(scf->dirs, ndx);
458         if (!scf->dirs || scf->dirs->key != ndx) {
459             continue;
460         }
461         fam_dir_entry *fam_dir = scf->dirs->data;
462         if (FAMREQUEST_GETREQNUM(&fam_dir->req)
463             != FAMREQUEST_GETREQNUM(&fe.fr)) {
464             continue;
465         }
466 
467         uint32_t fnlen = (fe.code != FAMCreated && fe.filename[0] != '/')
468           ? (uint32_t)strlen(fe.filename)
469           : 0;
470         stat_cache_handle_fdevent_fn(scf, fam_dir, fe.filename, fnlen, fe.code);
471     }
472   #endif
473 }
474 
475 static void stat_cache_handle_fdevent_fn(stat_cache_fam * const scf, fam_dir_entry *fam_dir, const char * const fn, const uint32_t fnlen, int code)
476 {
477         if (fnlen) {
478             buffer * const n = fam_dir->name;
479             fam_dir_entry *fam_link;
480             uint32_t len;
481             switch (code) {
482             case FAMCreated:
483                 /* file created in monitored dir modifies dir and
484                  * we should get a separate FAMChanged event for dir.
485                  * Therefore, ignore file FAMCreated event here.
486                  * Also, if FAMNoExists() is used, might get spurious
487                  * FAMCreated events as changes are made e.g. in monitored
488                  * sub-sub-sub dirs and the library discovers new (already
489                  * existing) dir entries */
490                 return;
491             case FAMChanged:
492                 /* file changed in monitored dir does not modify dir */
493             case FAMDeleted:
494             case FAMMoved:
495                 /* file deleted or moved in monitored dir modifies dir,
496                  * but FAM provides separate notification for that */
497 
498                 /* temporarily append filename to dir in fam_dir->name to
499                  * construct path, then delete stat_cache entry (if any)*/
500                 len = buffer_string_length(n);
501                 buffer_append_string_len(n, CONST_STR_LEN("/"));
502                 buffer_append_string_len(n, fn, fnlen);
503                 /* (alternatively, could chose to stat() and update)*/
504                 stat_cache_invalidate_entry(CONST_BUF_LEN(n));
505 
506                 fam_link = /*(check if might be symlink to monitored dir)*/
507                   stat_cache_sptree_find(&scf->dirs, CONST_BUF_LEN(n));
508                 if (fam_link && !buffer_is_equal(fam_link->name, n))
509                     fam_link = NULL;
510 
511                 buffer_string_set_length(n, len);
512 
513                 if (fam_link) {
514                     /* replaced symlink changes containing dir */
515                     stat_cache_invalidate_entry(CONST_BUF_LEN(n));
516                     /* handle symlink to dir as deleted dir below */
517                     code = FAMDeleted;
518                     fam_dir = fam_link;
519                     break;
520                 }
521                 return;
522             default:
523                 return;
524             }
525         }
526 
527         switch(code) {
528         case FAMChanged:
529             stat_cache_invalidate_entry(CONST_BUF_LEN(fam_dir->name));
530             break;
531         case FAMDeleted:
532         case FAMMoved:
533             stat_cache_delete_tree(CONST_BUF_LEN(fam_dir->name));
534             fam_dir_invalidate_node(fam_dir);
535             if (scf->dirs)
536                 fam_dir_invalidate_tree(scf->dirs,CONST_BUF_LEN(fam_dir->name));
537             fam_dir_periodic_cleanup();
538             break;
539         default:
540             break;
541         }
542 }
543 
544 static handler_t stat_cache_handle_fdevent(void *ctx, int revent)
545 {
546 	stat_cache_fam * const scf = ctx; /* sc.scf */
547 
548 	if (revent & FDEVENT_IN) {
549 		stat_cache_handle_fdevent_in(scf);
550 	}
551 
552 	if (revent & (FDEVENT_HUP|FDEVENT_RDHUP)) {
553 		/* fam closed the connection */
554 		log_error(scf->errh, __FILE__, __LINE__,
555 		  "FAM connection closed; disabling stat_cache.");
556 		/* (although effectively STAT_CACHE_ENGINE_NONE,
557 		 *  do not change here so that periodic jobs clean up memory)*/
558 		/*sc.stat_cache_engine = STAT_CACHE_ENGINE_NONE; */
559 		fdevent_fdnode_event_del(scf->ev, scf->fdn);
560 		fdevent_unregister(scf->ev, scf->fd);
561 		scf->fdn = NULL;
562 
563 		FAMClose(&scf->fam);
564 		scf->fd = -1;
565 	}
566 
567 	return HANDLER_GO_ON;
568 }
569 
570 static stat_cache_fam * stat_cache_init_fam(fdevents *ev, log_error_st *errh) {
571 	stat_cache_fam *scf = calloc(1, sizeof(*scf));
572 	force_assert(scf);
573 	scf->fd = -1;
574 	scf->ev = ev;
575 	scf->errh = errh;
576 
577   #ifdef HAVE_SYS_INOTIFY_H
578 	scf->fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
579 	if (scf->fd < 0) {
580 		log_perror(errh, __FILE__, __LINE__, "inotify_init1()");
581 		free(scf);
582 		return NULL;
583 	}
584   #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
585    #ifdef __NetBSD__
586 	scf->fd = kqueue1(O_NONBLOCK|O_CLOEXEC|O_NOSIGPIPE);
587    #else
588 	scf->fd = kqueue();
589 	if (scf->fd >= 0) fdevent_setfd_cloexec(scf->fd);
590    #endif
591 	if (scf->fd < 0) {
592 		log_perror(errh, __FILE__, __LINE__, "kqueue()");
593 		free(scf);
594 		return NULL;
595 	}
596   #else
597 	/* setup FAM */
598 	if (0 != FAMOpen2(&scf->fam, "lighttpd")) {
599 		log_error(errh, __FILE__, __LINE__,
600 		  "could not open a fam connection, dying.");
601 		free(scf);
602 		return NULL;
603 	}
604       #ifdef HAVE_FAMNOEXISTS
605       #ifdef LIGHTTPD_STATIC
606 	FAMNoExists(&scf->fam);
607       #else
608 	int (*FAMNoExists_fn)(FAMConnection *);
609 	FAMNoExists_fn =
610 	  (int (*)(FAMConnection *))(intptr_t)dlsym(RTLD_DEFAULT,"FAMNoExists");
611 	if (FAMNoExists_fn) FAMNoExists_fn(&scf->fam);
612       #endif
613       #endif
614 
615 	scf->fd = FAMCONNECTION_GETFD(&scf->fam);
616 	fdevent_setfd_cloexec(scf->fd);
617   #endif
618 	scf->fdn = fdevent_register(scf->ev, scf->fd, stat_cache_handle_fdevent, scf);
619 	fdevent_fdnode_event_set(scf->ev, scf->fdn, FDEVENT_IN | FDEVENT_RDHUP);
620 
621 	return scf;
622 }
623 
624 static void stat_cache_free_fam(stat_cache_fam *scf) {
625 	if (NULL == scf) return;
626 
627       #ifdef HAVE_SYS_INOTIFY_H
628 	while (scf->wds) {
629 		splay_tree *node = scf->wds;
630 		scf->wds = splaytree_delete(scf->wds, node->key);
631 	}
632       #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
633 	/*(quicker cleanup to close kqueue() before cancel per entry)*/
634 	close(scf->fd);
635 	scf->fd = -1;
636       #endif
637 	while (scf->dirs) {
638 		/*(skip entry invalidation and FAMCancelMonitor())*/
639 		splay_tree *node = scf->dirs;
640 		fam_dir_entry_free((fam_dir_entry *)node->data);
641 		scf->dirs = splaytree_delete(scf->dirs, node->key);
642 	}
643 
644 	if (-1 != scf->fd) {
645 		/*scf->fdn already cleaned up in fdevent_free()*/
646 		FAMClose(&scf->fam);
647 		/*scf->fd = -1;*/
648 	}
649 
650 	free(scf);
651 }
652 
653 static fam_dir_entry * fam_dir_monitor(stat_cache_fam *scf, char *fn, uint32_t dirlen, struct stat *st)
654 {
655     if (NULL == scf->fdn) return NULL; /* FAM connection closed; do nothing */
656     const int fn_is_dir = S_ISDIR(st->st_mode);
657     /*force_assert(0 != dirlen);*/
658     /*force_assert(fn[0] == '/');*/
659     /* consistency: ensure fn does not end in '/' unless root "/"
660      * FAM events will not end in '/', so easier to match this way */
661     if (fn[dirlen-1] == '/') --dirlen;
662     if (0 == dirlen) dirlen = 1; /* root dir ("/") */
663     /* Note: paths are expected to be normalized before calling stat_cache,
664      * e.g. without repeated '/' */
665     if (!fn_is_dir) {
666         while (fn[--dirlen] != '/') ;
667         if (0 == dirlen) dirlen = 1; /*(should not happen for file)*/
668     }
669     int dir_ndx = splaytree_djbhash(fn, dirlen);
670     fam_dir_entry *fam_dir = NULL;
671 
672     scf->dirs = splaytree_splay(scf->dirs, dir_ndx);
673     if (NULL != scf->dirs && scf->dirs->key == dir_ndx) {
674         fam_dir = scf->dirs->data;
675         if (!buffer_is_equal_string(fam_dir->name, fn, dirlen)) {
676             /* hash collision; preserve existing
677              * do not monitor new to avoid cache thrashing */
678             return NULL;
679         }
680         /* directory already registered */
681     }
682 
683     const time_t cur_ts = log_epoch_secs;
684     struct stat lst;
685     int ck_dir = fn_is_dir;
686     if (!fn_is_dir && (NULL==fam_dir || cur_ts - fam_dir->stat_ts >= 16)) {
687         ck_dir = 1;
688         /*(temporarily modify fn)*/
689         fn[dirlen] = '\0';
690         if (0 != lstat(fn, &lst)) {
691             fn[dirlen] = '/';
692             return NULL;
693         }
694         if (!S_ISLNK(lst.st_mode)) {
695             st = &lst;
696         }
697         else if (0 != stat(fn, st)) { /*st passed in now is stat() of dir*/
698             fn[dirlen] = '/';
699             return NULL;
700         }
701         fn[dirlen] = '/';
702     }
703 
704     int ck_lnk = (NULL == fam_dir);
705     if (ck_dir && NULL != fam_dir) {
706         /* check stat() matches device and inode, just in case an external event
707          * not being monitored occurs (e.g. rename of unmonitored parent dir)*/
708         if (st->st_dev != fam_dir->st_dev || st->st_ino != fam_dir->st_ino) {
709             ck_lnk = 1;
710             /*(modifies scf->dirs but no need to re-splay for dir_ndx since
711              * fam_dir is not NULL and so splaytree_insert not called below)*/
712             if (scf->dirs) fam_dir_invalidate_tree(scf->dirs, fn, dirlen);
713             if (!fn_is_dir) /*(if dir, caller is updating stat_cache_entry)*/
714                 stat_cache_update_entry(fn, dirlen, st, NULL);
715             /*(must not delete tree since caller is holding a valid node)*/
716             stat_cache_invalidate_dir_tree(fn, dirlen);
717           #ifdef HAVE_SYS_INOTIFY_H
718             scf->wds = splaytree_delete(scf->wds, fam_dir->req);
719           #endif
720             if (0 != FAMCancelMonitor(&scf->fam, &fam_dir->req)
721                 || 0 != FAMMonitorDirectory(&scf->fam, fam_dir->name->ptr,
722                                             &fam_dir->req,
723                                             (void *)(intptr_t)dir_ndx)) {
724                 fam_dir->stat_ts = 0; /* invalidate */
725                 return NULL;
726             }
727             fam_dir->st_dev = st->st_dev;
728             fam_dir->st_ino = st->st_ino;
729           #ifdef HAVE_SYS_INOTIFY_H
730             scf->wds = splaytree_insert(scf->wds, fam_dir->req, fam_dir);
731           #endif
732         }
733         fam_dir->stat_ts = cur_ts;
734     }
735 
736     if (NULL == fam_dir) {
737         fam_dir = fam_dir_entry_init(fn, dirlen);
738 
739         if (0 != FAMMonitorDirectory(&scf->fam,fam_dir->name->ptr,&fam_dir->req,
740                                      (void *)(intptr_t)dir_ndx)) {
741           #if defined(HAVE_SYS_INOTIFY_H) \
742            || (defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE)
743             log_perror(scf->errh, __FILE__, __LINE__,
744               "monitoring dir failed: %s file: %s",
745               fam_dir->name->ptr, fn);
746           #else
747             log_error(scf->errh, __FILE__, __LINE__,
748               "monitoring dir failed: %s file: %s %s",
749               fam_dir->name->ptr, fn, FamErrlist[FAMErrno]);
750           #endif
751             fam_dir_entry_free(fam_dir);
752             return NULL;
753         }
754 
755         scf->dirs = splaytree_insert(scf->dirs, dir_ndx, fam_dir);
756       #ifdef HAVE_SYS_INOTIFY_H
757         scf->wds = splaytree_insert(scf->wds, fam_dir->req, fam_dir);
758       #endif
759         fam_dir->stat_ts= cur_ts;
760         fam_dir->st_dev = st->st_dev;
761         fam_dir->st_ino = st->st_ino;
762     }
763 
764     if (ck_lnk) {
765         if (fn_is_dir) {
766             /*(temporarily modify fn)*/
767             char e = fn[dirlen];
768             fn[dirlen] = '\0';
769             if (0 != lstat(fn, &lst)) {
770                 fn[dirlen] = e;
771                 return NULL;
772             }
773             fn[dirlen] = e;
774         }
775         if (fam_dir->fam_parent) {
776             --fam_dir->fam_parent->refcnt;
777             fam_dir->fam_parent = NULL;
778         }
779         if (S_ISLNK(lst.st_mode)) {
780             fam_dir->fam_parent = fam_dir_monitor(scf, fn, dirlen, &lst);
781         }
782     }
783 
784     ++fam_dir->refcnt;
785     return fam_dir;
786 }
787 
788 #endif
789 
790 
791 static stat_cache_entry * stat_cache_entry_init(void) {
792     stat_cache_entry *sce = calloc(1, sizeof(*sce));
793     force_assert(NULL != sce);
794     sce->fd = -1;
795     sce->refcnt = 1;
796     return sce;
797 }
798 
799 static void stat_cache_entry_free(void *data) {
800     stat_cache_entry *sce = data;
801     if (!sce) return;
802 
803     if (--sce->refcnt) return;
804 
805   #ifdef HAVE_FAM_H
806     /*(decrement refcnt only;
807      * defer cancelling FAM monitor on dir even if refcnt reaches zero)*/
808     if (sce->fam_dir) --((fam_dir_entry *)sce->fam_dir)->refcnt;
809   #endif
810 
811     free(sce->name.ptr);
812     free(sce->etag.ptr);
813     if (sce->content_type.size) free(sce->content_type.ptr);
814     if (sce->fd >= 0) close(sce->fd);
815 
816     free(sce);
817 }
818 
819 void stat_cache_entry_refchg(void *data, int mod) {
820     /*(expect mod == -1 or mod == 1)*/
821     stat_cache_entry * const sce = data;
822     if (mod < 0 && 1 == sce->refcnt)
823         stat_cache_entry_free(data);
824     else
825         sce->refcnt += mod;
826 }
827 
828 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
829 
830 static const char *attrname = "Content-Type";
831 static char attrval[128];
832 static buffer attrb = { attrval, 0, 0 };
833 
834 static int stat_cache_attr_get(const char *name) {
835   #if defined(HAVE_XATTR)
836    #if defined(HAVE_SYS_XATTR_H)
837     ssize_t attrlen;
838     if (0 < (attrlen = getxattr(name, attrname,
839                                 attrval, sizeof(attrval)-1)))
840    #else
841     int attrlen = sizeof(attrval)-1;
842     if (0 == attr_get(name, attrname, attrval, &attrlen, 0))
843    #endif
844   #elif defined(HAVE_EXTATTR)
845     ssize_t attrlen;
846     if (0 < (attrlen = extattr_get_file(name, EXTATTR_NAMESPACE_USER, attrname,
847                                         attrval, sizeof(attrval)-1)))
848   #endif
849     {
850         attrval[attrlen] = '\0';
851         attrb.used = (uint32_t)(attrlen + 1);
852         return 1;
853     }
854     return 0;
855 }
856 
857 #endif
858 
859 int stat_cache_init(fdevents *ev, log_error_st *errh) {
860   #ifdef HAVE_FAM_H
861     if (sc.stat_cache_engine == STAT_CACHE_ENGINE_FAM) {
862         sc.scf = stat_cache_init_fam(ev, errh);
863         if (NULL == sc.scf) return 0;
864     }
865   #else
866     UNUSED(ev);
867     UNUSED(errh);
868   #endif
869 
870     return 1;
871 }
872 
873 void stat_cache_free(void) {
874     splay_tree *sptree = sc.files;
875     while (sptree) {
876         stat_cache_entry_free(sptree->data);
877         sptree = splaytree_delete(sptree, sptree->key);
878     }
879     sc.files = NULL;
880 
881   #ifdef HAVE_FAM_H
882     stat_cache_free_fam(sc.scf);
883     sc.scf = NULL;
884   #endif
885 
886   #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
887     attrname = "Content-Type";
888   #endif
889 
890     sc.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE; /*(default)*/
891 }
892 
893 void stat_cache_xattrname (const char *name) {
894   #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
895     attrname = name;
896   #else
897     UNUSED(name);
898   #endif
899 }
900 
901 int stat_cache_choose_engine (const buffer *stat_cache_string, log_error_st *errh) {
902     if (buffer_string_is_empty(stat_cache_string))
903         sc.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
904     else if (buffer_eq_slen(stat_cache_string, CONST_STR_LEN("simple")))
905         sc.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
906 #ifdef HAVE_SYS_INOTIFY_H
907     else if (buffer_eq_slen(stat_cache_string, CONST_STR_LEN("inotify")))
908         sc.stat_cache_engine = STAT_CACHE_ENGINE_INOTIFY;
909         /*(STAT_CACHE_ENGINE_FAM == STAT_CACHE_ENGINE_INOTIFY)*/
910 #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
911     else if (buffer_eq_slen(stat_cache_string, CONST_STR_LEN("kqueue")))
912         sc.stat_cache_engine = STAT_CACHE_ENGINE_KQUEUE;
913         /*(STAT_CACHE_ENGINE_FAM == STAT_CACHE_ENGINE_KQUEUE)*/
914 #endif
915 #ifdef HAVE_FAM_H
916     else if (buffer_eq_slen(stat_cache_string, CONST_STR_LEN("fam")))
917         sc.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
918 #endif
919     else if (buffer_eq_slen(stat_cache_string, CONST_STR_LEN("disable"))
920              || buffer_eq_slen(stat_cache_string, CONST_STR_LEN("none")))
921         sc.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
922     else {
923         log_error(errh, __FILE__, __LINE__,
924           "server.stat-cache-engine can be one of \"disable\", \"simple\","
925 #ifdef HAVE_SYS_INOTIFY_H
926           " \"inotify\","
927 #elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
928           " \"kqueue\","
929 #endif
930 #ifdef HAVE_FAM_H
931           " \"fam\","
932 #endif
933           " but not: %s", stat_cache_string->ptr);
934         return -1;
935     }
936     return 0;
937 }
938 
939 const buffer * stat_cache_mimetype_by_ext(const array * const mimetypes, const char * const name, const uint32_t nlen)
940 {
941     const char * const end = name + nlen; /*(end of string)*/
942     const uint32_t used = mimetypes->used;
943     if (used < 16) {
944         for (uint32_t i = 0; i < used; ++i) {
945             /* suffix match */
946             const data_string *ds = (data_string *)mimetypes->data[i];
947             const size_t klen = buffer_string_length(&ds->key);
948             if (klen <= nlen && buffer_eq_icase_ssn(end-klen, ds->key.ptr, klen))
949                 return &ds->value;
950         }
951     }
952     else {
953         const char *s;
954         const data_string *ds;
955         if (nlen) {
956             for (s = end-1; s != name && *s != '/'; --s) ; /*(like memrchr())*/
957             if (*s == '/') ++s;
958         }
959         else {
960             s = name;
961         }
962         /* search for basename, then longest .ext2.ext1, then .ext1, then "" */
963         ds = (const data_string *)array_get_element_klen(mimetypes, s, end - s);
964         if (NULL != ds) return &ds->value;
965         while (++s < end) {
966             while (*s != '.' && ++s != end) ;
967             if (s == end) break;
968             /* search ".ext" then "ext" */
969             ds = (const data_string *)array_get_element_klen(mimetypes, s, end - s);
970             if (NULL != ds) return &ds->value;
971             /* repeat search without leading '.' to handle situation where
972              * admin configured mimetype.assign keys without leading '.' */
973             if (++s < end) {
974                 if (*s == '.') { --s; continue; }
975                 ds = (const data_string *)array_get_element_klen(mimetypes, s, end - s);
976                 if (NULL != ds) return &ds->value;
977             }
978         }
979         /* search for ""; catchall */
980         ds = (const data_string *)array_get_element_klen(mimetypes, CONST_STR_LEN(""));
981         if (NULL != ds) return &ds->value;
982     }
983 
984     return NULL;
985 }
986 
987 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
988 
989 const buffer * stat_cache_mimetype_by_xattr(const char * const name)
990 {
991     return stat_cache_attr_get(name) ? &attrb : NULL;
992 }
993 
994 const buffer * stat_cache_content_type_get_by_xattr(stat_cache_entry *sce, const array *mimetypes, int use_xattr)
995 {
996     /*(invalid caching if user config has multiple, different
997      * r->conf.mimetypes for same extension (not expected))*/
998     if (!buffer_string_is_empty(&sce->content_type)) return &sce->content_type;
999 
1000     if (!S_ISREG(sce->st.st_mode)) return NULL;
1001 
1002     /* cache mimetype */
1003     const buffer *mtype =
1004       (use_xattr) ? stat_cache_mimetype_by_xattr(sce->name.ptr) : NULL;
1005     if (NULL == mtype)
1006         mtype = stat_cache_mimetype_by_ext(mimetypes,CONST_BUF_LEN(&sce->name));
1007     if (NULL != mtype) {
1008         if (sce->content_type.size) {
1009             buffer_copy_buffer(&sce->content_type, mtype);
1010         }
1011         else if (mtype == &attrb) {
1012             sce->content_type.ptr = NULL;
1013             buffer_copy_buffer(&sce->content_type, mtype);
1014         }
1015         else {
1016             /*(copy pointers from mimetypes array; avoid allocation)*/
1017             sce->content_type.ptr = mtype->ptr;
1018             sce->content_type.used = mtype->used;
1019             /*(leave sce->content_type.size = 0 to flag not-allocated)*/
1020         }
1021     }
1022     else
1023         buffer_clear(&sce->content_type);
1024 
1025     return &sce->content_type;
1026 }
1027 
1028 #else
1029 
1030 const buffer * stat_cache_content_type_get_by_ext(stat_cache_entry *sce, const array *mimetypes)
1031 {
1032     /*(invalid caching if user config has multiple, different
1033      * r->conf.mimetypes for same extension (not expected))*/
1034     if (!buffer_string_is_empty(&sce->content_type)) return &sce->content_type;
1035 
1036     if (!S_ISREG(sce->st.st_mode)) return NULL;
1037 
1038     /* cache mimetype */
1039     const buffer * const mtype =
1040       stat_cache_mimetype_by_ext(mimetypes, CONST_BUF_LEN(&sce->name));
1041     if (NULL != mtype) {
1042         /*(copy pointers from mimetypes array; avoid allocation)*/
1043         sce->content_type.ptr = mtype->ptr;
1044         sce->content_type.used = mtype->used;
1045         /*(leave sce->content_type.size = 0 to flag not-allocated)*/
1046     }
1047     else
1048         buffer_clear(&sce->content_type);
1049 
1050     return &sce->content_type;
1051 }
1052 
1053 #endif
1054 
1055 const buffer * stat_cache_etag_get(stat_cache_entry *sce, int flags) {
1056     /*(invalid caching if user cfg has multiple, different r->conf.etag_flags
1057      * for same path (not expected, since etag flags should be by filesystem))*/
1058     if (!buffer_string_is_empty(&sce->etag)) return &sce->etag;
1059 
1060     if (S_ISREG(sce->st.st_mode) || S_ISDIR(sce->st.st_mode)) {
1061         if (0 == flags) return NULL;
1062         etag_create(&sce->etag, &sce->st, flags);
1063         return &sce->etag;
1064     }
1065 
1066     return NULL;
1067 }
1068 
1069 __attribute_pure__
1070 static int stat_cache_stat_eq(const struct stat * const sta, const struct stat * const stb) {
1071     return
1072       #ifdef st_mtime /* use high-precision timestamp if available */
1073       #if defined(__APPLE__) && defined(__MACH__)
1074         sta->st_mtimespec.tv_nsec == stb->st_mtimespec.tv_nsec
1075       #else
1076         sta->st_mtim.tv_nsec == stb->st_mtim.tv_nsec
1077       #endif
1078       #else
1079         1
1080       #endif
1081         && sta->st_mtime == stb->st_mtime
1082         && sta->st_size  == stb->st_size
1083         && sta->st_ino   == stb->st_ino
1084         && sta->st_dev   == stb->st_dev;
1085 }
1086 
1087 void stat_cache_update_entry(const char *name, uint32_t len,
1088                              struct stat *st, buffer *etagb)
1089 {
1090     if (sc.stat_cache_engine == STAT_CACHE_ENGINE_NONE) return;
1091     force_assert(0 != len);
1092     if (name[len-1] == '/') { if (0 == --len) len = 1; }
1093     splay_tree **sptree = &sc.files;
1094     stat_cache_entry *sce =
1095       stat_cache_sptree_find(sptree, name, len);
1096     if (sce && buffer_is_equal_string(&sce->name, name, len)) {
1097         if (!stat_cache_stat_eq(&sce->st, st)) {
1098             /* etagb might be NULL to clear etag (invalidate) */
1099             buffer_copy_string_len(&sce->etag, CONST_BUF_LEN(etagb));
1100           #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
1101             buffer_clear(&sce->content_type);
1102           #endif
1103             if (sce->fd >= 0) {
1104                 if (1 == sce->refcnt) {
1105                     close(sce->fd);
1106                     sce->fd = -1;
1107                 }
1108                 else {
1109                     --sce->refcnt; /* stat_cache_entry_free(sce); */
1110                     (*sptree)->data = sce = stat_cache_entry_init();
1111                     buffer_copy_string_len(&sce->name, name, len);
1112                 }
1113             }
1114             sce->st = *st;
1115         }
1116         sce->stat_ts = log_epoch_secs;
1117     }
1118 }
1119 
1120 void stat_cache_delete_entry(const char *name, uint32_t len)
1121 {
1122     if (sc.stat_cache_engine == STAT_CACHE_ENGINE_NONE) return;
1123     force_assert(0 != len);
1124     if (name[len-1] == '/') { if (0 == --len) len = 1; }
1125     splay_tree **sptree = &sc.files;
1126     stat_cache_entry *sce = stat_cache_sptree_find(sptree, name, len);
1127     if (sce && buffer_is_equal_string(&sce->name, name, len)) {
1128         stat_cache_entry_free(sce);
1129         *sptree = splaytree_delete(*sptree, (*sptree)->key);
1130     }
1131 }
1132 
1133 void stat_cache_invalidate_entry(const char *name, uint32_t len)
1134 {
1135     splay_tree **sptree = &sc.files;
1136     stat_cache_entry *sce = stat_cache_sptree_find(sptree, name, len);
1137     if (sce && buffer_is_equal_string(&sce->name, name, len)) {
1138         sce->stat_ts = 0;
1139       #ifdef HAVE_FAM_H
1140         if (sce->fam_dir != NULL) {
1141             --((fam_dir_entry *)sce->fam_dir)->refcnt;
1142             sce->fam_dir = NULL;
1143         }
1144       #endif
1145     }
1146 }
1147 
1148 #ifdef HAVE_FAM_H
1149 
1150 static void stat_cache_invalidate_dir_tree_walk(splay_tree *t,
1151                                                 const char *name, size_t len)
1152 {
1153     if (t->left)  stat_cache_invalidate_dir_tree_walk(t->left,  name, len);
1154     if (t->right) stat_cache_invalidate_dir_tree_walk(t->right, name, len);
1155 
1156     buffer *b = &((stat_cache_entry *)t->data)->name;
1157     size_t blen = buffer_string_length(b);
1158     if (blen > len && b->ptr[len] == '/' && 0 == memcmp(b->ptr, name, len)) {
1159         stat_cache_entry *sce = t->data;
1160         sce->stat_ts = 0;
1161         if (sce->fam_dir != NULL) {
1162             --((fam_dir_entry *)sce->fam_dir)->refcnt;
1163             sce->fam_dir = NULL;
1164         }
1165     }
1166 }
1167 
1168 static void stat_cache_invalidate_dir_tree(const char *name, size_t len)
1169 {
1170     splay_tree * const sptree = sc.files;
1171     if (sptree) stat_cache_invalidate_dir_tree_walk(sptree, name, len);
1172 }
1173 
1174 #endif
1175 
1176 /*
1177  * walk though splay_tree and collect contents of dir tree.
1178  * remove tagged entries in a second loop
1179  */
1180 
1181 static void stat_cache_tag_dir_tree(splay_tree *t, const char *name, size_t len,
1182                                     int *keys, int *ndx)
1183 {
1184     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
1185     if (t->left)  stat_cache_tag_dir_tree(t->left,  name, len, keys, ndx);
1186     if (t->right) stat_cache_tag_dir_tree(t->right, name, len, keys, ndx);
1187     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
1188 
1189     buffer *b = &((stat_cache_entry *)t->data)->name;
1190     size_t blen = buffer_string_length(b);
1191     if (blen > len && b->ptr[len] == '/' && 0 == memcmp(b->ptr, name, len))
1192         keys[(*ndx)++] = t->key;
1193 }
1194 
1195 __attribute_noinline__
1196 static void stat_cache_prune_dir_tree(const char *name, size_t len)
1197 {
1198     splay_tree *sptree = sc.files;
1199     int max_ndx, i;
1200     int keys[8192]; /* 32k size on stack */
1201     do {
1202         if (!sptree) break;
1203         max_ndx = 0;
1204         stat_cache_tag_dir_tree(sptree, name, len, keys, &max_ndx);
1205         for (i = 0; i < max_ndx; ++i) {
1206             const int ndx = keys[i];
1207             splay_tree *node = sptree = splaytree_splay(sptree, ndx);
1208             if (node && node->key == ndx) {
1209                 stat_cache_entry_free(node->data);
1210                 sptree = splaytree_delete(sptree, ndx);
1211             }
1212         }
1213     } while (max_ndx == sizeof(keys)/sizeof(int));
1214     sc.files = sptree;
1215 }
1216 
1217 static void stat_cache_delete_tree(const char *name, uint32_t len)
1218 {
1219     stat_cache_delete_entry(name, len);
1220     stat_cache_prune_dir_tree(name, len);
1221 }
1222 
1223 void stat_cache_delete_dir(const char *name, uint32_t len)
1224 {
1225     force_assert(0 != len);
1226     if (name[len-1] == '/') { if (0 == --len) len = 1; }
1227     stat_cache_delete_tree(name, len);
1228   #ifdef HAVE_FAM_H
1229     if (sc.stat_cache_engine == STAT_CACHE_ENGINE_FAM) {
1230         splay_tree **sptree = &sc.scf->dirs;
1231         fam_dir_entry *fam_dir = stat_cache_sptree_find(sptree, name, len);
1232         if (fam_dir && buffer_is_equal_string(fam_dir->name, name, len))
1233             fam_dir_invalidate_node(fam_dir);
1234         if (*sptree) fam_dir_invalidate_tree(*sptree, name, len);
1235         fam_dir_periodic_cleanup();
1236     }
1237   #endif
1238 }
1239 
1240 /***
1241  *
1242  *
1243  *
1244  * returns:
1245  *  - HANDLER_FINISHED on cache-miss (don't forget to reopen the file)
1246  *  - HANDLER_ERROR on stat() failed -> see errno for problem
1247  */
1248 
1249 stat_cache_entry * stat_cache_get_entry(const buffer *name) {
1250 	stat_cache_entry *sce = NULL;
1251 	struct stat st;
1252 	int file_ndx;
1253 
1254 	/* consistency: ensure lookup name does not end in '/' unless root "/"
1255 	 * (but use full path given with stat(), even with trailing '/') */
1256 	int final_slash = 0;
1257 	size_t len = buffer_string_length(name);
1258 	force_assert(0 != len);
1259 	if (name->ptr[len-1] == '/') { final_slash = 1; if (0 == --len) len = 1; }
1260 	/* Note: paths are expected to be normalized before calling stat_cache,
1261 	 * e.g. without repeated '/' */
1262 
1263 	if (name->ptr[0] != '/') {
1264 		errno = EINVAL;
1265 		return NULL;
1266 	}
1267 
1268 	/*
1269 	 * check if the directory for this file has changed
1270 	 */
1271 
1272 	const time_t cur_ts = log_epoch_secs;
1273 
1274 	file_ndx = splaytree_djbhash(name->ptr, len);
1275 	splay_tree *sptree = sc.files = splaytree_splay(sc.files, file_ndx);
1276 
1277 	if (sptree && (sptree->key == file_ndx)) {
1278 		/* we have seen this file already and
1279 		 * don't stat() it again in the same second */
1280 
1281 		sce = sptree->data;
1282 
1283 		/* check if the name is the same, we might have a collision */
1284 
1285 		if (buffer_is_equal_string(&sce->name, name->ptr, len)) {
1286 			if (sc.stat_cache_engine == STAT_CACHE_ENGINE_SIMPLE) {
1287 				if (sce->stat_ts == cur_ts) {
1288 					if (final_slash && !S_ISDIR(sce->st.st_mode)) {
1289 						errno = ENOTDIR;
1290 						return NULL;
1291 					}
1292 					return sce;
1293 				}
1294 			}
1295 		      #ifdef HAVE_FAM_H
1296 			else if (sc.stat_cache_engine == STAT_CACHE_ENGINE_FAM
1297 				 && sce->fam_dir) { /* entry is in monitored dir */
1298 				/* re-stat() periodically, even if monitoring for changes
1299 				 * (due to limitations in stat_cache.c use of FAM)
1300 				 * (gaps due to not continually monitoring an entire tree) */
1301 				if (cur_ts - sce->stat_ts < 16) {
1302 					if (final_slash && !S_ISDIR(sce->st.st_mode)) {
1303 						errno = ENOTDIR;
1304 						return NULL;
1305 					}
1306 					return sce;
1307 				}
1308 			}
1309 		      #endif
1310 		} else {
1311 			/* collision, forget about the entry */
1312 			sce = NULL;
1313 		}
1314 	}
1315 
1316 	if (-1 == stat(name->ptr, &st)) {
1317 		return NULL;
1318 	}
1319 
1320 	if (S_ISREG(st.st_mode)) {
1321 		/* fix broken stat/open for symlinks to reg files with appended slash on freebsd,osx */
1322 		if (name->ptr[buffer_string_length(name) - 1] == '/') {
1323 			errno = ENOTDIR;
1324 			return NULL;
1325 		}
1326 	}
1327 
1328 	if (NULL == sce) {
1329 
1330 		sce = stat_cache_entry_init();
1331 		buffer_copy_string_len(&sce->name, name->ptr, len);
1332 
1333 		/* already splayed file_ndx */
1334 		if (NULL != sptree && sptree->key == file_ndx) {
1335 			/* hash collision: replace old entry */
1336 			stat_cache_entry_free(sptree->data);
1337 			sptree->data = sce;
1338 		} else {
1339 			sptree = sc.files = splaytree_insert(sptree, file_ndx, sce);
1340 		}
1341 
1342 	} else {
1343 
1344 		buffer_clear(&sce->etag);
1345 	      #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR)
1346 		buffer_clear(&sce->content_type);
1347 	      #endif
1348 
1349 	}
1350 
1351 	if (sce->fd >= 0) {
1352 		/* close fd if file changed */
1353 		if (!stat_cache_stat_eq(&sce->st, &st)) {
1354 			if (1 == sce->refcnt) {
1355 				close(sce->fd);
1356 				sce->fd = -1;
1357 			}
1358 			else {
1359 				--sce->refcnt; /* stat_cache_entry_free(sce); */
1360 				sptree->data = sce = stat_cache_entry_init();
1361 				buffer_copy_string_len(&sce->name, name->ptr, len);
1362 			}
1363 		}
1364 	}
1365 
1366 	sce->st = st; /*(copy prior to calling fam_dir_monitor())*/
1367 
1368 #ifdef HAVE_FAM_H
1369 	if (sc.stat_cache_engine == STAT_CACHE_ENGINE_FAM) {
1370 		if (sce->fam_dir) --((fam_dir_entry *)sce->fam_dir)->refcnt;
1371 		sce->fam_dir =
1372 		  fam_dir_monitor(sc.scf, CONST_BUF_LEN(name), &st);
1373 	      #if 0 /*(performed below)*/
1374 		if (NULL != sce->fam_dir) {
1375 			/*(may have been invalidated by dir change)*/
1376 			sce->stat_ts = cur_ts;
1377 		}
1378 	      #endif
1379 	}
1380 #endif
1381 
1382 	sce->stat_ts = cur_ts;
1383 	return sce;
1384 }
1385 
1386 stat_cache_entry * stat_cache_get_entry_open(const buffer * const name, const int symlinks) {
1387     stat_cache_entry * const sce = stat_cache_get_entry(name);
1388     if (NULL == sce) return NULL;
1389     if (sce->fd >= 0) return sce;
1390     if (sce->st.st_size > 0)
1391         sce->fd = stat_cache_open_rdonly_fstat(name, &sce->st, symlinks);
1392     return sce; /* (note: sce->fd might still be -1 if open() failed) */
1393 }
1394 
1395 const stat_cache_st * stat_cache_path_stat (const buffer * const name) {
1396     const stat_cache_entry * const sce = stat_cache_get_entry(name);
1397     return sce ? &sce->st : NULL;
1398 }
1399 
1400 int stat_cache_path_isdir(const buffer *name) {
1401     const stat_cache_entry * const sce = stat_cache_get_entry(name);
1402     return (sce && (S_ISDIR(sce->st.st_mode) ? 1 : (errno = ENOTDIR, 0)));
1403 }
1404 
1405 int stat_cache_path_contains_symlink(const buffer *name, log_error_st *errh) {
1406     /* caller should check for symlinks only if we should block symlinks. */
1407 
1408     /* catch the obvious symlinks
1409      *
1410      * this is not a secure check as we still have a race-condition between
1411      * the stat() and the open. We can only solve this by
1412      * 1. open() the file
1413      * 2. fstat() the fd
1414      *
1415      * and keeping the file open for the rest of the time. But this can
1416      * only be done at network level.
1417      * */
1418 
1419   #ifdef HAVE_LSTAT
1420     /* we assume "/" can not be symlink,
1421      * so skip the symlink stuff if path is "/" */
1422     size_t len = buffer_string_length(name);
1423     force_assert(0 != len);
1424     force_assert(name->ptr[0] == '/');
1425     if (1 == len) return 0;
1426    #ifndef PATH_MAX
1427    #define PATH_MAX 4096
1428    #endif
1429     if (len >= PATH_MAX) return -1;
1430 
1431     char buf[PATH_MAX];
1432     memcpy(buf, name->ptr, len);
1433     char *s_cur = buf+len;
1434     do {
1435         *s_cur = '\0';
1436         struct stat st;
1437         if (0 == lstat(buf, &st)) {
1438             if (S_ISLNK(st.st_mode)) return 1;
1439         }
1440         else {
1441             log_perror(errh, __FILE__, __LINE__, "lstat failed for: %s", buf);
1442             return -1;
1443         }
1444     } while ((s_cur = strrchr(buf, '/')) > buf); /*(&buf[0]==buf; NULL < buf)*/
1445   #endif
1446 
1447     return 0;
1448 }
1449 
1450 int stat_cache_open_rdonly_fstat (const buffer *name, struct stat *st, int symlinks) {
1451 	/*(Note: O_NOFOLLOW affects only the final path segment, the target file,
1452 	 * not any intermediate symlinks along the path)*/
1453 	const int fd = fdevent_open_cloexec(name->ptr, symlinks, O_RDONLY, 0);
1454 	if (fd >= 0) {
1455 		if (0 == fstat(fd, st)) {
1456 			return fd;
1457 		} else {
1458 			const int errnum = errno;
1459 			close(fd);
1460 			errno = errnum;
1461 		}
1462 	}
1463 	return -1;
1464 }
1465 
1466 /**
1467  * remove stat() from cache which haven't been stat()ed for
1468  * more than 2 seconds
1469  *
1470  *
1471  * walk though the stat-cache, collect the ids which are too old
1472  * and remove them in a second loop
1473  */
1474 
1475 static void stat_cache_tag_old_entries(splay_tree * const t, int * const keys, int * const ndx, const time_t max_age, const time_t cur_ts) {
1476     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
1477     if (t->left)
1478         stat_cache_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
1479     if (t->right)
1480         stat_cache_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
1481     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
1482 
1483     const stat_cache_entry * const sce = t->data;
1484     if (cur_ts - sce->stat_ts > max_age)
1485         keys[(*ndx)++] = t->key;
1486 }
1487 
1488 static void stat_cache_periodic_cleanup(const time_t max_age, const time_t cur_ts) {
1489     splay_tree *sptree = sc.files;
1490     int max_ndx, i;
1491     int keys[8192]; /* 32k size on stack */
1492     do {
1493         if (!sptree) break;
1494         max_ndx = 0;
1495         stat_cache_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts);
1496         for (i = 0; i < max_ndx; ++i) {
1497             int ndx = keys[i];
1498             sptree = splaytree_splay(sptree, ndx);
1499             if (sptree && sptree->key == ndx) {
1500                 stat_cache_entry_free(sptree->data);
1501                 sptree = splaytree_delete(sptree, ndx);
1502             }
1503         }
1504     } while (max_ndx == sizeof(keys)/sizeof(int));
1505     sc.files = sptree;
1506 }
1507 
1508 void stat_cache_trigger_cleanup(void) {
1509 	time_t max_age = 2;
1510 
1511       #ifdef HAVE_FAM_H
1512 	if (STAT_CACHE_ENGINE_FAM == sc.stat_cache_engine) {
1513 		if (log_epoch_secs & 0x1F) return;
1514 		/* once every 32 seconds (0x1F == 31) */
1515 		max_age = 32;
1516 		fam_dir_periodic_cleanup();
1517 		/* By doing this before stat_cache_periodic_cleanup(),
1518 		 * entries used within the next max_age secs will remain
1519 		 * monitored, instead of effectively flushing and
1520 		 * rebuilding the FAM monitoring every max_age seconds */
1521 	}
1522       #endif
1523 
1524 	stat_cache_periodic_cleanup(max_age, log_epoch_secs);
1525 }
1526