a0bde598051cc57c8295b1e64413a458347ade02
[dragonfly.git] / sys / emulation / linux / i386 / linprocfs / linprocfs_vnops.c
bloba0bde598051cc57c8295b1e64413a458347ade02
1 /*
2 * Copyright (c) 2000 Dag-Erling Coïdan Smørgrav
3 * Copyright (c) 1999 Pierre Beyssac
4 * Copyright (c) 1993, 1995 Jan-Simon Pendry
5 * Copyright (c) 1993, 1995
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry.
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.
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.
39 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
41 * $FreeBSD: src/sys/i386/linux/linprocfs/linprocfs_vnops.c,v 1.3.2.5 2001/08/12 14:29:19 rwatson Exp $
42 * $DragonFly: src/sys/emulation/linux/i386/linprocfs/linprocfs_vnops.c,v 1.42 2007/11/20 21:03:48 dillon Exp $
46 * procfs vnode interface
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/fcntl.h>
55 #include <sys/proc.h>
56 #include <sys/priv.h>
57 #include <sys/signalvar.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/dirent.h>
62 #include <sys/malloc.h>
63 #include <sys/reg.h>
64 #include <sys/jail.h>
65 #include <vm/vm_zone.h>
66 #include "linprocfs.h"
67 #include <sys/pioctl.h>
69 #include <machine/limits.h>
71 extern struct vnode *procfs_findtextvp (struct proc *);
73 static int linprocfs_access (struct vop_access_args *);
74 static int linprocfs_badop (struct vop_generic_args *);
75 static int linprocfs_bmap (struct vop_bmap_args *);
76 static int linprocfs_close (struct vop_close_args *);
77 static int linprocfs_getattr (struct vop_getattr_args *);
78 static int linprocfs_inactive (struct vop_inactive_args *);
79 static int linprocfs_ioctl (struct vop_ioctl_args *);
80 static int linprocfs_lookup (struct vop_old_lookup_args *);
81 static int linprocfs_open (struct vop_open_args *);
82 static int linprocfs_print (struct vop_print_args *);
83 static int linprocfs_readdir (struct vop_readdir_args *);
84 static int linprocfs_readlink (struct vop_readlink_args *);
85 static int linprocfs_reclaim (struct vop_reclaim_args *);
86 static int linprocfs_setattr (struct vop_setattr_args *);
88 static int linprocfs_readdir_proc(struct vop_readdir_args *);
89 static int linprocfs_readdir_root(struct vop_readdir_args *);
90 static int linprocfs_readdir_net(struct vop_readdir_args *ap);
91 static int linprocfs_readdir_sys(struct vop_readdir_args *ap);
92 static int linprocfs_readdir_syskernel(struct vop_readdir_args *ap);
95 * procfs vnode operations.
97 struct vop_ops linprocfs_vnode_vops = {
98 .vop_default = vop_defaultop,
99 .vop_access = linprocfs_access,
100 .vop_advlock = (void *)linprocfs_badop,
101 .vop_bmap = linprocfs_bmap,
102 .vop_close = linprocfs_close,
103 .vop_old_create = (void *)linprocfs_badop,
104 .vop_getattr = linprocfs_getattr,
105 .vop_inactive = linprocfs_inactive,
106 .vop_old_link = (void *)linprocfs_badop,
107 .vop_old_lookup = linprocfs_lookup,
108 .vop_old_mkdir = (void *)linprocfs_badop,
109 .vop_old_mknod = (void *)linprocfs_badop,
110 .vop_open = linprocfs_open,
111 .vop_pathconf = vop_stdpathconf,
112 .vop_print = linprocfs_print,
113 .vop_read = (void *)linprocfs_rw,
114 .vop_readdir = linprocfs_readdir,
115 .vop_readlink = linprocfs_readlink,
116 .vop_reclaim = linprocfs_reclaim,
117 .vop_old_remove = (void *)linprocfs_badop,
118 .vop_old_rename = (void *)linprocfs_badop,
119 .vop_old_rmdir = (void *)linprocfs_badop,
120 .vop_setattr = linprocfs_setattr,
121 .vop_old_symlink = (void *)linprocfs_badop,
122 .vop_write = (void *)linprocfs_rw,
123 .vop_ioctl = linprocfs_ioctl
127 * This is a list of the valid names in the
128 * process-specific sub-directories. It is
129 * used in linprocfs_lookup and linprocfs_readdir
131 static struct proc_target {
132 u_char pt_type;
133 u_char pt_namlen;
134 char *pt_name;
135 pfstype pt_pfstype;
136 int (*pt_valid) (struct proc *p);
137 } proc_targets[] = {
138 #define N(s) sizeof(s)-1, s
139 /* name type validp */
140 { DT_DIR, N("."), Pproc, NULL },
141 { DT_DIR, N(".."), Proot, NULL },
142 { DT_REG, N("mem"), Pmem, NULL },
144 { DT_LNK, N("exe"), Pexe, NULL },
145 { DT_LNK, N("cwd"), Pcwd, NULL },
146 { DT_LNK, N("root"), Pprocroot, NULL },
147 { DT_LNK, N("fd"), Pfd, NULL },
149 { DT_REG, N("stat"), Pprocstat, NULL },
150 { DT_REG, N("status"), Pprocstatus, NULL },
151 { DT_REG, N("maps"), Pmaps, NULL },
152 { DT_REG, N("statm"), Pstatm, NULL },
153 #if 0
154 { DT_REG, N("cmdline"), Pcmdline, NULL },
155 { DT_REG, N("environ"), Penviron, NULL },
156 #endif
157 #undef N
159 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
161 static pid_t atopid (const char *, u_int);
164 * set things up for doing i/o on
165 * the pfsnode (vp). (vp) is locked
166 * on entry, and should be left locked
167 * on exit.
169 * for procfs we don't need to do anything
170 * in particular for i/o. all that is done
171 * is to support exclusive open on process
172 * memory images.
174 static int
175 linprocfs_open(struct vop_open_args *ap)
177 struct pfsnode *pfs = VTOPFS(ap->a_vp);
178 struct proc *p2;
180 p2 = PFIND(pfs->pfs_pid);
181 if (p2 == NULL)
182 return (ENOENT);
183 if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred))
184 return (ENOENT);
186 switch (pfs->pfs_type) {
187 case Pmem:
188 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
189 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
190 return (EBUSY);
192 if (p_trespass(ap->a_cred, p2->p_ucred))
193 return (EPERM);
195 if (ap->a_mode & FWRITE)
196 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
198 break;
199 default:
200 break;
203 return (vop_stdopen(ap));
207 * close the pfsnode (vp) after doing i/o.
208 * (vp) is not locked on entry or exit.
210 * nothing to do for procfs other than undo
211 * any exclusive open flag (see _open above).
213 static int
214 linprocfs_close(struct vop_close_args *ap)
216 struct pfsnode *pfs = VTOPFS(ap->a_vp);
217 struct proc *p;
219 switch (pfs->pfs_type) {
220 case Pmem:
221 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
222 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
224 * If this is the last close, then it checks to see if
225 * the target process has PF_LINGER set in p_pfsflags,
226 * if this is *not* the case, then the process' stop flags
227 * are cleared, and the process is woken up. This is
228 * to help prevent the case where a process has been
229 * told to stop on an event, but then the requesting process
230 * has gone away or forgotten about it.
232 if ((ap->a_vp->v_opencount < 2)
233 && (p = pfind(pfs->pfs_pid))
234 && !(p->p_pfsflags & PF_LINGER)) {
235 p->p_stops = 0;
236 p->p_step = 0;
237 wakeup(&p->p_step);
239 break;
240 default:
241 break;
243 return (vop_stdclose(ap));
247 * do an ioctl operation on a pfsnode (vp).
248 * (vp) is not locked on entry or exit.
250 static int
251 linprocfs_ioctl(struct vop_ioctl_args *ap)
253 struct pfsnode *pfs = VTOPFS(ap->a_vp);
254 struct proc *procp;
255 int error;
256 int signo;
257 struct procfs_status *psp;
258 unsigned char flags;
260 procp = pfind(pfs->pfs_pid);
261 if (procp == NULL) {
262 return ENOTTY;
265 if (p_trespass(ap->a_cred, procp->p_ucred))
266 return EPERM;
268 switch (ap->a_command) {
269 case PIOCBIS:
270 procp->p_stops |= *(unsigned int*)ap->a_data;
271 break;
272 case PIOCBIC:
273 procp->p_stops &= ~*(unsigned int*)ap->a_data;
274 break;
275 case PIOCSFL:
277 * NFLAGS is "non-suser_xxx flags" -- currently, only
278 * PFS_ISUGID ("ignore set u/g id");
280 #define NFLAGS (PF_ISUGID)
281 flags = (unsigned char)*(unsigned int*)ap->a_data;
282 if (flags & NFLAGS && (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)))
283 return error;
284 procp->p_pfsflags = flags;
285 break;
286 case PIOCGFL:
287 *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags;
288 case PIOCSTATUS:
289 psp = (struct procfs_status *)ap->a_data;
290 psp->state = (procp->p_step == 0);
291 psp->flags = procp->p_pfsflags;
292 psp->events = procp->p_stops;
293 if (procp->p_step) {
294 psp->why = procp->p_stype;
295 psp->val = procp->p_xstat;
296 } else {
297 psp->why = psp->val = 0; /* Not defined values */
299 break;
300 case PIOCWAIT:
301 psp = (struct procfs_status *)ap->a_data;
302 if (procp->p_step == 0) {
303 error = tsleep(&procp->p_stype, PCATCH, "piocwait", 0);
304 if (error)
305 return error;
307 psp->state = 1; /* It stopped */
308 psp->flags = procp->p_pfsflags;
309 psp->events = procp->p_stops;
310 psp->why = procp->p_stype; /* why it stopped */
311 psp->val = procp->p_xstat; /* any extra info */
312 break;
313 case PIOCCONT: /* Restart a proc */
314 if (procp->p_step == 0)
315 return EINVAL; /* Can only start a stopped process */
316 if ((signo = *(int*)ap->a_data) != 0) {
317 if (signo >= NSIG || signo <= 0)
318 return EINVAL;
319 ksignal(procp, signo);
321 procp->p_step = 0;
322 wakeup(&procp->p_step);
323 break;
324 default:
325 return (ENOTTY);
327 return 0;
331 * do block mapping for pfsnode (vp).
332 * since we don't use the buffer cache
333 * for procfs this function should never
334 * be called. in any case, it's not clear
335 * what part of the kernel ever makes use
336 * of this function. for sanity, this is the
337 * usual no-op bmap, although returning
338 * (EIO) would be a reasonable alternative.
340 static int
341 linprocfs_bmap(struct vop_bmap_args *ap)
343 if (ap->a_doffsetp != NULL)
344 *ap->a_doffsetp = ap->a_loffset;
345 if (ap->a_runp != NULL)
346 *ap->a_runp = 0;
347 if (ap->a_runb != NULL)
348 *ap->a_runb = 0;
349 return (0);
353 * linprocfs_inactive is called when the pfsnode
354 * is vrele'd and the reference count is about
355 * to go to zero. (vp) will be on the vnode free
356 * list, so to get it back vget() must be
357 * used.
359 * (vp) is locked on entry and must remain locked
360 * on exit.
362 static int
363 linprocfs_inactive(struct vop_inactive_args *ap)
365 /*struct vnode *vp = ap->a_vp;*/
367 return (0);
371 * _reclaim is called when getnewvnode()
372 * wants to make use of an entry on the vnode
373 * free list. at this time the filesystem needs
374 * to free any private data and remove the node
375 * from any private lists.
377 static int
378 linprocfs_reclaim(struct vop_reclaim_args *ap)
380 return (linprocfs_freevp(ap->a_vp));
384 * _print is used for debugging.
385 * just print a readable description
386 * of (vp).
388 static int
389 linprocfs_print(struct vop_print_args *ap)
391 struct pfsnode *pfs = VTOPFS(ap->a_vp);
393 kprintf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n",
394 pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
395 return (0);
399 * generic entry point for unsupported operations
401 static int
402 linprocfs_badop(struct vop_generic_args *ap __unused)
405 return (EIO);
409 * Invent attributes for pfsnode (vp) and store
410 * them in (vap).
411 * Directories lengths are returned as zero since
412 * any real length would require the genuine size
413 * to be computed, and nothing cares anyway.
415 * this is relatively minimal for procfs.
417 static int
418 linprocfs_getattr(struct vop_getattr_args *ap)
420 struct pfsnode *pfs = VTOPFS(ap->a_vp);
421 struct vattr *vap = ap->a_vap;
422 struct proc *procp;
423 int error;
426 * First make sure that the process and its credentials
427 * still exist.
429 switch (pfs->pfs_type) {
430 case Proot:
431 case Pself:
432 procp = 0;
433 break;
435 default:
436 procp = PFIND(pfs->pfs_pid);
437 if (procp == 0 || procp->p_ucred == NULL)
438 return (ENOENT);
441 error = 0;
443 /* start by zeroing out the attributes */
444 VATTR_NULL(vap);
446 /* next do all the common fields */
447 vap->va_type = ap->a_vp->v_type;
448 vap->va_mode = pfs->pfs_mode;
449 vap->va_fileid = pfs->pfs_fileno;
450 vap->va_flags = 0;
451 vap->va_blocksize = PAGE_SIZE;
452 vap->va_bytes = vap->va_size = 0;
453 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
456 * Make all times be current TOD.
457 * It would be possible to get the process start
458 * time from the p_stat structure, but there's
459 * no "file creation" time stamp anyway, and the
460 * p_stat structure is not addressible if u. gets
461 * swapped out for that process.
463 nanotime(&vap->va_ctime);
464 vap->va_atime = vap->va_mtime = vap->va_ctime;
467 * now do the object specific fields
469 * The size could be set from struct reg, but it's hardly
470 * worth the trouble, and it puts some (potentially) machine
471 * dependent data into this machine-independent code. If it
472 * becomes important then this function should break out into
473 * a per-file stat function in the corresponding .c file.
476 vap->va_nlink = 1;
477 if (procp) {
478 vap->va_uid = procp->p_ucred->cr_uid;
479 vap->va_gid = procp->p_ucred->cr_gid;
482 switch (pfs->pfs_type) {
483 case Proot:
484 case Pnet:
485 case Psys:
486 case Psyskernel:
488 * Set nlink to 1 to tell fts(3) we don't actually know.
490 vap->va_nlink = 1;
491 vap->va_uid = 0;
492 vap->va_gid = 0;
493 vap->va_size = vap->va_bytes = DEV_BSIZE;
494 break;
496 case Pself: {
497 char buf[16]; /* should be enough */
498 vap->va_uid = 0;
499 vap->va_gid = 0;
500 vap->va_size = vap->va_bytes =
501 ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
502 break;
505 case Pproc:
506 vap->va_nlink = nproc_targets;
507 vap->va_size = vap->va_bytes = DEV_BSIZE;
508 break;
510 case Pexe: {
511 char *fullpath, *freepath;
512 error = cache_fullpath(procp, &procp->p_textnch, &fullpath, &freepath);
513 /* error = vn_fullpath(procp, NULL, &fullpath, &freepath); */
514 if (error == 0) {
515 vap->va_size = strlen(fullpath);
516 kfree(freepath, M_TEMP);
517 } else {
518 vap->va_size = sizeof("unknown") - 1;
519 error = 0;
521 vap->va_bytes = vap->va_size;
522 break;
524 case Pcwd: {
525 char *fullpath, *freepath;
526 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, &fullpath, &freepath);
527 if (error == 0) {
528 vap->va_size = strlen(fullpath);
529 kfree(freepath, M_TEMP);
530 } else {
531 vap->va_size = sizeof("unknown") - 1;
532 error = 0;
534 vap->va_bytes = vap->va_size;
535 break;
537 case Pprocroot: {
538 struct nchandle *nchp;
539 char *fullpath, *freepath;
540 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
541 error = cache_fullpath(procp, nchp, &fullpath, &freepath);
542 if (error == 0) {
543 vap->va_size = strlen(fullpath);
544 kfree(freepath, M_TEMP);
545 } else {
546 vap->va_size = sizeof("unknown") - 1;
547 error = 0;
549 vap->va_bytes = vap->va_size;
550 break;
552 case Pfd: {
553 if (procp == curproc) {
554 vap->va_size = sizeof("/dev/fd") - 1;
555 error = 0;
556 } else {
557 vap->va_size = sizeof("unknown") - 1;
558 error = 0;
560 vap->va_bytes = vap->va_size;
561 break;
564 case Pmeminfo:
565 case Pcpuinfo:
566 case Pstat:
567 case Puptime:
568 case Pversion:
569 case Ploadavg:
570 case Pnetdev:
571 case Pdevices:
572 case Posrelease:
573 case Postype:
574 case Ppidmax:
575 vap->va_bytes = vap->va_size = 0;
576 vap->va_uid = 0;
577 vap->va_gid = 0;
578 break;
580 case Pmem:
582 * If we denied owner access earlier, then we have to
583 * change the owner to root - otherwise 'ps' and friends
584 * will break even though they are setgid kmem. *SIGH*
586 if (procp->p_flag & P_SUGID)
587 vap->va_uid = 0;
588 else
589 vap->va_uid = procp->p_ucred->cr_uid;
590 break;
592 case Pprocstat:
593 case Pprocstatus:
594 case Pcmdline:
595 case Penviron:
596 case Pmaps:
597 case Pstatm:
598 vap->va_bytes = vap->va_size = 0;
599 /* uid, gid are already set */
600 break;
602 default:
603 panic("linprocfs_getattr");
606 return (error);
609 static int
610 linprocfs_setattr(struct vop_setattr_args *ap)
613 if (ap->a_vap->va_flags != VNOVAL)
614 return (EOPNOTSUPP);
617 * just fake out attribute setting
618 * it's not good to generate an error
619 * return, otherwise things like creat()
620 * will fail when they try to set the
621 * file length to 0. worse, this means
622 * that echo $note > /proc/$pid/note will fail.
625 return (0);
629 * implement access checking.
631 * something very similar to this code is duplicated
632 * throughout the 4bsd kernel and should be moved
633 * into kern/vfs_subr.c sometime.
635 * actually, the check for super-user is slightly
636 * broken since it will allow read access to write-only
637 * objects. this doesn't cause any particular trouble
638 * but does mean that the i/o entry points need to check
639 * that the operation really does make sense.
641 static int
642 linprocfs_access(struct vop_access_args *ap)
644 struct vattr *vap;
645 struct vattr vattr;
646 int error;
649 * If you're the super-user,
650 * you always get access.
652 if (ap->a_cred->cr_uid == 0)
653 return (0);
655 vap = &vattr;
656 error = VOP_GETATTR(ap->a_vp, vap);
657 if (error)
658 return (error);
661 * Access check is based on only one of owner, group, public.
662 * If not owner, then check group. If not a member of the
663 * group, then check public access.
665 if (ap->a_cred->cr_uid != vap->va_uid) {
666 gid_t *gp;
667 int i;
669 ap->a_mode >>= 3;
670 gp = ap->a_cred->cr_groups;
671 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
672 if (vap->va_gid == *gp)
673 goto found;
674 ap->a_mode >>= 3;
675 found:
679 if ((vap->va_mode & ap->a_mode) == ap->a_mode)
680 return (0);
682 return (EACCES);
686 * lookup. this is incredibly complicated in the general case, however
687 * for most pseudo-filesystems very little needs to be done.
689 static int
690 linprocfs_lookup(struct vop_old_lookup_args *ap)
692 struct componentname *cnp = ap->a_cnp;
693 struct vnode **vpp = ap->a_vpp;
694 struct vnode *dvp = ap->a_dvp;
695 char *pname = cnp->cn_nameptr;
696 struct proc_target *pt;
697 pid_t pid;
698 struct pfsnode *pfs;
699 struct proc *p;
700 int i;
701 int error;
703 *vpp = NULL;
705 if (cnp->cn_nameiop == NAMEI_DELETE ||
706 cnp->cn_nameiop == NAMEI_RENAME ||
707 cnp->cn_nameiop == NAMEI_CREATE) {
708 return (EROFS);
711 error = 0;
713 if (cnp->cn_namelen == 1 && *pname == '.') {
714 *vpp = dvp;
715 vref(*vpp);
716 goto out;
719 pfs = VTOPFS(dvp);
720 switch (pfs->pfs_type) {
721 case Psys:
722 if (cnp->cn_flags & CNP_ISDOTDOT) {
723 error = linprocfs_root(dvp->v_mount, vpp);
724 goto out;
726 if (CNEQ(cnp, "kernel", 6)) {
727 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psyskernel);
728 goto out;
730 break;
731 case Pnet:
732 if (cnp->cn_flags & CNP_ISDOTDOT) {
733 error = linprocfs_root(dvp->v_mount, vpp);
734 goto out;
736 if (CNEQ(cnp, "dev", 3)) {
737 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnetdev);
738 goto out;
740 break;
741 case Psyskernel:
742 if (cnp->cn_flags & CNP_ISDOTDOT) {
743 /* XXX: this is wrong, wrong, wrong. */
744 error = linprocfs_root(dvp->v_mount, vpp);
745 goto out;
747 if (CNEQ(cnp, "osrelease", 9)) {
748 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Posrelease);
749 goto out;
751 if (CNEQ(cnp, "ostype", 6)) {
752 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Postype);
753 goto out;
755 if (CNEQ(cnp, "pid_max", 7)) {
756 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ppidmax);
757 goto out;
759 if (CNEQ(cnp, "version", 7)) {
760 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
761 goto out;
763 break;
765 case Proot:
766 if (cnp->cn_flags & CNP_ISDOTDOT)
767 return (EIO);
769 if (CNEQ(cnp, "self", 4)) {
770 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pself);
771 goto out;
773 if (CNEQ(cnp, "meminfo", 7)) {
774 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmeminfo);
775 goto out;
777 if (CNEQ(cnp, "cpuinfo", 7)) {
778 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pcpuinfo);
779 goto out;
781 if (CNEQ(cnp, "stat", 4)) {
782 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pstat);
783 goto out;
785 if (CNEQ(cnp, "uptime", 6)) {
786 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Puptime);
787 goto out;
789 if (CNEQ(cnp, "version", 7)) {
790 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
791 goto out;
793 if (CNEQ(cnp, "loadavg", 7)) {
794 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ploadavg);
795 goto out;
797 if (CNEQ(cnp, "net", 3)) {
798 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnet);
799 goto out;
801 if (CNEQ(cnp, "sys", 3)) {
802 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psys);
803 goto out;
806 pid = atopid(pname, cnp->cn_namelen);
807 if (pid == NO_PID)
808 break;
810 p = PFIND(pid);
811 if (p == 0)
812 break;
814 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
815 break;
817 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
818 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
819 break;
821 error = linprocfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
822 goto out;
824 case Pproc:
825 if (cnp->cn_flags & CNP_ISDOTDOT) {
826 error = linprocfs_root(dvp->v_mount, vpp);
827 goto out;
830 p = PFIND(pfs->pfs_pid);
831 if (p == 0)
832 break;
834 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
835 break;
837 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
838 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
839 break;
841 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
842 if (cnp->cn_namelen == pt->pt_namlen &&
843 bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
844 (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
845 goto found;
847 break;
849 found:
850 error = linprocfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
851 pt->pt_pfstype);
852 goto out;
854 default:
855 error = ENOTDIR;
856 goto out;
859 if (cnp->cn_nameiop == NAMEI_LOOKUP)
860 error = ENOENT;
861 else
862 error = EROFS;
865 * If no error occured *vpp will hold a referenced locked vnode.
866 * dvp was passed to us locked and *vpp must be returned locked
867 * so if dvp != *vpp and CNP_LOCKPARENT is not set, unlock dvp.
869 out:
870 if (error == 0) {
871 if (*vpp != dvp && (cnp->cn_flags & CNP_LOCKPARENT) == 0) {
872 cnp->cn_flags |= CNP_PDIRUNLOCK;
873 vn_unlock(dvp);
876 return (error);
880 * Does this process have a text file?
883 linprocfs_validfile(struct proc *p)
886 return (procfs_findtextvp(p) != NULLVP);
890 * readdir() returns directory entries from pfsnode (vp).
892 * We generate just one directory entry at a time, as it would probably
893 * not pay off to buffer several entries locally to save uiomove calls.
895 * linprocfs_readdir(struct vnode *a_vp, struct uio *a_uio,
896 * struct ucred *a_cred, int *a_eofflag,
897 * int *a_ncookies, off_t **a_cookies)
899 static int
900 linprocfs_readdir(struct vop_readdir_args *ap)
902 struct pfsnode *pfs;
903 int error;
905 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
906 return (EINVAL);
908 pfs = VTOPFS(ap->a_vp);
909 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
910 return (error);
912 switch (pfs->pfs_type) {
913 case Pproc:
915 * This is for the process-specific sub-directories.
916 * all that is needed to is copy out all the entries
917 * from the procent[] table (top of this file).
919 error = linprocfs_readdir_proc(ap);
920 break;
921 case Proot:
923 * This is for the root of the procfs filesystem
924 * what is needed is a special entry for "self"
925 * followed by an entry for each process on allproc
927 error = linprocfs_readdir_root(ap);
928 break;
929 case Pnet:
930 error = linprocfs_readdir_net(ap);
931 break;
932 case Psys:
933 error = linprocfs_readdir_sys(ap);
934 break;
935 case Psyskernel:
936 error = linprocfs_readdir_syskernel(ap);
937 break;
938 default:
939 error = ENOTDIR;
940 break;
942 vn_unlock(ap->a_vp);
944 return (error);
947 static int
948 linprocfs_readdir_proc(struct vop_readdir_args *ap)
950 struct pfsnode *pfs;
951 int error, i, retval;
952 struct proc *p;
953 struct proc_target *pt;
954 struct uio *uio = ap->a_uio;
956 pfs = VTOPFS(ap->a_vp);
957 p = PFIND(pfs->pfs_pid);
958 if (p == NULL)
959 return(0);
960 if (!PRISON_CHECK(ap->a_cred, p->p_ucred))
961 return(0);
963 error = 0;
964 i = uio->uio_offset;
966 for (pt = &proc_targets[i];
967 !error && uio->uio_resid > 0 && i < nproc_targets; pt++, i++) {
968 if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
969 continue;
971 retval = vop_write_dirent(&error, uio,
972 PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype), pt->pt_type,
973 pt->pt_namlen, pt->pt_name);
974 if (retval)
975 break;
978 uio->uio_offset = i;
980 return(error);
983 struct linprocfs_readdir_root_info {
984 int error;
985 int pcnt;
986 int i;
987 struct uio *uio;
988 struct ucred *cred;
992 * Scan the root directory by scanning all process
994 static int linprocfs_readdir_root_callback(struct proc *p, void *data);
996 static int
997 linprocfs_readdir_root(struct vop_readdir_args *ap)
999 struct linprocfs_readdir_root_info info;
1000 struct uio *uio = ap->a_uio;
1001 int res = 0;
1003 info.error = 0;
1004 info.i = uio->uio_offset;
1005 info.pcnt = 0;
1006 info.uio = uio;
1007 info.cred = ap->a_cred;
1009 while (info.pcnt < 11) {
1010 res = linprocfs_readdir_root_callback(NULL, &info);
1011 if (res < 0)
1012 break;
1014 if (res >= 0)
1015 allproc_scan(linprocfs_readdir_root_callback, &info);
1017 uio->uio_offset = info.i;
1018 return(info.error);
1021 static int
1022 linprocfs_readdir_root_callback(struct proc *p, void *data)
1024 struct linprocfs_readdir_root_info *info = data;
1025 int retval;
1026 struct uio *uio = info->uio;
1027 ino_t d_ino;
1028 const char *d_name;
1029 char d_name_pid[20];
1030 size_t d_namlen;
1031 uint8_t d_type;
1033 switch (info->pcnt) {
1034 case 0: /* `.' */
1035 d_ino = PROCFS_FILENO(0, Proot);
1036 d_name = ".";
1037 d_namlen = 1;
1038 d_type = DT_DIR;
1039 break;
1040 case 1: /* `..' */
1041 d_ino = PROCFS_FILENO(0, Proot);
1042 d_name = "..";
1043 d_namlen = 2;
1044 d_type = DT_DIR;
1045 break;
1047 case 2:
1048 d_ino = PROCFS_FILENO(0, Proot);
1049 d_namlen = 4;
1050 d_name = "self";
1051 d_type = DT_LNK;
1052 break;
1054 case 3:
1055 d_ino = PROCFS_FILENO(0, Pmeminfo);
1056 d_namlen = 7;
1057 d_name = "meminfo";
1058 d_type = DT_REG;
1059 break;
1061 case 4:
1062 d_ino = PROCFS_FILENO(0, Pcpuinfo);
1063 d_namlen = 7;
1064 d_name = "cpuinfo";
1065 d_type = DT_REG;
1066 break;
1068 case 5:
1069 d_ino = PROCFS_FILENO(0, Pstat);
1070 d_namlen = 4;
1071 d_name = "stat";
1072 d_type = DT_REG;
1073 break;
1075 case 6:
1076 d_ino = PROCFS_FILENO(0, Puptime);
1077 d_namlen = 6;
1078 d_name = "uptime";
1079 d_type = DT_REG;
1080 break;
1082 case 7:
1083 d_ino = PROCFS_FILENO(0, Pversion);
1084 d_namlen = 7;
1085 d_name = "version";
1086 d_type = DT_REG;
1087 break;
1089 case 8:
1090 d_ino = PROCFS_FILENO(0, Ploadavg);
1091 d_namlen = 7;
1092 d_name = "loadavg";
1093 d_type = DT_REG;
1094 break;
1095 case 9:
1096 d_ino = PROCFS_FILENO(0, Pnet);
1097 d_namlen = 3;
1098 d_name = "net";
1099 d_type = DT_DIR;
1100 break;
1101 case 10:
1102 d_ino = PROCFS_FILENO(0, Psys);
1103 d_namlen = 3;
1104 d_name = "sys";
1105 d_type = DT_DIR;
1106 break;
1107 #if 0
1108 case 11:
1109 d_ino = PROCFS_FILENO(0, Pdevices);
1110 d_namlen = 7;
1111 d_name = "devices";
1112 d_type = DT_REG;
1113 break;
1114 #endif
1115 default:
1117 * Ignore processes that aren't in our prison
1119 if (PRISON_CHECK(info->cred, p->p_ucred) == 0)
1120 return(0);
1123 * Ignore processes that we do not want to be visible.
1125 if (ps_showallprocs == 0 &&
1126 info->cred->cr_uid != 0 &&
1127 info->cred->cr_uid != p->p_ucred->cr_uid) {
1128 return(0);
1132 * Skip processes we have already read (optimization)
1134 if (info->pcnt < info->i) {
1135 ++info->pcnt;
1136 return(0);
1138 d_ino = PROCFS_FILENO(p->p_pid, Pproc);
1139 d_namlen = ksnprintf(d_name_pid, sizeof(d_name_pid),
1140 "%ld", (long)p->p_pid);
1141 d_name = d_name_pid;
1142 d_type = DT_DIR;
1143 break;
1147 * Skip processes we have already read
1149 if (info->pcnt < info->i) {
1150 ++info->pcnt;
1151 return(0);
1153 retval = vop_write_dirent(&info->error, info->uio,
1154 d_ino, d_type, d_namlen, d_name);
1155 if (retval == 0) {
1156 ++info->pcnt; /* iterate proc candidates scanned */
1157 ++info->i; /* iterate entries written */
1159 if (retval || info->error || uio->uio_resid <= 0)
1160 return(-1);
1161 return(0);
1165 * Scan the root directory by scanning all process
1167 static int linprocfs_readdir_net_callback(struct proc *p, void *data);
1169 static int
1170 linprocfs_readdir_net(struct vop_readdir_args *ap)
1172 struct linprocfs_readdir_root_info info;
1173 struct uio *uio = ap->a_uio;
1174 int res;
1176 info.error = 0;
1177 info.i = uio->uio_offset;
1178 info.pcnt = 0;
1179 info.uio = uio;
1180 info.cred = ap->a_cred;
1182 while (info.pcnt < 3) {
1183 res = linprocfs_readdir_net_callback(NULL, &info);
1184 if (res < 0)
1185 break;
1188 uio->uio_offset = info.i;
1189 return(info.error);
1192 static int
1193 linprocfs_readdir_net_callback(struct proc *p, void *data)
1195 struct linprocfs_readdir_root_info *info = data;
1196 int retval;
1197 struct uio *uio = info->uio;
1198 ino_t d_ino;
1199 const char *d_name;
1200 size_t d_namlen;
1201 uint8_t d_type;
1203 switch (info->pcnt) {
1204 case 0: /* `.' */
1205 d_ino = PROCFS_FILENO(0, Pnet);
1206 d_name = ".";
1207 d_namlen = 1;
1208 d_type = DT_DIR;
1209 break;
1210 case 1: /* `..' */
1211 d_ino = PROCFS_FILENO(0, Proot);
1212 d_name = "..";
1213 d_namlen = 2;
1214 d_type = DT_DIR;
1215 break;
1217 case 2:
1218 d_ino = PROCFS_FILENO(0, Pnet);
1219 d_namlen = 3;
1220 d_name = "dev";
1221 d_type = DT_REG;
1222 break;
1223 default:
1224 d_ino = 0;
1225 d_namlen = 0;
1226 d_name = NULL;
1227 d_type = DT_REG;
1228 break;
1232 * Skip processes we have already read
1234 if (info->pcnt < info->i) {
1235 ++info->pcnt;
1236 return(0);
1238 retval = vop_write_dirent(&info->error, info->uio,
1239 d_ino, d_type, d_namlen, d_name);
1240 if (retval == 0) {
1241 ++info->pcnt; /* iterate proc candidates scanned */
1242 ++info->i; /* iterate entries written */
1244 if (retval || info->error || uio->uio_resid <= 0)
1245 return(-1);
1246 return(0);
1256 * Scan the root directory by scanning all process
1258 static int linprocfs_readdir_sys_callback(struct proc *p, void *data);
1260 static int
1261 linprocfs_readdir_sys(struct vop_readdir_args *ap)
1263 struct linprocfs_readdir_root_info info;
1264 struct uio *uio = ap->a_uio;
1265 int res;
1267 info.error = 0;
1268 info.i = uio->uio_offset;
1269 info.pcnt = 0;
1270 info.uio = uio;
1271 info.cred = ap->a_cred;
1273 while (info.pcnt < 3) {
1274 res = linprocfs_readdir_sys_callback(NULL, &info);
1275 if (res < 0)
1276 break;
1279 uio->uio_offset = info.i;
1280 return(info.error);
1283 static int
1284 linprocfs_readdir_sys_callback(struct proc *p, void *data)
1286 struct linprocfs_readdir_root_info *info = data;
1287 int retval;
1288 struct uio *uio = info->uio;
1289 ino_t d_ino;
1290 const char *d_name;
1291 size_t d_namlen;
1292 uint8_t d_type;
1294 switch (info->pcnt) {
1295 case 0: /* `.' */
1296 d_ino = PROCFS_FILENO(0, Psys);
1297 d_name = ".";
1298 d_namlen = 1;
1299 d_type = DT_DIR;
1300 break;
1301 case 1: /* `..' */
1302 d_ino = PROCFS_FILENO(0, Proot);
1303 d_name = "..";
1304 d_namlen = 2;
1305 d_type = DT_DIR;
1306 break;
1308 case 2:
1309 d_ino = PROCFS_FILENO(0, Psyskernel);
1310 d_namlen = 6;
1311 d_name = "kernel";
1312 d_type = DT_DIR;
1313 break;
1314 default:
1315 d_ino = 0;
1316 d_namlen = 0;
1317 d_name = NULL;
1318 d_type = DT_REG;
1319 break;
1323 * Skip processes we have already read
1325 if (info->pcnt < info->i) {
1326 ++info->pcnt;
1327 return(0);
1329 retval = vop_write_dirent(&info->error, info->uio,
1330 d_ino, d_type, d_namlen, d_name);
1331 if (retval == 0) {
1332 ++info->pcnt; /* iterate proc candidates scanned */
1333 ++info->i; /* iterate entries written */
1335 if (retval || info->error || uio->uio_resid <= 0)
1336 return(-1);
1337 return(0);
1345 * Scan the root directory by scanning all process
1347 static int linprocfs_readdir_syskernel_callback(struct proc *p, void *data);
1349 static int
1350 linprocfs_readdir_syskernel(struct vop_readdir_args *ap)
1352 struct linprocfs_readdir_root_info info;
1353 struct uio *uio = ap->a_uio;
1354 int res;
1356 info.error = 0;
1357 info.i = uio->uio_offset;
1358 info.pcnt = 0;
1359 info.uio = uio;
1360 info.cred = ap->a_cred;
1362 while (info.pcnt < 6) {
1363 res = linprocfs_readdir_syskernel_callback(NULL, &info);
1364 if (res < 0)
1365 break;
1368 uio->uio_offset = info.i;
1369 return(info.error);
1372 static int
1373 linprocfs_readdir_syskernel_callback(struct proc *p, void *data)
1375 struct linprocfs_readdir_root_info *info = data;
1376 int retval;
1377 struct uio *uio = info->uio;
1378 ino_t d_ino;
1379 const char *d_name;
1380 size_t d_namlen;
1381 uint8_t d_type;
1383 switch (info->pcnt) {
1384 case 0: /* `.' */
1385 d_ino = PROCFS_FILENO(0, Psyskernel);
1386 d_name = ".";
1387 d_namlen = 1;
1388 d_type = DT_DIR;
1389 break;
1390 case 1: /* `..' */
1391 d_ino = PROCFS_FILENO(0, Psys);
1392 d_name = "..";
1393 d_namlen = 2;
1394 d_type = DT_DIR;
1395 break;
1397 case 2:
1398 d_ino = PROCFS_FILENO(0, Posrelease);
1399 d_namlen = 9;
1400 d_name = "osrelease";
1401 d_type = DT_REG;
1402 break;
1404 case 3:
1405 d_ino = PROCFS_FILENO(0, Postype);
1406 d_namlen = 4;
1407 d_name = "ostype";
1408 d_type = DT_REG;
1409 break;
1411 case 4:
1412 d_ino = PROCFS_FILENO(0, Pversion);
1413 d_namlen = 7;
1414 d_name = "version";
1415 d_type = DT_REG;
1416 break;
1418 case 5:
1419 d_ino = PROCFS_FILENO(0, Ppidmax);
1420 d_namlen = 7;
1421 d_name = "pid_max";
1422 d_type = DT_REG;
1423 break;
1424 default:
1425 d_ino = 0;
1426 d_namlen = 0;
1427 d_name = NULL;
1428 d_type = DT_REG;
1429 break;
1433 * Skip processes we have already read
1435 if (info->pcnt < info->i) {
1436 ++info->pcnt;
1437 return(0);
1439 retval = vop_write_dirent(&info->error, info->uio,
1440 d_ino, d_type, d_namlen, d_name);
1441 if (retval == 0) {
1442 ++info->pcnt; /* iterate proc candidates scanned */
1443 ++info->i; /* iterate entries written */
1445 if (retval || info->error || uio->uio_resid <= 0)
1446 return(-1);
1447 return(0);
1451 * readlink reads the link of `self' or `exe'
1453 static int
1454 linprocfs_readlink(struct vop_readlink_args *ap)
1456 char buf[16]; /* should be enough */
1457 struct proc *procp;
1458 struct vnode *vp = ap->a_vp;
1459 struct nchandle *nchp;
1460 struct pfsnode *pfs = VTOPFS(vp);
1461 char *fullpath, *freepath;
1462 int error, len;
1464 switch (pfs->pfs_type) {
1465 case Pself:
1466 if (pfs->pfs_fileno != PROCFS_FILENO(0, Pself))
1467 return (EINVAL);
1469 len = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
1471 return (uiomove(buf, len, ap->a_uio));
1473 * There _should_ be no way for an entire process to disappear
1474 * from under us...
1476 case Pexe:
1477 procp = PFIND(pfs->pfs_pid);
1478 if (procp == NULL || procp->p_ucred == NULL) {
1479 kprintf("linprocfs_readlink: pid %d disappeared\n",
1480 pfs->pfs_pid);
1481 return (uiomove("unknown", sizeof("unknown") - 1,
1482 ap->a_uio));
1484 error = cache_fullpath(procp, &procp->p_textnch, &fullpath, &freepath);
1485 if (error != 0)
1486 return (uiomove("unknown", sizeof("unknown") - 1,
1487 ap->a_uio));
1488 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1489 kfree(freepath, M_TEMP);
1490 return (error);
1491 case Pcwd:
1492 procp = PFIND(pfs->pfs_pid);
1493 if (procp == NULL || procp->p_ucred == NULL) {
1494 kprintf("linprocfs_readlink: pid %d disappeared\n",
1495 pfs->pfs_pid);
1496 return (uiomove("unknown", sizeof("unknown") - 1,
1497 ap->a_uio));
1499 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, &fullpath, &freepath);
1500 if (error != 0)
1501 return (uiomove("unknown", sizeof("unknown") - 1,
1502 ap->a_uio));
1503 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1504 kfree(freepath, M_TEMP);
1505 return (error);
1506 case Pprocroot:
1507 procp = PFIND(pfs->pfs_pid);
1508 if (procp == NULL || procp->p_ucred == NULL) {
1509 kprintf("linprocfs_readlink: pid %d disappeared\n",
1510 pfs->pfs_pid);
1511 return (uiomove("unknown", sizeof("unknown") - 1,
1512 ap->a_uio));
1514 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
1515 error = cache_fullpath(procp, nchp, &fullpath, &freepath);
1516 if (error != 0)
1517 return (uiomove("unknown", sizeof("unknown") - 1,
1518 ap->a_uio));
1519 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1520 kfree(freepath, M_TEMP);
1521 return (error);
1522 case Pfd:
1523 procp = PFIND(pfs->pfs_pid);
1524 if (procp == NULL || procp->p_ucred == NULL) {
1525 kprintf("linprocfs_readlink: pid %d disappeared\n",
1526 pfs->pfs_pid);
1527 return (uiomove("unknown", sizeof("unknown") - 1,
1528 ap->a_uio));
1530 if (procp == curproc) {
1531 return (uiomove("/dev/fd", sizeof("/dev/fd") - 1,
1532 ap->a_uio));
1533 } else {
1534 return (uiomove("unknown", sizeof("unknown") - 1,
1535 ap->a_uio));
1537 /* notreached */
1538 break;
1539 default:
1540 return (EINVAL);
1545 * convert decimal ascii to pid_t
1547 static pid_t
1548 atopid(const char *b, u_int len)
1550 pid_t p = 0;
1552 while (len--) {
1553 char c = *b++;
1554 if (c < '0' || c > '9')
1555 return (NO_PID);
1556 p = 10 * p + (c - '0');
1557 if (p > PID_MAX)
1558 return (NO_PID);
1561 return (p);