psshfs.c revision 1.46 1 /* $NetBSD: psshfs.c,v 1.46 2007/11/30 16:24:04 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.46 2007/11/30 16:24:04 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, inactive);
183 PUFFSOP_SET(pops, psshfs, node, readdir);
184 PUFFSOP_SET(pops, psshfs, node, getattr);
185 PUFFSOP_SET(pops, psshfs, node, setattr);
186 PUFFSOP_SET(pops, psshfs, node, mkdir);
187 PUFFSOP_SET(pops, psshfs, node, remove);
188 PUFFSOP_SET(pops, psshfs, node, readlink);
189 PUFFSOP_SET(pops, psshfs, node, rmdir);
190 PUFFSOP_SET(pops, psshfs, node, symlink);
191 PUFFSOP_SET(pops, psshfs, node, rename);
192 PUFFSOP_SET(pops, psshfs, node, read);
193 PUFFSOP_SET(pops, psshfs, node, write);
194 PUFFSOP_SET(pops, psshfs, node, reclaim);
195
196 pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
197 if (pu == NULL)
198 err(1, "puffs_init");
199
200 memset(&pctx, 0, sizeof(pctx));
201 pctx.mounttime = time(NULL);
202 pctx.refreshival = refreshival;
203
204 userhost = argv[0];
205 hostpath = strchr(userhost, ':');
206 if (hostpath) {
207 *hostpath++ = '\0';
208 pctx.mountpath = hostpath;
209 } else
210 pctx.mountpath = ".";
211
212 add_ssharg(&sshargs, &nargs, argv[0]);
213 add_ssharg(&sshargs, &nargs, "sftp");
214
215 signal(SIGHUP, takehup);
216 puffs_ml_setloopfn(pu, psshfs_loopfn);
217
218 pssh_connect(&pctx, sshargs);
219
220 if (exportfs)
221 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
222 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
223
224 if (psshfs_domount(pu) != 0)
225 errx(1, "psshfs_domount");
226 x = 1;
227 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
228 err(1, "nonblocking descriptor");
229
230 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL,
231 puffs_framev_unmountonclose);
232 if (puffs_framev_addfd(pu, pctx.sshfd,
233 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
234 err(1, "framebuf addfd");
235
236 if (detach)
237 if (puffs_daemon(pu, 1, 1) == -1)
238 err(1, "puffs_daemon");
239
240 if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
241 err(1, "puffs_mount");
242 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
243 err(1, "setblockingmode");
244
245 if (puffs_mainloop(pu) == -1)
246 err(1, "mainloop");
247
248 return 0;
249 }
250
251 static void
252 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
253 {
254 int fds[2];
255 pid_t pid;
256 int dnfd;
257
258 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
259 err(1, "socketpair");
260
261 pid = fork();
262 switch (pid) {
263 case -1:
264 err(1, "fork");
265 /*NOTREACHED*/
266 case 0: /* child */
267 if (dup2(fds[0], STDIN_FILENO) == -1)
268 err(1, "child dup2");
269 if (dup2(fds[0], STDOUT_FILENO) == -1)
270 err(1, "child dup2");
271 close(fds[0]);
272 close(fds[1]);
273
274 dnfd = open(_PATH_DEVNULL, O_RDWR);
275 if (dnfd != -1)
276 dup2(dnfd, STDERR_FILENO);
277
278 execvp(sshargs[0], sshargs);
279 break;
280 default:
281 pctx->sshpid = pid;
282 pctx->sshfd = fds[1];
283 close(fds[0]);
284 break;
285 }
286 }
287
288 static void *
289 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
290 {
291 struct psshfs_node *psn = pn->pn_data;
292
293 psn->attrread = 0;
294 psn->dentread = 0;
295 psn->slread = 0;
296
297 return NULL;
298 }
299
300 static void
301 psshfs_loopfn(struct puffs_usermount *pu)
302 {
303
304 if (sighup) {
305 puffs_pn_nodewalk(pu, invalone, NULL);
306 sighup = 0;
307 }
308 }
309