Kernel - Fix numerous procfs/ptrace issues
[dragonfly.git] / sys / vfs / procfs / procfs_subr.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1993 Jan-Simon Pendry
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
38 *
39 * $FreeBSD: src/sys/miscfs/procfs/procfs_subr.c,v 1.26.2.3 2002/02/18 21:28:04 des Exp $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/sysctl.h>
45#include <sys/proc.h>
46#include <sys/mount.h>
47#include <sys/vnode.h>
48#include <sys/malloc.h>
198c0ff2 49#include <sys/thread2.h>
984263bc 50
1f2de5d4 51#include <vfs/procfs/procfs.h>
984263bc 52
3446c007
MD
53#define PFS_HSIZE 256
54#define PFS_HMASK (PFS_HSIZE - 1)
55
56static struct pfsnode *pfshead[PFS_HSIZE];
984263bc
MD
57static int pfsvplock;
58
3446c007
MD
59#define PFSHASH(pid) &pfshead[(pid) & PFS_HMASK]
60
984263bc 61/*
3446c007
MD
62 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode
63 * will be referenced and exclusively locked.
984263bc 64 *
3446c007
MD
65 * The pid, pfs_type, and mount point uniquely identify a pfsnode.
66 * The mount point is needed because someone might mount this filesystem
984263bc
MD
67 * twice.
68 *
3446c007
MD
69 * All pfsnodes are maintained on a singly-linked list. new nodes are
70 * only allocated when they cannot be found on this list. entries on
71 * the list are removed when the vfs reclaim entry is called.
984263bc 72 *
3446c007
MD
73 * A single lock is kept for the entire list. this is needed because the
74 * getnewvnode() function can block waiting for a vnode to become free,
75 * in which case there may be more than one process trying to get the same
76 * vnode. this lock is only taken if we are going to call getnewvnode,
77 * since the kernel itself is single-threaded.
984263bc 78 *
3446c007
MD
79 * If an entry is found on the list, then call vget() to take a reference
80 * and obtain the lock. This will properly re-reference the vnode if it
81 * had gotten onto the free list.
984263bc
MD
82 */
83int
ac424f9b 84procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type)
984263bc 85{
984263bc
MD
86 struct pfsnode *pfs;
87 struct vnode *vp;
88 struct pfsnode **pp;
89 int error;
90
3446c007 91 pp = PFSHASH(pid);
984263bc 92loop:
3446c007
MD
93 for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
94 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type &&
95 PFSTOV(pfs)->v_mount == mp) {
96 vp = PFSTOV(pfs);
ccd6bbb9
MD
97 vhold_interlocked(vp);
98 if (vget(vp, LK_EXCLUSIVE)) {
99 vdrop(vp);
5fd012e0 100 goto loop;
ccd6bbb9 101 }
3446c007
MD
102
103 /*
104 * Make sure the vnode is still in the cache after
105 * getting the interlock to avoid racing a free.
106 */
107 for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
108 if (PFSTOV(pfs) == vp &&
109 pfs->pfs_pid == pid &&
110 pfs->pfs_type == pfs_type &&
111 PFSTOV(pfs)->v_mount == mp) {
112 break;
113 }
114 }
ccd6bbb9 115 vdrop(vp);
5fd012e0
MD
116 if (pfs == NULL || PFSTOV(pfs) != vp) {
117 vput(vp);
3446c007
MD
118 goto loop;
119
120 }
ccd6bbb9 121 KKASSERT(vp->v_data == pfs);
984263bc
MD
122 *vpp = vp;
123 return (0);
124 }
125 }
126
127 /*
128 * otherwise lock the vp list while we call getnewvnode
129 * since that can block.
130 */
131 if (pfsvplock & PROCFS_LOCKED) {
132 pfsvplock |= PROCFS_WANT;
377d4740 133 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
984263bc
MD
134 goto loop;
135 }
136 pfsvplock |= PROCFS_LOCKED;
137
138 /*
139 * Do the MALLOC before the getnewvnode since doing so afterward
140 * might cause a bogus v_data pointer to get dereferenced
141 * elsewhere if MALLOC should block.
5fd012e0
MD
142 *
143 * XXX this may not matter anymore since getnewvnode now returns
144 * a VX locked vnode.
984263bc 145 */
884717e1 146 pfs = kmalloc(sizeof(struct pfsnode), M_TEMP, M_WAITOK);
984263bc 147
6ddb7618 148 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
3446c007 149 if (error) {
efda3bd0 150 kfree(pfs, M_TEMP);
984263bc
MD
151 goto out;
152 }
153 vp = *vpp;
154
155 vp->v_data = pfs;
156
157 pfs->pfs_next = 0;
158 pfs->pfs_pid = (pid_t) pid;
159 pfs->pfs_type = pfs_type;
160 pfs->pfs_vnode = vp;
161 pfs->pfs_flags = 0;
162 pfs->pfs_lockowner = 0;
163 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
164
165 switch (pfs_type) {
166 case Proot: /* /proc = dr-xr-xr-x */
167 pfs->pfs_mode = (VREAD|VEXEC) |
168 (VREAD|VEXEC) >> 3 |
169 (VREAD|VEXEC) >> 6;
170 vp->v_type = VDIR;
171 vp->v_flag = VROOT;
172 break;
173
174 case Pcurproc: /* /proc/curproc = lr--r--r-- */
175 pfs->pfs_mode = (VREAD) |
176 (VREAD >> 3) |
177 (VREAD >> 6);
178 vp->v_type = VLNK;
179 break;
180
181 case Pproc:
182 pfs->pfs_mode = (VREAD|VEXEC) |
183 (VREAD|VEXEC) >> 3 |
184 (VREAD|VEXEC) >> 6;
185 vp->v_type = VDIR;
186 break;
187
188 case Pfile:
189 pfs->pfs_mode = (VREAD|VEXEC) |
190 (VREAD|VEXEC) >> 3 |
191 (VREAD|VEXEC) >> 6;
192 vp->v_type = VLNK;
193 break;
194
195 case Pmem:
196 pfs->pfs_mode = (VREAD|VWRITE);
197 vp->v_type = VREG;
198 break;
199
200 case Pregs:
201 case Pfpregs:
202 case Pdbregs:
203 pfs->pfs_mode = (VREAD|VWRITE);
204 vp->v_type = VREG;
205 break;
206
207 case Pctl:
208 case Pnote:
209 case Pnotepg:
210 pfs->pfs_mode = (VWRITE);
211 vp->v_type = VREG;
212 break;
213
214 case Ptype:
215 case Pmap:
216 case Pstatus:
217 case Pcmdline:
218 case Prlimit:
219 pfs->pfs_mode = (VREAD) |
220 (VREAD >> 3) |
221 (VREAD >> 6);
222 vp->v_type = VREG;
223 break;
224
225 default:
226 panic("procfs_allocvp");
227 }
228
229 /* add to procfs vnode list */
3446c007 230 pfs->pfs_next = *pp;
984263bc
MD
231 *pp = pfs;
232
233out:
234 pfsvplock &= ~PROCFS_LOCKED;
235
236 if (pfsvplock & PROCFS_WANT) {
237 pfsvplock &= ~PROCFS_WANT;
238 wakeup((caddr_t) &pfsvplock);
239 }
240
241 return (error);
242}
243
244int
ac424f9b 245procfs_freevp(struct vnode *vp)
984263bc
MD
246{
247 struct pfsnode **pfspp;
3446c007 248 struct pfsnode *pfs;
984263bc 249
3446c007
MD
250 pfs = VTOPFS(vp);
251 vp->v_data = NULL;
984263bc 252
3446c007
MD
253 pfspp = PFSHASH(pfs->pfs_pid);
254 while (*pfspp != pfs && *pfspp)
255 pfspp = &(*pfspp)->pfs_next;
256 KKASSERT(*pfspp);
257 *pfspp = pfs->pfs_next;
258 pfs->pfs_next = NULL;
ccd6bbb9 259 pfs->pfs_vnode = NULL;
efda3bd0 260 kfree(pfs, M_TEMP);
984263bc
MD
261 return (0);
262}
263
198c0ff2
AHJ
264/*
265 * Try to find the calling pid. Note that pfind()
266 * now references the proc structure to be returned
267 * and needs to be released later with PRELE().
268 */
269struct proc *
270pfs_pfind(pid_t pfs_pid)
271{
272 struct proc *p = NULL;
273
274 if (pfs_pid == 0) {
275 p = &proc0;
276 PHOLD(p);
277 } else {
278 p = pfind(pfs_pid);
279 }
5ed1af41
MD
280
281 /*
282 * Make sure the process is not in the middle of exiting (where
283 * a lot of its structural members may wind up being NULL). If it
284 * is we give up on it.
285 */
286 if (p) {
f44c73be 287 lwkt_gettoken(&p->p_token);
b2a6ad87
MD
288 if (p->p_flags & P_POSTEXIT) {
289 lwkt_reltoken(&p->p_token);
290 PRELE(p);
291 p = NULL;
292 }
293 }
294 return p;
295}
296
297struct proc *
298pfs_zpfind(pid_t pfs_pid)
299{
300 struct proc *p = NULL;
301
302 if (pfs_pid == 0) {
303 p = &proc0;
304 PHOLD(p);
305 } else {
306 p = zpfind(pfs_pid);
307 }
308
309 /*
310 * Make sure the process is not in the middle of exiting (where
311 * a lot of its structural members may wind up being NULL). If it
312 * is we give up on it.
313 */
314 if (p) {
315 lwkt_gettoken(&p->p_token);
316 if (p->p_flags & P_POSTEXIT) {
5ed1af41
MD
317 lwkt_reltoken(&p->p_token);
318 PRELE(p);
319 p = NULL;
320 }
321 }
198c0ff2
AHJ
322 return p;
323}
324
f44c73be
MD
325void
326pfs_pdone(struct proc *p)
327{
328 if (p) {
329 lwkt_reltoken(&p->p_token);
330 PRELE(p);
331 }
332}
333
984263bc 334int
dadab5e9 335procfs_rw(struct vop_read_args *ap)
984263bc
MD
336{
337 struct vnode *vp = ap->a_vp;
338 struct uio *uio = ap->a_uio;
dadab5e9
MD
339 struct thread *curtd = uio->uio_td;
340 struct proc *curp;
984263bc
MD
341 struct pfsnode *pfs = VTOPFS(vp);
342 struct proc *p;
c7e98b2f 343 struct lwp *lp;
984263bc
MD
344 int rtval;
345
dadab5e9
MD
346 if (curtd == NULL)
347 return (EINVAL);
348 if ((curp = curtd->td_proc) == NULL) /* XXX */
349 return (EINVAL);
350
98c2b8ac 351 lwkt_gettoken(&proc_token);
198c0ff2 352 p = pfs_pfind(pfs->pfs_pid);
98c2b8ac 353 if (p == NULL) {
198c0ff2
AHJ
354 rtval = (EINVAL);
355 goto out;
98c2b8ac
MD
356 }
357 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
198c0ff2
AHJ
358 rtval = (EACCES);
359 goto out;
98c2b8ac 360 }
c7e98b2f
SS
361 /* XXX lwp */
362 lp = FIRST_LWP_IN_PROC(p);
9d044741 363 LWPHOLD(lp);
984263bc
MD
364
365 while (pfs->pfs_lockowner) {
377d4740 366 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
984263bc
MD
367 }
368 pfs->pfs_lockowner = curproc->p_pid;
369
370 switch (pfs->pfs_type) {
371 case Pnote:
372 case Pnotepg:
c7e98b2f 373 rtval = procfs_donote(curp, lp, pfs, uio);
984263bc
MD
374 break;
375
376 case Pregs:
c7e98b2f 377 rtval = procfs_doregs(curp, lp, pfs, uio);
984263bc
MD
378 break;
379
380 case Pfpregs:
c7e98b2f 381 rtval = procfs_dofpregs(curp, lp, pfs, uio);
984263bc
MD
382 break;
383
384 case Pdbregs:
c7e98b2f 385 rtval = procfs_dodbregs(curp, lp, pfs, uio);
984263bc
MD
386 break;
387
388 case Pctl:
c7e98b2f 389 rtval = procfs_doctl(curp, lp, pfs, uio);
984263bc
MD
390 break;
391
392 case Pstatus:
c7e98b2f 393 rtval = procfs_dostatus(curp, lp, pfs, uio);
984263bc
MD
394 break;
395
396 case Pmap:
c7e98b2f 397 rtval = procfs_domap(curp, lp, pfs, uio);
984263bc
MD
398 break;
399
400 case Pmem:
c7e98b2f 401 rtval = procfs_domem(curp, lp, pfs, uio);
984263bc
MD
402 break;
403
404 case Ptype:
c7e98b2f 405 rtval = procfs_dotype(curp, lp, pfs, uio);
984263bc
MD
406 break;
407
408 case Pcmdline:
c7e98b2f 409 rtval = procfs_docmdline(curp, lp, pfs, uio);
984263bc
MD
410 break;
411
412 case Prlimit:
c7e98b2f 413 rtval = procfs_dorlimit(curp, lp, pfs, uio);
984263bc
MD
414 break;
415
416 default:
417 rtval = EOPNOTSUPP;
418 break;
419 }
9d044741 420 LWPRELE(lp);
5686ec5a 421
984263bc
MD
422 pfs->pfs_lockowner = 0;
423 wakeup(&pfs->pfs_lockowner);
198c0ff2
AHJ
424
425out:
f44c73be 426 pfs_pdone(p);
8db21154 427 lwkt_reltoken(&proc_token);
98c2b8ac 428
984263bc
MD
429 return rtval;
430}
431
432/*
433 * Get a string from userland into (buf). Strip a trailing
434 * nl character (to allow easy access from the shell).
435 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
436 * will automatically add a nul char at the end.
437 *
438 * Returns 0 on success or the following errors
439 *
440 * EINVAL: file offset is non-zero.
441 * EMSGSIZE: message is longer than kernel buffer
442 * EFAULT: user i/o buffer is not addressable
443 */
444int
ac424f9b 445vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
984263bc
MD
446{
447 int xlen;
448 int error;
449
450 if (uio->uio_offset != 0)
451 return (EINVAL);
452
453 xlen = *buflenp;
454
455 /* must be able to read the whole string in one go */
456 if (xlen < uio->uio_resid)
457 return (EMSGSIZE);
458 xlen = uio->uio_resid;
459
460 if ((error = uiomove(buf, xlen, uio)) != 0)
461 return (error);
462
463 /* allow multiple writes without seeks */
464 uio->uio_offset = 0;
465
466 /* cleanup string and remove trailing newline */
467 buf[xlen] = '\0';
468 xlen = strlen(buf);
469 if (xlen > 0 && buf[xlen-1] == '\n')
470 buf[--xlen] = '\0';
471 *buflenp = xlen;
472
473 return (0);
474}
475
476vfs_namemap_t *
ac424f9b 477vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
984263bc
MD
478{
479
480 for (; nm->nm_name; nm++)
481 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
482 return (nm);
483
484 return (0);
485}
486
487void
dadab5e9 488procfs_exit(struct thread *td)
984263bc
MD
489{
490 struct pfsnode *pfs;
5fd012e0 491 struct vnode *vp;
dadab5e9
MD
492 pid_t pid;
493
494 KKASSERT(td->td_proc);
495 pid = td->td_proc->p_pid;
984263bc
MD
496
497 /*
e193508c
MD
498 * NOTE: We can't just vgone() the vnode any more, not while
499 * it may potentially still be active. This will clean
500 * the vp and clear the mount and cause the new VOP subsystem
501 * to assert or panic when someone tries to do an operation
502 * on an open (exited) procfs descriptor.
984263bc 503 *
e193508c
MD
504 * Prevent further operations on this pid by setting pfs_pid to -1.
505 * Note that a pfs_pid of 0 is used for nodes which do not track
506 * any particular pid.
984263bc 507 *
e193508c
MD
508 * Use vx_get() to properly ref/lock a vp which may not have any
509 * refs and which may or may not already be reclaimed. vx_put()
510 * will then properly deactivate it and cause it to be recycled.
511 *
512 * The hash table can also get ripped out from under us when
513 * we block so take the easy way out and restart the scan.
984263bc 514 */
3446c007
MD
515again:
516 pfs = *PFSHASH(pid);
984263bc
MD
517 while (pfs) {
518 if (pfs->pfs_pid == pid) {
5fd012e0 519 vp = PFSTOV(pfs);
e193508c
MD
520 vx_get(vp);
521 pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */
522 vx_put(vp);
3446c007
MD
523 goto again;
524 }
525 pfs = pfs->pfs_next;
984263bc
MD
526 }
527}
3446c007 528