Home | History | Annotate | Line # | Download | only in mount_9p
ninepuffs.c revision 1.26
      1 /*	$NetBSD: ninepuffs.c,v 1.26 2019/05/20 08:55:31 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.26 2019/05/20 08:55:31 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 	exit(1);
     64 }
     65 
     66 /*
     67  * TCPv4 connection to 9P file server, forget IL and IPv6 for now.
     68  * Return connected socket or exit with error.
     69  */
     70 static int
     71 serverconnect(const char *addr, unsigned short port)
     72 {
     73 	struct sockaddr_in mysin;
     74 	struct hostent *he;
     75 	int s;
     76 
     77 	he = gethostbyname2(addr, AF_INET);
     78 	if (he == NULL) {
     79 		herror("gethostbyname");
     80 		exit(1);
     81 	}
     82 
     83 	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     84 	if (s == -1)
     85 		err(1, "socket");
     86 
     87 	memset(&mysin, 0, sizeof(struct sockaddr_in));
     88 	mysin.sin_family = AF_INET;
     89 	mysin.sin_port = htons(port);
     90 	memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr));
     91 
     92 	if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
     93 		err(1, "connect");
     94 
     95 	return s;
     96 }
     97 
     98 int
     99 main(int argc, char *argv[])
    100 {
    101 	struct puffs9p p9p;
    102 	struct puffs_usermount *pu;
    103 	struct puffs_ops *pops;
    104 	struct puffs_node *pn_root;
    105 	mntoptparse_t mp;
    106 	const char *user, *srvhost, *srvpath;
    107 	char *p;
    108 	unsigned short port;
    109 	int mntflags, pflags, ch;
    110 	int detach;
    111 	int protover = P9PROTO_VERSION;
    112 
    113 	setprogname(argv[0]);
    114 
    115 	if (argc < 2)
    116 		usage();
    117 
    118 	mntflags = pflags = 0;
    119 	detach = 1;
    120 	port = DEFPORT_9P;
    121 
    122 	while ((ch = getopt(argc, argv, "o:p:su")) != -1) {
    123 		switch (ch) {
    124 		case 'o':
    125 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    126 			if (mp == NULL)
    127 				err(1, "getmntopts");
    128 			freemntopts(mp);
    129 			break;
    130 		case 'p':
    131 			port = atoi(optarg);
    132 			break;
    133 		case 's':
    134 			detach = 0;
    135 			break;
    136 		case 'u':
    137 			protover = P9PROTO_VERSION_U;
    138 			break;
    139 		default:
    140 			usage();
    141 			/*NOTREACHED*/
    142 		}
    143 	}
    144 	argc -= optind;
    145 	argv += optind;
    146 
    147 	if (argc != 2)
    148 		usage();
    149 
    150 	if (pflags & PUFFS_FLAG_OPDUMP)
    151 		detach = 0;
    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 	pu = puffs_init(pops, argv[0], "9p", &p9p, pflags);
    179 	if (pu == NULL)
    180 		err(1, "puffs_init");
    181 
    182 	memset(&p9p, 0, sizeof(p9p));
    183 	p9p.maxreq = P9P_DEFREQLEN;
    184 	p9p.nextfid = 1;
    185 	p9p.protover = protover;
    186 
    187 	/* user@ */
    188 	if ((p = strchr(argv[0], '@')) != NULL) {
    189 		*p = '\0';
    190 		srvhost = p+1;
    191 		user = argv[0];
    192 	} else {
    193 		struct passwd *pw;
    194 
    195 		srvhost = argv[0];
    196 		pw = getpwuid(getuid());
    197 		if (pw == NULL)
    198 			err(1, "getpwuid");
    199 		user = pw->pw_name;
    200 	}
    201 
    202 	/* :/mountpath */
    203 	if ((p = strchr(srvhost, ':')) != NULL) {
    204 		*p = '\0';
    205 		srvpath = p+1;
    206 		if (*srvpath != '/')
    207 			errx(1, "%s is not an absolute path", srvpath);
    208 	} else {
    209 		srvpath = "/";
    210 	}
    211 
    212 	p9p.servsock = serverconnect(srvhost, port);
    213 	if ((pn_root = p9p_handshake(pu, user, srvpath)) == NULL) {
    214 		puffs_exit(pu, 1);
    215 		exit(1);
    216 	}
    217 
    218 	puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp, NULL,
    219 	    puffs_framev_unmountonclose);
    220 	if (puffs_framev_addfd(pu, p9p.servsock,
    221 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
    222 		err(1, "puffs_framebuf_addfd");
    223 
    224 	if (detach)
    225 		if (puffs_daemon(pu, 1, 1) == -1)
    226 			err(1, "puffs_daemon");
    227 
    228 	if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
    229 		err(1, "puffs_mount");
    230 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
    231 		err(1, "setblockingmode");
    232 
    233 	if (puffs_mainloop(pu) == -1)
    234 		err(1, "mainloop");
    235 
    236 	return 0;
    237 }
    238