| plan 9 kernel history: overview | file list | diff list |
1991/1109/port/devsrv.c (diff list | history)
| port/devsrv.c on 1990/0227 | ||
| 1990/0227 | #include "u.h" #include "lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "errno.h" | |
| 1991/0828 | ||
| 1990/0227 | #include "devtab.h" | |
| 1991/0828 | typedef struct Srv Srv; struct Srv{ | |
| 1990/0227 | Lock; | |
| 1991/0828 | char *name; Chan **chan; }srv; | |
| 1990/0227 | ||
| 1991/0828 | int srvgen(Chan *c, Dirtab *tab, int ntab, int s, Dir *dp) { if(s >= conf.nsrv) return -1; if(srv.chan[s] == 0) return 0; | |
| 1991/1109 | devdir(c, (Qid){s, 0}, &srv.name[s*NAMELEN], 0, eve, 0666, dp); | |
| 1991/0828 | return 1; } | |
| 1990/0227 | void srvinit(void) { } void srvreset(void) { | |
| 1991/0828 | srv.chan = ialloc(conf.nsrv*sizeof(Chan*), 0); srv.name = ialloc(conf.nsrv*NAMELEN, 0); | |
| 1990/0227 | } Chan * srvattach(char *spec) { | |
| 1991/0828 | return devattach('s', spec); | |
| 1990/0227 | } Chan * srvclone(Chan *c, Chan *nc) { | |
| 1991/0828 | return devclone(c, nc); | |
| 1990/0227 | } int srvwalk(Chan *c, char *name) { | |
| 1991/0828 | return devwalk(c, name, 0, 0, srvgen); | |
| 1990/0227 | } void srvstat(Chan *c, char *db) { | |
| 1991/0828 | devstat(c, db, 0, 0, srvgen); | |
| 1990/0227 | } Chan * srvopen(Chan *c, int omode) { Chan *f; | |
| 1991/0828 | if(c->qid.path == CHDIR){ | |
| 1990/1110 | if(omode != OREAD) | |
| 1990/11211 | error(Eisdir); | |
| 1990/0227 | c->mode = omode; c->flag |= COPEN; c->offset = 0; return c; } | |
| 1991/0828 | lock(&srv); if(waserror()){ unlock(&srv); nexterror(); } f = srv.chan[c->qid.path]; if(f == 0) | |
| 1990/11211 | error(Eshutdown); | |
| 1991/0828 | if(omode&OTRUNC) | |
| 1990/11211 | error(Eperm); | |
| 1990/1110 | if(omode!=f->mode && f->mode!=ORDWR) | |
| 1990/11211 | error(Eperm); | |
| 1990/0227 | close(c); incref(f); | |
| 1991/0828 | unlock(&srv); poperror(); | |
| 1990/0227 | return f; } void srvcreate(Chan *c, char *name, int omode, ulong perm) { | |
| 1991/0828 | int j, i; | |
| 1990/0227 | ||
| 1991/0828 | if(omode != OWRITE) error(Eperm); lock(&srv); if(waserror()){ unlock(&srv); | |
| 1990/0227 | nexterror(); } | |
| 1991/0828 | j = -1; for(i=0; i<conf.nsrv; i++){ if(srv.chan[i] == 0){ if(j == -1) j = i; }else if(strcmp(name, &srv.name[i*NAMELEN]) == 0) | |
| 1990/11211 | error(Einuse); | |
| 1991/0828 | } if(j == -1) error(Enosrv); srv.chan[j] = c; unlock(&srv); | |
| 1990/1002 | poperror(); | |
| 1991/0828 | strcpy(&srv.name[j*NAMELEN], name); c->qid.path = j; | |
| 1990/0227 | c->flag |= COPEN; | |
| 1991/0828 | c->mode = OWRITE; | |
| 1990/0227 | } void srvremove(Chan *c) { | |
| 1991/0828 | Chan *f; | |
| 1990/0227 | ||
| 1991/0828 | if(c->qid.path == CHDIR) | |
| 1990/11211 | error(Eperm); | |
| 1991/0828 | lock(&srv); | |
| 1990/1110 | if(waserror()){ | |
| 1991/0828 | unlock(&srv); | |
| 1990/0227 | nexterror(); } | |
| 1991/0828 | f = srv.chan[c->qid.path]; if(f == 0) error(Eshutdown); if(strcmp(&srv.name[c->qid.path*NAMELEN], "boot") == 0) error(Eperm); srv.chan[c->qid.path] = 0; unlock(&srv); | |
| 1990/0227 | poperror(); | |
| 1991/0828 | close(f); | |
| 1990/0227 | } void srvwstat(Chan *c, char *dp) { | |
| 1990/11211 | error(Egreg); | |
| 1990/0227 | } void srvclose(Chan *c) { } long | |
| 1991/0411 | srvread(Chan *c, void *va, long n, ulong offset) | |
| 1990/0227 | { | |
| 1990/1002 | isdir(c); | |
| 1991/0828 | return devdirread(c, va, n, 0, 0, srvgen); | |
| 1990/0227 | } long | |
| 1991/0411 | srvwrite(Chan *c, void *va, long n, ulong offset) | |
| 1990/0227 | { int i, fd; char buf[32]; | |
| 1991/0828 | i = c->qid.path; if(srv.chan[i] != c) /* already been written to */ | |
| 1990/11211 | error(Egreg); | |
| 1990/1110 | if(n >= sizeof buf) | |
| 1990/11211 | error(Egreg); | |
| 1991/0318 | memmove(buf, va, n); /* so we can NUL-terminate */ | |
| 1990/0227 | buf[n] = 0; fd = strtoul(buf, 0, 0); | |
| 1991/1011 | fdtochan(fd, -1, 0); /* error check only */ | |
| 1991/0828 | srv.chan[i] = u->p->fgrp->fd[fd]; | |
| 1991/0921 | incref(srv.chan[i]); | |
| 1990/0227 | return n; } | |