]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: add PACKET.PROTOCOL.RAW, a new column
authorMasatake YAMATO <yamato@redhat.com>
Sun, 5 Jan 2025 18:34:22 +0000 (03:34 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Tue, 29 Jul 2025 09:52:15 +0000 (18:52 +0900)
Using sizeof for the calculating buffer size is suggested
by @karelzak.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
lsfd-cmd/lsfd.1.adoc
lsfd-cmd/lsfd.c
lsfd-cmd/lsfd.h
lsfd-cmd/sock-xinfo.c
tests/expected/lsfd/mkfds-mapped-packet-socket
tests/ts/lsfd/mkfds-mapped-packet-socket

index 2fa13b91693c74ed43c4d1f070ae57b81ff61c0a..39a1312c4f69609354e6a36b03f5547c2056297d 100644 (file)
@@ -397,7 +397,10 @@ PACKET.IFACE <``string``>::
 Interface name associated with the packet socket.
 
 PACKET.PROTOCOL <``string``>::
-L2 protocol associated with the packet socket.
+L2 protocol name associated with the packet socket (decoded).
+
+PACKET.PROTOCOL.RAW <``number``>::
+L2 protocol number associated with the packet socket (raw).
 
 PARTITION <``string``>::
 Block device name resolved by `/proc/partition`.
index ffaca8c311ba56cfc4a053de05bf4aa64eccd187..4d05c3b27a48bdf77eaae8869b2b5fd5757392c3 100644 (file)
@@ -312,7 +312,10 @@ static const struct colinfo infos[] = {
                                   N_("net interface associated with the packet socket") },
        [COL_PACKET_PROTOCOL]  = { "PACKET.PROTOCOL",
                                   0,   SCOLS_FL_RIGHT,SCOLS_JSON_STRING,
-                                  N_("L2 protocol associated with the packet socket") },
+                                  N_("L2 protocol name associated with the packet socket (decoded)") },
+       [COL_PACKET_PROTOCOL_RAW]={"PACKET.PROTOCOL.RAW",
+                                  0,   SCOLS_FL_RIGHT,SCOLS_JSON_NUMBER,
+                                  N_("L2 protocol number associated with the packet socket (raw)") },
        [COL_PARTITION]        = { "PARTITION",
                                   0,   SCOLS_FL_RIGHT, SCOLS_JSON_STRING,
                                   N_("block device name resolved by /proc/partition") },
index f6e640ae6bbc30752913bfb17aaa4dcd3d34f4e5..814605a1bf173d4610eec2a6e771967094045b40 100644 (file)
@@ -101,6 +101,7 @@ enum {
        COL_OWNER,              /* file */
        COL_PACKET_IFACE,
        COL_PACKET_PROTOCOL,
+       COL_PACKET_PROTOCOL_RAW,
        COL_PARTITION,
        COL_PID,
        COL_PIDFD_COMM,
index be1da8d2889fa080fd8be1708d5f0d185547e122..438756274c16871eab8a72aa5a92d17222b6f115 100644 (file)
@@ -2110,8 +2110,6 @@ struct packet_xinfo {
 static const char *packet_decode_protocol(uint16_t proto)
 {
        switch (proto) {
-       case 0:
-               return NULL;
        case ETH_P_802_3:
                return "802_3";
        case ETH_P_AX25:
@@ -2372,9 +2370,9 @@ static const char *packet_decode_protocol(uint16_t proto)
        case ETH_P_802_3_MIN:
                return "802_3_min";
 #endif
-       default:
-               return "unknown";
        }
+
+       return NULL;
 }
 
 static char *packet_get_name(struct sock_xinfo *sock_xinfo,
@@ -2383,21 +2381,22 @@ static char *packet_get_name(struct sock_xinfo *sock_xinfo,
        struct packet_xinfo *pkt = (struct packet_xinfo *)sock_xinfo;
        char *str = NULL;
        const char *type = sock_decode_type(pkt->type);
-       const char *proto = packet_decode_protocol(pkt->protocol);
+       const char *proto_str = packet_decode_protocol(pkt->protocol);
+       char proto_buf[ sizeof("unknown(") + sizeof (stringify_value(UINT16_MAX)) + sizeof (")") ];
        const char *iface = get_iface_name(sock_xinfo->netns_inode,
                                           pkt->iface);
 
-       if (iface && proto)
+       if (proto_str == NULL) {
+               snprintf(proto_buf, sizeof(proto_buf), "unknown(%"PRIu16")", pkt->protocol);
+               proto_str = proto_buf;
+       }
+
+       if (iface)
                xasprintf(&str, "type=%s protocol=%s iface=%s",
-                         type, proto, iface);
-       else if (proto)
-               xasprintf(&str, "type=%s protocol=%s",
-                         type, proto);
-       else if (iface)
-               xasprintf(&str, "type=%s iface=%s",
-                         type, iface);
+                         type, proto_str, iface);
        else
-               xasprintf(&str, "type=%s", type);
+               xasprintf(&str, "type=%s protocol=%s",
+                         type, proto_str);
 
        return str;
 }
@@ -2436,12 +2435,15 @@ static bool packet_fill_column(struct proc *proc __attribute__((__unused__)),
        case COL_PACKET_PROTOCOL: {
                const char *proto;
                proto = packet_decode_protocol(pkt->protocol);
-               if (proto) {
+               if (proto)
                        *str = xstrdup(proto);
-                       return true;
-               }
-               break;
+               else
+                       xstrfappend(str, "unknown(%"PRIu16")", pkt->protocol);
+               return true;
        }
+       case COL_PACKET_PROTOCOL_RAW:
+               xasprintf(str, "%"PRIu16, pkt->protocol);
+               return true;
        default:
                break;
        }
index 2e9db5eb56a53b32518708e39d4d81b4507c647b..92893f2dd88f36a1f2699c59baa6e5a61ca52e0d 100644 (file)
@@ -1,8 +1,16 @@
 PACKET
 SOCK.PROTONAME: 0
-type=raw protocol=ppptalk iface=lo raw PACKET lo ppptalk
-NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL: 0
+type=raw protocol=ppptalk iface=lo raw PACKET lo ppptalk 16
+NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL,PACKET.PROTOCOL.RAW: 0
 PACKET
 SOCK.PROTONAME: 0
-type=dgram protocol=ppptalk iface=lo dgram PACKET lo ppptalk
-NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL: 0
+type=dgram protocol=ppptalk iface=lo dgram PACKET lo ppptalk 16
+NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL,PACKET.PROTOCOL.RAW: 0
+PACKET
+SOCK.PROTONAME: 0
+type=raw protocol=unknown(10000) iface=lo raw PACKET lo unknown(10000) 10000
+NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL,PACKET.PROTOCOL.RAW: 0
+PACKET
+SOCK.PROTONAME: 0
+type=dgram protocol=unknown(10000) iface=lo dgram PACKET lo unknown(10000) 10000
+NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL,PACKET.PROTOCOL.RAW: 0
index c3a73c54e9fffc93e883c1350e8f1edbf6cca04e..6df9579b54a0158a6696f8ea19e18b14d06f4b35 100755 (executable)
@@ -35,28 +35,29 @@ FD=3
 EXPR=
 INTERFACE=lo
 SOCKTYPE=
-PROTOCOL=$(printf "%d" 0x10)
 ERR=
 
-for SOCKTYPE in RAW DGRAM; do
-    coproc MKFDS { "$TS_HELPER_MKFDS" mapped-packet-socket $FD \
-                                     interface=${INTERFACE} socktype=${SOCKTYPE} protocol=${PROTOCOL}; }
-    if read -u ${MKFDS[0]} PID; then
-       EXPR='(ASSOC == "shm") and (STTYPE == "SOCK") and (MODE == "-w-")'
-       ${TS_CMD_LSFD} -p "$PID" -n -o SOCK.PROTONAME -Q "${EXPR}"
-       echo 'SOCK.PROTONAME': $?
+for PROTOCOL in $(printf "%d" 0x10) 10000; do
+    for SOCKTYPE in RAW DGRAM; do
+       coproc MKFDS { "$TS_HELPER_MKFDS" mapped-packet-socket $FD \
+                                         interface=${INTERFACE} socktype=${SOCKTYPE} protocol=${PROTOCOL}; }
+       if read -u ${MKFDS[0]} PID; then
+           EXPR='(ASSOC == "shm") and (STTYPE == "SOCK") and (MODE == "-w-")'
+           ${TS_CMD_LSFD} -p "$PID" -n -o SOCK.PROTONAME -Q "${EXPR}"
+           echo 'SOCK.PROTONAME': $?
 
-       EXPR="(FD == $FD)"
-       ${TS_CMD_LSFD} -p "$PID" -n -o NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL -Q "${EXPR}"
-       echo 'NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL': $?
+           EXPR="(FD == $FD)"
+           ${TS_CMD_LSFD} -p "$PID" -n -o NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL,PACKET.PROTOCOL.RAW -Q "${EXPR}"
+           echo 'NAME,SOCK.TYPE,SOCK.PROTONAME,PACKET.IFACE,PACKET.PROTOCOL,PACKET.PROTOCOL.RAW': $?
 
-       echo DONE >&"${MKFDS[1]}"
-    fi
-    wait ${MKFDS_PID}
-    ERR=$?
-    if [ $ERR != 0 ]; then
-          break
-    fi
+           echo DONE >&"${MKFDS[1]}"
+       fi
+       wait ${MKFDS_PID}
+       ERR=$?
+       if [ $ERR != 0 ]; then
+           break
+       fi
+    done
 done > $TS_OUTPUT 2>&1
 
 if [[ "$ERR" == "$ENOPROTOOPT" ]]; then