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
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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);
+}
/*
- * 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
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;
/* 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") },
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);
columns[ncolumns++] = COL_USER;
columns[ncolumns++] = COL_FD;
columns[ncolumns++] = COL_TYPE;
+ columns[ncolumns++] = COL_DEVICE;
columns[ncolumns++] = COL_INODE;
columns[ncolumns++] = COL_NAME;
}
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:
*/
enum {
COL_COMMAND,
+ COL_DEVICE,
COL_FD,
COL_INODE,
COL_NAME,
};
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,
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;