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