summaryrefslogtreecommitdiffstats
path: root/misc-utils/lsfd.h
blob: bcee186d658954d3531705d11eb120ac45b01be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * 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>
#include <inttypes.h>

#include "list.h"

/*
 * column IDs
 */
enum {
	COL_ASSOC,
	COL_BLKDRV,
	COL_CHRDRV,
	COL_COMMAND,
	COL_DELETED,
	COL_DEV,
	COL_DEVTYPE,
	COL_FD,
	COL_FLAGS,
	COL_INODE,
	COL_KTHREAD,
	COL_MAJMIN,
	COL_MAPLEN,
	COL_MISCDEV,
	COL_MNT_ID,
	COL_MODE,
	COL_NAME,
	COL_NLINK,
	COL_PARTITION,
	COL_PID,
	COL_POS,
	COL_PROTONAME,
	COL_RDEV,
	COL_SIZE,
	COL_SOURCE,
	COL_TID,
	COL_TYPE,
	COL_UID,		/* process */
	COL_USER,		/* process */
	COL_FUID,		/* file */
	COL_OWNER,		/* file */
	LSFD_N_COLS		/* This must be at last. */
};

/*
 * Process structure
 */
enum association {
	ASSOC_EXE = 1,
	ASSOC_CWD,
	ASSOC_ROOT,
	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,		/* private file mapping */
	ASSOC_SHM,		/* shared file mapping */
	N_ASSOCS,
};

struct proc {
	pid_t pid;
	struct proc * leader;
	char *command;
	uid_t uid;
	ino_t ns_mnt;
	struct list_head procs;
	struct list_head files;
	unsigned int kthread: 1;
};

/*
 * File class
 */
struct file {
	struct list_head files;
	const struct file_class *class;
	int association;
	char *name;
	struct stat stat;
	mode_t mode;
	struct proc *proc;

	uint64_t pos;
	uint64_t map_start;
	uint64_t map_end;

	unsigned int sys_flags;
	unsigned int mnt_id;
};

#define is_association(_f, a)	((_f)->association < 0 && (_f)->association == -ASSOC_ ## a)

struct file_class {
	const struct file_class *super;
	size_t size;
	void (*initialize_class)(void);
	void (*finalize_class)(void);
	bool (*fill_column)(struct proc *proc,
			    struct file *file,
			    struct libscols_line *ln,
			    int column_id,
			    size_t column_index);
	int  (*handle_fdinfo)(struct file *file, const char *key, const char* value);
	void (*initialize_content)(struct file *file);
	void (*free_content)(struct file *file);
};

extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class;

/*
 * 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);

const char *get_partition(dev_t dev);
const char *get_blkdrv(unsigned long major);
const char *get_chrdrv(unsigned long major);
const char *get_miscdev(unsigned long minor);
const char *get_nodev_filesystem(unsigned long minor);

#endif /* UTIL_LINUX_LSFD_H */