From: Masatake YAMATO Date: Fri, 16 Apr 2021 20:49:58 +0000 (+0900) Subject: lsfd: purge fd layer X-Git-Tag: v2.38-rc1~144^2~173 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2ac344fba8aaccbbf28a392a1e4fa8e1d0ec4e80;p=thirdparty%2Futil-linux.git lsfd: purge fd layer --- diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am index 91e550e8ab..64be038a79 100644 --- a/misc-utils/Makemodule.am +++ b/misc-utils/Makemodule.am @@ -253,10 +253,9 @@ lsfd_SOURCES = \ misc-utils/lsfd.c \ 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-cdev-fd-file.c \ - misc-utils/lsfd-bdev-fd-file.c + misc-utils/lsfd-regular-file.c \ + misc-utils/lsfd-cdev-file.c \ + misc-utils/lsfd-bdev-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-file.c similarity index 75% rename from misc-utils/lsfd-bdev-fd-file.c rename to misc-utils/lsfd-bdev-file.c index 6046f2701d..0c4393df16 100644 --- a/misc-utils/lsfd-bdev-fd-file.c +++ b/misc-utils/lsfd-bdev-file.c @@ -25,11 +25,11 @@ #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) +static bool bdev_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) { @@ -53,16 +53,16 @@ static bool bdev_fd_file_fill_column(struct proc *proc __attribute__((__unused__ 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, +const struct file_class bdev_file_class = { + .super = &file_class, + .size = sizeof(struct file), + .fill_column = bdev_file_fill_column, .free_content = NULL, }; -struct file *make_bdev_fd_file(const struct file_class *class, +struct file *make_bdev_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); + return make_file(class? class: &bdev_file_class, + sb, name, fd); } diff --git a/misc-utils/lsfd-cdev-fd-file.c b/misc-utils/lsfd-cdev-file.c similarity index 72% rename from misc-utils/lsfd-cdev-fd-file.c rename to misc-utils/lsfd-cdev-file.c index db8ccd57d6..1d05dcff4b 100644 --- a/misc-utils/lsfd-cdev-fd-file.c +++ b/misc-utils/lsfd-cdev-file.c @@ -25,11 +25,11 @@ #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) +static bool cdev_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) { @@ -53,16 +53,16 @@ static bool cdev_fd_file_fill_column(struct proc *proc __attribute__((__unused__ 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, +const struct file_class cdev_file_class = { + .super = &file_class, + .size = sizeof (struct file), + .fill_column = cdev_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) +struct file *make_cdev_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); + return make_file(class? class: &cdev_file_class, + sb, name, fd); } diff --git a/misc-utils/lsfd-fd-file.c b/misc-utils/lsfd-fd-file.c deleted file mode 100644 index eee0ffd6c5..0000000000 --- a/misc-utils/lsfd-fd-file.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * lsfd-file.c - handle associations opening file objects - * - * 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 fd_file_fill_column(struct proc *proc __attribute__((__unused__)), - struct file *file, - struct libscols_line *ln, - int column_id, - size_t column_index) -{ - char *str = NULL; - struct fd_file * fd_file = (struct fd_file *)file; - - switch(column_id) { - case COL_FD: - case COL_ASSOC: - xasprintf(&str, "%d", fd_file->fd); - 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 fd_file_class = { - .super = &file_class, - .size = sizeof(struct fd_file), - .fill_column = fd_file_fill_column, - .free_content = NULL, -}; - -struct file *make_fd_file(const struct file_class *class, - struct stat *sb, const char *name, int fd) -{ - struct file *file = make_file(class? class: &fd_file_class, - sb, name); - - ((struct fd_file *)(file))->fd = fd; - - return file; -} diff --git a/misc-utils/lsfd-file.c b/misc-utils/lsfd-file.c index 82ffa8d317..d554030210 100644 --- a/misc-utils/lsfd-file.c +++ b/misc-utils/lsfd-file.c @@ -83,6 +83,13 @@ static bool file_fill_column(struct proc *proc, (int)file->stat.st_uid)->name)) err(EXIT_FAILURE, _("failed to add output data")); return true; + case COL_FD: + if (file->association < 0) + return false; + /* FALL THROUGH */ + case COL_ASSOC: + xasprintf(&str, "%d", file->association); + break; case COL_INODE: xasprintf(&str, "%llu", (unsigned long long)file->stat.st_ino); break; @@ -114,11 +121,15 @@ static void file_free_content(struct file *file) } struct file *make_file(const struct file_class *class, - struct stat *sb, const char *name) + struct stat *sb, const char *name, int association) { - struct file *file = xcalloc(1, class->size); + struct file *file; + + class = class? class: &file_class; + file = xcalloc(1, class->size); file->class = class; + file->association = association; file->name = xstrdup(name); file->stat = *sb; return file; @@ -126,7 +137,7 @@ struct file *make_file(const struct file_class *class, const struct file_class file_class = { .super = NULL, - .size = 0, + .size = sizeof (struct file), .fill_column = file_fill_column, .free_content = file_free_content, }; diff --git a/misc-utils/lsfd-regular-fd-file.c b/misc-utils/lsfd-regular-file.c similarity index 69% rename from misc-utils/lsfd-regular-fd-file.c rename to misc-utils/lsfd-regular-file.c index 1bcf8962cb..0a7cd79904 100644 --- a/misc-utils/lsfd-regular-fd-file.c +++ b/misc-utils/lsfd-regular-file.c @@ -28,11 +28,11 @@ #include "lsfd.h" -static bool regular_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) +static bool regular_file_fill_column(struct proc *proc __attribute__((__unused__)), + struct file *file __attribute__((__unused__)), + struct libscols_line *ln, + int column_id, + size_t column_index) { switch(column_id) { case COL_TYPE: @@ -44,16 +44,16 @@ static bool regular_fd_file_fill_column(struct proc *proc __attribute__((__unuse return false; } -const struct file_class regular_fd_file_class = { - .super = &fd_file_class, - .size = sizeof(struct fd_file), - .fill_column = regular_fd_file_fill_column, +const struct file_class regular_file_class = { + .super = &file_class, + .size = sizeof(struct file), + .fill_column = regular_file_fill_column, .free_content = NULL, }; -struct file *make_regular_fd_file(const struct file_class *class, - struct stat *sb, const char *name, int fd) +struct file *make_regular_file(const struct file_class *class, + struct stat *sb, const char *name, int fd) { - return make_fd_file(class? class: ®ular_fd_file_class, - sb, name, fd); + return make_file(class? class: ®ular_file_class, + sb, name, fd); } diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index c37de3dc6a..4c53224e28 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -47,7 +47,7 @@ /* * Multi-threading related stuffs */ -#define NUM_COLLECTORS 2 +#define NUM_COLLECTORS 1 static pthread_cond_t procs_ready = PTHREAD_COND_INITIALIZER; static pthread_mutex_t procs_ready_lock = PTHREAD_MUTEX_INITIALIZER; @@ -247,14 +247,14 @@ 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); + return make_regular_file(NULL, &sb, sym, (int)num); case S_IFCHR: - return make_cdev_fd_file(NULL, &sb, sym, (int)num); + return make_cdev_file(NULL, &sb, sym, (int)num); case S_IFBLK: - return make_bdev_fd_file(NULL, &sb, sym, (int)num); + return make_bdev_file(NULL, &sb, sym, (int)num); } - return make_fd_file(NULL, &sb, sym, (int)num); + return make_file(NULL, &sb, sym, (int)num); } static void enqueue_file(struct proc *proc, struct file * file) diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index 77363b73f8..896bbf58af 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -79,15 +79,11 @@ struct proc { struct file { struct list_head files; const struct file_class *class; + int association; char *name; struct stat stat; }; -struct fd_file { - struct file file; - int fd; -}; - struct file_class { const struct file_class *super; size_t size; @@ -100,20 +96,18 @@ struct file_class { }; extern const struct file_class -file_class, fd_file_class, regular_fd_file_class, - cdev_fd_file_class, bdev_fd_file_class +file_class, regular_file_class, + cdev_file_class, bdev_file_class ; struct file *make_file(const struct file_class *class, - struct stat *sb, const char *name); -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 association); +struct file *make_regular_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 file *make_cdev_file(const struct file_class *class, struct stat *sb, const char *name, int fd); +struct file *make_bdev_file(const struct file_class *class, + struct stat *sb, const char *name, int fd); extern struct idcache *username_cache;