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