[CRIU] [PATCH] mount: restore cwd in open_mountpoint()

Andrew Vagin avagin at odin.com
Thu Apr 2 09:24:08 PDT 2015


On Thu, Apr 02, 2015 at 05:43:11PM +0300, Pavel Emelyanov wrote:
> On 04/02/2015 03:47 PM, Andrey Vagin wrote:
> > setns(fd, CLONE_NEWNS) resets cwd and root, so we need to
> > restore them back.
> > 
> > Without this patch stats-dump isn't saved in the work dir:
> > -rw-r--r-- 1 root root 32 Apr  2 14:21 /stats-dump
> 
> Why? I mean -- who does open_mountpoint() and screws dump-stats?

Because dump_stats is written in a current work dir, but criu dump can
call open_mountpoint() which changes criu cwd.

For example, open_mountpoint() is called if criu dumps a tmpfs mount
point and a part of it is over-mounted.

> 
> > Signed-off-by: Andrey Vagin <avagin at openvz.org>
> > ---
> >  mount.c | 19 ++++++++++++++++++-
> >  1 file changed, 18 insertions(+), 1 deletion(-)
> > 
> > diff --git a/mount.c b/mount.c
> > index 11651e4..2b7597b 100644
> > --- a/mount.c
> > +++ b/mount.c
> > @@ -775,6 +775,7 @@ static int open_mountpoint(struct mount_info *pm)
> >  {
> >  	int fd = -1, ns_old = -1;
> >  	char mnt_path[] = "/tmp/cr-tmpfs.XXXXXX";
> > +	int cwd_fd;
> >  
> >  	/*
> >  	 * If a mount doesn't have children, we can open a mount point,
> > @@ -793,8 +794,14 @@ static int open_mountpoint(struct mount_info *pm)
> >  	 * mkdtemp, setns(tgt), mount, open, detach, setns(old).
> >  	 */
> >  
> > -	if (switch_ns(root_item->pid.real, &mnt_ns_desc, &ns_old) < 0)
> > +	cwd_fd = open(".", O_DIRECTORY);
> > +	if (cwd_fd < 0) {
> > +		pr_perror("Unable to open cwd");
> >  		return -1;
> > +	}
> > +
> > +	if (switch_ns(root_item->pid.real, &mnt_ns_desc, &ns_old) < 0)
> 
> Is it the only place where we can switch_ns to mntns?

Yes, I think so.

> 
> > +		goto out;
> >  
> >  	if (mkdtemp(mnt_path) == NULL) {
> >  		pr_perror("Can't create a temporary directory");
> > @@ -816,12 +823,22 @@ static int open_mountpoint(struct mount_info *pm)
> >  		ns_old = -1;
> >  		goto out;
> >  	}
> > +	if (fchdir(cwd_fd)) {
> > +		pr_perror("Unable to restore cwd");
> > +		close(cwd_fd);
> > +		close(fd);
> > +		return -1;
> > +	}
> > +	close(cwd_fd);
> >  
> >  	return __open_mountpoint(pm, fd);;
> >  out:
> >  	if (ns_old >= 0)
> >  		 restore_ns(ns_old, &mnt_ns_desc);
> >  	close_safe(&fd);
> > +	if (fchdir(cwd_fd))
> > +		pr_perror("Unable to restore cwd");
> > +	close(cwd_fd);
> >  	return -1;
> >  }
> >  
> > 
> 


More information about the CRIU mailing list