void psshfs_notify(struct puffs_usermount *pu, int fd, int what) { struct psshfs_ctx *pctx = puffs_getspecific(pu); int nretry, which, newfd, dummy; if (fd == pctx->sshfd) { which = PSSHFD_META; } else { assert(fd == pctx->sshfd_data); which = PSSHFD_DATA; } if (puffs_getstate(pu) != PUFFS_STATE_RUNNING) return; if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) { puffs_framev_removefd(pu, fd, ECONNRESET); return; } close(fd); /* deal with zmobies, beware of half-eaten brain */ while (waitpid(-1, &dummy, WNOHANG) > 0) continue; for (nretry = 0;;nretry++) { if ((newfd = pssh_connect(pu, which)) == -1) goto retry2; if (puffs_framev_addfd(pu, newfd, PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) goto retry1; break; retry1: fprintf(stderr, "reconnect failed... "); close(newfd); retry2: if (nretry < RETRY_MAX) { fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry); sleep(nretry); } else { fprintf(stderr, "retry count exceeded, going south\n"); exit(1); /* XXXXXXX */ } } }
int main(int argc, char *argv[]) { struct psshfs_ctx pctx; struct puffs_usermount *pu; struct puffs_ops *pops; struct psshfs_node *root = &pctx.psn_root; struct puffs_node *pn_root; puffs_framev_fdnotify_fn notfn; struct vattr *rva; mntoptparse_t mp; char **sshargs; char *user; char *host; char *path; int mntflags, pflags, ch; int detach; int exportfs, refreshival, numconnections; int nargs; setprogname(argv[0]); puffs_unmountonsignal(SIGINT, true); puffs_unmountonsignal(SIGTERM, true); if (argc < 3) usage(); memset(&pctx, 0, sizeof(pctx)); mntflags = pflags = exportfs = nargs = 0; numconnections = 1; detach = 1; refreshival = DEFAULTREFRESH; notfn = puffs_framev_unmountonclose; sshargs = NULL; add_ssharg(&sshargs, &nargs, SSH_PATH); add_ssharg(&sshargs, &nargs, "-axs"); add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes"); while ((ch = getopt(argc, argv, "c:eF:g:o:O:pr:st:u:")) != -1) { switch (ch) { case 'c': numconnections = atoi(optarg); if (numconnections < 1 || numconnections > 2) { fprintf(stderr, "%s: only 1 or 2 connections " "permitted currently\n", getprogname()); usage(); /*NOTREACHED*/ } break; case 'e': exportfs = 1; break; case 'F': add_ssharg(&sshargs, &nargs, "-F"); add_ssharg(&sshargs, &nargs, optarg); break; case 'g': pctx.domanglegid = 1; pctx.manglegid = atoi(optarg); if (pctx.manglegid == (gid_t)-1) errx(1, "-1 not allowed for -g"); pctx.mygid = getegid(); break; case 'O': add_ssharg(&sshargs, &nargs, "-o"); add_ssharg(&sshargs, &nargs, optarg); break; case 'o': mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags); if (mp == NULL) err(1, "getmntopts"); freemntopts(mp); break; case 'p': notfn = psshfs_notify; break; case 'r': max_reads = atoi(optarg); break; case 's': detach = 0; break; case 't': refreshival = atoi(optarg); if (refreshival < 0 && refreshival != -1) errx(1, "invalid timeout %d", refreshival); break; case 'u': pctx.domangleuid = 1; pctx.mangleuid = atoi(optarg); if (pctx.mangleuid == (uid_t)-1) errx(1, "-1 not allowed for -u"); pctx.myuid = geteuid(); break; default: usage(); /*NOTREACHED*/ } } argc -= optind; argv += optind; if (pflags & PUFFS_FLAG_OPDUMP) detach = 0; pflags |= PUFFS_FLAG_BUILDPATH; pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND; if (argc != 2) usage(); PUFFSOP_INIT(pops); PUFFSOP_SET(pops, psshfs, fs, unmount); PUFFSOP_SETFSNOP(pops, sync); /* XXX */ PUFFSOP_SET(pops, psshfs, fs, statvfs); PUFFSOP_SET(pops, psshfs, fs, nodetofh); PUFFSOP_SET(pops, psshfs, fs, fhtonode); PUFFSOP_SET(pops, psshfs, node, lookup); PUFFSOP_SET(pops, psshfs, node, create); PUFFSOP_SET(pops, psshfs, node, open); PUFFSOP_SET(pops, psshfs, node, inactive); PUFFSOP_SET(pops, psshfs, node, readdir); PUFFSOP_SET(pops, psshfs, node, getattr); PUFFSOP_SET(pops, psshfs, node, setattr); PUFFSOP_SET(pops, psshfs, node, mkdir); PUFFSOP_SET(pops, psshfs, node, remove); PUFFSOP_SET(pops, psshfs, node, readlink); PUFFSOP_SET(pops, psshfs, node, rmdir); PUFFSOP_SET(pops, psshfs, node, symlink); PUFFSOP_SET(pops, psshfs, node, rename); PUFFSOP_SET(pops, psshfs, node, read); PUFFSOP_SET(pops, psshfs, node, write); PUFFSOP_SET(pops, psshfs, node, reclaim); pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags); if (pu == NULL) err(1, "puffs_init"); pctx.mounttime = time(NULL); pctx.refreshival = refreshival; pctx.numconnections = numconnections; user = strdup(argv[0]); if ((host = strrchr(user, '@')) == NULL) { host = user; } else { *host++ = '\0'; /* break at the '@' */ if (user[0] == '\0') { fprintf(stderr, "Missing username\n"); usage(); } add_ssharg(&sshargs, &nargs, "-l"); add_ssharg(&sshargs, &nargs, user); } if ((path = colon(host)) != NULL) { *path++ = '\0'; /* break at the ':' */ pctx.mountpath = path; } else { pctx.mountpath = "."; } host = cleanhostname(host); if (host[0] == '\0') { fprintf(stderr, "Missing hostname\n"); usage(); } add_ssharg(&sshargs, &nargs, host); add_ssharg(&sshargs, &nargs, "sftp"); pctx.sshargs = sshargs; pctx.nextino = 2; memset(root, 0, sizeof(struct psshfs_node)); TAILQ_INIT(&root->pw); pn_root = puffs_pn_new(pu, root); if (pn_root == NULL) return errno; puffs_setroot(pu, pn_root); puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn); signal(SIGHUP, takehup); puffs_ml_setloopfn(pu, psshfs_loopfn); if (pssh_connect(pu, PSSHFD_META) == -1) err(1, "can't connect meta"); if (puffs_framev_addfd(pu, pctx.sshfd, PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) err(1, "framebuf addfd meta"); if (numconnections == 2) { if (pssh_connect(pu, PSSHFD_DATA) == -1) err(1, "can't connect data"); if (puffs_framev_addfd(pu, pctx.sshfd_data, PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) err(1, "framebuf addfd data"); } else { pctx.sshfd_data = pctx.sshfd; } if (exportfs) puffs_setfhsize(pu, sizeof(struct psshfs_fid), PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3); rva = &pn_root->pn_va; rva->va_fileid = pctx.nextino++; /* * For root link count, just guess something ridiculously high. * Guessing too high has no known adverse effects, but fts(3) * doesn't like too low values. This guess will be replaced * with the real value when readdir is first called for * the root directory. */ rva->va_nlink = 8811; if (detach) if (puffs_daemon(pu, 1, 1) == -1) err(1, "puffs_daemon"); if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1) err(1, "puffs_mount"); if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) err(1, "setblockingmode"); if (puffs_mainloop(pu) == -1) err(1, "mainloop"); puffs_exit(pu, 1); return 0; }
int main(int argc, char *argv[]) { struct psshfs_ctx pctx; struct puffs_usermount *pu; struct puffs_ops *pops; struct psshfs_node *root = &pctx.psn_root; struct puffs_node *pn_root; puffs_framev_fdnotify_fn notfn; struct vattr *rva; char **sshargs; char *userhost; char *hostpath; int mntflags, pflags, ch; int detach; int exportfs, refreshival, numconnections; int nargs; setprogname(argv[0]); if (argc < 3) usage(); mntflags = pflags = exportfs = nargs = 0; numconnections = 1; detach = 1; refreshival = DEFAULTREFRESH; notfn = puffs_framev_unmountonclose; sshargs = NULL; add_ssharg(&sshargs, &nargs, SSH_PATH); add_ssharg(&sshargs, &nargs, "-axs"); add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes"); while ((ch = getopt(argc, argv, "c:eF:o:O:pr:st:")) != -1) { switch (ch) { case 'c': numconnections = atoi(optarg); if (numconnections < 1 || numconnections > 2) { fprintf(stderr, "%s: only 1 or 2 connections " "permitted currently\n", getprogname()); usage(); /*NOTREACHED*/ } break; case 'e': exportfs = 1; break; case 'F': add_ssharg(&sshargs, &nargs, "-F"); add_ssharg(&sshargs, &nargs, optarg); break; case 'O': add_ssharg(&sshargs, &nargs, "-o"); add_ssharg(&sshargs, &nargs, optarg); break; case 'o': getmntopts(optarg, puffsmopts, &mntflags, &pflags); break; case 'p': notfn = psshfs_notify; break; case 'r': max_reads = atoi(optarg); break; case 's': detach = 0; break; case 't': refreshival = atoi(optarg); if (refreshival < 0 && refreshival != -1) errx(1, "invalid timeout %d", refreshival); break; default: usage(); /*NOTREACHED*/ } } argc -= optind; argv += optind; if (pflags & PUFFS_FLAG_OPDUMP) detach = 0; pflags |= PUFFS_FLAG_BUILDPATH; pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND; if (argc != 2) usage(); PUFFSOP_INIT(pops); PUFFSOP_SET(pops, psshfs, fs, unmount); PUFFSOP_SETFSNOP(pops, sync); /* XXX */ PUFFSOP_SET(pops, psshfs, fs, statvfs); PUFFSOP_SET(pops, psshfs, fs, nodetofh); PUFFSOP_SET(pops, psshfs, fs, fhtonode); PUFFSOP_SET(pops, psshfs, node, lookup); PUFFSOP_SET(pops, psshfs, node, create); PUFFSOP_SET(pops, psshfs, node, open); PUFFSOP_SET(pops, psshfs, node, inactive); PUFFSOP_SET(pops, psshfs, node, readdir); PUFFSOP_SET(pops, psshfs, node, getattr); PUFFSOP_SET(pops, psshfs, node, setattr); PUFFSOP_SET(pops, psshfs, node, mkdir); PUFFSOP_SET(pops, psshfs, node, remove); PUFFSOP_SET(pops, psshfs, node, readlink); PUFFSOP_SET(pops, psshfs, node, rmdir); PUFFSOP_SET(pops, psshfs, node, symlink); PUFFSOP_SET(pops, psshfs, node, rename); PUFFSOP_SET(pops, psshfs, node, read); PUFFSOP_SET(pops, psshfs, node, write); PUFFSOP_SET(pops, psshfs, node, reclaim); pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags); if (pu == NULL) err(1, "puffs_init"); memset(&pctx, 0, sizeof(pctx)); pctx.mounttime = time(NULL); pctx.refreshival = refreshival; pctx.numconnections = numconnections; userhost = argv[0]; hostpath = strchr(userhost, ':'); if (hostpath) { *hostpath++ = '\0'; pctx.mountpath = hostpath; } else pctx.mountpath = "."; add_ssharg(&sshargs, &nargs, argv[0]); add_ssharg(&sshargs, &nargs, "sftp"); pctx.sshargs = sshargs; pctx.nextino = 2; memset(root, 0, sizeof(struct psshfs_node)); pn_root = puffs_pn_new(pu, root); if (pn_root == NULL) return errno; puffs_setroot(pu, pn_root); puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn); signal(SIGHUP, takehup); puffs_ml_setloopfn(pu, psshfs_loopfn); if (pssh_connect(pu, PSSHFD_META) == -1) err(1, "can't connect meta"); if (puffs_framev_addfd(pu, pctx.sshfd, PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) err(1, "framebuf addfd meta"); if (numconnections == 2) { if (pssh_connect(pu, PSSHFD_DATA) == -1) err(1, "can't connect data"); if (puffs_framev_addfd(pu, pctx.sshfd_data, PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) err(1, "framebuf addfd data"); } else { pctx.sshfd_data = pctx.sshfd; } if (exportfs) puffs_setfhsize(pu, sizeof(struct psshfs_fid), PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3); rva = &pn_root->pn_va; rva->va_fileid = pctx.nextino++; rva->va_nlink = 101; /* XXX */ if (detach) if (puffs_daemon(pu, 1, 1) == -1) err(1, "puffs_daemon"); if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1) err(1, "puffs_mount"); if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) err(1, "setblockingmode"); if (puffs_mainloop(pu) == -1) err(1, "mainloop"); puffs_exit(pu, 1); return 0; }