procfs_vnops.c revision 1.2 1 /*
2 * Copyright (c) 1993 Paul Kranenburg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christopher G. Demetriou.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software withough specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $Id: procfs_vnops.c,v 1.2 1993/08/24 16:25:13 pk Exp $
31 */
32
33 /*
34 * PROCFS vnode interface routines
35 */
36
37 #include "param.h"
38 #include "systm.h"
39 #include "time.h"
40 #include "kernel.h"
41 #include "ioctl.h"
42 #include "file.h"
43 #include "proc.h"
44 #include "buf.h"
45 #include "vnode.h"
46 #include "namei.h"
47 #include "resourcevar.h"
48 #include "vm/vm.h"
49 #include "kinfo.h"
50 #include "kinfo_proc.h"
51
52 #include "procfs.h"
53 #include "pfsnode.h"
54
55 #include "machine/vmparam.h"
56
57 /*
58 * procfs vnode operations.
59 */
60 struct vnodeops pfs_vnodeops = {
61 pfs_lookup, /* lookup */
62 pfs_create, /* create */
63 pfs_mknod, /* mknod */
64 pfs_open, /* open */
65 pfs_close, /* close */
66 pfs_access, /* access */
67 pfs_getattr, /* getattr */
68 pfs_setattr, /* setattr */
69 pfs_read, /* read */
70 pfs_write, /* write */
71 pfs_ioctl, /* ioctl */
72 pfs_select, /* select */
73 pfs_mmap, /* mmap */
74 pfs_fsync, /* fsync */
75 pfs_seek, /* seek */
76 pfs_remove, /* remove */
77 pfs_link, /* link */
78 pfs_rename, /* rename */
79 pfs_mkdir, /* mkdir */
80 pfs_rmdir, /* rmdir */
81 pfs_symlink, /* symlink */
82 pfs_readdir, /* readdir */
83 pfs_readlink, /* readlink */
84 pfs_abortop, /* abortop */
85 pfs_inactive, /* inactive */
86 pfs_reclaim, /* reclaim */
87 pfs_lock, /* lock */
88 pfs_unlock, /* unlock */
89 pfs_bmap, /* bmap */
90 pfs_strategy, /* strategy */
91 pfs_print, /* print */
92 pfs_islocked, /* islocked */
93 pfs_advlock, /* advlock */
94 };
95
96 /*
97 * Vnode Operations.
98 *
99 */
100 /* ARGSUSED */
101 int
102 pfs_open(vp, mode, cred, p)
103 register struct vnode *vp;
104 int mode;
105 struct ucred *cred;
106 struct proc *p;
107 {
108 struct pfsnode *pfsp = VTOPFS(vp);
109
110 #ifdef DEBUG
111 if (pfs_debug)
112 printf("pfs_open: vp 0x%x, proc %d\n", vp, p->p_pid);
113 #endif
114
115 if ((pfsp->pfs_pid?pfind(pfsp->pfs_pid):&proc0) == NULL)
116 return ESRCH;
117
118 if ( (pfsp->flags & FWRITE) && (mode & O_EXCL) ||
119 (pfsp->flags & O_EXCL) && (mode & FWRITE) )
120 return EBUSY;
121
122
123 if (mode & FWRITE)
124 pfsp->flags = (mode & (FWRITE|O_EXCL));
125 return 0;
126 }
127
128 /*
129 * /proc filesystem close routine
130 */
131 /* ARGSUSED */
132 int
133 pfs_close(vp, flag, cred, p)
134 register struct vnode *vp;
135 int flag;
136 struct ucred *cred;
137 struct proc *p;
138 {
139 struct pfsnode *pfsp = VTOPFS(vp);
140
141 #ifdef DEBUG
142 if (pfs_debug)
143 printf("pfs_close: vp 0x%x proc %d\n", vp, p->p_pid);
144 #endif
145 if ((flag & FWRITE) && (pfsp->flags & O_EXCL))
146 pfsp->flags &= ~(FWRITE|O_EXCL);
147
148 return (0);
149 }
150
151 /*
152 * Ioctl operation.
153 */
154 /* ARGSUSED */
155 int
156 pfs_ioctl(vp, com, data, fflag, cred, p)
157 struct vnode *vp;
158 int com;
159 caddr_t data;
160 int fflag;
161 struct ucred *cred;
162 struct proc *p;
163 {
164 int error = 0;
165 struct proc *procp;
166 struct pfsnode *pfsp = VTOPFS(vp);
167
168 procp = pfsp->pfs_pid?pfind(pfsp->pfs_pid):&proc0;
169 if (!procp)
170 return ESRCH;
171
172 switch (com) {
173
174 case PIOCGPINFO: {
175 int copysize = sizeof(struct kinfo_proc), needed;
176 kinfo_doproc(KINFO_PROC_PID, data, ©size,
177 pfsp->pfs_pid, &needed);
178 break;
179 }
180
181 #ifdef notyet /* Changes to proc.h needed */
182 case PIOCGSIGSET:
183 procp->p_psigset = *(sigset_t *)data;
184 break;
185
186 case PIOCSSIGSET:
187 *(sigset_t *)data = procp->p_psigset;
188 break;
189
190 case PIOCGFLTSET:
191 procp->p_pfltset = *(sigflt_t *)data;
192 break;
193
194 case PIOCSFLTSET:
195 *(fltset_t *)data = procp->p_pfltset;
196 break;
197 #endif
198
199 case PIOCGMAPFD:
200 error = pfs_vmfd(procp, pfsp, (struct vmfd *)data, p);
201 break;
202
203 case PIOCGNMAP:
204 *(int *)data = pfs_vm_nentries(procp, pfsp);
205 break;
206
207 case PIOCGMAP:
208 error = pfs_vmmap(procp, pfsp, *(struct procmap *)data);
209 break;
210
211 default:
212 error = EIO;
213 break;
214 }
215 return error;
216 }
217
218 /*
219 * Pass I/O requests to the memory filesystem process.
220 */
221 int
222 pfs_strategy(bp)
223 register struct buf *bp;
224 {
225 struct vnode *vp;
226 struct proc *p = curproc; /* XXX */
227
228 return (0);
229 }
230
231 /*
232 * This is a noop, simply returning what one has been given.
233 */
234 int
235 pfs_bmap(vp, bn, vpp, bnp)
236 struct vnode *vp;
237 daddr_t bn;
238 struct vnode **vpp;
239 daddr_t *bnp;
240 {
241
242 if (vpp != NULL)
243 *vpp = vp;
244 if (bnp != NULL)
245 *bnp = bn;
246 return (0);
247 }
248
249 /*
250 * /proc filesystem inactive routine
251 */
252 /* ARGSUSED */
253 int
254 pfs_inactive(vp, p)
255 struct vnode *vp;
256 struct proc *p;
257 {
258 struct pfsnode *pfsp = VTOPFS(vp);
259
260 #if 0
261 if ((pfsp->pfs_pid?pfind(pfsp->pfs_pid):&proc0) && vp->v_usecount == 0)
262 vgone(vp);
263 #endif
264 if (vp->v_usecount == 0)
265 vgone(vp);
266 return 0;
267 }
268
269 /*
270 * /proc filesystem reclaim routine
271 */
272 /* ARGSUSED */
273 int
274 pfs_reclaim(vp)
275 struct vnode *vp;
276 {
277 struct pfsnode **pp, *pfsp = VTOPFS(vp);
278
279 for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next) {
280 if (*pp == pfsp) {
281 *pp = pfsp->pfs_next;
282 break;
283 }
284 }
285 return 0;
286 }
287
288 /*
289 * Print out the contents of an pfsnode.
290 */
291 void
292 pfs_print(vp)
293 struct vnode *vp;
294 {
295 return;
296 }
297
298 /*
299 * /proc bad operation
300 */
301 int
302 pfs_badop()
303 {
304 printf("pfs_badop called\n");
305 return EIO;
306 }
307
308 #if 0 /* Moved to pfs_subr.c */
309 /*
310 * Vnode op for reading/writing.
311 */
312 /* ARGSUSED */
313 int
314 pfs_doio(vp, uio, ioflag, cred)
315 struct vnode *vp;
316 register struct uio *uio;
317 int ioflag;
318 struct ucred *cred;
319 {
320 struct pfsnode *pfsp = VTOPFS(vp);
321 struct proc *procp;
322 int error = 0;
323 long n, off;
324 caddr_t kva;
325
326 #ifdef DEBUG
327 if (pfs_debug)
328 printf("pfs_doio(%s): vp 0x%x, proc %x\n",
329 uio->uio_rw==UIO_READ?"R":"W", vp, uio->uio_procp);
330 #endif
331
332 #ifdef DIAGNOSTIC
333 if (vp->v_type != VPROC)
334 panic("pfs_doio vtype");
335 #endif
336 procp = pfsp->pfs_pid?pfind(pfsp->pfs_pid):&proc0;
337 if (!procp)
338 return ESRCH;
339
340 if (uio->uio_resid == 0)
341 return (0);
342 if (uio->uio_offset < 0)
343 return (EINVAL);
344
345 do { /* One page at a time */
346 off = uio->uio_offset - trunc_page(uio->uio_offset);
347 n = MIN(PAGE_SIZE-off, uio->uio_resid);
348
349 /* Map page into kernel space */
350 error = pfs_map(procp, &kva, uio->uio_rw, uio->uio_offset);
351 if (error)
352 return error;
353
354 error = uiomove(kva + off, (int)n, uio);
355 pfs_unmap(procp, kva);
356
357 } while (error == 0 && uio->uio_resid > 0);
358
359 return (error);
360 }
361 #endif
362
363 /*
364 * Make up some attributes for a process file
365 */
366 int
367 pfs_getattr (vp, vap, cred, p)
368 struct vnode *vp;
369 struct vattr *vap;
370 struct ucred *cred;
371 struct proc *p;
372 {
373 struct pfsnode *pfsp = VTOPFS(vp);
374 struct proc *procp;
375
376 VATTR_NULL(vap);
377 vap->va_type = vp->v_type;
378
379 if (vp->v_flag & VROOT) {
380 vap->va_mode = 0755;
381 vap->va_nlink = 2;
382 vap->va_size =
383 roundup((2+nprocs)*sizeof(struct pfsdent), DIRBLKSIZ);
384 vap->va_size_rsv = 0;
385 vap->va_uid = 0;
386 vap->va_gid = 0;
387 vap->va_atime = vap->va_mtime = vap->va_ctime = time; /*XXX*/
388 return 0;
389 }
390
391 procp = pfsp->pfs_pid?pfind(pfsp->pfs_pid):&proc0;
392 if (!procp)
393 return ESRCH;
394
395 vap->va_mode = 0700;
396 vap->va_nlink = 1;
397 vap->va_size = ctob( procp->p_vmspace->vm_tsize +
398 procp->p_vmspace->vm_dsize +
399 procp->p_vmspace->vm_ssize);
400 vap->va_size_rsv = 0;
401 vap->va_blocksize = page_size;
402 vap->va_uid = procp->p_ucred->cr_uid;
403 vap->va_gid = procp->p_ucred->cr_gid;
404 if (vap->va_uid != procp->p_cred->p_ruid)
405 vap->va_mode |= VSUID;
406 if (vap->va_gid != procp->p_cred->p_rgid)
407 vap->va_mode |= VSGID;
408 if (procp->p_flag & SLOAD) {
409 vap->va_atime = vap->va_mtime = vap->va_ctime =
410 procp->p_stats->p_start;
411 }
412
413 return 0;
414 }
415
416 int
417 pfs_access (vp, mode, cred, p)
418 struct vnode *vp;
419 int mode;
420 struct ucred *cred;
421 struct proc *p;
422 {
423 register struct vattr *vap;
424 register gid_t *gp;
425 struct vattr vattr;
426 register int i;
427 int error;
428
429 /*
430 * If you're the super-user,
431 * you always get access.
432 */
433 if (cred->cr_uid == 0)
434 return (0);
435 vap = &vattr;
436 if (error = pfs_getattr(vp, vap, cred, p))
437 return (error);
438 /*
439 * Access check is based on only one of owner, group, public.
440 * If not owner, then check group. If not a member of the
441 * group, then check public access.
442 */
443 if (cred->cr_uid != vap->va_uid) {
444 mode >>= 3;
445 gp = cred->cr_groups;
446 for (i = 0; i < cred->cr_ngroups; i++, gp++)
447 if (vap->va_gid == *gp)
448 goto found;
449 mode >>= 3;
450 found:
451 ;
452 }
453 if ((vap->va_mode & mode) != 0)
454 return (0);
455 return (EACCES);
456 }
457
458 /*
459 * /proc lookup
460 */
461 int
462 pfs_lookup(vp, ndp, p)
463 register struct vnode *vp;
464 register struct nameidata *ndp;
465 struct proc *p;
466 {
467 int lockparent, wantparent, flag, error = 0;
468 pid_t pid;
469 struct vnode *nvp;
470 struct pfsnode *pfsp;
471 struct proc *procp;
472
473 #ifdef DEBUG
474 if (pfs_debug)
475 printf("pfs_lookup: vp 0x%x name %s proc %d\n",
476 vp, ndp->ni_ptr, p->p_pid);
477 #endif
478
479 ndp->ni_dvp = vp;
480 ndp->ni_vp = NULL;
481 if (vp->v_type != VDIR)
482 return (ENOTDIR);
483
484 lockparent = ndp->ni_nameiop & LOCKPARENT;
485 flag = ndp->ni_nameiop & OPMASK;
486 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
487 if (flag != LOOKUP)
488 return EACCES;
489 if (ndp->ni_isdotdot) {
490 /* Should not happen */
491 printf("pfs_lookup: vp 0x%x: dotdot\n", vp);
492 return EIO;
493 }
494 if (ndp->ni_namelen == 1 && *ndp->ni_ptr == '.') {
495 VREF(vp);
496 ndp->ni_vp = vp;
497 return 0;
498 }
499
500 pid = (pid_t)atoi(ndp->ni_ptr, ndp->ni_namelen);
501 if (pid == (pid_t)-1)
502 return ENOENT;
503
504 if ((procp = pid?pfind(pid):&proc0) == NULL)
505 return ENOENT;
506
507 for (pfsp = pfshead; pfsp != NULL; pfsp = pfsp->pfs_next) {
508 if (pfsp->pfs_pid == pid)
509 break;
510 }
511
512 if (pfsp == NULL) {
513 struct pfsnode **pp;
514 error = getnewvnode(VT_PROCFS, vp->v_mount, &pfs_vnodeops, &nvp);
515 if (error)
516 return error;
517
518 nvp->v_type = VPROC;
519 pfsp = VTOPFS(nvp);
520 pfsp->pfs_pid = pid;
521 pfsp->pfs_vnode = nvp;
522 for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next);
523 *pp = pfsp;
524
525 }
526 ndp->ni_vp = pfsp->pfs_vnode;
527
528 return (error);
529 }
530
531 int
532 pfs_readdir(vp, uio, cred, eofflagp)
533 struct vnode *vp;
534 register struct uio *uio;
535 struct ucred *cred;
536 int *eofflagp;
537 {
538 int error = 0;
539 int count, lost, pcnt, skipcnt, doingzomb = 0;
540 struct proc *p;
541 struct pfsdent dent;
542
543 #ifdef DEBUG
544 if (pfs_debug)
545 printf("pfs_readdir: vp 0x%x proc %d\n",
546 vp, uio->uio_procp->p_pid);
547 #endif
548 count = uio->uio_resid;
549 count &= ~(DIRBLKSIZ - 1);
550 lost = uio->uio_resid - count;
551 if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
552 return (EINVAL);
553 uio->uio_resid = count;
554 uio->uio_iov->iov_len = count;
555 *eofflagp = 1;
556 skipcnt = uio->uio_offset / sizeof(struct pfsdent);
557
558 count = 0;
559 if (skipcnt == 0) {
560 /* Fake "." and ".." entries? */
561 #if 1
562 dent.d_fileno = 2; /* XXX - Filesystem root */
563 dent.d_reclen = sizeof(struct pfsdent);
564
565 dent.d_namlen = 1;
566 dent.d_nam[0] = '.';
567 dent.d_nam[1] = '\0';
568 error = uiomove((char *)&dent, sizeof(struct pfsdent) , uio);
569 if (error)
570 return error;
571
572 dent.d_fileno = 2;
573 dent.d_namlen = 2;
574 dent.d_nam[1] = '.';
575 dent.d_nam[2] = '\0';
576 error = uiomove((char *)&dent, sizeof(struct pfsdent) , uio);
577 if (error)
578 return error;
579 #endif
580 count += 2*dent.d_reclen;
581 }
582
583 p = allproc;
584 for (pcnt = 0; p && uio->uio_resid; pcnt++) {
585 if (pcnt < skipcnt) {
586 p = p->p_nxt;
587 if (p == NULL && doingzomb == 0) {
588 doingzomb = 1;
589 p = zombproc;
590 }
591 continue;
592 }
593 *eofflagp = 0;
594
595 /* "inode" is process slot (actually position on list) */
596 dent.d_fileno = (unsigned long)(pcnt+1);
597 dent.d_namlen = itos((unsigned int)p->p_pid, dent.d_nam);
598 dent.d_nam[dent.d_namlen] = '\0';
599
600 p = p->p_nxt;
601 if (p == NULL && doingzomb == 0) {
602 doingzomb = 1;
603 p = zombproc;
604 }
605 if (p == NULL) {
606 /* Extend 'reclen' to end of block */;
607 dent.d_reclen = DIRBLKSIZ - (count & (DIRBLKSIZ - 1));
608 } else
609 dent.d_reclen = sizeof(struct pfsdent);
610 count += dent.d_reclen;
611 error = uiomove((char *)&dent, dent.d_reclen, uio);
612 if (error)
613 break;
614 }
615 if (count == 0)
616 *eofflagp = 1;
617
618 uio->uio_resid += lost;
619 return error;
620 }
621
622 /*
623 * convert n to decimal representation in character array b
624 * return number of decimal digits produced.
625 */
626 int
627 itos(n, b)
628 unsigned int n;
629 char *b;
630 {
631 #define BASE 10
632 int m = (n<BASE)?0:itos(n/BASE, b);
633
634 *(b+m) = "0123456789abcdef"[n%BASE];
635 return m+1;
636 }
637
638 /*
639 * convert decimal ascii representation in b of length len to integer
640 */
641 int
642 atoi(b, len)
643 char *b;
644 unsigned int len;
645 {
646 int n = 0;
647
648 while (len--) {
649 register char c = *b++;
650 if (c < '0' || c > '9')
651 return -1;
652 n = 10 * n + (c - '0');
653 }
654 return n;
655 }
656