1 /* $NetBSD: ninepuffs.c,v 1.23 2007/12/02 21:03:39 wiz 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.23 2007/12/02 21:03:39 wiz 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 <pwd.h> 47 #include <puffs.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 52 #include "ninepuffs.h" 53 54 #define DEFPORT_9P 564 55 56 static void 57 usage(void) 58 { 59 60 fprintf(stderr, "usage: %s [-s] [-o mntopts] [-p port] " 61 "[user@]server[:path] mountpoint\n", getprogname()); 62 exit(1); 63 } 64 65 /* 66 * TCPv4 connection to 9P file server, forget IL and IPv6 for now. 67 * Return connected socket or exit with error. 68 */ 69 static int 70 serverconnect(const char *addr, unsigned short port) 71 { 72 struct sockaddr_in mysin; 73 struct hostent *he; 74 int s; 75 76 he = gethostbyname2(addr, AF_INET); 77 if (he == NULL) { 78 herror("gethostbyname"); 79 exit(1); 80 } 81 82 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 83 if (s == -1) 84 err(1, "socket"); 85 86 memset(&mysin, 0, sizeof(struct sockaddr_in)); 87 mysin.sin_family = AF_INET; 88 mysin.sin_port = htons(port); 89 memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr)); 90 91 if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1) 92 err(1, "connect"); 93 94 return s; 95 } 96 97 int 98 main(int argc, char *argv[]) 99 { 100 struct puffs9p p9p; 101 struct puffs_usermount *pu; 102 struct puffs_ops *pops; 103 struct puffs_node *pn_root; 104 mntoptparse_t mp; 105 const char *user, *srvhost, *srvpath; 106 char *p; 107 unsigned short port; 108 int mntflags, pflags, ch; 109 int detach; 110 111 setprogname(argv[0]); 112 113 if (argc < 2) 114 usage(); 115 116 mntflags = pflags = 0; 117 detach = 1; 118 port = DEFPORT_9P; 119 120 while ((ch = getopt(argc, argv, "o:p:s")) != -1) { 121 switch (ch) { 122 case 'o': 123 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags); 124 if (mp == NULL) 125 err(1, "getmntopts"); 126 freemntopts(mp); 127 break; 128 case 'p': 129 port = atoi(optarg); 130 break; 131 case 's': 132 detach = 0; 133 break; 134 default: 135 usage(); 136 /*NOTREACHED*/ 137 } 138 } 139 argc -= optind; 140 argv += optind; 141 142 if (argc != 2) 143 usage(); 144 145 if (pflags & PUFFS_FLAG_OPDUMP) 146 detach = 0; 147 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND; 148 149 PUFFSOP_INIT(pops); 150 151 PUFFSOP_SET(pops, puffs9p, fs, unmount); 152 PUFFSOP_SETFSNOP(pops, sync); 153 PUFFSOP_SETFSNOP(pops, statvfs); 154 155 PUFFSOP_SET(pops, puffs9p, node, lookup); 156 PUFFSOP_SET(pops, puffs9p, node, readdir); 157 PUFFSOP_SET(pops, puffs9p, node, getattr); 158 PUFFSOP_SET(pops, puffs9p, node, setattr); 159 PUFFSOP_SET(pops, puffs9p, node, create); 160 PUFFSOP_SET(pops, puffs9p, node, open); 161 PUFFSOP_SET(pops, puffs9p, node, mkdir); 162 PUFFSOP_SET(pops, puffs9p, node, remove); 163 PUFFSOP_SET(pops, puffs9p, node, rmdir); 164 PUFFSOP_SET(pops, puffs9p, node, read); 165 PUFFSOP_SET(pops, puffs9p, node, write); 166 PUFFSOP_SET(pops, puffs9p, node, rename); 167 PUFFSOP_SET(pops, puffs9p, node, inactive); 168 PUFFSOP_SET(pops, puffs9p, node, reclaim); 169 #if 0 170 PUFFSOP_SET(pops, puffs9p, node, mknod); 171 #endif 172 173 pu = puffs_init(pops, argv[0], "9p", &p9p, pflags); 174 if (pu == NULL) 175 err(1, "puffs_init"); 176 177 memset(&p9p, 0, sizeof(p9p)); 178 p9p.maxreq = P9P_DEFREQLEN; 179 p9p.nextfid = 1; 180 181 /* user@ */ 182 if ((p = strchr(argv[0], '@')) != NULL) { 183 *p = '\0'; 184 srvhost = p+1; 185 user = argv[0]; 186 } else { 187 struct passwd *pw; 188 189 srvhost = argv[0]; 190 pw = getpwuid(getuid()); 191 if (pw == NULL) 192 err(1, "getpwuid"); 193 user = pw->pw_name; 194 } 195 196 /* :/mountpath */ 197 if ((p = strchr(srvhost, ':')) != NULL) { 198 *p = '\0'; 199 srvpath = p+1; 200 if (*srvpath != '/') 201 errx(1, "%s is not an absolute path", srvpath); 202 } else { 203 srvpath = "/"; 204 } 205 206 p9p.servsock = serverconnect(srvhost, port); 207 if ((pn_root = p9p_handshake(pu, user, srvpath)) == NULL) { 208 puffs_exit(pu, 1); 209 exit(1); 210 } 211 212 puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp, NULL, 213 puffs_framev_unmountonclose); 214 if (puffs_framev_addfd(pu, p9p.servsock, 215 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) 216 err(1, "puffs_framebuf_addfd"); 217 218 if (detach) 219 if (puffs_daemon(pu, 1, 1) == -1) 220 err(1, "puffs_daemon"); 221 222 if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1) 223 err(1, "puffs_mount"); 224 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) 225 err(1, "setblockingmode"); 226 227 if (puffs_mainloop(pu) == -1) 228 err(1, "mainloop"); 229 230 return 0; 231 } 232