Browse Source

[core] replace open() with fdevent_open_cloexec()

fdevent_open_cloexec() opens files O_BINARY for consistency,
and with O_NONBLOCK, so that the server will not block if the
target file to open is a fifo.
personal/stbuehler/ci-build
Glenn Strauss 3 years ago
parent
commit
a15f40a569
  1. 3
      src/fdevent_solaris_devpoll.c
  2. 12
      src/mod_compress.c
  3. 16
      src/mod_dirlisting.c
  4. 9
      src/mod_ssi.c
  5. 27
      src/mod_webdav.c
  6. 7
      src/stat_cache.c

3
src/fdevent_solaris_devpoll.c

@ -63,8 +63,7 @@ __attribute_cold__
int fdevent_solaris_devpoll_reset(fdevents *ev) {
/* a forked process does only inherit the filedescriptor,
* but every operation on the device will lead to a EACCES */
if ((ev->devpoll_fd = open("/dev/poll", O_RDWR)) < 0) return -1;
fdevent_setfd_cloexec(ev->devpoll_fd);
if ((ev->devpoll_fd = fdevent_open_cloexec("/dev/poll", 1, O_RDWR, 0)) < 0) return -1;
return 0;
}

12
src/mod_compress.c

@ -3,6 +3,7 @@
#include "base.h"
#include "log.h"
#include "buffer.h"
#include "fdevent.h"
#include "http_header.h"
#include "response.h"
#include "stat_cache.h"
@ -23,10 +24,6 @@
#include <errno.h>
#include <time.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if defined HAVE_ZLIB_H && defined HAVE_LIBZ
# define USE_ZLIB
# include <zlib.h>
@ -540,7 +537,8 @@ static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, bu
return -1;
}
if (-1 == (ofd = open(p->ofn->ptr, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))) {
/*(note: follows symlinks in protected cache dir)*/
if (-1 == (ofd = fdevent_open_cloexec(p->ofn->ptr, 1, O_WRONLY | O_CREAT | O_EXCL, 0600))) {
if (errno == EEXIST) {
return -1; /* cache file being created */
}
@ -552,7 +550,7 @@ static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, bu
#if 0
log_error_write(srv, __FILE__, __LINE__, "bs", p->ofn, "compress-cache miss");
#endif
if (-1 == (ifd = open(filename, O_RDONLY | O_BINARY))) {
if (-1 == (ifd = fdevent_open_cloexec(filename, con->conf.follow_symlink, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno));
close(ofd);
@ -687,7 +685,7 @@ static int deflate_file_to_buffer(server *srv, connection *con, plugin_data *p,
return -1;
}
if (-1 == (ifd = open(fn->ptr, O_RDONLY | O_BINARY))) {
if (-1 == (ifd = fdevent_open_cloexec(fn->ptr, con->conf.follow_symlink, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno));
return -1;

16
src/mod_dirlisting.c

@ -3,6 +3,7 @@
#include "base.h"
#include "log.h"
#include "buffer.h"
#include "fdevent.h"
#include "http_header.h"
#include "plugin.h"
@ -489,15 +490,8 @@ static int http_list_directory_sizefmt(char *buf, size_t bufsz, off_t size) {
return buflen + 3;
}
/* don't want to block when open()ing a fifo */
#if defined(O_NONBLOCK)
# define FIFO_NONBLOCK O_NONBLOCK
#else
# define FIFO_NONBLOCK 0
#endif
static void http_list_directory_include_file(buffer *out, buffer *path, const char *classname, int encode) {
int fd = open(path->ptr, O_RDONLY | FIFO_NONBLOCK);
static void http_list_directory_include_file(buffer *out, int symlinks, buffer *path, const char *classname, int encode) {
int fd = fdevent_open_cloexec(path->ptr, symlinks, O_RDONLY, 0);
ssize_t rd;
char buf[8192];
@ -802,7 +796,7 @@ static void http_list_directory_header(server *srv, connection *con, plugin_data
hb = p->tmp_buf;
}
http_list_directory_include_file(out, hb, "header", p->conf.encode_header);
http_list_directory_include_file(out, con->conf.follow_symlink, hb, "header", p->conf.encode_header);
}
buffer_append_string_len(out, CONST_STR_LEN("<h2>Index of "));
@ -852,7 +846,7 @@ static void http_list_directory_footer(server *srv, connection *con, plugin_data
rb = p->tmp_buf;
}
http_list_directory_include_file(out, rb, "readme", p->conf.encode_readme);
http_list_directory_include_file(out, con->conf.follow_symlink, rb, "readme", p->conf.encode_readme);
}
if(p->conf.auto_layout) {

9
src/mod_ssi.c

@ -1168,15 +1168,8 @@ static void mod_ssi_read_fd(server *srv, connection *con, handler_ctx *p, struct
}
/* don't want to block when open()ing a fifo */
#if defined(O_NONBLOCK)
# define FIFO_NONBLOCK O_NONBLOCK
#else
# define FIFO_NONBLOCK 0
#endif
static int mod_ssi_process_file(server *srv, connection *con, handler_ctx *p, struct stat *st) {
int fd = open(con->physical.path->ptr, O_RDONLY | FIFO_NONBLOCK);
int fd = fdevent_open_cloexec(con->physical.path->ptr, con->conf.follow_symlink, O_RDONLY, 0);
if (-1 == fd) {
log_error_write(srv, __FILE__, __LINE__, "SsB", "open(): ",
strerror(errno), con->physical.path);

27
src/mod_webdav.c

@ -659,17 +659,6 @@ static int webdav_delete_dir(server *srv, connection *con, handler_ctx *hctx, ph
return have_multi_status;
}
/* don't want to block when open()ing a fifo */
#if defined(O_NONBLOCK)
# define FIFO_NONBLOCK O_NONBLOCK
#else
# define FIFO_NONBLOCK 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
static int webdav_copy_file(server *srv, connection *con, handler_ctx *hctx, physical *src, physical *dst, int overwrite) {
char *data;
ssize_t rd, wr, offset;
@ -677,11 +666,11 @@ static int webdav_copy_file(server *srv, connection *con, handler_ctx *hctx, phy
UNUSED(srv);
UNUSED(con);
if (-1 == (ifd = open(src->path->ptr, O_RDONLY | O_BINARY | FIFO_NONBLOCK))) {
if (-1 == (ifd = fdevent_open_cloexec(src->path->ptr, 0, O_RDONLY, 0))) {
return 403;
}
if (-1 == (ofd = open(dst->path->ptr, O_WRONLY|O_TRUNC|O_CREAT|(overwrite ? 0 : O_EXCL), WEBDAV_FILE_MODE))) {
if (-1 == (ofd = fdevent_open_cloexec(dst->path->ptr, 0, O_WRONLY|O_TRUNC|O_CREAT|(overwrite ? 0 : O_EXCL), WEBDAV_FILE_MODE))) {
/* opening the destination failed for some reason */
switch(errno) {
case EEXIST:
@ -1128,7 +1117,7 @@ static int webdav_parse_chunkqueue(server *srv, connection *con, handler_ctx *hc
data = c->file.mmap.start + c->offset;
} else {
if (-1 == c->file.fd && /* open the file if not already open */
-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, con->conf.follow_symlink, O_RDONLY, 0))) {
-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, 1, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
return -1;
@ -1781,7 +1770,7 @@ static handler_t mod_webdav_put(server *srv, connection *con, plugin_data *p, ha
return HANDLER_FINISHED;
}
if (-1 == (fd = open(con->physical.path->ptr, O_WRONLY, WEBDAV_FILE_MODE))) {
if (-1 == (fd = fdevent_open_cloexec(con->physical.path->ptr, 0, O_WRONLY, WEBDAV_FILE_MODE))) {
switch (errno) {
case ENOENT:
con->http_status = 404; /* not found */
@ -1805,9 +1794,9 @@ static handler_t mod_webdav_put(server *srv, connection *con, plugin_data *p, ha
/* take what we have in the request-body and write it to a file */
/* if the file doesn't exist, create it */
if (-1 == (fd = open(con->physical.path->ptr, O_WRONLY|O_TRUNC, WEBDAV_FILE_MODE))) {
if (-1 == (fd = fdevent_open_cloexec(con->physical.path->ptr, 0, O_WRONLY|O_TRUNC, WEBDAV_FILE_MODE))) {
if (errno != ENOENT ||
-1 == (fd = open(con->physical.path->ptr, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, WEBDAV_FILE_MODE))) {
-1 == (fd = fdevent_open_cloexec(con->physical.path->ptr, 0, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, WEBDAV_FILE_MODE))) {
/* we can't open the file */
con->http_status = 403;
@ -1838,7 +1827,7 @@ static handler_t mod_webdav_put(server *srv, connection *con, plugin_data *p, ha
data = c->file.mmap.start + c->offset;
} else {
if (-1 == c->file.fd && /* open the file if not already open */
-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, con->conf.follow_symlink, O_RDONLY, 0))) {
-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, 1, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
close(fd);
return HANDLER_ERROR;
@ -2429,7 +2418,7 @@ static handler_t mod_webdav_lock(server *srv, connection *con, plugin_data *p, h
if (0 != stat(con->physical.path->ptr, &st)) {
if (errno == ENOENT) {
int fd = open(con->physical.path->ptr, O_WRONLY|O_CREAT|O_APPEND|O_BINARY|FIFO_NONBLOCK, WEBDAV_FILE_MODE);
int fd = fdevent_open_cloexec(con->physical.path->ptr, 0, O_WRONLY|O_CREAT|O_APPEND, WEBDAV_FILE_MODE);
if (fd >= 0) {
close(fd);
created = 1;

7
src/stat_cache.c

@ -674,7 +674,12 @@ handler_t stat_cache_get_entry(server *srv, connection *con, buffer *name, stat_
}
/* try to open the file to check if we can read it */
if (-1 == (fd = open(name->ptr, O_RDONLY))) {
#ifdef O_NONBLOCK
fd = open(name->ptr, O_RDONLY | O_NONBLOCK, 0);
#else
fd = open(name->ptr, O_RDONLY, 0);
#endif
if (-1 == fd) {
return HANDLER_ERROR;
}
close(fd);

Loading…
Cancel
Save