]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: use extra information loaded from /proc/net/icmp
authorMasatake YAMATO <yamato@redhat.com>
Thu, 23 Feb 2023 15:09:54 +0000 (00:09 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Sun, 26 Feb 2023 08:26:40 +0000 (17:26 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-sock-xinfo.c
misc-utils/lsfd.1.adoc
misc-utils/lsfd.c
misc-utils/lsfd.h

index 59037729b5e906f0eb5e8ad8d6a65247729d4bb0..91c10bd7bd2bc23e7fbdbb97b1a0ab252f6f35bc 100644 (file)
@@ -39,6 +39,7 @@
 #include "lsfd.h"
 #include "lsfd-sock.h"
 
+static void load_xinfo_from_proc_icmp(ino_t netns_inode);
 static void load_xinfo_from_proc_unix(ino_t netns_inode);
 static void load_xinfo_from_proc_raw(ino_t netns_inode);
 static void load_xinfo_from_proc_tcp(ino_t netns_inode);
@@ -92,6 +93,7 @@ static void load_sock_xinfo_no_nsswitch(ino_t netns)
        load_xinfo_from_proc_udp6(netns);
        load_xinfo_from_proc_udplite6(netns);
        load_xinfo_from_proc_raw6(netns);
+       load_xinfo_from_proc_icmp(netns);
 }
 
 static void load_sock_xinfo_with_fd(int fd, ino_t netns)
@@ -889,8 +891,9 @@ struct raw_xinfo {
        uint16_t protocol;
 };
 
-static char *raw_get_name(struct sock_xinfo *sock_xinfo,
-                         struct sock *sock  __attribute__((__unused__)))
+static char *raw_get_name_common(struct sock_xinfo *sock_xinfo,
+                                struct sock *sock  __attribute__((__unused__)),
+                                const char *port_label)
 {
        char *str = NULL;
        struct l4_xinfo_class *class = (struct l4_xinfo_class *)sock_xinfo->class;
@@ -906,16 +909,24 @@ static char *raw_get_name(struct sock_xinfo *sock_xinfo,
                xasprintf(&str, "state=%s", st_str);
        else if (class->is_any_addr(raddr)
                 || !inet_ntop(class->family, raddr, remote_s, sizeof(remote_s)))
-               xasprintf(&str, "state=%s protocol=%"PRIu16" laddr=%s",
+               xasprintf(&str, "state=%s %s=%"PRIu16" laddr=%s",
                          st_str,
+                         port_label,
                          raw->protocol, local_s);
        else
-               xasprintf(&str, "state=%s protocol=%"PRIu16" laddr=%s raddr=%s",
+               xasprintf(&str, "state=%s %s=%"PRIu16" laddr=%s raddr=%s",
                          st_str,
+                         port_label,
                          raw->protocol, local_s, remote_s);
        return str;
 }
 
+static char *raw_get_name(struct sock_xinfo *sock_xinfo,
+                         struct sock *sock  __attribute__((__unused__)))
+{
+       return raw_get_name_common(sock_xinfo, sock, "protocol");
+}
+
 static char *raw_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)),
                          struct sock *sock __attribute__((__unused__)))
 {
@@ -1001,6 +1012,64 @@ static void load_xinfo_from_proc_raw(ino_t netns_inode)
                                     &raw_xinfo_class);
 }
 
+/*
+ * PING
+ */
+static char *ping_get_name(struct sock_xinfo *sock_xinfo,
+                         struct sock *sock  __attribute__((__unused__)))
+{
+       return raw_get_name_common(sock_xinfo, sock, "id");
+}
+
+static char *ping_get_type(struct sock_xinfo *sock_xinfo __attribute__((__unused__)),
+                          struct sock *sock __attribute__((__unused__)))
+{
+       return strdup("dgram");
+}
+
+static bool ping_fill_column(struct proc *proc __attribute__((__unused__)),
+                            struct sock_xinfo *sock_xinfo,
+                            struct sock *sock __attribute__((__unused__)),
+                            struct libscols_line *ln __attribute__((__unused__)),
+                            int column_id,
+                            size_t column_index __attribute__((__unused__)),
+                            char **str)
+{
+       if (l3_fill_column_handler(INET, sock_xinfo, column_id, str))
+               return true;
+
+       if (column_id == COL_PING_ID) {
+               xasprintf(str, "%"PRIu16,
+                         ((struct raw_xinfo *)sock_xinfo)->protocol);
+               return true;
+       }
+
+       return false;
+}
+
+static const struct l4_xinfo_class ping_xinfo_class = {
+       .sock = {
+               .get_name = ping_get_name,
+               .get_type = ping_get_type,
+               .get_state = tcp_get_state,
+               .get_listening = NULL,
+               .fill_column = ping_fill_column,
+               .free = NULL,
+       },
+       .scan_line = raw_xinfo_scan_line,
+       .get_addr = tcp_xinfo_get_addr,
+       .is_any_addr = tcp_xinfo_is_any_addr,
+       .family = AF_INET,
+       .l3_decorator = {"", ""},
+};
+
+static void load_xinfo_from_proc_icmp(ino_t netns_inode)
+{
+       load_xinfo_from_proc_inet_L4(netns_inode,
+                                    "/proc/net/icmp",
+                                    &ping_xinfo_class);
+}
+
 /*
  * TCP6
  */
index bef4508af3f88fe4bb611fcdac9918494e922f79..5ac4bd8a6d15dc8d5c205ae57bc61296b9f8d47b 100644 (file)
@@ -279,6 +279,9 @@ ____
 PIDFD.PID <``number``>::
 PID of the process targeted by the pidfd.
 
+PING.ID <`number`>::
+ICMP echo request id used on the PING socket.
+
 POS <``number``>::
 File position.
 
index 5cb1dc40be05068e898daeb2ec86253130b350a6..c49505d673b5d6eea67d7f12a748eaa7f2c4ad90 100644 (file)
@@ -189,6 +189,8 @@ static const struct colinfo infos[] = {
                N_("NSpid field in fdinfo of the pidfd") },
        [COL_PIDFD_PID]={ "PIDFD.PID",5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                N_("PID of the process targeted by the pidfd") },
+       [COL_PING_ID]={ "PING.ID",    0,SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
+               N_("ICMP echo request ID") },
        [COL_POS]     = { "POS",      5, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER,
                N_("file position") },
        [COL_RAW_PROTOCOL]={ "RAW.PROTOCOL",0,SCOLS_FL_RIGHT,SCOLS_JSON_NUMBER,
index 1166f9fe02793ed3b1caf8e3d9ac5dbe045cbbb5..cfeae57174cab82d1d3285bcdd6a0b64835e7d45 100644 (file)
@@ -69,6 +69,7 @@ enum {
        COL_PIDFD_COMM,
        COL_PIDFD_NSPID,
        COL_PIDFD_PID,
+       COL_PING_ID,
        COL_POS,
        COL_RAW_PROTOCOL,
        COL_RDEV,