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