Home | History | Annotate | Line # | Download | only in mount_9p
ninepuffs.c revision 1.14
      1 /*	$NetBSD: ninepuffs.c,v 1.14 2007/07/08 16:29:29 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.14 2007/07/08 16:29:29 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 #define DEFUSER_9P "glenda"
     55 
     56 static void
     57 usage(void)
     58 {
     59 
     60 	errx(1, "usage: %s [-o mntopts] [-p port] [-u user] [-s] server mount",
     61 	    getprogname());
     62 }
     63 
     64 /*
     65  * TCPv4 connection to 9P file server, forget IL and IPv6 for now.
     66  * Return connected socket or exit with error.
     67  */
     68 static int
     69 serverconnect(const char *addr, unsigned short port)
     70 {
     71 	struct sockaddr_in mysin;
     72 	struct hostent *he;
     73 	int s;
     74 
     75 	he = gethostbyname2(addr, AF_INET);
     76 	if (he == NULL) {
     77 		herror("gethostbyname");
     78 		exit(1);
     79 	}
     80 
     81 	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     82 	if (s == -1)
     83 		err(1, "socket");
     84 
     85 	memset(&mysin, 0, sizeof(struct sockaddr_in));
     86 	mysin.sin_family = AF_INET;
     87 	mysin.sin_port = htons(port);
     88 	memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr));
     89 
     90 	if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
     91 		err(1, "connect");
     92 
     93 	return s;
     94 }
     95 
     96 int
     97 main(int argc, char *argv[])
     98 {
     99 	struct puffs9p p9p;
    100 	struct puffs_usermount *pu;
    101 	struct puffs_ops *pops;
    102 	struct puffs_node *pn_root;
    103 	mntoptparse_t mp;
    104 	char *srvhost;
    105 	const char *user;
    106 	unsigned short port;
    107 	int mntflags, pflags, lflags, ch;
    108 	int detach;
    109 
    110 	setprogname(argv[0]);
    111 
    112 	if (argc < 2)
    113 		usage();
    114 
    115 	mntflags = pflags = lflags = 0;
    116 	detach = 1;
    117 	port = DEFPORT_9P;
    118 	user = DEFUSER_9P;
    119 
    120 	while ((ch = getopt(argc, argv, "o:p:su:")) != -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 			lflags |= PUFFSLOOP_NODAEMON;
    133 			break;
    134 		case 'u':
    135 			user = optarg;
    136 			break;
    137 		default:
    138 			usage();
    139 			/*NOTREACHED*/
    140 		}
    141 	}
    142 	argc -= optind;
    143 	argv += optind;
    144 
    145 	if (argc != 2)
    146 		usage();
    147 
    148 	srvhost = argv[0];
    149 
    150 	if (pflags & PUFFS_FLAG_OPDUMP)
    151 		lflags |= PUFFSLOOP_NODAEMON;
    152 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
    153 
    154 	PUFFSOP_INIT(pops);
    155 
    156 	PUFFSOP_SET(pops, puffs9p, fs, unmount);
    157 	PUFFSOP_SETFSNOP(pops, sync);
    158 	PUFFSOP_SETFSNOP(pops, statvfs);
    159 
    160 	PUFFSOP_SET(pops, puffs9p, node, lookup);
    161 	PUFFSOP_SET(pops, puffs9p, node, readdir);
    162 	PUFFSOP_SET(pops, puffs9p, node, getattr);
    163 	PUFFSOP_SET(pops, puffs9p, node, setattr);
    164 	PUFFSOP_SET(pops, puffs9p, node, create);
    165 	PUFFSOP_SET(pops, puffs9p, node, open);
    166 	PUFFSOP_SET(pops, puffs9p, node, mkdir);
    167 	PUFFSOP_SET(pops, puffs9p, node, remove);
    168 	PUFFSOP_SET(pops, puffs9p, node, rmdir);
    169 	PUFFSOP_SET(pops, puffs9p, node, read);
    170 	PUFFSOP_SET(pops, puffs9p, node, write);
    171 	PUFFSOP_SET(pops, puffs9p, node, rename);
    172 	PUFFSOP_SET(pops, puffs9p, node, inactive);
    173 	PUFFSOP_SET(pops, puffs9p, node, reclaim);
    174 #if 0
    175 	PUFFSOP_SET(pops, puffs9p, node, mknod);
    176 #endif
    177 
    178 	memset(&p9p, 0, sizeof(p9p));
    179 	p9p.maxreq = P9P_DEFREQLEN;
    180 	p9p.nextfid = 1;
    181 
    182 	p9p.servsock = serverconnect(srvhost, port);
    183 	pu = puffs_init(pops, "9P", &p9p, pflags);
    184 	if (pu == NULL)
    185 		err(1, "puffs_init");
    186 
    187 	if ((pn_root = p9p_handshake(pu, user)) == NULL) {
    188 		puffs_exit(pu, 1);
    189 		exit(1);
    190 	}
    191 
    192 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    193 		err(1, "setblockingmode");
    194 
    195 	puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp,
    196 	    puffs_framev_unmountonclose);
    197 	if (puffs_framev_addfd(pu, p9p.servsock,
    198 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
    199 		err(1, "puffs_framebuf_addfd");
    200 
    201 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    202 		err(1, "puffs_mount");
    203 
    204 	return puffs_mainloop(pu, lflags);
    205 }
    206