[CRIU] [PATCH] compel: check whether a parasite socket is prepared each time

Pavel Emelyanov xemul at virtuozzo.com
Thu Nov 17 00:19:36 PST 2016


On 11/17/2016 09:45 AM, Andrei Vagin wrote:
> From: Andrei Vagin <avagin at virtuozzo.com>
> 
> Currently we prepare a parasite socket only once and
> save it in a static variable.
> 
> It's bad idea to use a static variable in a library.

Agreed.

> In addition, it doesn't work if we have processes in
> different network namespaces. In this case, we have to have
> a separate socket for each namespace.

Also agreed.

> Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
> ---
>  compel/src/lib/infect.c | 17 ++++++++++++-----
>  1 file changed, 12 insertions(+), 5 deletions(-)
> 
> diff --git a/compel/src/lib/infect.c b/compel/src/lib/infect.c
> index 0d462d8..af8555e 100644
> --- a/compel/src/lib/infect.c
> +++ b/compel/src/lib/infect.c
> @@ -322,20 +322,27 @@ static int gen_parasite_saddr(struct sockaddr_un *saddr, int key)
>  static int prepare_tsock(struct parasite_ctl *ctl, pid_t pid,
>  		struct parasite_init_args *args)
>  {
> -	static int ssock = -1;
> +	int ssock = -1;
> +	socklen_t sk_len;
> +	struct sockaddr_un addr;
>  
>  	pr_info("Putting tsock into pid %d\n", pid);
>  	args->h_addr_len = gen_parasite_saddr(&args->h_addr, getpid());
>  
> -	if (ssock == -1) {
> -		ssock = *ctl->ictx.p_sock;
> +	ssock = *ctl->ictx.p_sock;
> +	sk_len = sizeof(addr);
> +
> +	if (getsockname(ssock, &addr, &sk_len) < 0) {
> +		pr_perror("Unable to get name for a socket");
> +		return -1;
> +	}
> +
> +	if (sk_len == sizeof(addr.sun_family)) {

The kernel's unix_getname places here sizeof(short). Maybe we do the
same here, to bee 100% explicit?

>  		if (ssock == -1) {
>  			pr_err("No socket in ictx\n");
>  			goto err;
>  		}
>  
> -		*ctl->ictx.p_sock = -1;
> -
>  		if (bind(ssock, (struct sockaddr *)&args->h_addr, args->h_addr_len) < 0) {
>  			pr_perror("Can't bind socket");
>  			goto err;
> 



More information about the CRIU mailing list