From 5308adbd7cdc2d521b7b87af2aeec5f25f7f5074 Mon Sep 17 00:00:00 2001 From: Emil Renner Berthing Date: Sun, 27 Jan 2013 17:28:32 +0100 Subject: io: set FD_CLOEXEC on all new filedescriptors --- lem/io/core.c | 15 +++++++++++---- lem/io/file.c | 3 +-- lem/io/server.c | 30 ++++++++++++++++++++---------- lem/io/stream.c | 6 +++--- lem/io/tcp.c | 40 +++++++++++++++++++++++----------------- lem/io/unix.c | 41 ++++++++++++++++++++++++----------------- 6 files changed, 82 insertions(+), 53 deletions(-) diff --git a/lem/io/core.c b/lem/io/core.c index 459fa2a..6f7aa8b 100644 --- a/lem/io/core.c +++ b/lem/io/core.c @@ -121,10 +121,17 @@ io_open_work(struct lem_async *a) int fd; struct stat st; - fd = open(o->path, o->flags | O_NONBLOCK, - o->fd >= 0 ? o->fd : + fd = open(o->path, o->flags +#ifdef O_CLOEXEC + | O_CLOEXEC +#endif + | O_NONBLOCK, o->fd >= 0 ? o->fd : S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - if (fd < 0) { + if (fd < 0 +#ifndef O_CLOXEC + || fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 +#endif + ) { o->flags = -errno; return; } @@ -249,7 +256,7 @@ push_stdstream(lua_State *L, int fd) struct stream *s; /* make the socket non-blocking */ - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) + if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) luaL_error(L, "error making fd %d non-blocking: %s", fd, strerror(errno)); diff --git a/lem/io/file.c b/lem/io/file.c index 30515a0..d820363 100644 --- a/lem/io/file.c +++ b/lem/io/file.c @@ -394,9 +394,8 @@ file_lock_work(struct lem_async *a) .l_start = f->lock.start, .l_len = f->lock.len, }; - int ret = fcntl(f->fd, F_SETLK, &fl); - if (ret == -1) + if (fcntl(f->fd, F_SETLK, &fl) == -1) f->ret = errno; else f->ret = 0; diff --git a/lem/io/server.c b/lem/io/server.c index 2240254..08c4d54 100644 --- a/lem/io/server.c +++ b/lem/io/server.c @@ -96,7 +96,11 @@ server_interrupt(lua_State *T) static int server__accept(lua_State *T, struct ev_io *w, int mt) { +#ifdef SOCK_CLOEXEC + int sock = accept4(w->fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK); +#else int sock = accept(w->fd, NULL, NULL); +#endif if (sock < 0) { switch (errno) { @@ -114,16 +118,17 @@ server__accept(lua_State *T, struct ev_io *w, int mt) strerror(errno)); return 2; } - - /* make the socket non-blocking */ - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { +#ifndef SOCK_CLOEXEC + /* set FD_CLOEXEC and make the socket non-blocking */ + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 || + fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { close(sock); lua_pushnil(T); - lua_pushfstring(T, "error making socket non-blocking: %s", + lua_pushfstring(T, "error setting socket flags: %s", strerror(errno)); return 2; } - +#endif stream_new(T, sock, mt); return 1; } @@ -187,7 +192,11 @@ server_autospawn_cb(EV_P_ struct ev_io *w, int revents) (void)revents; /* dequeue the incoming connection */ +#ifdef SOCK_CLOEXEC + sock = accept4(w->fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK); +#else sock = accept(w->fd, NULL, NULL); +#endif if (sock < 0) { switch (errno) { case EAGAIN: case EINTR: case ECONNABORTED: @@ -204,16 +213,17 @@ server_autospawn_cb(EV_P_ struct ev_io *w, int revents) strerror(errno)); goto error; } - - /* make the socket non-blocking */ - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { +#ifndef SOCK_CLOEXEC + /* set FD_CLOEXEC and make the socket non-blocking */ + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 || + fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { close(sock); lua_pushnil(T); - lua_pushfstring(T, "error making socket non-blocking: %s", + lua_pushfstring(T, "error setting socket flags: %s", strerror(errno)); goto error; } - +#endif S = lem_newthread(); /* copy handler function */ diff --git a/lem/io/stream.c b/lem/io/stream.c index 1c37eaa..b2b8f00 100644 --- a/lem/io/stream.c +++ b/lem/io/stream.c @@ -422,7 +422,7 @@ stream_sendfile_work(struct lem_async *a) struct stream *s = sf->s; /* make socket blocking */ - if (fcntl(s->w.fd, F_SETFL, 0)) { + if (fcntl(s->w.fd, F_SETFL, 0) == -1) { sf->ret = errno; return; } @@ -461,7 +461,7 @@ stream_sendfile_work(struct lem_async *a) #endif /* make socket non-blocking again */ - if (fcntl(s->w.fd, F_SETFL, O_NONBLOCK)) { + if (fcntl(s->w.fd, F_SETFL, O_NONBLOCK) == -1) { sf->ret = errno; return; } @@ -581,7 +581,7 @@ stream_popen(lua_State *T) } /* make the pipe non-blocking */ - if (fcntl(fd[0], F_SETFL, O_NONBLOCK) < 0) { + if (fcntl(fd[0], F_SETFL, O_NONBLOCK) == -1) { err = errno; close(fd[0]); return io_strerror(T, err); diff --git a/lem/io/tcp.c b/lem/io/tcp.c index 14e0b82..20c4ab6 100644 --- a/lem/io/tcp.c +++ b/lem/io/tcp.c @@ -60,10 +60,17 @@ tcp_connect_work(struct lem_async *a) /* try the addresses in the order returned */ for (addr = result; addr; addr = addr->ai_next) { sock = socket(addr->ai_family, +#ifdef SOCK_CLOEXEC + SOCK_CLOEXEC | +#endif addr->ai_socktype, addr->ai_protocol); lem_debug("addr->ai_family = %d, sock = %d", addr->ai_family, sock); - if (sock < 0) { + if (sock < 0 +#ifndef SOCK_CLOEXEC + || fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 +#endif + ) { int err = errno; if (err == EAFNOSUPPORT || err == EPROTONOSUPPORT) @@ -81,8 +88,8 @@ tcp_connect_work(struct lem_async *a) } /* make the socket non-blocking */ - if (fcntl(sock, F_SETFL, O_NONBLOCK)) { - g->sock = -3; + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { + g->sock = -2; g->err = errno; goto error; } @@ -91,7 +98,7 @@ tcp_connect_work(struct lem_async *a) goto out; } - g->sock = -4; + g->sock = -3; goto out; error: @@ -127,10 +134,6 @@ tcp_connect_reap(struct lem_async *a) strerror(g->err)); break; case 3: - lua_pushfstring(T, "error making socket non-blocking: %s", - strerror(g->err)); - break; - case 4: lua_pushfstring(T, "error connecting to '%s:%s'", g->node, g->service); break; @@ -185,9 +188,17 @@ tcp_listen_work(struct lem_async *a) } /* create the TCP socket */ - sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + sock = socket(addr->ai_family, +#ifdef SOCK_CLOEXEC + SOCK_CLOEXEC | +#endif + addr->ai_socktype, addr->ai_protocol); lem_debug("addr->ai_family = %d, sock = %d", addr->ai_family, sock); - if (sock < 0) { + if (sock < 0 +#ifndef SOCK_CLOEXEC + || fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 +#endif + ) { g->sock = -2; g->err = errno; goto out; @@ -216,8 +227,8 @@ tcp_listen_work(struct lem_async *a) } /* make the socket non-blocking */ - if (fcntl(sock, F_SETFL, O_NONBLOCK)) { - g->sock = -5; + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { + g->sock = -2; g->err = errno; goto error; } @@ -277,11 +288,6 @@ tcp_listen_reap(struct lem_async *a) lua_pushfstring(T, "error listening on '%s:%s': %s", g->node, g->service, strerror(g->err)); break; - - case 5: - lua_pushfstring(T, "error making socket non-blocking: %s", - strerror(g->err)); - break; } free(g); lem_queue(T, 2); diff --git a/lem/io/unix.c b/lem/io/unix.c index 25e1b00..0b18432 100644 --- a/lem/io/unix.c +++ b/lem/io/unix.c @@ -31,8 +31,16 @@ unix_connect_work(struct lem_async *a) struct sockaddr_un addr; int sock; - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) { + sock = socket(AF_UNIX, +#ifdef SOCK_CLOEXEC + SOCK_CLOEXEC | +#endif + SOCK_STREAM, 0); + if (sock < 0 +#ifndef SOCK_CLOEXEC + || fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 +#endif + ) { u->sock = -1; u->err = errno; return; @@ -49,8 +57,8 @@ unix_connect_work(struct lem_async *a) } /* make the socket non-blocking */ - if (fcntl(sock, F_SETFL, O_NONBLOCK)) { - u->sock = -3; + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { + u->sock = -1; u->err = errno; goto error; } @@ -87,10 +95,6 @@ unix_connect_reap(struct lem_async *a) lua_pushfstring(T, "error connecting to '%s': %s", u->path, strerror(u->err)); break; - case 3: - lua_pushfstring(T, "error making socket non-blocking: %s", - strerror(u->err)); - break; } lem_queue(T, 2); free(u); @@ -123,8 +127,16 @@ unix_listen_work(struct lem_async *a) struct sockaddr_un addr; int sock; - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) { + sock = socket(AF_UNIX, +#ifdef SOCK_CLOEXEC + SOCK_CLOEXEC | +#endif + SOCK_STREAM, 0); + if (sock < 0 +#ifndef SOCK_CLOEXEC + || fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 +#endif + ) { u->sock = -1; u->err = errno; return; @@ -153,8 +165,8 @@ unix_listen_work(struct lem_async *a) } /* make the socket non-blocking */ - if (fcntl(sock, F_SETFL, O_NONBLOCK)) { - u->sock = -5; + if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) { + u->sock = -1; u->err = errno; goto error; } @@ -209,11 +221,6 @@ unix_listen_reap(struct lem_async *a) lua_pushfstring(T, "error listening on '%s': %s", u->path, strerror(u->err)); break; - - case 5: - lua_pushfstring(T, "error making socket non-blocking: %s", - strerror(u->err)); - break; } lem_queue(T, 2); free(u); -- cgit v1.2.1