Home | History | Annotate | Line # | Download | only in mount_9p
ninepuffs.c revision 1.13
      1 /*	$NetBSD: ninepuffs.c,v 1.13 2007/07/07 21:14:28 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.13 2007/07/07 21:14:28 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 	mntoptparse_t mp;
    102 	char *srvhost;
    103 	char *user;
    104 	unsigned short port;
    105 	int mntflags, pflags, lflags, ch;
    106 	int detach;
    107 
    108 	setprogname(argv[0]);
    109 
    110 	if (argc < 3)
    111 		usage();
    112 
    113 	mntflags = pflags = lflags = 0;
    114 	detach = 1;
    115 	port = DEFPORT_9P;
    116 
    117 	while ((ch = getopt(argc, argv, "o:p:s")) != -1) {
    118 		switch (ch) {
    119 		case 'o':
    120 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    121 			if (mp == NULL)
    122 				err(1, "getmntopts");
    123 			freemntopts(mp);
    124 			break;
    125 		case 'p':
    126 			port = atoi(optarg);
    127 			break;
    128 		case 's':
    129 			lflags |= PUFFSLOOP_NODAEMON;
    130 			break;
    131 		default:
    132 			usage();
    133 			/*NOTREACHED*/
    134 		}
    135 	}
    136 	argc -= optind;
    137 	argv += optind;
    138 
    139 	if (argc != 3)
    140 		usage();
    141 
    142 	user = argv[0];
    143 	srvhost = argv[1];
    144 
    145 	if (pflags & PUFFS_FLAG_OPDUMP)
    146 		lflags |= PUFFSLOOP_NODAEMON;
    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 	memset(&p9p, 0, sizeof(p9p));
    174 	p9p.maxreq = P9P_DEFREQLEN;
    175 	p9p.nextfid = 1;
    176 
    177 	p9p.servsock = serverconnect(srvhost, port);
    178 	pu = puffs_init(pops, "9P", &p9p, pflags);
    179 	if (pu == NULL)
    180 		err(1, "puffs_init");
    181 
    182 	if ((pn_root = p9p_handshake(pu, user)) == NULL) {
    183 		puffs_exit(pu, 1);
    184 		exit(1);
    185 	}
    186 
    187 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    188 		err(1, "setblockingmode");
    189 
    190 	puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp,
    191 	    puffs_framev_unmountonclose);
    192 	if (puffs_framev_addfd(pu, p9p.servsock,
    193 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
    194 		err(1, "puffs_framebuf_addfd");
    195 
    196 	if (puffs_mount(pu, argv[2], mntflags, pn_root) == -1)
    197 		err(1, "puffs_mount");
    198 
    199 	return puffs_mainloop(pu, lflags);
    200 }
    201