Merge from vendor branch OPENPAM:
[dragonfly.git] / sys / vfs / procfs / procfs_subr.c
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  * $DragonFly: src/sys/vfs/procfs/procfs_subr.c,v 1.12 2004/12/17 00:18:34 dillon Exp $
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
51 #include <vfs/procfs/procfs.h>
52
53 #define PFS_HSIZE       256
54 #define PFS_HMASK       (PFS_HSIZE - 1)
55
56 static struct pfsnode *pfshead[PFS_HSIZE];
57 static int pfsvplock;
58
59 #define PFSHASH(pid)    &pfshead[(pid) & PFS_HMASK]
60
61 /*
62  * Allocate a pfsnode/vnode pair.  If no error occurs the returned vnode
63  * will be referenced and exclusively locked.
64  *
65  * The pid, pfs_type, and mount point uniquely identify a pfsnode.
66  * The mount point is needed because someone might mount this filesystem
67  * twice.
68  *
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.
72  *
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.
78  *
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.
82  */
83 int
84 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type)
85 {
86         struct thread *td = curthread;  /* XXX */
87         struct pfsnode *pfs;
88         struct vnode *vp;
89         struct pfsnode **pp;
90         int error;
91
92         pp = PFSHASH(pid);
93 loop:
94         for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
95                 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type &&
96                     PFSTOV(pfs)->v_mount == mp) {
97                         vp = PFSTOV(pfs);
98                         if (vget(vp, LK_EXCLUSIVE, td))
99                                 goto loop;
100
101                         /*
102                          * Make sure the vnode is still in the cache after
103                          * getting the interlock to avoid racing a free.
104                          */
105                         for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
106                                 if (PFSTOV(pfs) == vp &&
107                                     pfs->pfs_pid == pid && 
108                                     pfs->pfs_type == pfs_type &&
109                                     PFSTOV(pfs)->v_mount == mp) {
110                                         break;
111                                 }
112                         }
113                         if (pfs == NULL || PFSTOV(pfs) != vp) {
114                                 vput(vp);
115                                 goto loop;
116
117                         }
118                         *vpp = vp;
119                         return (0);
120                 }
121         }
122
123         /*
124          * otherwise lock the vp list while we call getnewvnode
125          * since that can block.
126          */
127         if (pfsvplock & PROCFS_LOCKED) {
128                 pfsvplock |= PROCFS_WANT;
129                 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
130                 goto loop;
131         }
132         pfsvplock |= PROCFS_LOCKED;
133
134         /*
135          * Do the MALLOC before the getnewvnode since doing so afterward
136          * might cause a bogus v_data pointer to get dereferenced
137          * elsewhere if MALLOC should block.
138          *
139          * XXX this may not matter anymore since getnewvnode now returns
140          * a VX locked vnode.
141          */
142         MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
143
144         error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
145         if (error) {
146                 free(pfs, M_TEMP);
147                 goto out;
148         }
149         vp = *vpp;
150
151         vp->v_data = pfs;
152
153         pfs->pfs_next = 0;
154         pfs->pfs_pid = (pid_t) pid;
155         pfs->pfs_type = pfs_type;
156         pfs->pfs_vnode = vp;
157         pfs->pfs_flags = 0;
158         pfs->pfs_lockowner = 0;
159         pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
160
161         switch (pfs_type) {
162         case Proot:     /* /proc = dr-xr-xr-x */
163                 pfs->pfs_mode = (VREAD|VEXEC) |
164                                 (VREAD|VEXEC) >> 3 |
165                                 (VREAD|VEXEC) >> 6;
166                 vp->v_type = VDIR;
167                 vp->v_flag = VROOT;
168                 break;
169
170         case Pcurproc:  /* /proc/curproc = lr--r--r-- */
171                 pfs->pfs_mode = (VREAD) |
172                                 (VREAD >> 3) |
173                                 (VREAD >> 6);
174                 vp->v_type = VLNK;
175                 break;
176
177         case Pproc:
178                 pfs->pfs_mode = (VREAD|VEXEC) |
179                                 (VREAD|VEXEC) >> 3 |
180                                 (VREAD|VEXEC) >> 6;
181                 vp->v_type = VDIR;
182                 break;
183
184         case Pfile:
185                 pfs->pfs_mode = (VREAD|VEXEC) |
186                                 (VREAD|VEXEC) >> 3 |
187                                 (VREAD|VEXEC) >> 6;
188                 vp->v_type = VLNK;
189                 break;
190
191         case Pmem:
192                 pfs->pfs_mode = (VREAD|VWRITE);
193                 vp->v_type = VREG;
194                 break;
195
196         case Pregs:
197         case Pfpregs:
198         case Pdbregs:
199                 pfs->pfs_mode = (VREAD|VWRITE);
200                 vp->v_type = VREG;
201                 break;
202
203         case Pctl:
204         case Pnote:
205         case Pnotepg:
206                 pfs->pfs_mode = (VWRITE);
207                 vp->v_type = VREG;
208                 break;
209
210         case Ptype:
211         case Pmap:
212         case Pstatus:
213         case Pcmdline:
214         case Prlimit:
215                 pfs->pfs_mode = (VREAD) |
216                                 (VREAD >> 3) |
217                                 (VREAD >> 6);
218                 vp->v_type = VREG;
219                 break;
220
221         default:
222                 panic("procfs_allocvp");
223         }
224
225         /* add to procfs vnode list */
226         pfs->pfs_next = *pp;
227         *pp = pfs;
228
229 out:
230         pfsvplock &= ~PROCFS_LOCKED;
231
232         if (pfsvplock & PROCFS_WANT) {
233                 pfsvplock &= ~PROCFS_WANT;
234                 wakeup((caddr_t) &pfsvplock);
235         }
236
237         return (error);
238 }
239
240 int
241 procfs_freevp(struct vnode *vp)
242 {
243         struct pfsnode **pfspp;
244         struct pfsnode *pfs;
245
246         pfs = VTOPFS(vp);
247         vp->v_data = NULL;
248
249         pfspp = PFSHASH(pfs->pfs_pid);
250         while (*pfspp != pfs && *pfspp)
251                 pfspp = &(*pfspp)->pfs_next;
252         KKASSERT(*pfspp);
253         *pfspp = pfs->pfs_next;
254         pfs->pfs_next = NULL;
255         free(pfs, M_TEMP);
256         return (0);
257 }
258
259 int
260 procfs_rw(struct vop_read_args *ap)
261 {
262         struct vnode *vp = ap->a_vp;
263         struct uio *uio = ap->a_uio;
264         struct thread *curtd = uio->uio_td;
265         struct proc *curp;
266         struct pfsnode *pfs = VTOPFS(vp);
267         struct proc *p;
268         int rtval;
269
270         if (curtd == NULL)
271                 return (EINVAL);
272         if ((curp = curtd->td_proc) == NULL)    /* XXX */
273                 return (EINVAL);
274
275         p = PFIND(pfs->pfs_pid);
276         if (p == NULL)
277                 return (EINVAL);
278         if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
279                 return (EACCES);
280
281         while (pfs->pfs_lockowner) {
282                 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
283         }
284         pfs->pfs_lockowner = curproc->p_pid;
285
286         switch (pfs->pfs_type) {
287         case Pnote:
288         case Pnotepg:
289                 rtval = procfs_donote(curp, p, pfs, uio);
290                 break;
291
292         case Pregs:
293                 rtval = procfs_doregs(curp, p, pfs, uio);
294                 break;
295
296         case Pfpregs:
297                 rtval = procfs_dofpregs(curp, p, pfs, uio);
298                 break;
299
300         case Pdbregs:
301                 rtval = procfs_dodbregs(curp, p, pfs, uio);
302                 break;
303
304         case Pctl:
305                 rtval = procfs_doctl(curp, p, pfs, uio);
306                 break;
307
308         case Pstatus:
309                 rtval = procfs_dostatus(curp, p, pfs, uio);
310                 break;
311
312         case Pmap:
313                 rtval = procfs_domap(curp, p, pfs, uio);
314                 break;
315
316         case Pmem:
317                 rtval = procfs_domem(curp, p, pfs, uio);
318                 break;
319
320         case Ptype:
321                 rtval = procfs_dotype(curp, p, pfs, uio);
322                 break;
323
324         case Pcmdline:
325                 rtval = procfs_docmdline(curp, p, pfs, uio);
326                 break;
327
328         case Prlimit:
329                 rtval = procfs_dorlimit(curp, p, pfs, uio);
330                 break;
331
332         default:
333                 rtval = EOPNOTSUPP;
334                 break;
335         }
336         pfs->pfs_lockowner = 0;
337         wakeup(&pfs->pfs_lockowner);
338         return rtval;
339 }
340
341 /*
342  * Get a string from userland into (buf).  Strip a trailing
343  * nl character (to allow easy access from the shell).
344  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
345  * will automatically add a nul char at the end.
346  *
347  * Returns 0 on success or the following errors
348  *
349  * EINVAL:    file offset is non-zero.
350  * EMSGSIZE:  message is longer than kernel buffer
351  * EFAULT:    user i/o buffer is not addressable
352  */
353 int
354 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
355 {
356         int xlen;
357         int error;
358
359         if (uio->uio_offset != 0)
360                 return (EINVAL);
361
362         xlen = *buflenp;
363
364         /* must be able to read the whole string in one go */
365         if (xlen < uio->uio_resid)
366                 return (EMSGSIZE);
367         xlen = uio->uio_resid;
368
369         if ((error = uiomove(buf, xlen, uio)) != 0)
370                 return (error);
371
372         /* allow multiple writes without seeks */
373         uio->uio_offset = 0;
374
375         /* cleanup string and remove trailing newline */
376         buf[xlen] = '\0';
377         xlen = strlen(buf);
378         if (xlen > 0 && buf[xlen-1] == '\n')
379                 buf[--xlen] = '\0';
380         *buflenp = xlen;
381
382         return (0);
383 }
384
385 vfs_namemap_t *
386 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
387 {
388
389         for (; nm->nm_name; nm++)
390                 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
391                         return (nm);
392
393         return (0);
394 }
395
396 void
397 procfs_exit(struct thread *td)
398 {
399         struct pfsnode *pfs;
400         struct vnode *vp;
401         pid_t pid;
402
403         KKASSERT(td->td_proc);
404         pid = td->td_proc->p_pid;
405
406         /*
407          * The reason for this loop is not obvious -- basicly,
408          * procfs_freevp(), which is called via vgone() (eventually),
409          * removes the specified procfs node from the pfshead list.
410          * It does this by *pfsp = pfs->pfs_next, meaning that it
411          * overwrites the node.  So when we do pfs = pfs->next, we
412          * end up skipping the node that replaces the one that was
413          * vgone'd.  Since it may have been the last one on the list,
414          * it may also have been set to null -- but *our* pfs pointer,
415          * here, doesn't see this.  So the loop starts from the beginning
416          * again.
417          *
418          * This is not a for() loop because the final event
419          * would be "pfs = pfs->pfs_next"; in the case where
420          * pfs is set to pfshead again, that would mean that
421          * pfshead is skipped over.
422          *
423          */
424 again:
425         pfs = *PFSHASH(pid);
426         while (pfs) {
427                 if (pfs->pfs_pid == pid) {
428                         vp = PFSTOV(pfs);
429                         if (vx_lock(vp) == 0) {
430                                 vgone(vp);
431                                 vx_unlock(vp);
432                         }
433                         goto again;
434                 }
435                 pfs = pfs->pfs_next;
436         }
437 }
438