Home | History | Annotate | Line # | Download | only in mount_9p
ninepuffs.c revision 1.27
      1 /*	$NetBSD: ninepuffs.c,v 1.27 2019/10/28 02:59:25 ozaki-r 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.27 2019/10/28 02:59:25 ozaki-r 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 #include "nineproto.h"
     54 
     55 #define DEFPORT_9P 564
     56 
     57 __dead static void
     58 usage(void)
     59 {
     60 
     61 	fprintf(stderr, "usage: %s [-su] [-o mntopts] [-p port] "
     62 	    "[user@]server[:path] mountpoint\n", getprogname());
     63 	fprintf(stderr, "       %s -c [-su] [-o mntopts] devfile mountpoint\n",
     64 	    getprogname());
     65 	exit(1);
     66 }
     67 
     68 /*
     69  * TCPv4 connection to 9P file server, forget IL and IPv6 for now.
     70  * Return connected socket or exit with error.
     71  */
     72 static int
     73 serverconnect(const char *addr, unsigned short port)
     74 {
     75 	struct sockaddr_in mysin;
     76 	struct hostent *he;
     77 	int s, ret, opt;
     78 
     79 	he = gethostbyname2(addr, AF_INET);
     80 	if (he == NULL) {
     81 		herror("gethostbyname");
     82 		exit(1);
     83 	}
     84 
     85 	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     86 	if (s == -1)
     87 		err(1, "socket");
     88 
     89 	opt = 1;
     90 	ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
     91 	if (ret == -1)
     92 		err(1, "setsockopt(SO_NOSIGPIPE)");
     93 
     94 	memset(&mysin, 0, sizeof(struct sockaddr_in));
     95 	mysin.sin_family = AF_INET;
     96 	mysin.sin_port = htons(port);
     97 	memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr));
     98 
     99 	if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
    100 		err(1, "connect");
    101 
    102 	return s;
    103 }
    104 
    105 static int
    106 open_cdev(const char *path)
    107 {
    108 	int s;
    109 
    110 	s = open(path, O_RDWR, 0);
    111 	if (s == -1)
    112 		err(1, "%s", path);
    113 	return s;
    114 }
    115 
    116 int
    117 main(int argc, char *argv[])
    118 {
    119 	struct puffs9p p9p;
    120 	struct puffs_usermount *pu;
    121 	struct puffs_ops *pops;
    122 	struct puffs_node *pn_root;
    123 	mntoptparse_t mp;
    124 	const char *user, *srvhost, *srvpath;
    125 	char *p;
    126 	unsigned short port;
    127 	int mntflags, pflags, ch;
    128 	int detach;
    129 	int protover;
    130 	int server;
    131 
    132 	setprogname(argv[0]);
    133 
    134 	if (argc < 2)
    135 		usage();
    136 
    137 	mntflags = pflags = 0;
    138 	detach = 1;
    139 	port = DEFPORT_9P;
    140 	protover = P9PROTO_VERSION;
    141 	server = P9P_SERVER_TCP;
    142 
    143 	while ((ch = getopt(argc, argv, "co:p:su")) != -1) {
    144 		switch (ch) {
    145 		case 'c':
    146 			server = P9P_SERVER_CDEV;
    147 			break;
    148 		case 'o':
    149 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    150 			if (mp == NULL)
    151 				err(1, "getmntopts");
    152 			freemntopts(mp);
    153 			break;
    154 		case 'p':
    155 			port = atoi(optarg);
    156 			break;
    157 		case 's':
    158 			detach = 0;
    159 			break;
    160 		case 'u':
    161 			protover = P9PROTO_VERSION_U;
    162 			break;
    163 		default:
    164 			usage();
    165 			/*NOTREACHED*/
    166 		}
    167 	}
    168 	argc -= optind;
    169 	argv += optind;
    170 
    171 	if (argc != 2)
    172 		usage();
    173 
    174 	if (pflags & PUFFS_FLAG_OPDUMP)
    175 		detach = 0;
    176 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
    177 
    178 	PUFFSOP_INIT(pops);
    179 
    180 	PUFFSOP_SET(pops, puffs9p, fs, unmount);
    181 	PUFFSOP_SETFSNOP(pops, sync);
    182 	PUFFSOP_SETFSNOP(pops, statvfs);
    183 
    184 	PUFFSOP_SET(pops, puffs9p, node, lookup);
    185 	PUFFSOP_SET(pops, puffs9p, node, readdir);
    186 	PUFFSOP_SET(pops, puffs9p, node, getattr);
    187 	PUFFSOP_SET(pops, puffs9p, node, setattr);
    188 	PUFFSOP_SET(pops, puffs9p, node, create);
    189 	PUFFSOP_SET(pops, puffs9p, node, open);
    190 	PUFFSOP_SET(pops, puffs9p, node, mkdir);
    191 	PUFFSOP_SET(pops, puffs9p, node, remove);
    192 	PUFFSOP_SET(pops, puffs9p, node, rmdir);
    193 	PUFFSOP_SET(pops, puffs9p, node, read);
    194 	PUFFSOP_SET(pops, puffs9p, node, write);
    195 	PUFFSOP_SET(pops, puffs9p, node, rename);
    196 	PUFFSOP_SET(pops, puffs9p, node, inactive);
    197 	PUFFSOP_SET(pops, puffs9p, node, reclaim);
    198 #if 0
    199 	PUFFSOP_SET(pops, puffs9p, node, mknod);
    200 #endif
    201 
    202 	pu = puffs_init(pops, argv[0], "9p", &p9p, pflags);
    203 	if (pu == NULL)
    204 		err(1, "puffs_init");
    205 
    206 	memset(&p9p, 0, sizeof(p9p));
    207 	p9p.maxreq = P9P_DEFREQLEN;
    208 	p9p.nextfid = 1;
    209 	p9p.protover = protover;
    210 
    211 	/* user@ */
    212 	if ((p = strchr(argv[0], '@')) != NULL) {
    213 		*p = '\0';
    214 		srvhost = p+1;
    215 		user = argv[0];
    216 	} else {
    217 		struct passwd *pw;
    218 
    219 		srvhost = argv[0];
    220 		pw = getpwuid(getuid());
    221 		if (pw == NULL)
    222 			err(1, "getpwuid");
    223 		user = pw->pw_name;
    224 	}
    225 
    226 	/* :/mountpath */
    227 	if ((p = strchr(srvhost, ':')) != NULL) {
    228 		*p = '\0';
    229 		srvpath = p+1;
    230 		if (*srvpath != '/')
    231 			errx(1, "%s is not an absolute path", srvpath);
    232 	} else {
    233 		srvpath = "/";
    234 	}
    235 
    236 	if (server == P9P_SERVER_TCP) {
    237 		p9p.servsock = serverconnect(srvhost, port);
    238 	} else {
    239 		/* path to a viop9fs chardev file, e.g., /dev/viop9fs0 */
    240 		p9p.servsock = open_cdev(argv[0]);
    241 	}
    242 
    243 	if ((pn_root = p9p_handshake(pu, user, srvpath)) == NULL) {
    244 		close(p9p.servsock);
    245 		puffs_exit(pu, 1);
    246 		exit(1);
    247 	}
    248 
    249 	puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp, NULL,
    250 	    puffs_framev_unmountonclose);
    251 	if (puffs_framev_addfd(pu, p9p.servsock,
    252 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
    253 		err(1, "puffs_framebuf_addfd");
    254 
    255 	if (detach)
    256 		if (puffs_daemon(pu, 1, 1) == -1)
    257 			err(1, "puffs_daemon");
    258 
    259 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    260 		err(1, "puffs_mount");
    261 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    262 		err(1, "setblockingmode");
    263 
    264 	if (puffs_mainloop(pu) == -1)
    265 		err(1, "mainloop");
    266 	close(p9p.servsock);
    267 	puffs_exit(pu, 1);
    268 
    269 	return 0;
    270 }
    271