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