Commit 261d4fba authored by Niels Möller's avatar Niels Möller

Set the close-on-exec flag on all fd:s.

Rev: src/io.c:1.21
Rev: src/io.h:1.15
parent 58f38a1a
...@@ -99,7 +99,11 @@ static void close_fd(struct io_fd *fd) ...@@ -99,7 +99,11 @@ static void close_fd(struct io_fd *fd)
(void) CLOSE_CALLBACK(fd->close_callback, fd->close_reason); (void) CLOSE_CALLBACK(fd->close_callback, fd->close_reason);
close(fd->fd); close(fd->fd);
/* Make sure writing to the buffer fails. */
if (fd->buffer)
write_buffer_close(fd->buffer);
/* There can be other objects around that may still /* There can be other objects around that may still
* attempt to write to the buffer. So let gc handle it * attempt to write to the buffer. So let gc handle it
* instead of freeing it explicitly */ * instead of freeing it explicitly */
...@@ -532,6 +536,22 @@ void io_set_nonblocking(int fd) ...@@ -532,6 +536,22 @@ void io_set_nonblocking(int fd)
fatal("io_set_nonblocking: fcntl() failed, %s", strerror(errno)); fatal("io_set_nonblocking: fcntl() failed, %s", strerror(errno));
} }
void io_set_close_on_exec(int fd)
{
if (fcntl(fd, F_SETFD, 1) < 0)
fatal("Can't set close-on-exec flag for fd %d: %s\n",
fd, strerror(errno));
}
/* ALL file descripters handled by the backend should use non-blocking mode,
* and have the close-on-exec flag set. */
void io_init_fd(int fd)
{
io_set_nonblocking(fd);
io_set_close_on_exec(fd);
}
/* Some code is taken from bellman's tcputils. */ /* Some code is taken from bellman's tcputils. */
struct connect_fd *io_connect(struct io_backend *b, struct connect_fd *io_connect(struct io_backend *b,
struct sockaddr_in *remote, struct sockaddr_in *remote,
...@@ -544,7 +564,7 @@ struct connect_fd *io_connect(struct io_backend *b, ...@@ -544,7 +564,7 @@ struct connect_fd *io_connect(struct io_backend *b,
if (s<0) if (s<0)
return NULL; return NULL;
io_set_nonblocking(s); io_init_fd(s);
if (local && bind(s, (struct sockaddr *)local, sizeof *local) < 0) if (local && bind(s, (struct sockaddr *)local, sizeof *local) < 0)
{ {
...@@ -585,7 +605,7 @@ struct listen_fd *io_listen(struct io_backend *b, ...@@ -585,7 +605,7 @@ struct listen_fd *io_listen(struct io_backend *b,
if (s<0) if (s<0)
return NULL; return NULL;
io_set_nonblocking(s); io_init_fd(s);
{ {
int yes = 1; int yes = 1;
...@@ -625,6 +645,8 @@ struct abstract_write *io_read_write(struct io_backend *b, ...@@ -625,6 +645,8 @@ struct abstract_write *io_read_write(struct io_backend *b,
struct io_fd *f; struct io_fd *f;
struct write_buffer *buffer = write_buffer_alloc(block_size); struct write_buffer *buffer = write_buffer_alloc(block_size);
io_init_fd(fd);
NEW(f); NEW(f);
f->fd = fd; f->fd = fd;
...@@ -653,6 +675,8 @@ struct io_fd *io_read(struct io_backend *b, ...@@ -653,6 +675,8 @@ struct io_fd *io_read(struct io_backend *b,
{ {
struct io_fd *f; struct io_fd *f;
io_init_fd(fd);
NEW(f); NEW(f);
f->fd = fd; f->fd = fd;
...@@ -675,14 +699,16 @@ struct io_fd *io_read(struct io_backend *b, ...@@ -675,14 +699,16 @@ struct io_fd *io_read(struct io_backend *b,
return f; return f;
} }
struct abstract_write *io_write(struct io_backend *b, struct io_fd *io_write(struct io_backend *b,
int fd, int fd,
UINT32 block_size, UINT32 block_size,
struct close_callback *close_callback) struct close_callback *close_callback)
{ {
struct io_fd *f; struct io_fd *f;
struct write_buffer *buffer = write_buffer_alloc(block_size); struct write_buffer *buffer = write_buffer_alloc(block_size);
io_init_fd(fd);
NEW(f); NEW(f);
f->fd = fd; f->fd = fd;
...@@ -700,6 +726,5 @@ struct abstract_write *io_write(struct io_backend *b, ...@@ -700,6 +726,5 @@ struct abstract_write *io_write(struct io_backend *b,
b->io = f; b->io = f;
b->nio++; b->nio++;
return &buffer->super; return f;
} }
...@@ -128,6 +128,8 @@ int get_inaddr(struct sockaddr_in * addr, ...@@ -128,6 +128,8 @@ int get_inaddr(struct sockaddr_in * addr,
const char * protocol); const char * protocol);
void io_set_nonblocking(int fd); void io_set_nonblocking(int fd);
void io_set_close_on_exec(int fd);
void io_init_fd(int fd);
struct connect_fd *io_connect(struct io_backend *b, struct connect_fd *io_connect(struct io_backend *b,
struct sockaddr_in *remote, struct sockaddr_in *remote,
...@@ -150,10 +152,9 @@ struct io_fd *io_read(struct io_backend *b, ...@@ -150,10 +152,9 @@ struct io_fd *io_read(struct io_backend *b,
struct read_handler *read_callback, struct read_handler *read_callback,
struct close_callback *close_callback); struct close_callback *close_callback);
struct abstract_write *io_write(struct io_backend *b, struct io_fd *io_write(struct io_backend *b,
int fd, int fd,
UINT32 block_size, UINT32 block_size,
struct close_callback *close_callback); struct close_callback *close_callback);
#endif /* LSH_IO_H_INCLUDED */ #endif /* LSH_IO_H_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment