]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
Merge branch 'xry111/pidfs' of https://github.com/xry111/util-linux
authorKarel Zak <kzak@redhat.com>
Mon, 8 Apr 2024 07:48:10 +0000 (09:48 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 8 Apr 2024 07:48:10 +0000 (09:48 +0200)
* 'xry111/pidfs' of https://github.com/xry111/util-linux:
  lsfd: test: Adapt test cases for pidfs
  lsfd: Support pidfs
  lsfd: Refactor the pidfd logic into lsfd-pidfd.c
  include: Include <unistd.h> in pidfd-utils.h for syscall()

16 files changed:
include/pidfd-utils.h
misc-utils/Makemodule.am
misc-utils/lsfd-file.c
misc-utils/lsfd-pidfd.c [new file with mode: 0644]
misc-utils/lsfd-pidfd.h [new file with mode: 0644]
misc-utils/lsfd-unkn.c
misc-utils/lsfd.c
misc-utils/lsfd.h
misc-utils/meson.build
tests/expected/lsfd/column-name-pidfd
tests/expected/lsfd/column-type-pidfd
tests/expected/lsfd/mkfds-pidfd
tests/ts/lsfd/column-ainodeclass
tests/ts/lsfd/column-name
tests/ts/lsfd/column-type
tests/ts/lsfd/mkfds-pidfd

index ff0bc4c792fc5e07bcecd4244142c0529eed52f0..0ee55f3bac08c7df08ac63fb0cf35e0822f18335 100644 (file)
@@ -7,6 +7,7 @@
 
 #ifdef HAVE_SYS_SYSCALL_H
 # include <sys/syscall.h>
+# include <unistd.h>
 
 /*
  * If the kernel headers are too old to provide the syscall numbers, let's
index 9edf3d98edf68aa82c991b949d1e4deb047c3a04..7622a5d7bea26f2e8abba354ca4c4c9fec3a46db 100644 (file)
@@ -298,7 +298,9 @@ lsfd_SOURCES = \
        misc-utils/lsfd-sock.h \
        misc-utils/lsfd-sock-xinfo.c \
        misc-utils/lsfd-unkn.c \
-       misc-utils/lsfd-fifo.c
+       misc-utils/lsfd-fifo.c \
+       misc-utils/lsfd-pidfd.h \
+       misc-utils/lsfd-pidfd.c
 lsfd_LDADD = $(LDADD) $(MQ_LIBS) libsmartcols.la libcommon.la
 lsfd_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
 endif
index 7287a1de83b30898e89b21771f9bf9db25637464..35eabb3e456b1fcfc295afdbb6aaa7a1312e481f 100644 (file)
@@ -45,6 +45,8 @@
 #include "procfs.h"
 
 #include "lsfd.h"
+#include "lsfd-pidfd.h"
+#include "pidfd-utils.h"
 
 static size_t pagesize;
 
@@ -653,6 +655,22 @@ static unsigned long get_minor_for_mqueue(void)
        return minor(sb.st_dev);
 }
 
+static unsigned long get_minor_for_pidfs(void)
+{
+       int fd = pidfd_open(getpid(), 0);
+       struct stat sb;
+       unsigned long ret = 0;
+
+       if (fd < 0)
+               return 0;
+
+       if (fstat(fd, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG)
+               ret = minor(sb.st_dev);
+
+       close(fd);
+       return ret;
+}
+
 static void file_class_initialize(void)
 {
        unsigned long m;
@@ -667,6 +685,10 @@ static void file_class_initialize(void)
        m = get_minor_for_mqueue();
        if (m)
                add_nodev(m, "mqueue");
+
+       m = get_minor_for_pidfs();
+       if (m)
+               add_nodev(m, "pidfs");
 }
 
 const struct file_class file_class = {
@@ -935,3 +957,77 @@ const struct file_class mqueue_file_class = {
        .fill_column = mqueue_file_fill_column,
        .get_ipc_class = mqueue_file_get_ipc_class,
 };
+
+struct pidfs_file {
+       struct file file;
+       struct pidfd_data data;
+};
+
+static void init_pidfs_file_content(struct file *file)
+{
+       struct pidfs_file *pidfs_file = (struct pidfs_file *)file;
+
+       memset(&pidfs_file->data, 0, sizeof(pidfs_file->data));
+}
+
+static int pidfs_file_handle_fdinfo(struct file *file, const char *key, const char *value)
+{
+       struct pidfs_file *pidfs_file = (struct pidfs_file *)file;
+
+       return pidfd_handle_fdinfo(&pidfs_file->data, key, value);
+}
+
+static void pidfs_file_free_content(struct file *file)
+{
+       struct pidfs_file *pidfs_file = (struct pidfs_file *)file;
+
+       pidfd_free(&pidfs_file->data);
+}
+
+static bool pidfs_file_fill_column(struct proc *proc __attribute__((__unused__)),
+                                  struct file *file,
+                                  struct libscols_line *ln,
+                                  int column_id,
+                                  size_t column_index)
+{
+       struct pidfs_file *pidfs_file = (struct pidfs_file *)file;
+       char *buf = NULL;
+
+       switch(column_id) {
+       case COL_TYPE:
+               if (scols_line_set_data(ln, column_index, "pidfd"))
+                       err(EXIT_FAILURE, _("failed to add output data"));
+               return true;
+       case COL_NAME:
+               buf = pidfd_get_name(&pidfs_file->data);
+               break;
+       default:
+               if (!pidfd_fill_column(&pidfs_file->data, column_id, &buf))
+                       return false;
+       }
+
+       if (buf &&
+           scols_line_refer_data(ln, column_index, buf))
+               err(EXIT_FAILURE, _("failed to add output data"));
+
+       return true;
+}
+
+const struct file_class pidfs_file_class = {
+       .super = &file_class,
+       .size = sizeof(struct pidfs_file),
+       .initialize_content = init_pidfs_file_content,
+       .handle_fdinfo = pidfs_file_handle_fdinfo,
+       .fill_column = pidfs_file_fill_column,
+       .free_content = pidfs_file_free_content,
+};
+
+bool is_pidfs_dev(dev_t dev)
+{
+       const char *fs = get_nodev_filesystem(minor(dev));
+
+       if (fs && (strcmp (fs, "pidfs") == 0))
+               return true;
+
+       return false;
+}
diff --git a/misc-utils/lsfd-pidfd.c b/misc-utils/lsfd-pidfd.c
new file mode 100644 (file)
index 0000000..430a802
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * lsfd-pidfd.c - handle pidfd (from anon_inode or pidfs)
+ *
+ * Copyright (C) 2024 Xi Ruoyao <xry111@xry111.site>
+ *
+ * Refactored and moved out from lsfd-unkn.c (originally authored by
+ * Masatake YAMATO <yamato@redhat.com>).
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <string.h>
+
+#include "strutils.h"
+#include "xalloc.h"
+
+#include "lsfd.h"
+#include "lsfd-pidfd.h"
+
+int pidfd_handle_fdinfo(struct pidfd_data *data, const char *key,
+                       const char *value)
+{
+       if (strcmp(key, "Pid") == 0) {
+               uint64_t pid;
+               int rc = ul_strtou64(value, &pid, 10);
+
+               if (rc < 0)
+                       return 0; /* ignore -- parse failed */
+
+               data->pid = (pid_t)pid;
+               return 1;
+       } else if (strcmp(key, "NSpid") == 0) {
+               data->nspid = xstrdup(value);
+               return 1;
+       }
+
+       return 0;
+}
+
+char *pidfd_get_name(struct pidfd_data *data)
+{
+       char *str = NULL;
+       char *comm = NULL;
+       struct proc *proc = get_proc(data->pid);
+
+       if (proc)
+               comm = proc->command;
+
+       xasprintf(&str, "pid=%d comm=%s nspid=%s",
+                         data->pid,
+                         comm ? comm : "",
+                         data->nspid ? data->nspid : "");
+       return str;
+}
+
+bool pidfd_fill_column(struct pidfd_data *data, int column_id, char **str)
+{
+       switch(column_id) {
+       case COL_PIDFD_COMM: {
+               struct proc *pidfd_proc = get_proc(data->pid);
+               char *pidfd_comm = NULL;
+
+               if (pidfd_proc)
+                       pidfd_comm = pidfd_proc->command;
+               if (pidfd_comm) {
+                       *str = xstrdup(pidfd_comm);
+                       return true;
+               }
+               break;
+       }
+       case COL_PIDFD_NSPID:
+               if (data->nspid) {
+                       *str = xstrdup(data->nspid);
+                       return true;
+               }
+               break;
+       case COL_PIDFD_PID:
+               xasprintf(str, "%d", (int)data->pid);
+               return true;
+       }
+
+       return false;
+}
diff --git a/misc-utils/lsfd-pidfd.h b/misc-utils/lsfd-pidfd.h
new file mode 100644 (file)
index 0000000..2f65d3b
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * lsfd-pidfd.h - handle pidfd (from anon_inode or pidfs)
+ *
+ * Copyright (C) 2024 Xi Ruoyao <xry111@xry111.site>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+struct pidfd_data {
+       pid_t pid;
+       char *nspid;
+};
+
+int pidfd_handle_fdinfo(struct pidfd_data *, const char *, const char *);
+char *pidfd_get_name(struct pidfd_data *);
+bool pidfd_fill_column(struct pidfd_data *, int, char **);
+
+static inline void __attribute__((nonnull(1)))
+pidfd_free(struct pidfd_data *data)
+{
+       free(data->nspid);
+}
index 8f6e90846890d38b2e4358cca3e1b833057fcce9..8e257f479be038a1fe3bf49fad28cd6a60692d8f 100644 (file)
@@ -28,6 +28,7 @@
 #include "timeutils.h"
 
 #include "lsfd.h"
+#include "lsfd-pidfd.h"
 
 #define offsetofend(TYPE, MEMBER)                              \
        (offsetof(TYPE, MEMBER) + sizeof_member(TYPE, MEMBER))
@@ -183,10 +184,6 @@ static int unkn_handle_fdinfo(struct file *file, const char *key, const char *va
 /*
  * pidfd
  */
-struct anon_pidfd_data {
-       pid_t pid;
-       char *nspid;
-};
 
 static bool anon_pidfd_probe(const char *str)
 {
@@ -195,51 +192,28 @@ static bool anon_pidfd_probe(const char *str)
 
 static char *anon_pidfd_get_name(struct unkn *unkn)
 {
-       char *str = NULL;
-       struct anon_pidfd_data *data = (struct anon_pidfd_data *)unkn->anon_data;
+       struct pidfd_data *data = (struct pidfd_data *)unkn->anon_data;
 
-       char *comm = NULL;
-       struct proc *proc = get_proc(data->pid);
-       if (proc)
-               comm = proc->command;
-
-       xasprintf(&str, "pid=%d comm=%s nspid=%s",
-                 data->pid,
-                 comm? comm: "",
-                 data->nspid? data->nspid: "");
-       return str;
+       return pidfd_get_name(data);
 }
 
 static void anon_pidfd_init(struct unkn *unkn)
 {
-       unkn->anon_data = xcalloc(1, sizeof(struct anon_pidfd_data));
+       unkn->anon_data = xcalloc(1, sizeof(struct pidfd_data));
 }
 
 static void anon_pidfd_free(struct unkn *unkn)
 {
-       struct anon_pidfd_data *data = (struct anon_pidfd_data *)unkn->anon_data;
+       struct pidfd_data *data = (struct pidfd_data *)unkn->anon_data;
 
-       if (data->nspid)
-               free(data->nspid);
+       pidfd_free(data);
        free(data);
 }
 
 static int anon_pidfd_handle_fdinfo(struct unkn *unkn, const char *key, const char *value)
 {
-       if (strcmp(key, "Pid") == 0) {
-               uint64_t pid;
-
-               int rc = ul_strtou64(value, &pid, 10);
-               if (rc < 0)
-                       return 0; /* ignore -- parse failed */
-               ((struct anon_pidfd_data *)unkn->anon_data)->pid = (pid_t)pid;
-               return 1;
-       } else if (strcmp(key, "NSpid") == 0) {
-               ((struct anon_pidfd_data *)unkn->anon_data)->nspid = xstrdup(value);
-               return 1;
-
-       }
-       return 0;
+       return pidfd_handle_fdinfo((struct pidfd_data *)unkn->anon_data,
+                                  key, value);
 }
 
 static bool anon_pidfd_fill_column(struct proc *proc  __attribute__((__unused__)),
@@ -249,32 +223,9 @@ static bool anon_pidfd_fill_column(struct proc *proc  __attribute__((__unused__)
                                   size_t column_index __attribute__((__unused__)),
                                   char **str)
 {
-       struct anon_pidfd_data *data = (struct anon_pidfd_data *)unkn->anon_data;
-
-       switch(column_id) {
-       case COL_PIDFD_COMM: {
-               struct proc *pidfd_proc = get_proc(data->pid);
-               char *pidfd_comm = NULL;
-               if (pidfd_proc)
-                       pidfd_comm = pidfd_proc->command;
-               if (pidfd_comm) {
-                       *str = xstrdup(pidfd_comm);
-                       return true;
-               }
-               break;
-       }
-       case COL_PIDFD_NSPID:
-               if (data->nspid) {
-                       *str = xstrdup(data->nspid);
-                       return true;
-               }
-               break;
-       case COL_PIDFD_PID:
-               xasprintf(str, "%d", (int)data->pid);
-               return true;
-       }
-
-       return false;
+       return pidfd_fill_column((struct pidfd_data *)unkn->anon_data,
+                                column_id,
+                                str);
 }
 
 static const struct anon_ops anon_pidfd_ops = {
index d4d2a99bc9f67e45d6b6dd8419534e045d58117f..f042f5caaacbb6cd85f09c523ae98f5859f027ae 100644 (file)
@@ -667,6 +667,9 @@ static const struct file_class *stat2class(struct stat *sb)
                if (is_mqueue_dev(dev))
                        return &mqueue_file_class;
 
+               if (is_pidfs_dev(dev))
+                       return &pidfs_file_class;
+
                return &file_class;
        default:
                break;
index b9a402ad20d628e732a61dad896d034a1f5b4039..217bf4533830c80e5e78653e13cabc883ae1097d 100644 (file)
@@ -220,7 +220,7 @@ struct file_class {
 
 extern const struct file_class abst_class, readlink_error_class, stat_error_class,
        file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class,
-       nsfs_file_class, mqueue_file_class;
+       nsfs_file_class, mqueue_file_class, pidfs_file_class;
 
 /*
  * IPC
@@ -299,4 +299,9 @@ bool is_mqueue_dev(dev_t dev);
  */
 bool is_multiplexed_by_eventpoll(int fd, struct list_head *eventpolls);
 
+/*
+ * Pidfs
+ */
+bool is_pidfs_dev(dev_t dev);
+
 #endif /* UTIL_LINUX_LSFD_H */
index 847b1012cfdfb5a9d1b4d2861bf764bbb1ea4732..68ea9777ec1746684c2082ed3437fbe5b3984256 100644 (file)
@@ -56,6 +56,7 @@ lsfd_sources = files (
   'lsfd-sock-xinfo.c',
   'lsfd-unkn.c',
   'lsfd-fifo.c',
+  'lsfd-pidfd.c',
 )
 
 uuidgen_sources = files(
index 10e3c5e7f5343e971d5bb6e04d3d3ce58744200a..68787d69fc590da8279601b7fd4204a9701ec86c 100644 (file)
@@ -1,2 +1,2 @@
-3 anon_inode:[pidfd] pid=1 comm= nspid=1
+3 [KNAME] pid=1 comm= nspid=1
 pidfd:ASSOC,KNAME,NAME: 0
index 6c9a9632ea9637263b72fde8544f16647165ab1d..a4379807d461f3281aead0cb91d56767370b8ecf 100644 (file)
@@ -1,2 +1,2 @@
-3 UNKN pidfd
+3 [STTYPE] pidfd
 pidfd:ASSOC,STTYPE,TYPE: 0
index 9484699204d4dd7f153a1bec2c969a59d00668d4..bce4dd42a2c085de327f109edbef54c2fb32691a 100644 (file)
@@ -1,2 +1,2 @@
-3 UNKN anon_inodefs pid=1 comm=systemd nspid=1 systemd     1
+3 [STTYPE] [SOURCE] pid=1 comm=systemd nspid=1 systemd     1
 ASSOC,STTYPE,SOURCE,NAME,PIDFD.COMM,PIDFD.PID: 0
index 6829494f0e640fd0fcff1f21090b1b40f352b801..ab2abebd6ade225765a264dce383e8e7487cb0ac 100755 (executable)
@@ -42,10 +42,18 @@ for C in pidfd inotify; do
        fi
        wait "${MKFDS_PID}"
     } > "$TS_OUTPUT" 2>&1
+
     if [ "$C-$?" == "pidfd-$TS_EXIT_NOTSUPP" ]; then
        ts_skip_subtest "pidfd_open(2) is not available"
        continue
     fi
+
+    STTYPE="$(head -n1 "$TS_OUTPUT" | awk '{print $2}')"
+    if [ "$C-$STTYPE" == "pidfd-REG" ]; then
+       ts_skip_subtest "pidfd is from pidfs instead of anon inode"
+       continue
+    fi
+
     ts_finalize_subtest
 done
 
index 8bf8f42178ab3aa7f9cab4cfc0e85a0c96f90bb1..9c67de889351003da127bfc14c09c0a5f66129a6 100755 (executable)
@@ -64,10 +64,17 @@ for C in ro-regular-file pidfd socketpair; do
        fi
     } > "$TS_OUTPUT" 2>&1
     wait "${MKFDS_PID}"
+
     if [ "$C-$?" == "pidfd-$TS_EXIT_NOTSUPP" ]; then
        ts_skip_subtest "pidfd_open(2) is not available"
        continue
     fi
+
+    case $C in
+    pidfd)
+       sed -i -E 's/(pidfd|anon_inode):\[[a-zA-Z]+\]/[KNAME]/' "$TS_OUTPUT"
+    esac
+
     ts_finalize_subtest
 done
 
index 77bc5c940068995d494ee760ecea50efa5e5f470..1b8aa8c6fc511a650aac1119f897e6084f17a967 100755 (executable)
@@ -50,10 +50,17 @@ for C in ro-regular-file pidfd inotify socketpair; do
        fi
        wait "${MKFDS_PID}"
     } > "$TS_OUTPUT" 2>&1
+
     if [ "$C-$?" == "pidfd-$TS_EXIT_NOTSUPP" ]; then
        ts_skip_subtest "pidfd_open(2) is not available"
        continue
     fi
+
+    case $C in
+    pidfd)
+       sed -i -E 's/UNKN|REG/[STTYPE]/' "$TS_OUTPUT"
+    esac
+
     ts_finalize_subtest
 done
 
index c0fae4f70a5d0b952b9cf275cbdf86ba08680aec..9b0ff33c60a709d3f989c1bf1539e6f1edd432f8 100755 (executable)
@@ -44,8 +44,12 @@ EXPR="(PID != ${TARGET}) and (FD == 3) and (PIDFD.PID == ${TARGET})"
     fi
     wait ${MKFDS_PID}
 } > $TS_OUTPUT 2>&1
+
 if [ "$?" == "$TS_EXIT_NOTSUPP" ]; then
     ts_skip "pidfd_open(2) is not available"
 fi
 
+sed -i -E -e 's/UNKN|REG/[STTYPE]/' -e 's/pidfs|anon_inodefs/[SOURCE]/' \
+    $TS_OUTPUT
+
 ts_finalize