void mntinit(int argc, char **argv) { int tries; config = "config"; starttime = time(0); clog("nfs mount server init, starttime = %lud\n", starttime); tries = 0; ARGBEGIN{ case 'a': ++tries; srvinit(-1, 0, EARGF(usage())); break; case 'c': config = EARGF(usage()); break; case 'f': ++tries; srvinit(-1, EARGF(usage()), 0); break; case 'n': ++noauth; break; case 's': ++tries; srvinit(1, 0, 0); break; case 'T': staletime = atoi(EARGF(usage())); break; default: if(argopt(ARGC()) < 0) sysfatal("usage: %s %s [-ns] [-a dialstring] " "[-c uidmap] [-f srvfile] [-T staletime]", argv0, commonopts); break; }ARGEND noauth=1; /* ZZZ */ if(tries == 0 && head == 0) srvinit(-1, 0, "tcp!fs"); if(head == 0) panic("can't initialize services"); readunixidmaps(config); }
void Cputnm(Cdimg *cd, ulong val, int size) { switch(size) { default: sysfatal("bad size %d in bputnl", size); case 2: Cputc(cd, val>>8); Cputc(cd, val); break; case 4: Cputc(cd, val>>24); Cputc(cd, val>>16); Cputc(cd, val>>8); Cputc(cd, val); break; } }
void threadmain(int argc, char *argv[]) { IFile ifile; ARGBEGIN{ default: usage(); }ARGEND if(argc != 1) usage(); if(readifile(&ifile, argv[0]) < 0) sysfatal("readifile %s: %r", argv[0]); write(1, ifile.b->data, ifile.b->len); threadexitsall(nil); }
static void p_compile(Filter *f) { Mux *m; if(f->op == '='){ compile_cmp(aoerr.name, f, p_fields); return; } for(m = p_mux; m->name; m++) if(strcmp(f->s, m->name) == 0){ f->pr = m->pr; f->ulv = m->val; f->subop = Ocmd; return; } sysfatal("unknown aoemask field: %s", f->s); }
void loadmboxfaces(char *maildir) { CFid *dirfd; Dir *d; int i, n; dirfd = fsopen(mailfs, maildir, OREAD); if(dirfd != nil){ while((n = fsdirread(dirfd, &d)) > 0){ for(i=0; i<n; i++) addface(dirface(maildir, d[i].name)); free(d); } fsclose(dirfd); }else sysfatal("open %s: %r", maildir); }
static void p_compile(Filter *f) { Mux *m; if(f->op == '='){ compile_cmp(ip6.name, f, p_fields); return; } for(m = p_mux; m->name != nil; m++) if(strcmp(f->s, m->name) == 0){ f->pr = m->pr; f->ulv = m->val; f->subop = Ot; return; } sysfatal("unknown ip6 field or protocol: %s", f->s); }
Image * eloadfile(char *path) { Image *img; int fd; fd = open(path, OREAD); if(fd < 0) { fprint(2, "cannot open image file %s: %r\n", path); exits("image"); } img = readimage(display, fd, 0); if(img == nil) sysfatal("cannot load image: %r"); close(fd); return img; }
void freefid(Mfile *mf) { Mfile **l; lock(&mfalloc.lk); for(l = &mfalloc.inuse; *l != nil; l = &(*l)->next){ if(*l == mf){ *l = mf->next; if(mf->user) free(mf->user); free(mf); unlock(&mfalloc.lk); return; } } sysfatal("freeing unused fid"); }
char* estrappend(char *s, char *fmt, ...) { char *t; int l; va_list arg; va_start(arg, fmt); t = vsmprint(fmt, arg); if(t == nil) sysfatal("out of memory"); va_end(arg); l = s ? strlen(s) : 0; s = erealloc(s, l+strlen(t)+1); strcpy(s+l, t); free(t); return s; }
static int opennext(int out, char *prefix) { static int filenum = 0; if (out >= 0) { fprint(2, "%s: opennext called with file open\n", argv0); exits("open botch"); } free(filenm); filenm = smprint("%s%.5d", prefix, filenum++); fprint(2, "%s: %s ...", filenm, thisnm); out = create(filenm, OWRITE, 0666); if (out < 0) sysfatal("%s: %r", filenm); newarch(); return out; }
void handleagentmsg(Msg *m) { uint32_t chan, len; int n; Achan *a; assert(m->type == SSH_MSG_CHANNEL_DATA); debug(DBG_AUTH, "agent data\n"); debug(DBG_AUTH, "\t%.*H\n", (int)(m->ep - m->rp), m->rp); chan = getlong(m); len = getlong(m); if(m->rp+len != m->ep) sysfatal("got bad channel data"); if(chan >= nelem(achan)) error("bad channel in agent request"); a = &achan[chan]; while(m->rp < m->ep){ if(a->nlbuf < 4){ a->lbuf[a->nlbuf++] = getbyte(m); if(a->nlbuf == 4){ a->len = (a->lbuf[0]<<24) | (a->lbuf[1]<<16) | (a->lbuf[2]<<8) | a->lbuf[3]; a->data = erealloc(a->data, a->len); a->ndata = 0; } continue; } if(a->ndata < a->len){ n = a->len - a->ndata; if(n > m->ep - m->rp) n = m->ep - m->rp; memmove(a->data+a->ndata, getbytes(m, n), n); a->ndata += n; } if(a->ndata == a->len){ handlefullmsg(m->c, a); a->nlbuf = 0; } } }
void redirectinit(void) { static Biobuf *b = nil; static Qid qid; char *file, *line, *s, *host, *field[3]; static char pfx[] = "http://"; file = "/sys/lib/httpd.rewrite"; if(b != nil){ if(updateQid(Bfildes(b), &qid) == 0) return; Bterm(b); } b = Bopen(file, OREAD); if(b == nil) sysfatal("can't read from %s", file); updateQid(Bfildes(b), &qid); cleartab(redirtab); cleartab(vhosttab); while((line = Brdline(b, '\n')) != nil){ line[Blinelen(b)-1] = 0; s = strchr(line, '#'); if(s != nil && (s == line || s[-1] == ' ' || s[-1] == '\t')) *s = '\0'; /* chop comment iff after whitespace */ if(tokenize(line, field, nelem(field)) == 2){ if(strncmp(field[0], pfx, STRLEN(pfx)) == 0 && strncmp(undecorated(field[1]), pfx, STRLEN(pfx)) != 0){ /* url -> filename */ host = field[0] + STRLEN(pfx); s = strrchr(host, '/'); if(s) *s = 0; /* chop trailing slash */ insert(vhosttab, estrdup(host), estrdup(field[1])); }else{ insert(redirtab, estrdup(field[0]), estrdup(field[1])); } } } syslog(0, HTTPLOG, "redirectinit pid=%d", getpid()); }
void freefid(Mfile *mf) { Mfile **l; lock(&mfalloc); for(l = &mfalloc.inuse; *l != nil; l = &(*l)->next) if(*l == mf){ *l = mf->next; if(mf->user) free(mf->user); memset(mf, 0, sizeof *mf); /* cause trouble */ free(mf); unlock(&mfalloc); return; } unlock(&mfalloc); sysfatal("freeing unused fid"); }
void vtentrypack(VtEntry *e, uchar *p, int index) { ulong t32; int flags; uchar *op; int depth; int psize, dsize; p += index * VtEntrySize; op = p; depth = e->type&VtTypeDepthMask; flags = (e->flags&~(_VtEntryDir|_VtEntryDepthMask)); flags |= depth << _VtEntryDepthShift; if(e->type - depth == VtDirType) flags |= _VtEntryDir; U32PUT(p, e->gen); p += 4; psize = e->psize; dsize = e->dsize; if(psize >= (1<<16) || dsize >= (1<<16)) { flags |= _VtEntryBig; psize = vttobig(psize); dsize = vttobig(dsize); if(psize < 0 || dsize < 0) sysfatal("invalid entry psize/dsize: %ld/%ld", e->psize, e->dsize); } U16PUT(p, psize); p += 2; U16PUT(p, dsize); p += 2; U8PUT(p, flags); p++; memset(p, 0, 5); p += 5; U48PUT(p, e->size, t32); p += 6; memmove(p, e->score, VtScoreSize); p += VtScoreSize; assert(p-op == VtEntrySize); }
static int emptyentry(Channel *c) { int i, extra; assert((c->nentry==0 && c->qentry==nil) || (c->nentry && c->qentry)); for(i=0; i<c->nentry; i++) if(c->qentry[i]==nil) return i; extra = 16; c->nentry += extra; c->qentry = realloc((void*)c->qentry, c->nentry*sizeof(c->qentry[0])); if(c->qentry == nil) sysfatal("realloc channel entries: %r"); memset(&c->qentry[i], 0, extra*sizeof(c->qentry[0])); return i; }
static enum mad_flow input(void *data, struct mad_stream *stream) { int fd, n, m; static uint8_t buf[32768]; fd = (int)data; n = stream->bufend - stream->next_frame; memmove(buf, stream->next_frame, n); m = read(fd, buf+n, sizeof buf-n); offset += m; if(m < 0) sysfatal("reading input: %r"); if(m == 0) return MAD_FLOW_STOP; n += m; mad_stream_buffer(stream, buf, n); return MAD_FLOW_CONTINUE; }
static int fsopen(Usbfs *fs, Fid *fid, int omode) { int qt; int64_t qid; Conn *c; Dirtab *tab; Ether *e; qid = fid->qid.path & ~fs->qid; e = fs->aux; qt = qtype(qid); tab = qdirtab(qid); omode &= 3; if(omode != OREAD && (tab->mode&0222) == 0){ werrstr(Eperm); return -1; } switch(qt){ case Qclone: c = newconn(e); if(c == nil){ werrstr("no more connections"); return -1; } fid->qid.type = QTFILE; fid->qid.path = mkqid(c->nb, Qnctl)|fs->qid; fid->qid.vers = 0; break; case Qndata: case Qnctl: case Qnifstats: case Qnstats: case Qntype: c = getconn(e, qnum(qid), 1); if(c == nil) sysfatal("usb: ether: fsopen bug"); incref(c); break; } etherdump(e); return 0; }
void main(int argc, char *argv[]) { int f, i; argv0 = "cat"; if(argc == 1) cat(0, "<stdin>"); else for(i=1; i<argc; i++){ f = open(argv[i], OREAD); if(f < 0) sysfatal("can't open %s: %r", argv[i]); else{ cat(f, argv[i]); close(f); } } exits(0); }
void main(int argc, char* argv[]) { if(argc > 2) { fprint(2, "usage: %s [file]\n", argv[0]); exits("usage"); } // Run script from file given as parameter instead of stdin if necesary if(argc == 2) redirect_stdin(argv[1]); // Allocate buffer for standard input Biobuf bin; if(Binit(&bin, 0, OREAD)) sysfatal("%s: standard input: %r", argv0); tHeredoc heredoc; heredoc.mode = 0; // Main loop char* line; while((line = Brdstr(&bin, '\n', 0))) { // Process heredoc line = heredoc_process(&heredoc, line); if(line) { // Process script script_process(line); // Free procesed line free(line); } } // Free stdin buffer Bterm(&bin); // Exit sucesfully exits(nil); }
/* Network on fd1, mount driver on fd0 */ int filter(int fd, char *cmd, char *host) { int p[2], len, argc; char newport[256], buf[256], *s; char *argv[16], *file, *pbuf; if ((len = read(fd, newport, sizeof newport - 1)) < 0) sysfatal("filter: cannot write port; %r"); newport[len] = '\0'; if ((s = strchr(newport, '!')) == nil) sysfatal("filter: illegally formatted port %s", newport); strecpy(buf, buf+sizeof buf, netmkaddr(host, "tcp", "0")); pbuf = strrchr(buf, '!'); strecpy(pbuf, buf+sizeof buf, s); if(debug) fprint(2, "filter: remote port %s\n", newport); argc = tokenize(cmd, argv, nelem(argv)-2); if (argc == 0) sysfatal("filter: empty command"); argv[argc++] = "-c"; argv[argc++] = buf; argv[argc] = nil; file = argv[0]; if (s = strrchr(argv[0], '/')) argv[0] = s+1; if(pipe(p) < 0) sysfatal("pipe: %r"); switch(rfork(RFNOWAIT|RFPROC|RFFDG)) { case -1: sysfatal("rfork record module: %r"); case 0: dup(p[0], 1); dup(p[0], 0); close(p[0]); close(p[1]); exec(file, argv); sysfatal("exec record module: %r"); default: close(fd); close(p[0]); } return p[1]; }
/* * DigestInfo ::= SEQUENCE { * digestAlgorithm AlgorithmIdentifier, * digest OCTET STRING * } * * except that OpenSSL seems to sign * * DigestInfo ::= SEQUENCE { * SEQUENCE{ digestAlgorithm AlgorithmIdentifier, NULL } * digest OCTET STRING * } * * instead. Sigh. */ static int mkasn1(uchar *asn1, DigestAlg *alg, uchar *d, uint dlen) { uchar *obj, *p; uint olen; if(alg == sha1) { obj = oidsha1; olen = sizeof(oidsha1); } else if(alg == md5) { obj = oidmd5; olen = sizeof(oidmd5); } else { sysfatal("bad alg in mkasn1"); return -1; } p = asn1; *p++ = 0x30; /* sequence */ p++; *p++ = 0x30; /* another sequence */ p++; *p++ = 0x06; /* object id */ *p++ = olen; memmove(p, obj, olen); p += olen; *p++ = 0x05; /* null */ *p++ = 0; asn1[3] = p - (asn1+4); /* end of inner sequence */ *p++ = 0x04; /* octet string */ *p++ = dlen; memmove(p, d, dlen); p += dlen; asn1[1] = p - (asn1+2); /* end of outer sequence */ return p-asn1; }
void threadmain(int argc, char **argv) { int pid; char *cmd = nil; char **args = nil; ARGBEGIN{ case 'c': cmd = strdup(EARGF(usage())); args = argv; break; default: usage(); }ARGEND; /* run a command? */ if(cmd) { pid = fork(); if (pid < 0) { fprint(2, "No fork: %r\n"); exits("fork failed"); } if(pid == 0) { hang(getpid()); exec(cmd, args); fprint(2, "Bad exec: %s: %r\n", cmd); exits("Bad exec"); } } else { if(argc != 1) sysfatal("usage"); pid = atoi(argv[0]); } out = chancreate(sizeof(char*), 0); quit = chancreate(sizeof(char*), 0); forkc = chancreate(sizeof(ulong *), 0); nread++; procrfork(writer, nil, 8192, 0); reader((void*)pid); }
void main(int argc, char **argv) { char *addr = nil; char *srvname = nil; char *mtpt = nil; Qid q; fs.tree = alloctree(nil, nil, DMDIR|0777, fsdestroyfile); q = fs.tree->root->qid; ARGBEGIN{ case 'D': chatty9p++; break; case 'a': addr = EARGF(usage()); break; case 's': srvname = EARGF(usage()); break; case 'm': mtpt = EARGF(usage()); break; default: usage(); }ARGEND; if(argc) usage(); if(chatty9p) fprint(2, "ramsrv.nopipe %d srvname %s mtpt %s\n", fs.nopipe, srvname, mtpt); if(addr == nil && srvname == nil && mtpt == nil) sysfatal("must specify -a, -s, or -m option"); if(addr) listensrv(&fs, addr); if(srvname || mtpt) postmountsrv(&fs, srvname, mtpt, MREPL|MCREATE); exits(0); }
/* * Read a list of parameters. Each parameter is a name and a type. * The list ends with a ')'. We have already read the '('. */ static struct params * read_params(int *poffset) { char *token; struct params *ret, **pp, *p; int offset, size, rnd; ret = NULL; pp = &ret; token = read_token_no_eof(); offset = 0; if (strcmp(token, ")") != 0) { while (1) { p = xmalloc(sizeof(struct params)); p->name = token; p->type = read_type(); p->next = NULL; *pp = p; pp = &p->next; size = type_size(p->type); rnd = size; if(rnd > structround) rnd = structround; if(offset%rnd) offset += rnd - offset%rnd; offset += size; token = read_token_no_eof(); if (strcmp(token, ",") != 0) break; token = read_token_no_eof(); } } if (strcmp(token, ")") != 0) { sysfatal("%s:%ud: expected '('\n", file, lineno); } if (poffset != NULL) *poffset = offset; return ret; }
void main(int argc, char *argv[]) { int i, ok, stdout; level = 6; stdout = 0; ARGBEGIN{ case 'D': debug++; break; case 'v': verbose++; break; case 'c': stdout = 1; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': level = ARGC() - '0'; break; default: usage(); break; }ARGEND crctab = mkcrctab(GZCRCPOLY); ok = deflateinit(); if(ok != FlateOk) sysfatal("deflateinit failed: %s", flateerr(ok)); if(argc == 0){ Binit(&bout, 1, OWRITE); ok = gzip(nil, time(0), 0, &bout); Bterm(&bout); }else{ ok = 1; for(i = 0; i < argc; i++) ok &= gzipf(argv[i], stdout); } exits(ok ? nil: "errors"); }
int addaddr(char *addr) { Addr **l; Addr *a; for(l = &al; *l; l = &(*l)->next){ if(strcmp(addr, (*l)->addr) == 0) return 0; } na++; *l = a = malloc(sizeof(*a)+strlen(addr)+1); if(a == nil) sysfatal("allocating: %r"); a->addr = (char*)&a[1]; strcpy(a->addr, addr); a->next = nil; *l = a; return 1; }
Memimage* upsidedown(Memimage *m) { uchar *s, *d, *t; int w, y, dy; dy = Dy(m->r); w = m->width * sizeof(ulong); if((t = malloc(w)) == nil) sysfatal("malloc: %r"); for(y=0; y<dy/2; y++){ s = byteaddr(m, addpt(m->r.min, Pt(0, y))); d = byteaddr(m, addpt(m->r.min, Pt(0, dy-y-1))); memmove(t, d, w); memmove(d, s, w); memmove(s, t, w); } free(t); return m; }
static void closehub(Hub *h) { Hub **hl; dprint(2, "%s: closing hub %#p\n", argv0, h); for(hl = &hubs; *hl != nil; hl = &(*hl)->next) if(*hl == h) break; if(*hl == nil) sysfatal("closehub: no hub"); *hl = h->next; nhubs--; hubfail(h); /* detach all ports */ free(h->port); assert(h->dev != nil); devctl(h->dev, "detach"); closedev(h->dev); free(h); }
// guarantee at least n significant bits void mpbits(mpint *b, int m) { int n; n = DIGITS(m); if(b->size >= n){ if(b->top >= n) return; memset(&b->p[b->top], 0, Dbytes*(n - b->top)); b->top = n; return; } b->p = (mpdigit*)realloc(b->p, n*Dbytes); if(b->p == nil) sysfatal("mpbits: %r"); memset(&b->p[b->top], 0, Dbytes*(n - b->top)); b->size = n; b->top = n; }
static void pop3cmd(Pop *pop, char *fmt, ...) { char buf[128], *p; va_list va; va_start(va, fmt); vseprint(buf, buf+sizeof(buf), fmt, va); va_end(va); p = buf+strlen(buf); if(p > (buf+sizeof(buf)-3)) sysfatal("pop3 command too long"); if(pop->debug) fprint(2, "<- %s\n", buf); strcpy(p, "\r\n"); Bwrite(&pop->bout, buf, strlen(buf)); Bflush(&pop->bout); }