17
17
#include <mntent.h>
18
18
#include <sys/stat.h>
19
19
#include <sys/wait.h>
20
#include <sys/mount.h>
21
22
static int mtab_needs_update(const char *mnt)
25
/* If mtab is within new mount, don't touch it */
26
if (strncmp(mnt, _PATH_MOUNTED, strlen(mnt)) == 0 &&
27
_PATH_MOUNTED[strlen(mnt)] == '/')
30
if (lstat(_PATH_MOUNTED, &stbuf) != -1 && S_ISLNK(stbuf.st_mode))
26
/* If mtab is within new mount, don't touch it */
27
if (strncmp(mnt, _PATH_MOUNTED, strlen(mnt)) == 0 &&
28
_PATH_MOUNTED[strlen(mnt)] == '/')
31
if (lstat(_PATH_MOUNTED, &stbuf) != -1 && S_ISLNK(stbuf.st_mode))
36
37
int fuse_mnt_add_mount(const char *progname, const char *fsname,
37
const char *mnt, const char *type, const char *opts)
38
const char *mnt, const char *type, const char *opts)
42
if (!mtab_needs_update(mnt))
47
fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno));
51
char templ[] = "/tmp/fusermountXXXXXX";
57
* hide in a directory, where mount isn't able to resolve
58
* fsname as a valid path
62
fprintf(stderr, "%s: failed to create temporary directory\n",
67
fprintf(stderr, "%s: failed to chdir to %s: %s\n",
68
progname, tmp, strerror(errno));
72
execl("/bin/mount", "/bin/mount", "-i", "-f", "-t", type, "-o", opts,
74
fprintf(stderr, "%s: failed to execute /bin/mount: %s\n", progname,
78
res = waitpid(res, &status, 0);
80
fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno));
43
if (!mtab_needs_update(mnt))
48
fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno));
52
char templ[] = "/tmp/fusermountXXXXXX";
58
* hide in a directory, where mount isn't able to resolve
59
* fsname as a valid path
64
"%s: failed to create temporary directory\n",
69
fprintf(stderr, "%s: failed to chdir to %s: %s\n",
70
progname, tmp, strerror(errno));
74
execl("/bin/mount", "/bin/mount", "-i", "-f", "-t", type,
75
"-o", opts, fsname, mnt, NULL);
76
fprintf(stderr, "%s: failed to execute /bin/mount: %s\n",
77
progname, strerror(errno));
80
res = waitpid(res, &status, 0);
82
fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno));
89
91
int fuse_mnt_umount(const char *progname, const char *mnt, int lazy)
94
if (!mtab_needs_update(mnt))
99
fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno));
104
execl("/bin/umount", "/bin/umount", "-i", mnt, lazy ? "-l" : NULL,
106
fprintf(stderr, "%s: failed to execute /bin/umount: %s\n", progname,
110
res = waitpid(res, &status, 0);
112
fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno));
96
if (!mtab_needs_update(mnt)) {
97
res = umount2(mnt, lazy ? 2 : 0);
99
fprintf(stderr, "%s: failed to unmount %s: %s\n",
100
progname, mnt, strerror(errno));
106
fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno));
111
execl("/bin/umount", "/bin/umount", "-i", mnt,
112
lazy ? "-l" : NULL, NULL);
113
fprintf(stderr, "%s: failed to execute /bin/umount: %s\n",
114
progname, strerror(errno));
117
res = waitpid(res, &status, 0);
119
fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno));
121
128
char *fuse_mnt_resolve_path(const char *progname, const char *orig)
128
const char *toresolv;
131
fprintf(stderr, "%s: invalid mountpoint '%s'\n", progname, orig);
137
fprintf(stderr, "%s: failed to allocate memory\n", progname);
143
for (end = copy + strlen(copy) - 1; end > copy && *end == '/'; end --);
147
tmp = strrchr(copy, '/');
156
if (strcmp(lastcomp, ".") == 0 || strcmp(lastcomp, "..") == 0) {
163
if (realpath(toresolv, buf) == NULL) {
164
fprintf(stderr, "%s: bad mount point %s: %s\n", progname, orig,
169
if (lastcomp == NULL)
172
dst = (char *) malloc(strlen(buf) + 1 + strlen(lastcomp) + 1);
174
unsigned buflen = strlen(buf);
175
if (buflen && buf[buflen-1] == '/')
176
sprintf(dst, "%s%s", buf, lastcomp);
178
sprintf(dst, "%s/%s", buf, lastcomp);
183
fprintf(stderr, "%s: failed to allocate memory\n", progname);
135
const char *toresolv;
138
fprintf(stderr, "%s: invalid mountpoint '%s'\n", progname,
145
fprintf(stderr, "%s: failed to allocate memory\n", progname);
151
for (end = copy + strlen(copy) - 1; end > copy && *end == '/'; end --);
155
tmp = strrchr(copy, '/');
164
if (strcmp(lastcomp, ".") == 0 || strcmp(lastcomp, "..") == 0) {
171
if (realpath(toresolv, buf) == NULL) {
172
fprintf(stderr, "%s: bad mount point %s: %s\n", progname, orig,
177
if (lastcomp == NULL)
180
dst = (char *) malloc(strlen(buf) + 1 + strlen(lastcomp) + 1);
182
unsigned buflen = strlen(buf);
183
if (buflen && buf[buflen-1] == '/')
184
sprintf(dst, "%s%s", buf, lastcomp);
186
sprintf(dst, "%s/%s", buf, lastcomp);
191
fprintf(stderr, "%s: failed to allocate memory\n", progname);
187
195
int fuse_mnt_check_empty(const char *progname, const char *mnt,
188
mode_t rootmode, off_t rootsize)
196
mode_t rootmode, off_t rootsize)
192
if (S_ISDIR(rootmode)) {
194
DIR *dp = opendir(mnt);
196
fprintf(stderr, "%s: failed to open mountpoint for reading: %s\n",
197
progname, strerror(errno));
200
while ((ent = readdir(dp)) != NULL) {
201
if (strcmp(ent->d_name, ".") != 0 &&
202
strcmp(ent->d_name, "..") != 0) {
212
fprintf(stderr, "%s: mountpoint is not empty\n", progname);
213
fprintf(stderr, "%s: if you are sure this is safe, use the 'nonempty' mount option\n", progname);
200
if (S_ISDIR(rootmode)) {
202
DIR *dp = opendir(mnt);
205
"%s: failed to open mountpoint for reading: %s\n",
206
progname, strerror(errno));
209
while ((ent = readdir(dp)) != NULL) {
210
if (strcmp(ent->d_name, ".") != 0 &&
211
strcmp(ent->d_name, "..") != 0) {
221
fprintf(stderr, "%s: mountpoint is not empty\n", progname);
222
fprintf(stderr, "%s: if you are sure this is safe, use the 'nonempty' mount option\n", progname);
219
228
int fuse_mnt_check_fuseblk(void)
222
FILE *f = fopen("/proc/filesystems", "r");
226
while (fgets(buf, sizeof(buf), f))
227
if (strstr(buf, "fuseblk\n")) {
231
FILE *f = fopen("/proc/filesystems", "r");
235
while (fgets(buf, sizeof(buf), f))
236
if (strstr(buf, "fuseblk\n")) {