1 /* $NetBSD: procfs.h,v 1.87 2024/07/01 01:35:53 christos 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 */
83 typedef 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 PFSlimits, /* resource limits, Linux style (if -o linux) */
101 PFSmap, /* memory map */
102 PFSmaps, /* memory map, Linux style (if -o linux) */
103 PFSmem, /* the process's memory image */
104 PFSmeminfo, /* system memory info (if -o linux) */
105 PFSmounts, /* mounted filesystems (if -o linux) */
106 PFSmqueue, /* sys/fs/mqueue subdirectory (if -o linux) */
107 PFSmq_msg_def, /* sys/fs/mqueue/msg_default (if -o linux) */
108 PFSmq_msg_max, /* sys/fs/mqueue/msg_max (if -o linux) */
109 PFSmq_siz_def, /* sys/fs/mqueue/msgsize_default (if -o linux) */
110 PFSmq_siz_max, /* sys/fs/mqueue/msgsize_max (if -o linux) */
111 PFSmq_qmax, /* sys/fs/mqueue/queues_max (if -o linux) */
112 PFSnote, /* process notifier */
113 PFSnotepg, /* process group notifier */
114 PFSproc, /* a process-specific sub-directory */
115 PFSregs, /* the process's register set */
116 PFSroot, /* the filesystem root */
117 PFSself, /* like curproc, but this is the Linux name */
118 PFSstat, /* process status (if -o linux) */
119 PFSstatm, /* process memory info (if -o linux) */
120 PFSstatus, /* process status */
121 PFSsys, /* sys subdirectory (if -o linux) */
122 PFSsysfs, /* sys/fs subdirectory (if -o linux) */
123 PFSsysvipc, /* sysvipc subdirectory (if -o linux) */
124 PFSsysvipc_msg, /* sysvipc msg info (if -o linux) */
125 PFSsysvipc_sem, /* sysvipc sem info (if -o linux) */
126 PFSsysvipc_shm, /* sysvipc shm info (if -o linux) */
127 PFStask, /* task subdirectory (if -o linux) */
128 PFSuptime, /* elapsed time since (if -o linux) */
129 PFSversion, /* kernel version (if -o linux) */
130 #ifdef __HAVE_PROCFS_MACHDEP
131 PROCFS_MACHDEP_NODE_TYPES
132 #endif
133 PFSlast, /* track number of types */
134 } pfstype;
135
136 /*
137 * control data for the proc file system.
138 */
139 struct pfskey {
140 pfstype pk_type; /* type of procfs node */
141 pid_t pk_pid; /* associated process */
142 int pk_fd; /* associated fd if not -1 */
143 };
144 struct pfsnode {
145 LIST_ENTRY(pfsnode) pfs_hash; /* per pid hash list */
146 struct vnode *pfs_vnode; /* vnode associated with this pfsnode */
147 struct mount *pfs_mount; /* mount associated with this pfsnode */
148 struct pfskey pfs_key;
149 #define pfs_type pfs_key.pk_type
150 #define pfs_pid pfs_key.pk_pid
151 #define pfs_fd pfs_key.pk_fd
152 mode_t pfs_mode; /* mode bits for stat() */
153 u_long pfs_flags; /* open flags */
154 uint64_t pfs_fileno; /* unique file id */
155 };
156
157 #define PROCFS_NOTELEN 64 /* max length of a note (/proc/$pid/note) */
158 #define PROCFS_MAXNAMLEN 255
159
160 #endif /* _KERNEL */
161
162 struct procfs_args {
163 int version;
164 int flags;
165 };
166
167 #define PROCFS_ARGSVERSION 1
168
169 #define PROCFSMNT_LINUXCOMPAT 0x01
170
171 #define PROCFSMNT_BITS "\177\20" \
172 "b\00linuxcompat\0"
173
174 /*
175 * Kernel stuff follows
176 */
177 #ifdef _KERNEL
178 #define CNEQ(cnp, s, len) \
179 ((cnp)->cn_namelen == (len) && \
180 (memcmp((s), (cnp)->cn_nameptr, (len)) == 0))
181
182 #define UIO_MX 32
183
184 static __inline ino_t
procfs_fileno(pid_t _pid,pfstype _type,int _fd)185 procfs_fileno(pid_t _pid, pfstype _type, int _fd)
186 {
187 ino_t _ino;
188 switch (_type) {
189 case PFSroot:
190 return 2;
191 case PFScurproc:
192 return 3;
193 case PFSself:
194 return 4;
195 default:
196 _ino = _pid + 1;
197 if (_fd != -1)
198 _ino = _ino << 32 | _fd;
199 return _ino * PFSlast + _type;
200 }
201 }
202
203 #define PROCFS_FILENO(pid, type, fd) procfs_fileno(pid, type, fd)
204
205 #define PROCFS_TYPE(type) ((type) % PFSlast)
206
207 struct procfsmount {
208 int pmnt_flags;
209 };
210
211 #define VFSTOPROC(mp) ((struct procfsmount *)(mp)->mnt_data)
212
213 /*
214 * Convert between pfsnode vnode
215 */
216 #define VTOPFS(vp) ((struct pfsnode *)(vp)->v_data)
217 #define PFSTOV(pfs) ((pfs)->pfs_vnode)
218
219 typedef struct vfs_namemap vfs_namemap_t;
220 struct vfs_namemap {
221 const char *nm_name;
222 int nm_val;
223 };
224
225 int vfs_getuserstr(struct uio *, char *, int *);
226 const vfs_namemap_t *vfs_findname(const vfs_namemap_t *, const char *, int);
227
228 struct mount;
229
230 struct proc *procfs_proc_find(struct mount *, pid_t);
231 bool procfs_use_linux_compat(struct mount *);
232
233 static inline bool
procfs_proc_is_linux_compat(void)234 procfs_proc_is_linux_compat(void)
235 {
236 const char *emulname = curlwp->l_proc->p_emul->e_name;
237 return (strncmp(emulname, "linux", 5) == 0);
238 }
239
240 int procfs_proc_lock(struct mount *, int, struct proc **, int);
241 void procfs_proc_unlock(struct proc *);
242 int procfs_allocvp(struct mount *, struct vnode **, pid_t, pfstype, int);
243 int procfs_donote(struct lwp *, struct proc *, struct pfsnode *,
244 struct uio *);
245 int procfs_doregs(struct lwp *, struct lwp *, struct pfsnode *,
246 struct uio *);
247 int procfs_dofpregs(struct lwp *, struct lwp *, struct pfsnode *,
248 struct uio *);
249 int procfs_domem(struct lwp *, struct lwp *, struct pfsnode *,
250 struct uio *);
251 int procfs_do_pid_stat(struct lwp *, struct lwp *, struct pfsnode *,
252 struct uio *);
253 int procfs_dostatus(struct lwp *, struct lwp *, struct pfsnode *,
254 struct uio *);
255 int procfs_domap(struct lwp *, struct proc *, struct pfsnode *,
256 struct uio *, int);
257 int procfs_doprocargs(struct lwp *, struct proc *, struct pfsnode *,
258 struct uio *, int);
259 int procfs_domeminfo(struct lwp *, struct proc *, struct pfsnode *,
260 struct uio *);
261 int procfs_dodevices(struct lwp *, struct proc *, struct pfsnode *,
262 struct uio *);
263 int procfs_docpuinfo(struct lwp *, struct proc *, struct pfsnode *,
264 struct uio *);
265 int procfs_docpustat(struct lwp *, struct proc *, struct pfsnode *,
266 struct uio *);
267 int procfs_doloadavg(struct lwp *, struct proc *, struct pfsnode *,
268 struct uio *);
269 int procfs_do_pid_statm(struct lwp *, struct lwp *, struct pfsnode *,
270 struct uio *);
271 int procfs_dofd(struct lwp *, struct proc *, struct pfsnode *,
272 struct uio *);
273 int procfs_douptime(struct lwp *, struct proc *, struct pfsnode *,
274 struct uio *);
275 int procfs_domounts(struct lwp *, struct proc *, struct pfsnode *,
276 struct uio *);
277 int procfs_doemul(struct lwp *, struct proc *, struct pfsnode *,
278 struct uio *);
279 int procfs_doversion(struct lwp *, struct proc *, struct pfsnode *,
280 struct uio *);
281 int procfs_doauxv(struct lwp *, struct proc *, struct pfsnode *,
282 struct uio *);
283 int procfs_dolimit(struct lwp *, struct proc *, struct pfsnode *,
284 struct uio *);
285 int procfs_dolimits(struct lwp *, struct proc *, struct pfsnode *,
286 struct uio *);
287 int procfs_domq_msg_def(struct lwp *, struct proc *, struct pfsnode *,
288 struct uio *);
289 int procfs_domq_msg_max(struct lwp *, struct proc *, struct pfsnode *,
290 struct uio *);
291 int procfs_domq_siz_def(struct lwp *, struct proc *, struct pfsnode *,
292 struct uio *);
293 int procfs_domq_siz_max(struct lwp *, struct proc *, struct pfsnode *,
294 struct uio *);
295 int procfs_domq_qmax(struct lwp *, struct proc *, struct pfsnode *,
296 struct uio *);
297 int procfs_dosysvipc_msg(struct lwp *, struct proc *, struct pfsnode *,
298 struct uio *);
299 int procfs_dosysvipc_sem(struct lwp *, struct proc *, struct pfsnode *,
300 struct uio *);
301 int procfs_dosysvipc_shm(struct lwp *, struct proc *, struct pfsnode *,
302 struct uio *);
303
304 void procfs_hashrem(struct pfsnode *);
305 int procfs_getfp(struct pfsnode *, struct proc *, struct file **);
306
307 /* functions to check whether or not files should be displayed */
308 int procfs_validauxv(struct lwp *, struct mount *);
309 int procfs_validfile(struct lwp *, struct mount *);
310 int procfs_validfpregs(struct lwp *, struct mount *);
311 int procfs_validregs(struct lwp *, struct mount *);
312 int procfs_validmap(struct lwp *, struct mount *);
313
314 int procfs_rw(void *);
315
316 int procfs_getcpuinfstr(char *, size_t *);
317
318 #define PROCFS_LOCKED 0x01
319 #define PROCFS_WANT 0x02
320
321 extern int (**procfs_vnodeop_p)(void *);
322 extern struct vfsops procfs_vfsops;
323
324 int procfs_root(struct mount *, int, struct vnode **);
325
326 #ifdef __HAVE_PROCFS_MACHDEP
327 struct vattr;
328
329 void procfs_machdep_allocvp(struct vnode *);
330 int procfs_machdep_rw(struct lwp *, struct lwp *, struct pfsnode *,
331 struct uio *);
332 int procfs_machdep_getattr(struct vnode *, struct vattr *, struct proc *);
333 #endif
334
335 #endif /* _KERNEL */
336