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