files: simplify fchroot()

Submitted by Andrei Vagin on June 14, 2016, 5:16 a.m.

Details

Message ID 1465881394-11444-1-git-send-email-avagin@openvz.org
State Rejected
Series "files: simplify fchroot()"
Headers show

Commit Message

Andrei Vagin June 14, 2016, 5:16 a.m.
From: Andrew Vagin <avagin@virtuozzo.com>

We can call fchdir into the taget directory and call chroot(".")

Signed-off-by: Andrew Vagin <avagin@virtuozzo.com>
---
 criu/files.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/files.c b/criu/files.c
index e206dba..dfcb562 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -1224,26 +1224,18 @@  out:
 
 static int fchroot(int fd)
 {
-	char fd_path[PSFDS];
-	int proc;
-
 	/*
 	 * There's no such thing in syscalls. We can emulate
-	 * it using the /proc/self/fd/ :)
-	 *
-	 * But since there might be no /proc mount in our mount
-	 * namespace, we will have to ... workaround it.
+	 * it using fchdir()
 	 */
 
-	proc = get_service_fd(PROC_FD_OFF);
-	if (fchdir(proc) < 0) {
+	if (fchdir(fd) < 0) {
 		pr_perror("Can't chdir to proc");
 		return -1;
 	}
 
-	sprintf(fd_path, "./self/fd/%d", fd);
-	pr_debug("Going to chroot into %s\n", fd_path);
-	return chroot(fd_path);
+	pr_debug("Going to chroot into /proc/self/fd/%d\n", fd);
+	return chroot(".");
 }
 
 int restore_fs(struct pstree_item *me)

Comments

Pavel Emelianov June 15, 2016, 11:49 a.m.
On 06/14/2016 08:16 AM, Andrey Vagin wrote:
> From: Andrew Vagin <avagin@virtuozzo.com>
> 
> We can call fchdir into the taget directory and call chroot(".")
> 
> Signed-off-by: Andrew Vagin <avagin@virtuozzo.com>
> ---
>  criu/files.c | 16 ++++------------
>  1 file changed, 4 insertions(+), 12 deletions(-)
> 
> diff --git a/criu/files.c b/criu/files.c
> index e206dba..dfcb562 100644
> --- a/criu/files.c
> +++ b/criu/files.c
> @@ -1224,26 +1224,18 @@ out:
>  
>  static int fchroot(int fd)
>  {
> -	char fd_path[PSFDS];
> -	int proc;
> -
>  	/*
>  	 * There's no such thing in syscalls. We can emulate
> -	 * it using the /proc/self/fd/ :)
> -	 *
> -	 * But since there might be no /proc mount in our mount
> -	 * namespace, we will have to ... workaround it.
> +	 * it using fchdir()

Thanks :) But there's one more comment speaking about proc -- the one
right before __calling__ this function.

>  	 */
>  
> -	proc = get_service_fd(PROC_FD_OFF);
> -	if (fchdir(proc) < 0) {
> +	if (fchdir(fd) < 0) {
>  		pr_perror("Can't chdir to proc");
>  		return -1;
>  	}
>  
> -	sprintf(fd_path, "./self/fd/%d", fd);
> -	pr_debug("Going to chroot into %s\n", fd_path);
> -	return chroot(fd_path);
> +	pr_debug("Going to chroot into /proc/self/fd/%d\n", fd);
> +	return chroot(".");
>  }
>  
>  int restore_fs(struct pstree_item *me)
>