]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsfd.h
Merge branch 'libblkid/drbd-simplify' of https://github.com/t-8ch/util-linux
[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_RAW_PROTOCOL,
96 COL_RDEV,
97 COL_SIGNALFD_MASK,
98 COL_SIZE,
99 COL_SOCK_LISTENING,
100 COL_SOCK_NETNS,
101 COL_SOCK_PROTONAME,
102 COL_SOCK_STATE,
103 COL_SOCK_TYPE,
104 COL_SOURCE,
105 COL_STTYPE,
106 COL_TCP_LADDR,
107 COL_TCP_RADDR,
108 COL_TCP_LPORT,
109 COL_TCP_RPORT,
110 COL_TID,
111 COL_TIMERFD_CLOCKID,
112 COL_TIMERFD_INTERVAL,
113 COL_TIMERFD_REMAINING,
114 COL_TUN_IFACE,
115 COL_TYPE,
116 COL_UDP_LADDR,
117 COL_UDP_RADDR,
118 COL_UDP_LPORT,
119 COL_UDP_RPORT,
120 COL_UDPLITE_LADDR,
121 COL_UDPLITE_RADDR,
122 COL_UDPLITE_LPORT,
123 COL_UDPLITE_RPORT,
124 COL_UID, /* process */
125 COL_UNIX_PATH,
126 COL_USER, /* process */
127 COL_XMODE,
128 LSFD_N_COLS /* This must be at last. */
129 };
130
131 /*
132 * Process structure
133 */
134 enum association {
135 ASSOC_EXE = 1,
136 ASSOC_CWD,
137 ASSOC_ROOT,
138 ASSOC_NS_CGROUP,
139 ASSOC_NS_IPC,
140 ASSOC_NS_MNT,
141 ASSOC_NS_NET,
142 ASSOC_NS_PID,
143 ASSOC_NS_PID4C,
144 ASSOC_NS_TIME,
145 ASSOC_NS_TIME4C,
146 ASSOC_NS_USER,
147 ASSOC_NS_UTS,
148 ASSOC_MEM, /* private file mapping */
149 ASSOC_SHM, /* shared file mapping */
150 N_ASSOCS,
151 };
152
153 struct proc {
154 pid_t pid;
155 struct proc * leader;
156 char *command;
157 uid_t uid;
158 ino_t ns_mnt;
159 struct list_head procs;
160 struct list_head files;
161 unsigned int kthread: 1;
162 struct list_head eventpolls;
163 };
164
165 struct proc *get_proc(pid_t pid);
166
167 /*
168 * File class
169 */
170 struct file {
171 struct list_head files;
172 const struct file_class *class;
173 int association;
174 char *name;
175 struct stat stat;
176 mode_t mode;
177 struct proc *proc;
178
179 uint64_t pos;
180 uint64_t map_start;
181 uint64_t map_end;
182
183 unsigned int sys_flags;
184 unsigned int mnt_id;
185
186 struct {
187 uint8_t read:1, write:1;
188 } locked;
189 uint8_t multiplexed;
190 };
191
192 #define is_opened_file(_f) ((_f)->association >= 0)
193 #define is_mapped_file(_f) (is_association((_f), SHM) || is_association((_f), MEM))
194 #define is_association(_f, a) ((_f)->association < 0 && (_f)->association == -ASSOC_ ## a)
195
196 struct file_class {
197 const struct file_class *super;
198 size_t size;
199 void (*initialize_class)(void);
200 void (*finalize_class)(void);
201 bool (*fill_column)(struct proc *proc,
202 struct file *file,
203 struct libscols_line *ln,
204 int column_id,
205 size_t column_index);
206 int (*handle_fdinfo)(struct file *file, const char *key, const char* value);
207 void (*attach_xinfo)(struct file *file);
208 void (*initialize_content)(struct file *file);
209 void (*free_content)(struct file *file);
210 const struct ipc_class *(*get_ipc_class)(struct file *file);
211 };
212
213 extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class,
214 nsfs_file_class, mqueue_file_class;
215
216 /*
217 * IPC
218 */
219 struct ipc {
220 const struct ipc_class *class;
221 struct list_head endpoints;
222 struct list_head ipcs;
223 };
224
225 struct ipc_endpoint {
226 struct ipc *ipc;
227 struct list_head endpoints;
228 };
229
230 struct ipc_class {
231 size_t size;
232 unsigned int (*get_hash)(struct file *file);
233 bool (*is_suitable_ipc)(struct ipc *ipc, struct file *file);
234 void (*free)(struct ipc *ipc);
235 };
236
237 struct ipc *new_ipc(const struct ipc_class *class);
238 struct ipc *get_ipc(struct file *file);
239 void add_ipc(struct ipc *ipc, unsigned int hash);
240 void init_endpoint(struct ipc_endpoint *endpoint);
241 void add_endpoint(struct ipc_endpoint *endpoint, struct ipc *ipc);
242 #define foreach_endpoint(E,ENDPOINT) list_for_each_backwardly(E, &((ENDPOINT).ipc->endpoints))
243
244 enum decode_source_bit {
245 DECODE_SOURCE_MAJMIN_BIT = 1 << 0,
246 DECODE_SOURCE_PARTITION_BIT = 1 << 1,
247 DECODE_SOURCE_FILESYS_BIT = 1 << 2,
248 };
249
250 enum decode_source_level {
251 DECODE_SOURCE_MAJMIN = DECODE_SOURCE_MAJMIN_BIT,
252 DECODE_SOURCE_PARTITION = DECODE_SOURCE_PARTITION_BIT | DECODE_SOURCE_MAJMIN,
253 DECODE_SOURCE_FILESYS = DECODE_SOURCE_FILESYS_BIT | DECODE_SOURCE_PARTITION,
254 DECODE_SOURCE_FULL = DECODE_SOURCE_FILESYS,
255 };
256
257 void decode_source(char *buf, size_t bufsize, unsigned int dev_major, unsigned int dev_minor,
258 enum decode_source_level level);
259 /*
260 * Name managing
261 */
262 struct name_manager;
263
264 struct name_manager *new_name_manager(void);
265 void free_name_manager(struct name_manager *nm);
266 const char *get_name(struct name_manager *nm, unsigned long id);
267 unsigned long add_name(struct name_manager *nm, const char *name);
268
269 const char *get_partition(dev_t dev);
270 const char *get_blkdrv(unsigned long major);
271 const char *get_chrdrv(unsigned long major);
272 const char *get_miscdev(unsigned long minor);
273 const char *get_nodev_filesystem(unsigned long minor);
274 void add_nodev(unsigned long minor, const char *filesystem);
275
276 static inline void xstrappend(char **a, const char *b)
277 {
278 if (strappend(a, b) < 0)
279 err(XALLOC_EXIT_CODE, _("failed to allocate memory for string"));
280 }
281
282 static inline void xstrputc(char **a, char c)
283 {
284 char b[] = {c, '\0'};
285 xstrappend(a, b);
286 }
287
288 static inline
289 __attribute__((__format__(printf, 2, 0)))
290 int xstrvfappend(char **a, const char *format, va_list ap)
291 {
292 int ret = strvfappend(a, format, ap);
293
294 if (ret < 0)
295 err(XALLOC_EXIT_CODE, "cannot allocate string");
296 return ret;
297
298 }
299
300 static inline
301 __attribute__ ((__format__ (__printf__, 2, 3)))
302 int xstrfappend(char **a, const char *format, ...)
303 {
304 va_list ap;
305 int ret;
306
307 va_start(ap, format);
308 ret = xstrvfappend(a, format, ap);
309 va_end(ap);
310
311 return ret;
312 }
313
314 /*
315 * Net namespace
316 */
317 void load_sock_xinfo(struct path_cxt *pc, const char *name, ino_t netns);
318 bool is_nsfs_dev(dev_t dev);
319
320 /*
321 * POSIX Mqueue
322 */
323 /* 0 is assumed as the major dev for DEV. */
324 bool is_mqueue_dev(dev_t dev);
325
326 /*
327 * Eventpoll
328 */
329 bool is_multiplexed_by_eventpoll(int fd, struct list_head *eventpolls);
330
331 #endif /* UTIL_LINUX_LSFD_H */