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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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.38 2006/12/20 18:14:41 dillon Exp $
46 * procfs vnode interface
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/signalvar.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/namei.h>
60 #include <sys/dirent.h>
61 #include <sys/malloc.h>
63 #include <vm/vm_zone.h>
64 #include "linprocfs.h"
65 #include <sys/pioctl.h>
67 #include <machine/limits.h>
69 extern struct vnode *procfs_findtextvp (struct proc *);
71 static int linprocfs_access (struct vop_access_args *);
72 static int linprocfs_badop (struct vop_generic_args *);
73 static int linprocfs_bmap (struct vop_bmap_args *);
74 static int linprocfs_close (struct vop_close_args *);
75 static int linprocfs_getattr (struct vop_getattr_args *);
76 static int linprocfs_inactive (struct vop_inactive_args *);
77 static int linprocfs_ioctl (struct vop_ioctl_args *);
78 static int linprocfs_lookup (struct vop_old_lookup_args *);
79 static int linprocfs_open (struct vop_open_args *);
80 static int linprocfs_print (struct vop_print_args *);
81 static int linprocfs_readdir (struct vop_readdir_args *);
82 static int linprocfs_readlink (struct vop_readlink_args *);
83 static int linprocfs_reclaim (struct vop_reclaim_args *);
84 static int linprocfs_setattr (struct vop_setattr_args *);
86 static int linprocfs_readdir_proc(struct vop_readdir_args *);
87 static int linprocfs_readdir_root(struct vop_readdir_args *);
90 * procfs vnode operations.
92 struct vop_ops linprocfs_vnode_vops = {
93 .vop_default = vop_defaultop,
94 .vop_access = linprocfs_access,
95 .vop_advlock = (void *)linprocfs_badop,
96 .vop_bmap = linprocfs_bmap,
97 .vop_close = linprocfs_close,
98 .vop_old_create = (void *)linprocfs_badop,
99 .vop_getattr = linprocfs_getattr,
100 .vop_inactive = linprocfs_inactive,
101 .vop_old_link = (void *)linprocfs_badop,
102 .vop_old_lookup = linprocfs_lookup,
103 .vop_old_mkdir = (void *)linprocfs_badop,
104 .vop_old_mknod = (void *)linprocfs_badop,
105 .vop_open = linprocfs_open,
106 .vop_pathconf = vop_stdpathconf,
107 .vop_print = linprocfs_print,
108 .vop_read = (void *)linprocfs_rw,
109 .vop_readdir = linprocfs_readdir,
110 .vop_readlink = linprocfs_readlink,
111 .vop_reclaim = linprocfs_reclaim,
112 .vop_old_remove = (void *)linprocfs_badop,
113 .vop_old_rename = (void *)linprocfs_badop,
114 .vop_old_rmdir = (void *)linprocfs_badop,
115 .vop_setattr = linprocfs_setattr,
116 .vop_old_symlink = (void *)linprocfs_badop,
117 .vop_write = (void *)linprocfs_rw,
118 .vop_ioctl = linprocfs_ioctl
122 * This is a list of the valid names in the
123 * process-specific sub-directories. It is
124 * used in linprocfs_lookup and linprocfs_readdir
126 static struct proc_target {
131 int (*pt_valid) (struct proc *p);
133 #define N(s) sizeof(s)-1, s
134 /* name type validp */
135 { DT_DIR, N("."), Pproc, NULL },
136 { DT_DIR, N(".."), Proot, NULL },
137 { DT_REG, N("mem"), Pmem, NULL },
138 { DT_LNK, N("exe"), Pexe, NULL },
139 { DT_REG, N("stat"), Pprocstat, NULL },
140 { DT_REG, N("status"), Pprocstatus, NULL },
143 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
145 static pid_t atopid (const char *, u_int);
148 * set things up for doing i/o on
149 * the pfsnode (vp). (vp) is locked
150 * on entry, and should be left locked
153 * for procfs we don't need to do anything
154 * in particular for i/o. all that is done
155 * is to support exclusive open on process
159 linprocfs_open(struct vop_open_args *ap)
161 struct pfsnode *pfs = VTOPFS(ap->a_vp);
164 p2 = PFIND(pfs->pfs_pid);
167 if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred))
170 switch (pfs->pfs_type) {
172 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
173 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
176 if (p_trespass(ap->a_cred, p2->p_ucred))
179 if (ap->a_mode & FWRITE)
180 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
187 return (vop_stdopen(ap));
191 * close the pfsnode (vp) after doing i/o.
192 * (vp) is not locked on entry or exit.
194 * nothing to do for procfs other than undo
195 * any exclusive open flag (see _open above).
198 linprocfs_close(struct vop_close_args *ap)
200 struct pfsnode *pfs = VTOPFS(ap->a_vp);
203 switch (pfs->pfs_type) {
205 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
206 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
208 * If this is the last close, then it checks to see if
209 * the target process has PF_LINGER set in p_pfsflags,
210 * if this is *not* the case, then the process' stop flags
211 * are cleared, and the process is woken up. This is
212 * to help prevent the case where a process has been
213 * told to stop on an event, but then the requesting process
214 * has gone away or forgotten about it.
216 if ((ap->a_vp->v_usecount < 2)
217 && (p = pfind(pfs->pfs_pid))
218 && !(p->p_pfsflags & PF_LINGER)) {
227 return (vop_stdclose(ap));
231 * do an ioctl operation on a pfsnode (vp).
232 * (vp) is not locked on entry or exit.
235 linprocfs_ioctl(struct vop_ioctl_args *ap)
237 struct pfsnode *pfs = VTOPFS(ap->a_vp);
241 struct procfs_status *psp;
244 procp = pfind(pfs->pfs_pid);
249 if (p_trespass(ap->a_cred, procp->p_ucred))
252 switch (ap->a_command) {
254 procp->p_stops |= *(unsigned int*)ap->a_data;
257 procp->p_stops &= ~*(unsigned int*)ap->a_data;
261 * NFLAGS is "non-suser_xxx flags" -- currently, only
262 * PFS_ISUGID ("ignore set u/g id");
264 #define NFLAGS (PF_ISUGID)
265 flags = (unsigned char)*(unsigned int*)ap->a_data;
266 if (flags & NFLAGS && (error = suser_cred(ap->a_cred, 0)))
268 procp->p_pfsflags = flags;
271 *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags;
273 psp = (struct procfs_status *)ap->a_data;
274 psp->state = (procp->p_step == 0);
275 psp->flags = procp->p_pfsflags;
276 psp->events = procp->p_stops;
278 psp->why = procp->p_stype;
279 psp->val = procp->p_xstat;
281 psp->why = psp->val = 0; /* Not defined values */
285 psp = (struct procfs_status *)ap->a_data;
286 if (procp->p_step == 0) {
287 error = tsleep(&procp->p_stype, PCATCH, "piocwait", 0);
291 psp->state = 1; /* It stopped */
292 psp->flags = procp->p_pfsflags;
293 psp->events = procp->p_stops;
294 psp->why = procp->p_stype; /* why it stopped */
295 psp->val = procp->p_xstat; /* any extra info */
297 case PIOCCONT: /* Restart a proc */
298 if (procp->p_step == 0)
299 return EINVAL; /* Can only start a stopped process */
300 if ((signo = *(int*)ap->a_data) != 0) {
301 if (signo >= NSIG || signo <= 0)
303 ksignal(procp, signo);
306 wakeup(&procp->p_step);
315 * do block mapping for pfsnode (vp).
316 * since we don't use the buffer cache
317 * for procfs this function should never
318 * be called. in any case, it's not clear
319 * what part of the kernel ever makes use
320 * of this function. for sanity, this is the
321 * usual no-op bmap, although returning
322 * (EIO) would be a reasonable alternative.
325 linprocfs_bmap(struct vop_bmap_args *ap)
327 if (ap->a_vpp != NULL)
328 *ap->a_vpp = ap->a_vp;
329 if (ap->a_doffsetp != NULL)
330 *ap->a_doffsetp = ap->a_loffset;
331 if (ap->a_runp != NULL)
333 if (ap->a_runb != NULL)
339 * linprocfs_inactive is called when the pfsnode
340 * is vrele'd and the reference count is about
341 * to go to zero. (vp) will be on the vnode free
342 * list, so to get it back vget() must be
345 * (vp) is locked on entry and must remain locked
349 linprocfs_inactive(struct vop_inactive_args *ap)
351 /*struct vnode *vp = ap->a_vp;*/
357 * _reclaim is called when getnewvnode()
358 * wants to make use of an entry on the vnode
359 * free list. at this time the filesystem needs
360 * to free any private data and remove the node
361 * from any private lists.
364 linprocfs_reclaim(struct vop_reclaim_args *ap)
366 return (linprocfs_freevp(ap->a_vp));
370 * _print is used for debugging.
371 * just print a readable description
375 linprocfs_print(struct vop_print_args *ap)
377 struct pfsnode *pfs = VTOPFS(ap->a_vp);
379 printf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n",
380 pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
385 * generic entry point for unsupported operations
388 linprocfs_badop(struct vop_generic_args *ap __unused)
395 * Invent attributes for pfsnode (vp) and store
397 * Directories lengths are returned as zero since
398 * any real length would require the genuine size
399 * to be computed, and nothing cares anyway.
401 * this is relatively minimal for procfs.
404 linprocfs_getattr(struct vop_getattr_args *ap)
406 struct pfsnode *pfs = VTOPFS(ap->a_vp);
407 struct vattr *vap = ap->a_vap;
412 * First make sure that the process and its credentials
415 switch (pfs->pfs_type) {
422 procp = PFIND(pfs->pfs_pid);
423 if (procp == 0 || procp->p_ucred == NULL)
429 /* start by zeroing out the attributes */
432 /* next do all the common fields */
433 vap->va_type = ap->a_vp->v_type;
434 vap->va_mode = pfs->pfs_mode;
435 vap->va_fileid = pfs->pfs_fileno;
437 vap->va_blocksize = PAGE_SIZE;
438 vap->va_bytes = vap->va_size = 0;
439 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
442 * Make all times be current TOD.
443 * It would be possible to get the process start
444 * time from the p_stat structure, but there's
445 * no "file creation" time stamp anyway, and the
446 * p_stat structure is not addressible if u. gets
447 * swapped out for that process.
449 nanotime(&vap->va_ctime);
450 vap->va_atime = vap->va_mtime = vap->va_ctime;
453 * now do the object specific fields
455 * The size could be set from struct reg, but it's hardly
456 * worth the trouble, and it puts some (potentially) machine
457 * dependent data into this machine-independent code. If it
458 * becomes important then this function should break out into
459 * a per-file stat function in the corresponding .c file.
464 vap->va_uid = procp->p_ucred->cr_uid;
465 vap->va_gid = procp->p_ucred->cr_gid;
468 switch (pfs->pfs_type) {
471 * Set nlink to 1 to tell fts(3) we don't actually know.
476 vap->va_size = vap->va_bytes = DEV_BSIZE;
480 char buf[16]; /* should be enough */
483 vap->va_size = vap->va_bytes =
484 ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
489 vap->va_nlink = nproc_targets;
490 vap->va_size = vap->va_bytes = DEV_BSIZE;
494 char *fullpath, *freepath;
495 error = vn_fullpath(procp, NULL, &fullpath, &freepath);
497 vap->va_size = strlen(fullpath);
498 kfree(freepath, M_TEMP);
500 vap->va_size = sizeof("unknown") - 1;
503 vap->va_bytes = vap->va_size;
513 vap->va_bytes = vap->va_size = 0;
520 * If we denied owner access earlier, then we have to
521 * change the owner to root - otherwise 'ps' and friends
522 * will break even though they are setgid kmem. *SIGH*
524 if (procp->p_flag & P_SUGID)
527 vap->va_uid = procp->p_ucred->cr_uid;
532 vap->va_bytes = vap->va_size = 0;
533 /* uid, gid are already set */
537 panic("linprocfs_getattr");
544 linprocfs_setattr(struct vop_setattr_args *ap)
547 if (ap->a_vap->va_flags != VNOVAL)
551 * just fake out attribute setting
552 * it's not good to generate an error
553 * return, otherwise things like creat()
554 * will fail when they try to set the
555 * file length to 0. worse, this means
556 * that echo $note > /proc/$pid/note will fail.
563 * implement access checking.
565 * something very similar to this code is duplicated
566 * throughout the 4bsd kernel and should be moved
567 * into kern/vfs_subr.c sometime.
569 * actually, the check for super-user is slightly
570 * broken since it will allow read access to write-only
571 * objects. this doesn't cause any particular trouble
572 * but does mean that the i/o entry points need to check
573 * that the operation really does make sense.
576 linprocfs_access(struct vop_access_args *ap)
583 * If you're the super-user,
584 * you always get access.
586 if (ap->a_cred->cr_uid == 0)
590 error = VOP_GETATTR(ap->a_vp, vap);
595 * Access check is based on only one of owner, group, public.
596 * If not owner, then check group. If not a member of the
597 * group, then check public access.
599 if (ap->a_cred->cr_uid != vap->va_uid) {
604 gp = ap->a_cred->cr_groups;
605 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
606 if (vap->va_gid == *gp)
613 if ((vap->va_mode & ap->a_mode) == ap->a_mode)
620 * lookup. this is incredibly complicated in the general case, however
621 * for most pseudo-filesystems very little needs to be done.
624 linprocfs_lookup(struct vop_old_lookup_args *ap)
626 struct componentname *cnp = ap->a_cnp;
627 struct vnode **vpp = ap->a_vpp;
628 struct vnode *dvp = ap->a_dvp;
629 char *pname = cnp->cn_nameptr;
630 struct proc_target *pt;
639 if (cnp->cn_nameiop == NAMEI_DELETE ||
640 cnp->cn_nameiop == NAMEI_RENAME ||
641 cnp->cn_nameiop == NAMEI_CREATE) {
647 if (cnp->cn_namelen == 1 && *pname == '.') {
654 switch (pfs->pfs_type) {
656 if (cnp->cn_flags & CNP_ISDOTDOT)
659 if (CNEQ(cnp, "self", 4)) {
660 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pself);
663 if (CNEQ(cnp, "meminfo", 7)) {
664 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmeminfo);
667 if (CNEQ(cnp, "cpuinfo", 7)) {
668 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pcpuinfo);
671 if (CNEQ(cnp, "stat", 4)) {
672 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pstat);
675 if (CNEQ(cnp, "uptime", 6)) {
676 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Puptime);
679 if (CNEQ(cnp, "version", 7)) {
680 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
683 if (CNEQ(cnp, "loadavg", 7)) {
684 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ploadavg);
688 pid = atopid(pname, cnp->cn_namelen);
696 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
699 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
700 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
703 error = linprocfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
707 if (cnp->cn_flags & CNP_ISDOTDOT) {
708 error = linprocfs_root(dvp->v_mount, vpp);
712 p = PFIND(pfs->pfs_pid);
716 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
719 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
720 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
723 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
724 if (cnp->cn_namelen == pt->pt_namlen &&
725 bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
726 (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
732 error = linprocfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
741 if (cnp->cn_nameiop == NAMEI_LOOKUP)
747 * If no error occured *vpp will hold a referenced locked vnode.
748 * dvp was passed to us locked and *vpp must be returned locked
749 * so if dvp != *vpp and CNP_LOCKPARENT is not set, unlock dvp.
753 if (*vpp != dvp && (cnp->cn_flags & CNP_LOCKPARENT) == 0) {
754 cnp->cn_flags |= CNP_PDIRUNLOCK;
762 * Does this process have a text file?
765 linprocfs_validfile(struct proc *p)
768 return (procfs_findtextvp(p) != NULLVP);
772 * readdir() returns directory entries from pfsnode (vp).
774 * We generate just one directory entry at a time, as it would probably
775 * not pay off to buffer several entries locally to save uiomove calls.
777 * linprocfs_readdir(struct vnode *a_vp, struct uio *a_uio,
778 * struct ucred *a_cred, int *a_eofflag,
779 * int *a_ncookies, u_long **a_cookies)
782 linprocfs_readdir(struct vop_readdir_args *ap)
787 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
790 pfs = VTOPFS(ap->a_vp);
791 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
794 switch (pfs->pfs_type) {
797 * This is for the process-specific sub-directories.
798 * all that is needed to is copy out all the entries
799 * from the procent[] table (top of this file).
801 error = linprocfs_readdir_proc(ap);
805 * This is for the root of the procfs filesystem
806 * what is needed is a special entry for "self"
807 * followed by an entry for each process on allproc
809 error = linprocfs_readdir_root(ap);
821 linprocfs_readdir_proc(struct vop_readdir_args *ap)
824 int error, i, retval;
826 struct proc_target *pt;
827 struct uio *uio = ap->a_uio;
829 pfs = VTOPFS(ap->a_vp);
830 p = PFIND(pfs->pfs_pid);
833 if (!PRISON_CHECK(ap->a_cred, p->p_ucred))
839 for (pt = &proc_targets[i];
840 !error && uio->uio_resid > 0 && i < nproc_targets; pt++, i++) {
841 if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
844 retval = vop_write_dirent(&error, uio,
845 PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype), pt->pt_type,
846 pt->pt_namlen, pt->pt_name);
856 struct linprocfs_readdir_root_info {
865 * Scan the root directory by scanning all process
867 static int linprocfs_readdir_root_callback(struct proc *p, void *data);
870 linprocfs_readdir_root(struct vop_readdir_args *ap)
872 struct linprocfs_readdir_root_info info;
873 struct uio *uio = ap->a_uio;
877 info.i = uio->uio_offset;
880 info.cred = ap->a_cred;
882 while (info.pcnt < 9) {
883 res = linprocfs_readdir_root_callback(NULL, &info);
888 allproc_scan(linprocfs_readdir_root_callback, &info);
890 uio->uio_offset = info.i;
895 linprocfs_readdir_root_callback(struct proc *p, void *data)
897 struct linprocfs_readdir_root_info *info = data;
899 struct uio *uio = info->uio;
906 switch (info->pcnt) {
908 d_ino = PROCFS_FILENO(0, Proot);
914 d_ino = PROCFS_FILENO(0, Proot);
921 d_ino = PROCFS_FILENO(0, Proot);
928 d_ino = PROCFS_FILENO(0, Pmeminfo);
935 d_ino = PROCFS_FILENO(0, Pcpuinfo);
942 d_ino = PROCFS_FILENO(0, Pstat);
949 d_ino = PROCFS_FILENO(0, Puptime);
956 d_ino = PROCFS_FILENO(0, Pversion);
963 d_ino = PROCFS_FILENO(0, Ploadavg);
971 * Ignore processes that aren't in our prison
973 if (PRISON_CHECK(info->cred, p->p_ucred) == 0)
977 * Ignore processes that we do not want to be visible.
979 if (ps_showallprocs == 0 &&
980 info->cred->cr_uid != 0 &&
981 info->cred->cr_uid != p->p_ucred->cr_uid) {
986 * Skip processes we have already read (optimization)
988 if (info->pcnt < info->i) {
992 d_ino = PROCFS_FILENO(p->p_pid, Pproc);
993 d_namlen = ksnprintf(d_name_pid, sizeof(d_name_pid),
994 "%ld", (long)p->p_pid);
1001 * Skip processes we have already read
1003 if (info->pcnt < info->i) {
1007 retval = vop_write_dirent(&info->error, info->uio,
1008 d_ino, d_type, d_namlen, d_name);
1010 ++info->pcnt; /* iterate proc candidates scanned */
1011 ++info->i; /* iterate entries written */
1013 if (retval || info->error || uio->uio_resid <= 0)
1019 * readlink reads the link of `self' or `exe'
1022 linprocfs_readlink(struct vop_readlink_args *ap)
1024 char buf[16]; /* should be enough */
1026 struct vnode *vp = ap->a_vp;
1027 struct pfsnode *pfs = VTOPFS(vp);
1028 char *fullpath, *freepath;
1031 switch (pfs->pfs_type) {
1033 if (pfs->pfs_fileno != PROCFS_FILENO(0, Pself))
1036 len = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
1038 return (uiomove(buf, len, ap->a_uio));
1040 * There _should_ be no way for an entire process to disappear
1044 procp = PFIND(pfs->pfs_pid);
1045 if (procp == NULL || procp->p_ucred == NULL) {
1046 printf("linprocfs_readlink: pid %d disappeared\n",
1048 return (uiomove("unknown", sizeof("unknown") - 1,
1051 error = vn_fullpath(procp, NULL, &fullpath, &freepath);
1053 return (uiomove("unknown", sizeof("unknown") - 1,
1055 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1056 kfree(freepath, M_TEMP);
1064 * convert decimal ascii to pid_t
1067 atopid(const char *b, u_int len)
1073 if (c < '0' || c > '9')
1075 p = 10 * p + (c - '0');