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

Submitted by Radostin Stoyanov on July 8, 2018, 4:47 p.m.

Details

Message ID 20180708164711.857-7-rstoyanov1@gmail.com
State New
Series "Image-{cache,proxy} tweaks"
Headers show

Commit Message

Radostin Stoyanov July 8, 2018, 4:47 p.m.
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@gmail.com>
---
 criu/img-cache.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

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;
+		}
+	}
+
 	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;

Comments

Andrey Vagin July 11, 2018, 5:51 a.m.
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@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@openvz.org
> https://lists.openvz.org/mailman/listinfo/criu