[lxc-devel] [PATCH] cgmanager chown: close socketpair on error paths

Serge Hallyn serge.hallyn at ubuntu.com
Wed Jan 29 17:31:49 UTC 2014


Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/cgmanager.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/lxc/cgmanager.c b/src/lxc/cgmanager.c
index 797e2ae..6ff7f32 100644
--- a/src/lxc/cgmanager.c
+++ b/src/lxc/cgmanager.c
@@ -168,7 +168,7 @@ struct chown_data {
 
 static int do_chown_cgroup(const char *controller, const char *cgroup_path)
 {
-	int sv[2] = {-1, -1}, optval = 1;
+	int sv[2] = {-1, -1}, optval = 1, ret = -1;
 	char buf[1];
 
 	if (setgid(0) < 0)
@@ -178,20 +178,20 @@ static int do_chown_cgroup(const char *controller, const char *cgroup_path)
 
 	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) < 0) {
 		SYSERROR("Error creating socketpair");
-		return -1;
+		goto out;
 	}
 	if (setsockopt(sv[1], SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1) {
 		SYSERROR("setsockopt failed");
-		return -1;
+		goto out;
 	}
 	if (setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1) {
 		SYSERROR("setsockopt failed");
-		return -1;
+		goto out;
 	}
 	if ( cgmanager_chown_scm_sync(NULL, cgroup_manager, controller,
 				       cgroup_path, sv[1]) != 0) {
 		ERROR("call to cgmanager_chown_scm_sync failed");
-		return -1;
+		goto out;
 	}
 	/* now send credentials */
 
@@ -200,37 +200,38 @@ static int do_chown_cgroup(const char *controller, const char *cgroup_path)
 	FD_SET(sv[0], &rfds);
 	if (select(sv[0]+1, &rfds, NULL, NULL, NULL) < 0) {
 		ERROR("Error getting go-ahead from server: %s", strerror(errno));
-		return -1;
+		goto out;
 	}
 	if (read(sv[0], &buf, 1) != 1) {
 		ERROR("Error getting reply from server over socketpair");
-		return -1;
+		goto out;
 	}
 	if (send_creds(sv[0], getpid(), getuid(), getgid())) {
 		ERROR("Error sending pid over SCM_CREDENTIAL");
-		return -1;
+		goto out;
 	}
 	FD_ZERO(&rfds);
 	FD_SET(sv[0], &rfds);
 	if (select(sv[0]+1, &rfds, NULL, NULL, NULL) < 0) {
 		ERROR("Error getting go-ahead from server: %s", strerror(errno));
-		return -1;
+		goto out;
 	}
 	if (read(sv[0], &buf, 1) != 1) {
 		ERROR("Error getting reply from server over socketpair");
-		return -1;
+		goto out;
 	}
 	if (send_creds(sv[0], getpid(), 0, 0)) {
 		ERROR("Error sending pid over SCM_CREDENTIAL");
-		return -1;
+		goto out;
 	}
 	FD_ZERO(&rfds);
 	FD_SET(sv[0], &rfds);
 	if (select(sv[0]+1, &rfds, NULL, NULL, NULL) < 0) {
 		ERROR("Error getting go-ahead from server: %s", strerror(errno));
-		return -1;
+		goto out;
 	}
-	int ret = read(sv[0], buf, 1);
+	ret = read(sv[0], buf, 1);
+out:
 	close(sv[0]);
 	close(sv[1]);
 	if (ret == 1 && *buf == '1')
-- 
1.8.5.3



More information about the lxc-devel mailing list