Skip to content

Commit e9d159f

Browse files
committed
linux: store rootfsfd under private data only
Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 953a8c4 commit e9d159f

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/libcrun/linux.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ cleanup_private_data (void *private_data)
167167
if (p->dev_fds)
168168
cleanup_close_mapp (&(p->dev_fds));
169169

170+
if (p->rootfsfd >= 0)
171+
close (p->rootfsfd);
172+
170173
free (p->unified_cgroup_path);
171174
free (p->host_notify_socket_path);
172175
free (p->container_notify_socket_path);
@@ -2045,13 +2048,14 @@ get_force_cgroup_v1_annotation (libcrun_container_t *container)
20452048
}
20462049

20472050
static int
2048-
do_mounts (libcrun_container_t *container, int rootfsfd, const char *rootfs, libcrun_error_t *err)
2051+
do_mounts (libcrun_container_t *container, const char *rootfs, libcrun_error_t *err)
20492052
{
20502053
size_t i;
20512054
int ret;
20522055
runtime_spec_schema_config_schema *def = container->container_def;
20532056
const char *systemd_cgroup_v1 = get_force_cgroup_v1_annotation (container);
20542057
cleanup_close_map struct libcrun_fd_map *mount_fds = NULL;
2058+
int rootfsfd = get_private_data (container)->rootfsfd;
20552059

20562060
mount_fds = get_private_data (container)->mount_fds;
20572061
get_private_data (container)->mount_fds = NULL;
@@ -2576,9 +2580,7 @@ int
25762580
libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_container_t *container, const char *rootfs, set_mounts_cb_t cb, void *cb_data, libcrun_error_t *err)
25772581
{
25782582
runtime_spec_schema_config_schema *def = container->container_def;
2579-
cleanup_close int rootfsfd_cleanup = -1;
25802583
unsigned long rootfs_propagation = 0;
2581-
int rootfsfd = -1;
25822584
int cgroup_mode;
25832585
int is_user_ns = 0;
25842586
int ret = 0;
@@ -2609,12 +2611,12 @@ libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_cont
26092611
return ret;
26102612
}
26112613

2612-
rootfsfd = rootfsfd_cleanup = open (rootfs, O_PATH | O_CLOEXEC);
2613-
if (UNLIKELY (rootfsfd < 0))
2614+
ret = open (rootfs, O_PATH | O_CLOEXEC);
2615+
if (UNLIKELY (ret < 0))
26142616
return crun_make_error (err, errno, "open `%s`", rootfs);
26152617

2618+
get_private_data (container)->rootfsfd = ret;
26162619
get_private_data (container)->rootfs = rootfs;
2617-
get_private_data (container)->rootfsfd = rootfsfd;
26182620

26192621
// configure handler mounts
26202622
ret = libcrun_container_notify_handler (entrypoint_args, HANDLER_CONFIGURE_MOUNTS, container, rootfs, err);
@@ -2627,7 +2629,7 @@ libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_cont
26272629
unsigned long remount_flags = MS_REMOUNT | MS_BIND | MS_RDONLY;
26282630
int fd;
26292631

2630-
fd = dup (rootfsfd);
2632+
fd = dup (get_private_data (container)->rootfsfd);
26312633
if (UNLIKELY (fd < 0))
26322634
return crun_make_error (err, errno, "dup fd for `%s`", rootfs);
26332635

@@ -2655,7 +2657,7 @@ libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_cont
26552657
if (UNLIKELY (ret < 0))
26562658
return ret;
26572659

2658-
ret = do_mounts (container, rootfsfd, rootfs, err);
2660+
ret = do_mounts (container, rootfs, err);
26592661
if (UNLIKELY (ret < 0))
26602662
return ret;
26612663

@@ -2691,7 +2693,7 @@ libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_cont
26912693
libcrun_error_t tmp_err = NULL;
26922694
const char *rel_cwd = consume_slashes (def->process->cwd);
26932695
/* Ignore errors here and let it fail later. */
2694-
(void) crun_safe_ensure_directory_at (rootfsfd, rootfs, rel_cwd, 0755, &tmp_err);
2696+
(void) crun_safe_ensure_directory_at (get_private_data (container)->rootfsfd, rootfs, rel_cwd, 0755, &tmp_err);
26952697
crun_error_release (&tmp_err);
26962698
}
26972699

@@ -2708,7 +2710,7 @@ libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_cont
27082710
if (UNLIKELY (ret < 0))
27092711
return crun_make_error (err, errno, "failed configuring mounts for handler at phase: HANDLER_CONFIGURE_AFTER_MOUNTS");
27102712

2711-
get_private_data (container)->rootfsfd = -1;
2713+
close_and_reset (&(get_private_data (container)->rootfsfd));
27122714

27132715
return 0;
27142716
}

0 commit comments

Comments
 (0)