Commit 9c40efdd authored by Andrei Vagin's avatar Andrei Vagin Committed by Andrei Vagin

service-fd: close a source file descriptor in install_service_fd()

Currently, install_service_fd() always creates a new file descriptor,
but it is going to be reworked to reuse a file descriptor when it is
possible.
Reviewed-by: 's avatarKirill Tkhai <ktkhai@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 19d3da0d
...@@ -164,7 +164,7 @@ int add_rpc_notify(int sk) ...@@ -164,7 +164,7 @@ int add_rpc_notify(int sk)
BUG_ON(scripts_mode == SCRIPTS_SHELL); BUG_ON(scripts_mode == SCRIPTS_SHELL);
scripts_mode = SCRIPTS_RPC; scripts_mode = SCRIPTS_RPC;
if (install_service_fd(RPC_SK_OFF, sk) < 0) if (install_service_fd(RPC_SK_OFF, dup(sk)) < 0)
return -1; return -1;
return 0; return 0;
......
...@@ -1671,7 +1671,6 @@ static int prepare_cgroup_sfd(CgroupEntry *ce) ...@@ -1671,7 +1671,6 @@ static int prepare_cgroup_sfd(CgroupEntry *ce)
} }
ret = install_service_fd(CGROUP_YARD, i); ret = install_service_fd(CGROUP_YARD, i);
close(i);
if (ret < 0) if (ret < 0)
goto err; goto err;
......
...@@ -1297,8 +1297,6 @@ static int dump_one_task(struct pstree_item *item, InventoryEntry *parent_ie) ...@@ -1297,8 +1297,6 @@ static int dump_one_task(struct pstree_item *item, InventoryEntry *parent_ie)
if (install_service_fd(CR_PROC_FD_OFF, pfd) < 0) if (install_service_fd(CR_PROC_FD_OFF, pfd) < 0)
goto err_cure_imgset; goto err_cure_imgset;
close(pfd);
} }
ret = parasite_fixup_vdso(parasite_ctl, pid, &vmas); ret = parasite_fixup_vdso(parasite_ctl, pid, &vmas);
......
...@@ -2051,7 +2051,6 @@ static int restore_root_task(struct pstree_item *init) ...@@ -2051,7 +2051,6 @@ static int restore_root_task(struct pstree_item *init)
} }
ret = install_service_fd(CR_PROC_FD_OFF, fd); ret = install_service_fd(CR_PROC_FD_OFF, fd);
close(fd);
if (ret < 0) if (ret < 0)
return -1; return -1;
......
...@@ -78,7 +78,6 @@ int fdstore_init(void) ...@@ -78,7 +78,6 @@ int fdstore_init(void)
} }
ret = install_service_fd(FDSTORE_SK_OFF, sk); ret = install_service_fd(FDSTORE_SK_OFF, sk);
close(sk);
if (ret < 0) if (ret < 0)
return -1; return -1;
......
...@@ -1764,11 +1764,8 @@ int open_transport_socket(void) ...@@ -1764,11 +1764,8 @@ int open_transport_socket(void)
goto out; goto out;
} }
if (install_service_fd(TRANSPORT_FD_OFF, sock) < 0) { if (install_service_fd(TRANSPORT_FD_OFF, sock) < 0)
close(sock);
goto out; goto out;
}
close(sock);
ret = 0; ret = 0;
out: out:
return ret; return ret;
......
...@@ -472,7 +472,8 @@ int open_image_dir(char *dir) ...@@ -472,7 +472,8 @@ int open_image_dir(char *dir)
} }
ret = install_service_fd(IMG_FD_OFF, fd); ret = install_service_fd(IMG_FD_OFF, fd);
close(fd); if (ret < 0)
return -1;
fd = ret; fd = ret;
if (opts.img_parent) { if (opts.img_parent) {
......
...@@ -183,7 +183,6 @@ int log_init(const char *output) ...@@ -183,7 +183,6 @@ int log_init(const char *output)
} }
fd = install_service_fd(LOG_FD_OFF, new_logfd); fd = install_service_fd(LOG_FD_OFF, new_logfd);
close(new_logfd);
if (fd < 0) if (fd < 0)
goto err; goto err;
......
...@@ -3398,7 +3398,6 @@ static int mntns_set_root_fd(pid_t pid, int fd) ...@@ -3398,7 +3398,6 @@ static int mntns_set_root_fd(pid_t pid, int fd)
ret = install_service_fd(ROOT_FD_OFF, fd); ret = install_service_fd(ROOT_FD_OFF, fd);
if (ret >= 0) if (ret >= 0)
mntns_root_pid = pid; mntns_root_pid = pid;
close(fd);
return ret; return ret;
} }
......
...@@ -1427,11 +1427,9 @@ static int start_usernsd(void) ...@@ -1427,11 +1427,9 @@ static int start_usernsd(void)
if (install_service_fd(USERNSD_SK, sk[0]) < 0) { if (install_service_fd(USERNSD_SK, sk[0]) < 0) {
kill(usernsd_pid, SIGKILL); kill(usernsd_pid, SIGKILL);
waitpid(usernsd_pid, NULL, 0); waitpid(usernsd_pid, NULL, 0);
close(sk[0]);
return -1; return -1;
} }
close(sk[0]);
return 0; return 0;
} }
......
...@@ -2549,7 +2549,6 @@ int netns_keep_nsfd(void) ...@@ -2549,7 +2549,6 @@ int netns_keep_nsfd(void)
pr_err("Can't install ns net reference\n"); pr_err("Can't install ns net reference\n");
else else
pr_info("Saved netns fd for links restore\n"); pr_info("Saved netns fd for links restore\n");
close(ns_fd);
return ret >= 0 ? 0 : -1; return ret >= 0 ? 0 : -1;
} }
......
...@@ -314,7 +314,6 @@ static inline int set_proc_pid_fd(int pid, int fd) ...@@ -314,7 +314,6 @@ static inline int set_proc_pid_fd(int pid, int fd)
open_proc_pid = pid; open_proc_pid = pid;
ret = install_service_fd(PROC_PID_FD_OFF, fd); ret = install_service_fd(PROC_PID_FD_OFF, fd);
close(fd);
return ret; return ret;
} }
...@@ -348,7 +347,7 @@ void close_proc() ...@@ -348,7 +347,7 @@ void close_proc()
int set_proc_fd(int fd) int set_proc_fd(int fd)
{ {
if (install_service_fd(PROC_FD_OFF, fd) < 0) if (install_service_fd(PROC_FD_OFF, dup(fd)) < 0)
return -1; return -1;
return 0; return 0;
} }
...@@ -365,7 +364,6 @@ static int open_proc_sfd(char *path) ...@@ -365,7 +364,6 @@ static int open_proc_sfd(char *path)
} }
ret = install_service_fd(PROC_FD_OFF, fd); ret = install_service_fd(PROC_FD_OFF, fd);
close(fd);
if (ret < 0) if (ret < 0)
return -1; return -1;
...@@ -502,10 +500,12 @@ int install_service_fd(enum sfd_type type, int fd) ...@@ -502,10 +500,12 @@ int install_service_fd(enum sfd_type type, int fd)
if (dup3(fd, sfd, O_CLOEXEC) != sfd) { if (dup3(fd, sfd, O_CLOEXEC) != sfd) {
pr_perror("Dup %d -> %d failed", fd, sfd); pr_perror("Dup %d -> %d failed", fd, sfd);
close(fd);
return -1; return -1;
} }
set_bit(type, sfd_map); set_bit(type, sfd_map);
close(fd);
return sfd; return sfd;
} }
......
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