path: root/src/stat_cache.c
diff options
authorGlenn Strauss <>2016-03-30 06:39:33 -0400
committerGlenn Strauss <>2016-04-18 04:27:08 -0400
commita65c57a54812fa44a8e1b6e846f69ac6e314f5d1 (patch)
treec4e56c0241feade3727d6c442851e600988fa25d /src/stat_cache.c
parente0bafab2db7a97cdbb3ec984e506987053e1556b (diff)
[core] open fd when appending file to cq (fixes #2655)
http_chunk_append_file() opens fd when appending file to chunkqueue. Defers calculation of content length until response is finished. This reduces race conditions pertaining to stat() and then (later) open(), when the result of the stat() was used for Content-Length or to generate chunked headers. Note: this does not change how lighttpd handles files that are modified in-place by another process after having been opened by lighttpd -- don't do that. This *does* improve handling of files that are frequently modified via a temporary file and then atomically renamed into place. mod_fastcgi has been modified to use http_chunk_append_file_range() with X-Sendfile2 and will open the target file multiple times if there are multiple ranges. Note: (future todo) not implemented for chunk.[ch] interfaces used by range requests in mod_staticfile or by mod_ssi. Those uses could lead to too many open fds. For mod_staticfile, limits should be put in place for max number of ranges accepted by mod_staticfile. For mod_ssi, limits would need to be placed on the maximum number of includes, and the primary SSI file split across lots of SSI directives should either copy the pieces or perhaps chunk.h could be extended to allow for an open fd to be shared across multiple chunks. Doing either of these would improve the performance of SSI since they would replace many file opens on the pieces of the SSI file around the SSI directives. x-ref: "Serving a file that is getting updated can cause an empty response or incorrect content-length error" github: Closes #49
Diffstat (limited to 'src/stat_cache.c')
1 files changed, 32 insertions, 0 deletions
diff --git a/src/stat_cache.c b/src/stat_cache.c
index fa9b7cb9..9464625f 100644
--- a/src/stat_cache.c
+++ b/src/stat_cache.c
@@ -691,6 +691,38 @@ handler_t stat_cache_get_entry(server *srv, connection *con, buffer *name, stat_
+int stat_cache_open_rdonly_fstat (server *srv, connection *con, buffer *name, struct stat *st) {
+ /*(Note: O_NOFOLLOW affects only the final path segment, the target file,
+ * not any intermediate symlinks along the path)*/
+ #ifndef O_BINARY
+ #define O_BINARY 0
+ #endif
+ #ifndef O_LARGEFILE
+ #define O_LARGEFILE 0
+ #endif
+ #ifndef O_NOCTTY
+ #define O_NOCTTY 0
+ #endif
+ #ifndef O_NONBLOCK
+ #define O_NONBLOCK 0
+ #endif
+ #ifndef O_NOFOLLOW
+ #define O_NOFOLLOW 0
+ #endif
+ const int oflags = O_BINARY | O_LARGEFILE | O_NOCTTY | O_NONBLOCK
+ | (con->conf.follow_symlink ? 0 : O_NOFOLLOW);
+ const int fd = open(name->ptr, O_RDONLY | oflags);
+ if (fd >= 0) {
+ if (0 == fstat(fd, st)) {
+ return fd;
+ } else {
+ close(fd);
+ }
+ }
+ UNUSED(srv); /*(might log_error_write(srv, ...) in the future)*/
+ return -1;
* remove stat() from cache which havn't been stat()ed for
* more than 10 seconds