[CRIU] [PATCH 6/7] img-cache: Daemonize on start

Andrei Vagin avagin at virtuozzo.com
Wed Jul 11 08:51:50 MSK 2018


On Sun, Jul 08, 2018 at 05:47:10PM +0100, Radostin Stoyanov wrote:
> When the --daemon option is passed to image-cache, it will wait until a
> connection with image-proxy is established, and then will continue its
> execution in the background (daemonize).
> 
> This commit will daemonize image-cache immediately after start. This
> enables the following two commands to be executed sequentially (in a
> single terminal window):
> 
> $ criu image-cache -d --port 1234 -o img-cache.log
> $ criu image-proxy -d --port 1234 --address 0.0.0.0 -o img-proxy.log
> 
> Signed-off-by: Radostin Stoyanov <rstoyanov1 at gmail.com>
> ---
>  criu/img-cache.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/criu/img-cache.c b/criu/img-cache.c
> index ac737150..91f473fb 100644
> --- a/criu/img-cache.c
> +++ b/criu/img-cache.c
> @@ -65,6 +65,13 @@ int image_cache(bool background, char *local_cache_path, unsigned short cache_wr
>  			cache_write_port, local_cache_path);
>  	restoring = true;
>  
> +	if (background) {
> +		if (daemon(1, 0) == -1) {
> +			pr_perror("Can't run service server in the background");
> +			return -1;
> +		}
> +	}

It is a popular pattern when a process is daemonized after creating
listening sockets. In this case, we can be sure that we can connect to a
service immediately after executing it.

For example,

> $ criu image-cache -d --port 1234 -o img-cache.log
> $ criu image-proxy -d --port 1234 --address 0.0.0.0 -o img-proxy.log

The second process can fail, if it calls connect() for a client socket
before the first process calls listen() for a server socket.

What I want to say is that you have to create sockets, then call
daemon() and then do other things (accept(), etc).

In this function, two sockets are created: proxy_to_cache_fd and
local_req_fd.

> +
>  	if (opts.ps_socket != -1) {
>  		proxy_to_cache_fd = opts.ps_socket;
>  		pr_info("Re-using ps socket %d\n", proxy_to_cache_fd);
> @@ -89,13 +96,6 @@ int image_cache(bool background, char *local_cache_path, unsigned short cache_wr
>  
>  	}
>  
> -	if (background) {
> -		if (daemon(1, 0) == -1) {
> -			pr_perror("Can't run service server in the background");
> -			return -1;
> -		}
> -	}
> -
>  	accept_image_connections();
>  	pr_info("Finished image cache.");
>  	return 0;
> -- 
> 2.17.1
> 
> _______________________________________________
> CRIU mailing list
> CRIU at openvz.org
> https://lists.openvz.org/mailman/listinfo/criu


More information about the CRIU mailing list