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