procfs_vnops.c revision 1.143 1 /* $NetBSD: procfs_vnops.c,v 1.143 2006/12/09 16:11:52 chs Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
35 */
36
37 /*
38 * Copyright (c) 1993 Jan-Simon Pendry
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
72 */
73
74 /*
75 * procfs vnode interface
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.143 2006/12/09 16:11:52 chs Exp $");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/time.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/filedesc.h>
87 #include <sys/proc.h>
88 #include <sys/vnode.h>
89 #include <sys/namei.h>
90 #include <sys/malloc.h>
91 #include <sys/mount.h>
92 #include <sys/dirent.h>
93 #include <sys/resourcevar.h>
94 #include <sys/stat.h>
95 #include <sys/ptrace.h>
96 #include <sys/kauth.h>
97
98 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
99
100 #include <machine/reg.h>
101
102 #include <miscfs/genfs/genfs.h>
103 #include <miscfs/procfs/procfs.h>
104
105 /*
106 * Vnode Operations.
107 *
108 */
109
110 static int procfs_validfile_linux(struct lwp *, struct mount *);
111 static int procfs_root_readdir_callback(struct proc *, void *);
112 static struct vnode *procfs_dir(pfstype, struct lwp *, struct proc *,
113 char **, char *, int);
114
115 /*
116 * This is a list of the valid names in the
117 * process-specific sub-directories. It is
118 * used in procfs_lookup and procfs_readdir
119 */
120 static const struct proc_target {
121 u_char pt_type;
122 u_char pt_namlen;
123 const char *pt_name;
124 pfstype pt_pfstype;
125 int (*pt_valid)(struct lwp *, struct mount *);
126 } proc_targets[] = {
127 #define N(s) sizeof(s)-1, s
128 /* name type validp */
129 { DT_DIR, N("."), PFSproc, NULL },
130 { DT_DIR, N(".."), PFSroot, NULL },
131 { DT_DIR, N("fd"), PFSfd, NULL },
132 { DT_REG, N("file"), PFSfile, procfs_validfile },
133 { DT_REG, N("mem"), PFSmem, NULL },
134 { DT_REG, N("regs"), PFSregs, procfs_validregs },
135 { DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs },
136 { DT_REG, N("ctl"), PFSctl, NULL },
137 { DT_REG, N("stat"), PFSstat, procfs_validfile_linux },
138 { DT_REG, N("status"), PFSstatus, NULL },
139 { DT_REG, N("note"), PFSnote, NULL },
140 { DT_REG, N("notepg"), PFSnotepg, NULL },
141 { DT_REG, N("map"), PFSmap, procfs_validmap },
142 { DT_REG, N("maps"), PFSmaps, procfs_validmap },
143 { DT_REG, N("cmdline"), PFScmdline, NULL },
144 { DT_REG, N("exe"), PFSexe, procfs_validfile },
145 { DT_LNK, N("cwd"), PFScwd, NULL },
146 { DT_LNK, N("root"), PFSchroot, NULL },
147 { DT_LNK, N("emul"), PFSemul, NULL },
148 #ifdef __HAVE_PROCFS_MACHDEP
149 PROCFS_MACHDEP_NODETYPE_DEFNS
150 #endif
151 #undef N
152 };
153 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
154
155 /*
156 * List of files in the root directory. Note: the validate function will
157 * be called with p == NULL for these ones.
158 */
159 static const struct proc_target proc_root_targets[] = {
160 #define N(s) sizeof(s)-1, s
161 /* name type validp */
162 { DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux },
163 { DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux },
164 { DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux },
165 { DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux },
166 { DT_REG, N("devices"), PFSdevices, procfs_validfile_linux },
167 #undef N
168 };
169 static const int nproc_root_targets =
170 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
171
172 int procfs_lookup(void *);
173 #define procfs_create genfs_eopnotsupp
174 #define procfs_mknod genfs_eopnotsupp
175 int procfs_open(void *);
176 int procfs_close(void *);
177 int procfs_access(void *);
178 int procfs_getattr(void *);
179 int procfs_setattr(void *);
180 #define procfs_read procfs_rw
181 #define procfs_write procfs_rw
182 #define procfs_fcntl genfs_fcntl
183 #define procfs_ioctl genfs_enoioctl
184 #define procfs_poll genfs_poll
185 #define procfs_revoke genfs_revoke
186 #define procfs_fsync genfs_nullop
187 #define procfs_seek genfs_nullop
188 #define procfs_remove genfs_eopnotsupp
189 int procfs_link(void *);
190 #define procfs_rename genfs_eopnotsupp
191 #define procfs_mkdir genfs_eopnotsupp
192 #define procfs_rmdir genfs_eopnotsupp
193 int procfs_symlink(void *);
194 int procfs_readdir(void *);
195 int procfs_readlink(void *);
196 #define procfs_abortop genfs_abortop
197 int procfs_inactive(void *);
198 int procfs_reclaim(void *);
199 #define procfs_lock genfs_lock
200 #define procfs_unlock genfs_unlock
201 #define procfs_bmap genfs_badop
202 #define procfs_strategy genfs_badop
203 int procfs_print(void *);
204 int procfs_pathconf(void *);
205 #define procfs_islocked genfs_islocked
206 #define procfs_advlock genfs_einval
207 #define procfs_bwrite genfs_eopnotsupp
208 #define procfs_putpages genfs_null_putpages
209
210 static int atoi(const char *, size_t);
211
212 /*
213 * procfs vnode operations.
214 */
215 int (**procfs_vnodeop_p)(void *);
216 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
217 { &vop_default_desc, vn_default_error },
218 { &vop_lookup_desc, procfs_lookup }, /* lookup */
219 { &vop_create_desc, procfs_create }, /* create */
220 { &vop_mknod_desc, procfs_mknod }, /* mknod */
221 { &vop_open_desc, procfs_open }, /* open */
222 { &vop_close_desc, procfs_close }, /* close */
223 { &vop_access_desc, procfs_access }, /* access */
224 { &vop_getattr_desc, procfs_getattr }, /* getattr */
225 { &vop_setattr_desc, procfs_setattr }, /* setattr */
226 { &vop_read_desc, procfs_read }, /* read */
227 { &vop_write_desc, procfs_write }, /* write */
228 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
229 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
230 { &vop_poll_desc, procfs_poll }, /* poll */
231 { &vop_revoke_desc, procfs_revoke }, /* revoke */
232 { &vop_fsync_desc, procfs_fsync }, /* fsync */
233 { &vop_seek_desc, procfs_seek }, /* seek */
234 { &vop_remove_desc, procfs_remove }, /* remove */
235 { &vop_link_desc, procfs_link }, /* link */
236 { &vop_rename_desc, procfs_rename }, /* rename */
237 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
238 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
239 { &vop_symlink_desc, procfs_symlink }, /* symlink */
240 { &vop_readdir_desc, procfs_readdir }, /* readdir */
241 { &vop_readlink_desc, procfs_readlink }, /* readlink */
242 { &vop_abortop_desc, procfs_abortop }, /* abortop */
243 { &vop_inactive_desc, procfs_inactive }, /* inactive */
244 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
245 { &vop_lock_desc, procfs_lock }, /* lock */
246 { &vop_unlock_desc, procfs_unlock }, /* unlock */
247 { &vop_bmap_desc, procfs_bmap }, /* bmap */
248 { &vop_strategy_desc, procfs_strategy }, /* strategy */
249 { &vop_print_desc, procfs_print }, /* print */
250 { &vop_islocked_desc, procfs_islocked }, /* islocked */
251 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
252 { &vop_advlock_desc, procfs_advlock }, /* advlock */
253 { &vop_putpages_desc, procfs_putpages }, /* putpages */
254 { NULL, NULL }
255 };
256 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
257 { &procfs_vnodeop_p, procfs_vnodeop_entries };
258 /*
259 * set things up for doing i/o on
260 * the pfsnode (vp). (vp) is locked
261 * on entry, and should be left locked
262 * on exit.
263 *
264 * for procfs we don't need to do anything
265 * in particular for i/o. all that is done
266 * is to support exclusive open on process
267 * memory images.
268 */
269 int
270 procfs_open(v)
271 void *v;
272 {
273 struct vop_open_args /* {
274 struct vnode *a_vp;
275 int a_mode;
276 kauth_cred_t a_cred;
277 struct lwp *a_l;
278 } */ *ap = v;
279 struct pfsnode *pfs = VTOPFS(ap->a_vp);
280 struct lwp *l1;
281 struct proc *p2;
282 int error;
283
284 l1 = ap->a_l; /* tracer */
285 p2 = PFIND(pfs->pfs_pid); /* traced */
286
287 if (p2 == NULL)
288 return (ENOENT); /* was ESRCH, jsp */
289
290 if (ISSET(p2->p_flag, P_INEXEC))
291 return (EAGAIN);
292
293 switch (pfs->pfs_type) {
294 case PFSmem:
295 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
296 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
297 return (EBUSY);
298
299 #define M2K(m) (((m) & FREAD) && ((m) & FWRITE) ? \
300 KAUTH_REQ_PROCESS_CANPROCFS_RW : \
301 (m) & FWRITE ? KAUTH_REQ_PROCESS_CANPROCFS_WRITE : \
302 KAUTH_REQ_PROCESS_CANPROCFS_READ)
303
304 error = kauth_authorize_process(l1->l_cred,
305 KAUTH_PROCESS_CANPROCFS, p2, pfs,
306 KAUTH_ARG(M2K(ap->a_mode)), NULL);
307 if (error)
308 return (error);
309
310 #undef M2K
311
312 if (!proc_isunder(p2, l1))
313 return (EPERM);
314
315 if (ap->a_mode & FWRITE)
316 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
317
318 return (0);
319
320 default:
321 break;
322 }
323
324 return (0);
325 }
326
327 /*
328 * close the pfsnode (vp) after doing i/o.
329 * (vp) is not locked on entry or exit.
330 *
331 * nothing to do for procfs other than undo
332 * any exclusive open flag (see _open above).
333 */
334 int
335 procfs_close(v)
336 void *v;
337 {
338 struct vop_close_args /* {
339 struct vnode *a_vp;
340 int a_fflag;
341 kauth_cred_t a_cred;
342 struct lwp *a_l;
343 } */ *ap = v;
344 struct pfsnode *pfs = VTOPFS(ap->a_vp);
345
346 switch (pfs->pfs_type) {
347 case PFSmem:
348 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
349 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
350 break;
351
352 default:
353 break;
354 }
355
356 return (0);
357 }
358
359 /*
360 * _inactive is called when the pfsnode
361 * is vrele'd and the reference count goes
362 * to zero. (vp) will be on the vnode free
363 * list, so to get it back vget() must be
364 * used.
365 *
366 * for procfs, check if the process is still
367 * alive and if it isn't then just throw away
368 * the vnode by calling vgone(). this may
369 * be overkill and a waste of time since the
370 * chances are that the process will still be
371 * there and PFIND is not free.
372 *
373 * (vp) is locked on entry, but must be unlocked on exit.
374 */
375 int
376 procfs_inactive(v)
377 void *v;
378 {
379 struct vop_inactive_args /* {
380 struct vnode *a_vp;
381 struct proc *a_p;
382 } */ *ap = v;
383 struct vnode *vp = ap->a_vp;
384 struct pfsnode *pfs = VTOPFS(vp);
385
386 VOP_UNLOCK(vp, 0);
387 if (PFIND(pfs->pfs_pid) == NULL && (vp->v_flag & VXLOCK) == 0)
388 vgone(vp);
389
390 return (0);
391 }
392
393 /*
394 * _reclaim is called when getnewvnode()
395 * wants to make use of an entry on the vnode
396 * free list. at this time the filesystem needs
397 * to free any private data and remove the node
398 * from any private lists.
399 */
400 int
401 procfs_reclaim(v)
402 void *v;
403 {
404 struct vop_reclaim_args /* {
405 struct vnode *a_vp;
406 } */ *ap = v;
407
408 return (procfs_freevp(ap->a_vp));
409 }
410
411 /*
412 * Return POSIX pathconf information applicable to special devices.
413 */
414 int
415 procfs_pathconf(v)
416 void *v;
417 {
418 struct vop_pathconf_args /* {
419 struct vnode *a_vp;
420 int a_name;
421 register_t *a_retval;
422 } */ *ap = v;
423
424 switch (ap->a_name) {
425 case _PC_LINK_MAX:
426 *ap->a_retval = LINK_MAX;
427 return (0);
428 case _PC_MAX_CANON:
429 *ap->a_retval = MAX_CANON;
430 return (0);
431 case _PC_MAX_INPUT:
432 *ap->a_retval = MAX_INPUT;
433 return (0);
434 case _PC_PIPE_BUF:
435 *ap->a_retval = PIPE_BUF;
436 return (0);
437 case _PC_CHOWN_RESTRICTED:
438 *ap->a_retval = 1;
439 return (0);
440 case _PC_VDISABLE:
441 *ap->a_retval = _POSIX_VDISABLE;
442 return (0);
443 case _PC_SYNC_IO:
444 *ap->a_retval = 1;
445 return (0);
446 default:
447 return (EINVAL);
448 }
449 /* NOTREACHED */
450 }
451
452 /*
453 * _print is used for debugging.
454 * just print a readable description
455 * of (vp).
456 */
457 int
458 procfs_print(v)
459 void *v;
460 {
461 struct vop_print_args /* {
462 struct vnode *a_vp;
463 } */ *ap = v;
464 struct pfsnode *pfs = VTOPFS(ap->a_vp);
465
466 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
467 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
468 return 0;
469 }
470
471 int
472 procfs_link(v)
473 void *v;
474 {
475 struct vop_link_args /* {
476 struct vnode *a_dvp;
477 struct vnode *a_vp;
478 struct componentname *a_cnp;
479 } */ *ap = v;
480
481 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
482 vput(ap->a_dvp);
483 return (EROFS);
484 }
485
486 int
487 procfs_symlink(v)
488 void *v;
489 {
490 struct vop_symlink_args /* {
491 struct vnode *a_dvp;
492 struct vnode **a_vpp;
493 struct componentname *a_cnp;
494 struct vattr *a_vap;
495 char *a_target;
496 } */ *ap = v;
497
498 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
499 vput(ap->a_dvp);
500 return (EROFS);
501 }
502
503 /*
504 * Works out the path to (and vnode of) the target process's current
505 * working directory or chroot. If the caller is in a chroot and
506 * can't "reach" the target's cwd or root (or some other error
507 * occurs), a "/" is returned for the path and a NULL pointer is
508 * returned for the vnode.
509 */
510 static struct vnode *
511 procfs_dir(pfstype t, struct lwp *caller, struct proc *target,
512 char **bpp, char *path, int len)
513 {
514 struct vnode *vp, *rvp = caller->l_proc->p_cwdi->cwdi_rdir;
515 char *bp;
516
517 bp = bpp ? *bpp : NULL;
518
519 switch (t) {
520 case PFScwd:
521 vp = target->p_cwdi->cwdi_cdir;
522 break;
523 case PFSchroot:
524 vp = target->p_cwdi->cwdi_rdir;
525 break;
526 case PFSexe:
527 rvp = rootvnode;
528 vp = target->p_textvp;
529 break;
530 default:
531 return (NULL);
532 }
533
534 if (rvp == NULL)
535 rvp = rootvnode;
536 if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
537 len / 2, 0, caller) != 0) {
538 vp = NULL;
539 if (bpp) {
540 bp = *bpp;
541 *--bp = '/';
542 }
543 }
544
545 if (bpp)
546 *bpp = bp;
547
548 return (vp);
549 }
550
551 /*
552 * Invent attributes for pfsnode (vp) and store
553 * them in (vap).
554 * Directories lengths are returned as zero since
555 * any real length would require the genuine size
556 * to be computed, and nothing cares anyway.
557 *
558 * this is relatively minimal for procfs.
559 */
560 int
561 procfs_getattr(v)
562 void *v;
563 {
564 struct vop_getattr_args /* {
565 struct vnode *a_vp;
566 struct vattr *a_vap;
567 kauth_cred_t a_cred;
568 struct lwp *a_l;
569 } */ *ap = v;
570 struct pfsnode *pfs = VTOPFS(ap->a_vp);
571 struct vattr *vap = ap->a_vap;
572 struct proc *procp;
573 int error;
574
575 /* first check the process still exists */
576 switch (pfs->pfs_type) {
577 case PFSroot:
578 case PFScurproc:
579 case PFSself:
580 procp = 0;
581 break;
582
583 default:
584 procp = PFIND(pfs->pfs_pid);
585 if (procp == NULL)
586 return (ENOENT);
587 break;
588 }
589
590 if (procp != NULL) {
591 if (kauth_authorize_process(kauth_cred_get(),
592 KAUTH_PROCESS_CANSEE, procp, NULL, NULL, NULL) != 0)
593 return (ENOENT);
594 }
595
596 error = 0;
597
598 /* start by zeroing out the attributes */
599 VATTR_NULL(vap);
600
601 /* next do all the common fields */
602 vap->va_type = ap->a_vp->v_type;
603 vap->va_mode = pfs->pfs_mode;
604 vap->va_fileid = pfs->pfs_fileno;
605 vap->va_flags = 0;
606 vap->va_blocksize = PAGE_SIZE;
607
608 /*
609 * Make all times be current TOD.
610 *
611 * It would be possible to get the process start
612 * time from the p_stats structure, but there's
613 * no "file creation" time stamp anyway, and the
614 * p_stats structure is not addressable if u. gets
615 * swapped out for that process.
616 */
617 getnanotime(&vap->va_ctime);
618 vap->va_atime = vap->va_mtime = vap->va_ctime;
619 if (procp)
620 TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
621 &vap->va_birthtime);
622 else
623 getnanotime(&vap->va_birthtime);
624
625 switch (pfs->pfs_type) {
626 case PFSmem:
627 case PFSregs:
628 case PFSfpregs:
629 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
630 PROCFS_MACHDEP_PROTECT_CASES
631 #endif
632 /*
633 * If the process has exercised some setuid or setgid
634 * privilege, then rip away read/write permission so
635 * that only root can gain access.
636 */
637 if (procp->p_flag & P_SUGID)
638 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
639 /* FALLTHROUGH */
640 case PFSctl:
641 case PFSstatus:
642 case PFSstat:
643 case PFSnote:
644 case PFSnotepg:
645 case PFSmap:
646 case PFSmaps:
647 case PFScmdline:
648 case PFSemul:
649 vap->va_nlink = 1;
650 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
651 vap->va_gid = kauth_cred_getegid(procp->p_cred);
652 break;
653 case PFSmeminfo:
654 case PFSdevices:
655 case PFScpuinfo:
656 case PFSuptime:
657 case PFSmounts:
658 vap->va_nlink = 1;
659 vap->va_uid = vap->va_gid = 0;
660 break;
661
662 default:
663 break;
664 }
665
666 /*
667 * now do the object specific fields
668 *
669 * The size could be set from struct reg, but it's hardly
670 * worth the trouble, and it puts some (potentially) machine
671 * dependent data into this machine-independent code. If it
672 * becomes important then this function should break out into
673 * a per-file stat function in the corresponding .c file.
674 */
675
676 switch (pfs->pfs_type) {
677 case PFSroot:
678 /*
679 * Set nlink to 1 to tell fts(3) we don't actually know.
680 */
681 vap->va_nlink = 1;
682 vap->va_uid = 0;
683 vap->va_gid = 0;
684 vap->va_bytes = vap->va_size = DEV_BSIZE;
685 break;
686
687 case PFSself:
688 case PFScurproc: {
689 char bf[16]; /* should be enough */
690 vap->va_nlink = 1;
691 vap->va_uid = 0;
692 vap->va_gid = 0;
693 vap->va_bytes = vap->va_size =
694 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
695 break;
696 }
697
698 case PFSfd:
699 if (pfs->pfs_fd != -1) {
700 struct file *fp;
701 struct proc *pown;
702
703 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
704 return error;
705 FILE_USE(fp);
706 vap->va_nlink = 1;
707 vap->va_uid = kauth_cred_geteuid(fp->f_cred);
708 vap->va_gid = kauth_cred_getegid(fp->f_cred);
709 switch (fp->f_type) {
710 case DTYPE_VNODE:
711 vap->va_bytes = vap->va_size =
712 ((struct vnode *)fp->f_data)->v_size;
713 break;
714 default:
715 vap->va_bytes = vap->va_size = 0;
716 break;
717 }
718 FILE_UNUSE(fp, proc_representative_lwp(pown));
719 break;
720 }
721 /*FALLTHROUGH*/
722 case PFSproc:
723 vap->va_nlink = 2;
724 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
725 vap->va_gid = kauth_cred_getegid(procp->p_cred);
726 vap->va_bytes = vap->va_size = DEV_BSIZE;
727 break;
728
729 case PFSfile:
730 error = EOPNOTSUPP;
731 break;
732
733 case PFSmem:
734 vap->va_bytes = vap->va_size =
735 ctob(procp->p_vmspace->vm_tsize +
736 procp->p_vmspace->vm_dsize +
737 procp->p_vmspace->vm_ssize);
738 break;
739
740 #if defined(PT_GETREGS) || defined(PT_SETREGS)
741 case PFSregs:
742 vap->va_bytes = vap->va_size = sizeof(struct reg);
743 break;
744 #endif
745
746 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
747 case PFSfpregs:
748 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
749 break;
750 #endif
751
752 case PFSctl:
753 case PFSstatus:
754 case PFSstat:
755 case PFSnote:
756 case PFSnotepg:
757 case PFScmdline:
758 case PFSmeminfo:
759 case PFSdevices:
760 case PFScpuinfo:
761 case PFSuptime:
762 case PFSmounts:
763 vap->va_bytes = vap->va_size = 0;
764 break;
765 case PFSmap:
766 case PFSmaps:
767 /*
768 * Advise a larger blocksize for the map files, so that
769 * they may be read in one pass.
770 */
771 vap->va_blocksize = 4 * PAGE_SIZE;
772 vap->va_bytes = vap->va_size = 0;
773 break;
774
775 case PFScwd:
776 case PFSchroot:
777 case PFSexe: {
778 char *path, *bp;
779
780 MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
781 M_WAITOK|M_CANFAIL);
782 if (path == NULL)
783 return (ENOMEM);
784 vap->va_nlink = 1;
785 vap->va_uid = 0;
786 vap->va_gid = 0;
787 bp = path + MAXPATHLEN;
788 *--bp = '\0';
789 (void)procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
790 MAXPATHLEN);
791 vap->va_bytes = vap->va_size = strlen(bp);
792 free(path, M_TEMP);
793 break;
794 }
795
796 case PFSemul:
797 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
798 break;
799
800 #ifdef __HAVE_PROCFS_MACHDEP
801 PROCFS_MACHDEP_NODETYPE_CASES
802 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
803 break;
804 #endif
805
806 default:
807 panic("procfs_getattr");
808 }
809
810 return (error);
811 }
812
813 /*ARGSUSED*/
814 int
815 procfs_setattr(void *v)
816 {
817 /*
818 * just fake out attribute setting
819 * it's not good to generate an error
820 * return, otherwise things like creat()
821 * will fail when they try to set the
822 * file length to 0. worse, this means
823 * that echo $note > /proc/$pid/note will fail.
824 */
825
826 return (0);
827 }
828
829 /*
830 * implement access checking.
831 *
832 * actually, the check for super-user is slightly
833 * broken since it will allow read access to write-only
834 * objects. this doesn't cause any particular trouble
835 * but does mean that the i/o entry points need to check
836 * that the operation really does make sense.
837 */
838 int
839 procfs_access(v)
840 void *v;
841 {
842 struct vop_access_args /* {
843 struct vnode *a_vp;
844 int a_mode;
845 kauth_cred_t a_cred;
846 struct lwp *a_l;
847 } */ *ap = v;
848 struct vattr va;
849 int error;
850
851 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
852 return (error);
853
854 return (vaccess(va.va_type, va.va_mode,
855 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
856 }
857
858 /*
859 * lookup. this is incredibly complicated in the
860 * general case, however for most pseudo-filesystems
861 * very little needs to be done.
862 *
863 * Locking isn't hard here, just poorly documented.
864 *
865 * If we're looking up ".", just vref the parent & return it.
866 *
867 * If we're looking up "..", unlock the parent, and lock "..". If everything
868 * went ok, and we're on the last component and the caller requested the
869 * parent locked, try to re-lock the parent. We do this to prevent lock
870 * races.
871 *
872 * For anything else, get the needed node. Then unlock the parent if not
873 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
874 * parent in the .. case).
875 *
876 * We try to exit with the parent locked in error cases.
877 */
878 int
879 procfs_lookup(v)
880 void *v;
881 {
882 struct vop_lookup_args /* {
883 struct vnode * a_dvp;
884 struct vnode ** a_vpp;
885 struct componentname * a_cnp;
886 } */ *ap = v;
887 struct componentname *cnp = ap->a_cnp;
888 struct vnode **vpp = ap->a_vpp;
889 struct vnode *dvp = ap->a_dvp;
890 const char *pname = cnp->cn_nameptr;
891 const struct proc_target *pt = NULL;
892 struct vnode *fvp;
893 pid_t pid;
894 struct pfsnode *pfs;
895 struct proc *p = NULL;
896 struct lwp *l = NULL;
897 int i, error, iscurproc = 0, isself = 0;
898
899 *vpp = NULL;
900
901 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
902 return (EROFS);
903
904 if (cnp->cn_namelen == 1 && *pname == '.') {
905 *vpp = dvp;
906 VREF(dvp);
907 return (0);
908 }
909
910 pfs = VTOPFS(dvp);
911 switch (pfs->pfs_type) {
912 case PFSroot:
913 /*
914 * Shouldn't get here with .. in the root node.
915 */
916 if (cnp->cn_flags & ISDOTDOT)
917 return (EIO);
918
919 iscurproc = CNEQ(cnp, "curproc", 7);
920 isself = CNEQ(cnp, "self", 4);
921
922 if (iscurproc || isself) {
923 error = procfs_allocvp(dvp->v_mount, vpp, 0,
924 iscurproc ? PFScurproc : PFSself, -1);
925 return (error);
926 }
927
928 for (i = 0; i < nproc_root_targets; i++) {
929 pt = &proc_root_targets[i];
930 if (cnp->cn_namelen == pt->pt_namlen &&
931 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
932 (pt->pt_valid == NULL ||
933 (*pt->pt_valid)(cnp->cn_lwp, dvp->v_mount)))
934 break;
935 }
936
937 if (i != nproc_root_targets) {
938 error = procfs_allocvp(dvp->v_mount, vpp, 0,
939 pt->pt_pfstype, -1);
940 return (error);
941 }
942
943 pid = (pid_t)atoi(pname, cnp->cn_namelen);
944
945 p = PFIND(pid);
946 if (p == NULL)
947 break;
948
949 error = procfs_allocvp(dvp->v_mount, vpp, pid, PFSproc, -1);
950 return (error);
951
952 case PFSproc:
953 /*
954 * do the .. dance. We unlock the directory, and then
955 * get the root dir. That will automatically return ..
956 * locked. Then if the caller wanted dvp locked, we
957 * re-lock.
958 */
959 if (cnp->cn_flags & ISDOTDOT) {
960 VOP_UNLOCK(dvp, 0);
961 error = procfs_root(dvp->v_mount, vpp);
962 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
963 return (error);
964 }
965
966 p = PFIND(pfs->pfs_pid);
967 if (p == NULL)
968 break;
969 l = proc_representative_lwp(p);
970
971 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
972 if (cnp->cn_namelen == pt->pt_namlen &&
973 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
974 (pt->pt_valid == NULL ||
975 (*pt->pt_valid)(cnp->cn_lwp, dvp->v_mount)))
976 goto found;
977 }
978 break;
979
980 found:
981 if (pt->pt_pfstype == PFSfile) {
982 fvp = p->p_textvp;
983 /* We already checked that it exists. */
984 VREF(fvp);
985 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
986 *vpp = fvp;
987 return (0);
988 }
989
990 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
991 pt->pt_pfstype, -1);
992 return (error);
993
994 case PFSfd: {
995 int fd;
996 struct file *fp;
997 /*
998 * do the .. dance. We unlock the directory, and then
999 * get the proc dir. That will automatically return ..
1000 * locked. Then re-lock the directory.
1001 */
1002 if (cnp->cn_flags & ISDOTDOT) {
1003 VOP_UNLOCK(dvp, 0);
1004 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1005 PFSproc, -1);
1006 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1007 return (error);
1008 }
1009 fd = atoi(pname, cnp->cn_namelen);
1010 p = PFIND(pfs->pfs_pid);
1011 if (p == NULL || (fp = fd_getfile(p->p_fd, fd)) == NULL)
1012 return ENOENT;
1013 FILE_USE(fp);
1014
1015 switch (fp->f_type) {
1016 case DTYPE_VNODE:
1017 fvp = (struct vnode *)fp->f_data;
1018
1019 /* Don't show directories */
1020 if (fvp->v_type == VDIR)
1021 goto symlink;
1022
1023 VREF(fvp);
1024 FILE_UNUSE(fp, l);
1025 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
1026 (p == curproc ? LK_CANRECURSE : 0));
1027 *vpp = fvp;
1028 error = 0;
1029 break;
1030 default:
1031 symlink:
1032 FILE_UNUSE(fp, l);
1033 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1034 PFSfd, fd);
1035 break;
1036 }
1037 return error;
1038 }
1039 default:
1040 return (ENOTDIR);
1041 }
1042
1043 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
1044 }
1045
1046 int
1047 procfs_validfile(struct lwp *l, struct mount *mp)
1048 {
1049 return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
1050 }
1051
1052 static int
1053 procfs_validfile_linux(l, mp)
1054 struct lwp *l;
1055 struct mount *mp;
1056 {
1057 int flags;
1058
1059 flags = VFSTOPROC(mp)->pmnt_flags;
1060 return (flags & PROCFSMNT_LINUXCOMPAT) &&
1061 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
1062 }
1063
1064 struct procfs_root_readdir_ctx {
1065 struct uio *uiop;
1066 off_t *cookies;
1067 int ncookies;
1068 off_t off;
1069 off_t startoff;
1070 int error;
1071 };
1072
1073 static int
1074 procfs_root_readdir_callback(struct proc *p, void *arg)
1075 {
1076 struct procfs_root_readdir_ctx *ctxp = arg;
1077 struct dirent d;
1078 struct uio *uiop;
1079 int error;
1080
1081 uiop = ctxp->uiop;
1082 if (uiop->uio_resid < UIO_MX)
1083 return -1; /* no space */
1084
1085 if (ctxp->off < ctxp->startoff) {
1086 ctxp->off++;
1087 return 0;
1088 }
1089
1090 if (kauth_authorize_process(kauth_cred_get(),
1091 KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
1092 return 0;
1093
1094 memset(&d, 0, UIO_MX);
1095 d.d_reclen = UIO_MX;
1096 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1097 d.d_namlen = snprintf(d.d_name,
1098 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1099 d.d_type = DT_DIR;
1100
1101 proclist_unlock_read();
1102 error = uiomove(&d, UIO_MX, uiop);
1103 proclist_lock_read();
1104 if (error) {
1105 ctxp->error = error;
1106 return -1;
1107 }
1108
1109 ctxp->ncookies++;
1110 if (ctxp->cookies)
1111 *(ctxp->cookies)++ = ctxp->off + 1;
1112 ctxp->off++;
1113
1114 return 0;
1115 }
1116
1117 /*
1118 * readdir returns directory entries from pfsnode (vp).
1119 *
1120 * the strategy here with procfs is to generate a single
1121 * directory entry at a time (struct dirent) and then
1122 * copy that out to userland using uiomove. a more efficent
1123 * though more complex implementation, would try to minimize
1124 * the number of calls to uiomove(). for procfs, this is
1125 * hardly worth the added code complexity.
1126 *
1127 * this should just be done through read()
1128 */
1129 int
1130 procfs_readdir(v)
1131 void *v;
1132 {
1133 struct vop_readdir_args /* {
1134 struct vnode *a_vp;
1135 struct uio *a_uio;
1136 kauth_cred_t a_cred;
1137 int *a_eofflag;
1138 off_t **a_cookies;
1139 int *a_ncookies;
1140 } */ *ap = v;
1141 struct uio *uio = ap->a_uio;
1142 struct dirent d;
1143 struct pfsnode *pfs;
1144 off_t i;
1145 int error;
1146 off_t *cookies = NULL;
1147 int ncookies;
1148 struct vnode *vp;
1149 const struct proc_target *pt;
1150 struct procfs_root_readdir_ctx ctx;
1151
1152 vp = ap->a_vp;
1153 pfs = VTOPFS(vp);
1154
1155 if (uio->uio_resid < UIO_MX)
1156 return (EINVAL);
1157 if (uio->uio_offset < 0)
1158 return (EINVAL);
1159
1160 error = 0;
1161 i = uio->uio_offset;
1162 memset(&d, 0, UIO_MX);
1163 d.d_reclen = UIO_MX;
1164 ncookies = uio->uio_resid / UIO_MX;
1165
1166 switch (pfs->pfs_type) {
1167 /*
1168 * this is for the process-specific sub-directories.
1169 * all that is needed to is copy out all the entries
1170 * from the procent[] table (top of this file).
1171 */
1172 case PFSproc: {
1173 struct proc *p;
1174
1175 if (i >= nproc_targets)
1176 return 0;
1177
1178 p = PFIND(pfs->pfs_pid);
1179 if (p == NULL)
1180 break;
1181
1182 if (ap->a_ncookies) {
1183 ncookies = min(ncookies, (nproc_targets - i));
1184 cookies = malloc(ncookies * sizeof (off_t),
1185 M_TEMP, M_WAITOK);
1186 *ap->a_cookies = cookies;
1187 }
1188
1189 for (pt = &proc_targets[i];
1190 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1191 if (pt->pt_valid &&
1192 (*pt->pt_valid)(proc_representative_lwp(p), vp->v_mount) == 0)
1193 continue;
1194
1195 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1196 pt->pt_pfstype, -1);
1197 d.d_namlen = pt->pt_namlen;
1198 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1199 d.d_type = pt->pt_type;
1200
1201 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1202 break;
1203 if (cookies)
1204 *cookies++ = i + 1;
1205 }
1206
1207 break;
1208 }
1209 case PFSfd: {
1210 struct proc *p;
1211 struct filedesc *fdp;
1212 struct file *fp;
1213 int lim, nc = 0;
1214
1215 p = PFIND(pfs->pfs_pid);
1216 if (p == NULL)
1217 return ESRCH;
1218
1219 if (kauth_authorize_process(kauth_cred_get(),
1220 KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
1221 return ESRCH;
1222
1223 fdp = p->p_fd;
1224
1225 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1226 if (i >= lim)
1227 return 0;
1228
1229 if (ap->a_ncookies) {
1230 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
1231 cookies = malloc(ncookies * sizeof (off_t),
1232 M_TEMP, M_WAITOK);
1233 *ap->a_cookies = cookies;
1234 }
1235
1236 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1237 pt = &proc_targets[i];
1238 d.d_namlen = pt->pt_namlen;
1239 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1240 pt->pt_pfstype, -1);
1241 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1242 d.d_type = pt->pt_type;
1243 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1244 break;
1245 if (cookies)
1246 *cookies++ = i + 1;
1247 nc++;
1248 }
1249 if (error) {
1250 ncookies = nc;
1251 break;
1252 }
1253 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
1254 /* check the descriptor exists */
1255 if ((fp = fd_getfile(fdp, i - 2)) == NULL)
1256 continue;
1257 simple_unlock(&fp->f_slock);
1258
1259 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1260 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1261 "%lld", (long long)(i - 2));
1262 d.d_type = VREG;
1263 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1264 break;
1265 if (cookies)
1266 *cookies++ = i + 1;
1267 nc++;
1268 }
1269 ncookies = nc;
1270 break;
1271 }
1272
1273 /*
1274 * this is for the root of the procfs filesystem
1275 * what is needed are special entries for "curproc"
1276 * and "self" followed by an entry for each process
1277 * on allproc.
1278 */
1279
1280 case PFSroot: {
1281 int nc = 0;
1282
1283 if (ap->a_ncookies) {
1284 /*
1285 * XXX Potentially allocating too much space here,
1286 * but I'm lazy. This loop needs some work.
1287 */
1288 cookies = malloc(ncookies * sizeof (off_t),
1289 M_TEMP, M_WAITOK);
1290 *ap->a_cookies = cookies;
1291 }
1292 error = 0;
1293 /* 0 ... 3 are static entries. */
1294 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1295 switch (i) {
1296 case 0: /* `.' */
1297 case 1: /* `..' */
1298 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1299 d.d_namlen = i + 1;
1300 memcpy(d.d_name, "..", d.d_namlen);
1301 d.d_name[i + 1] = '\0';
1302 d.d_type = DT_DIR;
1303 break;
1304
1305 case 2:
1306 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1307 d.d_namlen = sizeof("curproc") - 1;
1308 memcpy(d.d_name, "curproc", sizeof("curproc"));
1309 d.d_type = DT_LNK;
1310 break;
1311
1312 case 3:
1313 d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1314 d.d_namlen = sizeof("self") - 1;
1315 memcpy(d.d_name, "self", sizeof("self"));
1316 d.d_type = DT_LNK;
1317 break;
1318 }
1319
1320 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1321 break;
1322 nc++;
1323 if (cookies)
1324 *cookies++ = i + 1;
1325 }
1326 /* 4 ... are process entries. */
1327 ctx.uiop = uio;
1328 ctx.error = 0;
1329 ctx.off = 4;
1330 ctx.startoff = i;
1331 ctx.cookies = cookies;
1332 ctx.ncookies = nc;
1333 proclist_foreach_call(&allproc,
1334 procfs_root_readdir_callback, &ctx);
1335 cookies = ctx.cookies;
1336 nc = ctx.ncookies;
1337 error = ctx.error;
1338 if (error)
1339 break;
1340
1341 /* misc entries. */
1342 if (i < ctx.off)
1343 i = ctx.off;
1344 if (i >= ctx.off + nproc_root_targets)
1345 break;
1346 for (pt = &proc_root_targets[i - ctx.off];
1347 uio->uio_resid >= UIO_MX &&
1348 pt < &proc_root_targets[nproc_root_targets];
1349 pt++, i++) {
1350 if (pt->pt_valid &&
1351 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1352 continue;
1353 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1354 d.d_namlen = pt->pt_namlen;
1355 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1356 d.d_type = pt->pt_type;
1357
1358 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1359 break;
1360 nc++;
1361 if (cookies)
1362 *cookies++ = i + 1;
1363 }
1364
1365 ncookies = nc;
1366 break;
1367 }
1368
1369 default:
1370 error = ENOTDIR;
1371 break;
1372 }
1373
1374 if (ap->a_ncookies) {
1375 if (error) {
1376 if (cookies)
1377 free(*ap->a_cookies, M_TEMP);
1378 *ap->a_ncookies = 0;
1379 *ap->a_cookies = NULL;
1380 } else
1381 *ap->a_ncookies = ncookies;
1382 }
1383 uio->uio_offset = i;
1384 return (error);
1385 }
1386
1387 /*
1388 * readlink reads the link of `curproc' and others
1389 */
1390 int
1391 procfs_readlink(v)
1392 void *v;
1393 {
1394 struct vop_readlink_args *ap = v;
1395 char bf[16]; /* should be enough */
1396 char *bp = bf;
1397 char *path = NULL;
1398 int len;
1399 int error = 0;
1400 struct pfsnode *pfs = VTOPFS(ap->a_vp);
1401 struct proc *pown;
1402
1403 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1404 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
1405 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1406 len = snprintf(bf, sizeof(bf), "%s", "curproc");
1407 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
1408 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) ||
1409 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
1410 pown = PFIND(pfs->pfs_pid);
1411 if (pown == NULL)
1412 return (ESRCH);
1413 MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
1414 M_WAITOK|M_CANFAIL);
1415 if (path == NULL)
1416 return (ENOMEM);
1417 bp = path + MAXPATHLEN;
1418 *--bp = '\0';
1419 (void)procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
1420 &bp, path, MAXPATHLEN);
1421 len = strlen(bp);
1422 } else {
1423 struct file *fp;
1424 struct vnode *vxp, *vp;
1425
1426 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
1427 return error;
1428 FILE_USE(fp);
1429 switch (fp->f_type) {
1430 case DTYPE_VNODE:
1431 vxp = (struct vnode *)fp->f_data;
1432 if (vxp->v_type != VDIR) {
1433 FILE_UNUSE(fp, proc_representative_lwp(pown));
1434 return EINVAL;
1435 }
1436 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1437 == NULL) {
1438 FILE_UNUSE(fp, proc_representative_lwp(pown));
1439 return ENOMEM;
1440 }
1441 bp = path + MAXPATHLEN;
1442 *--bp = '\0';
1443 vp = curproc->p_cwdi->cwdi_rdir;
1444 if (vp == NULL)
1445 vp = rootvnode;
1446 error = getcwd_common(vxp, vp, &bp, path,
1447 MAXPATHLEN / 2, 0, curlwp);
1448 FILE_UNUSE(fp, proc_representative_lwp(pown));
1449 if (error) {
1450 free(path, M_TEMP);
1451 return error;
1452 }
1453 len = strlen(bp);
1454 break;
1455
1456 case DTYPE_MISC:
1457 len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1458 break;
1459
1460 case DTYPE_KQUEUE:
1461 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1462 break;
1463
1464 default:
1465 return EINVAL;
1466 }
1467 }
1468
1469 error = uiomove(bp, len, ap->a_uio);
1470 if (path)
1471 free(path, M_TEMP);
1472 return error;
1473 }
1474
1475 /*
1476 * convert decimal ascii to int
1477 */
1478 static int
1479 atoi(b, len)
1480 const char *b;
1481 size_t len;
1482 {
1483 int p = 0;
1484
1485 while (len--) {
1486 char c = *b++;
1487 if (c < '0' || c > '9')
1488 return -1;
1489 p = 10 * p + (c - '0');
1490 }
1491
1492 return p;
1493 }
1494