[2/2,v2] lazy-pages: use abstract UNIX socket

Submitted by Andrei Vagin on Nov. 2, 2016, 3:51 p.m.

Details

Message ID 1478101864-13133-1-git-send-email-avagin@openvz.org
State Superseded
Series "lazy-pages: misc fixes"
Headers show

Commit Message

Andrei Vagin Nov. 2, 2016, 3:51 p.m.
From: Andrei Vagin <avagin@virtuozzo.com>

Replace file-system based UNIX socket name with abstract. This ensures we
won't run into problems with invalid socket names.

v2: fix compilation on arm

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
Acked-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
---
 criu/uffd.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/uffd.c b/criu/uffd.c
index 2a9dab4..8cd788a 100644
--- a/criu/uffd.c
+++ b/criu/uffd.c
@@ -123,26 +123,26 @@  static void lpi_hash_fini(void)
 			lpi_fini(p);
 }
 
-static int prepare_sock_addr(struct sockaddr_un *saddr)
+static int prepare_sock_addr(struct sockaddr_un *saddr, socklen_t *plen)
 {
-	char cwd[PATH_MAX];
+	struct stat st;
 	int len;
 
-	if (!getcwd(cwd, PATH_MAX)) {
-		pr_perror("Cannot get CWD\n");
+	if (stat(".", &st) == -1)
 		return -1;
-	}
 
 	memset(saddr, 0, sizeof(struct sockaddr_un));
 
 	saddr->sun_family = AF_UNIX;
 	len = snprintf(saddr->sun_path, sizeof(saddr->sun_path),
-		       "%s/%s", cwd, LAZY_PAGES_SOCK_NAME);
+		       "X/%s/%lx/%lx", LAZY_PAGES_SOCK_NAME, st.st_dev, st.st_ino);
 	if (len >= sizeof(saddr->sun_path)) {
-		pr_err("Wrong UNIX socket name: %s/%s\n",
-		       cwd, LAZY_PAGES_SOCK_NAME);
+		pr_err("Wrong UNIX socket name: %s/%lx/%lx\n",
+			LAZY_PAGES_SOCK_NAME, st.st_dev, st.st_ino);
 		return -1;
 	}
+	*plen = SUN_LEN(saddr);
+	saddr->sun_path[0] = 0;
 
 	return 0;
 }
@@ -253,13 +253,13 @@  err:
 int prepare_lazy_pages_socket(void)
 {
 	int fd, new_fd;
-	int len;
+	socklen_t len;
 	struct sockaddr_un sun;
 
 	if (!opts.lazy_pages)
 		return 0;
 
-	if (prepare_sock_addr(&sun))
+	if (prepare_sock_addr(&sun, &len))
 		return -1;
 
 	lazy_sock_mutex = shmalloc(sizeof(*lazy_sock_mutex));
@@ -276,7 +276,6 @@  int prepare_lazy_pages_socket(void)
 	if (new_fd < 0)
 		return -1;
 
-	len = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path);
 	if (connect(new_fd, (struct sockaddr *) &sun, len) < 0) {
 		pr_perror("connect to %s failed", sun.sun_path);
 		close(new_fd);
@@ -286,18 +285,15 @@  int prepare_lazy_pages_socket(void)
 	return 0;
 }
 
-static int server_listen(struct sockaddr_un *saddr)
+static int server_listen(struct sockaddr_un *saddr, int len)
 {
 	int fd;
-	int len;
 
 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
 		return -1;
 
 	unlink(saddr->sun_path);
 
-	len = offsetof(struct sockaddr_un, sun_path) + strlen(saddr->sun_path);
-
 	if (bind(fd, (struct sockaddr *) saddr, len) < 0) {
 		goto out;
 	}
@@ -846,11 +842,11 @@  static int prepare_uffds(int epollfd)
 	socklen_t len;
 	struct sockaddr_un saddr;
 
-	if (prepare_sock_addr(&saddr))
+	if (prepare_sock_addr(&saddr, &len))
 		return -1;
 
 	pr_debug("Waiting for incoming connections on %s\n", saddr.sun_path);
-	if ((listen = server_listen(&saddr)) < 0) {
+	if ((listen = server_listen(&saddr, len)) < 0) {
 		pr_perror("server_listen error");
 		return -1;
 	}