From: Masatake YAMATO Date: Sat, 27 Mar 2021 21:22:50 +0000 (+0900) Subject: lsfd: fill DEVICE field X-Git-Tag: v2.38-rc1~144^2~175 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6ada35230d9574098e38b3b0bb4587d9fb705ff6;p=thirdparty%2Futil-linux.git lsfd: fill DEVICE field --- diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am index 1e36b9c9c5..91e550e8ab 100644 --- a/misc-utils/Makemodule.am +++ b/misc-utils/Makemodule.am @@ -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 index 0000000000..6046f2701d --- /dev/null +++ b/misc-utils/lsfd-bdev-fd-file.c @@ -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 + * + * 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 index 0000000000..db8ccd57d6 --- /dev/null +++ b/misc-utils/lsfd-cdev-fd-file.c @@ -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 + * + * 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); +} diff --git a/misc-utils/lsfd-fd-file.c b/misc-utils/lsfd-fd-file.c index a9f645edb5..6a53b0d306 100644 --- a/misc-utils/lsfd-fd-file.c +++ b/misc-utils/lsfd-fd-file.c @@ -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 * - * Very generally based on lsof(8) by Victor A. Abell - * 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 diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 8a8aec1bc0..82ffa8d317 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -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; diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index cbe1f9bc78..a577845c28 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -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: diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 7a679e92f7..24c5fe8dcf 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -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;