kernel - Add per-process token, adjust signal code to use it.
[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 $
9d044741 40 * $DragonFly: src/sys/vfs/procfs/procfs_subr.c,v 1.18 2007/08/25 23:27:02 corecode Exp $
984263bc
MD
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/sysctl.h>
46#include <sys/proc.h>
47#include <sys/mount.h>
48#include <sys/vnode.h>
49#include <sys/malloc.h>
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);
87de5057 97 if (vget(vp, LK_EXCLUSIVE))
5fd012e0 98 goto loop;
3446c007
MD
99
100 /*
101 * Make sure the vnode is still in the cache after
102 * getting the interlock to avoid racing a free.
103 */
104 for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
105 if (PFSTOV(pfs) == vp &&
106 pfs->pfs_pid == pid &&
107 pfs->pfs_type == pfs_type &&
108 PFSTOV(pfs)->v_mount == mp) {
109 break;
110 }
111 }
5fd012e0
MD
112 if (pfs == NULL || PFSTOV(pfs) != vp) {
113 vput(vp);
3446c007
MD
114 goto loop;
115
116 }
984263bc
MD
117 *vpp = vp;
118 return (0);
119 }
120 }
121
122 /*
123 * otherwise lock the vp list while we call getnewvnode
124 * since that can block.
125 */
126 if (pfsvplock & PROCFS_LOCKED) {
127 pfsvplock |= PROCFS_WANT;
377d4740 128 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
984263bc
MD
129 goto loop;
130 }
131 pfsvplock |= PROCFS_LOCKED;
132
133 /*
134 * Do the MALLOC before the getnewvnode since doing so afterward
135 * might cause a bogus v_data pointer to get dereferenced
136 * elsewhere if MALLOC should block.
5fd012e0
MD
137 *
138 * XXX this may not matter anymore since getnewvnode now returns
139 * a VX locked vnode.
984263bc
MD
140 */
141 MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
142
6ddb7618 143 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
3446c007 144 if (error) {
efda3bd0 145 kfree(pfs, M_TEMP);
984263bc
MD
146 goto out;
147 }
148 vp = *vpp;
149
150 vp->v_data = pfs;
151
152 pfs->pfs_next = 0;
153 pfs->pfs_pid = (pid_t) pid;
154 pfs->pfs_type = pfs_type;
155 pfs->pfs_vnode = vp;
156 pfs->pfs_flags = 0;
157 pfs->pfs_lockowner = 0;
158 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
159
160 switch (pfs_type) {
161 case Proot: /* /proc = dr-xr-xr-x */
162 pfs->pfs_mode = (VREAD|VEXEC) |
163 (VREAD|VEXEC) >> 3 |
164 (VREAD|VEXEC) >> 6;
165 vp->v_type = VDIR;
166 vp->v_flag = VROOT;
167 break;
168
169 case Pcurproc: /* /proc/curproc = lr--r--r-- */
170 pfs->pfs_mode = (VREAD) |
171 (VREAD >> 3) |
172 (VREAD >> 6);
173 vp->v_type = VLNK;
174 break;
175
176 case Pproc:
177 pfs->pfs_mode = (VREAD|VEXEC) |
178 (VREAD|VEXEC) >> 3 |
179 (VREAD|VEXEC) >> 6;
180 vp->v_type = VDIR;
181 break;
182
183 case Pfile:
184 pfs->pfs_mode = (VREAD|VEXEC) |
185 (VREAD|VEXEC) >> 3 |
186 (VREAD|VEXEC) >> 6;
187 vp->v_type = VLNK;
188 break;
189
190 case Pmem:
191 pfs->pfs_mode = (VREAD|VWRITE);
192 vp->v_type = VREG;
193 break;
194
195 case Pregs:
196 case Pfpregs:
197 case Pdbregs:
198 pfs->pfs_mode = (VREAD|VWRITE);
199 vp->v_type = VREG;
200 break;
201
202 case Pctl:
203 case Pnote:
204 case Pnotepg:
205 pfs->pfs_mode = (VWRITE);
206 vp->v_type = VREG;
207 break;
208
209 case Ptype:
210 case Pmap:
211 case Pstatus:
212 case Pcmdline:
213 case Prlimit:
214 pfs->pfs_mode = (VREAD) |
215 (VREAD >> 3) |
216 (VREAD >> 6);
217 vp->v_type = VREG;
218 break;
219
220 default:
221 panic("procfs_allocvp");
222 }
223
224 /* add to procfs vnode list */
3446c007 225 pfs->pfs_next = *pp;
984263bc
MD
226 *pp = pfs;
227
228out:
229 pfsvplock &= ~PROCFS_LOCKED;
230
231 if (pfsvplock & PROCFS_WANT) {
232 pfsvplock &= ~PROCFS_WANT;
233 wakeup((caddr_t) &pfsvplock);
234 }
235
236 return (error);
237}
238
239int
ac424f9b 240procfs_freevp(struct vnode *vp)
984263bc
MD
241{
242 struct pfsnode **pfspp;
3446c007 243 struct pfsnode *pfs;
984263bc 244
3446c007
MD
245 pfs = VTOPFS(vp);
246 vp->v_data = NULL;
984263bc 247
3446c007
MD
248 pfspp = PFSHASH(pfs->pfs_pid);
249 while (*pfspp != pfs && *pfspp)
250 pfspp = &(*pfspp)->pfs_next;
251 KKASSERT(*pfspp);
252 *pfspp = pfs->pfs_next;
253 pfs->pfs_next = NULL;
efda3bd0 254 kfree(pfs, M_TEMP);
984263bc
MD
255 return (0);
256}
257
258int
dadab5e9 259procfs_rw(struct vop_read_args *ap)
984263bc
MD
260{
261 struct vnode *vp = ap->a_vp;
262 struct uio *uio = ap->a_uio;
dadab5e9
MD
263 struct thread *curtd = uio->uio_td;
264 struct proc *curp;
984263bc
MD
265 struct pfsnode *pfs = VTOPFS(vp);
266 struct proc *p;
c7e98b2f 267 struct lwp *lp;
984263bc
MD
268 int rtval;
269
dadab5e9
MD
270 if (curtd == NULL)
271 return (EINVAL);
272 if ((curp = curtd->td_proc) == NULL) /* XXX */
273 return (EINVAL);
274
98c2b8ac
MD
275 lwkt_gettoken(&proc_token);
276
984263bc 277 p = PFIND(pfs->pfs_pid);
98c2b8ac
MD
278 if (p == NULL) {
279 lwkt_reltoken(&proc_token);
984263bc 280 return (EINVAL);
98c2b8ac
MD
281 }
282 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
283 lwkt_reltoken(&proc_token);
984263bc 284 return (EACCES);
98c2b8ac 285 }
c7e98b2f
SS
286 /* XXX lwp */
287 lp = FIRST_LWP_IN_PROC(p);
9d044741 288 LWPHOLD(lp);
984263bc
MD
289
290 while (pfs->pfs_lockowner) {
377d4740 291 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
984263bc
MD
292 }
293 pfs->pfs_lockowner = curproc->p_pid;
294
5686ec5a
MD
295 lwkt_gettoken(&p->p_token);
296
984263bc
MD
297 switch (pfs->pfs_type) {
298 case Pnote:
299 case Pnotepg:
c7e98b2f 300 rtval = procfs_donote(curp, lp, pfs, uio);
984263bc
MD
301 break;
302
303 case Pregs:
c7e98b2f 304 rtval = procfs_doregs(curp, lp, pfs, uio);
984263bc
MD
305 break;
306
307 case Pfpregs:
c7e98b2f 308 rtval = procfs_dofpregs(curp, lp, pfs, uio);
984263bc
MD
309 break;
310
311 case Pdbregs:
c7e98b2f 312 rtval = procfs_dodbregs(curp, lp, pfs, uio);
984263bc
MD
313 break;
314
315 case Pctl:
c7e98b2f 316 rtval = procfs_doctl(curp, lp, pfs, uio);
984263bc
MD
317 break;
318
319 case Pstatus:
c7e98b2f 320 rtval = procfs_dostatus(curp, lp, pfs, uio);
984263bc
MD
321 break;
322
323 case Pmap:
c7e98b2f 324 rtval = procfs_domap(curp, lp, pfs, uio);
984263bc
MD
325 break;
326
327 case Pmem:
c7e98b2f 328 rtval = procfs_domem(curp, lp, pfs, uio);
984263bc
MD
329 break;
330
331 case Ptype:
c7e98b2f 332 rtval = procfs_dotype(curp, lp, pfs, uio);
984263bc
MD
333 break;
334
335 case Pcmdline:
c7e98b2f 336 rtval = procfs_docmdline(curp, lp, pfs, uio);
984263bc
MD
337 break;
338
339 case Prlimit:
c7e98b2f 340 rtval = procfs_dorlimit(curp, lp, pfs, uio);
984263bc
MD
341 break;
342
343 default:
344 rtval = EOPNOTSUPP;
345 break;
346 }
5686ec5a 347 lwkt_reltoken(&p->p_token);
9d044741 348 LWPRELE(lp);
5686ec5a 349
984263bc 350 pfs->pfs_lockowner = 0;
98c2b8ac 351 lwkt_reltoken(&proc_token);
984263bc 352 wakeup(&pfs->pfs_lockowner);
98c2b8ac 353
984263bc
MD
354 return rtval;
355}
356
357/*
358 * Get a string from userland into (buf). Strip a trailing
359 * nl character (to allow easy access from the shell).
360 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
361 * will automatically add a nul char at the end.
362 *
363 * Returns 0 on success or the following errors
364 *
365 * EINVAL: file offset is non-zero.
366 * EMSGSIZE: message is longer than kernel buffer
367 * EFAULT: user i/o buffer is not addressable
368 */
369int
ac424f9b 370vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
984263bc
MD
371{
372 int xlen;
373 int error;
374
375 if (uio->uio_offset != 0)
376 return (EINVAL);
377
378 xlen = *buflenp;
379
380 /* must be able to read the whole string in one go */
381 if (xlen < uio->uio_resid)
382 return (EMSGSIZE);
383 xlen = uio->uio_resid;
384
385 if ((error = uiomove(buf, xlen, uio)) != 0)
386 return (error);
387
388 /* allow multiple writes without seeks */
389 uio->uio_offset = 0;
390
391 /* cleanup string and remove trailing newline */
392 buf[xlen] = '\0';
393 xlen = strlen(buf);
394 if (xlen > 0 && buf[xlen-1] == '\n')
395 buf[--xlen] = '\0';
396 *buflenp = xlen;
397
398 return (0);
399}
400
401vfs_namemap_t *
ac424f9b 402vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
984263bc
MD
403{
404
405 for (; nm->nm_name; nm++)
406 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
407 return (nm);
408
409 return (0);
410}
411
412void
dadab5e9 413procfs_exit(struct thread *td)
984263bc
MD
414{
415 struct pfsnode *pfs;
5fd012e0 416 struct vnode *vp;
dadab5e9
MD
417 pid_t pid;
418
419 KKASSERT(td->td_proc);
420 pid = td->td_proc->p_pid;
984263bc
MD
421
422 /*
e193508c
MD
423 * NOTE: We can't just vgone() the vnode any more, not while
424 * it may potentially still be active. This will clean
425 * the vp and clear the mount and cause the new VOP subsystem
426 * to assert or panic when someone tries to do an operation
427 * on an open (exited) procfs descriptor.
984263bc 428 *
e193508c
MD
429 * Prevent further operations on this pid by setting pfs_pid to -1.
430 * Note that a pfs_pid of 0 is used for nodes which do not track
431 * any particular pid.
984263bc 432 *
e193508c
MD
433 * Use vx_get() to properly ref/lock a vp which may not have any
434 * refs and which may or may not already be reclaimed. vx_put()
435 * will then properly deactivate it and cause it to be recycled.
436 *
437 * The hash table can also get ripped out from under us when
438 * we block so take the easy way out and restart the scan.
984263bc 439 */
3446c007
MD
440again:
441 pfs = *PFSHASH(pid);
984263bc
MD
442 while (pfs) {
443 if (pfs->pfs_pid == pid) {
5fd012e0 444 vp = PFSTOV(pfs);
e193508c
MD
445 vx_get(vp);
446 pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */
447 vx_put(vp);
3446c007
MD
448 goto again;
449 }
450 pfs = pfs->pfs_next;
984263bc
MD
451 }
452}
3446c007 453