]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: fill DEVICE field
authorMasatake YAMATO <yamato@redhat.com>
Sat, 27 Mar 2021 21:22:50 +0000 (06:22 +0900)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Oct 2021 09:01:53 +0000 (11:01 +0200)
misc-utils/Makemodule.am
misc-utils/lsfd-bdev-fd-file.c [new file with mode: 0644]
misc-utils/lsfd-cdev-fd-file.c [new file with mode: 0644]
misc-utils/lsfd-fd-file.c
misc-utils/lsfd-file.c
misc-utils/lsfd.c
misc-utils/lsfd.h

index 1e36b9c9c55592af803d30147427099ae8986854..91e550e8abf12886cfb327576d04d314c47fcc2b 100644 (file)
@@ -254,7 +254,9 @@ lsfd_SOURCES = \
        misc-utils/lsfd.h \
        misc-utils/lsfd-file.c \
        misc-utils/lsfd-fd-file.c \
-       misc-utils/lsfd-regular-fd-file.c
+       misc-utils/lsfd-regular-fd-file.c \
+       misc-utils/lsfd-cdev-fd-file.c \
+       misc-utils/lsfd-bdev-fd-file.c
 lsfd_LDADD = $(LDADD) libsmartcols.la libcommon.la
 lsfd_LDFLAGS =  -pthread
 lsfd_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir) -pthread
diff --git a/misc-utils/lsfd-bdev-fd-file.c b/misc-utils/lsfd-bdev-fd-file.c
new file mode 100644 (file)
index 0000000..6046f27
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * lsfd-bdev.c - handle associations opening block devices
+ *
+ * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
+ * Written 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 "xalloc.h"
+#include "nls.h"
+#include "libsmartcols.h"
+
+#include "lsfd.h"
+
+static bool bdev_fd_file_fill_column(struct proc *proc __attribute__((__unused__)),
+                                    struct file *file __attribute__((__unused__)),
+                                    struct libscols_line *ln,
+                                    int column_id,
+                                    size_t column_index)
+{
+       char *str = NULL;
+       switch(column_id) {
+       case COL_TYPE:
+               if (scols_line_set_data(ln, column_index, "BLK"))
+                       err(EXIT_FAILURE, _("failed to add output data"));
+               return true;
+       case COL_DEVICE:
+               xasprintf(&str, "%u:%u",
+                         major(file->stat.st_rdev),
+                         minor(file->stat.st_rdev));
+               break;
+       default:
+               return false;
+       }
+
+       if (!str)
+               err(EXIT_FAILURE, _("failed to add output data"));
+       if (scols_line_refer_data(ln, column_index, str))
+               err(EXIT_FAILURE, _("failed to add output data"));
+       return true;
+}
+
+const struct file_class bdev_fd_file_class = {
+       .super = &fd_file_class,
+       .size = sizeof(struct fd_file),
+       .fill_column = bdev_fd_file_fill_column,
+       .free_content = NULL,
+};
+
+struct file *make_bdev_fd_file(const struct file_class *class,
+                              struct stat *sb, const char *name, int fd)
+{
+       return make_fd_file(class? class: &bdev_fd_file_class,
+                           sb, name, fd);
+}
diff --git a/misc-utils/lsfd-cdev-fd-file.c b/misc-utils/lsfd-cdev-fd-file.c
new file mode 100644 (file)
index 0000000..db8ccd5
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * lsfd-cdev.c - handle associations opening character devices
+ *
+ * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
+ * Written 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 "xalloc.h"
+#include "nls.h"
+#include "libsmartcols.h"
+
+#include "lsfd.h"
+
+static bool cdev_fd_file_fill_column(struct proc *proc __attribute__((__unused__)),
+                                    struct file *file __attribute__((__unused__)),
+                                    struct libscols_line *ln,
+                                    int column_id,
+                                    size_t column_index)
+{
+       char *str = NULL;
+       switch(column_id) {
+       case COL_TYPE:
+               if (scols_line_set_data(ln, column_index, "CHR"))
+                       err(EXIT_FAILURE, _("failed to add output data"));
+               return true;
+       case COL_DEVICE:
+               xasprintf(&str, "%u:%u",
+                         major(file->stat.st_rdev),
+                         minor(file->stat.st_rdev));
+               break;
+       default:
+               return false;
+       }
+
+       if (!str)
+               err(EXIT_FAILURE, _("failed to add output data"));
+       if (scols_line_refer_data(ln, column_index, str))
+               err(EXIT_FAILURE, _("failed to add output data"));
+       return true;
+}
+
+const struct file_class cdev_fd_file_class = {
+       .super = &fd_file_class,
+       .size = sizeof (struct fd_file),
+       .fill_column = cdev_fd_file_fill_column,
+       .free_content = NULL,
+};
+
+struct file *make_cdev_fd_file(const struct file_class *class,
+                              struct stat *sb, const char *name, int fd)
+{
+       return make_fd_file(class? class: &cdev_fd_file_class,
+                           sb, name, fd);
+}
index a9f645edb55b8aada817e2a31104e71403dfccf9..6a53b0d30633bd2d073ffaaac33bef55be3b81c9 100644 (file)
@@ -1,12 +1,9 @@
 /*
- * lsfd(1) - list file descriptors
+ * lsfd-file.c - handle associations opening file objects
  *
  * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
  * Written by Masatake YAMATO <yamato@redhat.com>
  *
- * Very generally based on lsof(8) by Victor A. Abell <abe@purdue.edu>
- * It supports multiple OSes. lsfd specializes to Linux.
- *
  * 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
index 8a8aec1bc00c069c5a44d9c84a62a9e06343e714..82ffa8d317e0a9e88d4e978169d7719407aceea0 100644 (file)
@@ -86,6 +86,11 @@ static bool file_fill_column(struct proc *proc,
        case COL_INODE:
                xasprintf(&str, "%llu", (unsigned long long)file->stat.st_ino);
                break;
+       case COL_DEVICE:
+               xasprintf(&str, "%u:%u",
+                         major(file->stat.st_dev),
+                         minor(file->stat.st_dev));
+               break;
        case COL_PID:
                xasprintf(&str, "%d", (int)proc->pid);
                break;
index cbe1f9bc78927fd4af009b5f0fffaa6c431ebb36..a577845c284a29d4a5770e749e539a76a6a44087 100644 (file)
@@ -76,6 +76,7 @@ struct colinfo {
 /* columns descriptions */
 static struct colinfo infos[] = {
        [COL_COMMAND] = { "COMMAND",  0, 0,              N_("command of the process opening the file") },
+       [COL_DEVICE]  = { "DEVICE",   0, SCOLS_FL_RIGHT, N_("device major and minor number") },
        [COL_FD]      = { "FD",       0, SCOLS_FL_RIGHT, N_("file descriptor for the file") },
        [COL_INODE]   = { "INODE",    0, SCOLS_FL_RIGHT, N_("inode number") },
        [COL_NAME]    = { "NAME",     0, 0,              N_("name of the file") },
@@ -246,6 +247,10 @@ static struct file *collect_file(int dd, struct dirent *dp)
        switch (sb.st_mode & S_IFMT) {
        case S_IFREG:
                return make_regular_fd_file(NULL, &sb, sym, (int)num);
+       case S_IFCHR:
+               return make_cdev_fd_file(NULL, &sb, sym, (int)num);
+       case S_IFBLK:
+               return make_bdev_fd_file(NULL, &sb, sym, (int)num);
        }
 
        return make_fd_file(NULL, &sb, sym, (int)num);
@@ -473,6 +478,7 @@ int main(int argc, char *argv[])
                columns[ncolumns++] = COL_USER;
                columns[ncolumns++] = COL_FD;
                columns[ncolumns++] = COL_TYPE;
+               columns[ncolumns++] = COL_DEVICE;
                columns[ncolumns++] = COL_INODE;
                columns[ncolumns++] = COL_NAME;
        }
@@ -509,14 +515,15 @@ int main(int argc, char *argv[])
                        int id = get_column_id(i);
 
                        switch (id) {
-                       case COL_NAME:
                        case COL_COMMAND:
+                       case COL_DEVICE:
+                       case COL_NAME:
                        case COL_TYPE:
                        case COL_USER:
                                scols_column_set_json_type(cl, SCOLS_JSON_STRING);
                                break;
-                       case COL_PID:
                        case COL_FD:
+                       case COL_PID:
                        case COL_UID:
                                /* fallthrough */
                        default:
index 7a679e92f7d82450fdae7ea1720be4ae59d311dc..24c5fe8dcf2968337e57c326c15ce0d5735ad21c 100644 (file)
@@ -52,6 +52,7 @@ DIR *opendirf(const char *format, ...) __attribute__((format (printf, 1, 2)));
  */
 enum {
        COL_COMMAND,
+       COL_DEVICE,
        COL_FD,
        COL_INODE,
        COL_NAME,
@@ -98,7 +99,8 @@ struct file_class {
 };
 
 extern const struct file_class
-file_class, fd_file_class, regular_fd_file_class
+file_class, fd_file_class, regular_fd_file_class,
+       cdev_fd_file_class, bdev_fd_file_class
        ;
 
 struct file *make_file(const struct file_class *class,
@@ -107,6 +109,10 @@ struct file *make_fd_file(const struct file_class *class,
                          struct stat *sb, const char *name, int fd);
 struct file *make_regular_fd_file(const struct file_class *class,
                                  struct stat *sb, const char *name, int fd);
+struct file *make_cdev_fd_file(const struct file_class *class,
+                              struct stat *sb, const char *name, int fd);
+struct file *make_bdev_fd_file(const struct file_class *class,
+                              struct stat *sb, const char *name, int fd);
 
 extern struct idcache *username_cache;