]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsfd.h
Merge branch 'PR/libsmartcols-cell-data' of github.com:karelzak/util-linux-work
[thirdparty/util-linux.git] / misc-utils / lsfd.h
1 /*
2 * lsfd(1) - list file descriptors
3 *
4 * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
5 * Written by Masatake YAMATO <yamato@redhat.com>
6 *
7 * Very generally based on lsof(8) by Victor A. Abell <abe@purdue.edu>
8 * It supports multiple OSes. lsfd specializes to Linux.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it would be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 #ifndef UTIL_LINUX_LSFD_H
25 #define UTIL_LINUX_LSFD_H
26
27 #include <stdbool.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <inttypes.h>
31
32 #include "libsmartcols.h"
33 #include "list.h"
34 #include "nls.h"
35 #include "path.h"
36 #include "strutils.h"
37 #include "xalloc.h"
38
39 /*
40 * column IDs
41 */
42 enum {
43 COL_AINODECLASS,
44 COL_ASSOC,
45 COL_BLKDRV,
46 COL_BPF_MAP_ID,
47 COL_BPF_MAP_TYPE,
48 COL_BPF_MAP_TYPE_RAW,
49 COL_BPF_NAME,
50 COL_BPF_PROG_ID,
51 COL_BPF_PROG_TYPE,
52 COL_BPF_PROG_TYPE_RAW,
53 COL_CHRDRV,
54 COL_COMMAND,
55 COL_DELETED,
56 COL_DEV,
57 COL_DEVTYPE,
58 COL_ENDPOINTS,
59 COL_EVENTFD_ID,
60 COL_EVENTPOLL_TFDS,
61 COL_FD,
62 COL_FLAGS,
63 COL_FUID, /* file */
64 COL_INET_LADDR,
65 COL_INET_RADDR,
66 COL_INET6_LADDR,
67 COL_INET6_RADDR,
68 COL_INODE,
69 COL_INOTIFY_INODES,
70 COL_INOTIFY_INODES_RAW,
71 COL_KNAME,
72 COL_KTHREAD,
73 COL_MAJMIN,
74 COL_MAPLEN,
75 COL_MISCDEV,
76 COL_MNT_ID,
77 COL_MODE,
78 COL_NAME,
79 COL_NETLINK_GROUPS,
80 COL_NETLINK_LPORT,
81 COL_NETLINK_PROTOCOL,
82 COL_NLINK,
83 COL_NS_NAME,
84 COL_NS_TYPE,
85 COL_OWNER, /* file */
86 COL_PACKET_IFACE,
87 COL_PACKET_PROTOCOL,
88 COL_PARTITION,
89 COL_PID,
90 COL_PIDFD_COMM,
91 COL_PIDFD_NSPID,
92 COL_PIDFD_PID,
93 COL_PING_ID,
94 COL_POS,
95 COL_PTMX_TTY_INDEX,
96 COL_RAW_PROTOCOL,
97 COL_RDEV,
98 COL_SIGNALFD_MASK,
99 COL_SIZE,
100 COL_SOCK_LISTENING,
101 COL_SOCK_NETNS,
102 COL_SOCK_PROTONAME,
103 COL_SOCK_SHUTDOWN,
104 COL_SOCK_STATE,
105 COL_SOCK_TYPE,
106 COL_SOURCE,
107 COL_STTYPE,
108 COL_TCP_LADDR,
109 COL_TCP_RADDR,
110 COL_TCP_LPORT,
111 COL_TCP_RPORT,
112 COL_TID,
113 COL_TIMERFD_CLOCKID,
114 COL_TIMERFD_INTERVAL,
115 COL_TIMERFD_REMAINING,
116 COL_TUN_IFACE,
117 COL_TYPE,
118 COL_UDP_LADDR,
119 COL_UDP_RADDR,
120 COL_UDP_LPORT,
121 COL_UDP_RPORT,
122 COL_UDPLITE_LADDR,
123 COL_UDPLITE_RADDR,
124 COL_UDPLITE_LPORT,
125 COL_UDPLITE_RPORT,
126 COL_UID, /* process */
127 COL_UNIX_PATH,
128 COL_USER, /* process */
129 COL_XMODE,
130 LSFD_N_COLS /* This must be at last. */
131 };
132
133 /*
134 * Process structure
135 */
136 enum association {
137 ASSOC_EXE = 1,
138 ASSOC_CWD,
139 ASSOC_ROOT,
140 ASSOC_NS_CGROUP,
141 ASSOC_NS_IPC,
142 ASSOC_NS_MNT,
143 ASSOC_NS_NET,
144 ASSOC_NS_PID,
145 ASSOC_NS_PID4C,
146 ASSOC_NS_TIME,
147 ASSOC_NS_TIME4C,
148 ASSOC_NS_USER,
149 ASSOC_NS_UTS,
150 ASSOC_MEM, /* private file mapping */
151 ASSOC_SHM, /* shared file mapping */
152 N_ASSOCS,
153 };
154
155 struct proc {
156 pid_t pid;
157 struct proc * leader;
158 char *command;
159 uid_t uid;
160 ino_t ns_mnt;
161 struct list_head procs;
162 struct list_head files;
163 unsigned int kthread: 1;
164 struct list_head eventpolls;
165 };
166
167 struct proc *get_proc(pid_t pid);
168
169 /*
170 * File class
171 */
172 struct file {
173 struct list_head files;
174 const struct file_class *class;
175 int association;
176 char *name;
177 struct stat stat;
178 mode_t mode;
179 struct proc *proc;
180
181 uint64_t pos;
182 uint64_t map_start;
183 uint64_t map_end;
184
185 unsigned int sys_flags;
186 unsigned int mnt_id;
187
188 struct {
189 uint8_t read:1, write:1;
190 } locked;
191 uint8_t multiplexed;
192 };
193
194 #define is_opened_file(_f) ((_f)->association >= 0)
195 #define is_mapped_file(_f) (is_association((_f), SHM) || is_association((_f), MEM))
196 #define is_association(_f, a) ((_f)->association < 0 && (_f)->association == -ASSOC_ ## a)
197
198 struct file_class {
199 const struct file_class *super;
200 size_t size;
201 void (*initialize_class)(void);
202 void (*finalize_class)(void);
203 bool (*fill_column)(struct proc *proc,
204 struct file *file,
205 struct libscols_line *ln,
206 int column_id,
207 size_t column_index);
208 int (*handle_fdinfo)(struct file *file, const char *key, const char* value);
209 void (*attach_xinfo)(struct file *file);
210 void (*initialize_content)(struct file *file);
211 void (*free_content)(struct file *file);
212 const struct ipc_class *(*get_ipc_class)(struct file *file);
213 };
214
215 extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class,
216 nsfs_file_class, mqueue_file_class;
217
218 /*
219 * IPC
220 */
221 struct ipc {
222 const struct ipc_class *class;
223 struct list_head endpoints;
224 struct list_head ipcs;
225 };
226
227 struct ipc_endpoint {
228 struct ipc *ipc;
229 struct list_head endpoints;
230 };
231
232 struct ipc_class {
233 size_t size;
234 unsigned int (*get_hash)(struct file *file);
235 bool (*is_suitable_ipc)(struct ipc *ipc, struct file *file);
236 void (*free)(struct ipc *ipc);
237 };
238
239 struct ipc *new_ipc(const struct ipc_class *class);
240 struct ipc *get_ipc(struct file *file);
241 void add_ipc(struct ipc *ipc, unsigned int hash);
242 void init_endpoint(struct ipc_endpoint *endpoint);
243 void add_endpoint(struct ipc_endpoint *endpoint, struct ipc *ipc);
244 #define foreach_endpoint(E,ENDPOINT) list_for_each_backwardly(E, &((ENDPOINT).ipc->endpoints))
245
246 enum decode_source_bit {
247 DECODE_SOURCE_MAJMIN_BIT = 1 << 0,
248 DECODE_SOURCE_PARTITION_BIT = 1 << 1,
249 DECODE_SOURCE_FILESYS_BIT = 1 << 2,
250 };
251
252 enum decode_source_level {
253 DECODE_SOURCE_MAJMIN = DECODE_SOURCE_MAJMIN_BIT,
254 DECODE_SOURCE_PARTITION = DECODE_SOURCE_PARTITION_BIT | DECODE_SOURCE_MAJMIN,
255 DECODE_SOURCE_FILESYS = DECODE_SOURCE_FILESYS_BIT | DECODE_SOURCE_PARTITION,
256 DECODE_SOURCE_FULL = DECODE_SOURCE_FILESYS,
257 };
258
259 void decode_source(char *buf, size_t bufsize, unsigned int dev_major, unsigned int dev_minor,
260 enum decode_source_level level);
261 /*
262 * Name managing
263 */
264 struct name_manager;
265
266 struct name_manager *new_name_manager(void);
267 void free_name_manager(struct name_manager *nm);
268 const char *get_name(struct name_manager *nm, unsigned long id);
269 unsigned long add_name(struct name_manager *nm, const char *name);
270
271 const char *get_partition(dev_t dev);
272 const char *get_blkdrv(unsigned long major);
273 const char *get_chrdrv(unsigned long major);
274 const char *get_miscdev(unsigned long minor);
275 const char *get_nodev_filesystem(unsigned long minor);
276 void add_nodev(unsigned long minor, const char *filesystem);
277
278 /*
279 * Net namespace
280 */
281 void load_sock_xinfo(struct path_cxt *pc, const char *name, ino_t netns);
282 bool is_nsfs_dev(dev_t dev);
283
284 /*
285 * POSIX Mqueue
286 */
287 /* 0 is assumed as the major dev for DEV. */
288 bool is_mqueue_dev(dev_t dev);
289
290 /*
291 * Eventpoll
292 */
293 bool is_multiplexed_by_eventpoll(int fd, struct list_head *eventpolls);
294
295 #endif /* UTIL_LINUX_LSFD_H */