[PATCH v2] Factorize send_query functions

Fabien Siron fabien.siron at epita.fr
Sun May 8 10:45:52 UTC 2016


* socketutils.c (send_query): New function.
(inet_send_query, unix_send_query): Use it.
---
 socketutils.c | 65 +++++++++++++++++++++++------------------------------------
 1 file changed, 25 insertions(+), 40 deletions(-)

diff --git a/socketutils.c b/socketutils.c
index 640c673..8851198 100644
--- a/socketutils.c
+++ b/socketutils.c
@@ -78,29 +78,14 @@ print_sockaddr_by_inode_cached(const unsigned long inode)
 }
 
 static bool
-inet_send_query(const int fd, const int family, const int proto)
+send_query(const int fd, void *req, size_t req_size)
 {
 	struct sockaddr_nl nladdr = {
 		.nl_family = AF_NETLINK
 	};
-	struct {
-		const struct nlmsghdr nlh;
-		const struct inet_diag_req_v2 idr;
-	} req = {
-		.nlh = {
-			.nlmsg_len = sizeof(req),
-			.nlmsg_type = SOCK_DIAG_BY_FAMILY,
-			.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
-		},
-		.idr = {
-			.sdiag_family = family,
-			.sdiag_protocol = proto,
-			.idiag_states = -1
-		}
-	};
 	struct iovec iov = {
-		.iov_base = &req,
-		.iov_len = sizeof(req)
+		.iov_base = req,
+		.iov_len = req_size
 	};
 	const struct msghdr msg = {
 		.msg_name = &nladdr,
@@ -119,6 +104,27 @@ inet_send_query(const int fd, const int family, const int proto)
 	}
 }
 
+static bool
+inet_send_query(const int fd, const int family, const int proto)
+{
+	struct {
+		const struct nlmsghdr nlh;
+		const struct inet_diag_req_v2 idr;
+	} req = {
+		.nlh = {
+			.nlmsg_len = sizeof(req),
+			.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+			.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
+		},
+		.idr = {
+			.sdiag_family = family,
+			.sdiag_protocol = proto,
+			.idiag_states = -1
+		}
+	};
+	return send_query(fd, &req, sizeof(req));
+}
+
 static int
 inet_parse_response(const char *const proto_name, const void *const data,
 		    const int data_len, const unsigned long inode)
@@ -232,9 +238,6 @@ inet_print(const int fd, const int family, const int protocol,
 static bool
 unix_send_query(const int fd, const unsigned long inode)
 {
-	struct sockaddr_nl nladdr = {
-		.nl_family = AF_NETLINK
-	};
 	struct {
 		const struct nlmsghdr nlh;
 		const struct unix_diag_req udr;
@@ -251,25 +254,7 @@ unix_send_query(const int fd, const unsigned long inode)
 			.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
 		}
 	};
-	struct iovec iov = {
-		.iov_base = &req,
-		.iov_len = sizeof(req)
-	};
-	const struct msghdr msg = {
-		.msg_name = &nladdr,
-		.msg_namelen = sizeof(nladdr),
-		.msg_iov = &iov,
-		.msg_iovlen = 1
-	};
-
-	for (;;) {
-		if (sendmsg(fd, &msg, 0) < 0) {
-			if (errno == EINTR)
-				continue;
-			return false;
-		}
-		return true;
-	}
+	return send_query(fd, &req, sizeof(req));
 }
 
 static int
-- 
2.8.0





More information about the Strace-devel mailing list