You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
7.4 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2005-2008 Csaba Henk <csaba.henk@creo.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB.
  6. */
  7. #include "fuse_i.h"
  8. #include "fuse_misc.h"
  9. #include "fuse_opt.h"
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <sys/sysctl.h>
  13. #include <sys/user.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <stddef.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <paths.h>
  22. #include <limits.h>
  23. #define FUSERMOUNT_PROG "mount_fusefs"
  24. #define FUSE_DEV_TRUNK "/dev/fuse"
  25. enum {
  26. KEY_ALLOW_ROOT,
  27. KEY_RO,
  28. KEY_HELP,
  29. KEY_VERSION,
  30. KEY_KERN
  31. };
  32. struct mount_opts {
  33. int allow_other;
  34. int allow_root;
  35. int ishelp;
  36. char *kernel_opts;
  37. };
  38. #define FUSE_DUAL_OPT_KEY(templ, key) \
  39. FUSE_OPT_KEY(templ, key), FUSE_OPT_KEY("no" templ, key)
  40. static const struct fuse_opt fuse_mount_opts[] = {
  41. { "allow_other", offsetof(struct mount_opts, allow_other), 1 },
  42. { "allow_root", offsetof(struct mount_opts, allow_root), 1 },
  43. FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT),
  44. FUSE_OPT_KEY("-r", KEY_RO),
  45. FUSE_OPT_KEY("-h", KEY_HELP),
  46. FUSE_OPT_KEY("--help", KEY_HELP),
  47. FUSE_OPT_KEY("-V", KEY_VERSION),
  48. FUSE_OPT_KEY("--version", KEY_VERSION),
  49. /* standard FreeBSD mount options */
  50. FUSE_DUAL_OPT_KEY("dev", KEY_KERN),
  51. FUSE_DUAL_OPT_KEY("async", KEY_KERN),
  52. FUSE_DUAL_OPT_KEY("atime", KEY_KERN),
  53. FUSE_DUAL_OPT_KEY("dev", KEY_KERN),
  54. FUSE_DUAL_OPT_KEY("exec", KEY_KERN),
  55. FUSE_DUAL_OPT_KEY("suid", KEY_KERN),
  56. FUSE_DUAL_OPT_KEY("symfollow", KEY_KERN),
  57. FUSE_DUAL_OPT_KEY("rdonly", KEY_KERN),
  58. FUSE_DUAL_OPT_KEY("sync", KEY_KERN),
  59. FUSE_DUAL_OPT_KEY("union", KEY_KERN),
  60. FUSE_DUAL_OPT_KEY("userquota", KEY_KERN),
  61. FUSE_DUAL_OPT_KEY("groupquota", KEY_KERN),
  62. FUSE_DUAL_OPT_KEY("clusterr", KEY_KERN),
  63. FUSE_DUAL_OPT_KEY("clusterw", KEY_KERN),
  64. FUSE_DUAL_OPT_KEY("suiddir", KEY_KERN),
  65. FUSE_DUAL_OPT_KEY("snapshot", KEY_KERN),
  66. FUSE_DUAL_OPT_KEY("multilabel", KEY_KERN),
  67. FUSE_DUAL_OPT_KEY("acls", KEY_KERN),
  68. FUSE_DUAL_OPT_KEY("force", KEY_KERN),
  69. FUSE_DUAL_OPT_KEY("update", KEY_KERN),
  70. FUSE_DUAL_OPT_KEY("ro", KEY_KERN),
  71. FUSE_DUAL_OPT_KEY("rw", KEY_KERN),
  72. FUSE_DUAL_OPT_KEY("auto", KEY_KERN),
  73. /* options supported under both Linux and FBSD */
  74. FUSE_DUAL_OPT_KEY("allow_other", KEY_KERN),
  75. FUSE_DUAL_OPT_KEY("default_permissions",KEY_KERN),
  76. FUSE_OPT_KEY("max_read=", KEY_KERN),
  77. FUSE_OPT_KEY("subtype=", KEY_KERN),
  78. /* FBSD FUSE specific mount options */
  79. FUSE_DUAL_OPT_KEY("private", KEY_KERN),
  80. FUSE_DUAL_OPT_KEY("neglect_shares", KEY_KERN),
  81. FUSE_DUAL_OPT_KEY("push_symlinks_in", KEY_KERN),
  82. FUSE_OPT_KEY("nosync_unmount", KEY_KERN),
  83. /* stock FBSD mountopt parsing routine lets anything be negated... */
  84. /*
  85. * Linux specific mount options, but let just the mount util
  86. * handle them
  87. */
  88. FUSE_OPT_KEY("fsname=", KEY_KERN),
  89. FUSE_OPT_KEY("nonempty", KEY_KERN),
  90. FUSE_OPT_KEY("large_read", KEY_KERN),
  91. FUSE_OPT_END
  92. };
  93. static void mount_help(void)
  94. {
  95. fprintf(stderr,
  96. " -o allow_root allow access to root\n"
  97. );
  98. system(FUSERMOUNT_PROG " --help");
  99. fputc('\n', stderr);
  100. }
  101. static void mount_version(void)
  102. {
  103. system(FUSERMOUNT_PROG " --version");
  104. }
  105. static int fuse_mount_opt_proc(void *data, const char *arg, int key,
  106. struct fuse_args *outargs)
  107. {
  108. struct mount_opts *mo = data;
  109. switch (key) {
  110. case KEY_ALLOW_ROOT:
  111. if (fuse_opt_add_opt(&mo->kernel_opts, "allow_other") == -1 ||
  112. fuse_opt_add_arg(outargs, "-oallow_root") == -1)
  113. return -1;
  114. return 0;
  115. case KEY_RO:
  116. arg = "ro";
  117. /* fall through */
  118. case KEY_KERN:
  119. return fuse_opt_add_opt(&mo->kernel_opts, arg);
  120. case KEY_HELP:
  121. mount_help();
  122. mo->ishelp = 1;
  123. break;
  124. case KEY_VERSION:
  125. mount_version();
  126. mo->ishelp = 1;
  127. break;
  128. }
  129. return 1;
  130. }
  131. static void do_unmount(char *dev, int fd)
  132. {
  133. char device_path[SPECNAMELEN + 12];
  134. const char *argv[4];
  135. const char umount_cmd[] = "/sbin/umount";
  136. pid_t pid;
  137. snprintf(device_path, SPECNAMELEN + 12, _PATH_DEV "%s", dev);
  138. argv[0] = umount_cmd;
  139. argv[1] = "-f";
  140. argv[2] = device_path;
  141. argv[3] = NULL;
  142. pid = fork();
  143. if (pid == -1)
  144. return;
  145. if (pid == 0) {
  146. close(fd);
  147. execvp(umount_cmd, (char **)argv);
  148. exit(1);
  149. }
  150. waitpid(pid, NULL, 0);
  151. }
  152. void fuse_kern_unmount(const char *mountpoint, int fd)
  153. {
  154. char *ep, dev[128];
  155. struct stat sbuf;
  156. (void)mountpoint;
  157. if (fstat(fd, &sbuf) == -1)
  158. goto out;
  159. devname_r(sbuf.st_rdev, S_IFCHR, dev, 128);
  160. if (strncmp(dev, "fuse", 4))
  161. goto out;
  162. strtol(dev + 4, &ep, 10);
  163. if (*ep != '\0')
  164. goto out;
  165. do_unmount(dev, fd);
  166. out:
  167. close(fd);
  168. }
  169. /* Check if kernel is doing init in background */
  170. static int init_backgrounded(void)
  171. {
  172. unsigned ibg, len;
  173. len = sizeof(ibg);
  174. if (sysctlbyname("vfs.fuse.init_backgrounded", &ibg, &len, NULL, 0))
  175. return 0;
  176. return ibg;
  177. }
  178. static int fuse_mount_core(const char *mountpoint, const char *opts)
  179. {
  180. const char *mountprog = FUSERMOUNT_PROG;
  181. int fd;
  182. char *fdnam, *dev;
  183. pid_t pid, cpid;
  184. int status;
  185. fdnam = getenv("FUSE_DEV_FD");
  186. if (fdnam) {
  187. char *ep;
  188. fd = strtol(fdnam, &ep, 10);
  189. if (*ep != '\0') {
  190. fprintf(stderr, "invalid value given in FUSE_DEV_FD\n");
  191. return -1;
  192. }
  193. if (fd < 0)
  194. return -1;
  195. goto mount;
  196. }
  197. dev = getenv("FUSE_DEV_NAME");
  198. if (! dev)
  199. dev = (char *)FUSE_DEV_TRUNK;
  200. if ((fd = open(dev, O_RDWR)) < 0) {
  201. perror("fuse: failed to open fuse device");
  202. return -1;
  203. }
  204. mount:
  205. if (getenv("FUSE_NO_MOUNT") || ! mountpoint)
  206. goto out;
  207. pid = fork();
  208. cpid = pid;
  209. if (pid == -1) {
  210. perror("fuse: fork() failed");
  211. close(fd);
  212. return -1;
  213. }
  214. if (pid == 0) {
  215. if (! init_backgrounded()) {
  216. /*
  217. * If init is not backgrounded, we have to
  218. * call the mount util backgrounded, to avoid
  219. * deadlock.
  220. */
  221. pid = fork();
  222. if (pid == -1) {
  223. perror("fuse: fork() failed");
  224. close(fd);
  225. exit(1);
  226. }
  227. }
  228. if (pid == 0) {
  229. const char *argv[32];
  230. int a = 0;
  231. if (! fdnam && asprintf(&fdnam, "%d", fd) == -1) {
  232. perror("fuse: failed to assemble mount arguments");
  233. exit(1);
  234. }
  235. argv[a++] = mountprog;
  236. if (opts) {
  237. argv[a++] = "-o";
  238. argv[a++] = opts;
  239. }
  240. argv[a++] = fdnam;
  241. argv[a++] = mountpoint;
  242. argv[a++] = NULL;
  243. execvp(mountprog, (char **) argv);
  244. perror("fuse: failed to exec mount program");
  245. exit(1);
  246. }
  247. exit(0);
  248. }
  249. if (waitpid(cpid, &status, 0) == -1 || WEXITSTATUS(status) != 0) {
  250. perror("fuse: failed to mount file system");
  251. close(fd);
  252. return -1;
  253. }
  254. out:
  255. return fd;
  256. }
  257. int fuse_kern_mount(const char *mountpoint, struct fuse_args *args)
  258. {
  259. struct mount_opts mo;
  260. int res = -1;
  261. memset(&mo, 0, sizeof(mo));
  262. /* mount util should not try to spawn the daemon */
  263. setenv("MOUNT_FUSEFS_SAFE", "1", 1);
  264. /* to notify the mount util it's called from lib */
  265. setenv("MOUNT_FUSEFS_CALL_BY_LIB", "1", 1);
  266. if (args &&
  267. fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1)
  268. return -1;
  269. if (mo.allow_other && mo.allow_root) {
  270. fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n");
  271. goto out;
  272. }
  273. if (mo.ishelp)
  274. return 0;
  275. res = fuse_mount_core(mountpoint, mo.kernel_opts);
  276. out:
  277. free(mo.kernel_opts);
  278. return res;
  279. }