1 /* $NetBSD: perfused.c,v 1.22 2012/02/04 18:36:30 joerg 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 "perfused.h" 51 52 /* 53 * This is used for trace file. of course it will not work if 54 * we ever mount multiple filesystems in a single perfused, 55 * but it is not sure we will ever want to do that. 56 */ 57 static struct puffs_usermount *perfused_mount = NULL; 58 static FILE *perfused_trace = NULL; 59 60 static int access_mount(const char *, uid_t, int); 61 static void new_mount(int, int); 62 static int parse_debug(char *); 63 static void siginfo_handler(int); 64 static void sigusr1_handler(int); 65 static int parse_options(int, char **); 66 static void get_mount_info(int, struct perfuse_mount_info *, int); 67 68 /* 69 * Flags for new_mount() 70 */ 71 #define PMNT_DEVFUSE 0x0 /* We use /dev/fuse */ 72 #define PMNT_SOCKPAIR 0x1 /* We use socketpair */ 73 #define PMNT_DGRAM 0x2 /* We use SOCK_DGRAM sockets */ 74 75 76 static int 77 access_mount(const char *mnt, uid_t uid, int ro) 78 { 79 struct stat st; 80 mode_t mode; 81 82 if (uid == 0) 83 return 0; 84 85 if (stat(mnt, &st) == -1) 86 return -1; 87 88 if (st.st_uid != uid) 89 return -1; 90 91 mode = S_IRUSR; 92 if (!ro) 93 mode |= S_IWUSR; 94 95 if ((st.st_mode & mode) == mode) 96 return 0; 97 98 return -1; 99 } 100 101 static void 102 get_mount_info(int fd, struct perfuse_mount_info *pmi, int sock_type) 103 { 104 struct perfuse_mount_out *pmo; 105 struct sockcred cred; 106 int opt; 107 char *cp; 108 char *source = NULL; 109 char *target = NULL; 110 char *filesystemtype = NULL; 111 long mountflags = 0; 112 void *data = NULL; 113 char *sock = NULL; 114 115 pmo = (struct perfuse_mount_out *) 116 perfused_recv_early(fd, &cred, sizeof(cred)); 117 118 if (pmo == NULL) { 119 if (shutdown(fd, SHUT_RDWR) != 0) 120 DERR(EX_OSERR, "shutdown failed"); 121 exit(EX_PROTOCOL); 122 } 123 124 /* 125 * We do not need peer creds beyond this point 126 */ 127 opt = 0; 128 if (setsockopt(fd, 0, LOCAL_CREDS, &opt, sizeof(opt)) != 0) 129 DWARN("%s: setsockopt LOCAL_CREDS failed", __func__); 130 131 #ifdef PERFUSE_DEBUG 132 if (perfuse_diagflags & PDF_MISC) 133 DPRINTF("perfuse lengths: source = %"PRId32", " 134 "target = %"PRId32", filesystemtype = %"PRId32", " 135 "data = %"PRId32", sock = %"PRId32"\n", 136 pmo->pmo_source_len, pmo->pmo_target_len, 137 pmo->pmo_filesystemtype_len, pmo->pmo_data_len, 138 pmo->pmo_sock_len); 139 #endif 140 cp = (char *)(void *)(pmo + 1); 141 142 if (pmo->pmo_source_len != 0) { 143 source = cp; 144 cp += pmo->pmo_source_len; 145 } 146 147 if (pmo->pmo_target_len != 0) { 148 target = cp; 149 cp += pmo->pmo_target_len; 150 } 151 152 if (pmo->pmo_filesystemtype_len != 0) { 153 filesystemtype = cp; 154 cp += pmo->pmo_filesystemtype_len; 155 } 156 157 mountflags = pmo->pmo_mountflags; 158 159 if (pmo->pmo_data_len != 0) { 160 data = cp; 161 cp += pmo->pmo_data_len; 162 } 163 164 if (pmo->pmo_sock_len != 0) { 165 sock = cp; 166 cp += pmo->pmo_sock_len; 167 } 168 169 #ifdef PERFUSE_DEBUG 170 if (perfuse_diagflags & PDF_MISC) 171 DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\", \"%s\")\n", 172 __func__, source, target, filesystemtype, 173 mountflags, (const char *)data, sock); 174 #endif 175 pmi->pmi_source = source; 176 pmi->pmi_target = target; 177 pmi->pmi_filesystemtype = filesystemtype; 178 pmi->pmi_mountflags = (int)mountflags; 179 pmi->pmi_data = data; 180 181 pmi->pmi_uid = cred.sc_euid; 182 183 /* 184 * Connect to the remote socket if provided ans using SOCK_DGRAM 185 */ 186 if ((sock_type == SOCK_DGRAM) && sock) { 187 const struct sockaddr *sa; 188 struct sockaddr_un sun; 189 190 sa = (const struct sockaddr *)(void *)&sun; 191 sun.sun_len = sizeof(sun); 192 sun.sun_family = AF_LOCAL; 193 strcpy(sun.sun_path, sock); 194 195 if (connect(fd, sa, sun.sun_len) != 0) 196 DERR(EX_OSERR, "connect \"%s\" failed", sun.sun_path); 197 } 198 199 return; 200 } 201 202 static void 203 new_mount(int fd, int pmnt_flags) 204 { 205 struct puffs_usermount *pu; 206 struct perfuse_mount_info pmi; 207 struct perfuse_callbacks pc; 208 int ro_flag; 209 pid_t pid; 210 int flags; 211 int sock_type; 212 char trace_file[MAXPATHLEN + 1]; 213 char trace_name[MAXPATHLEN + 1]; 214 ssize_t trace_namelen; 215 int i; 216 217 pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork(); 218 switch(pid) { 219 case -1: 220 DERR(EX_OSERR, "cannot fork"); 221 break; 222 case 0: 223 break; 224 default: 225 return; 226 /* NOTREACHED */ 227 break; 228 } 229 230 /* 231 * Mount information (source, target, mount flags...) 232 */ 233 sock_type = pmnt_flags & PMNT_DGRAM ? SOCK_DGRAM : SOCK_SEQPACKET; 234 get_mount_info(fd, &pmi, sock_type); 235 236 /* 237 * Check that peer owns mountpoint and read (and write) on it? 238 */ 239 ro_flag = pmi.pmi_mountflags & MNT_RDONLY; 240 if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0) 241 DERRX(EX_NOPERM, "insuficient privileges to mount on %s", 242 pmi.pmi_target); 243 244 245 /* 246 * Initialize libperfuse, which will initialize libpuffs 247 */ 248 pc.pc_new_msg = perfused_new_pb; 249 pc.pc_xchg_msg = perfused_xchg_pb; 250 pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy; 251 pc.pc_get_inhdr = perfused_get_inhdr; 252 pc.pc_get_inpayload = perfused_get_inpayload; 253 pc.pc_get_outhdr = perfused_get_outhdr; 254 pc.pc_get_outpayload = perfused_get_outpayload; 255 pc.pc_umount = perfused_umount; 256 257 pu = perfuse_init(&pc, &pmi); 258 259 puffs_framev_init(pu, perfused_readframe, perfused_writeframe, 260 perfused_cmpframe, perfused_gotframe, perfused_fdnotify); 261 262 if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1) 263 DERR(EX_SOFTWARE, "puffs_framev_addfd failed"); 264 265 perfuse_setspecific(pu, (void *)(intptr_t)fd); 266 267 setproctitle("perfused %s", pmi.pmi_target); 268 (void)kill(getpid(), SIGINFO); /* This is for -s option */ 269 270 perfuse_fs_init(pu); 271 272 /* 273 * Non blocking I/O on /dev/fuse 274 * This must be done after perfused_fs_init 275 */ 276 if ((flags = fcntl(fd, F_GETFL, 0)) == -1) 277 DERR(EX_OSERR, "fcntl failed"); 278 if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0) 279 DERR(EX_OSERR, "fcntl failed"); 280 281 /* 282 * Setup trace file facility 283 */ 284 perfused_mount = pu; 285 286 trace_namelen = strlcpy(trace_name, pmi.pmi_target, MAXPATHLEN); 287 for (i = 0; i < trace_namelen; i++) 288 if (trace_name[i] == '/') 289 trace_name[i] = '-'; 290 291 (void)snprintf(trace_file, MAXPATHLEN, _PATH_VAR_RUN_PERFUSE_TRACE, 292 trace_name); 293 294 if ((perfused_trace = fopen(trace_file, "w")) == NULL) 295 DERR(EX_OSFILE, 296 "could not open \"%s\"", 297 _PATH_VAR_RUN_PERFUSE_TRACE); 298 299 if (signal(SIGUSR1, sigusr1_handler) != 0) 300 DERR(EX_OSERR, "signal failed"); 301 302 /* 303 * Hand over control to puffs main loop. 304 */ 305 if (perfuse_mainloop(pu) != 0) 306 DERRX(EX_SOFTWARE, "perfused_mainloop exit"); 307 308 /* 309 * Normal return after unmount 310 */ 311 return; 312 } 313 314 static int 315 parse_debug(char *optstr) 316 { 317 int retval = PDF_SYSLOG; 318 char *opt; 319 char *lastp; 320 321 for (opt = strtok_r(optstr, ",", &lastp); 322 opt; 323 opt = strtok_r(NULL, ",", &lastp)) { 324 if (strcmp(opt, "fuse") == 0) 325 retval |= PDF_FUSE; 326 else if (strcmp(opt, "puffs") == 0) 327 retval |= PDF_PUFFS; 328 else if (strcmp(opt, "dump") == 0) 329 retval |= PDF_DUMP; 330 else if (strcmp(opt, "fh") == 0) 331 retval |= PDF_FH; 332 else if (strcmp(opt, "readdir") == 0) 333 retval |= PDF_READDIR; 334 else if (strcmp(opt, "reclaim") == 0) 335 retval |= PDF_RECLAIM; 336 else if (strcmp(opt, "requeue") == 0) 337 retval |= PDF_REQUEUE; 338 else if (strcmp(opt, "sync") == 0) 339 retval |= PDF_SYNC; 340 else if (strcmp(opt, "misc") == 0) 341 retval |= PDF_MISC; 342 else if (strcmp(opt, "filename") == 0) 343 retval |= PDF_FILENAME; 344 else if (strcmp(opt, "reize") == 0) 345 retval |= PDF_RESIZE; 346 else 347 DWARNX("unknown debug flag \"%s\"", opt); 348 } 349 350 return retval; 351 } 352 353 /* ARGSUSED0 */ 354 static void 355 siginfo_handler(int sig) 356 { 357 static int old_flags = 0; 358 int swap; 359 360 swap = perfuse_diagflags; 361 perfuse_diagflags = old_flags; 362 old_flags = swap; 363 364 DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis"); 365 366 return; 367 } 368 369 /* ARGSUSED0 */ 370 static void 371 sigusr1_handler(int sig) 372 { 373 if (perfuse_diagflags & PDF_TRACE) { 374 perfuse_trace_dump(perfused_mount, perfused_trace); 375 perfuse_diagflags &= ~PDF_TRACE; 376 DPRINTF("trace dumped, trace disabled"); 377 } else { 378 perfuse_diagflags |= PDF_TRACE; 379 DPRINTF("trace enabled"); 380 } 381 382 return; 383 } 384 385 static int 386 parse_options(int argc, char **argv) 387 { 388 int ch; 389 int foreground = 0; 390 int retval = -1; 391 392 perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG; 393 394 while ((ch = getopt(argc, argv, "d:fsi:")) != -1) { 395 switch (ch) { 396 case 'd': 397 perfuse_diagflags |= parse_debug(optarg); 398 break; 399 case 's': 400 if (signal(SIGINFO, siginfo_handler) != 0) 401 DERR(EX_OSERR, "signal failed"); 402 break; 403 case 'f': 404 foreground = 1; 405 perfuse_diagflags |= PDF_MISC; 406 break; 407 case 'i': 408 retval = atoi(optarg); 409 foreground = 1; 410 break; 411 default: 412 DERRX(EX_USAGE, "%s [-fs] [-d classes] [-i fd]", argv[0]); 413 break; 414 } 415 } 416 417 if (!foreground) 418 perfuse_diagflags &= ~PDF_FOREGROUND; 419 420 return retval; 421 } 422 423 int 424 main(int argc, char **argv) 425 { 426 int s; 427 int sock_type; 428 socklen_t len; 429 430 s = parse_options(argc, argv); 431 432 if (perfuse_diagflags & PDF_SYSLOG) 433 openlog("perfused", LOG_NDELAY, LOG_DAEMON); 434 435 if (!(perfuse_diagflags & PDF_FOREGROUND)) 436 if (daemon(0, 0) != 0) 437 DERR(EX_OSERR, "daemon failed"); 438 439 if (s != -1) { 440 new_mount(s, PMNT_SOCKPAIR); 441 exit(0); 442 } 443 444 s = perfused_open_sock(); 445 446 #ifdef PERFUSE_DEBUG 447 if (perfuse_diagflags & PDF_MISC) 448 DPRINTF("perfused ready\n"); 449 #endif 450 len = sizeof(sock_type); 451 if (getsockopt(s, SOL_SOCKET, SO_TYPE, &sock_type, &len) != 0) 452 DERR(EX_OSERR, "getsockopt SO_TYPE failed"); 453 454 switch(sock_type) { 455 case SOCK_DGRAM: 456 new_mount(s, PMNT_DEVFUSE|PMNT_DGRAM); 457 exit(0); 458 case SOCK_SEQPACKET: 459 if (listen(s, 0) != 0) 460 DERR(EX_OSERR, "listen failed"); 461 462 for (;;) { 463 int fd; 464 struct sockaddr_un sun; 465 struct sockaddr *sa; 466 467 len = sizeof(sun); 468 sa = (struct sockaddr *)(void *)&sun; 469 if ((fd = accept(s, sa, &len)) == -1) 470 DERR(EX_OSERR, "accept failed"); 471 472 new_mount(fd, PMNT_DEVFUSE); 473 } 474 default: 475 DERRX(EX_SOFTWARE, "unexpected so_type %d", sock_type); 476 break; 477 } 478 479 /* NOTREACHED */ 480 return 0; 481 } 482 483 void 484 perfused_panic(void) 485 { 486 DWARNX("filesystem crashed"); 487 exit(EX_OK); 488 } 489