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