ninepuffs.c revision 1.31 1 1.31 wiz /* $NetBSD: ninepuffs.c,v 1.31 2020/06/13 16:56:46 wiz Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 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 * 9puffs: access a 9P file server as a vfs via puffs
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka #include <sys/cdefs.h>
33 1.1 pooka #ifndef lint
34 1.31 wiz __RCSID("$NetBSD: ninepuffs.c,v 1.31 2020/06/13 16:56:46 wiz Exp $");
35 1.1 pooka #endif /* !lint */
36 1.1 pooka
37 1.1 pooka #include <sys/types.h>
38 1.1 pooka #include <sys/socket.h>
39 1.1 pooka #include <sys/poll.h>
40 1.1 pooka
41 1.1 pooka #include <netinet/in.h>
42 1.1 pooka
43 1.1 pooka #include <assert.h>
44 1.1 pooka #include <err.h>
45 1.30 uwe #include <errno.h>
46 1.1 pooka #include <netdb.h>
47 1.15 pooka #include <pwd.h>
48 1.1 pooka #include <puffs.h>
49 1.1 pooka #include <stdio.h>
50 1.1 pooka #include <stdlib.h>
51 1.1 pooka #include <unistd.h>
52 1.1 pooka
53 1.1 pooka #include "ninepuffs.h"
54 1.25 ozaki #include "nineproto.h"
55 1.1 pooka
56 1.30 uwe #define DEFPORT_9P "564" /* "9pfs", but don't depend on it being in services */
57 1.1 pooka
58 1.24 joerg __dead static void
59 1.1 pooka usage(void)
60 1.1 pooka {
61 1.1 pooka
62 1.31 wiz fprintf(stderr, "usage: %s [-46su] [-o mntopts] [-p port] "
63 1.18 pooka "[user@]server[:path] mountpoint\n", getprogname());
64 1.31 wiz fprintf(stderr, " %s -c [-46su] [-o mntopts] devfile mountpoint\n",
65 1.27 ozaki getprogname());
66 1.18 pooka exit(1);
67 1.1 pooka }
68 1.1 pooka
69 1.1 pooka /*
70 1.30 uwe * TCP connection to 9P file server.
71 1.1 pooka * Return connected socket or exit with error.
72 1.1 pooka */
73 1.1 pooka static int
74 1.30 uwe serverconnect(const char *hostname, const char *portname, int family)
75 1.1 pooka {
76 1.30 uwe int ret;
77 1.30 uwe
78 1.30 uwe struct addrinfo hints;
79 1.30 uwe memset(&hints, 0, sizeof(hints));
80 1.30 uwe hints.ai_family = family;
81 1.30 uwe hints.ai_socktype = SOCK_STREAM;
82 1.30 uwe
83 1.30 uwe if (portname == NULL) {
84 1.30 uwe portname = DEFPORT_9P;
85 1.30 uwe hints.ai_flags |= AI_NUMERICSERV;
86 1.1 pooka }
87 1.1 pooka
88 1.30 uwe struct addrinfo *ai0;
89 1.30 uwe ret = getaddrinfo(hostname, portname, &hints, &ai0);
90 1.30 uwe if (ret != 0)
91 1.30 uwe errx(EXIT_FAILURE, "%s", gai_strerror(ret));
92 1.30 uwe
93 1.30 uwe int s = -1;
94 1.30 uwe const char *cause = NULL;
95 1.30 uwe for (struct addrinfo *ai = ai0; ai != NULL; ai = ai->ai_next) {
96 1.30 uwe s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
97 1.30 uwe if (s < 0) {
98 1.30 uwe cause = "socket";
99 1.30 uwe continue;
100 1.30 uwe }
101 1.30 uwe
102 1.30 uwe const int opt = 1;
103 1.30 uwe ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
104 1.30 uwe if (ret < 0) {
105 1.30 uwe cause = "SO_NOSIGPIPE";
106 1.30 uwe continue;
107 1.30 uwe }
108 1.1 pooka
109 1.30 uwe ret = connect(s, ai->ai_addr, ai->ai_addrlen);
110 1.30 uwe if (ret < 0) {
111 1.30 uwe close(s);
112 1.30 uwe s = -1;
113 1.30 uwe cause = "connect";
114 1.30 uwe continue;
115 1.30 uwe }
116 1.30 uwe }
117 1.1 pooka
118 1.30 uwe if (s < 0)
119 1.30 uwe err(EXIT_FAILURE, "%s", cause);
120 1.1 pooka
121 1.30 uwe freeaddrinfo(ai0);
122 1.1 pooka return s;
123 1.1 pooka }
124 1.1 pooka
125 1.27 ozaki static int
126 1.27 ozaki open_cdev(const char *path)
127 1.27 ozaki {
128 1.27 ozaki int s;
129 1.27 ozaki
130 1.27 ozaki s = open(path, O_RDWR, 0);
131 1.27 ozaki if (s == -1)
132 1.27 ozaki err(1, "%s", path);
133 1.27 ozaki return s;
134 1.27 ozaki }
135 1.27 ozaki
136 1.1 pooka int
137 1.1 pooka main(int argc, char *argv[])
138 1.1 pooka {
139 1.1 pooka struct puffs9p p9p;
140 1.1 pooka struct puffs_usermount *pu;
141 1.1 pooka struct puffs_ops *pops;
142 1.1 pooka struct puffs_node *pn_root;
143 1.1 pooka mntoptparse_t mp;
144 1.30 uwe int family;
145 1.15 pooka const char *user, *srvhost, *srvpath;
146 1.15 pooka char *p;
147 1.30 uwe const char *port;
148 1.19 pooka int mntflags, pflags, ch;
149 1.1 pooka int detach;
150 1.27 ozaki int protover;
151 1.27 ozaki int server;
152 1.1 pooka
153 1.1 pooka setprogname(argv[0]);
154 1.1 pooka
155 1.14 pooka if (argc < 2)
156 1.1 pooka usage();
157 1.1 pooka
158 1.19 pooka mntflags = pflags = 0;
159 1.1 pooka detach = 1;
160 1.30 uwe #ifdef INET6
161 1.30 uwe family = AF_UNSPEC;
162 1.30 uwe #else
163 1.30 uwe family = AF_INET;
164 1.30 uwe #endif
165 1.30 uwe port = NULL;
166 1.27 ozaki protover = P9PROTO_VERSION;
167 1.27 ozaki server = P9P_SERVER_TCP;
168 1.1 pooka
169 1.30 uwe while ((ch = getopt(argc, argv, "46co:p:su")) != -1) {
170 1.1 pooka switch (ch) {
171 1.30 uwe case '4':
172 1.30 uwe family = AF_INET;
173 1.30 uwe break;
174 1.30 uwe case '6':
175 1.30 uwe #ifdef INET6
176 1.30 uwe family = AF_INET6;
177 1.30 uwe break;
178 1.30 uwe #else
179 1.30 uwe errno = EPFNOSUPPORT;
180 1.30 uwe err(EXIT_FAILURE, "IPv6");
181 1.30 uwe /* NOTREACHED */
182 1.30 uwe #endif
183 1.27 ozaki case 'c':
184 1.27 ozaki server = P9P_SERVER_CDEV;
185 1.27 ozaki break;
186 1.1 pooka case 'o':
187 1.1 pooka mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
188 1.1 pooka if (mp == NULL)
189 1.1 pooka err(1, "getmntopts");
190 1.1 pooka freemntopts(mp);
191 1.1 pooka break;
192 1.1 pooka case 'p':
193 1.30 uwe port = optarg;
194 1.1 pooka break;
195 1.1 pooka case 's':
196 1.19 pooka detach = 0;
197 1.1 pooka break;
198 1.25 ozaki case 'u':
199 1.25 ozaki protover = P9PROTO_VERSION_U;
200 1.25 ozaki break;
201 1.1 pooka default:
202 1.1 pooka usage();
203 1.1 pooka /*NOTREACHED*/
204 1.1 pooka }
205 1.1 pooka }
206 1.1 pooka argc -= optind;
207 1.1 pooka argv += optind;
208 1.1 pooka
209 1.14 pooka if (argc != 2)
210 1.1 pooka usage();
211 1.1 pooka
212 1.1 pooka if (pflags & PUFFS_FLAG_OPDUMP)
213 1.19 pooka detach = 0;
214 1.7 pooka pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
215 1.1 pooka
216 1.1 pooka PUFFSOP_INIT(pops);
217 1.1 pooka
218 1.1 pooka PUFFSOP_SET(pops, puffs9p, fs, unmount);
219 1.1 pooka PUFFSOP_SETFSNOP(pops, sync);
220 1.1 pooka PUFFSOP_SETFSNOP(pops, statvfs);
221 1.1 pooka
222 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, lookup);
223 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, readdir);
224 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, getattr);
225 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, setattr);
226 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, create);
227 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, open);
228 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, mkdir);
229 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, remove);
230 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, rmdir);
231 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, read);
232 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, write);
233 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, rename);
234 1.6 pooka PUFFSOP_SET(pops, puffs9p, node, inactive);
235 1.12 pooka PUFFSOP_SET(pops, puffs9p, node, reclaim);
236 1.1 pooka #if 0
237 1.1 pooka PUFFSOP_SET(pops, puffs9p, node, mknod);
238 1.1 pooka #endif
239 1.1 pooka
240 1.16 pooka pu = puffs_init(pops, argv[0], "9p", &p9p, pflags);
241 1.15 pooka if (pu == NULL)
242 1.15 pooka err(1, "puffs_init");
243 1.15 pooka
244 1.1 pooka memset(&p9p, 0, sizeof(p9p));
245 1.1 pooka p9p.maxreq = P9P_DEFREQLEN;
246 1.1 pooka p9p.nextfid = 1;
247 1.25 ozaki p9p.protover = protover;
248 1.29 uwe p9p.server = server;
249 1.1 pooka
250 1.15 pooka /* user@ */
251 1.15 pooka if ((p = strchr(argv[0], '@')) != NULL) {
252 1.15 pooka *p = '\0';
253 1.15 pooka srvhost = p+1;
254 1.15 pooka user = argv[0];
255 1.15 pooka } else {
256 1.15 pooka struct passwd *pw;
257 1.15 pooka
258 1.15 pooka srvhost = argv[0];
259 1.15 pooka pw = getpwuid(getuid());
260 1.15 pooka if (pw == NULL)
261 1.15 pooka err(1, "getpwuid");
262 1.15 pooka user = pw->pw_name;
263 1.15 pooka }
264 1.15 pooka
265 1.15 pooka /* :/mountpath */
266 1.15 pooka if ((p = strchr(srvhost, ':')) != NULL) {
267 1.15 pooka *p = '\0';
268 1.15 pooka srvpath = p+1;
269 1.15 pooka if (*srvpath != '/')
270 1.15 pooka errx(1, "%s is not an absolute path", srvpath);
271 1.15 pooka } else {
272 1.15 pooka srvpath = "/";
273 1.15 pooka }
274 1.15 pooka
275 1.29 uwe if (p9p.server == P9P_SERVER_TCP) {
276 1.30 uwe p9p.servsock = serverconnect(srvhost, port, family);
277 1.27 ozaki } else {
278 1.28 uwe /* path to a vio9p(4) device, e.g., /dev/vio9p0 */
279 1.27 ozaki p9p.servsock = open_cdev(argv[0]);
280 1.27 ozaki }
281 1.27 ozaki
282 1.15 pooka if ((pn_root = p9p_handshake(pu, user, srvpath)) == NULL) {
283 1.27 ozaki close(p9p.servsock);
284 1.1 pooka puffs_exit(pu, 1);
285 1.1 pooka exit(1);
286 1.1 pooka }
287 1.1 pooka
288 1.17 pooka puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp, NULL,
289 1.10 pooka puffs_framev_unmountonclose);
290 1.13 pooka if (puffs_framev_addfd(pu, p9p.servsock,
291 1.13 pooka PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
292 1.9 pooka err(1, "puffs_framebuf_addfd");
293 1.1 pooka
294 1.19 pooka if (detach)
295 1.22 pooka if (puffs_daemon(pu, 1, 1) == -1)
296 1.22 pooka err(1, "puffs_daemon");
297 1.19 pooka
298 1.20 pooka if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
299 1.20 pooka err(1, "puffs_mount");
300 1.21 pooka if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
301 1.21 pooka err(1, "setblockingmode");
302 1.21 pooka
303 1.19 pooka if (puffs_mainloop(pu) == -1)
304 1.20 pooka err(1, "mainloop");
305 1.27 ozaki close(p9p.servsock);
306 1.27 ozaki puffs_exit(pu, 1);
307 1.19 pooka
308 1.19 pooka return 0;
309 1.1 pooka }
310