]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsfd.h
lsfd: add BPF-MAP.TYPE, BPF-MAP.TYPE.RAW, and BPF-MAP.ID columns
[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_PROG_ID,
50 COL_BPF_PROG_TYPE,
51 COL_BPF_PROG_TYPE_RAW,
52 COL_CHRDRV,
53 COL_COMMAND,
54 COL_DELETED,
55 COL_DEV,
56 COL_DEVTYPE,
57 COL_ENDPOINTS,
58 COL_EVENTFD_ID,
59 COL_EVENTPOLL_TFDS,
60 COL_FD,
61 COL_FLAGS,
62 COL_FUID, /* file */
63 COL_INET_LADDR,
64 COL_INET_RADDR,
65 COL_INET6_LADDR,
66 COL_INET6_RADDR,
67 COL_INODE,
68 COL_INOTIFY_INODES,
69 COL_INOTIFY_INODES_RAW,
70 COL_KNAME,
71 COL_KTHREAD,
72 COL_MAJMIN,
73 COL_MAPLEN,
74 COL_MISCDEV,
75 COL_MNT_ID,
76 COL_MODE,
77 COL_NAME,
78 COL_NETLINK_GROUPS,
79 COL_NETLINK_LPORT,
80 COL_NETLINK_PROTOCOL,
81 COL_NLINK,
82 COL_NS_NAME,
83 COL_NS_TYPE,
84 COL_OWNER, /* file */
85 COL_PACKET_IFACE,
86 COL_PACKET_PROTOCOL,
87 COL_PARTITION,
88 COL_PID,
89 COL_PIDFD_COMM,
90 COL_PIDFD_NSPID,
91 COL_PIDFD_PID,
92 COL_PING_ID,
93 COL_POS,
94 COL_RAW_PROTOCOL,
95 COL_RDEV,
96 COL_SIGNALFD_MASK,
97 COL_SIZE,
98 COL_SOCK_LISTENING,
99 COL_SOCK_NETNS,
100 COL_SOCK_PROTONAME,
101 COL_SOCK_STATE,
102 COL_SOCK_TYPE,
103 COL_SOURCE,
104 COL_STTYPE,
105 COL_TCP_LADDR,
106 COL_TCP_RADDR,
107 COL_TCP_LPORT,
108 COL_TCP_RPORT,
109 COL_TID,
110 COL_TIMERFD_CLOCKID,
111 COL_TIMERFD_INTERVAL,
112 COL_TIMERFD_REMAINING,
113 COL_TUN_IFACE,
114 COL_TYPE,
115 COL_UDP_LADDR,
116 COL_UDP_RADDR,
117 COL_UDP_LPORT,
118 COL_UDP_RPORT,
119 COL_UDPLITE_LADDR,
120 COL_UDPLITE_RADDR,
121 COL_UDPLITE_LPORT,
122 COL_UDPLITE_RPORT,
123 COL_UID, /* process */
124 COL_UNIX_PATH,
125 COL_USER, /* process */
126 COL_XMODE,
127 LSFD_N_COLS /* This must be at last. */
128 };
129
130 /*
131 * Process structure
132 */
133 enum association {
134 ASSOC_EXE = 1,
135 ASSOC_CWD,
136 ASSOC_ROOT,
137 ASSOC_NS_CGROUP,
138 ASSOC_NS_IPC,
139 ASSOC_NS_MNT,
140 ASSOC_NS_NET,
141 ASSOC_NS_PID,
142 ASSOC_NS_PID4C,
143 ASSOC_NS_TIME,
144 ASSOC_NS_TIME4C,
145 ASSOC_NS_USER,
146 ASSOC_NS_UTS,
147 ASSOC_MEM, /* private file mapping */
148 ASSOC_SHM, /* shared file mapping */
149 N_ASSOCS,
150 };
151
152 struct proc {
153 pid_t pid;
154 struct proc * leader;
155 char *command;
156 uid_t uid;
157 ino_t ns_mnt;
158 struct list_head procs;
159 struct list_head files;
160 unsigned int kthread: 1;
161 struct list_head eventpolls;
162 };
163
164 struct proc *get_proc(pid_t pid);
165
166 /*
167 * File class
168 */
169 struct file {
170 struct list_head files;
171 const struct file_class *class;
172 int association;
173 char *name;
174 struct stat stat;
175 mode_t mode;
176 struct proc *proc;
177
178 uint64_t pos;
179 uint64_t map_start;
180 uint64_t map_end;
181
182 unsigned int sys_flags;
183 unsigned int mnt_id;
184
185 struct {
186 uint8_t read:1, write:1;
187 } locked;
188 uint8_t multiplexed;
189 };
190
191 #define is_opened_file(_f) ((_f)->association >= 0)
192 #define is_mapped_file(_f) (is_association((_f), SHM) || is_association((_f), MEM))
193 #define is_association(_f, a) ((_f)->association < 0 && (_f)->association == -ASSOC_ ## a)
194
195 struct file_class {
196 const struct file_class *super;
197 size_t size;
198 void (*initialize_class)(void);
199 void (*finalize_class)(void);
200 bool (*fill_column)(struct proc *proc,
201 struct file *file,
202 struct libscols_line *ln,
203 int column_id,
204 size_t column_index);
205 int (*handle_fdinfo)(struct file *file, const char *key, const char* value);
206 void (*attach_xinfo)(struct file *file);
207 void (*initialize_content)(struct file *file);
208 void (*free_content)(struct file *file);
209 const struct ipc_class *(*get_ipc_class)(struct file *file);
210 };
211
212 extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class,
213 nsfs_file_class, mqueue_file_class;
214
215 /*
216 * IPC
217 */
218 struct ipc {
219 const struct ipc_class *class;
220 struct list_head endpoints;
221 struct list_head ipcs;
222 };
223
224 struct ipc_endpoint {
225 struct ipc *ipc;
226 struct list_head endpoints;
227 };
228
229 struct ipc_class {
230 size_t size;
231 unsigned int (*get_hash)(struct file *file);
232 bool (*is_suitable_ipc)(struct ipc *ipc, struct file *file);
233 void (*free)(struct ipc *ipc);
234 };
235
236 struct ipc *new_ipc(const struct ipc_class *class);
237 struct ipc *get_ipc(struct file *file);
238 void add_ipc(struct ipc *ipc, unsigned int hash);
239 void init_endpoint(struct ipc_endpoint *endpoint);
240 void add_endpoint(struct ipc_endpoint *endpoint, struct ipc *ipc);
241 #define foreach_endpoint(E,ENDPOINT) list_for_each_backwardly(E, &((ENDPOINT).ipc->endpoints))
242
243 enum decode_source_bit {
244 DECODE_SOURCE_MAJMIN_BIT = 1 << 0,
245 DECODE_SOURCE_PARTITION_BIT = 1 << 1,
246 DECODE_SOURCE_FILESYS_BIT = 1 << 2,
247 };
248
249 enum decode_source_level {
250 DECODE_SOURCE_MAJMIN = DECODE_SOURCE_MAJMIN_BIT,
251 DECODE_SOURCE_PARTITION = DECODE_SOURCE_PARTITION_BIT | DECODE_SOURCE_MAJMIN,
252 DECODE_SOURCE_FILESYS = DECODE_SOURCE_FILESYS_BIT | DECODE_SOURCE_PARTITION,
253 DECODE_SOURCE_FULL = DECODE_SOURCE_FILESYS,
254 };
255
256 void decode_source(char *buf, size_t bufsize, unsigned int dev_major, unsigned int dev_minor,
257 enum decode_source_level level);
258 /*
259 * Name managing
260 */
261 struct name_manager;
262
263 struct name_manager *new_name_manager(void);
264 void free_name_manager(struct name_manager *nm);
265 const char *get_name(struct name_manager *nm, unsigned long id);
266 unsigned long add_name(struct name_manager *nm, const char *name);
267
268 const char *get_partition(dev_t dev);
269 const char *get_blkdrv(unsigned long major);
270 const char *get_chrdrv(unsigned long major);
271 const char *get_miscdev(unsigned long minor);
272 const char *get_nodev_filesystem(unsigned long minor);
273 void add_nodev(unsigned long minor, const char *filesystem);
274
275 static inline void xstrappend(char **a, const char *b)
276 {
277 if (strappend(a, b) < 0)
278 err(XALLOC_EXIT_CODE, _("failed to allocate memory for string"));
279 }
280
281 static inline void xstrputc(char **a, char c)
282 {
283 char b[] = {c, '\0'};
284 xstrappend(a, b);
285 }
286
287 /*
288 * Net namespace
289 */
290 void load_sock_xinfo(struct path_cxt *pc, const char *name, ino_t netns);
291 bool is_nsfs_dev(dev_t dev);
292
293 /*
294 * POSIX Mqueue
295 */
296 /* 0 is assumed as the major dev for DEV. */
297 bool is_mqueue_dev(dev_t dev);
298
299 /*
300 * Eventpoll
301 */
302 bool is_multiplexed_by_eventpoll(int fd, struct list_head *eventpolls);
303
304 #endif /* UTIL_LINUX_LSFD_H */