2021-03-25 07:42:15 +09:00
|
|
|
/*
|
|
|
|
* lsfd(1) - list file descriptors
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (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
|
|
|
|
*/
|
|
|
|
#ifndef UTIL_LINUX_LSFD_H
|
|
|
|
#define UTIL_LINUX_LSFD_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2021-03-26 16:55:57 +09:00
|
|
|
#include "idcache.h"
|
2021-03-25 07:42:15 +09:00
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Utilities
|
|
|
|
*/
|
|
|
|
#define list_free(LIST,TYPE,MEMBER,FREEFN) \
|
|
|
|
do { \
|
|
|
|
struct list_head *__p, *__pnext; \
|
|
|
|
\
|
|
|
|
list_for_each_safe (__p, __pnext, (LIST)) { \
|
|
|
|
TYPE *__elt = list_entry(__p, TYPE, MEMBER); \
|
|
|
|
list_del(__p); \
|
|
|
|
FREEFN(__elt); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
DIR *opendirf(const char *format, ...) __attribute__((format (printf, 1, 2)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* column IDs
|
|
|
|
*/
|
|
|
|
enum {
|
2021-03-28 06:23:24 +09:00
|
|
|
COL_ASSOC,
|
2021-03-26 17:26:18 +09:00
|
|
|
COL_COMMAND,
|
2021-03-28 06:22:50 +09:00
|
|
|
COL_DEVICE,
|
2021-05-01 20:59:51 +09:00
|
|
|
COL_DEV,
|
2021-03-25 07:42:15 +09:00
|
|
|
COL_FD,
|
2021-03-26 17:26:18 +09:00
|
|
|
COL_INODE,
|
2021-03-25 07:42:15 +09:00
|
|
|
COL_NAME,
|
2021-03-26 17:26:18 +09:00
|
|
|
COL_PID,
|
2021-05-01 20:59:51 +09:00
|
|
|
COL_RDEV,
|
2021-05-03 15:59:26 +09:00
|
|
|
COL_SIZE,
|
2021-03-25 07:42:15 +09:00
|
|
|
COL_TYPE,
|
2021-03-26 02:56:28 +09:00
|
|
|
COL_UID,
|
2021-03-26 16:55:57 +09:00
|
|
|
COL_USER,
|
2021-03-25 07:42:15 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process structure
|
|
|
|
*/
|
2021-04-17 06:50:00 +09:00
|
|
|
enum association {
|
|
|
|
ASSOC_CWD = 1,
|
|
|
|
ASSOC_EXE,
|
|
|
|
ASSOC_ROOT,
|
2021-04-17 07:15:50 +09:00
|
|
|
ASSOC_NS_CGROUP,
|
|
|
|
ASSOC_NS_IPC,
|
|
|
|
ASSOC_NS_MNT,
|
|
|
|
ASSOC_NS_NET,
|
|
|
|
ASSOC_NS_PID,
|
|
|
|
ASSOC_NS_PID4C,
|
|
|
|
ASSOC_NS_TIME,
|
|
|
|
ASSOC_NS_TIME4C,
|
|
|
|
ASSOC_NS_USER,
|
|
|
|
ASSOC_NS_UTS,
|
|
|
|
ASSOC_MEM,
|
2021-04-17 06:50:00 +09:00
|
|
|
N_ASSOCS,
|
|
|
|
};
|
|
|
|
|
2021-03-25 07:42:15 +09:00
|
|
|
struct proc {
|
|
|
|
pid_t pid;
|
|
|
|
char *command;
|
|
|
|
struct list_head procs;
|
|
|
|
struct list_head files;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File classes
|
|
|
|
*/
|
|
|
|
struct file {
|
|
|
|
struct list_head files;
|
|
|
|
const struct file_class *class;
|
2021-04-17 05:49:58 +09:00
|
|
|
int association;
|
2021-03-25 07:42:15 +09:00
|
|
|
char *name;
|
|
|
|
struct stat stat;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct file_class {
|
|
|
|
const struct file_class *super;
|
|
|
|
size_t size;
|
|
|
|
bool (*fill_column)(struct proc *proc,
|
|
|
|
struct file *file,
|
|
|
|
struct libscols_line *ln,
|
|
|
|
int column_id,
|
|
|
|
size_t column_index);
|
|
|
|
void (*free_content)(struct file *file);
|
|
|
|
};
|
|
|
|
|
|
|
|
extern const struct file_class
|
2021-04-17 05:49:58 +09:00
|
|
|
file_class, regular_file_class,
|
|
|
|
cdev_file_class, bdev_file_class
|
2021-03-25 07:42:15 +09:00
|
|
|
;
|
|
|
|
|
|
|
|
struct file *make_file(const struct file_class *class,
|
2021-04-17 05:49:58 +09:00
|
|
|
struct stat *sb, const char *name, int association);
|
|
|
|
struct file *make_regular_file(const struct file_class *class,
|
2021-03-28 06:22:50 +09:00
|
|
|
struct stat *sb, const char *name, int fd);
|
2021-04-17 05:49:58 +09:00
|
|
|
struct file *make_cdev_file(const struct file_class *class,
|
2021-03-28 06:22:50 +09:00
|
|
|
struct stat *sb, const char *name, int fd);
|
2021-04-17 05:49:58 +09:00
|
|
|
struct file *make_bdev_file(const struct file_class *class,
|
|
|
|
struct stat *sb, const char *name, int fd);
|
2021-03-25 07:42:15 +09:00
|
|
|
|
2021-03-26 16:55:57 +09:00
|
|
|
extern struct idcache *username_cache;
|
|
|
|
|
2021-03-25 07:42:15 +09:00
|
|
|
#endif /* UTIL_LINUX_LSFD_H */
|