Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.c revision 1.29
      1 /*	$NetBSD: psshfs.c,v 1.29 2007/06/06 01:55:03 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.29 2007/06/06 01:55:03 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 void	pssh_connect(struct psshfs_ctx *, char **);
     64 static void	usage(void);
     65 static void	add_ssharg(char ***, int *, char *);
     66 
     67 #define SSH_PATH "/usr/bin/ssh"
     68 
     69 static void
     70 add_ssharg(char ***sshargs, int *nargs, char *arg)
     71 {
     72 
     73 	*sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
     74 	if (!*sshargs)
     75 		err(1, "realloc");
     76 	(*sshargs)[(*nargs)++] = arg;
     77 	(*sshargs)[*nargs] = NULL;
     78 }
     79 
     80 static void
     81 usage()
     82 {
     83 
     84 	fprintf(stderr, "usage: %s "
     85 	    "[-es] [-O sshopt=value] [-o opts] user@host:path mountpath\n",
     86 	    getprogname());
     87 	exit(1);
     88 }
     89 
     90 int
     91 main(int argc, char *argv[])
     92 {
     93 	struct psshfs_ctx pctx;
     94 	struct puffs_usermount *pu;
     95 	struct puffs_ops *pops;
     96 	mntoptparse_t mp;
     97 	char **sshargs;
     98 	char *userhost;
     99 	char *hostpath;
    100 	int mntflags, pflags, lflags, ch;
    101 	int exportfs;
    102 	int nargs, x;
    103 
    104 	setprogname(argv[0]);
    105 
    106 	if (argc < 3)
    107 		usage();
    108 
    109 	mntflags = pflags = lflags = exportfs = nargs = 0;
    110 	sshargs = NULL;
    111 	add_ssharg(&sshargs, &nargs, SSH_PATH);
    112 	add_ssharg(&sshargs, &nargs, "-axs");
    113 	add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
    114 
    115 	while ((ch = getopt(argc, argv, "eo:O:s")) != -1) {
    116 		switch (ch) {
    117 		case 'e':
    118 			exportfs = 1;
    119 			break;
    120 		case 'O':
    121 			add_ssharg(&sshargs, &nargs, "-o");
    122 			add_ssharg(&sshargs, &nargs, optarg);
    123 			break;
    124 		case 'o':
    125 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    126 			if (mp == NULL)
    127 				err(1, "getmntopts");
    128 			freemntopts(mp);
    129 			break;
    130 		case 's':
    131 			lflags |= PUFFSLOOP_NODAEMON;
    132 			break;
    133 		default:
    134 			usage();
    135 			/*NOTREACHED*/
    136 		}
    137 	}
    138 	argc -= optind;
    139 	argv += optind;
    140 
    141 #if 0
    142 	/* XXX: noatime is mandatory for now */
    143 	mntflags |= MNT_NOATIME;
    144 #endif
    145 
    146 	if (pflags & PUFFS_FLAG_OPDUMP)
    147 		lflags |= PUFFSLOOP_NODAEMON;
    148 	pflags |= PUFFS_FLAG_BUILDPATH;
    149 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
    150 
    151 	if (argc != 2)
    152 		usage();
    153 
    154 	PUFFSOP_INIT(pops);
    155 
    156 	PUFFSOP_SET(pops, psshfs, fs, unmount);
    157 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
    158 	PUFFSOP_SETFSNOP(pops, statvfs);
    159 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
    160 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
    161 
    162 	PUFFSOP_SET(pops, psshfs, node, lookup);
    163 	PUFFSOP_SET(pops, psshfs, node, create);
    164 	PUFFSOP_SET(pops, psshfs, node, open);
    165 	PUFFSOP_SET(pops, psshfs, node, inactive);
    166 	PUFFSOP_SET(pops, psshfs, node, readdir);
    167 	PUFFSOP_SET(pops, psshfs, node, getattr);
    168 	PUFFSOP_SET(pops, psshfs, node, setattr);
    169 	PUFFSOP_SET(pops, psshfs, node, mkdir);
    170 	PUFFSOP_SET(pops, psshfs, node, remove);
    171 	PUFFSOP_SET(pops, psshfs, node, readlink);
    172 	PUFFSOP_SET(pops, psshfs, node, rmdir);
    173 	PUFFSOP_SET(pops, psshfs, node, symlink);
    174 	PUFFSOP_SET(pops, psshfs, node, rename);
    175 	PUFFSOP_SET(pops, psshfs, node, read);
    176 	PUFFSOP_SET(pops, psshfs, node, write);
    177 	PUFFSOP_SET(pops, psshfs, node, reclaim);
    178 
    179 	memset(&pctx, 0, sizeof(pctx));
    180 	pctx.mounttime = time(NULL);
    181 
    182 	userhost = argv[0];
    183 	hostpath = strchr(userhost, ':');
    184 	if (hostpath) {
    185 		*hostpath++ = '\0';
    186 		pctx.mountpath = hostpath;
    187 	} else
    188 		pctx.mountpath = ".";
    189 
    190 	add_ssharg(&sshargs, &nargs, argv[0]);
    191 	add_ssharg(&sshargs, &nargs, "sftp");
    192 
    193 	pu = puffs_init(pops, "ppshfs", &pctx, pflags);
    194 	if (pu == NULL)
    195 		err(1, "puffs_init");
    196 
    197 	pssh_connect(&pctx, sshargs);
    198 
    199 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    200 		err(1, "setblockingmode");
    201 	if (exportfs)
    202 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
    203 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
    204 
    205 	if (psshfs_domount(pu) != 0)
    206 		errx(1, "psshfs_domount");
    207 	x = 1;
    208 	if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
    209 		err(1, "nonblocking descriptor");
    210 
    211 	puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp,
    212 	    puffs_framev_unmountonclose);
    213 	if (puffs_framev_addfd(pu, pctx.sshfd) == -1)
    214 		err(1, "framebuf addfd");
    215 
    216 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
    217 		err(1, "puffs_mount");
    218 
    219 	return puffs_mainloop(pu, lflags);
    220 }
    221 
    222 static void
    223 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
    224 {
    225 	int fds[2];
    226 	pid_t pid;
    227 	int dnfd;
    228 
    229 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
    230 		err(1, "socketpair");
    231 
    232 	pid = fork();
    233 	switch (pid) {
    234 	case -1:
    235 		err(1, "fork");
    236 		/*NOTREACHED*/
    237 	case 0: /* child */
    238 		if (dup2(fds[0], STDIN_FILENO) == -1)
    239 			err(1, "child dup2");
    240 		if (dup2(fds[0], STDOUT_FILENO) == -1)
    241 			err(1, "child dup2");
    242 		close(fds[0]);
    243 		close(fds[1]);
    244 
    245 		dnfd = open(_PATH_DEVNULL, O_RDWR);
    246 		if (dnfd != -1)
    247 			dup2(dnfd, STDERR_FILENO);
    248 
    249 		execvp(sshargs[0], sshargs);
    250 		break;
    251 	default:
    252 		pctx->sshpid = pid;
    253 		pctx->sshfd = fds[1];
    254 		close(fds[0]);
    255 		break;
    256 	}
    257 }
    258