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.

324 lines
6.8 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("large_read", KEY_KERN),
  86. FUSE_OPT_END
  87. };
  88. static void mount_help(void)
  89. {
  90. system(FUSERMOUNT_PROG " --help");
  91. fputc('\n', stderr);
  92. }
  93. static void mount_version(void)
  94. {
  95. system(FUSERMOUNT_PROG " --version");
  96. }
  97. static int fuse_mount_opt_proc(void *data, const char *arg, int key,
  98. struct fuse_args *outargs)
  99. {
  100. struct mount_opts *mo = data;
  101. switch (key) {
  102. case KEY_RO:
  103. arg = "ro";
  104. /* fall through */
  105. case KEY_KERN:
  106. return fuse_opt_add_opt(&mo->kernel_opts, arg);
  107. case KEY_HELP:
  108. mount_help();
  109. mo->ishelp = 1;
  110. break;
  111. case KEY_VERSION:
  112. mount_version();
  113. mo->ishelp = 1;
  114. break;
  115. }
  116. return 1;
  117. }
  118. static void do_unmount(char *dev, int fd)
  119. {
  120. char device_path[SPECNAMELEN + 12];
  121. const char *argv[4];
  122. const char umount_cmd[] = "/sbin/umount";
  123. pid_t pid;
  124. snprintf(device_path, SPECNAMELEN + 12, _PATH_DEV "%s", dev);
  125. argv[0] = umount_cmd;
  126. argv[1] = "-f";
  127. argv[2] = device_path;
  128. argv[3] = NULL;
  129. pid = fork();
  130. if (pid == -1)
  131. return;
  132. if (pid == 0) {
  133. close(fd);
  134. execvp(umount_cmd, (char **)argv);
  135. exit(1);
  136. }
  137. waitpid(pid, NULL, 0);
  138. }
  139. void fuse_kern_unmount(const char *mountpoint, int fd)
  140. {
  141. char *ep, dev[128];
  142. struct stat sbuf;
  143. (void)mountpoint;
  144. if (fstat(fd, &sbuf) == -1)
  145. goto out;
  146. devname_r(sbuf.st_rdev, S_IFCHR, dev, 128);
  147. if (strncmp(dev, "fuse", 4))
  148. goto out;
  149. strtol(dev + 4, &ep, 10);
  150. if (*ep != '\0')
  151. goto out;
  152. do_unmount(dev, fd);
  153. out:
  154. close(fd);
  155. }
  156. /* Check if kernel is doing init in background */
  157. static int init_backgrounded(void)
  158. {
  159. unsigned ibg, len;
  160. len = sizeof(ibg);
  161. if (sysctlbyname("vfs.fuse.init_backgrounded", &ibg, &len, NULL, 0))
  162. return 0;
  163. return ibg;
  164. }
  165. static int fuse_mount_core(const char *mountpoint, const char *opts)
  166. {
  167. const char *mountprog = FUSERMOUNT_PROG;
  168. int fd;
  169. char *fdnam, *dev;
  170. pid_t pid, cpid;
  171. int status;
  172. fdnam = getenv("FUSE_DEV_FD");
  173. if (fdnam) {
  174. char *ep;
  175. fd = strtol(fdnam, &ep, 10);
  176. if (*ep != '\0') {
  177. fprintf(stderr, "invalid value given in FUSE_DEV_FD\n");
  178. return -1;
  179. }
  180. if (fd < 0)
  181. return -1;
  182. goto mount;
  183. }
  184. dev = getenv("FUSE_DEV_NAME");
  185. if (! dev)
  186. dev = (char *)FUSE_DEV_TRUNK;
  187. if ((fd = open(dev, O_RDWR)) < 0) {
  188. perror("fuse: failed to open fuse device");
  189. return -1;
  190. }
  191. mount:
  192. if (getenv("FUSE_NO_MOUNT") || ! mountpoint)
  193. goto out;
  194. pid = fork();
  195. cpid = pid;
  196. if (pid == -1) {
  197. perror("fuse: fork() failed");
  198. close(fd);
  199. return -1;
  200. }
  201. if (pid == 0) {
  202. if (! init_backgrounded()) {
  203. /*
  204. * If init is not backgrounded, we have to
  205. * call the mount util backgrounded, to avoid
  206. * deadlock.
  207. */
  208. pid = fork();
  209. if (pid == -1) {
  210. perror("fuse: fork() failed");
  211. close(fd);
  212. exit(1);
  213. }
  214. }
  215. if (pid == 0) {
  216. const char *argv[32];
  217. int a = 0;
  218. if (! fdnam && asprintf(&fdnam, "%d", fd) == -1) {
  219. perror("fuse: failed to assemble mount arguments");
  220. exit(1);
  221. }
  222. argv[a++] = mountprog;
  223. if (opts) {
  224. argv[a++] = "-o";
  225. argv[a++] = opts;
  226. }
  227. argv[a++] = fdnam;
  228. argv[a++] = mountpoint;
  229. argv[a++] = NULL;
  230. execvp(mountprog, (char **) argv);
  231. perror("fuse: failed to exec mount program");
  232. exit(1);
  233. }
  234. exit(0);
  235. }
  236. if (waitpid(cpid, &status, 0) == -1 || WEXITSTATUS(status) != 0) {
  237. perror("fuse: failed to mount file system");
  238. close(fd);
  239. return -1;
  240. }
  241. out:
  242. return fd;
  243. }
  244. int fuse_kern_mount(const char *mountpoint, struct fuse_args *args)
  245. {
  246. struct mount_opts mo;
  247. int res = -1;
  248. memset(&mo, 0, sizeof(mo));
  249. /* mount util should not try to spawn the daemon */
  250. setenv("MOUNT_FUSEFS_SAFE", "1", 1);
  251. /* to notify the mount util it's called from lib */
  252. setenv("MOUNT_FUSEFS_CALL_BY_LIB", "1", 1);
  253. if (args &&
  254. fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1)
  255. return -1;
  256. if (mo.ishelp)
  257. return 0;
  258. res = fuse_mount_core(mountpoint, mo.kernel_opts);
  259. out:
  260. free(mo.kernel_opts);
  261. return res;
  262. }