[CRIU] [PATCH] test: use .pid.inprogress file for macvlan test

Andrei Vagin avagin at virtuozzo.com
Wed Oct 26 14:56:43 PDT 2016


On Wed, Oct 26, 2016 at 02:58:00PM -0600, Tycho Andersen wrote:
> On Wed, Oct 26, 2016 at 11:20:14AM -0700, Andrei Vagin wrote:
> > On Wed, Oct 26, 2016 at 04:23:48PM +0000, Tycho Andersen wrote:
> > > Note, this depends on Pavel's patch here:
> > > https://lists.openvz.org/pipermail/criu/2016-October/032499.html which is
> > > not yet applied.
> > > 
> > > Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
> > > ---
> > >  test/zdtm/static/macvlan.hook | 7 +++++--
> > >  1 file changed, 5 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/test/zdtm/static/macvlan.hook b/test/zdtm/static/macvlan.hook
> > > index 0dea7d1..d0a06b6 100755
> > > --- a/test/zdtm/static/macvlan.hook
> > > +++ b/test/zdtm/static/macvlan.hook
> > > @@ -6,7 +6,8 @@ if [ "$1" == "--post-start" ]; then
> > >  	set -e
> > >  
> > >  	i=0
> > > -	while [ -z "$(pidof -s macvlan)" ]; do
> > > +	PIDF="zdtm/static/macvlan.pid.inprogress"
> > > +	while [ ! -f "$PIDF" ]; do
> > >  		i=$(($i+1))
> > >  		if [ "$i" -eq "10" ]; then
> > >  			echo "failed to create macvlan test"
> > > @@ -15,12 +16,14 @@ if [ "$1" == "--post-start" ]; then
> > >  		sleep 1
> > >  	done
> > >  
> > > +	TPID=$(cat $PIDF)
> > 
> > It is racy, because open() + write() is not atomic.
> 
> Yes, I think we can make it not racy by this little patch to pidfile;
> this will fix both Pavel's and my tests. Does that make sense?

Yes, it does. Thanks!

> 
> Tycho

> From 3b6b2cb36fc37a650ed8d0158231e191f03f30f4 Mon Sep 17 00:00:00 2001
> From: Tycho Andersen <tycho.andersen at canonical.com>
> Date: Wed, 26 Oct 2016 14:25:50 -0600
> Subject: [PATCH] test: make write_pidfile atomic
> 
> This is useful so that hooks can do a $(cat $pidfile) or [ -f $pidfile ]
> and rely on the result.
> 
> Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
> ---
>  test/zdtm/lib/test.c | 28 +++++++++++++++++++++++-----
>  1 file changed, 23 insertions(+), 5 deletions(-)
> 
> diff --git a/test/zdtm/lib/test.c b/test/zdtm/lib/test.c
> index a3576a5..75b7d93 100644
> --- a/test/zdtm/lib/test.c
> +++ b/test/zdtm/lib/test.c
> @@ -100,21 +100,39 @@ void test_ext_init(int argc, char **argv)
>  
>  int write_pidfile(int pid)
>  {
> -	int fd;
> +	int fd = -1;
> +	char tmp[] = ".zdtm.pidfile.XXXXXX";
>  
> -	fd = open(pidfile, O_CREAT | O_EXCL | O_WRONLY, 0666);
> +	fd = mkstemp(tmp);
>  	if (fd == -1) {
> -		fprintf(stderr, "Can't create the file %s: %m\n", pidfile);
> +		fprintf(stderr, "Can't create the file %s: %m\n", tmp);
>  		return -1;
>  	}
> +
> +	if (fchmod(fd, 0666) < 0) {
> +		fprintf(stderr, "Can't fchmod %s: %m\n", tmp);
> +		goto err_c;
> +	}
> +
>  	if (dprintf(fd, "%d", pid) == -1) {
> -		fprintf(stderr, "Can't write in the file %s: %m\n", pidfile);
> -		return -1;
> +		fprintf(stderr, "Can't write in the file %s: %m\n", tmp);
> +		goto err_c;
>  	}
>  
>  	close(fd);
>  
> +	if (rename(tmp, pidfile) < 0) {
> +		fprintf(stderr, "Can't rename %s to %s: %m\n", tmp, pidfile);
> +		goto err_u;
> +	}
> +
>  	return 0;
> +
> +err_c:
> +	close(fd);
> +err_u:
> +	unlink(tmp);
> +	return -1;
>  }
>  
>  void test_init(int argc, char **argv)
> -- 
> 2.7.4
> 



More information about the CRIU mailing list