11 #include "fuse_config.h" 
   12 #include "mount_util.h" 
   28 #include "fuse_mount_compat.h" 
   30 #include <sys/fsuid.h> 
   31 #include <sys/socket.h> 
   32 #include <sys/utsname.h> 
   37 #define FUSE_COMMFD_ENV         "_FUSE_COMMFD" 
   39 #define FUSE_DEV "/dev/fuse" 
   41 static const char *progname;
 
   43 static int user_allow_other = 0;
 
   44 static int mount_max = 1000;
 
   46 static int auto_unmount = 0;
 
   48 #ifdef GETMNTENT_NEEDS_UNESCAPING 
   53 static void unescape(
char *buf) {
 
   57                 char *next_src = strchrnul(src, 
'\\');
 
   58                 int offset = next_src - src;
 
   59                 memmove(dest, src, offset);
 
   69                 if(
'0' <= src[0] && src[0] < 
'2' &&
 
   70                    '0' <= src[1] && src[1] < 
'8' &&
 
   71                    '0' <= src[2] && src[2] < 
'8') {
 
   72                         *dest++ = (src[0] - 
'0') << 6
 
   74                                 | (src[2] - 
'0') << 0;
 
   76                 } 
else if (src[0] == 
'\\') {
 
   85 static struct mntent *GETMNTENT(FILE *stream)
 
   87         struct mntent *entp = getmntent(stream);
 
   89                 unescape(entp->mnt_fsname);
 
   90                 unescape(entp->mnt_dir);
 
   91                 unescape(entp->mnt_type);
 
   92                 unescape(entp->mnt_opts);
 
   97 #define GETMNTENT getmntent 
  101 static const char *get_user_name(
void)
 
  103         struct passwd *pw = getpwuid(getuid());
 
  104         if (pw != NULL && pw->pw_name != NULL)
 
  107                 fprintf(stderr, 
"%s: could not determine username\n", progname);
 
  112 static uid_t oldfsuid;
 
  113 static gid_t oldfsgid;
 
  115 static void drop_privs(
void)
 
  118                 oldfsuid = setfsuid(getuid());
 
  119                 oldfsgid = setfsgid(getgid());
 
  123 static void restore_privs(
void)
 
  135 static int lock_umount(
void)
 
  137         const char *mtab_lock = _PATH_MOUNTED 
".fuselock";
 
  140         struct stat mtab_stat;
 
  143         if (lstat(_PATH_MOUNTED, &mtab_stat) == 0 && S_ISLNK(mtab_stat.st_mode))
 
  146         mtablock = open(mtab_lock, O_RDWR | O_CREAT, 0600);
 
  147         if (mtablock == -1) {
 
  148                 fprintf(stderr, 
"%s: unable to open fuse lock file: %s\n",
 
  149                         progname, strerror(errno));
 
  152         res = lockf(mtablock, F_LOCK, 0);
 
  154                 fprintf(stderr, 
"%s: error getting lock: %s\n", progname,
 
  163 static void unlock_umount(
int mtablock)
 
  168                 res = lockf(mtablock, F_ULOCK, 0);
 
  170                         fprintf(stderr, 
"%s: error releasing lock: %s\n",
 
  171                                 progname, strerror(errno));
 
  177 static int add_mount(
const char *source, 
const char *mnt, 
const char *type,
 
  180         return fuse_mnt_add_mount(progname, source, mnt, type, opts);
 
  183 static int may_unmount(
const char *mnt, 
int quiet)
 
  187         const char *user = NULL;
 
  191         const char *mtab = _PATH_MOUNTED;
 
  193         user = get_user_name();
 
  197         fp = setmntent(mtab, 
"r");
 
  199                 fprintf(stderr, 
"%s: failed to open %s: %s\n", progname, mtab,
 
  204         uidlen = sprintf(uidstr, 
"%u", getuid());
 
  207         while ((entp = GETMNTENT(fp)) != NULL) {
 
  208                 if (!found && strcmp(entp->mnt_dir, mnt) == 0 &&
 
  209                     (strcmp(entp->mnt_type, 
"fuse") == 0 ||
 
  210                      strcmp(entp->mnt_type, 
"fuseblk") == 0 ||
 
  211                      strncmp(entp->mnt_type, 
"fuse.", 5) == 0 ||
 
  212                      strncmp(entp->mnt_type, 
"fuseblk.", 8) == 0)) {
 
  213                         char *p = strstr(entp->mnt_opts, 
"user=");
 
  215                             (p == entp->mnt_opts || *(p-1) == 
',') &&
 
  216                             strcmp(p + 5, user) == 0) {
 
  223                                   strstr(entp->mnt_opts, 
"user_id=")) &&
 
  224                                  (p == entp->mnt_opts ||
 
  226                                  strncmp(p + 8, uidstr, uidlen) == 0 &&
 
  227                                  (*(p+8+uidlen) == 
',' ||
 
  228                                   *(p+8+uidlen) == 
'\0')) {
 
  239                                 "%s: entry for %s not found in %s\n",
 
  240                                 progname, mnt, mtab);
 
  271 static int check_is_mount_child(
void *p)
 
  274         const char *last = a[0];
 
  275         const char *mnt = a[1];
 
  276         const char *type = a[2];
 
  278         const char *procmounts = 
"/proc/mounts";
 
  284         res = mount(
"", 
"/", 
"", MS_PRIVATE | MS_REC, NULL);
 
  286                 fprintf(stderr, 
"%s: failed to mark mounts private: %s\n",
 
  287                         progname, strerror(errno));
 
  291         fp = setmntent(procmounts, 
"r");
 
  293                 fprintf(stderr, 
"%s: failed to open %s: %s\n", progname,
 
  294                         procmounts, strerror(errno));
 
  299         while (GETMNTENT(fp) != NULL)
 
  303         fp = setmntent(procmounts, 
"r");
 
  305                 fprintf(stderr, 
"%s: failed to open %s: %s\n", progname,
 
  306                         procmounts, strerror(errno));
 
  310         res = mount(
".", 
"/", 
"", MS_BIND | MS_REC, NULL);
 
  312                 fprintf(stderr, 
"%s: failed to bind parent to /: %s\n",
 
  313                         progname, strerror(errno));
 
  318         while ((entp = GETMNTENT(fp)) != NULL) {
 
  323                 if (entp->mnt_dir[0] == 
'/' &&
 
  324                     strcmp(entp->mnt_dir + 1, last) == 0 &&
 
  325                     (!type || strcmp(entp->mnt_type, type) == 0)) {
 
  333                 fprintf(stderr, 
"%s: %s not mounted\n", progname, mnt);
 
  340 static pid_t clone_newns(
void *a)
 
  343         char *stack = buf + (
sizeof(buf) / 2 - ((
size_t) buf & 15));
 
  346         extern int __clone2(
int (*fn)(
void *),
 
  347                             void *child_stack_base, 
size_t stack_size,
 
  348                             int flags, 
void *arg, pid_t *ptid,
 
  349                             void *tls, pid_t *ctid);
 
  351         return __clone2(check_is_mount_child, stack, 
sizeof(buf) / 2,
 
  352                         CLONE_NEWNS, a, NULL, NULL, NULL);
 
  354         return clone(check_is_mount_child, stack, CLONE_NEWNS, a);
 
  358 static int check_is_mount(
const char *last, 
const char *mnt, 
const char *type)
 
  362         const char *a[3] = { last, mnt, type };
 
  364         pid = clone_newns((
void *) a);
 
  365         if (pid == (pid_t) -1) {
 
  366                 fprintf(stderr, 
"%s: failed to clone namespace: %s\n",
 
  367                         progname, strerror(errno));
 
  370         p = waitpid(pid, &status, __WCLONE);
 
  371         if (p == (pid_t) -1) {
 
  372                 fprintf(stderr, 
"%s: waitpid failed: %s\n",
 
  373                         progname, strerror(errno));
 
  376         if (!WIFEXITED(status)) {
 
  377                 fprintf(stderr, 
"%s: child terminated abnormally (status %i)\n",
 
  381         if (WEXITSTATUS(status) != 0)
 
  387 static int chdir_to_parent(
char *copy, 
const char **lastp)
 
  394         tmp = strrchr(copy, 
'/');
 
  395         if (tmp == NULL || tmp[1] == 
'\0') {
 
  396                 fprintf(stderr, 
"%s: internal error: invalid abs path: <%s>\n",
 
  404         } 
else if (tmp[1] != 
'\0') {
 
  414                 fprintf(stderr, 
"%s: failed to chdir to %s: %s\n",
 
  415                         progname, parent, strerror(errno));
 
  419         if (getcwd(buf, 
sizeof(buf)) == NULL) {
 
  420                 fprintf(stderr, 
"%s: failed to obtain current directory: %s\n",
 
  421                         progname, strerror(errno));
 
  424         if (strcmp(buf, parent) != 0) {
 
  425                 fprintf(stderr, 
"%s: mountpoint moved (%s -> %s)\n", progname,
 
  435 static int unmount_fuse_locked(
const char *mnt, 
int quiet, 
int lazy)
 
  440         int umount_flags = (lazy ? UMOUNT_DETACH : 0) | UMOUNT_NOFOLLOW;
 
  443                 res = may_unmount(mnt, quiet);
 
  450                 fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
  455         res = chdir_to_parent(copy, &last);
 
  460         res = umount2(last, umount_flags);
 
  461         if (res == -1 && !quiet) {
 
  462                 fprintf(stderr, 
"%s: failed to unmount %s: %s\n",
 
  463                         progname, mnt, strerror(errno));
 
  473                 fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
  477         return fuse_mnt_remove_mount(progname, mnt);
 
  480 static int unmount_fuse(
const char *mnt, 
int quiet, 
int lazy)
 
  483         int mtablock = lock_umount();
 
  485         res = unmount_fuse_locked(mnt, quiet, lazy);
 
  486         unlock_umount(mtablock);
 
  491 static int count_fuse_fs(
void)
 
  495         const char *mtab = _PATH_MOUNTED;
 
  496         FILE *fp = setmntent(mtab, 
"r");
 
  498                 fprintf(stderr, 
"%s: failed to open %s: %s\n", progname, mtab,
 
  502         while ((entp = GETMNTENT(fp)) != NULL) {
 
  503                 if (strcmp(entp->mnt_type, 
"fuse") == 0 ||
 
  504                     strncmp(entp->mnt_type, 
"fuse.", 5) == 0)
 
  513 static int count_fuse_fs(
void)
 
  518 static int add_mount(
const char *source, 
const char *mnt, 
const char *type,
 
  528 static int unmount_fuse(
const char *mnt, 
int quiet, 
int lazy)
 
  531         return fuse_mnt_umount(progname, mnt, mnt, lazy);
 
  535 static void strip_line(
char *line)
 
  537         char *s = strchr(line, 
'#');
 
  540         for (s = line + strlen(line) - 1;
 
  541              s >= line && isspace((
unsigned char) *s); s--);
 
  543         for (s = line; isspace((
unsigned char) *s); s++);
 
  545                 memmove(line, s, strlen(s)+1);
 
  548 static void parse_line(
char *line, 
int linenum)
 
  551         if (strcmp(line, 
"user_allow_other") == 0)
 
  552                 user_allow_other = 1;
 
  553         else if (sscanf(line, 
"mount_max = %i", &tmp) == 1)
 
  557                         "%s: unknown parameter in %s at line %i: '%s'\n",
 
  558                         progname, FUSE_CONF, linenum, line);
 
  561 static void read_conf(
void)
 
  563         FILE *fp = fopen(FUSE_CONF, 
"r");
 
  568                 while (fgets(line, 
sizeof(line), fp) != NULL) {
 
  570                                 if (line[strlen(line)-1] == 
'\n') {
 
  572                                         parse_line(line, linenum);
 
  576                         } 
else if(line[strlen(line)-1] == 
'\n') {
 
  577                                 fprintf(stderr, 
"%s: reading %s: line %i too long\n", progname, FUSE_CONF, linenum);
 
  585                         fprintf(stderr, 
"%s: reading %s: missing newline at end of file\n", progname, FUSE_CONF);
 
  589                         fprintf(stderr, 
"%s: reading %s: read failed\n", progname, FUSE_CONF);
 
  593         } 
else if (errno != ENOENT) {
 
  594                 bool fatal = (errno != EACCES && errno != ELOOP &&
 
  595                               errno != ENAMETOOLONG && errno != ENOTDIR &&
 
  597                 fprintf(stderr, 
"%s: failed to open %s: %s\n",
 
  598                         progname, FUSE_CONF, strerror(errno));
 
  604 static int begins_with(
const char *s, 
const char *beg)
 
  606         if (strncmp(s, beg, strlen(beg)) == 0)
 
  619 static struct mount_flags mount_flags[] = {
 
  620         {
"rw",      MS_RDONLY,      0, 1},
 
  621         {
"ro",      MS_RDONLY,      1, 1},
 
  622         {
"suid",    MS_NOSUID,      0, 0},
 
  623         {
"nosuid",  MS_NOSUID,      1, 1},
 
  624         {
"dev",     MS_NODEV,       0, 0},
 
  625         {
"nodev",   MS_NODEV,       1, 1},
 
  626         {
"exec",    MS_NOEXEC,      0, 1},
 
  627         {
"noexec",  MS_NOEXEC,      1, 1},
 
  628         {
"async",   MS_SYNCHRONOUS, 0, 1},
 
  629         {
"sync",    MS_SYNCHRONOUS, 1, 1},
 
  630         {
"atime",   MS_NOATIME,     0, 1},
 
  631         {
"noatime", MS_NOATIME,     1, 1},
 
  632         {
"diratime",        MS_NODIRATIME,  0, 1},
 
  633         {
"nodiratime",      MS_NODIRATIME,  1, 1},
 
  634         {
"lazytime",        MS_LAZYTIME,    1, 1},
 
  635         {
"nolazytime",      MS_LAZYTIME,    0, 1},
 
  636         {
"relatime",        MS_RELATIME,    1, 1},
 
  637         {
"norelatime",      MS_RELATIME,    0, 1},
 
  638         {
"strictatime",     MS_STRICTATIME, 1, 1},
 
  639         {
"nostrictatime",   MS_STRICTATIME, 0, 1},
 
  640         {
"dirsync", MS_DIRSYNC,     1, 1},
 
  644 static int find_mount_flag(
const char *s, 
unsigned len, 
int *on, 
int *flag)
 
  648         for (i = 0; mount_flags[i].opt != NULL; i++) {
 
  649                 const char *opt = mount_flags[i].opt;
 
  650                 if (strlen(opt) == len && strncmp(opt, s, len) == 0) {
 
  651                         *on = mount_flags[i].on;
 
  652                         *flag = mount_flags[i].flag;
 
  653                         if (!mount_flags[i].safe && getuid() != 0) {
 
  656                                         "%s: unsafe option %s ignored\n",
 
  665 static int add_option(
char **optsp, 
const char *opt, 
unsigned expand)
 
  669                 newopts = strdup(opt);
 
  671                 unsigned oldsize = strlen(*optsp);
 
  672                 unsigned newsize = oldsize + 1 + strlen(opt) + expand + 1;
 
  673                 newopts = (
char *) realloc(*optsp, newsize);
 
  675                         sprintf(newopts + oldsize, 
",%s", opt);
 
  677         if (newopts == NULL) {
 
  678                 fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
  685 static int get_mnt_opts(
int flags, 
char *opts, 
char **mnt_optsp)
 
  690         if (!(flags & MS_RDONLY) && add_option(mnt_optsp, 
"rw", 0) == -1)
 
  693         for (i = 0; mount_flags[i].opt != NULL; i++) {
 
  694                 if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
 
  695                     add_option(mnt_optsp, mount_flags[i].opt, 0) == -1)
 
  699         if (add_option(mnt_optsp, opts, 0) == -1)
 
  702         l = strlen(*mnt_optsp);
 
  703         if ((*mnt_optsp)[l-1] == 
',')
 
  704                 (*mnt_optsp)[l-1] = 
'\0';
 
  706                 const char *user = get_user_name();
 
  710                 if (add_option(mnt_optsp, 
"user=", strlen(user)) == -1)
 
  712                 strcat(*mnt_optsp, user);
 
  717 static int opt_eq(
const char *s, 
unsigned len, 
const char *opt)
 
  719         if(strlen(opt) == len && strncmp(s, opt, len) == 0)
 
  725 static int get_string_opt(
const char *s, 
unsigned len, 
const char *opt,
 
  729         unsigned opt_len = strlen(opt);
 
  734         *val = (
char *) malloc(len - opt_len + 1);
 
  736                 fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
  743         for (i = 0; i < len; i++) {
 
  744                 if (s[i] == 
'\\' && i + 1 < len)
 
  757 static int mount_notrunc(
const char *source, 
const char *target,
 
  758                          const char *filesystemtype, 
unsigned long mountflags,
 
  760         if (strlen(data) > sysconf(_SC_PAGESIZE) - 1) {
 
  761                 fprintf(stderr, 
"%s: mount options too long\n", progname);
 
  765         return mount(source, target, filesystemtype, mountflags, data);
 
  769 static int do_mount(
const char *mnt, 
const char **typep, mode_t rootmode,
 
  770                     int fd, 
const char *opts, 
const char *dev, 
char **sourcep,
 
  774         int flags = MS_NOSUID | MS_NODEV;
 
  776         char *mnt_opts = NULL;
 
  780         char *subtype = NULL;
 
  785         optbuf = (
char *) malloc(strlen(opts) + 128);
 
  787                 fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
  791         for (s = opts, d = optbuf; *s;) {
 
  793                 const char *fsname_str = 
"fsname=";
 
  794                 const char *subtype_str = 
"subtype=";
 
  795                 bool escape_ok = begins_with(s, fsname_str) ||
 
  796                                  begins_with(s, subtype_str);
 
  797                 for (len = 0; s[len]; len++) {
 
  798                         if (escape_ok && s[len] == 
'\\' && s[len + 1])
 
  800                         else if (s[len] == 
',')
 
  803                 if (begins_with(s, fsname_str)) {
 
  804                         if (!get_string_opt(s, len, fsname_str, &fsname))
 
  806                 } 
else if (begins_with(s, subtype_str)) {
 
  807                         if (!get_string_opt(s, len, subtype_str, &subtype))
 
  809                 } 
else if (opt_eq(s, len, 
"blkdev")) {
 
  812                                         "%s: option blkdev is privileged\n",
 
  817                 } 
else if (opt_eq(s, len, 
"auto_unmount")) {
 
  819                 } 
else if (!opt_eq(s, len, 
"nonempty") &&
 
  820                            !begins_with(s, 
"fd=") &&
 
  821                            !begins_with(s, 
"rootmode=") &&
 
  822                            !begins_with(s, 
"user_id=") &&
 
  823                            !begins_with(s, 
"group_id=")) {
 
  827                         if (opt_eq(s, len, 
"large_read")) {
 
  828                                 struct utsname utsname;
 
  830                                 res = uname(&utsname);
 
  832                                     sscanf(utsname.release, 
"%u.%u",
 
  833                                            &kmaj, &kmin) == 2 &&
 
  834                                     (kmaj > 2 || (kmaj == 2 && kmin > 4))) {
 
  835                                         fprintf(stderr, 
"%s: note: 'large_read' mount option is deprecated for %i.%i kernels\n", progname, kmaj, kmin);
 
  839                         if (getuid() != 0 && !user_allow_other &&
 
  840                             (opt_eq(s, len, 
"allow_other") ||
 
  841                              opt_eq(s, len, 
"allow_root"))) {
 
  842                                 fprintf(stderr, 
"%s: option %.*s only allowed if 'user_allow_other' is set in %s\n", progname, len, s, FUSE_CONF);
 
  846                                 if (find_mount_flag(s, len, &on, &flag)) {
 
  851                                 } 
else if (opt_eq(s, len, 
"default_permissions") ||
 
  852                                            opt_eq(s, len, 
"allow_other") ||
 
  853                                            begins_with(s, 
"max_read=") ||
 
  854                                            begins_with(s, 
"blksize=")) {
 
  859                                         fprintf(stderr, 
"%s: unknown option '%.*s'\n", progname, len, s);
 
  869         res = get_mnt_opts(flags, optbuf, &mnt_opts);
 
  873         sprintf(d, 
"fd=%i,rootmode=%o,user_id=%u,group_id=%u",
 
  874                 fd, rootmode, getuid(), getgid());
 
  876         source = malloc((fsname ? strlen(fsname) : 0) +
 
  877                         (subtype ? strlen(subtype) : 0) + strlen(dev) + 32);
 
  879         type = malloc((subtype ? strlen(subtype) : 0) + 32);
 
  880         if (!type || !source) {
 
  881                 fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
  886                 sprintf(type, 
"%s.%s", blkdev ? 
"fuseblk" : 
"fuse", subtype);
 
  888                 strcpy(type, blkdev ? 
"fuseblk" : 
"fuse");
 
  891                 strcpy(source, fsname);
 
  893                 strcpy(source, subtype ? subtype : dev);
 
  895         res = mount_notrunc(source, mnt, type, flags, optbuf);
 
  896         if (res == -1 && errno == ENODEV && subtype) {
 
  898                 strcpy(type, blkdev ? 
"fuseblk" : 
"fuse");
 
  901                                 sprintf(source, 
"%s#%s", subtype, fsname);
 
  903                         strcpy(source, type);
 
  906                 res = mount_notrunc(source, mnt, type, flags, optbuf);
 
  908         if (res == -1 && errno == EINVAL) {
 
  910                 sprintf(d, 
"fd=%i,rootmode=%o,user_id=%u",
 
  911                         fd, rootmode, getuid());
 
  912                 res = mount_notrunc(source, mnt, type, flags, optbuf);
 
  915                 int errno_save = errno;
 
  916                 if (blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk())
 
  917                         fprintf(stderr, 
"%s: 'fuseblk' support missing\n",
 
  920                         fprintf(stderr, 
"%s: mount failed: %s\n", progname,
 
  921                                 strerror(errno_save));
 
  926         *mnt_optsp = mnt_opts;
 
  942 static int check_perm(
const char **mntp, 
struct stat *stbuf, 
int *mountpoint_fd)
 
  945         const char *mnt = *mntp;
 
  946         const char *origmnt = mnt;
 
  947         struct statfs fs_buf;
 
  950         res = lstat(mnt, stbuf);
 
  952                 fprintf(stderr, 
"%s: failed to access mountpoint %s: %s\n",
 
  953                         progname, mnt, strerror(errno));
 
  961         if (S_ISDIR(stbuf->st_mode)) {
 
  965                                 "%s: failed to chdir to mountpoint: %s\n",
 
  966                                 progname, strerror(errno));
 
  970                 res = lstat(mnt, stbuf);
 
  973                                 "%s: failed to access mountpoint %s: %s\n",
 
  974                                 progname, origmnt, strerror(errno));
 
  978                 if ((stbuf->st_mode & S_ISVTX) && stbuf->st_uid != getuid()) {
 
  979                         fprintf(stderr, 
"%s: mountpoint %s not owned by user\n",
 
  984                 res = access(mnt, W_OK);
 
  986                         fprintf(stderr, 
"%s: user has no write access to mountpoint %s\n",
 
  990         } 
else if (S_ISREG(stbuf->st_mode)) {
 
  991                 static char procfile[256];
 
  992                 *mountpoint_fd = open(mnt, O_WRONLY);
 
  993                 if (*mountpoint_fd == -1) {
 
  994                         fprintf(stderr, 
"%s: failed to open %s: %s\n",
 
  995                                 progname, mnt, strerror(errno));
 
  998                 res = fstat(*mountpoint_fd, stbuf);
 
 1001                                 "%s: failed to access mountpoint %s: %s\n",
 
 1002                                 progname, mnt, strerror(errno));
 
 1005                 if (!S_ISREG(stbuf->st_mode)) {
 
 1007                                 "%s: mountpoint %s is no longer a regular file\n",
 
 1012                 sprintf(procfile, 
"/proc/self/fd/%i", *mountpoint_fd);
 
 1016                         "%s: mountpoint %s is not a directory or a regular file\n",
 
 1027         if (statfs(*mntp, &fs_buf)) {
 
 1028                 fprintf(stderr, 
"%s: failed to access mountpoint %s: %s\n",
 
 1029                         progname, mnt, strerror(errno));
 
 1038         typeof(fs_buf.f_type) f_type_whitelist[] = {
 
 1068                 0x736675005346544e ,
 
 1072         for (i = 0; i < 
sizeof(f_type_whitelist)/
sizeof(f_type_whitelist[0]); i++) {
 
 1073                 if (f_type_whitelist[i] == fs_buf.f_type)
 
 1077         fprintf(stderr, 
"%s: mounting over filesystem type %#010lx is forbidden\n",
 
 1078                 progname, (
unsigned long)fs_buf.f_type);
 
 1082 static int try_open(
const char *dev, 
char **devp, 
int silent)
 
 1084         int fd = open(dev, O_RDWR);
 
 1086                 *devp = strdup(dev);
 
 1087                 if (*devp == NULL) {
 
 1088                         fprintf(stderr, 
"%s: failed to allocate memory\n",
 
 1093         } 
else if (errno == ENODEV ||
 
 1097                 fprintf(stderr, 
"%s: failed to open %s: %s\n", progname, dev,
 
 1103 static int try_open_fuse_device(
char **devp)
 
 1108         fd = try_open(FUSE_DEV, devp, 0);
 
 1113 static int open_fuse_device(
char **devp)
 
 1115         int fd = try_open_fuse_device(devp);
 
 1120                 "%s: fuse device not found, try 'modprobe fuse' first\n",
 
 1127 static int mount_fuse(
const char *mnt, 
const char *opts, 
const char **type)
 
 1133         char *source = NULL;
 
 1134         char *mnt_opts = NULL;
 
 1135         const char *real_mnt = mnt;
 
 1136         int mountpoint_fd = -1;
 
 1138         fd = open_fuse_device(&dev);
 
 1145         if (getuid() != 0 && mount_max != -1) {
 
 1146                 int mount_count = count_fuse_fs();
 
 1147                 if (mount_count >= mount_max) {
 
 1148                         fprintf(stderr, 
"%s: too many FUSE filesystems mounted; mount_max=N can be set in %s\n", progname, FUSE_CONF);
 
 1153         res = check_perm(&real_mnt, &stbuf, &mountpoint_fd);
 
 1156                 res = do_mount(real_mnt, type, stbuf.st_mode & S_IFMT,
 
 1157                                fd, opts, dev, &source, &mnt_opts);
 
 1159         if (mountpoint_fd != -1)
 
 1160                 close(mountpoint_fd);
 
 1167                 fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
 1171         if (geteuid() == 0) {
 
 1172                 res = add_mount(source, mnt, *type, mnt_opts);
 
 1192 static int send_fd(
int sock_fd, 
int fd)
 
 1196         struct cmsghdr *p_cmsg;
 
 1198         size_t cmsgbuf[CMSG_SPACE(
sizeof(fd)) / 
sizeof(size_t)];
 
 1202         msg.msg_control = cmsgbuf;
 
 1203         msg.msg_controllen = 
sizeof(cmsgbuf);
 
 1204         p_cmsg = CMSG_FIRSTHDR(&msg);
 
 1205         p_cmsg->cmsg_level = SOL_SOCKET;
 
 1206         p_cmsg->cmsg_type = SCM_RIGHTS;
 
 1207         p_cmsg->cmsg_len = CMSG_LEN(
sizeof(fd));
 
 1208         p_fds = (
int *) CMSG_DATA(p_cmsg);
 
 1210         msg.msg_controllen = p_cmsg->cmsg_len;
 
 1211         msg.msg_name = NULL;
 
 1212         msg.msg_namelen = 0;
 
 1218         vec.iov_base = &sendchar;
 
 1219         vec.iov_len = 
sizeof(sendchar);
 
 1220         while ((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR);
 
 1222                 perror(
"sending file descriptor");
 
 1234 static int recheck_ENOTCONN_as_owner(
const char *mnt)
 
 1238                 perror(
"fuse: recheck_ENOTCONN_as_owner can't fork");
 
 1239                 _exit(EXIT_FAILURE);
 
 1240         } 
else if(pid == 0) {
 
 1241                 uid_t uid = getuid();
 
 1242                 gid_t gid = getgid();
 
 1243                 if(setresgid(gid, gid, gid) == -1) {
 
 1244                         perror(
"fuse: can't set resgid");
 
 1245                         _exit(EXIT_FAILURE);
 
 1247                 if(setresuid(uid, uid, uid) == -1) {
 
 1248                         perror(
"fuse: can't set resuid");
 
 1249                         _exit(EXIT_FAILURE);
 
 1252                 int fd = open(mnt, O_RDONLY);
 
 1253                 if(fd == -1 && errno == ENOTCONN)
 
 1254                         _exit(EXIT_SUCCESS);
 
 1256                         _exit(EXIT_FAILURE);
 
 1259                 int res = waitpid(pid, &status, 0);
 
 1261                         perror(
"fuse: waiting for child failed");
 
 1262                         _exit(EXIT_FAILURE);
 
 1264                 return WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS;
 
 1284 static int should_auto_unmount(
const char *mnt, 
const char *type)
 
 1293         fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
 1297         if (chdir_to_parent(copy, &last) == -1)
 
 1299         if (check_is_mount(last, mnt, type) == -1)
 
 1302         fd = open(mnt, O_RDONLY);
 
 1312                         result = recheck_ENOTCONN_as_owner(mnt);
 
 1324 static void usage(
void)
 
 1326         printf(
"%s: [options] mountpoint\n" 
 1329                " -V                 print version\n" 
 1330                " -o opt[,opt...]    mount options\n" 
 1333                " -z                 lazy unmount\n",
 
 1338 static void show_version(
void)
 
 1340         printf(
"fusermount3 version: %s\n", PACKAGE_VERSION);
 
 1344 int main(
int argc, 
char *argv[])
 
 1352         static int unmount = 0;
 
 1353         static int lazy = 0;
 
 1354         static int quiet = 0;
 
 1357         const char *opts = 
"";
 
 1358         const char *type = NULL;
 
 1359         int setup_auto_unmount_only = 0;
 
 1361         static const struct option long_opts[] = {
 
 1362                 {
"unmount", no_argument, NULL, 
'u'},
 
 1365                 {
"auto-unmount", no_argument, NULL, 
'U'},
 
 1366                 {
"lazy",    no_argument, NULL, 
'z'},
 
 1367                 {
"quiet",   no_argument, NULL, 
'q'},
 
 1368                 {
"help",    no_argument, NULL, 
'h'},
 
 1369                 {
"version", no_argument, NULL, 
'V'},
 
 1370                 {
"options", required_argument, NULL, 
'o'},
 
 1373         progname = strdup(argc > 0 ? argv[0] : 
"fusermount");
 
 1374         if (progname == NULL) {
 
 1375                 fprintf(stderr, 
"%s: failed to allocate memory\n", argv[0]);
 
 1379         while ((ch = getopt_long(argc, argv, 
"hVo:uzq", long_opts,
 
 1400                         setup_auto_unmount_only = 1;
 
 1415         if (lazy && !unmount) {
 
 1416                 fprintf(stderr, 
"%s: -z can only be used with -u\n", progname);
 
 1420         if (optind >= argc) {
 
 1421                 fprintf(stderr, 
"%s: missing mountpoint argument\n", progname);
 
 1423         } 
else if (argc > optind + 1) {
 
 1424                 fprintf(stderr, 
"%s: extra arguments after the mountpoint\n",
 
 1429         origmnt = argv[optind];
 
 1432         mnt = fuse_mnt_resolve_path(progname, origmnt);
 
 1436                         fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
 1445         if (!setup_auto_unmount_only && unmount)
 
 1448         commfd = getenv(FUSE_COMMFD_ENV);
 
 1449         if (commfd == NULL) {
 
 1450                 fprintf(stderr, 
"%s: old style mounting not supported\n",
 
 1457                 struct stat statbuf;
 
 1458                 fstat(cfd, &statbuf);
 
 1459                 if(!S_ISSOCK(statbuf.st_mode)) {
 
 1461                                 "%s: file descriptor %i is not a socket, can't send fuse fd\n",
 
 1467         if (setup_auto_unmount_only)
 
 1468                 goto wait_for_auto_unmount;
 
 1470         fd = mount_fuse(mnt, opts, &type);
 
 1474         res = send_fd(cfd, fd);
 
 1476                 umount2(mnt, MNT_DETACH); 
 
 1481         if (!auto_unmount) {
 
 1487 wait_for_auto_unmount:
 
 1495                 fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
 1499         sigfillset(&sigset);
 
 1500         sigprocmask(SIG_BLOCK, &sigset, NULL);
 
 1506                 unsigned char buf[16];
 
 1507                 int n = recv(cfd, buf, 
sizeof(buf), 0);
 
 1518         if (!should_auto_unmount(mnt, type)) {
 
 1524                 res = unmount_fuse(mnt, quiet, lazy);
 
 1526                 res = umount2(mnt, lazy ? UMOUNT_DETACH : 0);
 
 1527                 if (res == -1 && !quiet)
 
 1529                                 "%s: failed to unmount %s: %s\n",
 
 1530                                 progname, mnt, strerror(errno));