11#include "fuse_config.h" 
   12#include "mount_util.h" 
   28#include "fuse_mount_compat.h" 
   31#include <sys/socket.h> 
   32#include <sys/utsname.h> 
   37#define FUSE_COMMFD_ENV         "_FUSE_COMMFD" 
   39#define FUSE_DEV "/dev/fuse" 
   41static const char *progname;
 
   43static int user_allow_other = 0;
 
   44static int mount_max = 1000;
 
   46static int auto_unmount = 0;
 
   48#ifdef GETMNTENT_NEEDS_UNESCAPING 
   53static 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] == 
'\\') {
 
   85static 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 
  101static 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);
 
  112static uid_t oldfsuid;
 
  113static gid_t oldfsgid;
 
  115static void drop_privs(
void)
 
  118                oldfsuid = setfsuid(getuid());
 
  119                oldfsgid = setfsgid(getgid());
 
  123static void restore_privs(
void)
 
  135static 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,
 
  163static 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));
 
  177static int add_mount(
const char *source, 
const char *mnt, 
const char *type,
 
  180        return fuse_mnt_add_mount(progname, source, mnt, type, opts);
 
  183static 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);
 
  271static 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);
 
  340static 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);
 
  358static 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)
 
  387static 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,
 
  435static 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);
 
  480static 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);
 
  491static 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)
 
  513static int count_fuse_fs(
void)
 
  518static int add_mount(
const char *source, 
const char *mnt, 
const char *type,
 
  528static int unmount_fuse(
const char *mnt, 
int quiet, 
int lazy)
 
  531        return fuse_mnt_umount(progname, mnt, mnt, lazy);
 
  535static 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);
 
  548static 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);
 
  561static 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));
 
  604static int begins_with(
const char *s, 
const char *beg)
 
  606        if (strncmp(s, beg, strlen(beg)) == 0)
 
  619static 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},
 
  644static 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",
 
  665static 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);
 
  685static 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);
 
  717static int opt_eq(
const char *s, 
unsigned len, 
const char *opt)
 
  719        if(strlen(opt) == len && strncmp(s, opt, len) == 0)
 
  725static 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)
 
  757static 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);
 
  769static 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;
 
  942static 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[] = {
 
 1069                0x736675005346544e ,
 
 1073        for (i = 0; i < 
sizeof(f_type_whitelist)/
sizeof(f_type_whitelist[0]); i++) {
 
 1074                if (f_type_whitelist[i] == fs_buf.f_type)
 
 1078        fprintf(stderr, 
"%s: mounting over filesystem type %#010lx is forbidden\n",
 
 1079                progname, (
unsigned long)fs_buf.f_type);
 
 1083static int try_open(
const char *dev, 
char **devp, 
int silent)
 
 1085        int fd = open(dev, O_RDWR);
 
 1087                *devp = strdup(dev);
 
 1088                if (*devp == NULL) {
 
 1089                        fprintf(stderr, 
"%s: failed to allocate memory\n",
 
 1094        } 
else if (errno == ENODEV ||
 
 1098                fprintf(stderr, 
"%s: failed to open %s: %s\n", progname, dev,
 
 1104static int try_open_fuse_device(
char **devp)
 
 1109        fd = try_open(FUSE_DEV, devp, 0);
 
 1114static int open_fuse_device(
char **devp)
 
 1116        int fd = try_open_fuse_device(devp);
 
 1121                "%s: fuse device not found, try 'modprobe fuse' first\n",
 
 1128static int mount_fuse(
const char *mnt, 
const char *opts, 
const char **type)
 
 1134        char *source = NULL;
 
 1135        char *mnt_opts = NULL;
 
 1136        const char *real_mnt = mnt;
 
 1137        int mountpoint_fd = -1;
 
 1139        fd = open_fuse_device(&dev);
 
 1146        if (getuid() != 0 && mount_max != -1) {
 
 1147                int mount_count = count_fuse_fs();
 
 1148                if (mount_count >= mount_max) {
 
 1149                        fprintf(stderr, 
"%s: too many FUSE filesystems mounted; mount_max=N can be set in %s\n", progname, FUSE_CONF);
 
 1154        res = check_perm(&real_mnt, &stbuf, &mountpoint_fd);
 
 1157                res = do_mount(real_mnt, type, stbuf.st_mode & S_IFMT,
 
 1158                               fd, opts, dev, &source, &mnt_opts);
 
 1160        if (mountpoint_fd != -1)
 
 1161                close(mountpoint_fd);
 
 1168                fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
 1172        if (geteuid() == 0) {
 
 1173                res = add_mount(source, mnt, *type, mnt_opts);
 
 1193static int send_fd(
int sock_fd, 
int fd)
 
 1197        struct cmsghdr *p_cmsg;
 
 1199        size_t cmsgbuf[CMSG_SPACE(
sizeof(fd)) / 
sizeof(size_t)];
 
 1203        msg.msg_control = cmsgbuf;
 
 1204        msg.msg_controllen = 
sizeof(cmsgbuf);
 
 1205        p_cmsg = CMSG_FIRSTHDR(&msg);
 
 1206        p_cmsg->cmsg_level = SOL_SOCKET;
 
 1207        p_cmsg->cmsg_type = SCM_RIGHTS;
 
 1208        p_cmsg->cmsg_len = CMSG_LEN(
sizeof(fd));
 
 1209        p_fds = (
int *) CMSG_DATA(p_cmsg);
 
 1211        msg.msg_controllen = p_cmsg->cmsg_len;
 
 1212        msg.msg_name = NULL;
 
 1213        msg.msg_namelen = 0;
 
 1219        vec.iov_base = &sendchar;
 
 1220        vec.iov_len = 
sizeof(sendchar);
 
 1221        while ((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR);
 
 1223                perror(
"sending file descriptor");
 
 1235static int recheck_ENOTCONN_as_owner(
const char *mnt)
 
 1239                perror(
"fuse: recheck_ENOTCONN_as_owner can't fork");
 
 1240                _exit(EXIT_FAILURE);
 
 1241        } 
else if(pid == 0) {
 
 1242                uid_t uid = getuid();
 
 1243                gid_t gid = getgid();
 
 1244                if(setresgid(gid, gid, gid) == -1) {
 
 1245                        perror(
"fuse: can't set resgid");
 
 1246                        _exit(EXIT_FAILURE);
 
 1248                if(setresuid(uid, uid, uid) == -1) {
 
 1249                        perror(
"fuse: can't set resuid");
 
 1250                        _exit(EXIT_FAILURE);
 
 1253                int fd = open(mnt, O_RDONLY);
 
 1254                if(fd == -1 && errno == ENOTCONN)
 
 1255                        _exit(EXIT_SUCCESS);
 
 1257                        _exit(EXIT_FAILURE);
 
 1260                int res = waitpid(pid, &status, 0);
 
 1262                        perror(
"fuse: waiting for child failed");
 
 1263                        _exit(EXIT_FAILURE);
 
 1265                return WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS;
 
 1285static int should_auto_unmount(
const char *mnt, 
const char *type)
 
 1294        fprintf(stderr, 
"%s: failed to allocate memory\n", progname);
 
 1298        if (chdir_to_parent(copy, &last) == -1)
 
 1300        if (check_is_mount(last, mnt, type) == -1)
 
 1303        fd = open(mnt, O_RDONLY);
 
 1313                        result = recheck_ENOTCONN_as_owner(mnt);
 
 1325static void usage(
void)
 
 1327        printf(
"%s: [options] mountpoint\n" 
 1330               " -V                 print version\n" 
 1331               " -o opt[,opt...]    mount options\n" 
 1334               " -z                 lazy unmount\n",
 
 1339static void show_version(
void)
 
 1341        printf(
"fusermount3 version: %s\n", PACKAGE_VERSION);
 
 1345int main(
int argc, 
char *argv[])
 
 1353        static int unmount = 0;
 
 1354        static int lazy = 0;
 
 1355        static int quiet = 0;
 
 1358        const char *opts = 
"";
 
 1359        const char *type = NULL;
 
 1360        int setup_auto_unmount_only = 0;
 
 1362        static const struct option long_opts[] = {
 
 1363                {
"unmount", no_argument, NULL, 
'u'},
 
 1366                {
"auto-unmount", no_argument, NULL, 
'U'},
 
 1367                {
"lazy",    no_argument, NULL, 
'z'},
 
 1368                {
"quiet",   no_argument, NULL, 
'q'},
 
 1369                {
"help",    no_argument, NULL, 
'h'},
 
 1370                {
"version", no_argument, NULL, 
'V'},
 
 1371                {
"options", required_argument, NULL, 
'o'},
 
 1374        progname = strdup(argc > 0 ? argv[0] : 
"fusermount");
 
 1375        if (progname == NULL) {
 
 1376                fprintf(stderr, 
"%s: failed to allocate memory\n", argv[0]);
 
 1380        while ((ch = getopt_long(argc, argv, 
"hVo:uzq", long_opts,
 
 1401                        setup_auto_unmount_only = 1;
 
 1416        if (lazy && !unmount) {
 
 1417                fprintf(stderr, 
"%s: -z can only be used with -u\n", progname);
 
 1421        if (optind >= argc) {
 
 1422                fprintf(stderr, 
"%s: missing mountpoint argument\n", progname);
 
 1424        } 
else if (argc > optind + 1) {
 
 1425                fprintf(stderr, 
"%s: extra arguments after the mountpoint\n",
 
 1430        origmnt = argv[optind];
 
 1433        mnt = fuse_mnt_resolve_path(progname, origmnt);
 
 1437                        fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
 1446        if (!setup_auto_unmount_only && unmount)
 
 1449        commfd = getenv(FUSE_COMMFD_ENV);
 
 1450        if (commfd == NULL) {
 
 1451                fprintf(stderr, 
"%s: old style mounting not supported\n",
 
 1458                struct stat statbuf;
 
 1459                fstat(cfd, &statbuf);
 
 1460                if(!S_ISSOCK(statbuf.st_mode)) {
 
 1462                                "%s: file descriptor %i is not a socket, can't send fuse fd\n",
 
 1468        if (setup_auto_unmount_only)
 
 1469                goto wait_for_auto_unmount;
 
 1471        fd = mount_fuse(mnt, opts, &type);
 
 1475        res = send_fd(cfd, fd);
 
 1477                umount2(mnt, MNT_DETACH); 
 
 1482        if (!auto_unmount) {
 
 1488wait_for_auto_unmount:
 
 1496                fprintf(stderr, 
"%s: failed to chdir to '/'\n", progname);
 
 1500        sigfillset(&sigset);
 
 1501        sigprocmask(SIG_BLOCK, &sigset, NULL);
 
 1507                unsigned char buf[16];
 
 1508                int n = recv(cfd, buf, 
sizeof(buf), 0);
 
 1519        if (!should_auto_unmount(mnt, type)) {
 
 1525                res = unmount_fuse(mnt, quiet, lazy);
 
 1527                res = umount2(mnt, lazy ? UMOUNT_DETACH : 0);
 
 1528                if (res == -1 && !quiet)
 
 1530                                "%s: failed to unmount %s: %s\n",
 
 1531                                progname, mnt, strerror(errno));