Skip to content

Commit

Permalink
Merge pull request #1534 from sohankunkerkar/handle-setns-error
Browse files Browse the repository at this point in the history
src/libcrun: improve error handling for the mnt namespace restoration
  • Loading branch information
giuseppe authored Aug 30, 2024
2 parents f54d383 + 83c1355 commit 7dba7fc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/libcrun/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -4259,6 +4259,8 @@ prepare_and_send_dev_mounts (libcrun_container_t *container, int sync_socket_hos
size_t how_many = 0;
size_t i;
int ret;
// To track whether the namespace has been changed.
bool ns_changed = false;

if (def->linux == NULL || def->linux->devices_len == 0)
return 0;
Expand Down Expand Up @@ -4288,6 +4290,9 @@ prepare_and_send_dev_mounts (libcrun_container_t *container, int sync_socket_hos
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "unshare `CLONE_NEWNS`");

// This indicates that the mount namespace has been altered.
ns_changed = true;

ret = mount (NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);
if (UNLIKELY (ret < 0))
{
Expand Down Expand Up @@ -4348,13 +4353,16 @@ prepare_and_send_dev_mounts (libcrun_container_t *container, int sync_socket_hos

ret = send_mounts (sync_socket_host, dev_fds, how_many, def->linux->devices_len, err);
restore_mountns:
{
int setns_ret;
if (ns_changed && current_mountns >= 0)
{
int setns_ret;
setns_ret = setns (current_mountns, CLONE_NEWNS);
if (UNLIKELY (setns_ret < 0 && ret >= 0))
{
return crun_make_error (err, errno, "setns `CLONE_NEWNS`");
}
}

setns_ret = setns (current_mountns, CLONE_NEWNS);
if (UNLIKELY (setns_ret < 0 && ret >= 0))
return crun_make_error (err, errno, "setns `CLONE_NEWNS`");
}
return ret;
}

Expand Down

2 comments on commit 7dba7fc

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.