[lxc-devel] [lxc/master] af_unix: add function to remove duplicated codes for set sockaddr

2xsec on Github lxc-bot at linuxcontainers.org
Wed Sep 19 10:07:05 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 464 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180919/f1decaa9/attachment.bin>
-------------- next part --------------
From a6e1176239c2bbea204df162d452fbba53eb17c4 Mon Sep 17 00:00:00 2001
From: 2xsec <dh48.jeong at samsung.com>
Date: Wed, 19 Sep 2018 19:05:06 +0900
Subject: [PATCH] af_unix: add function to remove duplicated codes for set
 sockaddr

Signed-off-by: 2xsec <dh48.jeong at samsung.com>
---
 src/lxc/af_unix.c | 67 ++++++++++++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/src/lxc/af_unix.c b/src/lxc/af_unix.c
index 0afc6590c..a67774c9e 100644
--- a/src/lxc/af_unix.c
+++ b/src/lxc/af_unix.c
@@ -42,6 +42,34 @@
 
 lxc_log_define(af_unix, lxc);
 
+static size_t lxc_abstract_unix_set_sockaddr(struct sockaddr_un *addr,
+				const char *path)
+{
+	size_t len;
+
+	if (!addr || !path) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	/* Clear address structure */
+	memset(addr, 0, sizeof(*addr));
+
+	addr->sun_family = AF_UNIX;
+
+	len = strlen(&path[1]);
+
+	/* do not enforce \0-termination */
+	if (len >= sizeof(addr->sun_path)) {
+		errno = ENAMETOOLONG;
+		return -1;
+	}
+
+	/* do not enforce \0-termination */
+	memcpy(&addr->sun_path[1], &path[1], len);
+	return len;
+}
+
 int lxc_abstract_unix_open(const char *path, int type, int flags)
 {
 	int fd, ret;
@@ -52,40 +80,32 @@ int lxc_abstract_unix_open(const char *path, int type, int flags)
 	if (fd < 0)
 		return -1;
 
-	/* Clear address structure */
-	memset(&addr, 0, sizeof(addr));
-
 	if (!path)
 		return fd;
 
-	addr.sun_family = AF_UNIX;
-
-	len = strlen(&path[1]);
-	/* do not enforce \0-termination */
-	if (len >= sizeof(addr.sun_path)) {
+	len = lxc_abstract_unix_set_sockaddr(&addr, path);
+	if (len < 0) {
+		int saved_errno = errno;
 		close(fd);
-		errno = ENAMETOOLONG;
+		errno = saved_errno;
 		return -1;
 	}
 
-	/* do not enforce \0-termination */
-	memcpy(&addr.sun_path[1], &path[1], len);
-
 	ret = bind(fd, (struct sockaddr *)&addr,
 		   offsetof(struct sockaddr_un, sun_path) + len + 1);
 	if (ret < 0) {
-		int saved_erron = errno;
+		int saved_errno = errno;
 		close(fd);
-		errno = saved_erron;
+		errno = saved_errno;
 		return -1;
 	}
 
 	if (type == SOCK_STREAM) {
 		ret = listen(fd, 100);
 		if (ret < 0) {
-			int saved_erron = errno;
+			int saved_errno = errno;
 			close(fd);
-			errno = saved_erron;
+			errno = saved_errno;
 			return -1;
 		}
 	}
@@ -108,21 +128,14 @@ int lxc_abstract_unix_connect(const char *path)
 	if (fd < 0)
 		return -1;
 
-	memset(&addr, 0, sizeof(addr));
-
-	addr.sun_family = AF_UNIX;
-
-	len = strlen(&path[1]);
-	/* do not enforce \0-termination */
-	if (len >= sizeof(addr.sun_path)) {
+	len = lxc_abstract_unix_set_sockaddr(&addr, path);
+	if (len < 0) {
+		int saved_errno = errno;
 		close(fd);
-		errno = ENAMETOOLONG;
+		errno = saved_errno;
 		return -1;
 	}
 
-	/* do not enforce \0-termination */
-	memcpy(&addr.sun_path[1], &path[1], len);
-
 	ret = connect(fd, (struct sockaddr *)&addr,
 		      offsetof(struct sockaddr_un, sun_path) + len + 1);
 	if (ret < 0) {


More information about the lxc-devel mailing list