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