1/*	$NetBSD: procfs.h,v 1.82.4.1 2024/04/18 18:22:10 martin Exp $	*/
  2
  3/*
  4 * Copyright (c) 1993
  5 *	The Regents of the University of California.  All rights reserved.
  6 *
  7 * This code is derived from software contributed to Berkeley by
  8 * Jan-Simon Pendry.
  9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 * 1. Redistributions of source code must retain the above copyright
 14 *    notice, this list of conditions and the following disclaimer.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 *    notice, this list of conditions and the following disclaimer in the
 17 *    documentation and/or other materials provided with the distribution.
 18 * 3. Neither the name of the University nor the names of its contributors
 19 *    may be used to endorse or promote products derived from this software
 20 *    without specific prior written permission.
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 32 * SUCH DAMAGE.
 33 *
 34 *	@(#)procfs.h	8.9 (Berkeley) 5/14/95
 35 */
 36
 37/*
 38 * Copyright (c) 1993 Jan-Simon Pendry
 39 *
 40 * This code is derived from software contributed to Berkeley by
 41 * Jan-Simon Pendry.
 42 *
 43 * Redistribution and use in source and binary forms, with or without
 44 * modification, are permitted provided that the following conditions
 45 * are met:
 46 * 1. Redistributions of source code must retain the above copyright
 47 *    notice, this list of conditions and the following disclaimer.
 48 * 2. Redistributions in binary form must reproduce the above copyright
 49 *    notice, this list of conditions and the following disclaimer in the
 50 *    documentation and/or other materials provided with the distribution.
 51 * 3. All advertising materials mentioning features or use of this software
 52 *    must display the following acknowledgement:
 53 *	This product includes software developed by the University of
 54 *	California, Berkeley and its contributors.
 55 * 4. Neither the name of the University nor the names of its contributors
 56 *    may be used to endorse or promote products derived from this software
 57 *    without specific prior written permission.
 58 *
 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 62 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 69 * SUCH DAMAGE.
 70 *
 71 *	@(#)procfs.h	8.9 (Berkeley) 5/14/95
 72 */
 73
 74/* This also pulls in __HAVE_PROCFS_MACHDEP */
 75#include <sys/ptrace.h>
 76
 77#ifdef _KERNEL
 78#include <sys/proc.h>
 79
 80/*
 81 * The different types of node in a procfs filesystem
 82 */
 83typedef enum {
 84	PFSauxv,	/* ELF Auxiliary Vector */
 85	PFSchroot,	/* the process's current root directory */
 86	PFScmdline,	/* process command line args */
 87	PFScpuinfo,	/* CPU info (if -o linux) */
 88	PFScpustat,	/* status info (if -o linux) */
 89	PFScurproc,	/* symbolic link for curproc */
 90	PFScwd,		/* the process's current working directory */
 91	PFSdevices,	/* major/device name mappings (if -o linux) */
 92	PFSemul,	/* the process's emulation */
 93	PFSenviron,	/* process environment */
 94	PFSexe,		/* symlink to the executable file */
 95	PFSfd,		/* a directory containing the processes open fd's */
 96	PFSfile,	/* the executable file */
 97	PFSfpregs,	/* the process's FP register set */
 98	PFSloadavg,	/* load average (if -o linux) */
 99	PFSlimit,	/* resource limits */
100	PFSmap,		/* memory map */
101	PFSmaps,	/* memory map, Linux style (if -o linux) */
102	PFSmem,		/* the process's memory image */
103	PFSmeminfo,	/* system memory info (if -o linux) */
104	PFSmounts,	/* mounted filesystems (if -o linux) */
105	PFSnote,	/* process notifier */
106	PFSnotepg,	/* process group notifier */
107	PFSproc,	/* a process-specific sub-directory */
108	PFSregs,	/* the process's register set */
109	PFSroot,	/* the filesystem root */
110	PFSself,	/* like curproc, but this is the Linux name */
111	PFSstat,	/* process status (if -o linux) */
112	PFSstatm,	/* process memory info (if -o linux) */
113	PFSstatus,	/* process status */
114	PFStask,	/* task subdirector (if -o linux) */
115	PFSuptime,	/* elapsed time since (if -o linux) */
116	PFSversion,	/* kernel version (if -o linux) */
117#ifdef __HAVE_PROCFS_MACHDEP
118	PROCFS_MACHDEP_NODE_TYPES
119#endif
120	PFSlast,	/* track number of types */
121} pfstype;
122
123/*
124 * control data for the proc file system.
125 */
126struct pfskey {
127	pfstype		pk_type;	/* type of procfs node */
128	pid_t		pk_pid;		/* associated process */
129	int		pk_fd;		/* associated fd if not -1 */
130};
131struct pfsnode {
132	LIST_ENTRY(pfsnode) pfs_hash;	/* per pid hash list */
133	struct vnode	*pfs_vnode;	/* vnode associated with this pfsnode */
134	struct mount	*pfs_mount;	/* mount associated with this pfsnode */
135	struct pfskey	pfs_key;
136#define pfs_type pfs_key.pk_type
137#define pfs_pid pfs_key.pk_pid
138#define pfs_fd pfs_key.pk_fd
139	mode_t		pfs_mode;	/* mode bits for stat() */
140	u_long		pfs_flags;	/* open flags */
141	uint64_t	pfs_fileno;	/* unique file id */
142};
143
144#define PROCFS_NOTELEN	64	/* max length of a note (/proc/$pid/note) */
145#define PROCFS_MAXNAMLEN	255
146
147#endif /* _KERNEL */
148
149struct procfs_args {
150	int version;
151	int flags;
152};
153
154#define PROCFS_ARGSVERSION	1
155
156#define PROCFSMNT_LINUXCOMPAT	0x01
157
158#define PROCFSMNT_BITS "\177\20" \
159    "b\00linuxcompat\0"
160
161/*
162 * Kernel stuff follows
163 */
164#ifdef _KERNEL
165#define CNEQ(cnp, s, len) \
166	 ((cnp)->cn_namelen == (len) && \
167	  (memcmp((s), (cnp)->cn_nameptr, (len)) == 0))
168
169#define UIO_MX 32
170
171static __inline ino_t
172procfs_fileno(pid_t _pid, pfstype _type, int _fd)
173{
174	ino_t _ino;
175	switch (_type) {
176	case PFSroot:
177		return 2;
178	case PFScurproc:
179		return 3;
180	case PFSself:
181		return 4;
182	default:
183		_ino = _pid + 1;
184		if (_fd != -1)
185			_ino = _ino << 32 | _fd;
186		return _ino * PFSlast + _type;
187	}
188}
189
190#define PROCFS_FILENO(pid, type, fd) procfs_fileno(pid, type, fd)
191
192#define PROCFS_TYPE(type)	((type) % PFSlast)
193
194struct procfsmount {
195	int pmnt_flags;
196};
197
198#define VFSTOPROC(mp)	((struct procfsmount *)(mp)->mnt_data)
199
200/*
201 * Convert between pfsnode vnode
202 */
203#define VTOPFS(vp)	((struct pfsnode *)(vp)->v_data)
204#define PFSTOV(pfs)	((pfs)->pfs_vnode)
205
206typedef struct vfs_namemap vfs_namemap_t;
207struct vfs_namemap {
208	const char *nm_name;
209	int nm_val;
210};
211
212int vfs_getuserstr(struct uio *, char *, int *);
213const vfs_namemap_t *vfs_findname(const vfs_namemap_t *, const char *, int);
214
215struct mount;
216
217struct proc *procfs_proc_find(struct mount *, pid_t);
218bool procfs_use_linux_compat(struct mount *);
219
220static inline bool
221procfs_proc_is_linux_compat(void)
222{
223	const char *emulname = curlwp->l_proc->p_emul->e_name;
224	return (strncmp(emulname, "linux", 5) == 0);
225}
226
227int procfs_proc_lock(struct mount *, int, struct proc **, int);
228void procfs_proc_unlock(struct proc *);
229int procfs_allocvp(struct mount *, struct vnode **, pid_t, pfstype, int);
230int procfs_donote(struct lwp *, struct proc *, struct pfsnode *,
231    struct uio *);
232int procfs_doregs(struct lwp *, struct lwp *, struct pfsnode *,
233    struct uio *);
234int procfs_dofpregs(struct lwp *, struct lwp *, struct pfsnode *,
235    struct uio *);
236int procfs_domem(struct lwp *, struct lwp *, struct pfsnode *,
237    struct uio *);
238int procfs_do_pid_stat(struct lwp *, struct lwp *, struct pfsnode *,
239    struct uio *);
240int procfs_dostatus(struct lwp *, struct lwp *, struct pfsnode *,
241    struct uio *);
242int procfs_domap(struct lwp *, struct proc *, struct pfsnode *,
243    struct uio *, int);
244int procfs_doprocargs(struct lwp *, struct proc *, struct pfsnode *,
245    struct uio *, int);
246int procfs_domeminfo(struct lwp *, struct proc *, struct pfsnode *,
247    struct uio *);
248int procfs_dodevices(struct lwp *, struct proc *, struct pfsnode *,
249    struct uio *);
250int procfs_docpuinfo(struct lwp *, struct proc *, struct pfsnode *,
251    struct uio *);
252int procfs_docpustat(struct lwp *, struct proc *, struct pfsnode *,
253    struct uio *);
254int procfs_doloadavg(struct lwp *, struct proc *, struct pfsnode *,
255    struct uio *);
256int procfs_do_pid_statm(struct lwp *, struct lwp *, struct pfsnode *,
257    struct uio *);
258int procfs_dofd(struct lwp *, struct proc *, struct pfsnode *,
259    struct uio *);
260int procfs_douptime(struct lwp *, struct proc *, struct pfsnode *,
261    struct uio *);
262int procfs_domounts(struct lwp *, struct proc *, struct pfsnode *,
263    struct uio *);
264int procfs_doemul(struct lwp *, struct proc *, struct pfsnode *,
265    struct uio *);
266int procfs_doversion(struct lwp *, struct proc *, struct pfsnode *,
267    struct uio *);
268int procfs_doauxv(struct lwp *, struct proc *, struct pfsnode *,
269    struct uio *);
270int procfs_dolimit(struct lwp *, struct proc *, struct pfsnode *,
271    struct uio *);
272
273void procfs_hashrem(struct pfsnode *);
274int procfs_getfp(struct pfsnode *, struct proc *, struct file **);
275
276/* functions to check whether or not files should be displayed */
277int procfs_validauxv(struct lwp *, struct mount *);
278int procfs_validfile(struct lwp *, struct mount *);
279int procfs_validfpregs(struct lwp *, struct mount *);
280int procfs_validregs(struct lwp *, struct mount *);
281int procfs_validmap(struct lwp *, struct mount *);
282
283int procfs_rw(void *);
284
285int procfs_getcpuinfstr(char *, size_t *);
286
287#define PROCFS_LOCKED	0x01
288#define PROCFS_WANT	0x02
289
290extern int (**procfs_vnodeop_p)(void *);
291extern struct vfsops procfs_vfsops;
292
293int	procfs_root(struct mount *, int, struct vnode **);
294
295#ifdef __HAVE_PROCFS_MACHDEP
296struct vattr;
297
298void	procfs_machdep_allocvp(struct vnode *);
299int	procfs_machdep_rw(struct lwp *, struct lwp *, struct pfsnode *,
300	    struct uio *);
301int	procfs_machdep_getattr(struct vnode *, struct vattr *, struct proc *);
302#endif
303
304#endif /* _KERNEL */