int main (int argc, char *argv[]) { Npcfsys *fs; Npcfid *afid, *root; uid_t uid = geteuid (); char *aname, *path; int fd = 0; /* stdin */ diod_log_init (argv[0]); if (argc != 3) usage (); aname = argv[1]; path = argv[2]; if (!(fs = npc_start (fd, fd, 65536+24, 0))) errn_exit (np_rerror (), "npc_start"); if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) errn_exit (np_rerror (), "npc_auth"); if (!(root = npc_attach (fs, afid, aname, uid))) errn_exit (np_rerror (), "npc_attach"); if (afid && npc_clunk (afid) < 0) errn (np_rerror (), "npc_clunk afid"); if (_mkdir_p (root, path, 0755) < 0) errn_exit (np_rerror (), "mkdir"); if (npc_clunk (root) < 0) errn_exit (np_rerror (), "npc_clunk root"); npc_finish (fs); exit (0); }
int main (int argc, char *argv[]) { Npcfsys *fs; Npcfid *afid, *root; char *aname; int fd = 0; /* stdin */ uid_t uid = geteuid (); diod_log_init (argv[0]); if (argc != 2) usage (); aname = argv[1]; if (!(fs = npc_start (fd, 8192+24, 0))) errn_exit (np_rerror (), "npc_start"); if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) errn_exit (np_rerror (), "npc_auth"); if (!(root = npc_attach (fs, afid, aname, uid))) errn_exit (np_rerror (), "npc_attach"); if (afid && npc_clunk (afid) < 0) errn_exit (np_rerror (), "npc_clunk afid"); _flush_series (fs, root); if (npc_clunk (root) < 0) errn_exit (np_rerror (), "npc_clunk root"); npc_finish (fs); diod_log_fini (); exit (0); }
static void _diod_mount (Opt o, int rfd, int wfd, char *spec, char *dir, int vopt, int fopt, int nopt) { char *options, *options9p, *aname, *uname; uid_t uid; int msize; Npcfsys *fs; Npcfid *afid, *root; unsigned long mountflags = 0; options = opt_csv (o); _getflags (o, &mountflags); options9p = opt_csv (o); /* after mountflags removed from opt list */ if (!(uname = opt_find (o, "uname"))) msg_exit ("uname is not set"); /* can't happen */ uid = _uname2uid (uname); aname = opt_find (o, "aname"); /* can be null */ if (!opt_scanf (o, "msize=%d", &msize) || msize < P9_IOHDRSZ) msg_exit ("msize must be set to integer >= %d", P9_IOHDRSZ); if (vopt) msg ("pre-authenticating connection to server"); if (!(fs = npc_start (rfd, wfd, msize, 0))) errn_exit (np_rerror (), "version"); if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) errn_exit (np_rerror (), "auth"); if (!(root = npc_attach (fs, afid, aname, uid))) { errn (np_rerror (), "attach"); if (afid) (void)npc_clunk (afid); exit (1); } if (afid && npc_clunk (afid) < 0) errn_exit (np_rerror (), "clunk afid"); if (npc_clunk (root) < 0) errn_exit (np_rerror (), "clunk root"); if (vopt) msg ("mount -t 9p %s %s -o%s", spec, dir, options); if (!fopt) { /* kernel wants non-blocking */ if (fcntl (rfd, F_SETFL, O_NONBLOCK) < 0) err_exit ("setting O_NONBLOCK flag on rfd=%d", rfd); if (fcntl (wfd, F_SETFL, O_NONBLOCK) < 0) err_exit ("setting O_NONBLOCK flag on wfd=%d", wfd); _mount (spec, dir, mountflags, options9p); } npc_finish (fs); /* closes fd */ if (!nopt) { if (!_update_mtab (options, spec, dir)) msg_exit ("failed to update /etc/mtab"); } free (options); free (options9p); }
static void signal_ready (void) { int rc; if ((rc = pthread_mutex_lock (&start_lock))) errn_exit (rc, "pthread_mutex_lock"); start_count++; if ((rc = pthread_mutex_unlock (&start_lock))) errn_exit (rc, "pthread_mutex_unlock"); if ((rc = pthread_cond_signal (&start_cond))) errn_exit (rc, "pthread_cond_signal"); }
static void wait_ready (void) { int rc; if ((rc = pthread_mutex_lock (&start_lock))) errn_exit (rc, "pthread_mutex_lock"); while (start_count < nthreads) { if ((rc = pthread_cond_wait (&start_cond, &start_lock))) errn_exit (rc, "pthread_cond_wait"); } if ((rc = pthread_mutex_unlock (&start_lock))) errn_exit (rc, "pthread_mutex_unlock"); }
/* Switch to user/group, load the user's supplementary groups. * Print message and exit on failure. */ static void _become_user (char *name, uid_t uid, int realtoo) { int err; struct passwd pw, *pwd; int len = sysconf(_SC_GETPW_R_SIZE_MAX); char *buf; int nsg; gid_t sg[64]; char *endptr; if (len == -1) len = 4096; if (!(buf = malloc(len))) msg_exit ("out of memory"); if (name) { errno = 0; uid = strtoul (name, &endptr, 10); if (errno == 0 && *name != '\0' && *endptr == '\0') name = NULL; } if (name) { if ((err = getpwnam_r (name, &pw, buf, len, &pwd)) != 0) errn_exit (err, "error looking up user %s", name); if (!pwd) msg_exit ("error looking up user %s", name); } else { if ((err = getpwuid_r (uid, &pw, buf, len, &pwd)) != 0) errn_exit (err, "error looking up uid %d", uid); if (!pwd) msg_exit ("error looking up uid %d", uid); } nsg = sizeof (sg) / sizeof(sg[0]); if (getgrouplist(pwd->pw_name, pwd->pw_gid, sg, &nsg) == -1) err_exit ("user is in too many groups"); if (syscall(SYS_setgroups, nsg, sg) < 0) err_exit ("setgroups"); if (setregid (realtoo ? pwd->pw_gid : -1, pwd->pw_gid) < 0) err_exit ("setreuid"); if (setreuid (realtoo ? pwd->pw_uid : -1, pwd->pw_uid) < 0) err_exit ("setreuid"); free (buf); }
static void *module_thread (void *arg) { module_t *p = arg; assert (p->magic == MODULE_MAGIC); sigset_t signal_set; int errnum; char *uri = xasprintf ("shmem://%s", zuuid_str (p->uuid)); char **av = NULL; char *rankstr = NULL; int ac; assert (p->zctx); /* Connect to broker socket, enable logging, register built-in services */ if (!(p->h = flux_open (uri, 0))) err_exit ("flux_open %s", uri); if (flux_opt_set (p->h, FLUX_OPT_ZEROMQ_CONTEXT, p->zctx, sizeof (p->zctx)) < 0) err_exit ("flux_opt_set ZEROMQ_CONTEXT"); rankstr = xasprintf ("%u", p->rank); if (flux_attr_fake (p->h, "rank", rankstr, FLUX_ATTRFLAG_IMMUTABLE) < 0) { err ("%s: error faking rank attribute", p->name); goto done; } flux_log_set_facility (p->h, p->name); modservice_register (p->h, p); /* Block all signals */ if (sigfillset (&signal_set) < 0) err_exit ("%s: sigfillset", p->name); if ((errnum = pthread_sigmask (SIG_BLOCK, &signal_set, NULL)) != 0) errn_exit (errnum, "pthread_sigmask"); /* Run the module's main(). */ ac = argz_count (p->argz, p->argz_len); av = xzmalloc (sizeof (av[0]) * (ac + 1)); argz_extract (p->argz, p->argz_len, av); if (p->main(p->h, ac, av) < 0) { err ("%s: mod_main returned error", p->name); goto done; } done: if (rankstr) free (rankstr); if (av) free (av); flux_close (p->h); p->h = NULL; return NULL; }
int main (int argc, char *argv[]) { Npcfsys *fs; Npcfid *afid, *root, *fid; char *aname; const int fd = 0; /* stdin */ uid_t uid; gid_t gid, ngid; diod_log_init (argv[0]); if (argc != 2) usage (); aname = argv[1]; if (geteuid () != 0) /* the server actually must be running as root */ msg_exit ("must run as root"); pick_user (&uid, &gid, &ngid); if (!(fs = npc_start (fd, 8192+24, 0))) errn_exit (np_rerror (), "npc_start"); if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) errn_exit (np_rerror (), "npc_auth"); if (!(root = npc_attach (fs, afid, aname, uid))) errn_exit (np_rerror (), "npc_attach"); if (afid && npc_clunk (afid) < 0) errn_exit (np_rerror (), "npc_clunk afid"); /* should succeed */ if (!(fid = npc_create_bypath (root, "foo", 0644, O_RDONLY, gid))) errn_exit (np_rerror (), "npc_create_bypath as %d:%d with gid %d", uid, gid, gid); if (npc_clunk (fid) < 0) errn_exit (np_rerror (), "npc_clunk"); msg ("create foo with good gid succeeded"); /* should fail */ if ((fid = npc_create_bypath (root, "foo2", 0644, O_RDONLY, ngid))) msg_exit ("npc_create_bypath as %d:%d with gid %d succeeded: FAIL", uid, gid, ngid); msg ("create foo2 with bad gid failed"); if (npc_clunk (root) < 0) errn_exit (np_rerror (), "npc_clunk root"); npc_finish (fs); diod_log_fini (); exit (0); }
int main (int argc, char *argv[]) { flux_t h; int ch; char *message = NULL; size_t len = 0; char *priority = "user.notice"; int level; char *facility; int e; log_init ("flux-logger"); while ((ch = getopt_long (argc, argv, OPTIONS, longopts, NULL)) != -1) { switch (ch) { case 'h': /* --help */ usage (); break; case 'p': /* --priority facility.level */ priority = optarg; break; default: usage (); break; } } if (optind == argc) usage (); if ((e = argz_create (argv + optind, &message, &len)) != 0) errn_exit (e, "argz_create"); argz_stringify (message, len, ' '); if (!(h = flux_open (NULL, 0))) err_exit ("flux_open"); if (parse_logstr (priority, &level, &facility) < 0) msg_exit ("bad priority argument"); flux_log_set_facility (h, facility); if (flux_log (h, level, "%s", message) < 0) err_exit ("flux_log"); flux_close (h); free (message); free (facility); log_fini (); return 0; }
int main (int argc, char *argv[]) { Npcfsys *fs; Npcfid *afid, *root; uid_t uid = geteuid (); char *aname, *path, *attr, *value; int fd = 0; /* stdin */ diod_log_init (argv[0]); if (argc < 3) usage (); aname = argv[1]; path = argv[2]; attr = argv[3]; value = argv[4]; if (!(fs = npc_start (fd, fd, 65536+24, 0))) errn_exit (np_rerror (), "npc_start"); if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) errn_exit (np_rerror (), "npc_auth"); if (!(root = npc_attach (fs, afid, aname, uid))) errn_exit (np_rerror (), "npc_attach"); if (afid && npc_clunk (afid) < 0) errn (np_rerror (), "npc_clunk afid"); if (npc_setxattr (root, path, attr, NULL, 0, 0) < 0) errn_exit (np_rerror (), "npc_setxattr"); if (npc_clunk (root) < 0) errn_exit (np_rerror (), "npc_clunk root"); npc_finish (fs); exit (0); }
int main (int argc, char *argv[]) { Npsrv *srv; Npconn *conn; Nptrans *trans; int flags = SRV_FLAGS_DEBUG_9PTRACE | SRV_FLAGS_DEBUG_USER; diod_log_init (argv[0]); diod_conf_init (); if (!(srv = np_srv_create (16, flags))) errn_exit (np_rerror (), "out of memory"); srv->logmsg = diod_log_msg; srv->attach = myattach; srv->clunk = myclunk; /* create one connection */ if (!(trans = ttrans_create ())) err_exit ("ttrans_create"); if (!(conn = np_conn_create (srv, trans, "loopback"))) msg_exit ("np_conn_create failure"); _send_tversion (trans); _send_tauth (trans); _send_tattach (trans, 0, 0); _send_tattach (trans, 0, 1); _send_tattach (trans, 1, 2); _send_tattach (trans, 1, 3); _send_tattach (trans, 0, 4); _send_tclunk (trans, 4); _send_tclunk (trans, 3); _send_tclunk (trans, 2); _send_tclunk (trans, 1); _send_tclunk (trans, 0); ttrans_rpc (trans, NULL, NULL); /* signifies EOF to reader */ /* wait for exactly one connect/disconnect */ np_srv_wait_conncount (srv, 1); sleep(1); /* racy here - conn reader needs time to process EOF */ np_srv_destroy (srv); diod_conf_fini (); diod_log_fini (); exit (0); }
void diod_rdma_accept_one (Npsrv *srv, diod_rdma_t rdma) { Npconn *conn; Nptrans *trans; struct rdma_cm_event *event; struct rdma_cm_id *cmid; enum rdma_cm_event_type etype; int n; n = rdma_get_cm_event(rdma->event_channel, &event); if (n) errn_exit (n, "rdma_get_cm_event"); cmid = (struct rdma_cm_id *)event->id; etype = event->event; rdma_ack_cm_event(event); switch (etype) { case RDMA_CM_EVENT_CONNECT_REQUEST: msg ("rdma: connection request"); trans = np_rdmatrans_create(cmid, rdma_qdepth, rdma_maxmsize); if (trans) { conn = np_conn_create(srv, trans, "rdma", 0); cmid->context = conn; np_srv_add_conn(srv, conn); } else errn (np_rerror (), "np_rdmatrns_create failed"); break; case RDMA_CM_EVENT_ESTABLISHED: msg ("rdma: connection established"); break; case RDMA_CM_EVENT_DISCONNECTED: msg ("rdma: connection shutting down"); conn = cmid->context; //np_conn_shutdown(conn); /* FIXME: clean up properly */ break; default: msg ("rdma: event %d received waiting for a connect request\n", etype); } }
int diod_rdma_listen (diod_rdma_t rdma) { int n; rdma->addr.sin_family = AF_INET; rdma->addr.sin_port = htons(rdma_port); rdma->addr.sin_addr.s_addr = htonl(INADDR_ANY); n = rdma_bind_addr(rdma->listen_id, (struct sockaddr *)&rdma->addr); if (n) errn_exit (n, "rdma_bind_addr"); n = rdma_listen(rdma->listen_id, 1); if (n) errn (n, "rdma_listen"); return 0; }
static void module_destroy (module_t *p) { assert (p->magic == MODULE_MAGIC); int errnum; if (p->t) { errnum = pthread_join (p->t, NULL); if (errnum) errn_exit (errnum, "pthread_join"); } assert (p->h == NULL); flux_watcher_stop (p->broker_w); flux_watcher_destroy (p->broker_w); zsocket_destroy (p->zctx, p->sock); dlclose (p->dso); zuuid_destroy (&p->uuid); free (p->digest); if (p->argz) free (p->argz); if (p->name) free (p->name); if (p->rmmod_cb) p->rmmod_cb (p, p->rmmod_arg); if (p->rmmod) { flux_msg_t *msg; while ((msg = zlist_pop (p->rmmod))) flux_msg_destroy (msg); } if (p->subs) { char *s; while ((s = zlist_pop (p->subs))) free (s); zlist_destroy (&p->subs); } zlist_destroy (&p->rmmod); p->magic = ~MODULE_MAGIC; free (p); }
diod_rdma_t diod_rdma_create (void) { int n; diod_rdma_t rdma; rdma = malloc (sizeof (*rdma)); if (!rdma) msg_exit ("out of memory"); rdma->event_channel = rdma_create_event_channel(); if (!rdma->event_channel) msg_exit ("rdma_create_event_channel failed"); n = rdma_create_id(rdma->event_channel, &rdma->listen_id, NULL, RDMA_PS_TCP); if (n) errn_exit (n, "rdma_create_id"); return rdma; }
int main (int argc, char *argv[]) { char *aname = "ctl"; char *server = NULL; int msize = 65536; uid_t uid = geteuid (); int topt = 0; int Sopt = 0; int flags = 0; int fd, c; Npcfsys *fs = NULL; Npcfid *afid = NULL, *root = NULL; char *buf = NULL; struct timeval tv; struct timezone tz; 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 'S': /* --set-time */ Sopt = 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, flags)) < 0) exit (1); if (!(fs = npc_start (fd, fd, msize, 0))) { errn (np_rerror (), "error negotiating protocol with server"); goto done; } if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) { errn (np_rerror (), "error authenticating to server"); goto done; } if (!(root = npc_attach (fs, afid, aname, uid))) { errn (np_rerror (), "error attaching to aname='%s'", aname); goto done; } buf = npc_aget (root, "date"); if (!buf) { errn (np_rerror (), "error reading date"); goto done; } if (sscanf (buf, "%lu.%lu %d.%d", &tv.tv_sec, &tv.tv_usec, &tz.tz_minuteswest, &tz.tz_dsttime) != 4) { msg ("error scanning returned date: %s", buf); goto done; } if (Sopt) { if (settimeofday (&tv, &tz) < 0) err_exit ("settimeofday"); } else { time_t t = tv.tv_sec; printf ("%s", ctime (&t)); } done: if (buf) free (buf); if (root && npc_clunk (root) < 0) errn_exit (np_rerror (), "error clunking %s", aname); if (afid && npc_clunk (afid) < 0) errn_exit (np_rerror (), "error clunking afid"); if (fs) npc_finish (fs); close (fd); diod_log_fini (); exit (0); }
int main (int argc, char *argv[]) { char *host = NULL; char *port = "564"; int msize = 65536; int numthreads = 16; int runtime = 10; int i, c, err; time_t now = time (NULL); thd_t *t; uint64_t readbytes = 0, writebytes = 0, opcount = 0; load_t loadtype = LOAD_IO; diod_log_init (argv[0]); opterr = 0; while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) { switch (c) { case 'h': /* --hostname NAME */ host = optarg; break; case 'p': /* --port PORT */ port = optarg; break; case 'm': /* --msize SIZE */ msize = strtoul (optarg, NULL, 10); break; case 'n': /* --numthreads INT */ numthreads = strtoul (optarg, NULL, 10); break; case 'r': /* --runtime INT */ runtime = strtoul (optarg, NULL, 10); break; case 'g': /* --getattr */ loadtype = LOAD_GETATTR; break; default: usage (); } } if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) err_exit ("signal"); if (!(t = malloc (sizeof (thd_t) * numthreads))) msg_exit ("out of memory"); for (i = 0; i < numthreads; i++) { t[i].host = host ? host : "localhost"; t[i].port = port; t[i].msize = msize; t[i].stoptime = now + runtime; t[i].fd = -1; t[i].fs = NULL; t[i].root = t[i].afid = t[i].infile = t[i].outfile = NULL; t[i].readbytes = t[i].writebytes = 0; t[i].loadtype = loadtype; if (!(t[i].buf = malloc (msize))) msg_exit ("out of memory"); if ((err = pthread_create (&t[i].t, NULL, loadgen, &t[i]))) errn_exit (err, "pthread_create"); } for (i = 0; i < numthreads; i++) { if ((err = pthread_join (t[i].t, NULL))) errn_exit (err, "pthread_join"); readbytes += t[i].readbytes; writebytes += t[i].writebytes; opcount += t[i].opcount; free (t[i].buf); } free (t); msg ("%"PRIu64" ops/s, %"PRIu64" rMB/s, %"PRIu64" wMB/s", opcount/runtime, readbytes/(1024*1024*runtime), writebytes/(1024*1024*runtime)); diod_log_fini (); exit (0); }
static void _service_run (srvmode_t mode, int rfdno, int wfdno) { List l = diod_conf_get_listen (); int nwthreads = diod_conf_get_nwthreads (); int flags = diod_conf_get_debuglevel (); uid_t euid = geteuid (); int n; ss.mode = mode; ss.rfdno = rfdno; ss.wfdno = wfdno; ss.shutdown = 0; ss.reload = 0; _service_sigsetup (); ss.fds = NULL; ss.nfds = 0; switch (mode) { case SRV_FILEDES: break; case SRV_NORMAL: case SRV_SOCKTEST: if (!diod_sock_listen (l, &ss.fds, &ss.nfds)) msg_exit ("failed to set up listener"); #if WITH_RDMATRANS ss.rdma = diod_rdma_create (); diod_rdma_listen (ss.rdma); #endif break; } /* manipulate squash/runas users if not root */ if (euid != 0) { if (diod_conf_get_allsquash ()) { struct passwd *pw = getpwuid (euid); char *su = diod_conf_get_squashuser (); if (!pw) msg_exit ("getpwuid on effective uid failed"); if (strcmp (pw->pw_name, su) != 0) { if (strcmp (su, DFLT_SQUASHUSER) != 0) msg ("changing squashuser '%s' to '%s' " "since you are not root", su, pw->pw_name); diod_conf_set_squashuser (pw->pw_name); /* fixes issue 41 */ } } else { /* N.B. runasuser cannot be set in the config file */ uid_t ruid = diod_conf_get_runasuid (); if (diod_conf_opt_runasuid () && ruid != euid) msg ("changing runasuid %d to %d " "since you are not root", ruid, euid); diod_conf_set_runasuid (euid); } } if (!diod_conf_get_foreground () && mode != SRV_FILEDES) _daemonize (); /* implicit fork - no pthreads before this */ if (!diod_conf_get_foreground () && mode != SRV_FILEDES) diod_log_set_dest (diod_conf_get_logdest ()); /* drop root */ if (euid == 0) { if (diod_conf_get_allsquash ()) _become_user (diod_conf_get_squashuser (), -1, 1); else if (diod_conf_opt_runasuid ()) _become_user (NULL, diod_conf_get_runasuid (), 1); } /* clear umask */ umask (0); flags |= SRV_FLAGS_LOOSEFID; /* work around buggy clients */ flags |= SRV_FLAGS_AUTHCONN; //flags |= SRV_FLAGS_FLUSHSIG; /* XXX temporarily off */ if (geteuid () == 0) { flags |= SRV_FLAGS_SETFSID; flags |= SRV_FLAGS_DAC_BYPASS; if (_test_setgroups ()) flags |= SRV_FLAGS_SETGROUPS; else msg ("test_setgroups: groups are per-process (disabling)"); } /* Process dumpable flag may have been cleared by uid manipulation above. * Set it here, then maintain it in user.c::np_setfsid () as uids are * further manipulated. */ if (prctl (PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) err_exit ("prctl PR_SET_DUMPABLE failed"); if (!diod_conf_get_userdb ()) flags |= SRV_FLAGS_NOUSERDB; if (!(ss.srv = np_srv_create (nwthreads, flags))) /* starts threads */ errn_exit (np_rerror (), "np_srv_create"); if (diod_init (ss.srv) < 0) errn_exit (np_rerror (), "diod_init"); if ((n = pthread_create (&ss.t, NULL, _service_loop, NULL))) errn_exit (n, "pthread_create _service_loop"); #if WITH_RDMATRANS if ((n = pthread_create (&ss.rdma_t, NULL, _service_loop_rdma, NULL))) errn_exit (n, "pthread_create _service_loop_rdma"); #endif switch (mode) { case SRV_FILEDES: case SRV_SOCKTEST: np_srv_wait_conncount (ss.srv, 1); pthread_kill (ss.t, SIGUSR1); break; case SRV_NORMAL: break; } if ((n = pthread_join (ss.t, NULL))) errn_exit (n, "pthread_join _service_loop"); #if WITH_RDMATRANS if ((n = pthread_join (ss.rdma_t, NULL))) errn_exit (n, "pthread_join _service_loop_rdma"); #endif diod_fini (ss.srv); np_srv_destroy (ss.srv); }
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 _flush_series (Npcfsys *fs, Npcfid *root) { Npfcall *rc = NULL, *tc = NULL, *ac = NULL; u16 tag, flushtag; int n, i; struct sigaction sa; assert (fs->trans != NULL); sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sa.sa_handler = _alarm_clock; if (sigaction (SIGALRM, &sa, NULL) < 0) err_exit ("sigaction"); /* write 100 tversions */ for (i = 0; i < 100; i++) { if (!(tc = np_create_tversion (fs->msize, "9P2000.L"))) msg_exit ("out of memory"); flushtag = tag = npc_get_id(fs->tagpool); np_set_tag(tc, tag); n = np_trans_write(fs->trans, tc->pkt, tc->size); if (n < 0) errn_exit (np_rerror (), "np_trans_write"); if (n != tc->size) msg_exit ("np_trans_write came up unexpectedly short"); //msg ("sent tversion tag %d", tc->tag); free(tc); } msg ("sent 100 tversions"); /* flush the most recent */ if (!(ac = np_create_tflush (flushtag))) msg_exit ("out of memory"); tag = npc_get_id(fs->tagpool); np_set_tag(ac, tag); n = np_trans_write(fs->trans, ac->pkt, ac->size); if (n < 0) errn_exit (np_rerror (), "np_trans_write"); if (n != ac->size) msg_exit ("np_trans_write came up unexpectedly short"); //msg ("sent tflush tag %d (flushing tag %d)", ac->tag, flushtag); free (ac); msg ("sent 1 tflush"); /* receive up to 101 responses with 1s timeout */ for (i = 0; i < 101; i++) { if (!(rc = malloc(sizeof(*rc) + fs->msize))) msg_exit ("out of memory"); rc->pkt = (u8*)rc + sizeof(*rc); alarm (1); n = np_trans_read(fs->trans, rc->pkt, fs->msize); if (n < 0) { if (errno == EINTR) break; errn_exit (np_rerror (), "np_trans_read"); } alarm (0); if (n == 0) msg_exit ("np_trans_read: unexpected EOF"); if (!np_deserialize (rc, rc->pkt)) msg_exit ("failed to deserialize response in one go"); //msg ("received tag %d", rc->tag); free(rc); //npc_put_id(fs->tagpool, rc->tag); } if (i == 100 || i == 101) msg ("received 100/101 respones"); else msg ("received %d responses", i); }