xref: /freebsd/lib/libprocstat/libprocstat.h (revision 716fd348)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
30  */
31 
32 #ifndef _LIBPROCSTAT_H_
33 #define	_LIBPROCSTAT_H_
34 
35 /*
36  * XXX: sys/elf.h conflicts with zfs_context.h. Workaround this by not
37  * including conflicting parts when building zfs code.
38  */
39 #ifndef ZFS
40 #include <sys/elf.h>
41 #endif
42 #include <sys/caprights.h>
43 
44 /*
45  * Vnode types.
46  */
47 #define	PS_FST_VTYPE_VNON	1
48 #define	PS_FST_VTYPE_VREG	2
49 #define	PS_FST_VTYPE_VDIR	3
50 #define	PS_FST_VTYPE_VBLK	4
51 #define	PS_FST_VTYPE_VCHR	5
52 #define	PS_FST_VTYPE_VLNK	6
53 #define	PS_FST_VTYPE_VSOCK	7
54 #define	PS_FST_VTYPE_VFIFO	8
55 #define	PS_FST_VTYPE_VBAD	9
56 #define	PS_FST_VTYPE_UNKNOWN	255
57 
58 /*
59  * Descriptor types.
60  */
61 #define	PS_FST_TYPE_VNODE	1
62 #define	PS_FST_TYPE_FIFO	2
63 #define	PS_FST_TYPE_SOCKET	3
64 #define	PS_FST_TYPE_PIPE	4
65 #define	PS_FST_TYPE_PTS		5
66 #define	PS_FST_TYPE_KQUEUE	6
67 /* was PS_FST_TYPE_CRYPTO	7 */
68 #define	PS_FST_TYPE_MQUEUE	8
69 #define	PS_FST_TYPE_SHM		9
70 #define	PS_FST_TYPE_SEM		10
71 #define	PS_FST_TYPE_UNKNOWN	11
72 #define	PS_FST_TYPE_NONE	12
73 #define	PS_FST_TYPE_PROCDESC	13
74 #define	PS_FST_TYPE_DEV		14
75 #define	PS_FST_TYPE_EVENTFD	15
76 
77 /*
78  * Special descriptor numbers.
79  */
80 #define	PS_FST_UFLAG_RDIR	0x0001
81 #define	PS_FST_UFLAG_CDIR	0x0002
82 #define	PS_FST_UFLAG_JAIL	0x0004
83 #define	PS_FST_UFLAG_TRACE	0x0008
84 #define	PS_FST_UFLAG_TEXT	0x0010
85 #define	PS_FST_UFLAG_MMAP	0x0020
86 #define	PS_FST_UFLAG_CTTY	0x0040
87 
88 /*
89  * Descriptor flags.
90  */
91 #define PS_FST_FFLAG_READ	0x0001
92 #define PS_FST_FFLAG_WRITE	0x0002
93 #define	PS_FST_FFLAG_NONBLOCK	0x0004
94 #define	PS_FST_FFLAG_APPEND	0x0008
95 #define	PS_FST_FFLAG_SHLOCK	0x0010
96 #define	PS_FST_FFLAG_EXLOCK	0x0020
97 #define	PS_FST_FFLAG_ASYNC	0x0040
98 #define	PS_FST_FFLAG_SYNC	0x0080
99 #define	PS_FST_FFLAG_NOFOLLOW	0x0100
100 #define	PS_FST_FFLAG_CREAT	0x0200
101 #define	PS_FST_FFLAG_TRUNC	0x0400
102 #define	PS_FST_FFLAG_EXCL	0x0800
103 #define	PS_FST_FFLAG_DIRECT	0x1000
104 #define	PS_FST_FFLAG_EXEC	0x2000
105 #define	PS_FST_FFLAG_HASLOCK	0x4000
106 
107 struct kinfo_kstack;
108 struct kinfo_vmentry;
109 struct procstat;
110 struct ptrace_lwpinfo;
111 struct rlimit;
112 struct filestat {
113 	int	fs_type;	/* Descriptor type. */
114 	int	fs_flags;	/* filestat specific flags. */
115 	int	fs_fflags;	/* Descriptor access flags. */
116 	int	fs_uflags;	/* How this file is used. */
117 	int	fs_fd;		/* File descriptor number. */
118 	int	fs_ref_count;	/* Reference count. */
119 	off_t	fs_offset;	/* Seek location. */
120 	void	*fs_typedep;	/* Type dependent data. */
121 	char	*fs_path;
122 	STAILQ_ENTRY(filestat)	next;
123 	cap_rights_t	fs_cap_rights;	/* Capability rights, if flag set. */
124 };
125 struct vnstat {
126 	uint64_t	vn_fileid;
127 	uint64_t	vn_size;
128 	uint64_t	vn_dev;
129 	uint64_t	vn_fsid;
130 	char		*vn_mntdir;
131 	int		vn_type;
132 	uint16_t	vn_mode;
133 	char		vn_devname[SPECNAMELEN + 1];
134 };
135 struct ptsstat {
136 	uint64_t	dev;
137 	char		devname[SPECNAMELEN + 1];
138 };
139 struct pipestat {
140 	size_t		buffer_cnt;
141 	uint64_t	addr;
142 	uint64_t	peer;
143 };
144 struct semstat {
145 	uint32_t	value;
146 	uint16_t	mode;
147 };
148 struct shmstat {
149 	uint64_t	size;
150 	uint16_t	mode;
151 };
152 struct sockstat {
153 	uint64_t	inp_ppcb;
154 	uint64_t	so_addr;
155 	uint64_t	so_pcb;
156 	uint64_t	unp_conn;
157 	int		dom_family;
158 	int		proto;
159 	int		so_rcv_sb_state;
160 	int		so_snd_sb_state;
161 	struct sockaddr_storage	sa_local;	/* Socket address. */
162 	struct sockaddr_storage	sa_peer;	/* Peer address. */
163 	int		type;
164 	char		dname[32];
165 	unsigned int	sendq;
166 	unsigned int	recvq;
167 };
168 
169 STAILQ_HEAD(filestat_list, filestat);
170 
171 struct advlock {
172 	int		rw;			/* PS_ADVLOCK_RO/RW */
173 	int		type;			/* PS_ADVLOCK_TYPE_ */
174 	int		pid;
175 	int		sysid;
176 	uint64_t	file_fsid;
177 	uint64_t	file_rdev;
178 	uint64_t	file_fileid;
179 	off_t		start;
180 	off_t		len;			/* len == 0 till the EOF */
181 	const char	*path;
182 	STAILQ_ENTRY(advlock)	next;
183 };
184 
185 #define	PS_ADVLOCK_RO		0x01
186 #define	PS_ADVLOCK_RW		0x02
187 
188 #define	PS_ADVLOCK_TYPE_FLOCK	0x01
189 #define	PS_ADVLOCK_TYPE_PID	0x02
190 #define	PS_ADVLOCK_TYPE_REMOTE	0x03
191 
192 STAILQ_HEAD(advlock_list, advlock);
193 
194 __BEGIN_DECLS
195 void	procstat_close(struct procstat *procstat);
196 void	procstat_freeadvlock(struct procstat *procstat,
197     struct advlock_list *advlocks);
198 void	procstat_freeargv(struct procstat *procstat);
199 #ifndef ZFS
200 void	procstat_freeauxv(struct procstat *procstat, Elf_Auxinfo *auxv);
201 #endif
202 void	procstat_freeenvv(struct procstat *procstat);
203 void	procstat_freegroups(struct procstat *procstat, gid_t *groups);
204 void	procstat_freekstack(struct procstat *procstat,
205     struct kinfo_kstack *kkstp);
206 void	procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
207 void	procstat_freefiles(struct procstat *procstat,
208     struct filestat_list *head);
209 void	procstat_freeptlwpinfo(struct procstat *procstat,
210     struct ptrace_lwpinfo *pl);
211 void	procstat_freevmmap(struct procstat *procstat,
212     struct kinfo_vmentry *vmmap);
213 struct advlock_list	*procstat_getadvlock(struct procstat *procstat);
214 struct filestat_list	*procstat_getfiles(struct procstat *procstat,
215     struct kinfo_proc *kp, int mmapped);
216 struct kinfo_proc	*procstat_getprocs(struct procstat *procstat,
217     int what, int arg, unsigned int *count);
218 int	procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
219     struct pipestat *pipe, char *errbuf);
220 int	procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
221     struct ptsstat *pts, char *errbuf);
222 int	procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
223     struct semstat *sem, char *errbuf);
224 int	procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
225     struct shmstat *shm, char *errbuf);
226 int	procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
227     struct sockstat *sock, char *errbuf);
228 int	procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
229     struct vnstat *vn, char *errbuf);
230 char	**procstat_getargv(struct procstat *procstat, struct kinfo_proc *p,
231     size_t nchr);
232 #ifndef ZFS
233 Elf_Auxinfo	*procstat_getauxv(struct procstat *procstat,
234     struct kinfo_proc *kp, unsigned int *cntp);
235 #endif
236 struct ptrace_lwpinfo	*procstat_getptlwpinfo(struct procstat *procstat,
237     unsigned int *cntp);
238 char	**procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p,
239     size_t nchr);
240 gid_t	*procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
241     unsigned int *count);
242 struct kinfo_kstack	*procstat_getkstack(struct procstat *procstat,
243     struct kinfo_proc *kp, unsigned int *count);
244 int	procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp,
245     int *osrelp);
246 int	procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
247     char *pathname, size_t maxlen);
248 int	procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
249     int which, struct rlimit* rlimit);
250 int	procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
251     unsigned short* umask);
252 struct kinfo_vmentry	*procstat_getvmmap(struct procstat *procstat,
253     struct kinfo_proc *kp, unsigned int *count);
254 struct procstat	*procstat_open_core(const char *filename);
255 struct procstat	*procstat_open_sysctl(void);
256 struct procstat	*procstat_open_kvm(const char *nlistf, const char *memf);
257 __END_DECLS
258 
259 #endif	/* !_LIBPROCSTAT_H_ */
260