Merge from vendor branch NTPD:
[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.15 2004/12/17 00:18:05 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 #define PFSHSIZE        256
54 #define PFSHMASK        (PFSHSIZE - 1)
55
56 static struct pfsnode *pfshead[PFSHSIZE];
57 static int pfsvplock;
58
59 extern int procfs_domem (struct proc *, struct proc *, struct pfsnode *pfsp, struct uio *uio);
60
61 /*
62  * allocate a pfsnode/vnode pair.  the vnode is
63  * referenced, but not locked.
64  *
65  * the pid, pfs_type, and mount point uniquely
66  * identify a pfsnode.  the mount point is needed
67  * because someone might mount this filesystem
68  * twice.
69  *
70  * all pfsnodes are maintained on a singly-linked
71  * list.  new nodes are only allocated when they cannot
72  * be found on this list.  entries on the list are
73  * removed when the vfs reclaim entry is called.
74  *
75  * a single lock is kept for the entire list.  this is
76  * needed because the getnewvnode() function can block
77  * waiting for a vnode to become free, in which case there
78  * may be more than one process trying to get the same
79  * vnode.  this lock is only taken if we are going to
80  * call getnewvnode, since the kernel itself is single-threaded.
81  *
82  * if an entry is found on the list, then call vget() to
83  * take a reference.  this is done because there may be
84  * zero references to it and so it needs to removed from
85  * the vnode free list.
86  */
87 int
88 linprocfs_allocvp(mp, vpp, pid, pfs_type)
89         struct mount *mp;
90         struct vnode **vpp;
91         long pid;
92         pfstype pfs_type;
93 {
94         struct thread *td = curthread;  /* XXX */
95         struct pfsnode *pfs;
96         struct vnode *vp;
97         struct pfsnode **pp;
98         int error;
99
100 loop:
101         for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
102                 vp = PFSTOV(pfs);
103                 if (pfs->pfs_pid == pid &&
104                     pfs->pfs_type == pfs_type &&
105                     vp->v_mount == mp) {
106                         if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL, td))
107                                 goto loop;
108                         *vpp = vp;
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                 pfs->pfs_mode = (VREAD|VEXEC) |
151                                 (VREAD|VEXEC) >> 3 |
152                                 (VREAD|VEXEC) >> 6;
153                 vp->v_type = VDIR;
154                 vp->v_flag = VROOT;
155                 break;
156
157         case Pself:     /* /proc/self = lr--r--r-- */
158                 pfs->pfs_mode = (VREAD) |
159                                 (VREAD >> 3) |
160                                 (VREAD >> 6);
161                 vp->v_type = VLNK;
162                 break;
163
164         case Pproc:
165                 pfs->pfs_mode = (VREAD|VEXEC) |
166                                 (VREAD|VEXEC) >> 3 |
167                                 (VREAD|VEXEC) >> 6;
168                 vp->v_type = VDIR;
169                 break;
170
171         case Pexe:
172                 pfs->pfs_mode = (VREAD|VEXEC) |
173                                 (VREAD|VEXEC) >> 3 |
174                                 (VREAD|VEXEC) >> 6;
175                 vp->v_type = VLNK;
176                 break;
177
178         case Pmem:
179                 pfs->pfs_mode = (VREAD|VWRITE) |
180                                 (VREAD) >> 3;;
181                 vp->v_type = VREG;
182                 break;
183
184         case Pprocstat:
185         case Pprocstatus:
186                 /* fallthrough */
187                 
188         case Pmeminfo:
189         case Pcpuinfo:
190         case Pstat:
191         case Puptime:
192         case Pversion:
193         case Ploadavg:
194                 pfs->pfs_mode = (VREAD) |
195                                 (VREAD >> 3) |
196                                 (VREAD >> 6);
197                 vp->v_type = VREG;
198                 break;
199
200         default:
201                 panic("linprocfs_allocvp");
202         }
203
204         /* add to procfs vnode list */
205         for (pp = &pfshead[pid & PFSHMASK]; *pp; pp = &(*pp)->pfs_next)
206                 continue;
207         *pp = pfs;
208
209         vx_unlock(vp);  /* vnode ready to roll! */
210
211 out:
212         pfsvplock &= ~PROCFS_LOCKED;
213
214         if (pfsvplock & PROCFS_WANT) {
215                 pfsvplock &= ~PROCFS_WANT;
216                 wakeup((caddr_t) &pfsvplock);
217         }
218
219         return (error);
220 }
221
222 int
223 linprocfs_freevp(vp)
224         struct vnode *vp;
225 {
226         struct pfsnode **pfspp;
227         struct pfsnode *pfs = VTOPFS(vp);
228
229         pfspp = &pfshead[pfs->pfs_pid & PFSHMASK]; 
230         while (*pfspp != pfs) {
231                 KKASSERT(*pfspp != NULL);
232                 pfspp = &(*pfspp)->pfs_next;
233         }
234         *pfspp = pfs->pfs_next;
235         FREE(vp->v_data, M_TEMP);
236         vp->v_data = NULL;
237         return (0);
238 }
239
240 int
241 linprocfs_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 thread *td = uio->uio_td;
247         struct pfsnode *pfs = VTOPFS(vp);
248         struct proc *p;
249         struct proc *curp;
250         int rtval;
251
252         curp = td->td_proc;
253         KKASSERT(curp);
254
255         p = PFIND(pfs->pfs_pid);
256         if (p == 0)
257                 return (EINVAL);
258         if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
259                 return (EACCES);
260
261         while (pfs->pfs_lockowner) {
262                 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
263         }
264         pfs->pfs_lockowner = curthread;
265
266         switch (pfs->pfs_type) {
267         case Pmem:
268                 rtval = procfs_domem(curp, p, pfs, uio);
269                 break;
270         case Pprocstat:
271                 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
272                 break;
273         case Pprocstatus:
274                 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
275                 break;
276         case Pmeminfo:
277                 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
278                 break;
279         case Pcpuinfo:
280                 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
281                 break;
282         case Pstat:
283                 rtval = linprocfs_dostat(curp, p, pfs, uio);
284                 break;
285         case Puptime:
286                 rtval = linprocfs_douptime(curp, p, pfs, uio);
287                 break;
288         case Pversion:
289                 rtval = linprocfs_doversion(curp, p, pfs, uio);
290                 break;
291         case Ploadavg:
292                 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
293                 break;
294         default:
295                 rtval = EOPNOTSUPP;
296                 break;
297         }
298         pfs->pfs_lockowner = NULL;
299         wakeup(&pfs->pfs_lockowner);
300         return rtval;
301 }
302
303 #if 0
304 /*
305  * Get a string from userland into (buf).  Strip a trailing
306  * nl character (to allow easy access from the shell).
307  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
308  * will automatically add a nul char at the end.
309  *
310  * Returns 0 on success or the following errors
311  *
312  * EINVAL:    file offset is non-zero.
313  * EMSGSIZE:  message is longer than kernel buffer
314  * EFAULT:    user i/o buffer is not addressable
315  */
316 int
317 vfs_getuserstr(uio, buf, buflenp)
318         struct uio *uio;
319         char *buf;
320         int *buflenp;
321 {
322         int xlen;
323         int error;
324
325         if (uio->uio_offset != 0)
326                 return (EINVAL);
327
328         xlen = *buflenp;
329
330         /* must be able to read the whole string in one go */
331         if (xlen < uio->uio_resid)
332                 return (EMSGSIZE);
333         xlen = uio->uio_resid;
334
335         if ((error = uiomove(buf, xlen, uio)) != 0)
336                 return (error);
337
338         /* allow multiple writes without seeks */
339         uio->uio_offset = 0;
340
341         /* cleanup string and remove trailing newline */
342         buf[xlen] = '\0';
343         xlen = strlen(buf);
344         if (xlen > 0 && buf[xlen-1] == '\n')
345                 buf[--xlen] = '\0';
346         *buflenp = xlen;
347
348         return (0);
349 }
350
351 vfs_namemap_t *
352 vfs_findname(nm, buf, buflen)
353         vfs_namemap_t *nm;
354         char *buf;
355         int buflen;
356 {
357
358         for (; nm->nm_name; nm++)
359                 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
360                         return (nm);
361
362         return (0);
363 }
364 #endif
365
366 void
367 linprocfs_exit(struct thread *td)
368 {
369         struct pfsnode *pfs;
370         struct vnode *vp;
371         pid_t pid;
372
373         KKASSERT(td->td_proc);
374         pid = td->td_proc->p_pid;
375
376         /*
377          * Remove all the procfs vnodes associated with an exiting process.
378          */
379 restart:
380         for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
381                 if (pfs->pfs_pid == pid) {
382                         vp = PFSTOV(pfs);
383                         if (vx_get(vp) == 0)
384                                 vgone(vp);
385                         goto restart;
386                 }
387         }
388 }
389