Merge pull request #32256 from YHNdnzj/network-close-and-replace

sd-dhcp-server: use close_and_replace (avoid fd leak)
This commit is contained in:
Yu Watanabe
2024-04-13 05:09:36 +09:00
committed by GitHub
+4 -6
View File
@@ -1592,6 +1592,7 @@ int sd_dhcp_server_set_lease_file(sd_dhcp_server *server, int dir_fd, const char
int r;
assert_return(server, -EINVAL);
assert_return(!path || (dir_fd >= 0 || dir_fd == AT_FDCWD), -EBADF);
assert_return(!sd_dhcp_server_is_running(server), -EBUSY);
if (!path) {
@@ -1604,11 +1605,7 @@ int sd_dhcp_server_set_lease_file(sd_dhcp_server *server, int dir_fd, const char
if (!path_is_safe(path))
return -EINVAL;
if (dir_fd < 0 && dir_fd != AT_FDCWD)
return -EBADF;
_cleanup_close_ int fd = -EBADF;
fd = fd_reopen(dir_fd, O_CLOEXEC | O_DIRECTORY | O_PATH);
_cleanup_close_ int fd = fd_reopen(dir_fd, O_CLOEXEC | O_DIRECTORY | O_PATH);
if (fd < 0)
return fd;
@@ -1616,6 +1613,7 @@ int sd_dhcp_server_set_lease_file(sd_dhcp_server *server, int dir_fd, const char
if (r < 0)
return r;
server->lease_dir_fd = TAKE_FD(fd);
close_and_replace(server->lease_dir_fd, fd);
return 0;
}