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

Andrei Vagin avagin at openvz.org
Wed Nov 16 22:45:31 PST 2016


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.

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.

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)) {
 		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;
-- 
2.7.4



More information about the CRIU mailing list