2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
15 static char *progname
;
17 static char *xstrdup(const char *s
)
21 fprintf(stderr
, "%s: failed to allocate memory\n", progname
);
27 static void *xrealloc(void *oldptr
, size_t size
)
29 void *ptr
= realloc(oldptr
, size
);
31 fprintf(stderr
, "%s: failed to allocate memory\n", progname
);
37 static void add_arg(char **cmdp
, const char *opt
)
39 size_t optlen
= strlen(opt
);
40 size_t cmdlen
= *cmdp
? strlen(*cmdp
) : 0;
41 char *cmd
= xrealloc(*cmdp
, cmdlen
+ optlen
* 4 + 4);
62 static char *add_option(const char *opt
, char *options
)
64 int oldlen
= options
? strlen(options
) : 0;
66 options
= xrealloc(options
, oldlen
+ 1 + strlen(opt
) + 1);
76 int main(int argc
, char *argv
[])
80 const char *mountpoint
;
90 basename
= strrchr(argv
[0], '/');
96 if (strncmp(basename
, "mount.fuse.", 11) == 0)
98 if (strncmp(basename
, "mount.fuseblk.", 14) == 0)
101 if (type
&& !type
[0])
106 "usage: %s %s destination [-t type] [-o opt[,opts...]]\n",
107 progname
, type
? "source" : "type#[source]");
115 mountpoint
= argv
[2];
117 for (i
= 3; i
< argc
; i
++) {
118 if (strcmp(argv
[i
], "-v") == 0) {
120 } else if (strcmp(argv
[i
], "-t") == 0) {
125 "%s: missing argument to option '-t'\n",
130 if (strncmp(type
, "fuse.", 5) == 0)
132 else if (strncmp(type
, "fuseblk.", 8) == 0)
137 "%s: empty type given as argument to option '-t'\n",
141 } else if (strcmp(argv
[i
], "-o") == 0) {
148 opts
= xstrdup(argv
[i
]);
149 opt
= strtok(opts
, ",");
153 const char *ignore_opts
[] = { "",
161 if (strncmp(opt
, "setuid=", 7) == 0) {
162 setuid
= xstrdup(opt
+ 7);
165 for (j
= 0; ignore_opts
[j
]; j
++)
166 if (strcmp(opt
, ignore_opts
[j
]) == 0)
170 if (strcmp(opt
, "nodev") == 0)
172 else if (strcmp(opt
, "nosuid") == 0)
175 options
= add_option(opt
, options
);
177 opt
= strtok(NULL
, ",");
183 options
= add_option("dev", options
);
185 options
= add_option("suid", options
);
189 type
= xstrdup(source
);
190 source
= strchr(type
, '#');
194 fprintf(stderr
, "%s: empty filesystem type\n",
199 fprintf(stderr
, "%s: empty source\n", progname
);
204 add_arg(&command
, type
);
206 add_arg(&command
, source
);
207 add_arg(&command
, mountpoint
);
209 add_arg(&command
, "-o");
210 add_arg(&command
, options
);
213 if (setuid
&& setuid
[0]) {
214 char *sucommand
= command
;
216 add_arg(&command
, "su");
217 add_arg(&command
, "-");
218 add_arg(&command
, setuid
);
219 add_arg(&command
, "-c");
220 add_arg(&command
, sucommand
);
221 } else if (!getenv("HOME")) {
222 /* Hack to make filesystems work in the boot environment */
223 setenv("HOME", "/root", 0);
226 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
227 fprintf(stderr
, "%s: failed to execute /bin/sh: %s\n", progname
,