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