Browse Source

assert many previously unchecked return values, handle some explicitly, remove FD_CLOEXEC in worker - mustn't fork

personal/stbuehler/wip
Stefan Bühler 8 years ago
parent
commit
acd2967534
  1. 1
      include/lighttpd/utils.h
  2. 4
      src/common/memcached.c
  3. 33
      src/common/utils.c
  4. 6
      src/main/backends.c
  5. 3
      src/main/chunk.c
  6. 4
      src/main/plugin_core.c
  7. 2
      src/main/server.c
  8. 2
      src/main/virtualrequest.c
  9. 6
      src/modules/mod_cache_disk_etag.c
  10. 4
      src/modules/mod_flv.c
  11. 6
      src/modules/mod_status.c

1
include/lighttpd/utils.h

@ -21,6 +21,7 @@ LI_API void li_print_backtrace_stderr(void);
LI_API void li_fd_init(int fd);
LI_API void li_fd_no_block(int fd);
LI_API void li_fd_block(int fd);
LI_API void li_fd_close_on_exec(int fd);
#ifndef LIGHTY_OS_WINDOWS
/* return -2 for EAGAIN, -1 for some other error, 0 for success */

4
src/common/memcached.c

@ -330,7 +330,9 @@ static void memcached_connect(liMemcachedCon *con) {
int err;
len = sizeof(err);
#ifdef SO_ERROR
getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&err, &len);
if (-1 == getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&err, &len)) {
err = errno;
}
#else
{
char ch;

33
src/common/utils.c

@ -81,7 +81,10 @@ void li_fatal(const char *filename, unsigned int line, const char *function, con
void li_fd_no_block(int fd) {
#ifdef O_NONBLOCK
fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR);
int flags = fcntl(fd, F_GETFL, 0);
if (0 == (flags & O_NONBLOCK)) {
LI_FORCE_ASSERT(-1 != fcntl(fd, F_SETFL, flags | O_NONBLOCK));
}
#elif defined _WIN32
int i = 1;
ioctlsocket(fd, FIONBIO, &i);
@ -92,7 +95,10 @@ void li_fd_no_block(int fd) {
void li_fd_block(int fd) {
#ifdef O_NONBLOCK
fcntl(fd, F_SETFL, O_RDWR);
int flags = fcntl(fd, F_GETFL, 0);
if (0 != (flags & O_NONBLOCK)) {
LI_FORCE_ASSERT(-1 != fcntl(fd, F_SETFL, flags & ~O_NONBLOCK));
}
#elif defined _WIN32
int i = 0;
ioctlsocket(fd, FIONBIO, &i);
@ -101,11 +107,20 @@ void li_fd_block(int fd) {
#endif
}
void li_fd_init(int fd) {
void li_fd_close_on_exec(int fd) {
#ifdef FD_CLOEXEC
/* close fd on exec (cgi) */
fcntl(fd, F_SETFD, FD_CLOEXEC);
/* close fd on exec */
int flags = fcntl(fd, F_GETFD, 0);
if (0 == (flags & FD_CLOEXEC)) {
LI_FORCE_ASSERT(-1 != fcntl(fd, F_SETFD, flags | FD_CLOEXEC));
}
#else
UNUSED(fd);
#endif
}
void li_fd_init(int fd) {
li_fd_close_on_exec(fd);
li_fd_no_block(fd);
}
@ -679,7 +694,9 @@ liSocketAddress li_sockaddr_local_from_socket(gint fd) {
if (l <= sizeof(sa)) {
memcpy(saddr.addr, &sa.plain, l);
} else {
getsockname(fd, (struct sockaddr*) saddr.addr, &l);
if (-1 == getsockname(fd, (struct sockaddr*) saddr.addr, &l)) {
li_sockaddr_clear(&saddr);
}
}
return saddr;
@ -699,7 +716,9 @@ liSocketAddress li_sockaddr_remote_from_socket(gint fd) {
if (l <= sizeof(sa)) {
memcpy(saddr.addr, &sa.plain, l);
} else {
getpeername(fd, (struct sockaddr*) saddr.addr, &l);
if (-1 == getpeername(fd, (struct sockaddr*) saddr.addr, &l)) {
li_sockaddr_clear(&saddr);
}
}
return saddr;

6
src/main/backends.c

@ -337,7 +337,9 @@ static void backend_con_watch_connect_cb(liEventBase *watcher, int events) {
int err;
len = sizeof(err);
#ifdef SO_ERROR
getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&err, &len);
if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&err, &len)) {
err = errno;
}
#else
{
char ch;
@ -409,7 +411,7 @@ static gboolean S_backend_connection_connect(liBackendWorkerPool *wpool) {
ERROR(srv, "Couldn't open socket: %s", g_strerror(errno));
return FALSE;
}
li_fd_init(fd);
li_fd_no_block(fd);
if (-1 == connect(fd, &config->sock_addr.addr->plain, config->sock_addr.len)) {
switch (errno) {

3
src/main/chunk.c

@ -63,9 +63,6 @@ liHandlerResult li_chunkfile_open(liChunkFile *cf, GError **err) {
g_set_error(err, LI_CHUNK_ERROR, 0, "li_chunkfile_open: Couldn't open file '%s': %s", GSTR_SAFE_STR(cf->name), g_strerror(errno));
return LI_HANDLER_ERROR;
}
#ifdef FD_CLOEXEC
fcntl(cf->fd, F_SETFD, FD_CLOEXEC);
#endif
#if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_SEQUENTIAL)
/* tell the kernel that we want to stream the file */
if (-1 == posix_fadvise(cf->fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {

4
src/main/plugin_core.c

@ -642,10 +642,6 @@ static liHandlerResult core_handle_static(liVRequest *vr, gpointer param, gpoint
liChunkFile *cf;
static const GString default_mime_str = { CONST_STR_LEN("application/octet-stream"), 0 };
#ifdef FD_CLOEXEC
fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif
if (!li_vrequest_handle_direct(vr)) {
close(fd);
return LI_HANDLER_ERROR;

2
src/main/server.c

@ -21,7 +21,7 @@ static liServerSocket* server_socket_new(liServer *srv, int fd) {
sock->local_addr = li_sockaddr_local_from_socket(fd);
sock->refcount = 1;
li_fd_init(fd);
li_fd_no_block(fd);
li_event_io_init(&srv->main_worker->loop, &sock->watcher, li_server_listen_cb, fd, LI_EV_READ);
return sock;
}

2
src/main/virtualrequest.c

@ -430,7 +430,7 @@ static liHandlerResult vrequest_do_handle_actions(liVRequest *vr) {
case LI_HANDLER_GO_ON:
if (vr->state == LI_VRS_HANDLE_REQUEST_HEADERS) {
/* request not handled */
li_vrequest_handle_direct(vr);
LI_FORCE_ASSERT(li_vrequest_handle_direct(vr));
if (vr->request.http_method == LI_HTTP_METHOD_OPTIONS) {
vr->response.http_status = 200;
li_http_header_append(vr->response.headers, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));

6
src/modules/mod_cache_disk_etag.c

@ -77,9 +77,6 @@ static gboolean cache_etag_file_start(liVRequest *vr, cache_etag_file *cfile) {
VR_ERROR(vr, "Couldn't create cache tempfile '%s': %s", cfile->tmpfilename->str, g_strerror(errno));
return FALSE;
}
#ifdef FD_CLOEXEC
fcntl(cfile->fd, F_SETFD, FD_CLOEXEC);
#endif
return TRUE;
}
@ -272,9 +269,6 @@ static liHandlerResult cache_etag_handle(liVRequest *vr, gpointer param, gpointe
return LI_HANDLER_GO_ON; /* no caching */
}
cfile->hit_fd = fd;
#ifdef FD_CLOEXEC
fcntl(cfile->hit_fd, F_SETFD, FD_CLOEXEC);
#endif
if (CORE_OPTION(LI_CORE_OPTION_DEBUG_REQUEST_HANDLING).boolean) {
VR_DEBUG(vr, "cache hit for '%s'", vr->request.uri.path->str);
}

4
src/modules/mod_flv.c

@ -74,10 +74,6 @@ static liHandlerResult flv(liVRequest *vr, gpointer param, gpointer *context) {
} else {
liChunkFile *cf;
#ifdef FD_CLOEXEC
fcntl(fd, F_SETFD, FD_CLOEXEC);
#endif
if (!li_vrequest_handle_direct(vr)) {
close(fd);
return LI_HANDLER_ERROR;

6
src/modules/mod_status.c

@ -508,7 +508,7 @@ static void status_collect_cb(gpointer cbdata, gpointer fdata, GPtrArray *result
html = status_info_full(vr, p, short_info, result, uptime, &totals, total_connections, &connection_count[0]);
}
li_vrequest_handle_direct(vr);
LI_FORCE_ASSERT(li_vrequest_handle_direct(vr));
vr->response.http_status = 200;
li_chunkqueue_append_string(vr->direct_out, html);
li_vrequest_joblist_append(vr);
@ -954,7 +954,7 @@ static liHandlerResult status_info(liVRequest *vr, gpointer _param, gpointer *co
return status_info_runtime(vr, param->p);
} else {
vr->response.http_status = 403;
li_vrequest_handle_direct(vr);
LI_FORCE_ASSERT(li_vrequest_handle_direct(vr));
}
}
@ -1233,7 +1233,7 @@ static liHandlerResult status_info_runtime(liVRequest *vr, liPlugin *p) {
"</html>\n"
));
li_vrequest_handle_direct(vr);
LI_FORCE_ASSERT(li_vrequest_handle_direct(vr));
li_chunkqueue_append_string(vr->direct_out, html);
li_http_header_overwrite(vr->response.headers, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html; charset=utf-8"));

Loading…
Cancel
Save