[CRIU] [PATCH 14/15] sockets: Improve helpers to make sockets more readable

Cyrill Gorcunov gorcunov at gmail.com
Thu Sep 13 22:57:57 MSK 2018


Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 criu/include/sockets.h |  37 ++++++++++++-
 criu/sk-unix.c         |  13 +++--
 criu/sockets.c         | 118 ++++++++++++++++++++++++++---------------
 3 files changed, 119 insertions(+), 49 deletions(-)

diff --git a/criu/include/sockets.h b/criu/include/sockets.h
index 99e5762f76f6..15dca9af93cb 100644
--- a/criu/include/sockets.h
+++ b/criu/include/sockets.h
@@ -1,6 +1,7 @@
 #ifndef __CR_SOCKETS_H__
 #define __CR_SOCKETS_H__
 
+#include <alloca.h>
 #include <stdbool.h>
 #include <sys/socket.h>
 
@@ -97,7 +98,39 @@ extern int set_netns(uint32_t ns_id);
 extern int kerndat_socket_netns(void);
 extern int kerndat_socket_unix_file(void);
 
-extern const char *tcp_state_name(unsigned int state);
-extern const char *socket_type_name(unsigned int type);
+extern const char *tcp_state_name(unsigned int state, char *nm, size_t size);
+extern const char *socket_type_name(unsigned int type, char *nm, size_t size);
+extern const char *socket_family_name(unsigned int family, char *nm, size_t size);
+extern const char *socket_proto_name(unsigned int proto, char *nm, size_t size);
+
+static __always_inline const char *___tcp_state_name(unsigned int state)
+{
+	char *nm = alloca(32);
+	return tcp_state_name(state, nm, 32);
+}
+
+static __always_inline const char *___socket_type_name(unsigned int type)
+{
+	char *nm = alloca(32);
+	return socket_type_name(type, nm, 32);
+}
+
+static __always_inline const char *___socket_family_name(unsigned int family)
+{
+	char *nm = alloca(32);
+	return socket_family_name(family, nm, 32);
+}
+
+static __always_inline const char *___socket_proto_name(unsigned int proto)
+{
+	char *nm = alloca(32);
+	return socket_proto_name(proto, nm, 32);
+}
+
+
+#define __tcp_state_name(state, a)	tcp_state_name(state, a, sizeof(a))
+#define __socket_type_name(type, a)	socket_type_name(type, a, sizeof(a))
+#define __socket_family_name(family, a)	socket_family_name(type, a, sizeof(a))
+#define __socket_proto_name(proto, a)	socket_proto_name(proto, a, sizeof(a))
 
 #endif /* __CR_SOCKETS_H__ */
diff --git a/criu/sk-unix.c b/criu/sk-unix.c
index 2091cad2f73e..90124ed07acf 100644
--- a/criu/sk-unix.c
+++ b/criu/sk-unix.c
@@ -2060,9 +2060,12 @@ int unix_prepare_root_shared(void)
 	pr_debug("ghost: Resolving addresses\n");
 
 	list_for_each_entry(ui, &unix_ghost_addr, ghost_node) {
+		char tp_name[32];
+		char st_name[32];
+
 		pr_debug("ghost: id %#x type %s state %s ino %d peer %d address %s\n",
-			 ui->ue->id, socket_type_name(ui->ue->type),
-			 tcp_state_name(ui->ue->state),
+			 ui->ue->id, __socket_type_name(ui->ue->type, tp_name),
+			 __tcp_state_name(ui->ue->state, st_name),
 			 ui->ue->ino, ui->peer ? ui->peer->ue->ino : 0,
 			 ui->name);
 
@@ -2109,9 +2112,9 @@ static int collect_one_unixsk(void *o, ProtobufCMessage *base, struct cr_img *i)
 	}
 
 	pr_info(" `- Got id %#x ino %d type %s state %s peer %d (name %s%.*s dir %s)\n",
-		ui->ue->id, ui->ue->ino, socket_type_name(ui->ue->type),
-		tcp_state_name(ui->ue->state), ui->ue->peer, prefix, ulen, uname,
-		ui->name_dir ? ui->name_dir : "-");
+		ui->ue->id, ui->ue->ino, ___socket_type_name(ui->ue->type),
+		___tcp_state_name(ui->ue->state), ui->ue->peer, prefix, ulen,
+		uname, ui->name_dir ? ui->name_dir : "-");
 
 	if (ui->ue->peer || ui->name) {
 		if (ui->ue->peer)
diff --git a/criu/sockets.c b/criu/sockets.c
index cb19dcc87131..fc689ea834b2 100644
--- a/criu/sockets.c
+++ b/criu/sockets.c
@@ -42,48 +42,82 @@
 #define SO_GET_FILTER	SO_ATTACH_FILTER
 #endif
 
-const char *socket_type_name(unsigned int type)
-{
-       static const char *types[] = {
-               [SOCK_STREAM]           = __stringify_1(SOCK_STREAM),
-               [SOCK_DGRAM]            = __stringify_1(SOCK_DGRAM),
-               [SOCK_RAW]              = __stringify_1(SOCK_RAW),
-               [SOCK_SEQPACKET]        = __stringify_1(SOCK_SEQPACKET),
-               [SOCK_PACKET]           = __stringify_1(SOCK_PACKET),
-       };
-       size_t i;
-
-       for (i = 0; i < ARRAY_SIZE(types); i++) {
-               if (type == i)
-                       return types[i];
-       }
-
-       return "UNKNOWN";
-}
-
-const char *tcp_state_name(unsigned int state)
-{
-       static const char *states[] = {
-               [TCP_ESTABLISHED]       = __stringify_1(TCP_ESTABLISHED),
-               [TCP_SYN_SENT]          = __stringify_1(TCP_SYN_SENT),
-               [TCP_SYN_RECV]          = __stringify_1(TCP_SYN_RECV),
-               [TCP_FIN_WAIT1]         = __stringify_1(TCP_FIN_WAIT1),
-               [TCP_FIN_WAIT2]         = __stringify_1(TCP_FIN_WAIT2),
-               [TCP_TIME_WAIT]         = __stringify_1(TCP_TIME_WAIT),
-               [TCP_CLOSE]             = __stringify_1(TCP_CLOSE),
-               [TCP_CLOSE_WAIT]        = __stringify_1(TCP_CLOSE_WAIT),
-               [TCP_LAST_ACK]          = __stringify_1(TCP_LAST_ACK),
-               [TCP_LISTEN]            = __stringify_1(TCP_LISTEN),
-               [TCP_CLOSING]           = __stringify_1(TCP_CLOSING),
-       };
-       size_t i;
-
-       for (i = 0; i < ARRAY_SIZE(states); i++) {
-               if (state == i)
-                       return states[i];
-       }
-
-       return "UNKNOWN";
+static const char *__socket_const_name(char *dst, size_t len, const char **a, size_t n, unsigned int v)
+{
+	if (v < n) {
+		const char *name = a[v];
+		if (name)
+			return name;
+	}
+	snprintf(dst, len, "%u", v);
+	return dst;
+}
+
+const char *socket_proto_name(unsigned int proto, char *nm, size_t size)
+{
+	static const char *protos[] = {
+		[IPPROTO_IP]		= __stringify_1(IPPROTO_IP),
+		[IPPROTO_ICMP]		= __stringify_1(IPPROTO_ICMP),
+		[IPPROTO_IGMP]		= __stringify_1(IPPROTO_IGMP),
+		[IPPROTO_IPIP]		= __stringify_1(IPPROTO_IPIP),
+		[IPPROTO_TCP]		= __stringify_1(IPPROTO_TCP),
+		[IPPROTO_EGP]		= __stringify_1(IPPROTO_EGP),
+		[IPPROTO_UDP]		= __stringify_1(IPPROTO_UDP),
+		[IPPROTO_DCCP]		= __stringify_1(IPPROTO_DCCP),
+		[IPPROTO_IPV6]		= __stringify_1(IPPROTO_IPV6),
+		[IPPROTO_RSVP]		= __stringify_1(IPPROTO_RSVP),
+		[IPPROTO_GRE]		= __stringify_1(IPPROTO_GRE),
+		[IPPROTO_ESP]		= __stringify_1(IPPROTO_ESP),
+		[IPPROTO_AH]		= __stringify_1(IPPROTO_AH),
+		[IPPROTO_UDPLITE]	= __stringify_1(IPPROTO_UDPLITE),
+		[IPPROTO_RAW]		= __stringify_1(IPPROTO_RAW),
+	};
+	return __socket_const_name(nm, size, protos, ARRAY_SIZE(protos), proto);
+}
+
+const char *socket_family_name(unsigned int family, char *nm, size_t size)
+{
+	static const char *families[] = {
+		[AF_UNIX]		= __stringify_1(AF_UNIX),
+		[AF_INET]		= __stringify_1(AF_INET),
+		[AF_BRIDGE]		= __stringify_1(AF_BRIDGE),
+		[AF_INET6]		= __stringify_1(AF_INET6),
+		[AF_KEY]		= __stringify_1(AF_KEY),
+		[AF_NETLINK]		= __stringify_1(AF_NETLINK),
+		[AF_PACKET]		= __stringify_1(AF_PACKET),
+	};
+	/* WARN: This is not concurrent safe! */
+	return __socket_const_name(nm, size, families, ARRAY_SIZE(families), family);
+}
+
+const char *socket_type_name(unsigned int type, char *nm, size_t size)
+{
+	static const char *types[] = {
+		[SOCK_STREAM]		= __stringify_1(SOCK_STREAM),
+		[SOCK_DGRAM]		= __stringify_1(SOCK_DGRAM),
+		[SOCK_RAW]		= __stringify_1(SOCK_RAW),
+		[SOCK_SEQPACKET]	= __stringify_1(SOCK_SEQPACKET),
+		[SOCK_PACKET]		= __stringify_1(SOCK_PACKET),
+	};
+	return __socket_const_name(nm, size, types, ARRAY_SIZE(types), type);
+}
+
+const char *tcp_state_name(unsigned int state, char *nm, size_t size)
+{
+	static const char *states[] = {
+		[TCP_ESTABLISHED]	= __stringify_1(TCP_ESTABLISHED),
+		[TCP_SYN_SENT]		= __stringify_1(TCP_SYN_SENT),
+		[TCP_SYN_RECV]		= __stringify_1(TCP_SYN_RECV),
+		[TCP_FIN_WAIT1]		= __stringify_1(TCP_FIN_WAIT1),
+		[TCP_FIN_WAIT2]		= __stringify_1(TCP_FIN_WAIT2),
+		[TCP_TIME_WAIT]		= __stringify_1(TCP_TIME_WAIT),
+		[TCP_CLOSE]		= __stringify_1(TCP_CLOSE),
+		[TCP_CLOSE_WAIT]	= __stringify_1(TCP_CLOSE_WAIT),
+		[TCP_LAST_ACK]		= __stringify_1(TCP_LAST_ACK),
+		[TCP_LISTEN]		= __stringify_1(TCP_LISTEN),
+		[TCP_CLOSING]		= __stringify_1(TCP_CLOSING),
+	};
+	return __socket_const_name(nm, size, states, ARRAY_SIZE(states), state);
 }
 
 struct sock_diag_greq {
-- 
2.17.1



More information about the CRIU mailing list