[CRIU] [PATCH 17/18] sk-unix: Add ability to restore sockets with deleted vfs addresses

Kirill Tkhai ktkhai at virtuozzo.com
Mon Apr 10 02:16:22 PDT 2017



On 10.04.2017 01:28, Cyrill Gorcunov wrote:
> If dgram sockets are bound with vfs name and the name removed
> from the file system we can't bind/connect to such name. To
> resolve it we do the following
> 
>  - all ghost names previously gathered into chains are
>    changed to have more-less unique names adding postfixes
> 
>  - opon socket opening it's binding/connection is delayed
>    until previous copy is removed
> 
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---
>  criu/sk-unix.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 227 insertions(+), 20 deletions(-)
> 
> diff --git a/criu/sk-unix.c b/criu/sk-unix.c
> index cdad971e3dea..d2808631e144 100644
> --- a/criu/sk-unix.c
> +++ b/criu/sk-unix.c
> @@ -9,6 +9,7 @@
>  #include <sys/un.h>
>  #include <stdlib.h>
>  #include <dlfcn.h>
> +#include <libgen.h>
>  
>  #include "libnetlink.h"
>  #include "cr_options.h"
> @@ -812,6 +813,9 @@ struct unix_sk_info {
>  	struct list_head	node;		/* To link in peer's connected list  */
>  
>  	struct list_head	ghost_addr_node;
> +	struct unix_sk_info	*ghost_master;
> +	atomic_t		ghost_counter;
> +	size_t			ghost_keep;
>  
>  	/*
>  	 * For DGRAM sockets with queues, we should only restore the queue
> @@ -823,6 +827,8 @@ struct unix_sk_info {
>  	bool			listen;
>  };
>  
> +static int bind_unix_sk(int sk, struct unix_sk_info *ui);
> +
>  #define USK_PAIR_MASTER		(1 << 0)
>  #define USK_PAIR_SLAVE		(1 << 1)
>  #define USK_GHOST_NAME		(1 << 2)
> @@ -961,10 +967,27 @@ static int post_open_unix_sk(struct file_desc *d, int fd)
>  {
>  	struct unix_sk_info *ui;
>  	struct unix_sk_info *peer;
> +	struct unix_sk_info *gm;
>  	struct sockaddr_un addr;
>  	int cwd_fd = -1, root_fd = -1;
>  
>  	ui = container_of(d, struct unix_sk_info, d);
> +	gm = ui->ghost_master;
> +
> +	if (ui->flags & USK_GHOST_WAIT) {
> +		if (!(gm->flags & USK_ADDR_RDY))
> +			return 1;

Kiryuha, if we return "1", this means restore of file_desc is not possible
at the moment, and the process may fall to sleep this case. So, it's need
to wake it up, when the restore of the file_desc may continue. I.e., we need
to use set_fds_event() in the places, where we set ui->flags |= USK_ADDR_RDY.

> +		if (ui->flags & (USK_PAIR_MASTER | USK_PAIR_SLAVE)) {
> +			if (bind_unix_sk(fd, ui))
> +				return -1;
> +			return 0;
> +		}
> +	} else if (ui->flags & USK_GHOST_NAME) {
> +		if (bind_unix_sk(fd, ui))
> +			return -1;
> +		return 0;
> +	}
> +
>  	BUG_ON((ui->flags & (USK_PAIR_MASTER | USK_PAIR_SLAVE)) ||
>  			(ui->ue->uflags & (USK_CALLBACK | USK_INHERIT)));
>  
> @@ -993,6 +1016,13 @@ static int post_open_unix_sk(struct file_desc *d, int fd)
>  		return -1;
>  	}
>  
> +	if (gm) {
> +		if (!atomic_dec_and_test(&gm->ghost_counter)) {
> +			if (unlink((char *)gm->ue->name.data) < 0)
> +				pr_perror("failed to unlink %s", gm->ue->name.data);
> +		}
> +	}
> +
>  	revert_unix_sk_cwd(peer, &cwd_fd, &root_fd);
>  
>  	if (peer->queuer == ui->ue->ino && restore_sk_queue(fd, peer->ue->id))
> @@ -1001,6 +1031,71 @@ static int post_open_unix_sk(struct file_desc *d, int fd)
>  	return restore_sk_common(fd, ui);
>  }
>  
> +/*
> + * When path where socket lives is deleted, we need to reconstruct
> + * it back up but allow caller to remove it after.
> + */
> +static int bind_on_deleted(int sk, struct unix_sk_info *ui, size_t *keep)
> +{
> +	char path[PATH_MAX], *pos;
> +	struct sockaddr_un addr;
> +	int ret, _keep;
> +
> +	if (ui->ue->name.len >= sizeof(path)) {
> +		pr_err("Too long name for socket\n");
> +		return -ENOSPC;
> +	}
> +
> +	memcpy(path, ui->name, ui->ue->name.len);
> +	path[ui->ue->name.len] = '\0';
> +
> +	for (pos = strrchr(path, '/'); pos;
> +	     pos = strrchr(path, '/')) {
> +		*pos = '\0';
> +
> +		ret = access(path, R_OK | W_OK | X_OK);
> +		if (ret == 0)
> +			break;
> +
> +		if (errno != ENOENT) {
> +			ret = -errno;
> +			pr_perror("Can't access %s\n", path);
> +			return ret;
> +		}
> +	}
> +
> +	_keep = pos ? pos - path : ui->ue->name.len;
> +
> +	memcpy(path, ui->name, ui->ue->name.len);
> +	path[ui->ue->name.len] = '\0';
> +
> +	pos = dirname(path);
> +	ret = mkdirpat(AT_FDCWD, pos, 0755);
> +	if (ret) {
> +		pr_err("Can't create %s\n", pos);
> +		return ret;
> +	}
> +
> +	memset(&addr, 0, sizeof(addr));
> +	addr.sun_family = AF_UNIX;
> +	memcpy(&addr.sun_path, ui->name, ui->ue->name.len);
> +
> +	ret = bind(sk, (struct sockaddr *)&addr,
> +		   sizeof(addr.sun_family) + ui->ue->name.len);
> +	if (ret < 0) {
> +		pr_perror("Can't bind on socket %s", (char *)ui->ue->name.data);
> +		goto out;
> +	}
> +
> +	*keep = _keep;
> +	return 0;
> +
> +out:
> +	if (rmdirp(pos, _keep))
> +		pr_err("Can't cleanup %s\n", pos);
> +	return ret;
> +}
> +
>  static int bind_unix_sk(int sk, struct unix_sk_info *ui)
>  {
>  	struct sockaddr_un addr;
> @@ -1071,10 +1166,14 @@ static int bind_unix_sk(int sk, struct unix_sk_info *ui)
>  				ui->ue->deleted = false;
>  
>  			} else {
> -				pr_perror("Can't bind socket");
> -				goto done;
> +				if (bind_on_deleted(sk, ui, &ui->ghost_keep)) {
> +					pr_err("Can't bind socket\n");
> +					goto done;
> +				}
> +				ui->flags |= USK_ADDR_RDY;
>  			}
>  		}
> +		ui->flags |= USK_ADDR_RDY;

^^^ To use send_fds_event() somewhere near here.

>  
>  		if (*ui->name && ui->ue->file_perms) {
>  			FilePermsEntry *perms = ui->ue->file_perms;
> @@ -1099,9 +1198,24 @@ static int bind_unix_sk(int sk, struct unix_sk_info *ui)
>  			}
>  		}
>  
> -		if (ui->ue->deleted && unlink((char *)ui->ue->name.data) < 0) {
> -			pr_perror("failed to unlink %s", ui->ue->name.data);
> -			goto done;
> +		if (ui->ue->deleted) {
> +			struct unix_sk_info *gm = ui->ghost_master;
> +			bool do_unlink = true;
> +
> +			if (gm) {
> +				if (!atomic_dec_and_test(&gm->ghost_counter))
> +					do_unlink = false;
> +			}
> +
> +			if (!atomic_dec_and_test(&ui->ghost_counter))
> +				do_unlink = false;
> +
> +			if (do_unlink) {
> +				if (unlink((char *)ui->ue->name.data) < 0) {
> +					pr_perror("failed to unlink %s", ui->ue->name.data);
> +					goto done;
> +				}
> +			}
>  		}
>  	}
>  
> @@ -1118,7 +1232,7 @@ done:
>  
>  static int open_unixsk_pair_master(struct unix_sk_info *ui, int *new_fd)
>  {
> -	int sk[2];
> +	int sk[2], ret = 0;
>  	struct unix_sk_info *peer = ui->peer;
>  
>  	pr_info("Opening pair master (id %#x ino %#x peer %#x)\n",
> @@ -1137,8 +1251,11 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui, int *new_fd)
>  	if (restore_sk_queue(sk[1], ui->ue->id))
>  		return -1;
>  
> -	if (bind_unix_sk(sk[0], ui))
> -		return -1;
> +	if (!(ui->flags & USK_GHOST_WAIT)) {
> +		if (bind_unix_sk(sk[0], ui))
> +			return -1;
> +	} else
> +		ret = 1;
>  
>  	if (restore_sk_common(sk[0], ui))
>  		return -1;
> @@ -1151,12 +1268,12 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui, int *new_fd)
>  	close(sk[1]);
>  
>  	*new_fd = sk[0];
> -	return 0;
> +	return ret;
>  }
>  
>  static int open_unixsk_pair_slave(struct unix_sk_info *ui, int *new_fd)
>  {
> -	int sk, ret;
> +	int sk, ret = 0;
>  
>  	ret = recv_desc_from_peer(&ui->d, &sk);
>  	if (ret != 0) {
> @@ -1165,19 +1282,22 @@ static int open_unixsk_pair_slave(struct unix_sk_info *ui, int *new_fd)
>  		return ret;
>  	}
>  
> -	if (bind_unix_sk(sk, ui))
> -		return -1;
> +	if (!(ui->flags & USK_GHOST_WAIT)) {
> +		if (bind_unix_sk(sk, ui))
> +			return -1;
> +	} else
> +		ret = 1;
>  
>  	if (restore_sk_common(sk, ui))
>  		return -1;
>  
>  	*new_fd = sk;
> -	return 0;
> +	return ret;
>  }
>  
>  static int open_unixsk_standalone(struct unix_sk_info *ui, int *new_fd)
>  {
> -	int sk;
> +	int sk, ret = 0;
>  
>  	pr_info("Opening standalone socket (id %#x ino %#x peer %#x)\n",
>  			ui->ue->id, ui->ue->ino, ui->ue->peer);
> @@ -1295,8 +1415,11 @@ static int open_unixsk_standalone(struct unix_sk_info *ui, int *new_fd)
>  		}
>  	}
>  
> -	if (bind_unix_sk(sk, ui))
> -		return -1;
> +	if (!(ui->flags & USK_GHOST_WAIT)) {
> +		if (bind_unix_sk(sk, ui))
> +			return -1;
> +	} else
> +		ret = 1;
>  
>  	if (ui->ue->state == TCP_LISTEN) {
>  		pr_info("\tPutting %#x into listen state\n", ui->ue->ino);
> @@ -1323,7 +1446,7 @@ out:
>  		return -1;
>  
>  	*new_fd = sk;
> -	return 0;
> +	return ret;
>  }
>  
>  static int open_unix_sk(struct file_desc *d, int *new_fd)
> @@ -1332,12 +1455,12 @@ static int open_unix_sk(struct file_desc *d, int *new_fd)
>  	struct unix_sk_info *ui;
>  	int ret;
>  
> +	ui = container_of(d, struct unix_sk_info, d);
> +
>  	fle = file_master(d);
>  	if (fle->stage >= FLE_OPEN)
>  		return post_open_unix_sk(d, fle->fe->fd);
>  
> -	ui = container_of(d, struct unix_sk_info, d);
> -
>  	if (inherited_fd(d, new_fd)) {
>  		ui->ue->uflags |= USK_INHERIT;
>  		ret = *new_fd >= 0 ? 0 : -1;
> @@ -1408,6 +1531,9 @@ static int collect_one_unixsk(void *o, ProtobufCMessage *base, struct cr_img *i)
>  	ui->name_dir = (void *)ui->ue->name_dir;
>  
>  	INIT_LIST_HEAD(&ui->ghost_addr_node);
> +	ui->ghost_master = NULL;
> +	atomic_set(&ui->ghost_counter, 1);
> +	ui->ghost_keep = 0;
>  
>  	if (add_post_prepare_cb_once(resolve_unix_peers, NULL))
>  		return -1;
> @@ -1515,6 +1641,87 @@ static void interconnected_pair(struct unix_sk_info *ui, struct unix_sk_info *pe
>  	}
>  }
>  
> +static int ghost_new_name(char *name, size_t namelen,
> +			  char **name_new, size_t *namelen_new)
> +{
> +	static unsigned int cnt = 0;
> +	char sname[64];
> +	size_t k;
> +
> +	k = snprintf(sname, sizeof(sname), "criu-%u", cnt++);
> +	*namelen_new = namelen + k + 1;
> +	if (*namelen_new > UNIX_PATH_MAX) {
> +		pr_err("ghost: New name for socket is too long\n");
> +		return -1;
> +	}
> +
> +	*name_new = shmalloc(*namelen_new);
> +	if (!*name_new) {
> +		pr_err("ghost: Can't allocate new name for socket\n");
> +		return -ENOMEM;
> +	}
> +
> +	k = snprintf(*name_new, *namelen_new, "%s-%s", name, sname) + 1;
> +	if (k != *namelen_new) {
> +		pr_err("ghost: Name stripped\n");
> +		return -1;
> +	}
> +
> +	return 0;
> +}
> +
> +static int resolve_unix_ghosts(void)
> +{
> +	struct unix_sk_info *ui, *t;
> +	ghost_addr_t *ga;
> +
> +	pr_debug("ghost: Resolving addresses\n");
> +
> +	list_for_each_entry(ga, &unix_ghost_addr, list) {
> +		pr_debug("ghost: address %s\n", ga->name);
> +
> +		list_for_each_entry(ui, &ga->children, ghost_addr_node) {
> +			size_t newnamelen;
> +			char *newname;
> +
> +			if (ui->flags & USK_GHOST_NAME)
> +				continue;
> +
> +			pr_debug("\tghost: ino %#x peer %#x\n", ui->ue->ino,
> +				 ui->peer ? ui->peer->ue->ino : 0);
> +
> +			if (ghost_new_name(ga->name, ga->namelen,
> +					   &newname, &newnamelen))
> +				return -1;
> +
> +			pr_debug("\tghost: name transition %s -> %s\n",
> +				 ui->name, newname);
> +			ui->name = newname;
> +			ui->ue->name.len = newnamelen;
> +			ui->ue->name.data = (void *)newname;
> +			ui->flags |= USK_GHOST_NAME;
> +			atomic_inc(&ui->ghost_counter);
> +
> +			unlink_stale(ui);
> +
> +			list_for_each_entry(t, &unix_sockets, list) {
> +				if (t->flags & (USK_GHOST_NAME | USK_GHOST_WAIT))
> +					continue;
> +				if (t->peer != ui)
> +					continue;
> +				pr_debug("\t\tghost: connected to us %#x -> %#x\n",
> +					 t->ue->ino, ui->ue->ino);
> +
> +				t->flags |= USK_GHOST_NAME | USK_GHOST_WAIT;
> +				t->ghost_master = ui;
> +				atomic_inc(&ui->ghost_counter);
> +			}
> +		}
> +	}
> +
> +	return 0;
> +}
> +
>  static int resolve_unix_peers(void *unused)
>  {
>  	struct unix_sk_info *ui, *peer;
> @@ -1559,7 +1766,7 @@ static int resolve_unix_peers(void *unused)
>  
>  	}
>  
> -	return 0;
> +	return resolve_unix_ghosts();
>  }
>  
>  int unix_sk_id_add(unsigned int ino)

Kirill


More information about the CRIU mailing list