Make getfdproto return enum instead of string
Introduce a new enum type sock_proto and use it instead of strings for socket
protocols identification.
* defs.h (sock_proto): New enum.
(get_proto_by_name): New function.
* socketutils.c (protocols): New static table.
(print_sockaddr_by_inode): Use it. Change type of "proto" argument
to sock_proto.
(get_proto_by_name): New function.
* util.c (getfdproto): Use it. Change return type to sock_proto.
(printfd): Update.
diff --git a/util.c b/util.c
index bb23eeb..3122755 100644
--- a/util.c
+++ b/util.c
@@ -472,30 +472,33 @@
return buf;
}
-static char *
-getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
+static enum sock_proto
+getfdproto(struct tcb *tcp, int fd)
{
#ifdef HAVE_SYS_XATTR_H
+ size_t bufsize = 256;
+ char buf[bufsize];
ssize_t r;
char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
if (fd < 0)
- return NULL;
+ return SOCK_PROTO_UNKNOWN;
sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
if (r <= 0)
- return NULL;
+ return SOCK_PROTO_UNKNOWN;
else {
/*
* This is a protection for the case when the kernel
* side does not append a null byte to the buffer.
*/
buf[r] = '\0';
- return buf;
+
+ return get_proto_by_name(buf);
}
#else
- return NULL;
+ return SOCK_PROTO_UNKNOWN;
#endif
}
@@ -516,9 +519,8 @@
strtoul(path + socket_prefix_len, NULL, 10);
if (!print_sockaddr_by_inode_cached(inode)) {
- char buf[256];
- const char *proto =
- getfdproto(tcp, fd, buf, sizeof(buf));
+ const enum sock_proto proto =
+ getfdproto(tcp, fd);
if (!print_sockaddr_by_inode(inode, proto))
tprints(path);
}