mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-07-30 06:25:09 +00:00
utils: improve switch_to_ns()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
parent
c6494c4b88
commit
b280bc380c
@ -149,6 +149,7 @@
|
|||||||
#define LXC_LINELEN 4096
|
#define LXC_LINELEN 4096
|
||||||
#define LXC_IDMAPLEN 4096
|
#define LXC_IDMAPLEN 4096
|
||||||
#define LXC_MAX_BUFFER 4096
|
#define LXC_MAX_BUFFER 4096
|
||||||
|
#define LXC_NAMESPACE_NAME_MAX 256
|
||||||
|
|
||||||
/* /proc/ = 6
|
/* /proc/ = 6
|
||||||
* +
|
* +
|
||||||
|
@ -693,15 +693,18 @@ int detect_shared_rootfs(void)
|
|||||||
|
|
||||||
bool switch_to_ns(pid_t pid, const char *ns)
|
bool switch_to_ns(pid_t pid, const char *ns)
|
||||||
{
|
{
|
||||||
int fd, ret;
|
__do_close_prot_errno int fd = -EBADF;
|
||||||
char nspath[PATH_MAX];
|
int ret;
|
||||||
|
char nspath[STRLITERALLEN("/proc//ns/")
|
||||||
|
+ INTTYPE_TO_STRLEN(pid_t)
|
||||||
|
+ LXC_NAMESPACE_NAME_MAX];
|
||||||
|
|
||||||
/* Switch to new ns */
|
/* Switch to new ns */
|
||||||
ret = snprintf(nspath, PATH_MAX, "/proc/%d/ns/%s", pid, ns);
|
ret = snprintf(nspath, sizeof(nspath), "/proc/%d/ns/%s", pid, ns);
|
||||||
if (ret < 0 || ret >= PATH_MAX)
|
if (ret < 0 || ret >= sizeof(nspath))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fd = open(nspath, O_RDONLY);
|
fd = open(nspath, O_RDONLY | O_CLOEXEC);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
SYSERROR("Failed to open \"%s\"", nspath);
|
SYSERROR("Failed to open \"%s\"", nspath);
|
||||||
return false;
|
return false;
|
||||||
@ -709,12 +712,11 @@ bool switch_to_ns(pid_t pid, const char *ns)
|
|||||||
|
|
||||||
ret = setns(fd, 0);
|
ret = setns(fd, 0);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
SYSERROR("Failed to set process %d to \"%s\" of %d.", pid, ns, fd);
|
SYSERROR("Failed to set process %d to \"%s\" of %d.", pid, ns,
|
||||||
close(fd);
|
fd);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user