Npcfid * npc_opendir (Npcfid *root, char *path) { Npcfid *fid; if ((fid = npc_open_bypath (root, path, O_RDONLY))) { fid->dbuf_size = root->fsys->msize - P9_IOHDRSZ; if (!(fid->dbuf = malloc (fid->dbuf_size))) { (void)npc_closedir (fid); np_uerror (ENOMEM); fid = NULL; } fid->offset = 0; fid->dbuf_len = 0; fid->dbuf_used = 0; } return fid; }
char * npc_aget(Npcfid *root, char *path) { int n, len; Npcfid *fid = NULL; char *s = NULL;; int ssize = 0; if (!(fid = npc_open_bypath(root, path, O_RDONLY))) goto error; len = 0; do { if (!s) { ssize = AGET_CHUNK; s = malloc (ssize); } else if (ssize - len == 1) { ssize += AGET_CHUNK; s = realloc (s, ssize); } if (!s) { np_uerror (ENOMEM); goto error; } n = npc_read(fid, s + len, ssize - len - 1); if (n > 0) { len += n; if ((fid->fsys->flags & NPC_SHORTREAD_EOF) && (len - n < ssize - len - 1)) break; } } while (n > 0); if (n < 0) goto error; if (npc_clunk (fid) < 0) goto error; s[len] = '\0'; return s; error: if (s) free (s); if (fid) (void)npc_clunk (fid); return NULL; }
int npc_put(Npcfid *root, char *path, void *buf, u32 count) { int n, done = 0; Npcfid *fid; if (!(fid = npc_open_bypath (root, path, O_WRONLY))) return -1; while (done < count) { n = npc_write(fid, buf + done, count - done); if (n < 0) { done = -1; break; } done += n; } if (npc_clunk (fid) < 0) done = -1; return done; }
int npc_get(Npcfid *root, char *path, void *buf, u32 count) { int n, len = 0; Npcfid *fid; if (!(fid = npc_open_bypath(root, path, O_RDONLY))) return -1; while (len < count) { n = npc_read(fid, buf + len, count - len); if (n < 0) return -1; if (n == 0) break; len += n; if ((fid->fsys->flags & NPC_SHORTREAD_EOF) && (len - n < count - len)) break; } if (npc_clunk (fid) < 0) return -1; return len; }
int main (int argc, char *argv[]) { char *server = NULL; int msize = 65536; uid_t uid = geteuid (); int topt = 0; Npcfsys *fs = NULL; Npcfid *fid, *afid, *root; int c, fd; char buf[80], *host, *p; hostlist_t hl; hostlist_iterator_t itr; int lopt = 0; diod_log_init (argv[0]); opterr = 0; while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) { switch (c) { case 's': /* --server HOST[:PORT] or /path/to/socket */ server = optarg; break; case 'm': /* --msize SIZE */ msize = strtoul (optarg, NULL, 10); break; case 'u': /* --uid UID */ uid = strtoul (optarg, NULL, 10); break; case 't': /* --timeout SECS */ topt = strtoul (optarg, NULL, 10); break; case 'l': /* --long */ lopt = 1; break; default: usage (); } } if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) err_exit ("signal"); if (signal (SIGALRM, sigalarm) == SIG_ERR) err_exit ("signal"); if (topt > 0) alarm (topt); if ((fd = diod_sock_connect (server, 0)) < 0) exit (1); if (!(fs = npc_start (fd, fd, msize, 0))) errn_exit (np_rerror (), "error negotiating protocol with server"); if (!(afid = npc_auth (fs, "ctl", uid, diod_auth)) && np_rerror () != 0) errn_exit (np_rerror (), "error authenticating to server"); if (!(root = npc_attach (fs, afid, "ctl", uid))) errn_exit (np_rerror (), "error attaching to aname=ctl"); if (!(fid = npc_open_bypath (root, "connections", O_RDONLY))) errn_exit (np_rerror (), "open connections"); if (!(hl = hostlist_create (NULL))) err_exit ("hostlist_create"); while (npc_gets (fid, buf, sizeof(buf))) { if ((p = strchr (buf, ' '))) *p = '\0'; if (!lopt && (p = strchr (buf, '.'))) *p = '\0'; if (!hostlist_push_host (hl, buf)) err_exit ("hostlist_push_host"); } hostlist_uniq (hl); if (lopt) { if (!(itr = hostlist_iterator_create (hl))) err_exit ("hostlist_iterator_create"); while ((host = hostlist_next (itr))) printf ("%s\n", host); hostlist_iterator_destroy (itr); } else { char s[1024]; if (hostlist_ranged_string (hl, sizeof (s), s) < 0) msg_exit ("hostlist output would be too long (use -l)"); printf ("%s\n", s); } hostlist_destroy (hl); if (npc_clunk (fid) < 0) errn_exit (np_rerror (), "clunk connections"); if (npc_clunk (root) < 0) errn_exit (np_rerror (), "error clunking ctl"); if (npc_clunk (afid) < 0) errn_exit (np_rerror (), "error clunking afid"); npc_finish (fs); exit(0); }
static void * loadgen (void *arg) { thd_t *t = (thd_t *)arg; uid_t uid = geteuid (); void *ret = NULL; int n, loops = 0; if ((t->fd = diod_sock_connect (t->host, t->port, 0)) < 0) goto done; if (!(t->fs = npc_start (t->fd, t->fd, t->msize, 0))) { errn (np_rerror (), "error negotiating protocol with server"); goto done; } if (!(t->afid = npc_auth (t->fs, "ctl", uid, diod_auth)) && np_rerror () != 0) { errn (np_rerror (), "error authenticating to server"); goto done; } if (!(t->root = npc_attach (t->fs, t->afid, "ctl", uid))) { errn (np_rerror (), "error attaching to aname=ctl"); goto done; } if (t->loadtype == LOAD_IO) { if (!(t->infile = npc_open_bypath (t->root, "zero", O_RDONLY))) { errn (np_rerror (), "open zero"); goto done; } if (!(t->outfile = npc_open_bypath (t->root, "null", O_WRONLY))) { errn (np_rerror (), "open null"); goto done; } do { if ((n = npc_pread (t->infile, t->buf, t->msize, 0)) <= 0) { errn (np_rerror (), "read zero"); break; } t->readbytes += n; if ((n = npc_pwrite (t->outfile, t->buf, t->msize, 0)) < 0) { errn (np_rerror (), "write null"); break; } t->writebytes += n; t->opcount++; loops++; } while ((loops % 100 != 0) || time (NULL) < t->stoptime); } else if (t->loadtype == LOAD_GETATTR) { if (!(t->infile = npc_walk (t->root, "null"))) { errn (np_rerror (), "walk null"); goto done; } do { struct stat sb; if (npc_fstat (t->infile, &sb) < 0) { errn (np_rerror (), "walk null"); break; } t->opcount++; loops++; } while ((loops % 100 != 0) || time (NULL) < t->stoptime); } done: if (t->outfile && npc_clunk (t->outfile) < 0) errn (np_rerror (), "error clunking null"); if (t->infile && npc_clunk (t->infile) < 0) errn (np_rerror (), "error clunking zero"); if (t->root && npc_clunk (t->root) < 0) errn (np_rerror (), "error clunking ctl"); if (t->afid && npc_clunk (t->afid) < 0) errn (np_rerror (), "error clunking afid"); if (t->fs) { npc_finish (t->fs); /* closes fd */ t->fd = -1; } if (t->fd != -1) close (t->fd); return ret; }