From c173c610044890c539584f3beb927e71ff83c198 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 29 Oct 2012 23:08:01 +0100 Subject: [PATCH] use FD_CLOEXEC instead of tracking lists of fds --- main.c | 29 ++++------------------------- netifd.h | 10 ---------- proto-shell.c | 13 +++++++------ system.c | 8 ++++++++ system.h | 2 ++ ubus.c | 5 +---- 6 files changed, 22 insertions(+), 45 deletions(-) diff --git a/main.c b/main.c index 4ee86eb..2fef776 100644 --- a/main.c +++ b/main.c @@ -32,7 +32,6 @@ const char *resolv_conf = DEFAULT_RESOLV_CONF; static char **global_argv; static struct list_head process_list = LIST_HEAD_INIT(process_list); -static struct list_head fds = LIST_HEAD_INIT(fds); #define DEFAULT_LOG_LEVEL L_NOTICE @@ -58,8 +57,8 @@ netifd_delete_process(struct netifd_process *proc) if (proc->uloop.pending) uloop_process_delete(&proc->uloop); list_del(&proc->list); - netifd_fd_delete(&proc->log_fd); - close(proc->log_fd.fd); + uloop_fd_delete(&proc->log_uloop); + close(proc->log_uloop.fd); if (proc->log_buf) { free(proc->log_buf); proc->log_buf = NULL; @@ -168,7 +167,6 @@ netifd_process_cb(struct uloop_process *proc, int ret) int netifd_start_process(const char **argv, char **env, struct netifd_process *proc) { - struct netifd_fd *fd; int pfds[2]; int pid; @@ -190,13 +188,6 @@ netifd_start_process(const char **argv, char **env, struct netifd_process *proc) if (proc->dir_fd >= 0) fchdir(proc->dir_fd); - /* close all non-essential fds */ - list_for_each_entry(fd, &fds, list) { - if (fd->proc == proc) - continue; - close(fd->fd); - } - dup2(pfds[1], 0); dup2(pfds[1], 1); dup2(pfds[1], 2); @@ -217,10 +208,10 @@ netifd_start_process(const char **argv, char **env, struct netifd_process *proc) uloop_process_add(&proc->uloop); list_add_tail(&proc->list, &process_list); + system_fd_set_cloexec(pfds[0]); proc->log_buf_ofs = 0; - proc->log_uloop.fd = proc->log_fd.fd = pfds[0]; + proc->log_uloop.fd = pfds[0]; proc->log_uloop.cb = netifd_process_log_cb; - netifd_fd_add(&proc->log_fd); uloop_fd_add(&proc->log_uloop, ULOOP_EDGE_TRIGGER | ULOOP_READ); return 0; @@ -241,18 +232,6 @@ netifd_kill_process(struct netifd_process *proc) netifd_delete_process(proc); } -void -netifd_fd_add(struct netifd_fd *fd) -{ - list_add_tail(&fd->list, &fds); -} - -void -netifd_fd_delete(struct netifd_fd *fd) -{ - list_del(&fd->list); -} - static void netifd_do_restart(struct uloop_timeout *timeout) { execvp(global_argv[0], global_argv); diff --git a/netifd.h b/netifd.h index e51514e..ac0c0f2 100644 --- a/netifd.h +++ b/netifd.h @@ -71,19 +71,12 @@ static inline void no_debug(int level, const char *fmt, ...) { } -struct netifd_fd { - struct list_head list; - struct netifd_process *proc; - int fd; -}; - struct netifd_process { struct list_head list; struct uloop_process uloop; void (*cb)(struct netifd_process *, int ret); int dir_fd; - struct netifd_fd log_fd; struct uloop_fd log_uloop; const char *log_prefix; char *log_buf; @@ -96,9 +89,6 @@ void netifd_log_message(int priority, const char *format, ...); int netifd_start_process(const char **argv, char **env, struct netifd_process *proc); void netifd_kill_process(struct netifd_process *proc); -void netifd_fd_add(struct netifd_fd *fd); -void netifd_fd_delete(struct netifd_fd *fd); - struct device; struct interface; diff --git a/proto-shell.c b/proto-shell.c index ae0f938..2a5eda0 100644 --- a/proto-shell.c +++ b/proto-shell.c @@ -30,8 +30,9 @@ #include "interface.h" #include "interface-ip.h" #include "proto.h" +#include "system.h" -static struct netifd_fd proto_fd; +static int proto_fd = -1; enum proto_shell_sm { S_IDLE, @@ -704,10 +705,10 @@ proto_shell_attach(const struct proto_handler *h, struct interface *iface, state->proto.cb = proto_shell_handler; state->teardown_timeout.cb = proto_shell_teardown_timeout_cb; state->script_task.cb = proto_shell_script_cb; - state->script_task.dir_fd = proto_fd.fd; + state->script_task.dir_fd = proto_fd; state->script_task.log_prefix = iface->name; state->proto_task.cb = proto_shell_task_cb; - state->proto_task.dir_fd = proto_fd.fd; + state->proto_task.dir_fd = proto_fd; state->proto_task.log_prefix = iface->name; state->handler = container_of(h, struct proto_shell_handler, proto); @@ -907,11 +908,11 @@ static void __init proto_shell_init(void) if (chdir("./proto")) goto close_cur; - proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY); - if (proto_fd.fd < 0) + proto_fd = open(".", O_RDONLY | O_DIRECTORY); + if (proto_fd < 0) goto close_cur; - netifd_fd_add(&proto_fd); + system_fd_set_cloexec(proto_fd); glob("./*.sh", 0, NULL, &g); for (i = 0; i < g.gl_pathc; i++) proto_shell_add_script(g.gl_pathv[i]); diff --git a/system.c b/system.c index 1096ad2..f71bcbe 100644 --- a/system.c +++ b/system.c @@ -13,6 +13,7 @@ */ #include "netifd.h" #include "system.h" +#include static const struct blobmsg_policy tunnel_attrs[__TUNNEL_ATTR_MAX] = { [TUNNEL_ATTR_TYPE] = { "mode", BLOBMSG_TYPE_STRING }, @@ -27,3 +28,10 @@ const struct config_param_list tunnel_attr_list = { .n_params = __TUNNEL_ATTR_MAX, .params = tunnel_attrs, }; + +void system_fd_set_cloexec(int fd) +{ +#ifdef FD_CLOEXEC + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); +#endif +} diff --git a/system.h b/system.h index 7716fbd..bc1b932 100644 --- a/system.h +++ b/system.h @@ -99,4 +99,6 @@ int system_add_ip_tunnel(const char *name, struct blob_attr *attr); time_t system_get_rtime(void); +void system_fd_set_cloexec(int fd); + #endif diff --git a/ubus.c b/ubus.c index 3ea71bf..7b85930 100644 --- a/ubus.c +++ b/ubus.c @@ -25,7 +25,6 @@ static struct ubus_context *ctx = NULL; static struct blob_buf b; -static struct netifd_fd ubus_fd; static const char *ubus_path; /* global object */ @@ -258,8 +257,7 @@ static void netifd_ubus_add_fd(void) { ubus_add_uloop(ctx); - ubus_fd.fd = ctx->sock.fd; - netifd_fd_add(&ubus_fd); + system_fd_set_cloexec(ctx->sock.fd); } static void @@ -283,7 +281,6 @@ netifd_ubus_reconnect_timer(struct uloop_timeout *timeout) static void netifd_ubus_connection_lost(struct ubus_context *ctx) { - netifd_fd_delete(&ubus_fd); netifd_ubus_reconnect_timer(NULL); } -- 2.25.1