[lxc-devel] [PATCH] daemonized start: exit children on failure, don't return

Tycho Andersen tycho.andersen at canonical.com
Fri Jun 12 17:14:30 UTC 2015


On Thu, Jun 11, 2015 at 04:11:48AM +0000, Serge Hallyn wrote:
> When starting a daemonized container, only the original parent
> thread should return to the caller.  The first forked child
> immediately exits after forking, but the grandparent child
> was in some places returning on error - causing a second instance
> of the calling function.
> 
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>

Acked-by: Tycho Andersen <tycho.andersen at canonical.com>

> ---
>  src/lxc/lxccontainer.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
> index 7708a8c..1c103e8 100644
> --- a/src/lxc/lxccontainer.c
> +++ b/src/lxc/lxccontainer.c
> @@ -712,19 +712,19 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
>  		pid = fork();
>  		if (pid < 0) {
>  			SYSERROR("Error doing dual-fork");
> -			return false;
> +			exit(1);
>  		}
>  		if (pid != 0)
>  			exit(0);
>  		/* like daemon(), chdir to / and redirect 0,1,2 to /dev/null */
>  		if (chdir("/")) {
>  			SYSERROR("Error chdir()ing to /.");
> -			return false;
> +			exit(1);
>  		}
>  		lxc_check_inherited(conf, true, -1);
>  		if (null_stdfds() < 0) {
>  			ERROR("failed to close fds");
> -			return false;
> +			exit(1);
>  		}
>  		setsid();
>  	} else {
> @@ -742,6 +742,8 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
>  		if (pid_fp == NULL) {
>  			SYSERROR("Failed to create pidfile '%s' for '%s'",
>  				 c->pidfile, c->name);
> +			if (daemonize)
> +				exit(1);
>  			return false;
>  		}
>  
> @@ -749,6 +751,8 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
>  			SYSERROR("Failed to write '%s'", c->pidfile);
>  			fclose(pid_fp);
>  			pid_fp = NULL;
> +			if (daemonize)
> +				exit(1);
>  			return false;
>  		}
>  
> -- 
> 2.1.4
> 
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel


More information about the lxc-devel mailing list