Home | History | Annotate | Line # | Download | only in mount_psshfs
psshfs.c revision 1.34.4.2
      1  1.34.4.2   matt /*	$NetBSD: psshfs.c,v 1.34.4.2 2008/01/09 02:02:20 matt 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.34.4.2   matt __RCSID("$NetBSD: psshfs.c,v 1.34.4.2 2008/01/09 02:02:20 matt 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.34.4.2   matt 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.34.4.1   matt unsigned int max_reads;
     71  1.34.4.2   matt static int sighup;
     72  1.34.4.1   matt 
     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.34.4.2   matt 	    "[-es] [-F configfile] [-O sshopt=value] [-o opts] "
     90  1.34.4.2   matt 	    "user@host:path mountpath\n",
     91       1.4  pooka 	    getprogname());
     92      1.23  pooka 	exit(1);
     93       1.2  pooka }
     94       1.2  pooka 
     95  1.34.4.2   matt static void
     96  1.34.4.2   matt takehup(int sig)
     97  1.34.4.2   matt {
     98  1.34.4.2   matt 
     99  1.34.4.2   matt 	sighup = 1;
    100  1.34.4.2   matt }
    101  1.34.4.2   matt 
    102       1.1  pooka int
    103       1.1  pooka main(int argc, char *argv[])
    104       1.1  pooka {
    105       1.1  pooka 	struct psshfs_ctx pctx;
    106       1.1  pooka 	struct puffs_usermount *pu;
    107       1.1  pooka 	struct puffs_ops *pops;
    108       1.2  pooka 	mntoptparse_t mp;
    109      1.21    tnn 	char **sshargs;
    110       1.1  pooka 	char *userhost;
    111       1.1  pooka 	char *hostpath;
    112  1.34.4.1   matt 	int mntflags, pflags, ch;
    113  1.34.4.1   matt 	int detach;
    114  1.34.4.2   matt 	int exportfs, refreshival;
    115      1.21    tnn 	int nargs, x;
    116       1.1  pooka 
    117       1.1  pooka 	setprogname(argv[0]);
    118       1.1  pooka 
    119       1.2  pooka 	if (argc < 3)
    120       1.2  pooka 		usage();
    121       1.2  pooka 
    122  1.34.4.1   matt 	mntflags = pflags = exportfs = nargs = 0;
    123  1.34.4.1   matt 	detach = 1;
    124  1.34.4.2   matt 	refreshival = DEFAULTREFRESH;
    125      1.21    tnn 	sshargs = NULL;
    126      1.21    tnn 	add_ssharg(&sshargs, &nargs, SSH_PATH);
    127      1.21    tnn 	add_ssharg(&sshargs, &nargs, "-axs");
    128      1.21    tnn 	add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
    129      1.21    tnn 
    130  1.34.4.2   matt 	while ((ch = getopt(argc, argv, "eF:o:O:r:st:")) != -1) {
    131       1.2  pooka 		switch (ch) {
    132      1.11  pooka 		case 'e':
    133      1.13  pooka 			exportfs = 1;
    134      1.11  pooka 			break;
    135  1.34.4.2   matt 		case 'F':
    136  1.34.4.2   matt 			add_ssharg(&sshargs, &nargs, "-F");
    137  1.34.4.2   matt 			add_ssharg(&sshargs, &nargs, optarg);
    138  1.34.4.2   matt 			break;
    139      1.21    tnn 		case 'O':
    140      1.22    tnn 			add_ssharg(&sshargs, &nargs, "-o");
    141      1.22    tnn 			add_ssharg(&sshargs, &nargs, optarg);
    142      1.21    tnn 			break;
    143       1.2  pooka 		case 'o':
    144       1.2  pooka 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    145       1.2  pooka 			if (mp == NULL)
    146       1.2  pooka 				err(1, "getmntopts");
    147       1.2  pooka 			freemntopts(mp);
    148       1.2  pooka 			break;
    149  1.34.4.1   matt 		case 'r':
    150  1.34.4.1   matt 			max_reads = atoi(optarg);
    151  1.34.4.1   matt 			break;
    152       1.4  pooka 		case 's':
    153  1.34.4.1   matt 			detach = 0;
    154       1.4  pooka 			break;
    155  1.34.4.2   matt 		case 't':
    156  1.34.4.2   matt 			refreshival = atoi(optarg);
    157  1.34.4.2   matt 			if (refreshival < 0 && refreshival != -1)
    158  1.34.4.2   matt 				errx(1, "invalid timeout %d", refreshival);
    159  1.34.4.2   matt 			break;
    160       1.2  pooka 		default:
    161       1.2  pooka 			usage();
    162       1.2  pooka 			/*NOTREACHED*/
    163       1.2  pooka 		}
    164       1.2  pooka 	}
    165       1.2  pooka 	argc -= optind;
    166       1.2  pooka 	argv += optind;
    167       1.2  pooka 
    168       1.4  pooka 	if (pflags & PUFFS_FLAG_OPDUMP)
    169  1.34.4.1   matt 		detach = 0;
    170      1.20  pooka 	pflags |= PUFFS_FLAG_BUILDPATH;
    171      1.20  pooka 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
    172       1.4  pooka 
    173       1.2  pooka 	if (argc != 2)
    174       1.2  pooka 		usage();
    175       1.1  pooka 
    176       1.1  pooka 	PUFFSOP_INIT(pops);
    177       1.1  pooka 
    178       1.1  pooka 	PUFFSOP_SET(pops, psshfs, fs, unmount);
    179       1.1  pooka 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
    180       1.1  pooka 	PUFFSOP_SETFSNOP(pops, statvfs);
    181      1.11  pooka 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
    182      1.11  pooka 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
    183       1.1  pooka 
    184       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, lookup);
    185       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, create);
    186      1.19  pooka 	PUFFSOP_SET(pops, psshfs, node, open);
    187      1.19  pooka 	PUFFSOP_SET(pops, psshfs, node, inactive);
    188       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, readdir);
    189       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, getattr);
    190       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, setattr);
    191       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, mkdir);
    192       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, remove);
    193       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, readlink);
    194       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, rmdir);
    195       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, symlink);
    196       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, rename);
    197       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, read);
    198       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, write);
    199       1.1  pooka 	PUFFSOP_SET(pops, psshfs, node, reclaim);
    200       1.1  pooka 
    201      1.33  pooka 	pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
    202      1.33  pooka 	if (pu == NULL)
    203      1.33  pooka 		err(1, "puffs_init");
    204      1.33  pooka 
    205       1.1  pooka 	memset(&pctx, 0, sizeof(pctx));
    206      1.11  pooka 	pctx.mounttime = time(NULL);
    207  1.34.4.2   matt 	pctx.refreshival = refreshival;
    208       1.1  pooka 
    209       1.2  pooka 	userhost = argv[0];
    210       1.1  pooka 	hostpath = strchr(userhost, ':');
    211       1.1  pooka 	if (hostpath) {
    212       1.1  pooka 		*hostpath++ = '\0';
    213       1.1  pooka 		pctx.mountpath = hostpath;
    214       1.1  pooka 	} else
    215       1.1  pooka 		pctx.mountpath = ".";
    216       1.1  pooka 
    217      1.21    tnn 	add_ssharg(&sshargs, &nargs, argv[0]);
    218      1.21    tnn 	add_ssharg(&sshargs, &nargs, "sftp");
    219       1.1  pooka 
    220  1.34.4.2   matt 	signal(SIGHUP, takehup);
    221  1.34.4.2   matt 	puffs_ml_setloopfn(pu, psshfs_loopfn);
    222  1.34.4.2   matt 
    223       1.4  pooka 	pssh_connect(&pctx, sshargs);
    224       1.4  pooka 
    225      1.13  pooka 	if (exportfs)
    226      1.13  pooka 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
    227      1.13  pooka 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
    228      1.13  pooka 
    229       1.2  pooka 	if (psshfs_domount(pu) != 0)
    230      1.13  pooka 		errx(1, "psshfs_domount");
    231       1.1  pooka 	x = 1;
    232      1.17  pooka 	if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
    233       1.1  pooka 		err(1, "nonblocking descriptor");
    234       1.1  pooka 
    235  1.34.4.1   matt 	puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL,
    236      1.26  pooka 	    puffs_framev_unmountonclose);
    237      1.31  pooka 	if (puffs_framev_addfd(pu, pctx.sshfd,
    238      1.31  pooka 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
    239      1.25  pooka 		err(1, "framebuf addfd");
    240       1.1  pooka 
    241  1.34.4.1   matt 	if (detach)
    242  1.34.4.2   matt 		if (puffs_daemon(pu, 1, 1) == -1)
    243  1.34.4.2   matt 			err(1, "puffs_daemon");
    244  1.34.4.1   matt 
    245      1.27  pooka 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
    246      1.27  pooka 		err(1, "puffs_mount");
    247  1.34.4.2   matt 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    248  1.34.4.2   matt 		err(1, "setblockingmode");
    249  1.34.4.2   matt 
    250  1.34.4.1   matt 	if (puffs_mainloop(pu) == -1)
    251  1.34.4.1   matt 		err(1, "mainloop");
    252      1.27  pooka 
    253  1.34.4.1   matt 	return 0;
    254       1.1  pooka }
    255       1.1  pooka 
    256       1.1  pooka static void
    257       1.1  pooka pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
    258       1.1  pooka {
    259       1.1  pooka 	int fds[2];
    260       1.1  pooka 	pid_t pid;
    261      1.28  pooka 	int dnfd;
    262       1.1  pooka 
    263       1.1  pooka 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
    264       1.1  pooka 		err(1, "socketpair");
    265       1.1  pooka 
    266       1.1  pooka 	pid = fork();
    267       1.1  pooka 	switch (pid) {
    268       1.1  pooka 	case -1:
    269       1.1  pooka 		err(1, "fork");
    270       1.1  pooka 		/*NOTREACHED*/
    271       1.1  pooka 	case 0: /* child */
    272       1.1  pooka 		if (dup2(fds[0], STDIN_FILENO) == -1)
    273       1.1  pooka 			err(1, "child dup2");
    274       1.1  pooka 		if (dup2(fds[0], STDOUT_FILENO) == -1)
    275       1.1  pooka 			err(1, "child dup2");
    276       1.1  pooka 		close(fds[0]);
    277       1.1  pooka 		close(fds[1]);
    278      1.28  pooka 
    279      1.28  pooka 		dnfd = open(_PATH_DEVNULL, O_RDWR);
    280      1.28  pooka 		if (dnfd != -1)
    281      1.28  pooka 			dup2(dnfd, STDERR_FILENO);
    282      1.28  pooka 
    283       1.1  pooka 		execvp(sshargs[0], sshargs);
    284       1.1  pooka 		break;
    285       1.1  pooka 	default:
    286       1.1  pooka 		pctx->sshpid = pid;
    287       1.1  pooka 		pctx->sshfd = fds[1];
    288       1.1  pooka 		close(fds[0]);
    289       1.1  pooka 		break;
    290       1.1  pooka 	}
    291       1.1  pooka }
    292  1.34.4.2   matt 
    293  1.34.4.2   matt static void *
    294  1.34.4.2   matt invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
    295  1.34.4.2   matt {
    296  1.34.4.2   matt 	struct psshfs_node *psn = pn->pn_data;
    297  1.34.4.2   matt 
    298  1.34.4.2   matt 	psn->attrread = 0;
    299  1.34.4.2   matt 	psn->dentread = 0;
    300  1.34.4.2   matt 	psn->slread = 0;
    301  1.34.4.2   matt 
    302  1.34.4.2   matt 	return NULL;
    303  1.34.4.2   matt }
    304  1.34.4.2   matt 
    305  1.34.4.2   matt static void
    306  1.34.4.2   matt psshfs_loopfn(struct puffs_usermount *pu)
    307  1.34.4.2   matt {
    308  1.34.4.2   matt 
    309  1.34.4.2   matt 	if (sighup) {
    310  1.34.4.2   matt 		puffs_pn_nodewalk(pu, invalone, NULL);
    311  1.34.4.2   matt 		sighup = 0;
    312  1.34.4.2   matt 	}
    313  1.34.4.2   matt }
    314