aboutsummaryrefslogtreecommitdiff
path: root/lib/mount.c
blob: 98d9e591fd65c7a15cda2ca2c751e50198a79037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
    FUSE: Filesystem in Userspace
    Copyright (C) 2001  Miklos Szeredi (mszeredi@inf.bme.hu)

    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.
*/

#include "fuse_i.h"
#include <linux/fuse.h>

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <mntent.h>

static int do_mount(const char *dev, const char *dir, const char *type,
                    mode_t rootmode, int fd)
{
    int res;
    struct fuse_mount_data data;
    
    data.version = FUSE_KERNEL_VERSION;
    data.fd = fd;
    data.rootmode = rootmode;

    res = mount(dev, dir, type, MS_MGC_VAL | MS_NOSUID | MS_NODEV, &data);
    if(res == -1) {
        perror("mount failed");
	return -1;
    }
    
    return 0;
}

static void add_mntent(const char *dev, const char *dir, const char *type)
{
    int res;
    FILE *fp;
    struct mntent ent;
    
    fp = setmntent("/etc/mtab", "a");
    if(fp == NULL) {
        perror("setmntent");
	return;
    }
    
    ent.mnt_fsname = (char *) dev;
    ent.mnt_dir = (char *) dir;
    ent.mnt_type = (char *) type;
    ent.mnt_opts = "rw,nosuid,nodev";
    ent.mnt_freq = 0;
    ent.mnt_passno = 0;
    res = addmntent(fp, & ent);
    if(res != 0)
	perror("addmntent");
    
    endmntent(fp);

}

static void remove_mntent(const char *dir)
{
    int res;
    FILE *fdold, *fdnew;
    struct mntent *entp;
        
    fdold = setmntent("/etc/mtab", "r");
    if(fdold == NULL) {
        perror("/etc/mtab");
	return;
    }

    fdnew = setmntent("/etc/mtab~", "w");
    if(fdnew == NULL) {
        perror("/etc/mtab~");
	return;
    }

    do {
	entp = getmntent(fdold);
	if(entp != NULL && strcmp(entp->mnt_dir, dir) != 0) {
            res = addmntent(fdnew, entp);
            if(res != 0)
                perror("addmntent");
        }
    } while(entp != NULL);

    endmntent(fdold);
    endmntent(fdnew);

    res = rename("/etc/mtab~", "/etc/mtab");
    if(res == -1)
        perror("renameing /etc/mtab~ to /etc/mtab");
}

int fuse_mount(struct fuse *f, const char *dir)
{
    int res;
    const char *dev = FUSE_DEV;
    const char *type = "fuse";

    if(f->mnt != NULL)
        return 0;

    f->fd = open(dev, O_RDWR);
    if(f->fd == -1) {
        perror(dev);
        return -1;
    }
    
    res = do_mount(dev, dir, type, f->rootmode, f->fd);
    if(res == -1)
        return -1;

    add_mntent(dev, dir, type);
    f->mnt = g_strdup(dir);
    
    return 0;
}

int fuse_unmount(struct fuse *f)
{
    int res;

    if(f->mnt == NULL)
        return 0;

    close(f->fd);
    f->fd = -1;

    res = umount(f->mnt);
    if(res == -1)
        perror("umount failed");
    else
        remove_mntent(f->mnt);

    g_free(f->mnt);
    f->mnt = NULL;

    return res;
}