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.

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