871c843616ed82d1de7511425ae525e5b5c8f000
[dragonfly.git] / sys / emulation / linux / i386 / linprocfs / linprocfs_subr.c
1 /*
2  * Copyright (c) 2000 Dag-Erling Coïdan Smørgrav
3  * Copyright (c) 1999 Pierre Beyssac
4  * Copyright (c) 1993 Jan-Simon Pendry
5  * Copyright (c) 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)procfs_subr.c       8.6 (Berkeley) 5/14/95
40  *
41  * $FreeBSD: src/sys/i386/linux/linprocfs/linprocfs_subr.c,v 1.3.2.4 2001/06/25 19:46:47 pirzyk Exp $
42  * $DragonFly: src/sys/emulation/linux/i386/linprocfs/linprocfs_subr.c,v 1.11 2004/08/17 18:57:32 dillon Exp $
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include "linprocfs.h"
52
53 static struct pfsnode *pfshead;
54 static int pfsvplock;
55
56 extern int procfs_domem (struct proc *, struct proc *, struct pfsnode *pfsp, struct uio *uio);
57
58 /*
59  * allocate a pfsnode/vnode pair.  the vnode is
60  * referenced, but not locked.
61  *
62  * the pid, pfs_type, and mount point uniquely
63  * identify a pfsnode.  the mount point is needed
64  * because someone might mount this filesystem
65  * twice.
66  *
67  * all pfsnodes are maintained on a singly-linked
68  * list.  new nodes are only allocated when they cannot
69  * be found on this list.  entries on the list are
70  * removed when the vfs reclaim entry is called.
71  *
72  * a single lock is kept for the entire list.  this is
73  * needed because the getnewvnode() function can block
74  * waiting for a vnode to become free, in which case there
75  * may be more than one process trying to get the same
76  * vnode.  this lock is only taken if we are going to
77  * call getnewvnode, since the kernel itself is single-threaded.
78  *
79  * if an entry is found on the list, then call vget() to
80  * take a reference.  this is done because there may be
81  * zero references to it and so it needs to removed from
82  * the vnode free list.
83  */
84 int
85 linprocfs_allocvp(mp, vpp, pid, pfs_type)
86         struct mount *mp;
87         struct vnode **vpp;
88         long pid;
89         pfstype pfs_type;
90 {
91         struct thread *td = curthread;  /* XXX */
92         struct pfsnode *pfs;
93         struct vnode *vp;
94         struct pfsnode **pp;
95         int error;
96
97 loop:
98         for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
99                 vp = PFSTOV(pfs);
100                 if (pfs->pfs_pid == pid &&
101                     pfs->pfs_type == pfs_type &&
102                     vp->v_mount == mp) {
103                         if (vget(vp, NULL, 0, td))
104                                 goto loop;
105                         *vpp = vp;
106                         return (0);
107                 }
108         }
109
110         /*
111          * otherwise lock the vp list while we call getnewvnode
112          * since that can block.
113          */
114         if (pfsvplock & PROCFS_LOCKED) {
115                 pfsvplock |= PROCFS_WANT;
116                 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
117                 goto loop;
118         }
119         pfsvplock |= PROCFS_LOCKED;
120
121         /*
122          * Do the MALLOC before the getnewvnode since doing so afterward
123          * might cause a bogus v_data pointer to get dereferenced
124          * elsewhere if MALLOC should block.
125          */
126         MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
127
128         if ((error = getnewvnode(VT_PROCFS, mp, mp->mnt_vn_ops, vpp)) != 0) {
129                 FREE(pfs, M_TEMP);
130                 goto out;
131         }
132         vp = *vpp;
133
134         vp->v_data = pfs;
135
136         pfs->pfs_next = 0;
137         pfs->pfs_pid = (pid_t) pid;
138         pfs->pfs_type = pfs_type;
139         pfs->pfs_vnode = vp;
140         pfs->pfs_flags = 0;
141         pfs->pfs_lockowner = NULL;
142         pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
143
144         switch (pfs_type) {
145         case Proot:     /* /proc = dr-xr-xr-x */
146                 pfs->pfs_mode = (VREAD|VEXEC) |
147                                 (VREAD|VEXEC) >> 3 |
148                                 (VREAD|VEXEC) >> 6;
149                 vp->v_type = VDIR;
150                 vp->v_flag = VROOT;
151                 break;
152
153         case Pself:     /* /proc/self = lr--r--r-- */
154                 pfs->pfs_mode = (VREAD) |
155                                 (VREAD >> 3) |
156                                 (VREAD >> 6);
157                 vp->v_type = VLNK;
158                 break;
159
160         case Pproc:
161                 pfs->pfs_mode = (VREAD|VEXEC) |
162                                 (VREAD|VEXEC) >> 3 |
163                                 (VREAD|VEXEC) >> 6;
164                 vp->v_type = VDIR;
165                 break;
166
167         case Pexe:
168                 pfs->pfs_mode = (VREAD|VEXEC) |
169                                 (VREAD|VEXEC) >> 3 |
170                                 (VREAD|VEXEC) >> 6;
171                 vp->v_type = VLNK;
172                 break;
173
174         case Pmem:
175                 pfs->pfs_mode = (VREAD|VWRITE) |
176                                 (VREAD) >> 3;;
177                 vp->v_type = VREG;
178                 break;
179
180         case Pprocstat:
181         case Pprocstatus:
182                 /* fallthrough */
183                 
184         case Pmeminfo:
185         case Pcpuinfo:
186         case Pstat:
187         case Puptime:
188         case Pversion:
189         case Ploadavg:
190                 pfs->pfs_mode = (VREAD) |
191                                 (VREAD >> 3) |
192                                 (VREAD >> 6);
193                 vp->v_type = VREG;
194                 break;
195
196         default:
197                 panic("linprocfs_allocvp");
198         }
199
200         /* add to procfs vnode list */
201         for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
202                 continue;
203         *pp = pfs;
204
205 out:
206         pfsvplock &= ~PROCFS_LOCKED;
207
208         if (pfsvplock & PROCFS_WANT) {
209                 pfsvplock &= ~PROCFS_WANT;
210                 wakeup((caddr_t) &pfsvplock);
211         }
212
213         return (error);
214 }
215
216 int
217 linprocfs_freevp(vp)
218         struct vnode *vp;
219 {
220         struct pfsnode **pfspp;
221         struct pfsnode *pfs = VTOPFS(vp);
222
223         for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
224                 if (*pfspp == pfs) {
225                         *pfspp = pfs->pfs_next;
226                         break;
227                 }
228         }
229
230         FREE(vp->v_data, M_TEMP);
231         vp->v_data = 0;
232         return (0);
233 }
234
235 int
236 linprocfs_rw(ap)
237         struct vop_read_args *ap;
238 {
239         struct vnode *vp = ap->a_vp;
240         struct uio *uio = ap->a_uio;
241         struct thread *td = uio->uio_td;
242         struct pfsnode *pfs = VTOPFS(vp);
243         struct proc *p;
244         struct proc *curp;
245         int rtval;
246
247         curp = td->td_proc;
248         KKASSERT(curp);
249
250         p = PFIND(pfs->pfs_pid);
251         if (p == 0)
252                 return (EINVAL);
253         if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
254                 return (EACCES);
255
256         while (pfs->pfs_lockowner) {
257                 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
258         }
259         pfs->pfs_lockowner = curthread;
260
261         switch (pfs->pfs_type) {
262         case Pmem:
263                 rtval = procfs_domem(curp, p, pfs, uio);
264                 break;
265         case Pprocstat:
266                 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
267                 break;
268         case Pprocstatus:
269                 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
270                 break;
271         case Pmeminfo:
272                 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
273                 break;
274         case Pcpuinfo:
275                 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
276                 break;
277         case Pstat:
278                 rtval = linprocfs_dostat(curp, p, pfs, uio);
279                 break;
280         case Puptime:
281                 rtval = linprocfs_douptime(curp, p, pfs, uio);
282                 break;
283         case Pversion:
284                 rtval = linprocfs_doversion(curp, p, pfs, uio);
285                 break;
286         case Ploadavg:
287                 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
288                 break;
289         default:
290                 rtval = EOPNOTSUPP;
291                 break;
292         }
293         pfs->pfs_lockowner = NULL;
294         wakeup(&pfs->pfs_lockowner);
295         return rtval;
296 }
297
298 #if 0
299 /*
300  * Get a string from userland into (buf).  Strip a trailing
301  * nl character (to allow easy access from the shell).
302  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
303  * will automatically add a nul char at the end.
304  *
305  * Returns 0 on success or the following errors
306  *
307  * EINVAL:    file offset is non-zero.
308  * EMSGSIZE:  message is longer than kernel buffer
309  * EFAULT:    user i/o buffer is not addressable
310  */
311 int
312 vfs_getuserstr(uio, buf, buflenp)
313         struct uio *uio;
314         char *buf;
315         int *buflenp;
316 {
317         int xlen;
318         int error;
319
320         if (uio->uio_offset != 0)
321                 return (EINVAL);
322
323         xlen = *buflenp;
324
325         /* must be able to read the whole string in one go */
326         if (xlen < uio->uio_resid)
327                 return (EMSGSIZE);
328         xlen = uio->uio_resid;
329
330         if ((error = uiomove(buf, xlen, uio)) != 0)
331                 return (error);
332
333         /* allow multiple writes without seeks */
334         uio->uio_offset = 0;
335
336         /* cleanup string and remove trailing newline */
337         buf[xlen] = '\0';
338         xlen = strlen(buf);
339         if (xlen > 0 && buf[xlen-1] == '\n')
340                 buf[--xlen] = '\0';
341         *buflenp = xlen;
342
343         return (0);
344 }
345
346 vfs_namemap_t *
347 vfs_findname(nm, buf, buflen)
348         vfs_namemap_t *nm;
349         char *buf;
350         int buflen;
351 {
352
353         for (; nm->nm_name; nm++)
354                 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
355                         return (nm);
356
357         return (0);
358 }
359 #endif
360
361 void
362 linprocfs_exit(struct thread *td)
363 {
364         struct pfsnode *pfs;
365         pid_t pid = (td->td_proc) ? td->td_proc->p_pid : -1; /* YYY */
366
367         /*
368          * The reason for this loop is not obvious -- basicly,
369          * linprocfs_freevp(), which is called via vgone() (eventually),
370          * removes the specified procfs node from the pfshead list.
371          * It does this by *pfsp = pfs->pfs_next, meaning that it
372          * overwrites the node.  So when we do pfs = pfs->next, we
373          * end up skipping the node that replaces the one that was
374          * vgone'd.  Since it may have been the last one on the list,
375          * it may also have been set to null -- but *our* pfs pointer,
376          * here, doesn't see this.  So the loop starts from the beginning
377          * again.
378          *
379          * This is not a for() loop because the final event
380          * would be "pfs = pfs->pfs_next"; in the case where
381          * pfs is set to pfshead again, that would mean that
382          * pfshead is skipped over.
383          *
384          */
385         pfs = pfshead;
386         while (pfs) {
387                 if (pfs->pfs_pid == pid) {
388                         vgone(PFSTOV(pfs));
389                         pfs = pfshead;
390                 } else
391                         pfs = pfs->pfs_next;
392         }
393 }