Home | History | Annotate | Line # | Download | only in mount_9p
ninepuffs.c revision 1.16
      1 /*	$NetBSD: ninepuffs.c,v 1.16 2007/07/17 11:34:53 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.16 2007/07/17 11:34:53 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 <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 	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 	const char *user, *srvhost, *srvpath;
    105 	char *p;
    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 
    119 	while ((ch = getopt(argc, argv, "o:p:s")) != -1) {
    120 		switch (ch) {
    121 		case 'o':
    122 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    123 			if (mp == NULL)
    124 				err(1, "getmntopts");
    125 			freemntopts(mp);
    126 			break;
    127 		case 'p':
    128 			port = atoi(optarg);
    129 			break;
    130 		case 's':
    131 			lflags |= PUFFSLOOP_NODAEMON;
    132 			break;
    133 		default:
    134 			usage();
    135 			/*NOTREACHED*/
    136 		}
    137 	}
    138 	argc -= optind;
    139 	argv += optind;
    140 
    141 	if (argc != 2)
    142 		usage();
    143 
    144 	if (pflags & PUFFS_FLAG_OPDUMP)
    145 		lflags |= PUFFSLOOP_NODAEMON;
    146 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
    147 
    148 	PUFFSOP_INIT(pops);
    149 
    150 	PUFFSOP_SET(pops, puffs9p, fs, unmount);
    151 	PUFFSOP_SETFSNOP(pops, sync);
    152 	PUFFSOP_SETFSNOP(pops, statvfs);
    153 
    154 	PUFFSOP_SET(pops, puffs9p, node, lookup);
    155 	PUFFSOP_SET(pops, puffs9p, node, readdir);
    156 	PUFFSOP_SET(pops, puffs9p, node, getattr);
    157 	PUFFSOP_SET(pops, puffs9p, node, setattr);
    158 	PUFFSOP_SET(pops, puffs9p, node, create);
    159 	PUFFSOP_SET(pops, puffs9p, node, open);
    160 	PUFFSOP_SET(pops, puffs9p, node, mkdir);
    161 	PUFFSOP_SET(pops, puffs9p, node, remove);
    162 	PUFFSOP_SET(pops, puffs9p, node, rmdir);
    163 	PUFFSOP_SET(pops, puffs9p, node, read);
    164 	PUFFSOP_SET(pops, puffs9p, node, write);
    165 	PUFFSOP_SET(pops, puffs9p, node, rename);
    166 	PUFFSOP_SET(pops, puffs9p, node, inactive);
    167 	PUFFSOP_SET(pops, puffs9p, node, reclaim);
    168 #if 0
    169 	PUFFSOP_SET(pops, puffs9p, node, mknod);
    170 #endif
    171 
    172 	pu = puffs_init(pops, argv[0], "9p", &p9p, pflags);
    173 	if (pu == NULL)
    174 		err(1, "puffs_init");
    175 
    176 	memset(&p9p, 0, sizeof(p9p));
    177 	p9p.maxreq = P9P_DEFREQLEN;
    178 	p9p.nextfid = 1;
    179 
    180 	/* user@ */
    181 	if ((p = strchr(argv[0], '@')) != NULL) {
    182 		*p = '\0';
    183 		srvhost = p+1;
    184 		user = argv[0];
    185 	} else {
    186 		struct passwd *pw;
    187 
    188 		srvhost = argv[0];
    189 		pw = getpwuid(getuid());
    190 		if (pw == NULL)
    191 			err(1, "getpwuid");
    192 		user = pw->pw_name;
    193 	}
    194 
    195 	/* :/mountpath */
    196 	if ((p = strchr(srvhost, ':')) != NULL) {
    197 		*p = '\0';
    198 		srvpath = p+1;
    199 		if (*srvpath != '/')
    200 			errx(1, "%s is not an absolute path", srvpath);
    201 	} else {
    202 		srvpath = "/";
    203 	}
    204 
    205 	p9p.servsock = serverconnect(srvhost, port);
    206 	if ((pn_root = p9p_handshake(pu, user, srvpath)) == NULL) {
    207 		puffs_exit(pu, 1);
    208 		exit(1);
    209 	}
    210 
    211 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    212 		err(1, "setblockingmode");
    213 
    214 	puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp,
    215 	    puffs_framev_unmountonclose);
    216 	if (puffs_framev_addfd(pu, p9p.servsock,
    217 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
    218 		err(1, "puffs_framebuf_addfd");
    219 
    220 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    221 		err(1, "puffs_mount");
    222 
    223 	return puffs_mainloop(pu, lflags);
    224 }
    225