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-09-20 13:30:28 +02:00
|
|
|
#include <inttypes.h>
|
2021-03-25 07:42:15 +09:00
|
|
|
|
|
|
|
#include "list.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Utilities
|
|
|
|
*/
|
|
|
|
DIR *opendirf(const char *format, ...) __attribute__((format (printf, 1, 2)));
|
2021-05-10 02:29:31 +09:00
|
|
|
FILE *fopenf(const char *mode, const char *format, ...) __attribute__((format (printf, 2, 3)));
|
2021-03-25 07:42:15 +09:00
|
|
|
|
|
|
|
/*
|
|
|
|
* column IDs
|
|
|
|
*/
|
|
|
|
enum {
|
2021-03-28 06:23:24 +09:00
|
|
|
COL_ASSOC,
|
2021-05-08 03:51:25 +09:00
|
|
|
COL_CHRDRV,
|
2021-03-26 17:26:18 +09:00
|
|
|
COL_COMMAND,
|
2021-05-03 16:22:29 +09:00
|
|
|
COL_DELETED,
|
2021-05-01 20:59:51 +09:00
|
|
|
COL_DEV,
|
2021-09-17 00:22:14 +09:00
|
|
|
COL_DEVTYPE,
|
2021-03-25 07:42:15 +09:00
|
|
|
COL_FD,
|
2021-05-06 14:28:58 +09:00
|
|
|
COL_FLAGS,
|
2021-03-26 17:26:18 +09:00
|
|
|
COL_INODE,
|
2021-09-17 00:01:30 +09:00
|
|
|
COL_MAJMIN,
|
2021-05-10 12:54:55 +09:00
|
|
|
COL_MAPLEN,
|
2021-05-08 04:47:23 +09:00
|
|
|
COL_MISCDEV,
|
2021-05-06 14:28:58 +09:00
|
|
|
COL_MNT_ID,
|
2021-05-06 14:29:43 +09:00
|
|
|
COL_MODE,
|
2021-03-25 07:42:15 +09:00
|
|
|
COL_NAME,
|
2021-05-03 16:22:29 +09:00
|
|
|
COL_NLINK,
|
2021-05-08 03:28:46 +09:00
|
|
|
COL_PARTITION,
|
2021-03-26 17:26:18 +09:00
|
|
|
COL_PID,
|
2021-05-06 14:28:58 +09:00
|
|
|
COL_POS,
|
2021-05-07 03:57:45 +09:00
|
|
|
COL_PROTONAME,
|
2021-05-01 20:59:51 +09:00
|
|
|
COL_RDEV,
|
2021-05-03 15:59:26 +09:00
|
|
|
COL_SIZE,
|
2021-09-17 01:04:48 +09:00
|
|
|
COL_SOURCE,
|
2021-05-05 01:47:49 +09:00
|
|
|
COL_TID,
|
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-09-21 02:58:48 +09:00
|
|
|
LSFD_N_COLS /* This must be at last. */
|
2021-03-25 07:42:15 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process structure
|
|
|
|
*/
|
2021-04-17 06:50:00 +09:00
|
|
|
enum association {
|
2021-05-05 03:37:57 +09:00
|
|
|
ASSOC_EXE = 1,
|
|
|
|
ASSOC_CWD,
|
2021-04-17 06:50:00 +09:00
|
|
|
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,
|
2021-05-06 22:19:11 +09:00
|
|
|
ASSOC_MEM, /* private file mapping */
|
|
|
|
ASSOC_SHM, /* shared file mapping */
|
2021-04-17 06:50:00 +09:00
|
|
|
N_ASSOCS,
|
|
|
|
};
|
|
|
|
|
2021-03-25 07:42:15 +09:00
|
|
|
struct proc {
|
|
|
|
pid_t pid;
|
2021-05-05 01:47:49 +09:00
|
|
|
struct proc * leader;
|
2021-03-25 07:42:15 +09:00
|
|
|
char *command;
|
2021-05-05 03:36:09 +09:00
|
|
|
uid_t uid;
|
2021-03-25 07:42:15 +09:00
|
|
|
struct list_head procs;
|
|
|
|
struct list_head files;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2021-09-20 12:46:31 +02:00
|
|
|
* File class
|
2021-03-25 07:42:15 +09:00
|
|
|
*/
|
|
|
|
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;
|
2021-05-06 14:29:43 +09:00
|
|
|
mode_t mode;
|
2021-09-02 14:30:58 +02:00
|
|
|
struct proc *proc;
|
2021-09-20 12:28:50 +02:00
|
|
|
|
2021-09-20 13:30:28 +02:00
|
|
|
uint64_t pos;
|
|
|
|
uint64_t map_start;
|
|
|
|
uint64_t map_end;
|
2021-09-20 12:28:50 +02:00
|
|
|
|
2021-09-20 13:16:06 +02:00
|
|
|
unsigned int sys_flags;
|
|
|
|
unsigned int mnt_id;
|
2021-03-25 07:42:15 +09:00
|
|
|
};
|
|
|
|
|
2021-09-08 12:52:43 +02:00
|
|
|
#define is_association(_f, a) ((_f)->association < 0 && (_f)->association == -ASSOC_ ## a)
|
|
|
|
|
2021-03-25 07:42:15 +09:00
|
|
|
struct file_class {
|
|
|
|
const struct file_class *super;
|
|
|
|
size_t size;
|
2021-05-06 23:51:00 +09:00
|
|
|
void (*initialize_class)(void);
|
|
|
|
void (*finalize_class)(void);
|
2021-03-25 07:42:15 +09:00
|
|
|
bool (*fill_column)(struct proc *proc,
|
|
|
|
struct file *file,
|
|
|
|
struct libscols_line *ln,
|
|
|
|
int column_id,
|
|
|
|
size_t column_index);
|
2021-05-06 14:25:56 +09:00
|
|
|
int (*handle_fdinfo)(struct file *file, const char *key, const char* value);
|
2021-09-20 12:28:50 +02:00
|
|
|
void (*initialize_content)(struct file *file);
|
2021-03-25 07:42:15 +09:00
|
|
|
void (*free_content)(struct file *file);
|
|
|
|
};
|
|
|
|
|
2021-05-09 11:36:07 +09:00
|
|
|
extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class;
|
2021-03-25 07:42:15 +09:00
|
|
|
|
2021-05-08 01:50:20 +09:00
|
|
|
/*
|
|
|
|
* Name managing
|
|
|
|
*/
|
|
|
|
struct name_manager;
|
|
|
|
|
|
|
|
struct name_manager *new_name_manager(void);
|
|
|
|
void free_name_manager(struct name_manager *nm);
|
|
|
|
const char *get_name(struct name_manager *nm, unsigned long id);
|
|
|
|
unsigned long add_name(struct name_manager *nm, const char *name);
|
|
|
|
|
2021-05-08 03:25:30 +09:00
|
|
|
const char *get_partition(dev_t dev);
|
2021-05-08 03:50:16 +09:00
|
|
|
const char *get_chrdrv(unsigned long major);
|
2021-05-08 04:46:11 +09:00
|
|
|
const char *get_miscdev(unsigned long minor);
|
2021-05-14 05:24:21 +09:00
|
|
|
const char *get_nodev_filesystem(unsigned long minor);
|
2021-05-08 03:25:30 +09:00
|
|
|
|
2021-03-25 07:42:15 +09:00
|
|
|
#endif /* UTIL_LINUX_LSFD_H */
|