xref: /freebsd/lib/libprocstat/libprocstat.h (revision 47606b869eb149ebb7135d6594c3b9b9f05b9aed)
1 /*-
2  * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
3  * Copyright (c) 2017 Dell EMC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
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 #define	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 
72 /*
73  * Special descriptor numbers.
74  */
75 #define	PS_FST_UFLAG_RDIR	0x0001
76 #define	PS_FST_UFLAG_CDIR	0x0002
77 #define	PS_FST_UFLAG_JAIL	0x0004
78 #define	PS_FST_UFLAG_TRACE	0x0008
79 #define	PS_FST_UFLAG_TEXT	0x0010
80 #define	PS_FST_UFLAG_MMAP	0x0020
81 #define	PS_FST_UFLAG_CTTY	0x0040
82 
83 /*
84  * Descriptor flags.
85  */
86 #define PS_FST_FFLAG_READ	0x0001
87 #define PS_FST_FFLAG_WRITE	0x0002
88 #define	PS_FST_FFLAG_NONBLOCK	0x0004
89 #define	PS_FST_FFLAG_APPEND	0x0008
90 #define	PS_FST_FFLAG_SHLOCK	0x0010
91 #define	PS_FST_FFLAG_EXLOCK	0x0020
92 #define	PS_FST_FFLAG_ASYNC	0x0040
93 #define	PS_FST_FFLAG_SYNC	0x0080
94 #define	PS_FST_FFLAG_NOFOLLOW	0x0100
95 #define	PS_FST_FFLAG_CREAT	0x0200
96 #define	PS_FST_FFLAG_TRUNC	0x0400
97 #define	PS_FST_FFLAG_EXCL	0x0800
98 #define	PS_FST_FFLAG_DIRECT	0x1000
99 #define	PS_FST_FFLAG_EXEC	0x2000
100 #define	PS_FST_FFLAG_HASLOCK	0x4000
101 
102 struct kinfo_kstack;
103 struct kinfo_vmentry;
104 struct procstat;
105 struct ptrace_lwpinfo;
106 struct rlimit;
107 struct filestat {
108 	int	fs_type;	/* Descriptor type. */
109 	int	fs_flags;	/* filestat specific flags. */
110 	int	fs_fflags;	/* Descriptor access flags. */
111 	int	fs_uflags;	/* How this file is used. */
112 	int	fs_fd;		/* File descriptor number. */
113 	int	fs_ref_count;	/* Reference count. */
114 	off_t	fs_offset;	/* Seek location. */
115 	void	*fs_typedep;	/* Type dependent data. */
116 	char	*fs_path;
117 	STAILQ_ENTRY(filestat)	next;
118 	cap_rights_t	fs_cap_rights;	/* Capability rights, if flag set. */
119 };
120 struct vnstat {
121 	uint64_t	vn_fileid;
122 	uint64_t	vn_size;
123 	uint64_t	vn_dev;
124 	uint64_t	vn_fsid;
125 	char		*vn_mntdir;
126 	int		vn_type;
127 	uint16_t	vn_mode;
128 	char		vn_devname[SPECNAMELEN + 1];
129 };
130 struct ptsstat {
131 	uint64_t	dev;
132 	char		devname[SPECNAMELEN + 1];
133 };
134 struct pipestat {
135 	size_t		buffer_cnt;
136 	uint64_t	addr;
137 	uint64_t	peer;
138 };
139 struct semstat {
140 	uint32_t	value;
141 	uint16_t	mode;
142 };
143 struct shmstat {
144 	uint64_t	size;
145 	uint16_t	mode;
146 };
147 struct sockstat {
148 	uint64_t	inp_ppcb;
149 	uint64_t	so_addr;
150 	uint64_t	so_pcb;
151 	uint64_t	unp_conn;
152 	int		dom_family;
153 	int		proto;
154 	int		so_rcv_sb_state;
155 	int		so_snd_sb_state;
156 	struct sockaddr_storage	sa_local;	/* Socket address. */
157 	struct sockaddr_storage	sa_peer;	/* Peer address. */
158 	int		type;
159 	char		dname[32];
160 	unsigned int	sendq;
161 	unsigned int	recvq;
162 };
163 
164 STAILQ_HEAD(filestat_list, filestat);
165 
166 __BEGIN_DECLS
167 void	procstat_close(struct procstat *procstat);
168 void	procstat_freeargv(struct procstat *procstat);
169 #ifndef ZFS
170 void	procstat_freeauxv(struct procstat *procstat, Elf_Auxinfo *auxv);
171 #endif
172 void	procstat_freeenvv(struct procstat *procstat);
173 void	procstat_freegroups(struct procstat *procstat, gid_t *groups);
174 void	procstat_freekstack(struct procstat *procstat,
175     struct kinfo_kstack *kkstp);
176 void	procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
177 void	procstat_freefiles(struct procstat *procstat,
178     struct filestat_list *head);
179 void	procstat_freeptlwpinfo(struct procstat *procstat,
180     struct ptrace_lwpinfo *pl);
181 void	procstat_freevmmap(struct procstat *procstat,
182     struct kinfo_vmentry *vmmap);
183 struct filestat_list	*procstat_getfiles(struct procstat *procstat,
184     struct kinfo_proc *kp, int mmapped);
185 struct kinfo_proc	*procstat_getprocs(struct procstat *procstat,
186     int what, int arg, unsigned int *count);
187 int	procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
188     struct pipestat *pipe, char *errbuf);
189 int	procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
190     struct ptsstat *pts, char *errbuf);
191 int	procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
192     struct semstat *sem, char *errbuf);
193 int	procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
194     struct shmstat *shm, char *errbuf);
195 int	procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
196     struct sockstat *sock, char *errbuf);
197 int	procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
198     struct vnstat *vn, char *errbuf);
199 char	**procstat_getargv(struct procstat *procstat, struct kinfo_proc *p,
200     size_t nchr);
201 #ifndef ZFS
202 Elf_Auxinfo	*procstat_getauxv(struct procstat *procstat,
203     struct kinfo_proc *kp, unsigned int *cntp);
204 #endif
205 struct ptrace_lwpinfo	*procstat_getptlwpinfo(struct procstat *procstat,
206     unsigned int *cntp);
207 char	**procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p,
208     size_t nchr);
209 gid_t	*procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
210     unsigned int *count);
211 struct kinfo_kstack	*procstat_getkstack(struct procstat *procstat,
212     struct kinfo_proc *kp, unsigned int *count);
213 int	procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp,
214     int *osrelp);
215 int	procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
216     char *pathname, size_t maxlen);
217 int	procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
218     int which, struct rlimit* rlimit);
219 int	procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
220     unsigned short* umask);
221 struct kinfo_vmentry	*procstat_getvmmap(struct procstat *procstat,
222     struct kinfo_proc *kp, unsigned int *count);
223 struct procstat	*procstat_open_core(const char *filename);
224 struct procstat	*procstat_open_sysctl(void);
225 struct procstat	*procstat_open_kvm(const char *nlistf, const char *memf);
226 __END_DECLS
227 
228 #endif	/* !_LIBPROCSTAT_H_ */
229