1 /* $NetBSD: psshfs.c,v 1.49 2008/09/06 12:29:57 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Antti Kantee. All Rights Reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * psshfs: puffs sshfs 30 * 31 * psshfs implements sshfs functionality on top of puffs making it 32 * possible to mount a filesystme through the sftp service. 33 * 34 * psshfs can execute multiple operations in "parallel" by using the 35 * puffs_cc framework for continuations. 36 * 37 * Concurrency control is handled currently by vnode locking (this 38 * will change in the future). Context switch locations are easy to 39 * find by grepping for puffs_framebuf_enqueue_cc(). 40 */ 41 42 #include <sys/cdefs.h> 43 #ifndef lint 44 __RCSID("$NetBSD: psshfs.c,v 1.49 2008/09/06 12:29:57 pooka Exp $"); 45 #endif /* !lint */ 46 47 #include <sys/types.h> 48 49 #include <assert.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <mntopts.h> 53 #include <paths.h> 54 #include <poll.h> 55 #include <puffs.h> 56 #include <signal.h> 57 #include <stdlib.h> 58 #include <util.h> 59 #include <unistd.h> 60 61 #include "psshfs.h" 62 63 static int pssh_connect(struct psshfs_ctx *); 64 static void psshfs_loopfn(struct puffs_usermount *); 65 static void usage(void); 66 static void add_ssharg(char ***, int *, char *); 67 static void psshfs_notify(struct puffs_usermount *, int, int); 68 69 #define SSH_PATH "/usr/bin/ssh" 70 71 unsigned int max_reads; 72 static int sighup; 73 74 static void 75 add_ssharg(char ***sshargs, int *nargs, char *arg) 76 { 77 78 *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*)); 79 if (!*sshargs) 80 err(1, "realloc"); 81 (*sshargs)[(*nargs)++] = arg; 82 (*sshargs)[*nargs] = NULL; 83 } 84 85 static void 86 usage() 87 { 88 89 fprintf(stderr, "usage: %s " 90 "[-es] [-F configfile] [-O sshopt=value] [-o opts] " 91 "user@host:path mountpath\n", 92 getprogname()); 93 exit(1); 94 } 95 96 static void 97 takehup(int sig) 98 { 99 100 sighup = 1; 101 } 102 103 int 104 main(int argc, char *argv[]) 105 { 106 struct psshfs_ctx pctx; 107 struct puffs_usermount *pu; 108 struct puffs_ops *pops; 109 struct psshfs_node *root = &pctx.psn_root; 110 struct puffs_node *pn_root; 111 puffs_framev_fdnotify_fn notfn; 112 struct vattr *rva; 113 mntoptparse_t mp; 114 char **sshargs; 115 char *userhost; 116 char *hostpath; 117 int mntflags, pflags, ch; 118 int detach; 119 int exportfs, refreshival; 120 int nargs, x; 121 122 setprogname(argv[0]); 123 124 if (argc < 3) 125 usage(); 126 127 mntflags = pflags = exportfs = nargs = 0; 128 detach = 1; 129 refreshival = DEFAULTREFRESH; 130 notfn = puffs_framev_unmountonclose; 131 sshargs = NULL; 132 add_ssharg(&sshargs, &nargs, SSH_PATH); 133 add_ssharg(&sshargs, &nargs, "-axs"); 134 add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes"); 135 136 while ((ch = getopt(argc, argv, "eF:o:O:pr:st:")) != -1) { 137 switch (ch) { 138 case 'e': 139 exportfs = 1; 140 break; 141 case 'F': 142 add_ssharg(&sshargs, &nargs, "-F"); 143 add_ssharg(&sshargs, &nargs, optarg); 144 break; 145 case 'O': 146 add_ssharg(&sshargs, &nargs, "-o"); 147 add_ssharg(&sshargs, &nargs, optarg); 148 break; 149 case 'o': 150 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags); 151 if (mp == NULL) 152 err(1, "getmntopts"); 153 freemntopts(mp); 154 break; 155 case 'p': 156 notfn = psshfs_notify; 157 break; 158 case 'r': 159 max_reads = atoi(optarg); 160 break; 161 case 's': 162 detach = 0; 163 break; 164 case 't': 165 refreshival = atoi(optarg); 166 if (refreshival < 0 && refreshival != -1) 167 errx(1, "invalid timeout %d", refreshival); 168 break; 169 default: 170 usage(); 171 /*NOTREACHED*/ 172 } 173 } 174 argc -= optind; 175 argv += optind; 176 177 if (pflags & PUFFS_FLAG_OPDUMP) 178 detach = 0; 179 pflags |= PUFFS_FLAG_BUILDPATH; 180 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND; 181 182 if (argc != 2) 183 usage(); 184 185 PUFFSOP_INIT(pops); 186 187 PUFFSOP_SET(pops, psshfs, fs, unmount); 188 PUFFSOP_SETFSNOP(pops, sync); /* XXX */ 189 PUFFSOP_SETFSNOP(pops, statvfs); 190 PUFFSOP_SET(pops, psshfs, fs, nodetofh); 191 PUFFSOP_SET(pops, psshfs, fs, fhtonode); 192 193 PUFFSOP_SET(pops, psshfs, node, lookup); 194 PUFFSOP_SET(pops, psshfs, node, create); 195 PUFFSOP_SET(pops, psshfs, node, open); 196 PUFFSOP_SET(pops, psshfs, node, inactive); 197 PUFFSOP_SET(pops, psshfs, node, readdir); 198 PUFFSOP_SET(pops, psshfs, node, getattr); 199 PUFFSOP_SET(pops, psshfs, node, setattr); 200 PUFFSOP_SET(pops, psshfs, node, mkdir); 201 PUFFSOP_SET(pops, psshfs, node, remove); 202 PUFFSOP_SET(pops, psshfs, node, readlink); 203 PUFFSOP_SET(pops, psshfs, node, rmdir); 204 PUFFSOP_SET(pops, psshfs, node, symlink); 205 PUFFSOP_SET(pops, psshfs, node, rename); 206 PUFFSOP_SET(pops, psshfs, node, read); 207 PUFFSOP_SET(pops, psshfs, node, write); 208 PUFFSOP_SET(pops, psshfs, node, reclaim); 209 210 pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags); 211 if (pu == NULL) 212 err(1, "puffs_init"); 213 214 memset(&pctx, 0, sizeof(pctx)); 215 pctx.mounttime = time(NULL); 216 pctx.refreshival = refreshival; 217 218 userhost = argv[0]; 219 hostpath = strchr(userhost, ':'); 220 if (hostpath) { 221 *hostpath++ = '\0'; 222 pctx.mountpath = hostpath; 223 } else 224 pctx.mountpath = "."; 225 226 add_ssharg(&sshargs, &nargs, argv[0]); 227 add_ssharg(&sshargs, &nargs, "sftp"); 228 pctx.sshargs = sshargs; 229 230 pctx.nextino = 2; 231 memset(root, 0, sizeof(struct psshfs_node)); 232 pn_root = puffs_pn_new(pu, root); 233 if (pn_root == NULL) 234 return errno; 235 puffs_setroot(pu, pn_root); 236 237 signal(SIGHUP, takehup); 238 puffs_ml_setloopfn(pu, psshfs_loopfn); 239 if (pssh_connect(&pctx) == -1) 240 err(1, "can't connect"); 241 242 if (exportfs) 243 puffs_setfhsize(pu, sizeof(struct psshfs_fid), 244 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3); 245 246 if (psshfs_handshake(pu) != 0) 247 errx(1, "psshfs_handshake"); 248 x = 1; 249 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1) 250 err(1, "nonblocking descriptor"); 251 252 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn); 253 if (puffs_framev_addfd(pu, pctx.sshfd, 254 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) 255 err(1, "framebuf addfd"); 256 257 rva = &pn_root->pn_va; 258 rva->va_fileid = pctx.nextino++; 259 rva->va_nlink = 101; /* XXX */ 260 261 if (detach) 262 if (puffs_daemon(pu, 1, 1) == -1) 263 err(1, "puffs_daemon"); 264 265 if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1) 266 err(1, "puffs_mount"); 267 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) 268 err(1, "setblockingmode"); 269 270 if (puffs_mainloop(pu) == -1) 271 err(1, "mainloop"); 272 puffs_exit(pu, 1); 273 274 return 0; 275 } 276 277 #define RETRY_MAX 100 278 279 void 280 psshfs_notify(struct puffs_usermount *pu, int fd, int what) 281 { 282 struct psshfs_ctx *pctx = puffs_getspecific(pu); 283 int x, nretry; 284 285 if (puffs_getstate(pu) != PUFFS_STATE_RUNNING) 286 return; 287 288 if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) { 289 puffs_framev_removefd(pu, pctx->sshfd, ECONNRESET); 290 return; 291 } 292 close(pctx->sshfd); 293 294 for (nretry = 0;;nretry++) { 295 if (pssh_connect(pctx) == -1) 296 goto retry2; 297 298 if (psshfs_handshake(pu) != 0) 299 goto retry1; 300 301 x = 1; 302 if (ioctl(pctx->sshfd, FIONBIO, &x) == -1) 303 goto retry1; 304 305 if (puffs_framev_addfd(pu, pctx->sshfd, 306 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) 307 goto retry1; 308 309 break; 310 retry1: 311 fprintf(stderr, "reconnect failed... "); 312 close(pctx->sshfd); 313 retry2: 314 if (nretry < RETRY_MAX) { 315 fprintf(stderr, "retrying\n"); 316 sleep(nretry); 317 } else { 318 fprintf(stderr, "retry count exceeded, going south\n"); 319 exit(1); /* XXXXXXX */ 320 } 321 } 322 } 323 324 static int 325 pssh_connect(struct psshfs_ctx *pctx) 326 { 327 char **sshargs = pctx->sshargs; 328 int fds[2]; 329 pid_t pid; 330 int dnfd; 331 332 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) 333 return -1; 334 335 pid = fork(); 336 switch (pid) { 337 case -1: 338 return -1; 339 /*NOTREACHED*/ 340 case 0: /* child */ 341 if (dup2(fds[0], STDIN_FILENO) == -1) 342 err(1, "child dup2"); 343 if (dup2(fds[0], STDOUT_FILENO) == -1) 344 err(1, "child dup2"); 345 close(fds[0]); 346 close(fds[1]); 347 348 dnfd = open(_PATH_DEVNULL, O_RDWR); 349 if (dnfd != -1) 350 dup2(dnfd, STDERR_FILENO); 351 352 execvp(sshargs[0], sshargs); 353 break; 354 default: 355 pctx->sshpid = pid; 356 pctx->sshfd = fds[1]; 357 close(fds[0]); 358 break; 359 } 360 361 return 0; 362 } 363 364 static void * 365 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg) 366 { 367 struct psshfs_node *psn = pn->pn_data; 368 369 psn->attrread = 0; 370 psn->dentread = 0; 371 psn->slread = 0; 372 373 return NULL; 374 } 375 376 static void 377 psshfs_loopfn(struct puffs_usermount *pu) 378 { 379 380 if (sighup) { 381 puffs_pn_nodewalk(pu, invalone, NULL); 382 sighup = 0; 383 } 384 } 385