Npuser * np_attach2user (Npsrv *srv, Npstr *uname, u32 n_uname) { Npuser *u = NULL; char *s; if (n_uname != P9_NONUNAME) { u = np_uid2user (srv, n_uname); } else { if (uname->len == 0) { np_uerror (EIO); goto done; } s = np_strdup (uname); if (!s) { np_uerror (ENOMEM); goto done; } u = np_uname2user (srv, s); free (s); } done: return u; }
int main(int argc, char **argv) { int sfd, i, n, off; int c, port; char *addr, *s; char *uname, *path; Npuser *user; Npcfsys *fs; Npcfid *fid; struct sockaddr_in saddr; struct hostent *hostinfo; char buf[512]; port = 564; // npc_chatty = 1; user = np_uid2user(geteuid()); if (!user) { fprintf(stderr, "cannot retrieve user %d\n", geteuid()); exit(1); } uname = user->uname; while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': npc_chatty = 1; break; case 'p': port = strtol(optarg, &s, 10); if (*s != '\0') usage(); break; case 'u': uname = optarg; break; default: usage(); } } if (argc - optind < 2) usage(); addr = argv[optind]; path = argv[optind+1]; sfd = socket(PF_INET, SOCK_STREAM, 0); if (sfd < 0) { perror("socket"); exit(1); } hostinfo = gethostbyname (addr); if (!hostinfo) { perror("gethostbyname"); exit(1); } saddr.sin_family = AF_INET; saddr.sin_port = htons(port); saddr.sin_addr = *(struct in_addr *) hostinfo->h_addr; if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { perror("connect"); exit(1); } fs = npc_mount(sfd, NULL, "lucho"); fid = npc_open(fs, path, Oread); if (!fid) { fprintf(stderr, "cannot open\n"); exit(1); } off = 0; while ((n = npc_read(fid, (u8*) buf, sizeof(buf), off)) > 0) { i = write(1, buf, n); if (i != n) { fprintf(stderr, "error writing\n"); exit(1); } off += n; } npc_close(fid); npc_umount(fs); exit(0); }
int main(int argc, char **argv) { int c, debuglevel, nwthreads; pid_t pid; Npuser *user; char *opts, *s; debuglevel = 0; nwthreads = 16; user = np_uid2user(getuid()); while ((c = getopt(argc, argv, "dw:")) != -1) { switch (c) { case 'd': debuglevel = 1; break; case 'w': nwthreads = strtol(optarg, &s, 10); if (*s != '\0') usage(); break; default: fprintf(stderr, "invalid option\n"); } } if (optind >= argc) usage(); if (!user) { fprintf(stderr, "invalid user\n"); return -1; } close(0); close(1); close(2); pid = fork(); if (pid < 0) return -1; else if (pid != 0) return 0; setsid(); chdir("/"); root = npfile_alloc(NULL, strdup(""), 0755|Dmdir, 0, &rootops, NULL); root->parent = root; npfile_incref(root); root->atime = time(NULL); root->mtime = root->atime; root->uid = user; root->gid = user->dfltgroup; root->muid = user; null = npfile_alloc(root, strdup("null"), 0644, 1, &nullops, NULL); npfile_incref(null); root->dirfirst = null; root->dirlast = null; srv = np_pipesrv_create(nwthreads); if (!srv) return -1; srv->debuglevel = debuglevel; srv->connclose = null_connclose; npfile_init_srv(srv, root); np_pipesrv_mount(srv, argv[optind], user->uname, 0, opts); while (1) { sleep(100); } return 0; }