123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- #include "u.h"
- #include "../port/lib.h"
- #include "mem.h"
- #include "dat.h"
- #include "fns.h"
- #include "../port/error.h"
- /* Qid is (2*fd + (file is ctl))+1 */
- static int
- dupgen(Chan *c, char *d, Dirtab* dir, int mm, int s, Dir *dp)
- {
- Mach *m = machp();
- Fgrp *fgrp = m->externup->fgrp;
- Chan *f;
- static int perm[] = { 0400, 0200, 0600, 0 };
- int p;
- Qid q;
- if(s == DEVDOTDOT){
- devdir(c, c->qid, ".", 0, eve, DMDIR|0555, dp);
- return 1;
- }
- if(s == 0)
- return 0;
- s--;
- if(s/2 > fgrp->maxfd)
- return -1;
- if((f=fgrp->fd[s/2]) == nil)
- return 0;
- if(s & 1){
- p = 0400;
- sprint(m->externup->genbuf, "%dctl", s/2);
- }else{
- p = perm[f->mode&3];
- sprint(m->externup->genbuf, "%d", s/2);
- }
- mkqid(&q, s+1, 0, QTFILE);
- devdir(c, q, m->externup->genbuf, 0, eve, p, dp);
- return 1;
- }
- static Chan*
- dupattach(char *spec)
- {
- return devattach('d', spec);
- }
- static Walkqid*
- dupwalk(Chan *c, Chan *nc, char **name, int nname)
- {
- return devwalk(c, nc, name, nname, (Dirtab *)0, 0, dupgen);
- }
- static int32_t
- dupstat(Chan *c, uint8_t *db, int32_t n)
- {
- return devstat(c, db, n, (Dirtab *)0, 0L, dupgen);
- }
- static Chan*
- dupopen(Chan *c, int omode)
- {
- Chan *f;
- int fd, twicefd;
- if(c->qid.type & QTDIR){
- if(omode != 0)
- error(Eisdir);
- c->mode = 0;
- c->flag |= COPEN;
- c->offset = 0;
- return c;
- }
- if(c->qid.type & QTAUTH)
- error(Eperm);
- twicefd = c->qid.path - 1;
- fd = twicefd/2;
- if((twicefd & 1)){
- /* ctl file */
- f = c;
- f->mode = openmode(omode);
- f->flag |= COPEN;
- f->offset = 0;
- }else{
- /* fd file */
- f = fdtochan(fd, openmode(omode), 0, 1);
- cclose(c);
- }
- if(omode & OCEXEC)
- f->flag |= CCEXEC;
- return f;
- }
- static void
- dupclose(Chan* c)
- {
- }
- static int32_t
- dupread(Chan *c, void *va, int32_t n, int64_t off)
- {
- char buf[256];
- int fd, twicefd;
- if(c->qid.type & QTDIR)
- return devdirread(c, va, n, (Dirtab *)0, 0L, dupgen);
- twicefd = c->qid.path - 1;
- fd = twicefd/2;
- if(twicefd & 1){
- c = fdtochan(fd, -1, 0, 1);
- procfdprint(c, fd, 0, buf, sizeof buf);
- cclose(c);
- return readstr(off, va, n, buf);
- }
- panic("dupread");
- return 0;
- }
- static int32_t
- dupwrite(Chan* c, void* v, int32_t i, int64_t n)
- {
- error(Eperm);
- return 0; /* not reached */
- }
- Dev dupdevtab = {
- 'd',
- "dup",
- devreset,
- devinit,
- devshutdown,
- dupattach,
- dupwalk,
- dupstat,
- dupopen,
- devcreate,
- dupclose,
- dupread,
- devbread,
- dupwrite,
- devbwrite,
- devremove,
- devwstat,
- };
|