From: Masatake YAMATO Date: Fri, 7 Mar 2025 21:42:18 +0000 (+0900) Subject: lsns: make "-Q NETNSID ..." work even if NETNSID column is not enabled X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e04238952520d485d69a0ad4d30bb7ab9a6fd075;p=thirdparty%2Futil-linux.git lsns: make "-Q NETNSID ..." work even if NETNSID column is not enabled 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 --- diff --git a/sys-utils/lsns.c b/sys-utils/lsns.c index 1cf9640b3..a97e7ef3c 100644 --- a/sys-utils/lsns.c +++ b/sys-utils/lsns.c @@ -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);