]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsns: make "-Q NETNSID ..." work even if NETNSID column is not enabled
authorMasatake YAMATO <yamato@redhat.com>
Fri, 7 Mar 2025 21:42:18 +0000 (06:42 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Fri, 7 Mar 2025 23:03:24 +0000 (08:03 +0900)
With the original code, lsns didn't collect values for NETNSID
unless NETNSID column was enabled. As a result, a filter expression
referencing NETNSID value didn't work expectedly.

  # ip netns add X
  # ip netns set X 99999
  # lsns-original -Q 'NETNSID == "99999"
(PRINT NOTHING)
  # lsns-original -Q 'NETNSID == "99999" -o NETNSID
  NETNSID
    99999

With this change, lsns collects values for NETNSID if the
filter expression given to -Q option refers to NETNSID.

  # lsns-new -Q 'NETNSID == "99999"
            NS TYPE NPROCS PID USER COMMAND
    4026535564 net       0     root

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
sys-utils/lsns.c

index 1cf9640b3d7083d189deb899556aca44df3cfd83..a97e7ef3c64a25e6b9b6062180f76056c8c54aa6 100644 (file)
@@ -278,6 +278,26 @@ static int column_name_to_id(const char *name, size_t namesz)
        return -1;
 }
 
+static int is_column_in_filter(int id, struct libscols_filter *filter)
+{
+       int r = 0;
+       const char *name = NULL;
+       struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
+
+       if (!itr)
+               err(EXIT_FAILURE, _("failed to allocate iterator"));
+
+       while (scols_filter_next_holder(filter, itr, &name, 0) == 0) {
+               if (strcmp(infos[id].name, name) == 0) {
+                       r = 1;
+                       break;
+               }
+       }
+
+       scols_free_iter(itr);
+       return r;
+}
+
 static int has_column(int id)
 {
        size_t i;
@@ -1791,7 +1811,8 @@ int main(int argc, char *argv[])
                err(EXIT_FAILURE, _("failed to allocate UID cache"));
 
 #ifdef HAVE_LINUX_NET_NAMESPACE_H
-       if (has_column(COL_NETNSID))
+       if (has_column(COL_NETNSID)
+           || (ls.filter && is_column_in_filter(COL_NETNSID, ls.filter)))
                netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
 #endif
        ls.tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);