]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/lsfd.h
lsfd: use extra information loaded from /proc/net/udplite
[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_FD,
50 COL_FLAGS,
51 COL_INODE,
52 COL_INET_LADDR,
53 COL_INET_RADDR,
54 COL_INET6_LADDR,
55 COL_INET6_RADDR,
56 COL_KNAME,
57 COL_KTHREAD,
58 COL_MAJMIN,
59 COL_MAPLEN,
60 COL_MISCDEV,
61 COL_MNT_ID,
62 COL_MODE,
63 COL_NAME,
64 COL_NLINK,
65 COL_NS_NAME,
66 COL_NS_TYPE,
67 COL_PARTITION,
68 COL_PID,
69 COL_PIDFD_COMM,
70 COL_PIDFD_NSPID,
71 COL_PIDFD_PID,
72 COL_POS,
73 COL_RAW_PROTOCOL,
74 COL_RDEV,
75 COL_SIZE,
76 COL_SOCK_LISTENING,
77 COL_SOCK_NETNS,
78 COL_SOCK_PROTONAME,
79 COL_SOCK_STATE,
80 COL_SOCK_TYPE,
81 COL_SOURCE,
82 COL_STTYPE,
83 COL_TCP_LADDR,
84 COL_TCP_RADDR,
85 COL_TCP_LPORT,
86 COL_TCP_RPORT,
87 COL_TID,
88 COL_TYPE,
89 COL_UDP_LADDR,
90 COL_UDP_RADDR,
91 COL_UDP_LPORT,
92 COL_UDP_RPORT,
93 COL_UDPLITE_LADDR,
94 COL_UDPLITE_RADDR,
95 COL_UDPLITE_LPORT,
96 COL_UDPLITE_RPORT,
97 COL_UID, /* process */
98 COL_UNIX_PATH,
99 COL_USER, /* process */
100 COL_FUID, /* file */
101 COL_OWNER, /* file */
102 LSFD_N_COLS /* This must be at last. */
103 };
104
105 /*
106 * Process structure
107 */
108 enum association {
109 ASSOC_EXE = 1,
110 ASSOC_CWD,
111 ASSOC_ROOT,
112 ASSOC_NS_CGROUP,
113 ASSOC_NS_IPC,
114 ASSOC_NS_MNT,
115 ASSOC_NS_NET,
116 ASSOC_NS_PID,
117 ASSOC_NS_PID4C,
118 ASSOC_NS_TIME,
119 ASSOC_NS_TIME4C,
120 ASSOC_NS_USER,
121 ASSOC_NS_UTS,
122 ASSOC_MEM, /* private file mapping */
123 ASSOC_SHM, /* shared file mapping */
124 N_ASSOCS,
125 };
126
127 struct proc {
128 pid_t pid;
129 struct proc * leader;
130 char *command;
131 uid_t uid;
132 ino_t ns_mnt;
133 struct list_head procs;
134 struct list_head files;
135 unsigned int kthread: 1;
136 };
137
138 struct proc *get_proc(pid_t pid);
139
140 /*
141 * File class
142 */
143 struct file {
144 struct list_head files;
145 const struct file_class *class;
146 int association;
147 char *name;
148 struct stat stat;
149 mode_t mode;
150 struct proc *proc;
151
152 uint64_t pos;
153 uint64_t map_start;
154 uint64_t map_end;
155
156 unsigned int sys_flags;
157 unsigned int mnt_id;
158 };
159
160 #define is_opened_file(_f) ((_f)->association >= 0)
161 #define is_mapped_file(_f) (is_association((_f), SHM) || is_association((_f), MEM))
162 #define is_association(_f, a) ((_f)->association < 0 && (_f)->association == -ASSOC_ ## a)
163
164 struct file_class {
165 const struct file_class *super;
166 size_t size;
167 void (*initialize_class)(void);
168 void (*finalize_class)(void);
169 bool (*fill_column)(struct proc *proc,
170 struct file *file,
171 struct libscols_line *ln,
172 int column_id,
173 size_t column_index);
174 int (*handle_fdinfo)(struct file *file, const char *key, const char* value);
175 void (*attach_xinfo)(struct file *file);
176 void (*initialize_content)(struct file *file);
177 void (*free_content)(struct file *file);
178 const struct ipc_class *(*get_ipc_class)(struct file *file);
179 };
180
181 extern const struct file_class file_class, cdev_class, bdev_class, sock_class, unkn_class, fifo_class,
182 nsfs_file_class;
183
184 /*
185 * IPC
186 */
187 struct ipc {
188 const struct ipc_class *class;
189 struct list_head endpoints;
190 struct list_head ipcs;
191 };
192
193 struct ipc_endpoint {
194 struct ipc *ipc;
195 struct list_head endpoints;
196 };
197
198 struct ipc_class {
199 unsigned int (*get_hash)(struct file *file);
200 bool (*is_suitable_ipc)(struct ipc *ipc, struct file *file);
201 void (*free)(struct ipc *ipc);
202 };
203
204 struct ipc *get_ipc(struct file *file);
205 void add_ipc(struct ipc *ipc, unsigned int hash);
206
207 /*
208 * Name managing
209 */
210 struct name_manager;
211
212 struct name_manager *new_name_manager(void);
213 void free_name_manager(struct name_manager *nm);
214 const char *get_name(struct name_manager *nm, unsigned long id);
215 unsigned long add_name(struct name_manager *nm, const char *name);
216
217 const char *get_partition(dev_t dev);
218 const char *get_blkdrv(unsigned long major);
219 const char *get_chrdrv(unsigned long major);
220 const char *get_miscdev(unsigned long minor);
221 const char *get_nodev_filesystem(unsigned long minor);
222
223 static inline void xstrappend(char **a, const char *b)
224 {
225 if (strappend(a, b) < 0)
226 err(XALLOC_EXIT_CODE, _("failed to allocate memory for string"));
227 }
228
229 static inline void xstrputc(char **a, char c)
230 {
231 char b[] = {c, '\0'};
232 xstrappend(a, b);
233 }
234
235 /*
236 * Net namespace
237 */
238 void load_sock_xinfo(struct path_cxt *pc, const char *name, ino_t netns);
239 bool is_nsfs_dev(dev_t dev);
240
241 #endif /* UTIL_LINUX_LSFD_H */