xref: /freebsd/lib/libprocstat/libprocstat.h (revision 79102e108099237b1d702e960706a739c7b071bd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
5  * Copyright (c) 2017 Dell EMC
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef _LIBPROCSTAT_H_
31 #define	_LIBPROCSTAT_H_
32 
33 /*
34  * XXX: sys/elf.h conflicts with zfs_context.h. Workaround this by not
35  * including conflicting parts when building zfs code.
36  */
37 #ifndef ZFS
38 #include <sys/elf.h>
39 #endif
40 #include <sys/caprights.h>
41 
42 /*
43  * Vnode types.
44  */
45 #define	PS_FST_VTYPE_VNON	1
46 #define	PS_FST_VTYPE_VREG	2
47 #define	PS_FST_VTYPE_VDIR	3
48 #define	PS_FST_VTYPE_VBLK	4
49 #define	PS_FST_VTYPE_VCHR	5
50 #define	PS_FST_VTYPE_VLNK	6
51 #define	PS_FST_VTYPE_VSOCK	7
52 #define	PS_FST_VTYPE_VFIFO	8
53 #define	PS_FST_VTYPE_VBAD	9
54 #define	PS_FST_VTYPE_UNKNOWN	255
55 
56 /*
57  * Descriptor types.
58  */
59 #define	PS_FST_TYPE_VNODE	1
60 #define	PS_FST_TYPE_FIFO	2
61 #define	PS_FST_TYPE_SOCKET	3
62 #define	PS_FST_TYPE_PIPE	4
63 #define	PS_FST_TYPE_PTS		5
64 #define	PS_FST_TYPE_KQUEUE	6
65 /* was PS_FST_TYPE_CRYPTO	7 */
66 #define	PS_FST_TYPE_MQUEUE	8
67 #define	PS_FST_TYPE_SHM		9
68 #define	PS_FST_TYPE_SEM		10
69 #define	PS_FST_TYPE_UNKNOWN	11
70 #define	PS_FST_TYPE_NONE	12
71 #define	PS_FST_TYPE_PROCDESC	13
72 #define	PS_FST_TYPE_DEV		14
73 #define	PS_FST_TYPE_EVENTFD	15
74 #define	PS_FST_TYPE_INOTIFY	16
75 
76 /*
77  * Special descriptor numbers.
78  */
79 #define	PS_FST_UFLAG_RDIR	0x0001
80 #define	PS_FST_UFLAG_CDIR	0x0002
81 #define	PS_FST_UFLAG_JAIL	0x0004
82 #define	PS_FST_UFLAG_TRACE	0x0008
83 #define	PS_FST_UFLAG_TEXT	0x0010
84 #define	PS_FST_UFLAG_MMAP	0x0020
85 #define	PS_FST_UFLAG_CTTY	0x0040
86 
87 /*
88  * Descriptor flags.
89  */
90 #define PS_FST_FFLAG_READ	0x0001
91 #define PS_FST_FFLAG_WRITE	0x0002
92 #define	PS_FST_FFLAG_NONBLOCK	0x0004
93 #define	PS_FST_FFLAG_APPEND	0x0008
94 #define	PS_FST_FFLAG_SHLOCK	0x0010
95 #define	PS_FST_FFLAG_EXLOCK	0x0020
96 #define	PS_FST_FFLAG_ASYNC	0x0040
97 #define	PS_FST_FFLAG_SYNC	0x0080
98 #define	PS_FST_FFLAG_NOFOLLOW	0x0100
99 #define	PS_FST_FFLAG_CREAT	0x0200
100 #define	PS_FST_FFLAG_TRUNC	0x0400
101 #define	PS_FST_FFLAG_EXCL	0x0800
102 #define	PS_FST_FFLAG_DIRECT	0x1000
103 #define	PS_FST_FFLAG_EXEC	0x2000
104 #define	PS_FST_FFLAG_HASLOCK	0x4000
105 
106 #if !defined(__ILP32__) && !defined(__riscv)
107 /* Target architecture supports 32-bit compat */
108 #define	PS_ARCH_HAS_FREEBSD32	1
109 #endif
110 
111 struct kinfo_kstack;
112 struct kinfo_proc;
113 struct kinfo_vmentry;
114 struct kinfo_knote;
115 struct procstat;
116 struct ptrace_lwpinfo;
117 struct rlimit;
118 struct filestat {
119 	int	fs_type;	/* Descriptor type. */
120 	int	fs_flags;	/* filestat specific flags. */
121 	int	fs_fflags;	/* Descriptor access flags. */
122 	int	fs_uflags;	/* How this file is used. */
123 	int	fs_fd;		/* File descriptor number. */
124 	int	fs_ref_count;	/* Reference count. */
125 	off_t	fs_offset;	/* Seek location. */
126 	void	*fs_typedep;	/* Type dependent data. */
127 	char	*fs_path;
128 	STAILQ_ENTRY(filestat)	next;
129 	cap_rights_t	fs_cap_rights;	/* Capability rights, if flag set. */
130 };
131 struct vnstat {
132 	uint64_t	vn_fileid;
133 	uint64_t	vn_size;
134 	uint64_t	vn_dev;
135 	uint64_t	vn_fsid;
136 	char		*vn_mntdir;
137 	int		vn_type;
138 	uint16_t	vn_mode;
139 	char		vn_devname[SPECNAMELEN + 1];
140 };
141 struct ptsstat {
142 	uint64_t	dev;
143 	char		devname[SPECNAMELEN + 1];
144 };
145 struct pipestat {
146 	size_t		buffer_cnt;
147 	uint64_t	addr;
148 	uint64_t	peer;
149 };
150 struct semstat {
151 	uint32_t	value;
152 	uint16_t	mode;
153 };
154 struct shmstat {
155 	uint64_t	size;
156 	uint16_t	mode;
157 };
158 struct sockstat {
159 	uint64_t	so_addr;
160 	uint64_t	so_pcb;
161 	uint64_t	unp_conn;
162 	int		dom_family;
163 	int		proto;
164 	int		so_rcv_sb_state;
165 	int		so_snd_sb_state;
166 	struct sockaddr_storage	sa_local;	/* Socket address. */
167 	struct sockaddr_storage	sa_peer;	/* Peer address. */
168 	int		type;
169 	char		dname[32];
170 	unsigned int	sendq;
171 	unsigned int	recvq;
172 };
173 
174 STAILQ_HEAD(filestat_list, filestat);
175 
176 struct advlock {
177 	int		rw;			/* PS_ADVLOCK_RO/RW */
178 	int		type;			/* PS_ADVLOCK_TYPE_ */
179 	int		pid;
180 	int		sysid;
181 	uint64_t	file_fsid;
182 	uint64_t	file_rdev;
183 	uint64_t	file_fileid;
184 	off_t		start;
185 	off_t		len;			/* len == 0 till the EOF */
186 	const char	*path;
187 	STAILQ_ENTRY(advlock)	next;
188 };
189 
190 #define	PS_ADVLOCK_RO		0x01
191 #define	PS_ADVLOCK_RW		0x02
192 
193 #define	PS_ADVLOCK_TYPE_FLOCK	0x01
194 #define	PS_ADVLOCK_TYPE_PID	0x02
195 #define	PS_ADVLOCK_TYPE_REMOTE	0x03
196 
197 STAILQ_HEAD(advlock_list, advlock);
198 
199 __BEGIN_DECLS
200 void	procstat_close(struct procstat *procstat);
201 void	procstat_freeadvlock(struct procstat *procstat,
202     struct advlock_list *advlocks);
203 void	procstat_freeargv(struct procstat *procstat);
204 #ifndef ZFS
205 void	procstat_freeauxv(struct procstat *procstat, Elf_Auxinfo *auxv);
206 #endif
207 void	procstat_freeenvv(struct procstat *procstat);
208 void	procstat_freegroups(struct procstat *procstat, gid_t *groups);
209 void	procstat_freekqinfo(struct procstat *procstat, struct kinfo_knote *kni);
210 void	procstat_freekstack(struct procstat *procstat,
211     struct kinfo_kstack *kkstp);
212 void	procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
213 void	procstat_freefiles(struct procstat *procstat,
214     struct filestat_list *head);
215 void	procstat_freeptlwpinfo(struct procstat *procstat,
216     struct ptrace_lwpinfo *pl);
217 void	procstat_freerlimitusage(struct procstat *procstat, rlim_t *resusage);
218 void	procstat_freevmmap(struct procstat *procstat,
219     struct kinfo_vmentry *vmmap);
220 struct advlock_list	*procstat_getadvlock(struct procstat *procstat);
221 struct filestat_list	*procstat_getfiles(struct procstat *procstat,
222     struct kinfo_proc *kp, int mmapped);
223 struct kinfo_proc	*procstat_getprocs(struct procstat *procstat,
224     int what, int arg, unsigned int *count);
225 struct kinfo_knote	*procstat_get_kqueue_info(struct procstat *procstat,
226     struct kinfo_proc *kp, int kqfd, unsigned int *count, char *errbuf);
227 int	procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
228     struct pipestat *pipe, char *errbuf);
229 int	procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
230     struct ptsstat *pts, char *errbuf);
231 int	procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
232     struct semstat *sem, char *errbuf);
233 int	procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
234     struct shmstat *shm, char *errbuf);
235 int	procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
236     struct sockstat *sock, char *errbuf);
237 int	procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
238     struct vnstat *vn, char *errbuf);
239 char	**procstat_getargv(struct procstat *procstat, struct kinfo_proc *p,
240     size_t nchr);
241 #ifndef ZFS
242 Elf_Auxinfo	*procstat_getauxv(struct procstat *procstat,
243     struct kinfo_proc *kp, unsigned int *cntp);
244 #endif
245 struct ptrace_lwpinfo	*procstat_getptlwpinfo(struct procstat *procstat,
246     unsigned int *cntp);
247 char	**procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p,
248     size_t nchr);
249 gid_t	*procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
250     unsigned int *count);
251 struct kinfo_kstack	*procstat_getkstack(struct procstat *procstat,
252     struct kinfo_proc *kp, unsigned int *count);
253 int	procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp,
254     int *osrelp);
255 int	procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
256     char *pathname, size_t maxlen);
257 int	procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
258     int which, struct rlimit* rlimit);
259 rlim_t	*procstat_getrlimitusage(struct procstat *procstat,
260     struct kinfo_proc *kp, unsigned int *cntp);
261 int	procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
262     unsigned short* umask);
263 struct kinfo_vmentry	*procstat_getvmmap(struct procstat *procstat,
264     struct kinfo_proc *kp, unsigned int *count);
265 struct procstat	*procstat_open_core(const char *filename);
266 struct procstat	*procstat_open_sysctl(void);
267 struct procstat	*procstat_open_kvm(const char *nlistf, const char *memf);
268 __END_DECLS
269 
270 #endif	/* !_LIBPROCSTAT_H_ */
271