sbin/hammer: Directly access volume in volume list
[dragonfly.git] / sys / emulation / linux / i386 / linprocfs / linprocfs_vnops.c
blob3a4171d4f68f32c44eca489d081f45ad5529440c
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 $
45 * procfs vnode interface
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/time.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/fcntl.h>
54 #include <sys/proc.h>
55 #include <sys/priv.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>
62 #include <sys/reg.h>
63 #include <sys/jail.h>
64 #include <vm/vm_zone.h>
65 #include "linprocfs.h"
66 #include <sys/pioctl.h>
67 #include <sys/spinlock2.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 = NELEM(proc_targets);
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;
179 int error;
181 p2 = linprocfs_pfind(pfs->pfs_pid);
182 if (p2 == NULL) {
183 error = ENOENT;
184 } else if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred)) {
185 error = ENOENT;
186 } else {
187 error = 0;
189 switch (pfs->pfs_type) {
190 case Pmem:
191 if (((pfs->pfs_flags & FWRITE) &&
192 (ap->a_mode & O_EXCL)) ||
193 ((pfs->pfs_flags & O_EXCL) &&
194 (ap->a_mode & FWRITE))) {
195 error = EBUSY;
196 break;
199 if (p_trespass(ap->a_cred, p2->p_ucred)) {
200 error = EPERM;
201 break;
203 if (ap->a_mode & FWRITE)
204 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
205 break;
206 default:
207 break;
210 if (error == 0)
211 error = vop_stdopen(ap);
212 if (p2)
213 PRELE(p2);
214 return error;
218 * close the pfsnode (vp) after doing i/o.
219 * (vp) is not locked on entry or exit.
221 * nothing to do for procfs other than undo
222 * any exclusive open flag (see _open above).
224 static int
225 linprocfs_close(struct vop_close_args *ap)
227 struct pfsnode *pfs = VTOPFS(ap->a_vp);
228 struct proc *p;
230 switch (pfs->pfs_type) {
231 case Pmem:
232 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
233 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
235 * If this is the last close, then it checks to see if
236 * the target process has PF_LINGER set in p_pfsflags,
237 * if this is *not* the case, then the process' stop flags
238 * are cleared, and the process is woken up. This is
239 * to help prevent the case where a process has been
240 * told to stop on an event, but then the requesting process
241 * has gone away or forgotten about it.
243 p = NULL;
244 if ((ap->a_vp->v_opencount < 2)
245 && (p = linprocfs_pfind(pfs->pfs_pid))
246 && !(p->p_pfsflags & PF_LINGER)) {
247 spin_lock(&p->p_spin);
248 p->p_stops = 0;
249 p->p_step = 0;
250 spin_unlock(&p->p_spin);
251 wakeup(&p->p_stype);
253 if (p)
254 PRELE(p);
255 break;
256 default:
257 break;
259 return (vop_stdclose(ap));
263 * do an ioctl operation on a pfsnode (vp).
264 * (vp) is not locked on entry or exit.
266 static int
267 linprocfs_ioctl(struct vop_ioctl_args *ap)
269 struct pfsnode *pfs = VTOPFS(ap->a_vp);
270 struct proc *procp;
271 int error;
272 int signo;
273 struct procfs_status *psp;
274 unsigned char flags;
276 procp = linprocfs_pfind(pfs->pfs_pid);
277 if (procp == NULL)
278 return ENOTTY;
280 if (p_trespass(ap->a_cred, procp->p_ucred)) {
281 error = EPERM;
282 goto done;
285 switch (ap->a_command) {
286 case PIOCBIS:
287 procp->p_stops |= *(unsigned int*)ap->a_data;
288 break;
289 case PIOCBIC:
290 procp->p_stops &= ~*(unsigned int*)ap->a_data;
291 break;
292 case PIOCSFL:
294 * NFLAGS is "non-suser_xxx flags" -- currently, only
295 * PFS_ISUGID ("ignore set u/g id");
297 #define NFLAGS (PF_ISUGID)
298 flags = (unsigned char)*(unsigned int*)ap->a_data;
299 if (flags & NFLAGS && (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)))
300 goto done;
301 procp->p_pfsflags = flags;
302 break;
303 case PIOCGFL:
304 *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags;
305 case PIOCSTATUS:
306 psp = (struct procfs_status *)ap->a_data;
307 psp->flags = procp->p_pfsflags;
308 psp->events = procp->p_stops;
309 spin_lock(&procp->p_spin);
310 if (procp->p_step) {
311 psp->state = 0;
312 psp->why = procp->p_stype;
313 psp->val = procp->p_xstat;
314 spin_unlock(&procp->p_spin);
315 } else {
316 psp->state = 1;
317 spin_unlock(&procp->p_spin);
318 psp->why = 0; /* Not defined values */
319 psp->val = 0; /* Not defined values */
321 break;
322 case PIOCWAIT:
323 psp = (struct procfs_status *)ap->a_data;
324 spin_lock(&procp->p_spin);
325 if (procp->p_step == 0) {
326 tsleep_interlock(&procp->p_stype, PCATCH);
327 spin_unlock(&procp->p_spin);
328 if (procp->p_stops == 0) {
329 error = EINVAL;
330 goto done;
332 if (procp->p_flags & P_POSTEXIT) {
333 error = EINVAL;
334 goto done;
336 if (procp->p_flags & P_INEXEC) {
337 error = EAGAIN;
338 goto done;
340 error = tsleep(&procp->p_stype, PCATCH | PINTERLOCKED,
341 "piocwait", 0);
342 if (error)
343 goto done;
344 } else {
345 spin_unlock(&procp->p_spin);
347 psp->state = 1; /* It stopped */
348 psp->flags = procp->p_pfsflags;
349 psp->events = procp->p_stops;
350 psp->why = procp->p_stype; /* why it stopped */
351 psp->val = procp->p_xstat; /* any extra info */
352 break;
353 case PIOCCONT: /* Restart a proc */
354 if (procp->p_step == 0) {
355 error = EINVAL; /* Can only start a stopped process */
356 goto done;
358 if ((signo = *(int*)ap->a_data) != 0) {
359 if (signo >= NSIG || signo <= 0) {
360 error = EINVAL;
361 goto done;
363 ksignal(procp, signo);
365 procp->p_step = 0;
366 wakeup(&procp->p_step);
367 break;
368 default:
369 error = ENOTTY;
370 goto done;
372 error = 0;
373 done:
374 if (procp)
375 PRELE(procp);
376 return error;
380 * do block mapping for pfsnode (vp).
381 * since we don't use the buffer cache
382 * for procfs this function should never
383 * be called. in any case, it's not clear
384 * what part of the kernel ever makes use
385 * of this function. for sanity, this is the
386 * usual no-op bmap, although returning
387 * (EIO) would be a reasonable alternative.
389 static int
390 linprocfs_bmap(struct vop_bmap_args *ap)
392 if (ap->a_doffsetp != NULL)
393 *ap->a_doffsetp = ap->a_loffset;
394 if (ap->a_runp != NULL)
395 *ap->a_runp = 0;
396 if (ap->a_runb != NULL)
397 *ap->a_runb = 0;
398 return (0);
402 * linprocfs_inactive is called when the pfsnode
403 * is vrele'd and the reference count is about
404 * to go to zero. (vp) will be on the vnode free
405 * list, so to get it back vget() must be
406 * used.
408 * (vp) is locked on entry and must remain locked
409 * on exit.
411 static int
412 linprocfs_inactive(struct vop_inactive_args *ap)
414 struct pfsnode *pfs = VTOPFS(ap->a_vp);
416 if (pfs->pfs_pid & PFS_DEAD)
417 vrecycle(ap->a_vp);
418 return (0);
422 * _reclaim is called when getnewvnode()
423 * wants to make use of an entry on the vnode
424 * free list. at this time the filesystem needs
425 * to free any private data and remove the node
426 * from any private lists.
428 static int
429 linprocfs_reclaim(struct vop_reclaim_args *ap)
431 return (linprocfs_freevp(ap->a_vp));
435 * _print is used for debugging.
436 * just print a readable description
437 * of (vp).
439 static int
440 linprocfs_print(struct vop_print_args *ap)
442 struct pfsnode *pfs = VTOPFS(ap->a_vp);
444 kprintf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n",
445 pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
446 return (0);
450 * generic entry point for unsupported operations
452 static int
453 linprocfs_badop(struct vop_generic_args *ap __unused)
456 return (EIO);
460 * Invent attributes for pfsnode (vp) and store
461 * them in (vap).
462 * Directories lengths are returned as zero since
463 * any real length would require the genuine size
464 * to be computed, and nothing cares anyway.
466 * this is relatively minimal for procfs.
468 static int
469 linprocfs_getattr(struct vop_getattr_args *ap)
471 struct pfsnode *pfs = VTOPFS(ap->a_vp);
472 struct vattr *vap = ap->a_vap;
473 struct proc *procp;
474 int error;
477 * First make sure that the process and its credentials
478 * still exist.
480 switch (pfs->pfs_type) {
481 case Proot:
482 case Pself:
483 procp = NULL;
484 break;
486 default:
487 procp = linprocfs_pfind(pfs->pfs_pid);
488 if (procp == NULL || procp->p_ucred == NULL) {
489 error = ENOENT;
490 goto done;
494 error = 0;
496 /* start by zeroing out the attributes */
497 VATTR_NULL(vap);
499 /* next do all the common fields */
500 vap->va_type = ap->a_vp->v_type;
501 vap->va_mode = pfs->pfs_mode;
502 vap->va_fileid = pfs->pfs_fileno;
503 vap->va_flags = 0;
504 vap->va_blocksize = PAGE_SIZE;
505 vap->va_bytes = vap->va_size = 0;
506 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
509 * Make all times be current TOD.
510 * It would be possible to get the process start
511 * time from the p_stat structure, but there's
512 * no "file creation" time stamp anyway, and the
513 * p_stat structure is not addressible if u. gets
514 * swapped out for that process.
516 nanotime(&vap->va_ctime);
517 vap->va_atime = vap->va_mtime = vap->va_ctime;
520 * now do the object specific fields
522 * The size could be set from struct reg, but it's hardly
523 * worth the trouble, and it puts some (potentially) machine
524 * dependent data into this machine-independent code. If it
525 * becomes important then this function should break out into
526 * a per-file stat function in the corresponding .c file.
529 vap->va_nlink = 1;
530 if (procp) {
531 vap->va_uid = procp->p_ucred->cr_uid;
532 vap->va_gid = procp->p_ucred->cr_gid;
535 switch (pfs->pfs_type) {
536 case Proot:
537 case Pnet:
538 case Psys:
539 case Psyskernel:
541 * Set nlink to 1 to tell fts(3) we don't actually know.
543 vap->va_nlink = 1;
544 vap->va_uid = 0;
545 vap->va_gid = 0;
546 vap->va_size = vap->va_bytes = DEV_BSIZE;
547 break;
549 case Pself: {
550 char buf[16]; /* should be enough */
551 vap->va_uid = 0;
552 vap->va_gid = 0;
553 vap->va_size = vap->va_bytes =
554 ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
555 break;
558 case Pproc:
559 vap->va_nlink = nproc_targets;
560 vap->va_size = vap->va_bytes = DEV_BSIZE;
561 break;
563 case Pexe: {
564 char *fullpath, *freepath;
565 error = cache_fullpath(procp, &procp->p_textnch, NULL,
566 &fullpath, &freepath, 0);
567 /* error = vn_fullpath(procp, NULL, &fullpath, &freepath); */
568 if (error == 0) {
569 vap->va_size = strlen(fullpath);
570 kfree(freepath, M_TEMP);
571 } else {
572 vap->va_size = sizeof("unknown") - 1;
573 error = 0;
575 vap->va_bytes = vap->va_size;
576 break;
578 case Pcwd: {
579 char *fullpath, *freepath;
580 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, NULL,
581 &fullpath, &freepath, 0);
582 if (error == 0) {
583 vap->va_size = strlen(fullpath);
584 kfree(freepath, M_TEMP);
585 } else {
586 vap->va_size = sizeof("unknown") - 1;
587 error = 0;
589 vap->va_bytes = vap->va_size;
590 break;
592 case Pprocroot: {
593 struct nchandle *nchp;
594 char *fullpath, *freepath;
595 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
596 error = cache_fullpath(procp, nchp, NULL,
597 &fullpath, &freepath, 0);
598 if (error == 0) {
599 vap->va_size = strlen(fullpath);
600 kfree(freepath, M_TEMP);
601 } else {
602 vap->va_size = sizeof("unknown") - 1;
603 error = 0;
605 vap->va_bytes = vap->va_size;
606 break;
608 case Pfd: {
609 if (procp == curproc) {
610 vap->va_size = sizeof("/dev/fd") - 1;
611 error = 0;
612 } else {
613 vap->va_size = sizeof("unknown") - 1;
614 error = 0;
616 vap->va_bytes = vap->va_size;
617 break;
620 case Pmeminfo:
621 case Pcpuinfo:
622 case Pmounts:
623 case Pstat:
624 case Puptime:
625 case Pversion:
626 case Ploadavg:
627 case Pnetdev:
628 case Pdevices:
629 case Posrelease:
630 case Postype:
631 case Ppidmax:
632 vap->va_bytes = vap->va_size = 0;
633 vap->va_uid = 0;
634 vap->va_gid = 0;
635 break;
637 case Pmem:
639 * If we denied owner access earlier, then we have to
640 * change the owner to root - otherwise 'ps' and friends
641 * will break even though they are setgid kmem. *SIGH*
643 if (procp->p_flags & P_SUGID)
644 vap->va_uid = 0;
645 else
646 vap->va_uid = procp->p_ucred->cr_uid;
647 break;
649 case Pprocstat:
650 case Pprocstatus:
651 case Pcmdline:
652 case Penviron:
653 case Pmaps:
654 case Pstatm:
655 vap->va_bytes = vap->va_size = 0;
656 /* uid, gid are already set */
657 break;
659 default:
660 panic("linprocfs_getattr");
662 done:
663 if (procp)
664 PRELE(procp);
665 return (error);
668 static int
669 linprocfs_setattr(struct vop_setattr_args *ap)
672 if (ap->a_vap->va_flags != VNOVAL)
673 return (EOPNOTSUPP);
676 * just fake out attribute setting
677 * it's not good to generate an error
678 * return, otherwise things like creat()
679 * will fail when they try to set the
680 * file length to 0. worse, this means
681 * that echo $note > /proc/$pid/note will fail.
684 return (0);
688 * implement access checking.
690 * something very similar to this code is duplicated
691 * throughout the 4bsd kernel and should be moved
692 * into kern/vfs_subr.c sometime.
694 * actually, the check for super-user is slightly
695 * broken since it will allow read access to write-only
696 * objects. this doesn't cause any particular trouble
697 * but does mean that the i/o entry points need to check
698 * that the operation really does make sense.
700 static int
701 linprocfs_access(struct vop_access_args *ap)
703 struct vattr *vap;
704 struct vattr vattr;
705 int error;
708 * If you're the super-user,
709 * you always get access.
711 if (ap->a_cred->cr_uid == 0)
712 return (0);
714 vap = &vattr;
715 error = VOP_GETATTR(ap->a_vp, vap);
716 if (error)
717 return (error);
720 * Access check is based on only one of owner, group, public.
721 * If not owner, then check group. If not a member of the
722 * group, then check public access.
724 if (ap->a_cred->cr_uid != vap->va_uid) {
725 gid_t *gp;
726 int i;
728 ap->a_mode >>= 3;
729 gp = ap->a_cred->cr_groups;
730 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
731 if (vap->va_gid == *gp)
732 goto found;
733 ap->a_mode >>= 3;
734 found:
738 if ((vap->va_mode & ap->a_mode) == ap->a_mode)
739 return (0);
741 return (EACCES);
745 * lookup. this is incredibly complicated in the general case, however
746 * for most pseudo-filesystems very little needs to be done.
748 static int
749 linprocfs_lookup(struct vop_old_lookup_args *ap)
751 struct componentname *cnp = ap->a_cnp;
752 struct vnode **vpp = ap->a_vpp;
753 struct vnode *dvp = ap->a_dvp;
754 char *pname = cnp->cn_nameptr;
755 struct proc_target *pt;
756 pid_t pid;
757 struct pfsnode *pfs;
758 struct proc *p;
759 int i;
760 int error;
762 *vpp = NULL;
763 p = NULL;
765 if (cnp->cn_nameiop == NAMEI_DELETE ||
766 cnp->cn_nameiop == NAMEI_RENAME ||
767 cnp->cn_nameiop == NAMEI_CREATE) {
768 return (EROFS);
771 error = 0;
773 if (cnp->cn_namelen == 1 && *pname == '.') {
774 *vpp = dvp;
775 vref(*vpp);
776 goto out;
779 pfs = VTOPFS(dvp);
780 switch (pfs->pfs_type) {
781 case Psys:
782 if (cnp->cn_flags & CNP_ISDOTDOT) {
783 error = linprocfs_root(dvp->v_mount, vpp);
784 goto out;
786 if (CNEQ(cnp, "kernel", 6)) {
787 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psyskernel);
788 goto out;
790 break;
791 case Pnet:
792 if (cnp->cn_flags & CNP_ISDOTDOT) {
793 error = linprocfs_root(dvp->v_mount, vpp);
794 goto out;
796 if (CNEQ(cnp, "dev", 3)) {
797 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnetdev);
798 goto out;
800 break;
801 case Psyskernel:
802 if (cnp->cn_flags & CNP_ISDOTDOT) {
803 /* XXX: this is wrong, wrong, wrong. */
804 error = linprocfs_root(dvp->v_mount, vpp);
805 goto out;
807 if (CNEQ(cnp, "osrelease", 9)) {
808 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Posrelease);
809 goto out;
811 if (CNEQ(cnp, "ostype", 6)) {
812 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Postype);
813 goto out;
815 if (CNEQ(cnp, "pid_max", 7)) {
816 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ppidmax);
817 goto out;
819 if (CNEQ(cnp, "version", 7)) {
820 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
821 goto out;
823 break;
825 case Proot:
826 if (cnp->cn_flags & CNP_ISDOTDOT)
827 return (EIO);
829 if (CNEQ(cnp, "self", 4)) {
830 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pself);
831 goto out;
833 if (CNEQ(cnp, "meminfo", 7)) {
834 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmeminfo);
835 goto out;
837 if (CNEQ(cnp, "cpuinfo", 7)) {
838 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pcpuinfo);
839 goto out;
841 if (CNEQ(cnp, "mounts", 6)) {
842 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmounts);
843 goto out;
845 if (CNEQ(cnp, "stat", 4)) {
846 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pstat);
847 goto out;
849 if (CNEQ(cnp, "uptime", 6)) {
850 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Puptime);
851 goto out;
853 if (CNEQ(cnp, "version", 7)) {
854 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
855 goto out;
857 if (CNEQ(cnp, "loadavg", 7)) {
858 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ploadavg);
859 goto out;
861 if (CNEQ(cnp, "net", 3)) {
862 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnet);
863 goto out;
865 if (CNEQ(cnp, "sys", 3)) {
866 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psys);
867 goto out;
869 if (CNEQ(cnp, "devices", 7)) {
870 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pdevices);
871 goto out;
874 pid = atopid(pname, cnp->cn_namelen);
875 if (pid == NO_PID)
876 break;
878 p = linprocfs_pfind(pid);
879 if (p == NULL)
880 break;
882 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
883 break;
885 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
886 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
887 break;
889 error = linprocfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
890 goto out;
892 case Pproc:
893 if (cnp->cn_flags & CNP_ISDOTDOT) {
894 error = linprocfs_root(dvp->v_mount, vpp);
895 goto out;
898 p = linprocfs_pfind(pfs->pfs_pid);
899 if (p == NULL)
900 break;
902 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
903 break;
905 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
906 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
907 break;
909 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
910 if (cnp->cn_namelen == pt->pt_namlen &&
911 bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
912 (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
913 goto found;
915 break;
917 found:
918 error = linprocfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
919 pt->pt_pfstype);
920 goto out;
922 default:
923 error = ENOTDIR;
924 goto out;
927 if (cnp->cn_nameiop == NAMEI_LOOKUP)
928 error = ENOENT;
929 else
930 error = EROFS;
933 * If no error occured *vpp will hold a referenced locked vnode.
934 * dvp was passed to us locked and *vpp must be returned locked
935 * so if dvp != *vpp and CNP_LOCKPARENT is not set, unlock dvp.
937 out:
938 if (p)
939 PRELE(p);
940 if (error == 0) {
941 if (*vpp != dvp && (cnp->cn_flags & CNP_LOCKPARENT) == 0) {
942 cnp->cn_flags |= CNP_PDIRUNLOCK;
943 vn_unlock(dvp);
946 return (error);
950 * Does this process have a text file?
953 linprocfs_validfile(struct proc *p)
956 return (procfs_findtextvp(p) != NULLVP);
960 * readdir() returns directory entries from pfsnode (vp).
962 * We generate just one directory entry at a time, as it would probably
963 * not pay off to buffer several entries locally to save uiomove calls.
965 * linprocfs_readdir(struct vnode *a_vp, struct uio *a_uio,
966 * struct ucred *a_cred, int *a_eofflag,
967 * int *a_ncookies, off_t **a_cookies)
969 static int
970 linprocfs_readdir(struct vop_readdir_args *ap)
972 struct pfsnode *pfs;
973 int error;
975 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
976 return (EINVAL);
978 pfs = VTOPFS(ap->a_vp);
979 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
980 if (error)
981 return (error);
983 switch (pfs->pfs_type) {
984 case Pproc:
986 * This is for the process-specific sub-directories.
987 * all that is needed to is copy out all the entries
988 * from the procent[] table (top of this file).
990 error = linprocfs_readdir_proc(ap);
991 break;
992 case Proot:
994 * This is for the root of the procfs filesystem
995 * what is needed is a special entry for "self"
996 * followed by an entry for each process on allproc
998 error = linprocfs_readdir_root(ap);
999 break;
1000 case Pnet:
1001 error = linprocfs_readdir_net(ap);
1002 break;
1003 case Psys:
1004 error = linprocfs_readdir_sys(ap);
1005 break;
1006 case Psyskernel:
1007 error = linprocfs_readdir_syskernel(ap);
1008 break;
1009 default:
1010 error = ENOTDIR;
1011 break;
1013 vn_unlock(ap->a_vp);
1015 return (error);
1018 static int
1019 linprocfs_readdir_proc(struct vop_readdir_args *ap)
1021 struct pfsnode *pfs;
1022 int error, i, retval;
1023 struct proc *p;
1024 struct proc_target *pt;
1025 struct uio *uio = ap->a_uio;
1027 pfs = VTOPFS(ap->a_vp);
1028 p = linprocfs_pfind(pfs->pfs_pid);
1029 if (p == NULL)
1030 return(0);
1031 if (!PRISON_CHECK(ap->a_cred, p->p_ucred)) {
1032 PRELE(p);
1033 return(0);
1036 error = 0;
1037 i = uio->uio_offset;
1039 for (pt = &proc_targets[i];
1040 !error && uio->uio_resid > 0 && i < nproc_targets; pt++, i++) {
1041 if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
1042 continue;
1044 retval = vop_write_dirent(&error, uio,
1045 PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype), pt->pt_type,
1046 pt->pt_namlen, pt->pt_name);
1047 if (retval)
1048 break;
1051 uio->uio_offset = i;
1052 PRELE(p);
1054 return(error);
1057 struct linprocfs_readdir_root_info {
1058 int error;
1059 int pcnt;
1060 int i;
1061 struct uio *uio;
1062 struct ucred *cred;
1066 * Scan the root directory by scanning all process
1068 static int linprocfs_readdir_root_callback(struct proc *p, void *data);
1070 static int
1071 linprocfs_readdir_root(struct vop_readdir_args *ap)
1073 struct linprocfs_readdir_root_info info;
1074 struct uio *uio = ap->a_uio;
1075 int res = 0;
1077 info.error = 0;
1078 info.i = uio->uio_offset;
1079 info.pcnt = 0;
1080 info.uio = uio;
1081 info.cred = ap->a_cred;
1083 while (info.pcnt < 13) {
1084 res = linprocfs_readdir_root_callback(NULL, &info);
1085 if (res < 0)
1086 break;
1088 if (res >= 0)
1089 allproc_scan(linprocfs_readdir_root_callback, &info);
1091 uio->uio_offset = info.i;
1092 return(info.error);
1095 static int
1096 linprocfs_readdir_root_callback(struct proc *p, void *data)
1098 struct linprocfs_readdir_root_info *info = data;
1099 int retval;
1100 struct uio *uio = info->uio;
1101 ino_t d_ino;
1102 const char *d_name;
1103 char d_name_pid[20];
1104 size_t d_namlen;
1105 uint8_t d_type;
1107 switch (info->pcnt) {
1108 case 0: /* `.' */
1109 d_ino = PROCFS_FILENO(0, Proot);
1110 d_name = ".";
1111 d_namlen = 1;
1112 d_type = DT_DIR;
1113 break;
1114 case 1: /* `..' */
1115 d_ino = PROCFS_FILENO(0, Proot);
1116 d_name = "..";
1117 d_namlen = 2;
1118 d_type = DT_DIR;
1119 break;
1121 case 2:
1122 d_ino = PROCFS_FILENO(0, Proot);
1123 d_namlen = 4;
1124 d_name = "self";
1125 d_type = DT_LNK;
1126 break;
1128 case 3:
1129 d_ino = PROCFS_FILENO(0, Pmeminfo);
1130 d_namlen = 7;
1131 d_name = "meminfo";
1132 d_type = DT_REG;
1133 break;
1135 case 4:
1136 d_ino = PROCFS_FILENO(0, Pcpuinfo);
1137 d_namlen = 7;
1138 d_name = "cpuinfo";
1139 d_type = DT_REG;
1140 break;
1142 case 5:
1143 d_ino = PROCFS_FILENO(0, Pstat);
1144 d_namlen = 4;
1145 d_name = "stat";
1146 d_type = DT_REG;
1147 break;
1149 case 6:
1150 d_ino = PROCFS_FILENO(0, Puptime);
1151 d_namlen = 6;
1152 d_name = "uptime";
1153 d_type = DT_REG;
1154 break;
1156 case 7:
1157 d_ino = PROCFS_FILENO(0, Pversion);
1158 d_namlen = 7;
1159 d_name = "version";
1160 d_type = DT_REG;
1161 break;
1163 case 8:
1164 d_ino = PROCFS_FILENO(0, Ploadavg);
1165 d_namlen = 7;
1166 d_name = "loadavg";
1167 d_type = DT_REG;
1168 break;
1169 case 9:
1170 d_ino = PROCFS_FILENO(0, Pnet);
1171 d_namlen = 3;
1172 d_name = "net";
1173 d_type = DT_DIR;
1174 break;
1175 case 10:
1176 d_ino = PROCFS_FILENO(0, Psys);
1177 d_namlen = 3;
1178 d_name = "sys";
1179 d_type = DT_DIR;
1180 break;
1181 case 11:
1182 d_ino = PROCFS_FILENO(0, Pmounts);
1183 d_namlen = 6;
1184 d_name = "mounts";
1185 d_type = DT_DIR;
1186 break;
1187 case 12:
1188 d_ino = PROCFS_FILENO(0, Pdevices);
1189 d_namlen = 7;
1190 d_name = "devices";
1191 d_type = DT_REG;
1192 break;
1193 default:
1195 * Ignore processes that aren't in our prison
1197 if (PRISON_CHECK(info->cred, p->p_ucred) == 0)
1198 return(0);
1201 * Ignore processes that we do not want to be visible.
1203 if (ps_showallprocs == 0 &&
1204 info->cred->cr_uid != 0 &&
1205 info->cred->cr_uid != p->p_ucred->cr_uid) {
1206 return(0);
1210 * Skip processes we have already read (optimization)
1212 if (info->pcnt < info->i) {
1213 ++info->pcnt;
1214 return(0);
1216 d_ino = PROCFS_FILENO(p->p_pid, Pproc);
1217 d_namlen = ksnprintf(d_name_pid, sizeof(d_name_pid),
1218 "%ld", (long)p->p_pid);
1219 d_name = d_name_pid;
1220 d_type = DT_DIR;
1221 break;
1225 * Skip processes we have already read
1227 if (info->pcnt < info->i) {
1228 ++info->pcnt;
1229 return(0);
1231 retval = vop_write_dirent(&info->error, info->uio,
1232 d_ino, d_type, d_namlen, d_name);
1233 if (retval == 0) {
1234 ++info->pcnt; /* iterate proc candidates scanned */
1235 ++info->i; /* iterate entries written */
1237 if (retval || info->error || uio->uio_resid <= 0)
1238 return(-1);
1239 return(0);
1243 * Scan the root directory by scanning all process
1245 static int linprocfs_readdir_net_callback(struct proc *p, void *data);
1247 static int
1248 linprocfs_readdir_net(struct vop_readdir_args *ap)
1250 struct linprocfs_readdir_root_info info;
1251 struct uio *uio = ap->a_uio;
1252 int res;
1254 info.error = 0;
1255 info.i = uio->uio_offset;
1256 info.pcnt = 0;
1257 info.uio = uio;
1258 info.cred = ap->a_cred;
1260 while (info.pcnt < 3) {
1261 res = linprocfs_readdir_net_callback(NULL, &info);
1262 if (res < 0)
1263 break;
1266 uio->uio_offset = info.i;
1267 return(info.error);
1270 static int
1271 linprocfs_readdir_net_callback(struct proc *p, void *data)
1273 struct linprocfs_readdir_root_info *info = data;
1274 int retval;
1275 struct uio *uio = info->uio;
1276 ino_t d_ino;
1277 const char *d_name;
1278 size_t d_namlen;
1279 uint8_t d_type;
1281 switch (info->pcnt) {
1282 case 0: /* `.' */
1283 d_ino = PROCFS_FILENO(0, Pnet);
1284 d_name = ".";
1285 d_namlen = 1;
1286 d_type = DT_DIR;
1287 break;
1288 case 1: /* `..' */
1289 d_ino = PROCFS_FILENO(0, Proot);
1290 d_name = "..";
1291 d_namlen = 2;
1292 d_type = DT_DIR;
1293 break;
1295 case 2:
1296 d_ino = PROCFS_FILENO(0, Pnet);
1297 d_namlen = 3;
1298 d_name = "dev";
1299 d_type = DT_REG;
1300 break;
1301 default:
1302 d_ino = 0;
1303 d_namlen = 0;
1304 d_name = NULL;
1305 d_type = DT_REG;
1306 break;
1310 * Skip processes we have already read
1312 if (info->pcnt < info->i) {
1313 ++info->pcnt;
1314 return(0);
1316 retval = vop_write_dirent(&info->error, info->uio,
1317 d_ino, d_type, d_namlen, d_name);
1318 if (retval == 0) {
1319 ++info->pcnt; /* iterate proc candidates scanned */
1320 ++info->i; /* iterate entries written */
1322 if (retval || info->error || uio->uio_resid <= 0)
1323 return(-1);
1324 return(0);
1334 * Scan the root directory by scanning all process
1336 static int linprocfs_readdir_sys_callback(struct proc *p, void *data);
1338 static int
1339 linprocfs_readdir_sys(struct vop_readdir_args *ap)
1341 struct linprocfs_readdir_root_info info;
1342 struct uio *uio = ap->a_uio;
1343 int res;
1345 info.error = 0;
1346 info.i = uio->uio_offset;
1347 info.pcnt = 0;
1348 info.uio = uio;
1349 info.cred = ap->a_cred;
1351 while (info.pcnt < 3) {
1352 res = linprocfs_readdir_sys_callback(NULL, &info);
1353 if (res < 0)
1354 break;
1357 uio->uio_offset = info.i;
1358 return(info.error);
1361 static int
1362 linprocfs_readdir_sys_callback(struct proc *p, void *data)
1364 struct linprocfs_readdir_root_info *info = data;
1365 int retval;
1366 struct uio *uio = info->uio;
1367 ino_t d_ino;
1368 const char *d_name;
1369 size_t d_namlen;
1370 uint8_t d_type;
1372 switch (info->pcnt) {
1373 case 0: /* `.' */
1374 d_ino = PROCFS_FILENO(0, Psys);
1375 d_name = ".";
1376 d_namlen = 1;
1377 d_type = DT_DIR;
1378 break;
1379 case 1: /* `..' */
1380 d_ino = PROCFS_FILENO(0, Proot);
1381 d_name = "..";
1382 d_namlen = 2;
1383 d_type = DT_DIR;
1384 break;
1386 case 2:
1387 d_ino = PROCFS_FILENO(0, Psyskernel);
1388 d_namlen = 6;
1389 d_name = "kernel";
1390 d_type = DT_DIR;
1391 break;
1392 default:
1393 d_ino = 0;
1394 d_namlen = 0;
1395 d_name = NULL;
1396 d_type = DT_REG;
1397 break;
1401 * Skip processes we have already read
1403 if (info->pcnt < info->i) {
1404 ++info->pcnt;
1405 return(0);
1407 retval = vop_write_dirent(&info->error, info->uio,
1408 d_ino, d_type, d_namlen, d_name);
1409 if (retval == 0) {
1410 ++info->pcnt; /* iterate proc candidates scanned */
1411 ++info->i; /* iterate entries written */
1413 if (retval || info->error || uio->uio_resid <= 0)
1414 return(-1);
1415 return(0);
1423 * Scan the root directory by scanning all process
1425 static int linprocfs_readdir_syskernel_callback(struct proc *p, void *data);
1427 static int
1428 linprocfs_readdir_syskernel(struct vop_readdir_args *ap)
1430 struct linprocfs_readdir_root_info info;
1431 struct uio *uio = ap->a_uio;
1432 int res;
1434 info.error = 0;
1435 info.i = uio->uio_offset;
1436 info.pcnt = 0;
1437 info.uio = uio;
1438 info.cred = ap->a_cred;
1440 while (info.pcnt < 6) {
1441 res = linprocfs_readdir_syskernel_callback(NULL, &info);
1442 if (res < 0)
1443 break;
1446 uio->uio_offset = info.i;
1447 return(info.error);
1450 static int
1451 linprocfs_readdir_syskernel_callback(struct proc *p, void *data)
1453 struct linprocfs_readdir_root_info *info = data;
1454 int retval;
1455 struct uio *uio = info->uio;
1456 ino_t d_ino;
1457 const char *d_name;
1458 size_t d_namlen;
1459 uint8_t d_type;
1461 switch (info->pcnt) {
1462 case 0: /* `.' */
1463 d_ino = PROCFS_FILENO(0, Psyskernel);
1464 d_name = ".";
1465 d_namlen = 1;
1466 d_type = DT_DIR;
1467 break;
1468 case 1: /* `..' */
1469 d_ino = PROCFS_FILENO(0, Psys);
1470 d_name = "..";
1471 d_namlen = 2;
1472 d_type = DT_DIR;
1473 break;
1475 case 2:
1476 d_ino = PROCFS_FILENO(0, Posrelease);
1477 d_namlen = 9;
1478 d_name = "osrelease";
1479 d_type = DT_REG;
1480 break;
1482 case 3:
1483 d_ino = PROCFS_FILENO(0, Postype);
1484 d_namlen = 4;
1485 d_name = "ostype";
1486 d_type = DT_REG;
1487 break;
1489 case 4:
1490 d_ino = PROCFS_FILENO(0, Pversion);
1491 d_namlen = 7;
1492 d_name = "version";
1493 d_type = DT_REG;
1494 break;
1496 case 5:
1497 d_ino = PROCFS_FILENO(0, Ppidmax);
1498 d_namlen = 7;
1499 d_name = "pid_max";
1500 d_type = DT_REG;
1501 break;
1502 default:
1503 d_ino = 0;
1504 d_namlen = 0;
1505 d_name = NULL;
1506 d_type = DT_REG;
1507 break;
1511 * Skip processes we have already read
1513 if (info->pcnt < info->i) {
1514 ++info->pcnt;
1515 return(0);
1517 retval = vop_write_dirent(&info->error, info->uio,
1518 d_ino, d_type, d_namlen, d_name);
1519 if (retval == 0) {
1520 ++info->pcnt; /* iterate proc candidates scanned */
1521 ++info->i; /* iterate entries written */
1523 if (retval || info->error || uio->uio_resid <= 0)
1524 return(-1);
1525 return(0);
1529 * readlink reads the link of `self' or `exe'
1531 static int
1532 linprocfs_readlink(struct vop_readlink_args *ap)
1534 char buf[16]; /* should be enough */
1535 struct proc *procp;
1536 struct vnode *vp = ap->a_vp;
1537 struct nchandle *nchp;
1538 struct pfsnode *pfs = VTOPFS(vp);
1539 char *fullpath, *freepath;
1540 int error, len;
1542 error = 0;
1543 procp = NULL;
1545 switch (pfs->pfs_type) {
1546 case Pself:
1547 if (pfs->pfs_fileno != PROCFS_FILENO(0, Pself))
1548 return (EINVAL);
1550 len = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
1552 error = uiomove(buf, len, ap->a_uio);
1553 break;
1555 * There _should_ be no way for an entire process to disappear
1556 * from under us...
1558 case Pexe:
1559 procp = linprocfs_pfind(pfs->pfs_pid);
1560 if (procp == NULL || procp->p_ucred == NULL) {
1561 kprintf("linprocfs_readlink: pid %d disappeared\n",
1562 pfs->pfs_pid);
1563 error = uiomove("unknown", sizeof("unknown") - 1,
1564 ap->a_uio);
1565 break;
1567 error = cache_fullpath(procp, &procp->p_textnch, NULL,
1568 &fullpath, &freepath, 0);
1569 if (error != 0) {
1570 error = uiomove("unknown", sizeof("unknown") - 1,
1571 ap->a_uio);
1572 break;
1574 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1575 kfree(freepath, M_TEMP);
1576 break;
1577 case Pcwd:
1578 procp = linprocfs_pfind(pfs->pfs_pid);
1579 if (procp == NULL || procp->p_ucred == NULL) {
1580 kprintf("linprocfs_readlink: pid %d disappeared\n",
1581 pfs->pfs_pid);
1582 error = uiomove("unknown", sizeof("unknown") - 1,
1583 ap->a_uio);
1584 break;
1586 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, NULL,
1587 &fullpath, &freepath, 0);
1588 if (error != 0) {
1589 error = uiomove("unknown", sizeof("unknown") - 1,
1590 ap->a_uio);
1591 break;
1593 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1594 kfree(freepath, M_TEMP);
1595 break;
1596 case Pprocroot:
1597 procp = linprocfs_pfind(pfs->pfs_pid);
1598 if (procp == NULL || procp->p_ucred == NULL) {
1599 kprintf("linprocfs_readlink: pid %d disappeared\n",
1600 pfs->pfs_pid);
1601 error = uiomove("unknown", sizeof("unknown") - 1,
1602 ap->a_uio);
1603 break;
1605 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
1606 error = cache_fullpath(procp, nchp, NULL,
1607 &fullpath, &freepath, 0);
1608 if (error != 0) {
1609 error = uiomove("unknown", sizeof("unknown") - 1,
1610 ap->a_uio);
1611 break;
1613 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1614 kfree(freepath, M_TEMP);
1615 break;
1616 case Pfd:
1617 procp = linprocfs_pfind(pfs->pfs_pid);
1618 if (procp == NULL || procp->p_ucred == NULL) {
1619 kprintf("linprocfs_readlink: pid %d disappeared\n",
1620 pfs->pfs_pid);
1621 error = uiomove("unknown", sizeof("unknown") - 1,
1622 ap->a_uio);
1623 break;
1625 if (procp == curproc) {
1626 error = uiomove("/dev/fd", sizeof("/dev/fd") - 1,
1627 ap->a_uio);
1628 break;
1629 } else {
1630 error = uiomove("unknown", sizeof("unknown") - 1,
1631 ap->a_uio);
1632 break;
1634 /* notreached */
1635 break;
1636 default:
1637 error = EINVAL;
1638 break;
1640 if (procp)
1641 PRELE(procp);
1642 return error;
1646 * convert decimal ascii to pid_t
1648 static pid_t
1649 atopid(const char *b, u_int len)
1651 pid_t p = 0;
1653 while (len--) {
1654 char c = *b++;
1655 if (c < '0' || c > '9')
1656 return (NO_PID);
1657 p = 10 * p + (c - '0');
1658 if (p > PID_MAX)
1659 return (NO_PID);
1662 return (p);