ninepuffs.c revision 1.4 1 /* $NetBSD: ninepuffs.c,v 1.4 2007/05/05 15:49:51 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 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 * 9puffs: access a 9P file server as a vfs via puffs
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: ninepuffs.c,v 1.4 2007/05/05 15:49:51 pooka Exp $");
35 #endif /* !lint */
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/poll.h>
40
41 #include <netinet/in.h>
42
43 #include <assert.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <puffs.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 #include "ninepuffs.h"
52
53 #define DEFPORT_9P 564
54
55 static void
56 usage(void)
57 {
58
59 errx(1, "usage: %s user server mountpoint", getprogname());
60 }
61
62 /*
63 * TCPv4 connection to 9P file server, forget IL and IPv6 for now.
64 * Return connected socket or exit with error.
65 */
66 static int
67 serverconnect(const char *addr, unsigned short port)
68 {
69 struct sockaddr_in mysin;
70 struct hostent *he;
71 int s;
72
73 he = gethostbyname2(addr, AF_INET);
74 if (he == NULL) {
75 herror("gethostbyname");
76 exit(1);
77 }
78
79 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
80 if (s == -1)
81 err(1, "socket");
82
83 memset(&mysin, 0, sizeof(struct sockaddr_in));
84 mysin.sin_family = AF_INET;
85 mysin.sin_port = htons(port);
86 memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr));
87
88 if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
89 err(1, "connect");
90
91 return s;
92 }
93
94 int
95 main(int argc, char *argv[])
96 {
97 struct puffs9p p9p;
98 struct puffs_usermount *pu;
99 struct puffs_ops *pops;
100 struct puffs_node *pn_root;
101 struct statvfs svfsb;
102 mntoptparse_t mp;
103 char *srvhost;
104 char *user;
105 unsigned short port;
106 int mntflags, pflags, ch;
107 int detach;
108
109 setprogname(argv[0]);
110
111 if (argc < 3)
112 usage();
113
114 mntflags = pflags = 0;
115 detach = 1;
116 port = DEFPORT_9P;
117
118 while ((ch = getopt(argc, argv, "o:p:s")) != -1) {
119 switch (ch) {
120 case 'o':
121 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
122 if (mp == NULL)
123 err(1, "getmntopts");
124 freemntopts(mp);
125 break;
126 case 'p':
127 port = atoi(optarg);
128 break;
129 case 's':
130 detach = 0;
131 break;
132 default:
133 usage();
134 /*NOTREACHED*/
135 }
136 }
137 argc -= optind;
138 argv += optind;
139
140 if (argc != 3)
141 usage();
142
143 user = argv[0];
144 srvhost = argv[1];
145
146 if (pflags & PUFFS_FLAG_OPDUMP)
147 detach = 0;
148 pflags |= PUFFS_KFLAG_WTCACHE;
149
150 PUFFSOP_INIT(pops);
151
152 PUFFSOP_SET(pops, puffs9p, fs, unmount);
153 PUFFSOP_SETFSNOP(pops, sync);
154 PUFFSOP_SETFSNOP(pops, statvfs);
155
156 PUFFSOP_SET(pops, puffs9p, node, lookup);
157 PUFFSOP_SET(pops, puffs9p, node, readdir);
158 PUFFSOP_SET(pops, puffs9p, node, getattr);
159 PUFFSOP_SET(pops, puffs9p, node, setattr);
160 PUFFSOP_SET(pops, puffs9p, node, create);
161 PUFFSOP_SET(pops, puffs9p, node, open);
162 PUFFSOP_SET(pops, puffs9p, node, close);
163 PUFFSOP_SET(pops, puffs9p, node, mkdir);
164 PUFFSOP_SET(pops, puffs9p, node, remove);
165 PUFFSOP_SET(pops, puffs9p, node, rmdir);
166 PUFFSOP_SET(pops, puffs9p, node, read);
167 PUFFSOP_SET(pops, puffs9p, node, write);
168 PUFFSOP_SET(pops, puffs9p, node, rename);
169 #if 0
170 PUFFSOP_SET(pops, puffs9p, node, reclaim);
171 PUFFSOP_SET(pops, puffs9p, node, mknod);
172 #endif
173
174 memset(&p9p, 0, sizeof(p9p));
175 p9p.maxreq = P9P_DEFREQLEN;
176 p9p.nextfid = 1;
177
178 p9p.servsock = serverconnect(srvhost, port);
179 pu = puffs_init(pops, "9P", &p9p, pflags);
180 if (pu == NULL)
181 err(1, "puffs_init");
182
183 if ((pn_root = p9p_handshake(pu, user)) == NULL) {
184 puffs_exit(pu, 1);
185 exit(1);
186 }
187
188 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
189 err(1, "setblockingmode");
190
191 if (puffs_domount(pu, argv[2], mntflags) == -1)
192 err(1, "puffs_domount");
193
194 puffs_zerostatvfs(&svfsb);
195 if (puffs_start(pu, pn_root, &svfsb) == -1)
196 err(1, "puffs_start");
197
198 if (detach)
199 daemon(1, 0);
200
201 puffs_framebuf_eventloop(pu, p9p.servsock,
202 p9pbuf_read, p9pbuf_write, p9pbuf_cmp, NULL, NULL);
203
204 return 0;
205 }
206