Home | History | Annotate | Line # | Download | only in perfused
perfused.c revision 1.13
      1 /*  $NetBSD: perfused.c,v 1.13 2011/05/30 14:50:08 manu Exp $ */
      2 
      3 /*-
      4  *  Copyright (c) 2010 Emmanuel Dreyfus. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  *  POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <stdio.h>
     29 #include <unistd.h>
     30 #include <stdlib.h>
     31 #include <fcntl.h>
     32 #include <syslog.h>
     33 #include <ctype.h>
     34 #include <paths.h>
     35 #include <stdarg.h>
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <string.h>
     39 #include <sysexits.h>
     40 #include <signal.h>
     41 #include <puffs.h>
     42 #include <sys/wait.h>
     43 #include <sys/param.h>
     44 #include <sys/queue.h>
     45 #include <sys/uio.h>
     46 #include <sys/socket.h>
     47 #include <sys/un.h>
     48 #include <machine/vmparam.h>
     49 
     50 #include "../../lib/libperfuse/perfuse_if.h"
     51 #include "perfused.h"
     52 
     53 static int access_mount(const char *, uid_t, int);
     54 static void new_mount(int, int);
     55 static int parse_debug(char *);
     56 static void siginfo_handler(int);
     57 static int parse_options(int, char **);
     58 static void get_mount_info(int, struct perfuse_mount_info *, int);
     59 int main(int, char **);
     60 
     61 /*
     62  * Flags for new_mount()
     63  */
     64 #define  PMNT_DEVFUSE	0x0	/* We use /dev/fuse */
     65 #define  PMNT_SOCKPAIR	0x1	/* We use socketpair */
     66 #define  PMNT_DGRAM	0x2	/* We use SOCK_DGRAM sockets */
     67 
     68 
     69 static int
     70 access_mount(mnt, uid, ro)
     71 	const char *mnt;
     72 	uid_t uid;
     73 	int ro;
     74 {
     75 	struct stat st;
     76 	mode_t mode;
     77 
     78 	if (uid == 0)
     79 		return 0;
     80 
     81 	if (stat(mnt, &st) == -1)
     82 		return -1;
     83 
     84 	if (st.st_uid != uid)
     85 		return -1;
     86 
     87 	mode = S_IRUSR;
     88 	if (!ro)
     89 		mode |= S_IWUSR;
     90 
     91 	if ((st.st_mode & mode) == mode)
     92 		return 0;
     93 
     94 	return -1;
     95 }
     96 
     97 static void
     98 get_mount_info(fd, pmi, sock_type)
     99 	int fd;
    100 	struct perfuse_mount_info *pmi;
    101 	int sock_type;
    102 {
    103 	struct perfuse_mount_out *pmo;
    104 	struct sockcred cred;
    105 	int opt;
    106 	char *cp;
    107 	char *source = NULL;
    108 	char *target = NULL;
    109 	char *filesystemtype = NULL;
    110 	long mountflags = 0;
    111 	void *data = NULL;
    112 	char *sock = NULL;
    113 
    114 	pmo = (struct perfuse_mount_out *)
    115 		perfuse_recv_early(fd, &cred, sizeof(cred));
    116 
    117 	if (pmo == NULL) {
    118 		if (shutdown(fd, SHUT_RDWR) != 0)
    119 			DERR(EX_OSERR, "shutdown failed");
    120 		exit(EX_PROTOCOL);
    121 	}
    122 
    123 	/*
    124 	 * We do not need peer creds beyond this point
    125 	 */
    126 	opt = 0;
    127 	if (setsockopt(fd, 0, LOCAL_CREDS, &opt, sizeof(opt)) != 0)
    128 		DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
    129 
    130 #ifdef PERFUSE_DEBUG
    131 	if (perfuse_diagflags & PDF_MISC)
    132 		DPRINTF("perfuse lengths: source = %"PRId32", "
    133 			"target = %"PRId32", filesystemtype = %"PRId32", "
    134 			"data = %"PRId32", sock = %"PRId32"\n",
    135 			pmo->pmo_source_len, pmo->pmo_target_len,
    136 			pmo->pmo_filesystemtype_len, pmo->pmo_data_len,
    137 			pmo->pmo_sock_len);
    138 #endif
    139 	cp = (char *)(void *)(pmo + 1);
    140 
    141 	if (pmo->pmo_source_len != 0) {
    142 		source = cp;
    143 		cp += pmo->pmo_source_len;
    144 	}
    145 
    146 	if (pmo->pmo_target_len != 0) {
    147 		target = cp;
    148 		cp += pmo->pmo_target_len;
    149 	}
    150 
    151 	if (pmo->pmo_filesystemtype_len != 0) {
    152 		filesystemtype = cp;
    153 		cp += pmo->pmo_filesystemtype_len;
    154 	}
    155 
    156 	mountflags = pmo->pmo_mountflags;
    157 
    158 	if (pmo->pmo_data_len != 0) {
    159 		data = cp;
    160 		cp += pmo->pmo_data_len;
    161 	}
    162 
    163 	if (pmo->pmo_sock_len != 0) {
    164 		sock = cp;
    165 		cp += pmo->pmo_sock_len;
    166 	}
    167 
    168 #ifdef PERFUSE_DEBUG
    169 	if (perfuse_diagflags & PDF_MISC)
    170 		DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\", \"%s\")\n",
    171 		__func__, source, target, filesystemtype,
    172 		mountflags, (const char *)data, sock);
    173 #endif
    174 	pmi->pmi_source = source;
    175 	pmi->pmi_target = target;
    176 	pmi->pmi_filesystemtype = filesystemtype;
    177 	pmi->pmi_mountflags = (int)mountflags;
    178 	pmi->pmi_data = data;
    179 
    180 	pmi->pmi_uid = cred.sc_euid;
    181 
    182 	/*
    183 	 * Connect to the remote socket if provided ans using SOCK_DGRAM
    184 	 */
    185 	if ((sock_type == SOCK_DGRAM) && sock) {
    186 		const struct sockaddr *sa;
    187 		struct sockaddr_un sun;
    188 
    189 		sa = (const struct sockaddr *)(void *)&sun;
    190 		sun.sun_len = sizeof(sun);
    191 		sun.sun_family = AF_LOCAL;
    192 		strcpy(sun.sun_path, sock);
    193 
    194 		if (connect(fd, sa, sun.sun_len) != 0)
    195 			DERR(EX_OSERR, "connect \"%s\" failed", sun.sun_path);
    196 	}
    197 
    198 	return;
    199 }
    200 
    201 static void
    202 new_mount(fd, pmnt_flags)
    203 	int fd;
    204 	int pmnt_flags;
    205 {
    206 	struct puffs_usermount *pu;
    207 	struct perfuse_mount_info pmi;
    208 	struct perfuse_callbacks pc;
    209 	int ro_flag;
    210 	pid_t pid;
    211 	int flags;
    212 	int sock_type;
    213 
    214 	pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
    215 	switch(pid) {
    216 	case -1:
    217 		DERR(EX_OSERR, "cannot fork");
    218 		break;
    219 	case 0:
    220 		break;
    221 	default:
    222 		return;
    223 		/* NOTREACHED */
    224 		break;
    225 	}
    226 
    227 	/*
    228 	 * Mount information (source, target, mount flags...)
    229 	 */
    230 	sock_type = pmnt_flags & PMNT_DGRAM ? SOCK_DGRAM : SOCK_SEQPACKET;
    231 	get_mount_info(fd, &pmi, sock_type);
    232 
    233 	/*
    234 	 * Check that peer owns mountpoint and read (and write) on it?
    235 	 */
    236 	ro_flag = pmi.pmi_mountflags & MNT_RDONLY;
    237 	if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0)
    238 		DERRX(EX_NOPERM, "insuficient privileges to mount on %s",
    239 		      pmi.pmi_target);
    240 
    241 
    242 	/*
    243 	 * Initialize libperfuse, which will initialize libpuffs
    244 	 */
    245 	pc.pc_new_msg = perfuse_new_pb;
    246 	pc.pc_xchg_msg = perfuse_xchg_pb;
    247 	pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy;
    248 	pc.pc_get_inhdr = perfuse_get_inhdr;
    249 	pc.pc_get_inpayload = perfuse_get_inpayload;
    250 	pc.pc_get_outhdr = perfuse_get_outhdr;
    251 	pc.pc_get_outpayload = perfuse_get_outpayload;
    252 	pc.pc_umount = perfuse_umount;
    253 
    254 	pu = perfuse_init(&pc, &pmi);
    255 
    256 	puffs_framev_init(pu, perfuse_readframe, perfuse_writeframe,
    257 			  perfuse_cmpframe, perfuse_gotframe, perfuse_fdnotify);
    258 
    259 	if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
    260 		DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
    261 
    262 	perfuse_setspecific(pu, (void *)(long)fd);
    263 
    264 	setproctitle("perfused %s", pmi.pmi_target);
    265 	(void)kill(getpid(), SIGINFO);		/* This is for -s option */
    266 
    267 	perfuse_fs_init(pu);
    268 
    269 	/*
    270 	 * Non blocking I/O on /dev/fuse
    271 	 * This must be done after perfuse_fs_init
    272 	 */
    273 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
    274 		DERR(EX_OSERR, "fcntl failed");
    275 	if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0)
    276 		DERR(EX_OSERR, "fcntl failed");
    277 
    278 	/*
    279 	 * Hand over control to puffs main loop.
    280 	 */
    281 	if (perfuse_mainloop(pu) != 0)
    282 		DERRX(EX_SOFTWARE, "perfuse_mainloop exit");
    283 
    284 	/*
    285 	 * Normal return after unmount
    286 	 */
    287 	return;
    288 }
    289 
    290 static int
    291 parse_debug(optstr)
    292 	char *optstr;
    293 {
    294 	int retval = PDF_SYSLOG;
    295 	char *opt;
    296 	char *lastp;
    297 
    298 	for (opt = strtok_r(optstr, ",", &lastp);
    299 	     opt;
    300 	     opt = strtok_r(NULL, ",", &lastp)) {
    301 		if (strcmp(opt, "fuse") == 0)
    302 			retval |= PDF_FUSE;
    303 		else if (strcmp(opt, "puffs") == 0)
    304 			retval |= PDF_PUFFS;
    305 		else if (strcmp(opt, "dump") == 0)
    306 			retval |= PDF_DUMP;
    307 		else if (strcmp(opt, "fh") == 0)
    308 			retval |= PDF_FH;
    309 		else if (strcmp(opt, "readdir") == 0)
    310 			retval |= PDF_READDIR;
    311 		else if (strcmp(opt, "reclaim") == 0)
    312 			retval |= PDF_RECLAIM;
    313 		else if (strcmp(opt, "requeue") == 0)
    314 			retval |= PDF_REQUEUE;
    315 		else if (strcmp(opt, "sync") == 0)
    316 			retval |= PDF_SYNC;
    317 		else if (strcmp(opt, "misc") == 0)
    318 			retval |= PDF_MISC;
    319 		else if (strcmp(opt, "filename") == 0)
    320 			retval |= PDF_FILENAME;
    321 		else
    322 			DWARNX("unknown debug flag \"%s\"", opt);
    323 	}
    324 
    325 	return retval;
    326 }
    327 
    328 /* ARGSUSED0 */
    329 static void
    330 siginfo_handler(sig)
    331 	int sig;
    332 {
    333 	static int old_flags = 0;
    334 	int swap;
    335 
    336 	swap = perfuse_diagflags;
    337 	perfuse_diagflags = old_flags;
    338 	old_flags = swap;
    339 
    340 	DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis");
    341 
    342 	return;
    343 }
    344 
    345 static int
    346 parse_options(argc, argv)
    347 	int argc;
    348 	char **argv;
    349 {
    350 	int ch;
    351 	int foreground = 0;
    352 	int retval = -1;
    353 
    354 	perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
    355 
    356 	while ((ch = getopt(argc, argv, "d:fsi:")) != -1) {
    357 		switch (ch) {
    358 		case 'd':
    359 			perfuse_diagflags |= parse_debug(optarg);
    360 			break;
    361 		case 's':
    362 			if (signal(SIGINFO, siginfo_handler) != 0)
    363 				DERR(EX_OSERR, "signal failed");
    364 			break;
    365 		case 'f':
    366 			foreground = 1;
    367 			perfuse_diagflags |= PDF_MISC;
    368 			break;
    369 		case 'i':
    370 			retval = atoi(optarg);
    371 			foreground = 1;
    372 			break;
    373 		default:
    374 			DERRX(EX_USAGE, "%s [-fs] [-d classes] [-i fd]", argv[0]);
    375 			break;
    376 		}
    377 	}
    378 
    379 	if (!foreground)
    380 		perfuse_diagflags &= ~PDF_FOREGROUND;
    381 
    382 	return retval;
    383 }
    384 
    385 int
    386 main(argc, argv)
    387 	int argc;
    388 	char **argv;
    389 {
    390 	int s;
    391 	int sock_type;
    392 	socklen_t len;
    393 
    394 	s = parse_options(argc, argv);
    395 
    396 	if (perfuse_diagflags & PDF_SYSLOG)
    397 		openlog("perfused", 0, LOG_DAEMON);
    398 
    399 	if (!(perfuse_diagflags & PDF_FOREGROUND))
    400 		if (daemon(0, 0) != 0)
    401 			DERR(EX_OSERR, "daemon failed");
    402 
    403 	if (s != -1) {
    404 		new_mount(s, PMNT_SOCKPAIR);
    405 		exit(0);
    406 	}
    407 
    408 	s = perfuse_open_sock();
    409 
    410 #ifdef PERFUSE_DEBUG
    411 	if (perfuse_diagflags & PDF_MISC)
    412 		DPRINTF("perfused ready\n");
    413 #endif
    414 	len = sizeof(sock_type);
    415 	if (getsockopt(s, SOL_SOCKET, SO_TYPE, &sock_type, &len) != 0)
    416 		DERR(EX_OSERR, "getsockopt SO_TYPE failed");
    417 
    418 	switch(sock_type) {
    419 	case SOCK_DGRAM:
    420 		new_mount(s, PMNT_DEVFUSE|PMNT_DGRAM);
    421 		exit(0);
    422 		break;
    423 	case SOCK_SEQPACKET:
    424 		if (listen(s, 0) != 0)
    425 			DERR(EX_OSERR, "listen failed");
    426 
    427 		do {
    428 			int fd;
    429 			struct sockaddr_un sun;
    430 			struct sockaddr *sa;
    431 
    432 			len = sizeof(sun);
    433 			sa = (struct sockaddr *)(void *)&sun;
    434 			if ((fd = accept(s, sa, &len)) == -1)
    435 				DERR(EX_OSERR, "accept failed");
    436 
    437 			new_mount(fd, PMNT_DEVFUSE);
    438 		} while (1 /* CONSTCOND */);
    439 		break;
    440 	default:
    441 		DERRX(EX_SOFTWARE, "unexpected so_type %d", sock_type);
    442 		break;
    443 	}
    444 
    445 	/* NOTREACHED */
    446 	return 0;
    447 }
    448