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); }
ssize_t npc_listxattr (Npcfid *root, char *path, char *buf, size_t size) { Npcfid *fid = NULL; Npcfid *attrfid = NULL; ssize_t ret = -1; size_t n, tot; if (!(fid = npc_walk (root, path))) goto done; if (!(attrfid = npc_fid_alloc(fid->fsys))) goto done; ret = npc_xattrwalk (fid, attrfid, NULL); if (ret < 0) goto done; if (buf == NULL || size == 0) goto done; if (ret > size) { np_uerror (ERANGE); goto done; } for (tot = 0; tot < size; ) { n = npc_read (attrfid, buf + tot, size - tot); if (n <= 0) break; tot += n; } ret = tot; done: if (fid) (void)npc_clunk (fid); if (attrfid) (void)npc_clunk (attrfid); return ret; }
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); }
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); }
Npcfid* npc_open(Npcfsys *fs, char *path, int mode) { Npfcall *tc, *rc; Npcfid *fid; fid = npc_walk(fs, path); if (!fid) return NULL; tc = np_create_topen(fid->fid, mode); if (npc_rpc(fs, tc, &rc) < 0) { npc_clunk(fid); free(tc); return NULL; } fid->iounit = rc->iounit; if (!fid->iounit || fid->iounit>fid->fsys->msize-IOHDRSZ) fid->iounit = fid->fsys->msize-IOHDRSZ; free(tc); free(rc); return fid; }
Npcfid* npc_auth (Npcfsys *fs, char *aname, u32 uid, AuthFun auth) { Npcfid *afid = NULL; Npfcall *tc = NULL, *rc = NULL; if (!(afid = npc_fid_alloc (fs))) goto done; if (!(tc = np_create_tauth (afid->fid, NULL, aname, uid))) { np_uerror (ENOMEM); npc_fid_free (afid); afid = NULL; goto done; } if (afid->fsys->rpc (afid->fsys, tc, &rc) < 0) { npc_fid_free (afid); afid = NULL; goto done; } if (auth && auth (afid, uid) < 0) { int saved_err = np_rerror (); (void)npc_clunk (afid); afid = NULL; np_uerror (saved_err); goto done; } done: if (tc) free(tc); if (rc) free(rc); return afid; }
int npc_chown (Npcfid *root, char *path, uid_t owner, gid_t group) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_fchown (fid, owner, group) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
int npc_chmod (Npcfid *root, char *path, mode_t mode) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_fchmod (fid, mode) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
void npc_umount (Npcfid *fid) { Npcfsys *fs = fid->fsys; (void)npc_clunk (fid); npc_finish (fs); }
int npc_truncate (Npcfid *root, char *path, off_t length) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_ftruncate (fid, length) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
int npc_utime(Npcfid *root, char *path, const struct utimbuf *times) { Npcfid *fid; if (!(fid = npc_walk (root, path))) return -1; if (npc_futime (fid, times) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); np_uerror (saved_err); return -1; } if (npc_clunk (fid) < 0) return -1; return 0; }
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_closedir (Npcfid *fid) { if (fid->dbuf) { free (fid->dbuf); fid->dbuf = NULL; } return npc_clunk (fid); }
static int lsdir (int i, int count, Npcfid *root, int lopt, char *path) { Npcfid *dir = NULL; struct dirent d, *dp; if (!(dir = npc_opendir (root, path))) { if (np_rerror () == ENOTDIR) { if (lopt) lsfile_l (root, path); else printf ("%s\n", path); return 0; } errn (np_rerror (), "%s", path); goto error; } if (count > 1) printf ("%s:\n", path); do { if ((npc_readdir_r (dir, &d, &dp)) > 0) { errn (np_rerror (), "npc_readdir: %s", path); goto error; } if (dp) { if (lopt) lsfile_l (dir, dp->d_name); else printf ("%s\n", dp->d_name); } } while (dp != NULL); if (npc_clunk (dir) < 0) { errn (np_rerror (), "npc_clunk: %s", path); goto error; } if (count > 1 && i < count - 1) printf ("\n"); return 0; error: if (dir) (void)npc_clunk (dir); return -1; }
Npcfid* npc_create(Npcfsys *fs, char *path, u32 perm, int mode) { char *fname, *pname; Npfcall *tc, *rc; Npcfid *fid; pname = strdup(path); if (!pname) return NULL; fname = strrchr(pname, '/'); if (fname) { *fname = '\0'; fname++; } else { fname = pname; pname = NULL; } // fprintf(stderr, "path %s dir %s fname %s\n", path, pname, fname); tc = NULL; fid = npc_walk(fs, pname); if (!fid) goto error; tc = np_create_tcreate(fid->fid, fname, perm, mode); if (npc_rpc(fs, tc, &rc) < 0) goto error; fid->iounit = rc->iounit; if (!fid->iounit || fid->iounit>fid->fsys->msize-IOHDRSZ) fid->iounit = fid->fsys->msize-IOHDRSZ; free(tc); free(rc); if (pname) free(pname); else free(fname); return fid; error: npc_clunk(fid); free(tc); if (pname) free(pname); else free(fname); return NULL; }
static int lsfiles (int fd, uid_t uid, int msize, char *aname, int lopt, char **av, int ac) { Npcfsys *fs = NULL; Npcfid *afid = NULL, *root = NULL; int i, ret = -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 ? aname : ""); goto done; } if (ac == 0) { if (lsdir(1, 1, root, lopt, NULL) < 0) goto done; } else { for (i = 0; i < ac; i++) { if (lsdir (i, ac, root, lopt, av[i]) < 0) goto done; } } ret = 0; done: if (root && npc_clunk (root) < 0) { errn (np_rerror (), "error clunking %s", aname); ret = -1; } if (afid && npc_clunk (afid) < 0) { errn (np_rerror (), "error clunking afid"); ret = -1; } if (fs) npc_finish (fs); return ret; }
static void lsfile_l (Npcfid *dir, char *name) { Npcfid *fid = NULL; struct stat sb; struct passwd *pw; struct group *gr; char uid[16], gid[16]; char *mtime; if (!(fid = npc_walk (dir, name))) { errn (np_rerror (), "npc_walk %s\n", name); goto error; } if (npc_fstat (fid, &sb) < 0) { errn (np_rerror (), "npc_fstat %s\n", name); goto error; } if (npc_clunk (fid) < 0) { errn (np_rerror (), "npc_clunk %s\n", name); goto error; } if (!(pw = getpwuid (sb.st_uid))) snprintf (uid, sizeof (uid), "%d", sb.st_uid); if (!(gr = getgrgid (sb.st_gid))) snprintf (gid, sizeof (gid), "%d", sb.st_gid); mtime = ctime( &sb.st_mtime); printf ("%10s %4lu %s %s %12lu %.*s %s\n", mode2str (sb.st_mode), sb.st_nlink, pw ? pw->pw_name : uid, gr ? gr->gr_name : gid, sb.st_size, (int)strlen (mtime) - 13, mtime + 4, name); return; error: if (fid) (void)npc_clunk (fid); }
int npc_mkdir_bypath (Npcfid *root, char *path, u32 mode) { Npcfid *fid; char *cpy, *dname, *fname; /* walk to the parent */ if (!(cpy = strdup (path))) { np_uerror (ENOMEM); return -1; } dname = dirname (cpy); fid = npc_walk (root, dname); free (cpy); if (!fid) return -1; /* create the child */ if (!(cpy = strdup (path))) { (void)npc_clunk (fid); np_uerror (ENOMEM); return -1; } fname = basename (cpy); if (npc_mkdir (fid, fname, mode) < 0) { int saved_err = np_rerror (); (void)npc_clunk (fid); free (cpy); np_uerror (saved_err); return -1; } free (cpy); if (npc_clunk (fid) < 0) return -1; return 0; }
Npcfid * npc_mount (int rfd, int wfd, int msize, char *aname, AuthFun auth) { Npcfsys *fs; Npcfid *afid, *fid; if (!(fs = npc_start (rfd, wfd, msize, NPC_SHORTREAD_EOF))) return NULL; if (!(afid = npc_auth (fs, aname, geteuid (), auth)) && np_rerror ()) { npc_finish (fs); return NULL; } if (!(fid = npc_attach (fs, afid, aname, geteuid ()))) { int saved_err = np_rerror (); if (afid) (void)npc_clunk (afid); npc_finish (fs); np_uerror (saved_err); return NULL; } if (afid) (void)npc_clunk (afid); return fid; };
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 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_setxattr (Npcfid *root, char *path, char *name, char *val, size_t size, int flags) { Npcfid *fid = NULL; int n, tot = 0; int ret = -1; if (!(fid = npc_walk (root, path))) goto done; if (npc_xattrcreate (fid, name, size, flags) < 0) goto done; for (tot = 0; tot < size; tot += n) { n = npc_write(fid, val + tot, size - tot); if (n < 0) goto done; } ret = 0; done: if (fid) ret = npc_clunk(fid); return ret; }
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; }
int npc_close(Npcfid *fid) { npc_cancel_fid_requests(fid); return npc_clunk(fid); }
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); }