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
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;
int
netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
{
- struct netifd_fd *fd;
int pfds[2];
int pid;
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);
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;
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);
{
}
-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;
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;
#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,
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);
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]);
*/
#include "netifd.h"
#include "system.h"
+#include <fcntl.h>
static const struct blobmsg_policy tunnel_attrs[__TUNNEL_ATTR_MAX] = {
[TUNNEL_ATTR_TYPE] = { "mode", BLOBMSG_TYPE_STRING },
.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
+}
time_t system_get_rtime(void);
+void system_fd_set_cloexec(int fd);
+
#endif
static struct ubus_context *ctx = NULL;
static struct blob_buf b;
-static struct netifd_fd ubus_fd;
static const char *ubus_path;
/* global object */
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
static void
netifd_ubus_connection_lost(struct ubus_context *ctx)
{
- netifd_fd_delete(&ubus_fd);
netifd_ubus_reconnect_timer(NULL);
}