Home | History | Annotate | Line # | Download | only in perfused
perfused.c revision 1.10
      1 /*  $NetBSD: perfused.c,v 1.10 2010/09/23 16:02:34 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 *);
     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 
     67 
     68 static int
     69 access_mount(mnt, uid, ro)
     70 	const char *mnt;
     71 	uid_t uid;
     72 	int ro;
     73 {
     74 	struct stat st;
     75 	mode_t mode;
     76 
     77 	if (uid == 0)
     78 		return 0;
     79 
     80 	if (stat(mnt, &st) == -1)
     81 		return -1;
     82 
     83 	if (st.st_uid != uid)
     84 		return -1;
     85 
     86 	mode = S_IRUSR;
     87 	if (!ro)
     88 		mode |= S_IWUSR;
     89 
     90 	if ((st.st_mode & mode) == mode)
     91 		return 0;
     92 
     93 	return -1;
     94 }
     95 
     96 static void
     97 get_mount_info(fd, pmi)
     98 	int fd;
     99 	struct perfuse_mount_info *pmi;
    100 {
    101 	struct perfuse_mount_out *pmo;
    102 	struct sockcred cred;
    103 	int opt;
    104 	char *cp;
    105 	char *source = NULL;
    106 	char *target = NULL;
    107 	char *filesystemtype = NULL;
    108 	long mountflags = 0;
    109 	void *data = NULL;
    110 	char *sock = NULL;
    111 
    112 	pmo = (struct perfuse_mount_out *)
    113 		perfuse_recv_early(fd, &cred, sizeof(cred));
    114 
    115 	if (pmo == NULL) {
    116 		if (shutdown(fd, SHUT_RDWR) != 0)
    117 			DERR(EX_OSERR, "shutdown failed");
    118 		exit(EX_PROTOCOL);
    119 	}
    120 
    121 	/*
    122 	 * We do not need peer creds beyond this point
    123 	 */
    124 	opt = 0;
    125 	if (setsockopt(fd, 0, LOCAL_CREDS, &opt, sizeof(opt)) != 0)
    126 		DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
    127 
    128 #ifdef PERFUSE_DEBUG
    129 	if (perfuse_diagflags & PDF_MISC)
    130 		DPRINTF("perfuse lengths: source = %"PRId32", "
    131 			"target = %"PRId32", filesystemtype = %"PRId32", "
    132 			"data = %"PRId32", sock = %"PRId32"\n",
    133 			pmo->pmo_source_len, pmo->pmo_target_len,
    134 			pmo->pmo_filesystemtype_len, pmo->pmo_data_len,
    135 			pmo->pmo_sock_len);
    136 #endif
    137 	cp = (char *)(void *)(pmo + 1);
    138 
    139 	if (pmo->pmo_source_len != 0) {
    140 		source = cp;
    141 		cp += pmo->pmo_source_len;
    142 	}
    143 
    144 	if (pmo->pmo_target_len != 0) {
    145 		target = cp;
    146 		cp += pmo->pmo_target_len;
    147 	}
    148 
    149 	if (pmo->pmo_filesystemtype_len != 0) {
    150 		filesystemtype = cp;
    151 		cp += pmo->pmo_filesystemtype_len;
    152 	}
    153 
    154 	mountflags = pmo->pmo_mountflags;
    155 
    156 	if (pmo->pmo_data_len != 0) {
    157 		data = cp;
    158 		cp += pmo->pmo_data_len;
    159 	}
    160 
    161 	if (pmo->pmo_sock_len != 0) {
    162 		sock = cp;
    163 		cp += pmo->pmo_sock_len;
    164 	}
    165 
    166 #ifdef PERFUSE_DEBUG
    167 	if (perfuse_diagflags & PDF_MISC)
    168 		DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\", \"%s\")\n",
    169 		__func__, source, target, filesystemtype,
    170 		mountflags, (const char *)data, sock);
    171 #endif
    172 	pmi->pmi_source = source;
    173 	pmi->pmi_target = target;
    174 	pmi->pmi_filesystemtype = filesystemtype;
    175 	pmi->pmi_mountflags = (int)mountflags;
    176 	pmi->pmi_data = data;
    177 
    178 	pmi->pmi_uid = cred.sc_euid;
    179 
    180 	/*
    181 	 * Connect to the remote socket, if provided
    182 	 */
    183 	if (sock) {
    184 		const struct sockaddr *sa;
    185 		struct sockaddr_un sun;
    186 
    187 		sa = (const struct sockaddr *)(void *)&sun;
    188 		sun.sun_len = sizeof(sun);
    189 		sun.sun_family = AF_LOCAL;
    190 		strcpy(sun.sun_path, sock);
    191 
    192 		if (connect(fd, sa, sun.sun_len) != 0)
    193 			DERR(EX_OSERR, "connect \"%s\" failed", sun.sun_path);
    194 	}
    195 
    196 	return;
    197 }
    198 
    199 static void
    200 new_mount(fd, pmnt_flags)
    201 	int fd;
    202 	int pmnt_flags;
    203 {
    204 	struct puffs_usermount *pu;
    205 	struct perfuse_mount_info pmi;
    206 	struct perfuse_callbacks pc;
    207 	int ro_flag;
    208 	pid_t pid;
    209 	int flags;
    210 
    211 	pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
    212 	switch(pid) {
    213 	case -1:
    214 		DERR(EX_OSERR, "cannot fork");
    215 		break;
    216 	case 0:
    217 		break;
    218 	default:
    219 		return;
    220 		/* NOTREACHED */
    221 		break;
    222 	}
    223 
    224 	/*
    225 	 * Mount information (source, target, mount flags...)
    226 	 */
    227 	get_mount_info(fd, &pmi);
    228 
    229 	/*
    230 	 * Check that peer owns mountpoint and read (and write) on it?
    231 	 */
    232 	ro_flag = pmi.pmi_mountflags & MNT_RDONLY;
    233 	if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0)
    234 		DERRX(EX_NOPERM, "insuficient privileges to mount on %s",
    235 		      pmi.pmi_target);
    236 
    237 
    238 	/*
    239 	 * Initialize libperfuse, which will initialize libpuffs
    240 	 */
    241 	pc.pc_new_msg = perfuse_new_pb;
    242 	pc.pc_xchg_msg = perfuse_xchg_pb;
    243 	pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy;
    244 	pc.pc_get_inhdr = perfuse_get_inhdr;
    245 	pc.pc_get_inpayload = perfuse_get_inpayload;
    246 	pc.pc_get_outhdr = perfuse_get_outhdr;
    247 	pc.pc_get_outpayload = perfuse_get_outpayload;
    248 
    249 	pu = perfuse_init(&pc, &pmi);
    250 
    251 	puffs_framev_init(pu, perfuse_readframe, perfuse_writeframe,
    252 			  perfuse_cmpframe, perfuse_gotframe, perfuse_fdnotify);
    253 
    254 	if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
    255 		DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
    256 
    257 	perfuse_setspecific(pu, (void *)(long)fd);
    258 
    259 	setproctitle("perfused %s", pmi.pmi_target);
    260 	(void)kill(getpid(), SIGINFO);		/* This is for -s option */
    261 
    262 	perfuse_fs_init(pu);
    263 
    264 	/*
    265 	 * Non blocking I/O on /dev/fuse
    266 	 * This must be done after perfuse_fs_init
    267 	 */
    268 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
    269 		DERR(EX_OSERR, "fcntl failed");
    270 	if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0)
    271 		DERR(EX_OSERR, "fcntl failed");
    272 
    273 	/*
    274 	 * Hand over control to puffs main loop.
    275 	 */
    276 	(void)perfuse_mainloop(pu);
    277 
    278 	DERRX(EX_SOFTWARE, "perfuse_mainloop exit");
    279 
    280 	/* NOTREACHED */
    281 	return;
    282 }
    283 
    284 static int
    285 parse_debug(optstr)
    286 	char *optstr;
    287 {
    288 	int retval = PDF_SYSLOG;
    289 	char *opt;
    290 	char *lastp;
    291 
    292 	for (opt = strtok_r(optstr, ",", &lastp);
    293 	     opt;
    294 	     opt = strtok_r(NULL, ",", &lastp)) {
    295 		if (strcmp(opt, "fuse") == 0)
    296 			retval |= PDF_FUSE;
    297 		else if (strcmp(opt, "puffs") == 0)
    298 			retval |= PDF_PUFFS;
    299 		else if (strcmp(opt, "dump") == 0)
    300 			retval |= PDF_DUMP;
    301 		else if (strcmp(opt, "fh") == 0)
    302 			retval |= PDF_FH;
    303 		else if (strcmp(opt, "readdir") == 0)
    304 			retval |= PDF_READDIR;
    305 		else if (strcmp(opt, "reclaim") == 0)
    306 			retval |= PDF_RECLAIM;
    307 		else if (strcmp(opt, "requeue") == 0)
    308 			retval |= PDF_REQUEUE;
    309 		else if (strcmp(opt, "sync") == 0)
    310 			retval |= PDF_SYNC;
    311 		else if (strcmp(opt, "misc") == 0)
    312 			retval |= PDF_MISC;
    313 		else if (strcmp(opt, "filename") == 0)
    314 			retval |= PDF_FILENAME;
    315 		else
    316 			DERRX(EX_USAGE, "unknown debug flag \"%s\"", opt);
    317 	}
    318 
    319 	return retval;
    320 }
    321 
    322 /* ARGSUSED0 */
    323 static void
    324 siginfo_handler(sig)
    325 	int sig;
    326 {
    327 	static int old_flags = 0;
    328 	int swap;
    329 
    330 	swap = perfuse_diagflags;
    331 	perfuse_diagflags = old_flags;
    332 	old_flags = swap;
    333 
    334 	DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis");
    335 
    336 	return;
    337 }
    338 
    339 static int
    340 parse_options(argc, argv)
    341 	int argc;
    342 	char **argv;
    343 {
    344 	int ch;
    345 	int foreground = 0;
    346 	int retval = -1;
    347 
    348 	perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
    349 
    350 	while ((ch = getopt(argc, argv, "d:fsi:")) != -1) {
    351 		switch (ch) {
    352 		case 'd':
    353 			perfuse_diagflags |= parse_debug(optarg);
    354 			break;
    355 		case 's':
    356 			if (signal(SIGINFO, siginfo_handler) != 0)
    357 				DERR(EX_OSERR, "signal failed");
    358 			break;
    359 		case 'f':
    360 			foreground = 1;
    361 			perfuse_diagflags |= PDF_MISC;
    362 			break;
    363 		case 'i':
    364 			retval = atoi(optarg);
    365 			foreground = 1;
    366 			break;
    367 		default:
    368 			DERR(EX_USAGE, "%s [-fs] [-d level] [-i fd]", argv[0]);
    369 			break;
    370 		}
    371 	}
    372 
    373 	if (!foreground)
    374 		perfuse_diagflags &= ~PDF_FOREGROUND;
    375 
    376 	return retval;
    377 }
    378 
    379 int
    380 main(argc, argv)
    381 	int argc;
    382 	char **argv;
    383 {
    384 	int s;
    385 
    386 	s = parse_options(argc, argv);
    387 
    388 	if (perfuse_diagflags & PDF_SYSLOG)
    389 		openlog("perfused", 0, LOG_DAEMON);
    390 
    391 	if (!(perfuse_diagflags & PDF_FOREGROUND))
    392 		if (daemon(0, 0) != 0)
    393 			DERR(EX_OSERR, "daemon failed");
    394 
    395 	if (s != -1) {
    396 		new_mount(s, PMNT_SOCKPAIR);
    397 		DERRX(EX_SOFTWARE, "new_mount exit while -i is used");
    398 	}
    399 
    400 	s = perfuse_open_sock();
    401 
    402 	do {
    403 #if (PERFUSE_SOCKTYPE != SOCK_DGRAM)
    404 		struct sockaddr *sa;
    405 		struct sockaddr_storage ss;
    406 		socklen_t ss_len;
    407 #endif
    408 		int fd;
    409 
    410 #ifdef PERFUSE_DEBUG
    411 		if (perfuse_diagflags & PDF_MISC)
    412 			DPRINTF("waiting connexion\n");
    413 #endif
    414 #if (PERFUSE_SOCKTYPE == SOCK_DGRAM)
    415 		fd = s;
    416 #else
    417 		sa = (struct sockaddr *)(void *)&ss;
    418 		ss_len = sizeof(ss);
    419 
    420 		if ((fd = accept(s, sa, &ss_len)) == -1)
    421 			DERR(EX_OSERR,  "accept failed");
    422 
    423 #ifdef PERFUSE_DEBUG
    424 		if (perfuse_diagflags & PDF_MISC)
    425 			DPRINTF("connexion accepted\n");
    426 #endif /* PERFUSE_DEBUG */
    427 #endif /* PERFUSE_SOCKTYPE */
    428 
    429 		new_mount(fd, PMNT_DEVFUSE);
    430 	} while (1 /* CONSTCOND */);
    431 
    432 	/* NOTREACHED */
    433 	return 0;
    434 }
    435