linprocfs - further fix /proc/{pid}/maps
[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.23 2007/08/25 23:27:02 corecode 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 #define PFSHSIZE        256
54 #define PFSHMASK        (PFSHSIZE - 1)
55
56 static struct pfsnode *pfshead[PFSHSIZE];
57 static struct lwkt_token pfs_token;
58 static int pfsvplock;
59
60 extern int procfs_domem (struct proc *, struct lwp *, struct pfsnode *pfsp, struct uio *uio);
61
62 /*
63  * allocate a pfsnode/vnode pair.  the vnode is
64  * referenced, but not locked.
65  *
66  * the pid, pfs_type, and mount point uniquely
67  * identify a pfsnode.  the mount point is needed
68  * because someone might mount this filesystem
69  * twice.
70  *
71  * all pfsnodes are maintained on a singly-linked
72  * list.  new nodes are only allocated when they cannot
73  * be found on this list.  entries on the list are
74  * removed when the vfs reclaim entry is called.
75  *
76  * a single lock is kept for the entire list.  this is
77  * needed because the getnewvnode() function can block
78  * waiting for a vnode to become free, in which case there
79  * may be more than one process trying to get the same
80  * vnode.  this lock is only taken if we are going to
81  * call getnewvnode, since the kernel itself is single-threaded.
82  *
83  * if an entry is found on the list, then call vget() to
84  * take a reference.  this is done because there may be
85  * zero references to it and so it needs to removed from
86  * the vnode free list.
87  */
88 int
89 linprocfs_allocvp(struct mount *mp, struct vnode **vpp, long pid,
90                   pfstype pfs_type)
91 {
92         struct pfsnode *pfs;
93         struct vnode *vp;
94         struct pfsnode **pp;
95         lwkt_tokref ilock;
96         int error;
97
98         lwkt_gettoken(&ilock, &pfs_token);
99 loop:
100         for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
101                 vp = PFSTOV(pfs);
102                 if (pfs->pfs_pid == pid &&
103                     pfs->pfs_type == pfs_type &&
104                     vp->v_mount == mp) {
105                         if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL))
106                                 goto loop;
107                         *vpp = vp;
108                         lwkt_reltoken(&ilock);
109                         return (0);
110                 }
111         }
112
113         /*
114          * otherwise lock the vp list while we call getnewvnode
115          * since that can block.
116          */
117         if (pfsvplock & PROCFS_LOCKED) {
118                 pfsvplock |= PROCFS_WANT;
119                 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
120                 goto loop;
121         }
122         pfsvplock |= PROCFS_LOCKED;
123
124         /*
125          * Do the MALLOC before the getnewvnode since doing so afterward
126          * might cause a bogus v_data pointer to get dereferenced
127          * elsewhere if MALLOC should block.
128          */
129         MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
130
131         error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
132         if (error) {
133                 FREE(pfs, M_TEMP);
134                 goto out;
135         }
136         vp = *vpp;
137
138         vp->v_data = pfs;
139
140         pfs->pfs_next = 0;
141         pfs->pfs_pid = (pid_t) pid;
142         pfs->pfs_type = pfs_type;
143         pfs->pfs_vnode = vp;
144         pfs->pfs_flags = 0;
145         pfs->pfs_lockowner = NULL;
146         pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
147
148         switch (pfs_type) {
149         case Proot:     /* /proc = dr-xr-xr-x */
150                 vsetflags(vp, VROOT);
151                 /* fallthrough */
152         case Pnet:
153         case Psys:
154         case Psyskernel:
155                 pfs->pfs_mode = (VREAD|VEXEC) |
156                                 (VREAD|VEXEC) >> 3 |
157                                 (VREAD|VEXEC) >> 6;
158                 vp->v_type = VDIR;
159                 break;
160
161         case Pself:     /* /proc/self = lr--r--r-- */
162                 pfs->pfs_mode = (VREAD) |
163                                 (VREAD >> 3) |
164                                 (VREAD >> 6);
165                 vp->v_type = VLNK;
166                 break;
167
168         case Pproc:
169                 pfs->pfs_mode = (VREAD|VEXEC) |
170                                 (VREAD|VEXEC) >> 3 |
171                                 (VREAD|VEXEC) >> 6;
172                 vp->v_type = VDIR;
173                 break;
174
175         case Pexe:
176         case Pcwd:
177         case Pprocroot:
178         case Pfd:
179                 pfs->pfs_mode = (VREAD|VEXEC) |
180                                 (VREAD|VEXEC) >> 3 |
181                                 (VREAD|VEXEC) >> 6;
182                 vp->v_type = VLNK;
183                 break;
184
185         case Pmem:
186                 pfs->pfs_mode = (VREAD|VWRITE) |
187                                 (VREAD) >> 3;
188                 vp->v_type = VREG;
189                 break;
190
191         case Pprocstat:
192         case Pprocstatus:
193         case Pcmdline:
194         case Penviron:
195         case Pstatm:
196                 /* fallthrough */
197         case Pmaps:
198         case Pmeminfo:
199         case Pcpuinfo:
200         case Pmounts:
201         case Pstat:
202         case Puptime:
203         case Pversion:
204         case Ploadavg:
205         case Pdevices:
206         case Pnetdev:
207         case Posrelease:
208         case Postype:
209         case Ppidmax:
210                 pfs->pfs_mode = (VREAD) |
211                                 (VREAD >> 3) |
212                                 (VREAD >> 6);
213                 vp->v_type = VREG;
214                 break;
215
216         default:
217                 panic("linprocfs_allocvp");
218         }
219
220         /* add to procfs vnode list */
221         for (pp = &pfshead[pid & PFSHMASK]; *pp; pp = &(*pp)->pfs_next)
222                 continue;
223         *pp = pfs;
224
225         vx_unlock(vp);  /* vnode ready to roll! */
226
227 out:
228         pfsvplock &= ~PROCFS_LOCKED;
229
230         if (pfsvplock & PROCFS_WANT) {
231                 pfsvplock &= ~PROCFS_WANT;
232                 wakeup((caddr_t) &pfsvplock);
233         }
234         lwkt_reltoken(&ilock);
235
236         return (error);
237 }
238
239 int
240 linprocfs_freevp(struct vnode *vp)
241 {
242         struct pfsnode **pfspp;
243         struct pfsnode *pfs = VTOPFS(vp);
244         lwkt_tokref ilock;
245
246         lwkt_gettoken(&ilock, &pfs_token);
247         pfspp = &pfshead[pfs->pfs_pid & PFSHMASK]; 
248         while (*pfspp != pfs) {
249                 KKASSERT(*pfspp != NULL);
250                 pfspp = &(*pfspp)->pfs_next;
251         }
252         *pfspp = pfs->pfs_next;
253         lwkt_reltoken(&ilock);
254         FREE(vp->v_data, M_TEMP);
255         vp->v_data = NULL;
256         return (0);
257 }
258
259 int
260 linprocfs_rw(struct vop_read_args *ap)
261 {
262         struct vnode *vp = ap->a_vp;
263         struct uio *uio = ap->a_uio;
264         struct thread *td = uio->uio_td;
265         struct pfsnode *pfs = VTOPFS(vp);
266         struct proc *p;
267         struct proc *curp;
268         struct lwp *lp;
269         int rtval;
270
271         curp = td->td_proc;
272         KKASSERT(curp);
273
274         p = PFIND(pfs->pfs_pid);
275         if (p == 0)
276                 return (EINVAL);
277         if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
278                 return (EACCES);
279         lp = FIRST_LWP_IN_PROC(p);
280         LWPHOLD(lp);
281
282         while (pfs->pfs_lockowner) {
283                 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
284         }
285         pfs->pfs_lockowner = curthread;
286         switch (pfs->pfs_type) {
287         case Pmem:
288                 rtval = procfs_domem(curp, lp, pfs, uio);
289                 break;
290         case Pprocstat:
291                 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
292                 break;
293         case Pprocstatus:
294                 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
295                 break;
296         case Pmeminfo:
297                 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
298                 break;
299         case Pcpuinfo:
300                 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
301                 break;
302         case Pmounts:
303                 rtval = linprocfs_domounts(curp, p, pfs, uio);
304                 break;
305         case Pstat:
306                 rtval = linprocfs_dostat(curp, p, pfs, uio);
307                 break;
308         case Puptime:
309                 rtval = linprocfs_douptime(curp, p, pfs, uio);
310                 break;
311         case Pversion:
312                 rtval = linprocfs_doversion(curp, p, pfs, uio);
313                 break;
314         case Ploadavg:
315                 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
316                 break;
317         case Pnetdev:
318                 rtval = linprocfs_donetdev(curp, p, pfs, uio);
319                 break;
320         case Pdevices:
321                 rtval = linprocfs_dodevices(curp, p, pfs, uio);
322                 break;
323         case Posrelease:
324                 rtval = linprocfs_doosrelease(curp, p, pfs, uio);
325                 break;
326         case Postype:
327                 rtval = linprocfs_doostype(curp, p, pfs, uio);
328                 break;
329         case Ppidmax:
330                 rtval = linprocfs_dopidmax(curp, p, pfs, uio);
331                 break;
332         case Pmaps:
333                 rtval = linprocfs_domaps(curp, p, pfs, uio);
334                 break;
335         case Pstatm:
336                 rtval = linprocfs_dostatm(curp, p, pfs, uio);
337                 break;
338         default:
339                 rtval = EOPNOTSUPP;
340                 break;
341         }
342         LWPRELE(lp);
343         pfs->pfs_lockowner = NULL;
344         wakeup(&pfs->pfs_lockowner);
345         return rtval;
346 }
347
348 #if 0
349 /*
350  * Get a string from userland into (buf).  Strip a trailing
351  * nl character (to allow easy access from the shell).
352  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
353  * will automatically add a nul char at the end.
354  *
355  * Returns 0 on success or the following errors
356  *
357  * EINVAL:    file offset is non-zero.
358  * EMSGSIZE:  message is longer than kernel buffer
359  * EFAULT:    user i/o buffer is not addressable
360  */
361 int
362 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
363 {
364         int xlen;
365         int error;
366
367         if (uio->uio_offset != 0)
368                 return (EINVAL);
369
370         xlen = *buflenp;
371
372         /* must be able to read the whole string in one go */
373         if (xlen < uio->uio_resid)
374                 return (EMSGSIZE);
375         xlen = uio->uio_resid;
376
377         if ((error = uiomove(buf, xlen, uio)) != 0)
378                 return (error);
379
380         /* allow multiple writes without seeks */
381         uio->uio_offset = 0;
382
383         /* cleanup string and remove trailing newline */
384         buf[xlen] = '\0';
385         xlen = strlen(buf);
386         if (xlen > 0 && buf[xlen-1] == '\n')
387                 buf[--xlen] = '\0';
388         *buflenp = xlen;
389
390         return (0);
391 }
392
393 vfs_namemap_t *
394 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
395 {
396
397         for (; nm->nm_name; nm++)
398                 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
399                         return (nm);
400
401         return (0);
402 }
403 #endif
404
405 void
406 linprocfs_init(void)
407 {
408         lwkt_token_init(&pfs_token);
409
410
411 void
412 linprocfs_exit(struct thread *td)
413 {
414         lwkt_tokref ilock;
415         struct pfsnode *pfs;
416         struct vnode *vp;
417         pid_t pid;
418
419         KKASSERT(td->td_proc);
420         pid = td->td_proc->p_pid;
421
422         /*
423          * Remove all the procfs vnodes associated with an exiting process.
424          */
425         lwkt_gettoken(&ilock, &pfs_token);
426 restart:
427         for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
428                 if (pfs->pfs_pid == pid) {
429                         vp = PFSTOV(pfs);
430                         vx_get(vp);
431                         vgone_vxlocked(vp);
432                         vx_put(vp);
433                         goto restart;
434                 }
435         }
436         lwkt_reltoken(&ilock);
437         lwkt_token_uninit(&pfs_token);
438 }
439