kernel/linprocfs: Fix accessing files in /proc (such as /proc/meminfo).
[dragonfly.git] / sys / emulation / linux / i386 / linprocfs / linprocfs_subr.c
CommitLineData
984263bc
MD
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 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/proc.h>
47#include <sys/vnode.h>
48#include <sys/malloc.h>
0961aa92 49#include <sys/mount.h>
1f2de5d4 50#include "linprocfs.h"
984263bc 51
cef48461
MD
52#define PFSHSIZE 256
53#define PFSHMASK (PFSHSIZE - 1)
54
55static struct pfsnode *pfshead[PFSHSIZE];
0bfc853c 56static struct lwkt_token pfs_token;
984263bc
MD
57static int pfsvplock;
58
9d044741 59extern int procfs_domem (struct proc *, struct lwp *, struct pfsnode *pfsp, struct uio *uio);
984263bc
MD
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 */
87int
2da2a8af
SW
88linprocfs_allocvp(struct mount *mp, struct vnode **vpp, long pid,
89 pfstype pfs_type)
984263bc 90{
984263bc
MD
91 struct pfsnode *pfs;
92 struct vnode *vp;
93 struct pfsnode **pp;
94 int error;
95
3b998fa9 96 lwkt_gettoken(&pfs_token);
984263bc 97loop:
cef48461 98 for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
984263bc
MD
99 vp = PFSTOV(pfs);
100 if (pfs->pfs_pid == pid &&
101 pfs->pfs_type == pfs_type &&
102 vp->v_mount == mp) {
87de5057 103 if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL))
984263bc
MD
104 goto loop;
105 *vpp = vp;
3b998fa9 106 lwkt_reltoken(&pfs_token);
984263bc
MD
107 return (0);
108 }
109 }
110
111 /*
112 * otherwise lock the vp list while we call getnewvnode
113 * since that can block.
114 */
115 if (pfsvplock & PROCFS_LOCKED) {
116 pfsvplock |= PROCFS_WANT;
377d4740 117 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
984263bc
MD
118 goto loop;
119 }
120 pfsvplock |= PROCFS_LOCKED;
121
122 /*
123 * Do the MALLOC before the getnewvnode since doing so afterward
124 * might cause a bogus v_data pointer to get dereferenced
125 * elsewhere if MALLOC should block.
126 */
884717e1 127 pfs = kmalloc(sizeof(struct pfsnode), M_TEMP, M_WAITOK);
984263bc 128
6ddb7618 129 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
3446c007 130 if (error) {
884717e1 131 kfree(pfs, M_TEMP);
984263bc
MD
132 goto out;
133 }
134 vp = *vpp;
135
136 vp->v_data = pfs;
137
138 pfs->pfs_next = 0;
139 pfs->pfs_pid = (pid_t) pid;
140 pfs->pfs_type = pfs_type;
141 pfs->pfs_vnode = vp;
142 pfs->pfs_flags = 0;
dadab5e9 143 pfs->pfs_lockowner = NULL;
984263bc
MD
144 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
145
146 switch (pfs_type) {
147 case Proot: /* /proc = dr-xr-xr-x */
a1f82243
AH
148 vsetflags(vp, VROOT);
149 /* fallthrough */
150 case Pnet:
151 case Psys:
152 case Psyskernel:
984263bc
MD
153 pfs->pfs_mode = (VREAD|VEXEC) |
154 (VREAD|VEXEC) >> 3 |
155 (VREAD|VEXEC) >> 6;
156 vp->v_type = VDIR;
984263bc
MD
157 break;
158
159 case Pself: /* /proc/self = lr--r--r-- */
160 pfs->pfs_mode = (VREAD) |
161 (VREAD >> 3) |
162 (VREAD >> 6);
163 vp->v_type = VLNK;
164 break;
165
166 case Pproc:
167 pfs->pfs_mode = (VREAD|VEXEC) |
168 (VREAD|VEXEC) >> 3 |
169 (VREAD|VEXEC) >> 6;
170 vp->v_type = VDIR;
171 break;
172
173 case Pexe:
a1f82243
AH
174 case Pcwd:
175 case Pprocroot:
176 case Pfd:
984263bc
MD
177 pfs->pfs_mode = (VREAD|VEXEC) |
178 (VREAD|VEXEC) >> 3 |
179 (VREAD|VEXEC) >> 6;
180 vp->v_type = VLNK;
181 break;
182
183 case Pmem:
184 pfs->pfs_mode = (VREAD|VWRITE) |
fc6d0222 185 (VREAD) >> 3;
984263bc
MD
186 vp->v_type = VREG;
187 break;
188
189 case Pprocstat:
190 case Pprocstatus:
a1f82243
AH
191 case Pcmdline:
192 case Penviron:
193 case Pstatm:
984263bc 194 /* fallthrough */
a1f82243 195 case Pmaps:
984263bc
MD
196 case Pmeminfo:
197 case Pcpuinfo:
a3c5067f 198 case Pmounts:
984263bc
MD
199 case Pstat:
200 case Puptime:
201 case Pversion:
202 case Ploadavg:
a1f82243
AH
203 case Pdevices:
204 case Pnetdev:
205 case Posrelease:
206 case Postype:
207 case Ppidmax:
984263bc
MD
208 pfs->pfs_mode = (VREAD) |
209 (VREAD >> 3) |
210 (VREAD >> 6);
211 vp->v_type = VREG;
212 break;
213
214 default:
215 panic("linprocfs_allocvp");
216 }
217
218 /* add to procfs vnode list */
cef48461 219 for (pp = &pfshead[pid & PFSHMASK]; *pp; pp = &(*pp)->pfs_next)
984263bc
MD
220 continue;
221 *pp = pfs;
5fd012e0 222
984263bc
MD
223out:
224 pfsvplock &= ~PROCFS_LOCKED;
225
226 if (pfsvplock & PROCFS_WANT) {
227 pfsvplock &= ~PROCFS_WANT;
228 wakeup((caddr_t) &pfsvplock);
229 }
3b998fa9 230 lwkt_reltoken(&pfs_token);
984263bc
MD
231
232 return (error);
233}
234
235int
2da2a8af 236linprocfs_freevp(struct vnode *vp)
984263bc
MD
237{
238 struct pfsnode **pfspp;
239 struct pfsnode *pfs = VTOPFS(vp);
240
3b998fa9 241 lwkt_gettoken(&pfs_token);
cef48461
MD
242 pfspp = &pfshead[pfs->pfs_pid & PFSHMASK];
243 while (*pfspp != pfs) {
244 KKASSERT(*pfspp != NULL);
245 pfspp = &(*pfspp)->pfs_next;
984263bc 246 }
cef48461 247 *pfspp = pfs->pfs_next;
3b998fa9 248 lwkt_reltoken(&pfs_token);
884717e1 249 kfree(vp->v_data, M_TEMP);
cef48461 250 vp->v_data = NULL;
984263bc
MD
251 return (0);
252}
253
96a686f9
SW
254/*
255 * Try to find the calling pid. Note that pfind()
256 * now references the proc structure to be returned
257 * and needs to be released later with PRELE().
258 */
259struct proc *
260linprocfs_pfind(pid_t pfs_pid)
261{
262 struct proc *p = NULL;
263
264 if (pfs_pid == 0) {
265 p = &proc0;
266 PHOLD(p);
267 } else {
268 p = pfind(pfs_pid);
269 }
270
271 return p;
272}
273
984263bc 274int
2da2a8af 275linprocfs_rw(struct vop_read_args *ap)
984263bc
MD
276{
277 struct vnode *vp = ap->a_vp;
278 struct uio *uio = ap->a_uio;
dadab5e9 279 struct thread *td = uio->uio_td;
984263bc
MD
280 struct pfsnode *pfs = VTOPFS(vp);
281 struct proc *p;
dadab5e9 282 struct proc *curp;
9d044741 283 struct lwp *lp;
984263bc
MD
284 int rtval;
285
dadab5e9
MD
286 curp = td->td_proc;
287 KKASSERT(curp);
288
96a686f9
SW
289 p = linprocfs_pfind(pfs->pfs_pid);
290 if (p == NULL) {
291 rtval = EINVAL;
292 goto out;
293 }
b0c15cdf 294 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
96a686f9
SW
295 rtval = EACCES;
296 goto out;
b0c15cdf 297 }
9d044741
SS
298 lp = FIRST_LWP_IN_PROC(p);
299 LWPHOLD(lp);
984263bc 300
b0c15cdf 301 lwkt_gettoken(&pfs_token);
984263bc 302 while (pfs->pfs_lockowner) {
377d4740 303 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
984263bc 304 }
dadab5e9 305 pfs->pfs_lockowner = curthread;
b0c15cdf
MD
306 lwkt_reltoken(&pfs_token);
307
984263bc
MD
308 switch (pfs->pfs_type) {
309 case Pmem:
9d044741 310 rtval = procfs_domem(curp, lp, pfs, uio);
984263bc
MD
311 break;
312 case Pprocstat:
313 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
314 break;
315 case Pprocstatus:
316 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
317 break;
318 case Pmeminfo:
319 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
320 break;
321 case Pcpuinfo:
322 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
323 break;
a3c5067f
AH
324 case Pmounts:
325 rtval = linprocfs_domounts(curp, p, pfs, uio);
326 break;
984263bc
MD
327 case Pstat:
328 rtval = linprocfs_dostat(curp, p, pfs, uio);
329 break;
330 case Puptime:
331 rtval = linprocfs_douptime(curp, p, pfs, uio);
332 break;
333 case Pversion:
334 rtval = linprocfs_doversion(curp, p, pfs, uio);
335 break;
336 case Ploadavg:
337 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
338 break;
a1f82243
AH
339 case Pnetdev:
340 rtval = linprocfs_donetdev(curp, p, pfs, uio);
341 break;
342 case Pdevices:
343 rtval = linprocfs_dodevices(curp, p, pfs, uio);
344 break;
345 case Posrelease:
346 rtval = linprocfs_doosrelease(curp, p, pfs, uio);
347 break;
348 case Postype:
349 rtval = linprocfs_doostype(curp, p, pfs, uio);
350 break;
351 case Ppidmax:
352 rtval = linprocfs_dopidmax(curp, p, pfs, uio);
353 break;
354 case Pmaps:
355 rtval = linprocfs_domaps(curp, p, pfs, uio);
356 break;
357 case Pstatm:
358 rtval = linprocfs_dostatm(curp, p, pfs, uio);
359 break;
984263bc
MD
360 default:
361 rtval = EOPNOTSUPP;
362 break;
363 }
9d044741 364 LWPRELE(lp);
b0c15cdf
MD
365
366 lwkt_gettoken(&pfs_token);
dadab5e9 367 pfs->pfs_lockowner = NULL;
984263bc 368 wakeup(&pfs->pfs_lockowner);
b0c15cdf 369 lwkt_reltoken(&pfs_token);
96a686f9
SW
370out:
371 if (p)
372 PRELE(p);
b0c15cdf 373
984263bc
MD
374 return rtval;
375}
376
377#if 0
378/*
379 * Get a string from userland into (buf). Strip a trailing
380 * nl character (to allow easy access from the shell).
381 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
382 * will automatically add a nul char at the end.
383 *
384 * Returns 0 on success or the following errors
385 *
386 * EINVAL: file offset is non-zero.
387 * EMSGSIZE: message is longer than kernel buffer
388 * EFAULT: user i/o buffer is not addressable
389 */
390int
2da2a8af 391vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
984263bc
MD
392{
393 int xlen;
394 int error;
395
396 if (uio->uio_offset != 0)
397 return (EINVAL);
398
399 xlen = *buflenp;
400
401 /* must be able to read the whole string in one go */
402 if (xlen < uio->uio_resid)
403 return (EMSGSIZE);
404 xlen = uio->uio_resid;
405
406 if ((error = uiomove(buf, xlen, uio)) != 0)
407 return (error);
408
409 /* allow multiple writes without seeks */
410 uio->uio_offset = 0;
411
412 /* cleanup string and remove trailing newline */
413 buf[xlen] = '\0';
414 xlen = strlen(buf);
415 if (xlen > 0 && buf[xlen-1] == '\n')
416 buf[--xlen] = '\0';
417 *buflenp = xlen;
418
419 return (0);
420}
421
422vfs_namemap_t *
2da2a8af 423vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
984263bc
MD
424{
425
426 for (; nm->nm_name; nm++)
427 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
428 return (nm);
429
430 return (0);
431}
432#endif
433
434void
0bfc853c
MD
435linprocfs_init(void)
436{
a3c18566 437 lwkt_token_init(&pfs_token, "linprocfs");
a1f82243 438}
0bfc853c
MD
439
440void
dadab5e9 441linprocfs_exit(struct thread *td)
984263bc
MD
442{
443 struct pfsnode *pfs;
cef48461
MD
444 struct vnode *vp;
445 pid_t pid;
446
447 KKASSERT(td->td_proc);
448 pid = td->td_proc->p_pid;
984263bc
MD
449
450 /*
cef48461 451 * Remove all the procfs vnodes associated with an exiting process.
984263bc 452 */
3b998fa9 453 lwkt_gettoken(&pfs_token);
cef48461
MD
454restart:
455 for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
984263bc 456 if (pfs->pfs_pid == pid) {
cef48461 457 vp = PFSTOV(pfs);
e3332475 458 vx_get(vp);
3c37c940 459 vgone_vxlocked(vp);
e3332475 460 vx_put(vp);
cef48461
MD
461 goto restart;
462 }
984263bc 463 }
3b998fa9 464 lwkt_reltoken(&pfs_token);
0bfc853c 465 lwkt_token_uninit(&pfs_token);
984263bc 466}
cef48461 467