1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/*-
* Copyright (c) 2005 Jean-Sbastien Pdron
* Copyright (c) 2005 Csaba Henk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <paths.h>
#include "fuse4bsd.h"
#include "mntopts.h"
void __usage_short(void);
void usage(void);
void helpmsg(void);
void showversion(void);
struct mntopt mopts[] = {
#define ALTF_PRIVATE 0x01
{ "private", 0, ALTF_PRIVATE, 1 },
{ "neglect_shares", 0, 0x02, 1 },
{ "push_symlinks_in", 0, 0x04, 1 },
{ "allow_other", 0, 0x08, 1 },
{ "default_permissions", 0, 0x10, 1 },
#define ALTF_MAXREAD 0x20
{ "max_read=", 0, ALTF_MAXREAD, 1 },
#define ALTF_SUBTYPE 0x40
{ "subtype=", 0, ALTF_SUBTYPE, 1 },
#define ALTF_SYNC_UNMOUNT 0x80
{ "sync_unmount", 0, ALTF_SYNC_UNMOUNT, 1 },
/* Linux specific options, we silently ignore them */
{ "fsname=", 0, 0x00, 1 },
{ "fd=", 0, 0x00, 1 },
{ "rootmode=", 0, 0x00, 1 },
{ "user_id=", 0, 0x00, 1 },
{ "group_id=", 0, 0x00, 1 },
{ "large_read", 0, 0x00, 1 },
/* "nonempty", just the first two chars are stripped off during parsing */
{ "nempty", 0, 0x00, 1 },
MOPT_STDOPTS,
MOPT_END
};
struct mntval {
int mv_flag;
void *mv_value;
int mv_len;
};
struct mntval mvals[] = {
{ ALTF_MAXREAD, NULL, 0 },
{ ALTF_SUBTYPE, NULL, 0 },
{ 0, NULL, 0 }
};
char *progname;
#define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE | ALTF_SYNC_UNMOUNT
int
main(int argc, char *argv[])
{
struct iovec *iov;
char ch = '\0';
int mntflags, iovlen, verbose = 0;
char *dev = NULL, *dir = NULL, mntpath[MAXPATHLEN];
char *devo = NULL, *diro = NULL;
char ndev[128], fdstr[15];
int i, done = 0, reject_allow_other = 0, safe_level = 0;
int altflags = DEFAULT_MOUNT_FLAGS;
int __altflags = DEFAULT_MOUNT_FLAGS;
struct mntopt *mo;
struct mntval *mv;
static struct option longopts[] = {
{"reject-allow_other", no_argument, NULL, 'A'},
{"safe", no_argument, NULL, 'S'},
{"daemon", required_argument, NULL, 'D'},
{"daemon_opts", required_argument, NULL, 'O'},
{"special", required_argument, NULL, 's'},
{"mountpath", required_argument, NULL, 'm'},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{0,0,0,0}
};
int pid = 0;
int fd = -1, fdx;
char *ep;
char *daemon = NULL, *daemon_opts = NULL;
progname = argv[0];
/*
* We want a parsing routine which is not sensitive to
* the position of args/opts; it should extract the
* first two args and stop at the beginning of the rest.
* (This makes it easier to call mount_fusefs from external
* utils than it is with a strict "util flags args" syntax.)
*/
iov = NULL;
iovlen = 0;
mntflags = 0;
/* All in all, I feel it more robust this way... */
unsetenv("POSIXLY_CORRECT");
if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN"))
getmnt_silent = 1;
if (getenv("MOUNT_FUSEFS_VERBOSE"))
verbose = 1;
do {
char *inner_opt;
int optlen;
for (i = 0; i < 3; i++) {
if (optind < argc && argv[optind][0] != '-') {
if (dir) {
done = 1;
break;
}
if (dev)
dir = argv[optind];
else
dev = argv[optind];
optind++;
}
}
switch(ch) {
case 'A':
reject_allow_other = 1;
break;
case 'S':
safe_level = 1;
break;
case 'D':
if (daemon)
errx(1, "daemon specified inconsistently");
daemon = optarg;
break;
case 'O':
if (daemon_opts)
errx(1, "daemon opts specified inconsistently");
daemon_opts = optarg;
break;
case 'o':
getmntopts(optarg, mopts, &mntflags, &altflags);
for (mv = mvals; mv->mv_flag; ++mv) {
if (! (altflags & mv->mv_flag))
continue;
for (mo = mopts; mo->m_flag; ++mo) {
char *p, *q;
if (mo->m_flag != mv->mv_flag)
continue;
p = strstr(optarg, mo->m_option);
if (p) {
p += strlen(mo->m_option);
q = p;
while (*q != '\0' && *q != ',')
q++;
mv->mv_len = q - p + 1;
mv->mv_value = malloc(mv->mv_len);
memcpy(mv->mv_value, p, mv->mv_len - 1);
((char *)mv->mv_value)[mv->mv_len - 1] = '\0';
break;
}
}
}
break;
case 's':
if (devo)
errx(1, "special specified inconsistently");
devo = optarg;
break;
case 'm':
if (diro)
errx(1, "mount path specified inconsistently");
diro = optarg;
break;
case 'v':
verbose = 1;
break;
case 'h':
helpmsg();
break;
case 'V':
showversion();
break;
case '\0':
break;
case '?':
default:
usage();
}
if (done)
break;
} while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1);
argc -= optind;
argv += optind;
if (devo) {
if (dev)
errx(1, "special specified inconsistently");
dev = devo;
} else if (diro)
errx(1, "if mountpoint is given via an option, special should also be given via an option");
if (diro) {
if (dir)
errx(1, "mount path specified inconsistently");
dir = diro;
}
if ((! dev) && argc > 0) {
dev = *argv++;
argc--;
}
if ((! dir) && argc > 0) {
dir = *argv++;
argc--;
}
if (! (dev && dir))
errx(1, "missing special and/or mountpoint");
for (mo = mopts; mo->m_flag; ++mo) {
if (altflags & mo->m_flag) {
char *val;
int iov_done = 0;
if (reject_allow_other &&
strcmp(mo->m_option, "allow_other") == 0)
/*
* reject_allow_other is stronger than a
* negative of allow_other: if this is set,
* allow_other is blocked, period.
*/
errx(1, "\"allow_other\" usage is banned by respective option");
for (mv = mvals; mv->mv_flag; ++mv) {
if (mo->m_flag != mv->mv_flag)
continue;
if (mv->mv_value) {
build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len);
iov_done = 1;
break;
}
}
if (! iov_done)
build_iovec(&iov, &iovlen, mo->m_option, "", -1);
}
if (__altflags & mo->m_flag) {
char *uscore_opt;
if (asprintf(&uscore_opt, "__%s", mo->m_option) == -1)
err(1, "failed to allocate memory");
build_iovec(&iov, &iovlen, uscore_opt, "", -1);
free(uscore_opt);
}
}
if (getenv("MOUNT_FUSEFS_SAFE"))
safe_level = 1;
if (safe_level > 0 && (argc > 0 || daemon || daemon_opts))
errx(1, "safe mode, spawning daemon not allowed");
if ((argc > 0 && (daemon || daemon_opts)) || (daemon_opts && ! daemon))
errx(1, "daemon specified inconsistently");
/*
* Resolve the mountpoint with realpath(3) and remove unnecessary
* slashes from the devicename if there are any.
*/
(void)checkpath(dir, mntpath);
(void)rmslashes(dev, dev);
if (strcmp(dev, "auto") == 0)
dev = "/dev/fuse";
if (strcmp(dev, "/dev/fuse") == 0) {
if (! (argc > 0 || daemon)) {
fprintf(stderr, "Please also specify the fuse daemon to run when mounting via the multiplexer!\n");
usage();
}
if ((fd = open(dev, O_RDWR)) < 0)
err(1, "failed to open fuse device");
} else {
fdx = strtol(dev, &ep, 10);
if (*ep == '\0')
fd = fdx;
}
/* Identifying device */
if (fd >= 0) {
struct stat sbuf;
char *ndevbas, *ep;
if (fstat(fd, &sbuf) == -1)
err(1, "cannot stat device file descriptor");
strcpy(ndev, _PATH_DEV);
ndevbas = ndev + strlen(_PATH_DEV);
devname_r(sbuf.st_rdev, S_IFCHR, ndevbas,
sizeof(ndev) - strlen(_PATH_DEV));
if (strncmp(ndevbas, "fuse", 4))
errx(1, "mounting inappropriate device");
strtol(ndevbas + 4, &ep, 10);
if (*ep != '\0')
errx(1, "mounting inappropriate device");
dev = ndev;
}
if (argc > 0 || daemon) {
char *fds;
if (fd < 0 && (fd = open(dev, O_RDWR)) < 0)
err(1, "failed to open fuse device");
if (asprintf(&fds, "%d", fd) == -1)
err(1, "failed to allocate memory");
setenv("FUSE_DEV_FD", fds, 1);
free(fds);
setenv("FUSE_NO_MOUNT", "1", 1);
if (daemon) {
char *bgdaemon;
int len;
if (! daemon_opts)
daemon_opts = "";
len = strlen(daemon) + 1 + strlen(daemon_opts) + 2 + 1;
bgdaemon = calloc(1, len);
if (! bgdaemon)
err(1, "failed to allocate memory");
strlcpy(bgdaemon, daemon, len);
strlcat(bgdaemon, " ", len);
strlcat(bgdaemon, daemon_opts, len);
strlcat(bgdaemon, " &", len);
if (system(bgdaemon))
err(1, "failed to call fuse daemon");
} else {
if ((pid = fork()) < 0)
err(1, "failed to fork for fuse daemon");
if (pid == 0) {
execvp(argv[0], argv);
err(1, "failed to exec fuse daemon");
}
}
}
if (fd >= 0 && ! init_backgrounded() && close(fd) < 0) {
if (pid)
kill(pid, SIGKILL);
err(1, "failed to close fuse device");
}
/* Prepare the options vector for nmount(). build_iovec() is declared
* in mntopts.h. */
sprintf(fdstr, "%d", fd);
build_iovec(&iov, &iovlen, "fstype", "fusefs", -1);
build_iovec(&iov, &iovlen, "fspath", mntpath, -1);
build_iovec(&iov, &iovlen, "from", dev, -1);
build_iovec(&iov, &iovlen, "fd", fdstr, -1);
if (verbose)
fprintf(stderr, "mounting fuse daemon on device %s\n", dev);
if (nmount(iov, iovlen, mntflags) < 0)
err(EX_OSERR, "%s on %s", dev, mntpath);
exit(0);
}
void
__usage_short(void) {
fprintf(stderr,
"usage:\n%s [-A|-S|-v|-V|-h|-D daemon|-O args|-s special|-m node|-o option...] special node [daemon args...]\n\n",
basename(progname));
}
void
usage(void)
{
struct mntopt *mo;
__usage_short();
fprintf(stderr, "known options:\n");
for (mo = mopts; mo->m_flag; ++mo)
fprintf(stderr, "\t%s\n", mo->m_option);
fprintf(stderr, "\n(use -h for a detailed description of these options)\n");
exit(EX_USAGE);
}
void
helpmsg(void)
{
if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) {
__usage_short();
fprintf(stderr, "description of options:\n");
}
/*
* The main use case of this function is giving info embedded in general
* FUSE lib help output. Therefore the style and the content of the output
* tries to fit there as much as possible.
*/
fprintf(stderr,
" -o allow_other allow access to other users\n"
/* " -o nonempty allow mounts over non-empty file/dir\n" */
" -o default_permissions enable permission checking by kernel\n"
/*
" -o fsname=NAME set filesystem name\n"
" -o large_read issue large read requests (2.4 only)\n"
*/
" -o subtype=NAME set filesystem type\n"
" -o max_read=N set maximum size of read requests\n"
" -o noprivate allow secondary mounting of the filesystem\n"
" -o neglect_shares don't report EBUSY when unmount attempted\n"
" in presence of secondary mounts\n"
" -o push_symlinks_in prefix absolute symlinks with mountpoint\n"
" -o sync_unmount do unmount synchronously\n"
);
exit(EX_USAGE);
}
void
showversion(void)
{
puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION);
exit(EX_USAGE);
}
int
init_backgrounded(void)
{
int ibg;
size_t len;
len = sizeof(ibg);
if (sysctlbyname("vfs.fuse.init_backgrounded", &ibg, &len, NULL, 0))
return (0);
return (ibg);
}
|