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