1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/* $NetBSD: procfs_subr.c,v 1.58 2003/09/27 13:29:02 darcy Exp $ */
/*
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
*/
/*
* Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
* Copyright (c) 1993 Jan-Simon Pendry
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.58 2003/09/27 13:29:02 darcy Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <miscfs/procfs/procfs.h>
void procfs_hashins __P((struct pfsnode *));
void procfs_hashrem __P((struct pfsnode *));
struct vnode *procfs_hashget __P((pid_t, pfstype, int, struct mount *));
LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
u_long pfs_ihash; /* size of hash table - 1 */
#define PFSPIDHASH(pid) ((pid) & pfs_ihash)
struct lock pfs_hashlock;
struct simplelock pfs_hash_slock;
#define ISSET(t, f) ((t) & (f))
/*
* allocate a pfsnode/vnode pair. the vnode is
* referenced, and locked.
*
* the pid, pfs_type, and mount point uniquely
* identify a pfsnode. the mount point is needed
* because someone might mount this filesystem
* twice.
*
* all pfsnodes are maintained on a singly-linked
* list. new nodes are only allocated when they cannot
* be found on this list. entries on the list are
* removed when the vfs reclaim entry is called.
*
* a single lock is kept for the entire list. this is
* needed because the getnewvnode() function can block
* waiting for a vnode to become free, in which case there
* may be more than one process trying to get the same
* vnode. this lock is only taken if we are going to
* call getnewvnode, since the kernel itself is single-threaded.
*
* if an entry is found on the list, then call vget() to
* take a reference. this is done because there may be
* zero references to it and so it needs to removed from
* the vnode free list.
*/
int
procfs_allocvp(mp, vpp, pid, pfs_type, fd)
struct mount *mp;
struct vnode **vpp;
pid_t pid;
pfstype pfs_type;
int fd;
{
struct pfsnode *pfs;
struct vnode *vp;
int error;
do {
if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp)) != NULL)
return (0);
} while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, &vp)) != 0) {
*vpp = NULL;
lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
return (error);
}
MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
vp->v_data = pfs;
pfs->pfs_pid = pid;
pfs->pfs_type = pfs_type;
pfs->pfs_vnode = vp;
pfs->pfs_flags = 0;
pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
pfs->pfs_fd = fd;
switch (pfs_type) {
case PFSroot: /* /proc = dr-xr-xr-x */
pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
vp->v_type = VDIR;
vp->v_flag = VROOT;
break;
case PFScurproc: /* /proc/curproc = lr-xr-xr-x */
case PFSself: /* /proc/self = lr-xr-xr-x */
pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
vp->v_type = VLNK;
break;
case PFSproc: /* /proc/N = dr-xr-xr-x */
case PFSfd:
if (fd == -1) { /* /proc/N/fd = dr-xr-xr-x */
pfs->pfs_mode = S_IRUSR|S_IXUSR;
vp->v_type = VDIR;
} else { /* /proc/N/fd/M = [ps-]rw------- */
struct file *fp;
struct vnode *vxp;
struct proc *pown;
/* XXX can procfs_getfp() ever fail here? */
if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
goto bad;
FILE_USE(fp);
pfs->pfs_mode = S_IRUSR|S_IWUSR;
switch (fp->f_type) {
case DTYPE_VNODE:
vxp = (struct vnode *)fp->f_data;
/*
* We make symlinks for directories
* to avoid cycles.
*/
if (vxp->v_type == VDIR)
goto symlink;
vp->v_type = vxp->v_type;
break;
case DTYPE_PIPE:
vp->v_type = VFIFO;
break;
case DTYPE_SOCKET:
vp->v_type = VSOCK;
break;
case DTYPE_KQUEUE:
case DTYPE_MISC:
symlink:
pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
S_IXGRP|S_IROTH|S_IXOTH;
vp->v_type = VLNK;
break;
default:
error = EOPNOTSUPP;
FILE_UNUSE(fp, pown);
goto bad;
}
FILE_UNUSE(fp, pown);
}
break;
case PFSfile: /* /proc/N/file = -rw------- */
case PFSmem: /* /proc/N/mem = -rw------- */
case PFSregs: /* /proc/N/regs = -rw------- */
case PFSfpregs: /* /proc/N/fpregs = -rw------- */
pfs->pfs_mode = S_IRUSR|S_IWUSR;
vp->v_type = VREG;
break;
case PFSctl: /* /proc/N/ctl = --w------ */
case PFSnote: /* /proc/N/note = --w------ */
case PFSnotepg: /* /proc/N/notepg = --w------ */
pfs->pfs_mode = S_IWUSR;
vp->v_type = VREG;
break;
case PFSmap: /* /proc/N/map = -r--r--r-- */
case PFSmaps: /* /proc/N/maps = -r--r--r-- */
case PFSstatus: /* /proc/N/status = -r--r--r-- */
case PFSstat: /* /proc/N/stat = -r--r--r-- */
case PFScmdline: /* /proc/N/cmdline = -r--r--r-- */
case PFSmeminfo: /* /proc/meminfo = -r--r--r-- */
case PFScpuinfo: /* /proc/cpuinfo = -r--r--r-- */
case PFSuptime: /* /proc/uptime = -r--r--r-- */
pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
vp->v_type = VREG;
break;
#ifdef __HAVE_PROCFS_MACHDEP
PROCFS_MACHDEP_NODETYPE_CASES
procfs_machdep_allocvp(vp);
break;
#endif
default:
panic("procfs_allocvp");
}
procfs_hashins(pfs);
uvm_vnp_setsize(vp, 0);
lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
*vpp = vp;
return (0);
bad:
lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
FREE(pfs, M_TEMP);
ungetnewvnode(vp);
return (error);
}
int
procfs_freevp(vp)
struct vnode *vp;
{
struct pfsnode *pfs = VTOPFS(vp);
procfs_hashrem(pfs);
FREE(vp->v_data, M_TEMP);
vp->v_data = 0;
return (0);
}
int
procfs_rw(v)
void *v;
{
struct vop_read_args *ap = v;
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
struct proc *curp = uio->uio_procp;
struct pfsnode *pfs = VTOPFS(vp);
struct lwp *l;
struct proc *p;
p = PFIND(pfs->pfs_pid);
if (p == 0)
return (EINVAL);
/* XXX NJWLWP
* The entire procfs interface needs work to be useful to
* a process with multiple LWPs. For the moment, we'll
* just kluge this and fail on others.
*/
l = proc_representative_lwp(p);
switch (pfs->pfs_type) {
case PFSregs:
case PFSfpregs:
case PFSmem:
#if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
PROCFS_MACHDEP_PROTECT_CASES
#endif
/*
* Do not allow init to be modified while in secure mode; it
* could be duped into changing the security level.
*/
if (uio->uio_rw == UIO_WRITE &&
p == initproc && securelevel > -1)
return (EPERM);
break;
default:
break;
}
switch (pfs->pfs_type) {
case PFSnote:
case PFSnotepg:
return (procfs_donote(curp, p, pfs, uio));
case PFSregs:
return (procfs_doregs(curp, l, pfs, uio));
case PFSfpregs:
return (procfs_dofpregs(curp, l, pfs, uio));
case PFSctl:
return (procfs_doctl(curp, l, pfs, uio));
case PFSstatus:
return (procfs_dostatus(curp, l, pfs, uio));
case PFSstat:
return (procfs_do_pid_stat(curp, l, pfs, uio));
case PFSmap:
return (procfs_domap(curp, p, pfs, uio, 0));
case PFSmaps:
return (procfs_domap(curp, p, pfs, uio, 1));
case PFSmem:
return (procfs_domem(curp, p, pfs, uio));
case PFScmdline:
return (procfs_docmdline(curp, p, pfs, uio));
case PFSmeminfo:
return (procfs_domeminfo(curp, p, pfs, uio));
case PFScpuinfo:
return (procfs_docpuinfo(curp, p, pfs, uio));
case PFSfd:
return (procfs_dofd(curp, p, pfs, uio));
case PFSuptime:
return (procfs_douptime(curp, p, pfs, uio));
#ifdef __HAVE_PROCFS_MACHDEP
PROCFS_MACHDEP_NODETYPE_CASES
return (procfs_machdep_rw(curp, l, pfs, uio));
#endif
default:
return (EOPNOTSUPP);
}
}
/*
* Get a string from userland into (buf). Strip a trailing
* nl character (to allow easy access from the shell).
* The buffer should be *buflenp + 1 chars long. vfs_getuserstr
* will automatically add a nul char at the end.
*
* Returns 0 on success or the following errors
*
* EINVAL: file offset is non-zero.
* EMSGSIZE: message is longer than kernel buffer
* EFAULT: user i/o buffer is not addressable
*/
int
vfs_getuserstr(uio, buf, buflenp)
struct uio *uio;
char *buf;
int *buflenp;
{
int xlen;
int error;
if (uio->uio_offset != 0)
return (EINVAL);
xlen = *buflenp;
/* must be able to read the whole string in one go */
if (xlen < uio->uio_resid)
return (EMSGSIZE);
xlen = uio->uio_resid;
if ((error = uiomove(buf, xlen, uio)) != 0)
return (error);
/* allow multiple writes without seeks */
uio->uio_offset = 0;
/* cleanup string and remove trailing newline */
buf[xlen] = '\0';
xlen = strlen(buf);
if (xlen > 0 && buf[xlen-1] == '\n')
buf[--xlen] = '\0';
*buflenp = xlen;
return (0);
}
const vfs_namemap_t *
vfs_findname(nm, buf, buflen)
const vfs_namemap_t *nm;
const char *buf;
int buflen;
{
for (; nm->nm_name; nm++)
if (memcmp(buf, nm->nm_name, buflen+1) == 0)
return (nm);
return (0);
}
/*
* Initialize pfsnode hash table.
*/
void
procfs_hashinit()
{
lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
M_WAITOK, &pfs_ihash);
simple_lock_init(&pfs_hash_slock);
}
void
procfs_hashreinit()
{
struct pfsnode *pp;
struct pfs_hashhead *oldhash, *hash;
u_long i, oldmask, mask, val;
hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
&mask);
simple_lock(&pfs_hash_slock);
oldhash = pfs_hashtbl;
oldmask = pfs_ihash;
pfs_hashtbl = hash;
pfs_ihash = mask;
for (i = 0; i <= oldmask; i++) {
while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
LIST_REMOVE(pp, pfs_hash);
val = PFSPIDHASH(pp->pfs_pid);
LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
}
}
simple_unlock(&pfs_hash_slock);
hashdone(oldhash, M_UFSMNT);
}
/*
* Free pfsnode hash table.
*/
void
procfs_hashdone()
{
hashdone(pfs_hashtbl, M_UFSMNT);
}
struct vnode *
procfs_hashget(pid, type, fd, mp)
pid_t pid;
pfstype type;
int fd;
struct mount *mp;
{
struct pfs_hashhead *ppp;
struct pfsnode *pp;
struct vnode *vp;
loop:
simple_lock(&pfs_hash_slock);
ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
LIST_FOREACH(pp, ppp, pfs_hash) {
vp = PFSTOV(pp);
if (pid == pp->pfs_pid && pp->pfs_type == type &&
pp->pfs_fd == fd && vp->v_mount == mp) {
simple_lock(&vp->v_interlock);
simple_unlock(&pfs_hash_slock);
if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
goto loop;
return (vp);
}
}
simple_unlock(&pfs_hash_slock);
return (NULL);
}
/*
* Insert the pfsnode into the hash table and lock it.
*/
void
procfs_hashins(pp)
struct pfsnode *pp;
{
struct pfs_hashhead *ppp;
/* lock the pfsnode, then put it on the appropriate hash list */
lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
simple_lock(&pfs_hash_slock);
ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
LIST_INSERT_HEAD(ppp, pp, pfs_hash);
simple_unlock(&pfs_hash_slock);
}
/*
* Remove the pfsnode from the hash table.
*/
void
procfs_hashrem(pp)
struct pfsnode *pp;
{
simple_lock(&pfs_hash_slock);
LIST_REMOVE(pp, pfs_hash);
simple_unlock(&pfs_hash_slock);
}
void
procfs_revoke_vnodes(p, arg)
struct proc *p;
void *arg;
{
struct pfsnode *pfs, *pnext;
struct vnode *vp;
struct mount *mp = (struct mount *)arg;
struct pfs_hashhead *ppp;
if (!(p->p_flag & P_SUGID))
return;
ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
vp = PFSTOV(pfs);
pnext = LIST_NEXT(pfs, pfs_hash);
if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
vp->v_mount == mp)
VOP_REVOKE(vp, REVOKEALL);
}
}
int
procfs_getfp(pfs, pown, fp)
struct pfsnode *pfs;
struct proc **pown;
struct file **fp;
{
struct proc *p = PFIND(pfs->pfs_pid);
if (p == NULL)
return ESRCH;
if (pfs->pfs_fd == -1)
return EINVAL;
if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
return EBADF;
*pown = p;
return 0;
}
|