aboutsummaryrefslogtreecommitdiff
path: root/lib/libprocstat/libprocstat.h
blob: 3d30b4db4018a773d1cdfa3d4307f764a9839cf7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
 * Copyright (c) 2017 Dell EMC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _LIBPROCSTAT_H_
#define	_LIBPROCSTAT_H_

/*
 * XXX: sys/elf.h conflicts with zfs_context.h. Workaround this by not
 * including conflicting parts when building zfs code.
 */
#ifndef ZFS
#include <sys/elf.h>
#endif
#include <sys/caprights.h>

/*
 * Vnode types.
 */
#define	PS_FST_VTYPE_VNON	1
#define	PS_FST_VTYPE_VREG	2
#define	PS_FST_VTYPE_VDIR	3
#define	PS_FST_VTYPE_VBLK	4
#define	PS_FST_VTYPE_VCHR	5
#define	PS_FST_VTYPE_VLNK	6
#define	PS_FST_VTYPE_VSOCK	7
#define	PS_FST_VTYPE_VFIFO	8
#define	PS_FST_VTYPE_VBAD	9
#define	PS_FST_VTYPE_UNKNOWN	255

/*
 * Descriptor types.
 */
#define	PS_FST_TYPE_VNODE	1
#define	PS_FST_TYPE_FIFO	2
#define	PS_FST_TYPE_SOCKET	3
#define	PS_FST_TYPE_PIPE	4
#define	PS_FST_TYPE_PTS		5
#define	PS_FST_TYPE_KQUEUE	6
/* was PS_FST_TYPE_CRYPTO	7 */
#define	PS_FST_TYPE_MQUEUE	8
#define	PS_FST_TYPE_SHM		9
#define	PS_FST_TYPE_SEM		10
#define	PS_FST_TYPE_UNKNOWN	11
#define	PS_FST_TYPE_NONE	12
#define	PS_FST_TYPE_PROCDESC	13
#define	PS_FST_TYPE_DEV		14
#define	PS_FST_TYPE_EVENTFD	15

/*
 * Special descriptor numbers.
 */
#define	PS_FST_UFLAG_RDIR	0x0001
#define	PS_FST_UFLAG_CDIR	0x0002
#define	PS_FST_UFLAG_JAIL	0x0004
#define	PS_FST_UFLAG_TRACE	0x0008
#define	PS_FST_UFLAG_TEXT	0x0010
#define	PS_FST_UFLAG_MMAP	0x0020
#define	PS_FST_UFLAG_CTTY	0x0040

/*
 * Descriptor flags.
 */
#define PS_FST_FFLAG_READ	0x0001
#define PS_FST_FFLAG_WRITE	0x0002
#define	PS_FST_FFLAG_NONBLOCK	0x0004
#define	PS_FST_FFLAG_APPEND	0x0008
#define	PS_FST_FFLAG_SHLOCK	0x0010
#define	PS_FST_FFLAG_EXLOCK	0x0020
#define	PS_FST_FFLAG_ASYNC	0x0040
#define	PS_FST_FFLAG_SYNC	0x0080
#define	PS_FST_FFLAG_NOFOLLOW	0x0100
#define	PS_FST_FFLAG_CREAT	0x0200
#define	PS_FST_FFLAG_TRUNC	0x0400
#define	PS_FST_FFLAG_EXCL	0x0800
#define	PS_FST_FFLAG_DIRECT	0x1000
#define	PS_FST_FFLAG_EXEC	0x2000
#define	PS_FST_FFLAG_HASLOCK	0x4000

#if !defined(__ILP32__) && !defined(__riscv)
/* Target architecture supports 32-bit compat */
#define	PS_ARCH_HAS_FREEBSD32	1
#endif

struct kinfo_kstack;
struct kinfo_proc;
struct kinfo_vmentry;
struct procstat;
struct ptrace_lwpinfo;
struct rlimit;
struct filestat {
	int	fs_type;	/* Descriptor type. */
	int	fs_flags;	/* filestat specific flags. */
	int	fs_fflags;	/* Descriptor access flags. */
	int	fs_uflags;	/* How this file is used. */
	int	fs_fd;		/* File descriptor number. */
	int	fs_ref_count;	/* Reference count. */
	off_t	fs_offset;	/* Seek location. */
	void	*fs_typedep;	/* Type dependent data. */
	char	*fs_path;
	STAILQ_ENTRY(filestat)	next;
	cap_rights_t	fs_cap_rights;	/* Capability rights, if flag set. */
};
struct vnstat {
	uint64_t	vn_fileid;
	uint64_t	vn_size;
	uint64_t	vn_dev;
	uint64_t	vn_fsid;
	char		*vn_mntdir;
	int		vn_type;
	uint16_t	vn_mode;
	char		vn_devname[SPECNAMELEN + 1];
};
struct ptsstat {
	uint64_t	dev;
	char		devname[SPECNAMELEN + 1];
};
struct pipestat {
	size_t		buffer_cnt;
	uint64_t	addr;
	uint64_t	peer;
};
struct semstat {
	uint32_t	value;
	uint16_t	mode;
};
struct shmstat {
	uint64_t	size;
	uint16_t	mode;
};
struct sockstat {
	uint64_t	inp_ppcb;
	uint64_t	so_addr;
	uint64_t	so_pcb;
	uint64_t	unp_conn;
	int		dom_family;
	int		proto;
	int		so_rcv_sb_state;
	int		so_snd_sb_state;
	struct sockaddr_storage	sa_local;	/* Socket address. */
	struct sockaddr_storage	sa_peer;	/* Peer address. */
	int		type;
	char		dname[32];
	unsigned int	sendq;
	unsigned int	recvq;
};

STAILQ_HEAD(filestat_list, filestat);

struct advlock {
	int		rw;			/* PS_ADVLOCK_RO/RW */
	int		type;			/* PS_ADVLOCK_TYPE_ */
	int		pid;
	int		sysid;
	uint64_t	file_fsid;
	uint64_t	file_rdev;
	uint64_t	file_fileid;
	off_t		start;
	off_t		len;			/* len == 0 till the EOF */
	const char	*path;
	STAILQ_ENTRY(advlock)	next;
};

#define	PS_ADVLOCK_RO		0x01
#define	PS_ADVLOCK_RW		0x02

#define	PS_ADVLOCK_TYPE_FLOCK	0x01
#define	PS_ADVLOCK_TYPE_PID	0x02
#define	PS_ADVLOCK_TYPE_REMOTE	0x03

STAILQ_HEAD(advlock_list, advlock);

__BEGIN_DECLS
void	procstat_close(struct procstat *procstat);
void	procstat_freeadvlock(struct procstat *procstat,
    struct advlock_list *advlocks);
void	procstat_freeargv(struct procstat *procstat);
#ifndef ZFS
void	procstat_freeauxv(struct procstat *procstat, Elf_Auxinfo *auxv);
#endif
void	procstat_freeenvv(struct procstat *procstat);
void	procstat_freegroups(struct procstat *procstat, gid_t *groups);
void	procstat_freekstack(struct procstat *procstat,
    struct kinfo_kstack *kkstp);
void	procstat_freeprocs(struct procstat *procstat, struct kinfo_proc *p);
void	procstat_freefiles(struct procstat *procstat,
    struct filestat_list *head);
void	procstat_freeptlwpinfo(struct procstat *procstat,
    struct ptrace_lwpinfo *pl);
void	procstat_freevmmap(struct procstat *procstat,
    struct kinfo_vmentry *vmmap);
struct advlock_list	*procstat_getadvlock(struct procstat *procstat);
struct filestat_list	*procstat_getfiles(struct procstat *procstat,
    struct kinfo_proc *kp, int mmapped);
struct kinfo_proc	*procstat_getprocs(struct procstat *procstat,
    int what, int arg, unsigned int *count);
int	procstat_get_pipe_info(struct procstat *procstat, struct filestat *fst,
    struct pipestat *pipe, char *errbuf);
int	procstat_get_pts_info(struct procstat *procstat, struct filestat *fst,
    struct ptsstat *pts, char *errbuf);
int	procstat_get_sem_info(struct procstat *procstat, struct filestat *fst,
    struct semstat *sem, char *errbuf);
int	procstat_get_shm_info(struct procstat *procstat, struct filestat *fst,
    struct shmstat *shm, char *errbuf);
int	procstat_get_socket_info(struct procstat *procstat, struct filestat *fst,
    struct sockstat *sock, char *errbuf);
int	procstat_get_vnode_info(struct procstat *procstat, struct filestat *fst,
    struct vnstat *vn, char *errbuf);
char	**procstat_getargv(struct procstat *procstat, struct kinfo_proc *p,
    size_t nchr);
#ifndef ZFS
Elf_Auxinfo	*procstat_getauxv(struct procstat *procstat,
    struct kinfo_proc *kp, unsigned int *cntp);
#endif
struct ptrace_lwpinfo	*procstat_getptlwpinfo(struct procstat *procstat,
    unsigned int *cntp);
char	**procstat_getenvv(struct procstat *procstat, struct kinfo_proc *p,
    size_t nchr);
gid_t	*procstat_getgroups(struct procstat *procstat, struct kinfo_proc *kp,
    unsigned int *count);
struct kinfo_kstack	*procstat_getkstack(struct procstat *procstat,
    struct kinfo_proc *kp, unsigned int *count);
int	procstat_getosrel(struct procstat *procstat, struct kinfo_proc *kp,
    int *osrelp);
int	procstat_getpathname(struct procstat *procstat, struct kinfo_proc *kp,
    char *pathname, size_t maxlen);
int	procstat_getrlimit(struct procstat *procstat, struct kinfo_proc *kp,
    int which, struct rlimit* rlimit);
int	procstat_getumask(struct procstat *procstat, struct kinfo_proc *kp,
    unsigned short* umask);
struct kinfo_vmentry	*procstat_getvmmap(struct procstat *procstat,
    struct kinfo_proc *kp, unsigned int *count);
struct procstat	*procstat_open_core(const char *filename);
struct procstat	*procstat_open_sysctl(void);
struct procstat	*procstat_open_kvm(const char *nlistf, const char *memf);
__END_DECLS

#endif	/* !_LIBPROCSTAT_H_ */