[6/9] unix: split bind_unix_sk() for readability

Submitted by Andrey Vagin on March 16, 2018, 5:24 p.m.

Details

Message ID 20180316172430.7815-7-avagin@virtuozzo.com
State Accepted
Series "unix: restore sockets in proper mount namespaces"
Commit 94fcc4445b1998c0bae111eb8656201f33decac5
Headers show

Commit Message

Andrey Vagin March 16, 2018, 5:24 p.m.
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 criu/sk-unix.c | 79 ++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 44 insertions(+), 35 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/sk-unix.c b/criu/sk-unix.c
index 466c858e5..af81a1d83 100644
--- a/criu/sk-unix.c
+++ b/criu/sk-unix.c
@@ -1281,6 +1281,49 @@  restore_sk_common:
 	return restore_sk_common(fd, ui);
 }
 
+static int bind_deleted_unix_sk(int sk, struct unix_sk_info *ui,
+					struct sockaddr_un *addr)
+{
+	char temp[PATH_MAX];
+	int ret;
+
+	pr_info("found duplicate unix socket bound at %s\n", addr->sun_path);
+
+	ret = snprintf(temp, sizeof(temp),
+			"%s-%s-%d", addr->sun_path, "criu-temp", getpid());
+	/* this shouldn't happen, since sun_addr is only 108 chars long */
+	if (ret < 0 || ret >= sizeof(temp)) {
+		pr_err("snprintf of %s failed?\n", addr->sun_path);
+		return -1;;
+	}
+
+	ret = rename(addr->sun_path, temp);
+	if (ret < 0) {
+		pr_perror("couldn't move socket for binding");
+		return -1;
+	}
+
+	ret = bind(sk, (struct sockaddr *)addr,
+			sizeof(addr->sun_family) + ui->ue->name.len);
+	if (ret < 0) {
+		pr_perror("Can't bind socket after move");
+		return -1;;
+	}
+
+	ret = rename(temp, addr->sun_path);
+	if (ret < 0) {
+		pr_perror("couldn't move socket back");
+		return -1;
+	}
+
+	/* we've handled the deleted-ness of this
+	 * socket and we don't want to delete it later
+	 * since it's not /this/ socket.
+	 */
+	ui->ue->deleted = false;
+	return 0;
+}
+
 static int bind_unix_sk(int sk, struct unix_sk_info *ui)
 {
 	struct sockaddr_un addr;
@@ -1314,42 +1357,8 @@  static int bind_unix_sk(int sk, struct unix_sk_info *ui)
 				sizeof(addr.sun_family) + ui->ue->name.len);
 		if (ret < 0) {
 			if (ui->ue->has_deleted && ui->ue->deleted && errno == EADDRINUSE) {
-				char temp[PATH_MAX];
-
-				pr_info("found duplicate unix socket bound at %s\n", addr.sun_path);
-
-				ret = snprintf(temp, sizeof(temp), "%s-%s-%d", addr.sun_path, "criu-temp", getpid());
-				/* this shouldn't happen, since sun_addr is only 108 chars long */
-				if (ret < 0 || ret >= sizeof(temp)) {
-					pr_err("snprintf of %s failed?\n", addr.sun_path);
+				if (bind_deleted_unix_sk(sk, ui, &addr))
 					goto done;
-				}
-
-				ret = rename(addr.sun_path, temp);
-				if (ret < 0) {
-					pr_perror("couldn't move socket for binding");
-					goto done;
-				}
-
-				ret = bind(sk, (struct sockaddr *)&addr,
-						sizeof(addr.sun_family) + ui->ue->name.len);
-				if (ret < 0) {
-					pr_perror("Can't bind socket after move");
-					goto done;
-				}
-
-				ret = rename(temp, addr.sun_path);
-				if (ret < 0) {
-					pr_perror("couldn't move socket back");
-					goto done;
-				}
-
-				/* we've handled the deleted-ness of this
-				 * socket and we don't want to delete it later
-				 * since it's not /this/ socket.
-				 */
-				ui->ue->deleted = false;
-
 			} else {
 				pr_perror("Can't bind socket");
 				goto done;