2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "stat_cache.h"
|
2018-03-25 07:45:05 +00:00
|
|
|
#include "log.h"
|
2018-02-03 18:43:59 +00:00
|
|
|
#include "fdevent.h"
|
2020-12-25 08:56:39 +00:00
|
|
|
#include "http_etag.h"
|
2020-09-05 03:47:19 +00:00
|
|
|
#include "algo_splaytree.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2020-03-23 02:51:30 +00:00
|
|
|
#if defined(HAVE_SYS_XATTR_H)
|
|
|
|
# include <sys/xattr.h>
|
|
|
|
#elif defined(HAVE_ATTR_ATTRIBUTES_H)
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <attr/attributes.h>
|
2005-08-08 08:22:06 +00:00
|
|
|
#endif
|
|
|
|
|
2014-05-22 08:30:13 +00:00
|
|
|
#ifdef HAVE_SYS_EXTATTR_H
|
|
|
|
# include <sys/extattr.h>
|
|
|
|
#endif
|
|
|
|
|
2005-08-08 08:22:06 +00:00
|
|
|
#ifndef HAVE_LSTAT
|
2019-05-04 19:41:27 +00:00
|
|
|
#define lstat stat
|
|
|
|
#ifndef S_ISLNK
|
|
|
|
#define S_ISLNK(mode) (0)
|
|
|
|
#endif
|
2005-08-08 08:22:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stat-cache
|
|
|
|
*
|
|
|
|
* - a splay-tree is used as we can use the caching effect of it
|
2006-10-04 13:26:23 +00:00
|
|
|
*/
|
2005-08-08 08:22:06 +00:00
|
|
|
|
2017-10-29 21:01:54 +00:00
|
|
|
enum {
|
2020-11-03 05:46:50 +00:00
|
|
|
STAT_CACHE_ENGINE_SIMPLE = 0 /*(default)*/
|
|
|
|
,STAT_CACHE_ENGINE_NONE = 1
|
|
|
|
,STAT_CACHE_ENGINE_FAM = 2 /* same as STAT_CACHE_ENGINE_INOTIFY */
|
|
|
|
,STAT_CACHE_ENGINE_INOTIFY = 2 /* same as STAT_CACHE_ENGINE_FAM */
|
2020-11-04 09:20:00 +00:00
|
|
|
,STAT_CACHE_ENGINE_KQUEUE = 2 /* same as STAT_CACHE_ENGINE_FAM */
|
2017-10-29 21:01:54 +00:00
|
|
|
};
|
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
struct stat_cache_fam; /* declaration */
|
2017-10-29 21:01:54 +00:00
|
|
|
|
2017-03-28 04:28:53 +00:00
|
|
|
typedef struct stat_cache {
|
2019-12-05 08:16:25 +00:00
|
|
|
int stat_cache_engine;
|
2019-05-04 19:41:27 +00:00
|
|
|
splay_tree *files; /* nodes of tree are (stat_cache_entry *) */
|
2017-10-29 21:01:54 +00:00
|
|
|
struct stat_cache_fam *scf;
|
|
|
|
} stat_cache;
|
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
static stat_cache sc;
|
|
|
|
|
2017-10-29 21:01:54 +00:00
|
|
|
|
2019-04-26 06:33:24 +00:00
|
|
|
static void * stat_cache_sptree_find(splay_tree ** const sptree,
|
|
|
|
const char * const name,
|
2020-07-09 21:51:01 +00:00
|
|
|
uint32_t len)
|
2019-04-26 06:33:24 +00:00
|
|
|
{
|
2020-07-10 04:02:48 +00:00
|
|
|
const int ndx = splaytree_djbhash(name, len);
|
2019-04-26 06:33:24 +00:00
|
|
|
*sptree = splaytree_splay(*sptree, ndx);
|
|
|
|
return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-04 09:20:00 +00:00
|
|
|
#if defined(HAVE_SYS_INOTIFY_H) \
|
|
|
|
|| (defined(HAVE_SYS_EVENT_H) && defined(HAVE_KQUEUE))
|
2020-11-03 05:46:50 +00:00
|
|
|
#ifndef HAVE_FAM_H
|
|
|
|
#define HAVE_FAM_H
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-03-28 04:28:53 +00:00
|
|
|
#ifdef HAVE_FAM_H
|
2017-10-29 21:01:54 +00:00
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
/* monitor changes in directories using FAM
|
|
|
|
*
|
|
|
|
* This implementation employing FAM monitors directories as they are used,
|
|
|
|
* and maintains a reference count for cache use within stat_cache.c.
|
2020-02-22 22:24:12 +00:00
|
|
|
* A periodic job runs in lighttpd every 32 seconds, expiring entries unused
|
2019-05-04 19:41:27 +00:00
|
|
|
* in last 64 seconds out of the cache and cancelling FAM monitoring. Items
|
|
|
|
* within the cache are checked against the filesystem upon use if last stat()
|
|
|
|
* was greater than or equal to 16 seconds ago.
|
|
|
|
*
|
|
|
|
* This implementation does not monitor every directory in a tree, and therefore
|
|
|
|
* the cache may get out-of-sync with the filesystem. Delays in receiving and
|
|
|
|
* processing events from FAM might also lead to stale cache entries.
|
|
|
|
*
|
|
|
|
* For many websites, a large number of files are seldom, if ever, modified,
|
|
|
|
* and a common practice with images is to create a new file with a new name
|
|
|
|
* when a new version is needed, in order for client browsers and CDNs to better
|
|
|
|
* cache the content. Given this, most use will see little difference in
|
|
|
|
* performance between server.stat-cache-engine = "fam" and "simple" (default).
|
|
|
|
* The default server.stat-cache-engine = "simple" calls stat() on a target once
|
|
|
|
* per second, and reuses that information until the next second. For use where
|
|
|
|
* changes must be immediately visible, server.stat-cache-engine = "disable"
|
|
|
|
* should be used.
|
|
|
|
*
|
|
|
|
* When considering use of server.stat-cache-engine = "fam", there are a few
|
|
|
|
* additional limitations for this cache implementation using FAM.
|
|
|
|
* - symlinks to files located outside of the current directory do not result
|
|
|
|
* in changes to that file being monitored (unless that file is in a directory
|
|
|
|
* which is monitored as a result of a different request). symlinks can be
|
|
|
|
* chained and can be circular. This implementation *does not* readlink() or
|
|
|
|
* realpath() to resolve the chains to find and monitor the ultimate target
|
|
|
|
* directory. While symlinks to files located outside the current directory
|
|
|
|
* are not monitored, symlinks to directories *are* monitored, though chains
|
|
|
|
* of symlinks to directories do not result in monitoring of the directories
|
|
|
|
* containing intermediate symlinks to the target directory.
|
|
|
|
* - directory rename of a directory which is not currently being monitored will
|
|
|
|
* result in stale information in the cache if there is a subdirectory that is
|
|
|
|
* being monitored.
|
|
|
|
* Even though lighttpd will not receive FAM events in the above cases, lighttpd
|
|
|
|
* does re-validate the information in the cache upon use if the cache entry has
|
|
|
|
* not been checked in 16 seconds, so that is the upper limit for use of stale
|
|
|
|
* data.
|
|
|
|
*
|
|
|
|
* Use of server.stat-cache-engine = "fam" is discouraged for extremely volatile
|
|
|
|
* directories such as temporary directories (e.g. /tmp and maybe /var/tmp) due
|
|
|
|
* to the overhead of processing the additional noise generated from changes.
|
|
|
|
* Related, server.stat-cache-engine = "fam" is not recommended on trees of
|
|
|
|
* untrusted files where a malicious user could generate an excess of change
|
|
|
|
* events.
|
|
|
|
*
|
|
|
|
* Internal note: lighttpd walks the caches to prune trees in stat_cache when an
|
|
|
|
* event is received for a directory (or symlink to a directory) which has been
|
|
|
|
* deleted or renamed. The splaytree data structure is suboptimal for frequent
|
|
|
|
* changes of large directories trees where there have been a large number of
|
|
|
|
* different files recently accessed and part of the stat_cache.
|
|
|
|
*/
|
|
|
|
|
2020-11-05 06:08:11 +00:00
|
|
|
#if defined(HAVE_SYS_INOTIFY_H) \
|
|
|
|
&& !(defined(HAVE_SYS_EVENT_H) && defined(HAVE_KQUEUE))
|
2020-11-03 05:46:50 +00:00
|
|
|
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
|
|
|
/*(translate FAM API to inotify; this is specific to stat_cache.c use of FAM)*/
|
|
|
|
#define fam fd /*(translate struct stat_cache_fam scf->fam -> scf->fd)*/
|
|
|
|
typedef int FAMRequest; /*(fr)*/
|
|
|
|
#define FAMClose(fd) \
|
|
|
|
close(*(fd))
|
|
|
|
#define FAMCancelMonitor(fd, wd) \
|
|
|
|
inotify_rm_watch(*(fd), *(wd))
|
2020-12-08 20:15:11 +00:00
|
|
|
#define fam_watch_mask ( IN_ATTRIB | IN_CREATE | IN_DELETE | IN_DELETE_SELF \
|
|
|
|
| IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM \
|
|
|
|
| IN_EXCL_UNLINK | IN_ONLYDIR )
|
2020-11-03 05:46:50 +00:00
|
|
|
/*(note: follows symlinks; not providing IN_DONT_FOLLOW)*/
|
|
|
|
#define FAMMonitorDirectory(fd, fn, wd, userData) \
|
|
|
|
((*(wd) = inotify_add_watch(*(fd), (fn), (fam_watch_mask))) < 0)
|
|
|
|
typedef enum FAMCodes { /*(copied from fam.h to define arbitrary enum values)*/
|
|
|
|
FAMChanged=1,
|
|
|
|
FAMDeleted=2,
|
|
|
|
FAMCreated=5,
|
|
|
|
FAMMoved=6,
|
|
|
|
} FAMCodes;
|
|
|
|
|
2020-11-04 09:20:00 +00:00
|
|
|
#elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
2020-11-05 06:08:11 +00:00
|
|
|
#undef HAVE_SYS_INOTIFY_H
|
2020-11-04 09:20:00 +00:00
|
|
|
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
/*(translate FAM API to inotify; this is specific to stat_cache.c use of FAM)*/
|
|
|
|
#define fam fd /*(translate struct stat_cache_fam scf->fam -> scf->fd)*/
|
|
|
|
typedef int FAMRequest; /*(fr)*/
|
|
|
|
#define FAMClose(fd) \
|
|
|
|
(-1 != (*(fd)) ? close(*(fd)) : 0)
|
|
|
|
static int FAMCancelMonitor (const int * const fd, int * const wd)
|
|
|
|
{
|
|
|
|
if (-1 == *fd) return 0;
|
|
|
|
if (-1 == *wd) return 0;
|
|
|
|
struct timespec t0 = { 0, 0 };
|
|
|
|
struct kevent kev;
|
|
|
|
EV_SET(&kev, *wd, EVFILT_VNODE, EV_DELETE, 0, 0, 0);
|
|
|
|
int rc = kevent(*fd, &kev, 1, NULL, 0, &t0);
|
|
|
|
close(*wd);
|
|
|
|
*wd = -1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
static int FAMMonitorDirectory (int * const fd, char * const fn, int * const wd, void * const userData)
|
|
|
|
{
|
|
|
|
*wd = fdevent_open_dirname(fn, 1); /*(note: follows symlinks)*/
|
|
|
|
if (-1 == *wd) return -1;
|
|
|
|
struct timespec t0 = { 0, 0 };
|
|
|
|
struct kevent kev;
|
|
|
|
unsigned short kev_flags = EV_ADD | EV_ENABLE | EV_CLEAR;
|
|
|
|
unsigned int kev_fflags = NOTE_ATTRIB | NOTE_EXTEND | NOTE_LINK | NOTE_WRITE
|
|
|
|
| NOTE_DELETE | NOTE_REVOKE | NOTE_RENAME;
|
|
|
|
EV_SET(&kev, *wd, EVFILT_VNODE, kev_flags, kev_fflags, 0, userData);
|
|
|
|
return kevent(*fd, &kev, 1, NULL, 0, &t0);
|
|
|
|
}
|
|
|
|
typedef enum FAMCodes { /*(copied from fam.h to define arbitrary enum values)*/
|
|
|
|
FAMChanged=1,
|
|
|
|
FAMDeleted=2,
|
|
|
|
FAMCreated=5,
|
|
|
|
FAMMoved=6,
|
|
|
|
} FAMCodes;
|
|
|
|
|
2020-11-03 05:46:50 +00:00
|
|
|
#else
|
|
|
|
|
2017-10-29 21:01:54 +00:00
|
|
|
#include <fam.h>
|
|
|
|
|
2020-08-10 19:46:47 +00:00
|
|
|
#ifdef HAVE_FAMNOEXISTS
|
|
|
|
#ifndef LIGHTTPD_STATIC
|
2021-08-27 05:15:59 +00:00
|
|
|
#ifdef HAVE_DLFCN_H
|
2020-08-10 19:46:47 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-08-27 05:15:59 +00:00
|
|
|
#endif
|
2020-08-10 19:46:47 +00:00
|
|
|
|
2020-11-03 05:46:50 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
typedef struct fam_dir_entry {
|
2021-06-06 08:43:45 +00:00
|
|
|
buffer name;
|
2019-05-04 19:41:27 +00:00
|
|
|
int refcnt;
|
2019-04-26 00:37:02 +00:00
|
|
|
FAMRequest req;
|
2021-07-12 18:46:49 +00:00
|
|
|
unix_time64_t stat_ts;
|
2019-05-04 19:41:27 +00:00
|
|
|
dev_t st_dev;
|
|
|
|
ino_t st_ino;
|
|
|
|
struct fam_dir_entry *fam_parent;
|
2017-10-29 21:01:54 +00:00
|
|
|
} fam_dir_entry;
|
|
|
|
|
|
|
|
typedef struct stat_cache_fam {
|
2020-11-03 05:46:50 +00:00
|
|
|
splay_tree *dirs; /* indexed by path; node data is fam_dir_entry */
|
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
splay_tree *wds; /* indexed by inotify watch descriptor */
|
2020-11-04 09:20:00 +00:00
|
|
|
#elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
2020-11-03 05:46:50 +00:00
|
|
|
#else
|
2017-03-28 04:28:53 +00:00
|
|
|
FAMConnection fam;
|
2020-11-03 05:46:50 +00:00
|
|
|
#endif
|
2019-12-26 02:51:20 +00:00
|
|
|
log_error_st *errh;
|
|
|
|
fdevents *ev;
|
2019-03-01 04:58:49 +00:00
|
|
|
fdnode *fdn;
|
|
|
|
int fd;
|
2017-10-29 21:01:54 +00:00
|
|
|
} stat_cache_fam;
|
|
|
|
|
2021-09-17 09:06:16 +00:00
|
|
|
__attribute_returns_nonnull__
|
2019-05-04 19:41:27 +00:00
|
|
|
static fam_dir_entry * fam_dir_entry_init(const char *name, size_t len)
|
|
|
|
{
|
|
|
|
fam_dir_entry * const fam_dir = calloc(1, sizeof(*fam_dir));
|
|
|
|
force_assert(NULL != fam_dir);
|
2017-10-29 21:01:54 +00:00
|
|
|
|
2021-06-06 08:43:45 +00:00
|
|
|
buffer_copy_string_len(&fam_dir->name, name, len);
|
2019-05-04 19:41:27 +00:00
|
|
|
fam_dir->refcnt = 0;
|
2020-11-04 09:20:00 +00:00
|
|
|
#if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
fam_dir->req = -1;
|
|
|
|
#endif
|
2017-10-29 21:01:54 +00:00
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
return fam_dir;
|
2017-10-29 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
static void fam_dir_entry_free(fam_dir_entry *fam_dir)
|
|
|
|
{
|
|
|
|
if (!fam_dir) return;
|
2021-06-06 08:43:45 +00:00
|
|
|
/*(fam_dir->fam_parent might be invalid pointer here; ignore)*/
|
|
|
|
free(fam_dir->name.ptr);
|
2020-11-04 09:20:00 +00:00
|
|
|
#if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
if (-1 != fam_dir->req)
|
|
|
|
close(fam_dir->req);
|
|
|
|
#endif
|
2019-05-04 19:41:27 +00:00
|
|
|
free(fam_dir);
|
2017-10-29 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
static void fam_dir_invalidate_node(fam_dir_entry *fam_dir)
|
2019-04-26 06:33:24 +00:00
|
|
|
{
|
2019-05-04 19:41:27 +00:00
|
|
|
fam_dir->stat_ts = 0;
|
|
|
|
if (fam_dir->fam_parent) {
|
|
|
|
--fam_dir->fam_parent->refcnt;
|
|
|
|
fam_dir->fam_parent = NULL;
|
2019-04-26 06:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* walk though splay_tree and collect contents of dir tree.
|
|
|
|
* remove tagged entries in a second loop
|
|
|
|
*/
|
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
static void fam_dir_tag_refcnt(splay_tree *t, int *keys, int *ndx)
|
2019-04-26 06:33:24 +00:00
|
|
|
{
|
2020-11-05 05:48:41 +00:00
|
|
|
if (*ndx == 512) return; /*(must match num array entries in keys[])*/
|
2019-05-04 19:41:27 +00:00
|
|
|
if (t->left) fam_dir_tag_refcnt(t->left, keys, ndx);
|
|
|
|
if (t->right) fam_dir_tag_refcnt(t->right, keys, ndx);
|
2020-11-05 05:48:41 +00:00
|
|
|
if (*ndx == 512) return; /*(must match num array entries in keys[])*/
|
2019-04-26 06:33:24 +00:00
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
fam_dir_entry * const fam_dir = t->data;
|
|
|
|
if (0 == fam_dir->refcnt) {
|
|
|
|
fam_dir_invalidate_node(fam_dir);
|
2019-04-26 06:33:24 +00:00
|
|
|
keys[(*ndx)++] = t->key;
|
2019-05-04 19:41:27 +00:00
|
|
|
}
|
2019-04-26 06:33:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 04:57:54 +00:00
|
|
|
__attribute_noinline__
|
2019-12-05 08:16:25 +00:00
|
|
|
static void fam_dir_periodic_cleanup() {
|
2020-07-13 04:57:54 +00:00
|
|
|
stat_cache_fam * const scf = sc.scf;
|
2019-04-26 06:33:24 +00:00
|
|
|
int max_ndx, i;
|
2020-11-05 05:48:41 +00:00
|
|
|
int keys[512]; /* 2k size on stack */
|
|
|
|
#if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
struct kevent kevl[512]; /* 32k size on stack to batch kevent EV_DELETE */
|
|
|
|
#endif
|
2019-04-26 06:33:24 +00:00
|
|
|
do {
|
2020-07-13 04:57:54 +00:00
|
|
|
if (!scf->dirs) break;
|
2019-04-26 06:33:24 +00:00
|
|
|
max_ndx = 0;
|
2019-05-04 19:41:27 +00:00
|
|
|
fam_dir_tag_refcnt(scf->dirs, keys, &max_ndx);
|
2019-04-26 06:33:24 +00:00
|
|
|
for (i = 0; i < max_ndx; ++i) {
|
|
|
|
const int ndx = keys[i];
|
|
|
|
splay_tree *node = scf->dirs = splaytree_splay(scf->dirs, ndx);
|
|
|
|
if (node && node->key == ndx) {
|
2019-05-04 19:41:27 +00:00
|
|
|
fam_dir_entry *fam_dir = node->data;
|
2019-04-26 06:33:24 +00:00
|
|
|
scf->dirs = splaytree_delete(scf->dirs, ndx);
|
2020-11-03 05:46:50 +00:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
scf->wds = splaytree_delete(scf->wds, fam_dir->req);
|
2020-11-04 09:20:00 +00:00
|
|
|
#elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
/* batch process kevent removal; defer cancel */
|
|
|
|
EV_SET(kevl+i, fam_dir->req, EVFILT_VNODE, EV_DELETE, 0, 0, 0);
|
|
|
|
fam_dir->req = -1; /*(make FAMCancelMonitor() a no-op)*/
|
2020-11-03 05:46:50 +00:00
|
|
|
#endif
|
2019-05-04 19:41:27 +00:00
|
|
|
FAMCancelMonitor(&scf->fam, &fam_dir->req);
|
|
|
|
fam_dir_entry_free(fam_dir);
|
2019-04-26 06:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 09:20:00 +00:00
|
|
|
#if defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
2020-11-05 05:48:41 +00:00
|
|
|
/* batch process: kevent() to submit EV_DELETE, then close dir fds */
|
|
|
|
if (0 == max_ndx) break;
|
2020-11-04 09:20:00 +00:00
|
|
|
struct timespec t0 = { 0, 0 };
|
|
|
|
kevent(scf->fd, kevl, max_ndx, NULL, 0, &t0);
|
|
|
|
for (i = 0; i < max_ndx; ++i)
|
|
|
|
close((int)kevl[i].ident);
|
|
|
|
#endif
|
2019-04-26 06:33:24 +00:00
|
|
|
} while (max_ndx == sizeof(keys)/sizeof(int));
|
|
|
|
}
|
|
|
|
|
2019-05-04 19:41:27 +00:00
|
|
|
static void fam_dir_invalidate_tree(splay_tree *t, const char *name, size_t len)
|
|
|
|
{
|
2020-07-08 22:01:52 +00:00
|
|
|
#ifdef __clang_analyzer__
|
|
|
|
force_assert(name);
|
|
|
|
#endif
|
2019-05-04 19:41:27 +00:00
|
|
|
/*force_assert(t);*/
|
|
|
|
if (t->left) fam_dir_invalidate_tree(t->left, name, len);
|
|
|
|
if (t->right) fam_dir_invalidate_tree(t->right, name, len);
|
|
|
|
|
|
|
|
fam_dir_entry * const fam_dir = t->data;
|
2020-07-08 22:01:52 +00:00
|
|
|
#ifdef __clang_analyzer__
|
|
|
|
force_assert(fam_dir);
|
|
|
|
#endif
|
2021-06-06 08:43:45 +00:00
|
|
|
const buffer * const b = &fam_dir->name;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
size_t blen = buffer_clen(b);
|
2019-05-04 19:41:27 +00:00
|
|
|
if (blen > len && b->ptr[len] == '/' && 0 == memcmp(b->ptr, name, len))
|
|
|
|
fam_dir_invalidate_node(fam_dir);
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:36:54 +00:00
|
|
|
/* declarations */
|
2020-07-09 21:51:01 +00:00
|
|
|
static void stat_cache_delete_tree(const char *name, uint32_t len);
|
2019-12-05 08:16:25 +00:00
|
|
|
static void stat_cache_invalidate_dir_tree(const char *name, size_t len);
|
2020-11-03 05:46:50 +00:00
|
|
|
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);
|
2019-04-26 06:36:54 +00:00
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
static void stat_cache_handle_fdevent_in(stat_cache_fam *scf)
|
2019-05-04 19:46:50 +00:00
|
|
|
{
|
2020-11-03 05:46:50 +00:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
/*(inotify pads in->len to align struct following in->name[])*/
|
|
|
|
char buf[4096]
|
|
|
|
__attribute__ ((__aligned__(__alignof__(struct inotify_event))));
|
|
|
|
int rd;
|
|
|
|
do {
|
|
|
|
rd = (int)read(scf->fd, buf, sizeof(buf));
|
|
|
|
if (rd <= 0) {
|
|
|
|
if (-1 == rd && errno != EINTR && errno != EAGAIN) {
|
|
|
|
log_perror(scf->errh, __FILE__, __LINE__, "inotify error");
|
|
|
|
/* TODO: could flush cache, close scf->fd, and re-open inotify*/
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < rd; ) {
|
|
|
|
struct inotify_event * const in =
|
|
|
|
(struct inotify_event *)((uintptr_t)buf + i);
|
2020-11-30 01:04:19 +00:00
|
|
|
uint32_t len = in->len;
|
2020-12-17 08:50:25 +00:00
|
|
|
if (len > sizeof(buf)) break; /*(should not happen)*/
|
2020-11-30 01:04:19 +00:00
|
|
|
i += sizeof(struct inotify_event) + len;
|
|
|
|
if (i > rd) break; /*(should not happen (partial record))*/
|
2020-11-03 05:46:50 +00:00
|
|
|
if (in->mask & IN_CREATE)
|
|
|
|
continue; /*(see comment below for FAMCreated)*/
|
|
|
|
if (in->mask & IN_Q_OVERFLOW) {
|
|
|
|
log_error(scf->errh, __FILE__, __LINE__,
|
|
|
|
"inotify queue overflow");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* ignore events which may have been pending for
|
|
|
|
* paths recently cancelled via FAMCancelMonitor() */
|
|
|
|
scf->wds = splaytree_splay(scf->wds, in->wd);
|
|
|
|
if (!scf->wds || scf->wds->key != in->wd)
|
|
|
|
continue;
|
|
|
|
fam_dir_entry *fam_dir = scf->wds->data;
|
|
|
|
if (NULL == fam_dir) /*(should not happen)*/
|
|
|
|
continue;
|
|
|
|
if (fam_dir->req != in->wd) /*(should not happen)*/
|
|
|
|
continue;
|
|
|
|
/*(specific to use here in stat_cache.c)*/
|
|
|
|
int code = 0;
|
|
|
|
if (in->mask & (IN_ATTRIB | IN_MODIFY))
|
|
|
|
code = FAMChanged;
|
|
|
|
else if (in->mask & (IN_DELETE | IN_DELETE_SELF | IN_UNMOUNT))
|
|
|
|
code = FAMDeleted;
|
|
|
|
else if (in->mask & (IN_MOVE_SELF | IN_MOVED_FROM))
|
|
|
|
code = FAMMoved;
|
|
|
|
|
2020-11-30 01:04:19 +00:00
|
|
|
if (len) {
|
|
|
|
do { --len; } while (len && in->name[len-1] == '\0');
|
2020-11-03 05:46:50 +00:00
|
|
|
}
|
2020-11-30 01:04:19 +00:00
|
|
|
stat_cache_handle_fdevent_fn(scf, fam_dir, in->name, len, code);
|
2020-11-03 05:46:50 +00:00
|
|
|
}
|
|
|
|
} while (rd + sizeof(struct inotify_event) + NAME_MAX + 1 > sizeof(buf));
|
2020-11-04 09:20:00 +00:00
|
|
|
#elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
struct kevent kevl[256];
|
|
|
|
struct timespec t0 = { 0, 0 };
|
|
|
|
int n;
|
|
|
|
do {
|
|
|
|
n = kevent(scf->fd, NULL, 0, kevl, sizeof(kevl)/sizeof(*kevl), &t0);
|
|
|
|
if (n <= 0) break;
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
const struct kevent * const kev = kevl+i;
|
|
|
|
/* ignore events which may have been pending for
|
|
|
|
* paths recently cancelled via FAMCancelMonitor() */
|
|
|
|
int ndx = (int)(intptr_t)kev->udata;
|
|
|
|
scf->dirs = splaytree_splay(scf->dirs, ndx);
|
|
|
|
if (!scf->dirs || scf->dirs->key != ndx)
|
|
|
|
continue;
|
|
|
|
fam_dir_entry *fam_dir = scf->dirs->data;
|
|
|
|
if (fam_dir->req != (int)kev->ident)
|
|
|
|
continue;
|
|
|
|
/*(specific to use here in stat_cache.c)*/
|
|
|
|
/* note: stat_cache only monitors on directories,
|
|
|
|
* so events here are only on directories
|
|
|
|
* note: changes are treated as FAMDeleted since
|
|
|
|
* it is unknown which file in dir was changed
|
|
|
|
* This is not efficient, but this stat_cache mechanism also
|
|
|
|
* should not be used on frequently modified directories. */
|
|
|
|
int code = 0;
|
|
|
|
if (kev->fflags & (NOTE_WRITE|NOTE_ATTRIB|NOTE_EXTEND|NOTE_LINK))
|
|
|
|
code = FAMDeleted; /*(not FAMChanged; see comment above)*/
|
|
|
|
else if (kev->fflags & (NOTE_DELETE|NOTE_REVOKE))
|
|
|
|
code = FAMDeleted;
|
|
|
|
else if (kev->fflags & NOTE_RENAME)
|
|
|
|
code = FAMMoved;
|
|
|
|
if (kev->flags & EV_ERROR) /*(not expected; treat as FAMDeleted)*/
|
|
|
|
code = FAMDeleted;
|
|
|
|
stat_cache_handle_fdevent_fn(scf, fam_dir, NULL, 0, code);
|
|
|
|
}
|
|
|
|
} while (n == sizeof(kevl)/sizeof(*kevl));
|
2020-11-03 05:46:50 +00:00
|
|
|
#else
|
2019-05-04 19:54:26 +00:00
|
|
|
for (int i = 0, ndx; i || (i = FAMPending(&scf->fam)) > 0; --i) {
|
|
|
|
FAMEvent fe;
|
|
|
|
if (FAMNextEvent(&scf->fam, &fe) < 0) break;
|
|
|
|
|
|
|
|
/* ignore events which may have been pending for
|
|
|
|
* paths recently cancelled via FAMCancelMonitor() */
|
|
|
|
ndx = (int)(intptr_t)fe.userdata;
|
|
|
|
scf->dirs = splaytree_splay(scf->dirs, ndx);
|
|
|
|
if (!scf->dirs || scf->dirs->key != ndx) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fam_dir_entry *fam_dir = scf->dirs->data;
|
|
|
|
if (FAMREQUEST_GETREQNUM(&fam_dir->req)
|
|
|
|
!= FAMREQUEST_GETREQNUM(&fe.fr)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-03 05:46:50 +00:00
|
|
|
uint32_t fnlen = (fe.code != FAMCreated && fe.filename[0] != '/')
|
|
|
|
? (uint32_t)strlen(fe.filename)
|
|
|
|
: 0;
|
|
|
|
stat_cache_handle_fdevent_fn(scf, fam_dir, fe.filename, fnlen, fe.code);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (fnlen) {
|
2021-06-06 08:43:45 +00:00
|
|
|
buffer * const n = &fam_dir->name;
|
2019-05-04 19:54:26 +00:00
|
|
|
fam_dir_entry *fam_link;
|
2020-11-03 05:46:50 +00:00
|
|
|
uint32_t len;
|
|
|
|
switch (code) {
|
2019-05-04 19:54:26 +00:00
|
|
|
case FAMCreated:
|
|
|
|
/* file created in monitored dir modifies dir and
|
|
|
|
* we should get a separate FAMChanged event for dir.
|
|
|
|
* Therefore, ignore file FAMCreated event here.
|
|
|
|
* Also, if FAMNoExists() is used, might get spurious
|
|
|
|
* FAMCreated events as changes are made e.g. in monitored
|
|
|
|
* sub-sub-sub dirs and the library discovers new (already
|
|
|
|
* existing) dir entries */
|
2020-11-03 05:46:50 +00:00
|
|
|
return;
|
2019-05-04 19:54:26 +00:00
|
|
|
case FAMChanged:
|
|
|
|
/* file changed in monitored dir does not modify dir */
|
|
|
|
case FAMDeleted:
|
|
|
|
case FAMMoved:
|
|
|
|
/* file deleted or moved in monitored dir modifies dir,
|
|
|
|
* but FAM provides separate notification for that */
|
|
|
|
|
|
|
|
/* temporarily append filename to dir in fam_dir->name to
|
|
|
|
* construct path, then delete stat_cache entry (if any)*/
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
len = buffer_clen(n);
|
2020-11-27 01:49:29 +00:00
|
|
|
buffer_append_path_len(n, fn, fnlen);
|
2019-05-04 19:54:26 +00:00
|
|
|
/* (alternatively, could chose to stat() and update)*/
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
stat_cache_invalidate_entry(BUF_PTR_LEN(n));
|
2019-05-04 19:54:26 +00:00
|
|
|
|
|
|
|
fam_link = /*(check if might be symlink to monitored dir)*/
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
stat_cache_sptree_find(&scf->dirs, BUF_PTR_LEN(n));
|
2021-06-06 08:43:45 +00:00
|
|
|
if (fam_link && !buffer_is_equal(&fam_link->name, n))
|
2019-05-04 19:54:26 +00:00
|
|
|
fam_link = NULL;
|
|
|
|
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
buffer_truncate(n, len);
|
2019-05-04 19:54:26 +00:00
|
|
|
|
|
|
|
if (fam_link) {
|
|
|
|
/* replaced symlink changes containing dir */
|
2021-06-06 08:43:45 +00:00
|
|
|
stat_cache_invalidate_entry(n->ptr, len);
|
2019-05-04 19:54:26 +00:00
|
|
|
/* handle symlink to dir as deleted dir below */
|
2020-11-03 05:46:50 +00:00
|
|
|
code = FAMDeleted;
|
2019-05-04 19:54:26 +00:00
|
|
|
fam_dir = fam_link;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-03 05:46:50 +00:00
|
|
|
return;
|
2019-05-04 19:54:26 +00:00
|
|
|
default:
|
2020-11-03 05:46:50 +00:00
|
|
|
return;
|
2019-05-04 19:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 05:46:50 +00:00
|
|
|
switch(code) {
|
2019-05-04 19:54:26 +00:00
|
|
|
case FAMChanged:
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
stat_cache_invalidate_entry(BUF_PTR_LEN(&fam_dir->name));
|
2019-05-04 19:54:26 +00:00
|
|
|
break;
|
|
|
|
case FAMDeleted:
|
|
|
|
case FAMMoved:
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
stat_cache_delete_tree(BUF_PTR_LEN(&fam_dir->name));
|
2019-05-04 19:54:26 +00:00
|
|
|
fam_dir_invalidate_node(fam_dir);
|
2019-05-08 04:54:52 +00:00
|
|
|
if (scf->dirs)
|
2021-06-06 08:43:45 +00:00
|
|
|
fam_dir_invalidate_tree(scf->dirs,
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
BUF_PTR_LEN(&fam_dir->name));
|
2019-12-05 08:16:25 +00:00
|
|
|
fam_dir_periodic_cleanup();
|
2019-05-04 19:54:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-05-04 19:46:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 03:03:51 +00:00
|
|
|
static handler_t stat_cache_handle_fdevent(void *ctx, int revent)
|
2019-05-04 19:46:50 +00:00
|
|
|
{
|
2019-12-26 02:51:20 +00:00
|
|
|
stat_cache_fam * const scf = ctx; /* sc.scf */
|
2019-05-04 19:46:50 +00:00
|
|
|
|
|
|
|
if (revent & FDEVENT_IN) {
|
2019-12-05 08:16:25 +00:00
|
|
|
stat_cache_handle_fdevent_in(scf);
|
2017-10-29 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 17:01:09 +00:00
|
|
|
if (revent & (FDEVENT_HUP|FDEVENT_RDHUP)) {
|
2017-10-29 21:01:54 +00:00
|
|
|
/* fam closed the connection */
|
2019-12-26 02:51:20 +00:00
|
|
|
log_error(scf->errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"FAM connection closed; disabling stat_cache.");
|
2019-09-07 16:23:20 +00:00
|
|
|
/* (although effectively STAT_CACHE_ENGINE_NONE,
|
|
|
|
* do not change here so that periodic jobs clean up memory)*/
|
2019-12-05 08:16:25 +00:00
|
|
|
/*sc.stat_cache_engine = STAT_CACHE_ENGINE_NONE; */
|
2019-12-26 02:51:20 +00:00
|
|
|
fdevent_fdnode_event_del(scf->ev, scf->fdn);
|
|
|
|
fdevent_unregister(scf->ev, scf->fd);
|
2019-03-01 04:58:49 +00:00
|
|
|
scf->fdn = NULL;
|
2017-10-29 21:01:54 +00:00
|
|
|
|
|
|
|
FAMClose(&scf->fam);
|
2019-03-01 04:58:49 +00:00
|
|
|
scf->fd = -1;
|
2017-10-29 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2019-12-26 02:51:20 +00:00
|
|
|
static stat_cache_fam * stat_cache_init_fam(fdevents *ev, log_error_st *errh) {
|
2017-10-29 21:01:54 +00:00
|
|
|
stat_cache_fam *scf = calloc(1, sizeof(*scf));
|
2019-05-04 19:41:27 +00:00
|
|
|
force_assert(scf);
|
2019-03-01 04:58:49 +00:00
|
|
|
scf->fd = -1;
|
2019-12-26 02:51:20 +00:00
|
|
|
scf->ev = ev;
|
|
|
|
scf->errh = errh;
|
2017-10-29 21:01:54 +00:00
|
|
|
|
2020-11-03 05:46:50 +00:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
scf->fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
|
|
|
|
if (scf->fd < 0) {
|
|
|
|
log_perror(errh, __FILE__, __LINE__, "inotify_init1()");
|
2020-11-30 01:04:19 +00:00
|
|
|
free(scf);
|
2020-11-03 05:46:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-04 09:20:00 +00:00
|
|
|
#elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
scf->fd = kqueue1(O_NONBLOCK|O_CLOEXEC|O_NOSIGPIPE);
|
|
|
|
#else
|
|
|
|
scf->fd = kqueue();
|
|
|
|
if (scf->fd >= 0) fdevent_setfd_cloexec(scf->fd);
|
|
|
|
#endif
|
|
|
|
if (scf->fd < 0) {
|
|
|
|
log_perror(errh, __FILE__, __LINE__, "kqueue()");
|
2020-11-30 01:04:19 +00:00
|
|
|
free(scf);
|
2020-11-04 09:20:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-03 05:46:50 +00:00
|
|
|
#else
|
2017-10-29 21:01:54 +00:00
|
|
|
/* setup FAM */
|
|
|
|
if (0 != FAMOpen2(&scf->fam, "lighttpd")) {
|
2019-12-26 02:51:20 +00:00
|
|
|
log_error(errh, __FILE__, __LINE__,
|
2020-02-22 22:24:12 +00:00
|
|
|
"could not open a fam connection, dying.");
|
2020-11-30 01:04:19 +00:00
|
|
|
free(scf);
|
2017-10-29 21:01:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_FAMNOEXISTS
|
2020-08-10 19:46:47 +00:00
|
|
|
#ifdef LIGHTTPD_STATIC
|
2017-10-29 21:01:54 +00:00
|
|
|
FAMNoExists(&scf->fam);
|
2020-08-10 19:46:47 +00:00
|
|
|
#else
|
|
|
|
int (*FAMNoExists_fn)(FAMConnection *);
|
|
|
|
FAMNoExists_fn =
|
|
|
|
(int (*)(FAMConnection *))(intptr_t)dlsym(RTLD_DEFAULT,"FAMNoExists");
|
|
|
|
if (FAMNoExists_fn) FAMNoExists_fn(&scf->fam);
|
|
|
|
#endif
|
2017-10-29 21:01:54 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-01 04:58:49 +00:00
|
|
|
scf->fd = FAMCONNECTION_GETFD(&scf->fam);
|
|
|
|
fdevent_setfd_cloexec(scf->fd);
|
2020-11-03 05:46:50 +00:00
|
|
|
#endif
|
2019-12-26 02:51:20 +00:00
|
|
|
scf->fdn = fdevent_register(scf->ev, scf->fd, stat_cache_handle_fdevent, scf);
|
|
|
|
fdevent_fdnode_event_set(scf->ev, scf->fdn, FDEVENT_IN | FDEVENT_RDHUP);
|
2017-10-29 21:01:54 +00:00
|
|
|
|
|
|
|
return scf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stat_cache_free_fam(stat_cache_fam *scf) {
|
|
|
|
if (NULL == scf) return;
|
|
|
|
|
2020-11-03 05:46:50 +00:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
while (scf->wds) {
|
|
|
|
splay_tree *node = scf->wds;
|
|
|
|
scf->wds = splaytree_delete(scf->wds, node->key);
|
|
|
|
}
|
2020-11-04 09:20:00 +00:00
|
|
|
#elif defined HAVE_SYS_EVENT_H && defined HAVE_KQUEUE
|
|
|
|
/*(quicker cleanup to close kqueue() before cancel per entry)*/
|
|
|
|
close(scf->fd);
|
|
|
|
scf->fd = -1;
|
2020-11-03 05:46:50 +00:00
|
|
|
#endif
|
2017-10-29 21:01:54 +00:00
|
|
|
while (scf->dirs) {
|
2019-05-04 19:41:27 +00:00
|
|
|
/*(skip entry invalidation and FAMCancelMonitor())*/
|
2017-10-29 21:01:54 +00:00
|
|
|
splay_tree *node = scf->dirs;
|
2019-05-04 19:41:27 +00:00
|
|
|
fam_dir_entry_free((fam_dir_entry *)node->data);
|
2017-10-29 21:01:54 +00:00
|
|
|
scf->dirs = splaytree_delete(scf->dirs, node->key);
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:58:49 +00:00
|
|
|
if (-1 != scf->fd) {
|
|
|
|
/*scf->fdn already cleaned up in fdevent_free()*/
|
2017-10-29 21:01:54 +00:00
|
|
|
FAMClose(&scf->fam);
|
2019-03-01 04:58:49 +00:00
|
|
|
/*scf->fd = -1;*/
|
2017-10-29 21:01:54 +00:00
|
|
|
}
|
|
|