aboutsummaryrefslogtreecommitdiffstats
path: root/nmount.c
blob: 8142531a82f0079a833b6f82a63e2f0635e628ec (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>


/////////////////////////////////////////////////////////////////////////////////////////
/*FLAGS FROM KERNEL*/

#define _MS_RDONLY       (1<<0)
#define _MS_NOSUID       (1<<1)
#define _MS_NODEV        (1<<2)
#define _MS_NOEXEC       (1<<3)
#define _MS_SYNCHRONOUS  (1<<4)
#define _MS_REMOUNT      (1<<5)
#define _MS_MANDLOCK     (1<<6)
#define _MS_DIRSYNC      (1<<7)
#define _MS_NOATIME      (1<<10)
#define _MS_NODIRATIME   (1<<11)
#define _MS_BIND         (1<<12)
#define _MS_MOVE         (1<<13)
#define _MS_REC          (1<<14)
#define _MS_SILENT       (1<<15)
#define _MS_POSIXACL     (1<<16)
#define _MS_UNBINDABLE   (1<<17)
#define _MS_PRIVATE      (1<<18)
#define _MS_SLAVE        (1<<19)
#define _MS_SHARED       (1<<20)
#define _MS_RELATIME     (1<<21)
#define _MS_KERNMOUNT    (1<<22)
#define _MS_I_VERSION    (1<<23)
#define _MS_STRICTATIME  (1<<24)
#define _MS_LAZYTIME     (1<<25)

#define STRUCT_LEN(VAR,STRUCT) ((sizeof(VAR)/sizeof(STRUCT))-1)

#define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

typedef struct vfs_option_params {
    char *val;
    char *descr;
} vfs_option_params;

vfs_option_params vfs_options_proc_params_hidepid[] = {
    {"0","Everybody may access all proc"},
    {"1","User can access only their own proc, not other /proc/[pid]"},
    {"2","As for mode 1, extra hides pid directories of others"},
    {NULL,NULL},
};

vfs_option_params vfs_options_proc_params_gid[] = {
    {"gid","usergroup who sees /proc in mode 0"},
    {NULL,NULL},
};

typedef struct vfs_options 
{
    char *options;
    char *description;
    vfs_option_params *params;
} vfs_options;

vfs_options vfs_options_proc[] =
{
    {"hidepid=\%u" ,"",                                     (struct vfs_option_params *)&vfs_options_proc_params_hidepid},
    {"gid=\%u"     ,"user group that can access process",   (struct vfs_option_params *)&vfs_options_proc_params_gid},
    {NULL,NULL,NULL}
};


static const struct vfs {
    char *name;
    long flags;
    char *mountpoint;
    vfs_options *options;
} vfstab[] = {
    {"devtmpfs",  0, "/Device",  NULL},
    {"mqueue",    0, "/MQueue/", NULL},
    {"proc",      0, "/Process", (struct vfs_options*)&vfs_options_proc},
    {"tmpfs",     0, "/Ram",     NULL},
    {"sysfs",     0, "/System",  NULL},
    {"ext4",      0, NULL,       NULL},
    {"vfat",      0, NULL,       NULL},
    {NULL,        0, NULL,       NULL},
};

static const struct mount_flags
{
    char *name;
    unsigned long bitfield;
} mountflgs[] = {
    {"bind",_MS_BIND},
    {"move",_MS_MOVE},
    {"rdonly",_MS_RDONLY},
    {"lazytime",_MS_LAZYTIME},
    {"nodev",_MS_NODEV},
    {"noexec",_MS_NOEXEC},
    {"nosuid",_MS_NOSUID},
    {"remount",_MS_REMOUNT},
    {"silent",_MS_SILENT},
    {"sync",_MS_SYNCHRONOUS},
    {NULL,0},
};

typedef struct {
    char *f_source_dir;
    char *f_target_dir;
    char *f_type;
    char *f_dir;
    char *f_flags;//g_params.f_output = 1;
    unsigned long mount_flags;
    char *f_options;
    int   f_verbose;
    int   f_helper;
} mount_params;

static mount_params g_params;

void helper(char *progname)
{
    printf("Usage: %s [OPTS]\n\n"
        "Version: 0.0.1 \n"
        "-s source directory\n"
        "-d target directory\n"
        "-t filesystem type\n"
        "-f mount flags\n"
        "-o mount fs options\n"
        "-v verbose output\n"
        "-h help options\n"
        "-a helper for filetype options\n"
        "\n"
        , progname);
}

void sig_handler(int signo)
{
    switch(signo)
    {
        case SIGINT:
            //printf("Catch SIGINT Exit\n");
            //if (g_params.f_output)
            //{
            //    printf("Data in %d bytes Data out %d bytes",g_stats.cnt_in,g_stats.cnt_out);
            //}
        default:
            printf("Unknown signal %d\n",signo);
    }
    exit(0);
}

int check_filetype(char *fsname)
{
    int i;
    for (i=0;i<STRUCT_LEN(vfstab,struct vfs);i++)
    {
         if (strncmp(vfstab[i].name,fsname,max(strlen(vfstab[i].name),strlen(fsname))) == 0)
         {
            return 1;
         }
    }

    return 0;
}

unsigned long str2flag(char *opt)
{
    int i;
    for (i=0;i<STRUCT_LEN(mountflgs, struct mount_flags);i++)
    {
        if (strncmp(mountflgs[i].name,opt,strlen(mountflgs[i].name)) == 0)
        {
            return mountflgs[i].bitfield;
        }
    }
    return 0;
}

int smount(
    const char *source, 
    const char *target,
    const char *filesystemtype,
    unsigned long mountflags,
    const void *data)
{
    int ret=-1;

    if (g_params.f_verbose)
    {
        printf("Syscal params:\n");
        printf("source     :%s\n",source);
        printf("target     :%s\n",target);
        printf("filesystem :%s\n",filesystemtype);
        printf("mountflags :%lu\n",mountflags);
        printf("data       :%016xxd\n",data);
    }
    ret = syscall(SYS_mount,source,target,filesystemtype,mountflags,data);

    return ret;
}

int main(int argc, char **argv)
{
    int c;
    int i,j,k;

    printf("One big real mount\n");

    if (signal(SIGINT,sig_handler) == SIG_ERR)
    {
        printf("cannot register signal handler\n");
        exit(1);
    }

    memset(&g_params,0,sizeof(g_params));
    
    //process arguments
    while ((c = getopt(argc, argv, "ad:t:f:o:vs:")) != -1)
    {
        switch (c)
        {
            case 'a':
                //g_params.f_output = 1;
                g_params.f_helper = 1;
                break;
            case 's':
                g_params.f_source_dir = optarg;
                break;
            case 'd':
                g_params.f_target_dir = optarg;
                break;
            case 't':
                g_params.f_type = optarg;
                break;
            case 'f':
                g_params.f_flags = optarg;
                break;
            case 'o':
                g_params.f_options = optarg;
                break;
            case 'v':
                g_params.f_verbose = 1;
                break;
            default:
                helper(argv[0]);
                exit(1);
        }
    }

    if (1 == g_params.f_helper)
    {
        for (i=0;i<STRUCT_LEN(vfstab,struct vfs);i++)
        {
            printf("%s %s\n",vfstab[i].name,vfstab[i].mountpoint);
            if (vfstab[i].options != NULL)
            {
                j = 0;
                while (vfstab[i].options[j].options != NULL)
                {
                    printf("\t%s %s\n",
                        vfstab[i].options[j].options,
                        vfstab[i].options[j].description
                    );
                    k = 0;
                    while(vfstab[i].options[j].params[k].val != NULL)
                    {
                        printf("\t\t%s %s\n",
                            vfstab[i].options[j].params[k].val,
                            vfstab[i].options[j].params[k].descr
                        );
                        k++;
                    }
                    j++;
                }
            }
        }
    }

    if (NULL != g_params.f_type)
    {
        if (check_filetype(g_params.f_type) != 1)
        {
            printf("Unknow file type %s\n",g_params.f_type);
            printf("Supported formats: ");
            for (i=0;i<STRUCT_LEN(vfstab,struct vfs);i++)
            {
                printf("%s ",vfstab[i].name);
            }
            printf("\n");
            return 1;
        }
    }

    if (NULL != g_params.f_flags)
    {
        char *token = strtok(g_params.f_flags,",");
        printf("Flags: %d\n",g_params.mount_flags);
        while ( token != NULL )
        {
            printf("Flags: %s\n",token);
            g_params.mount_flags |= str2flag(token);
            printf("Flags: %d\n",g_params.mount_flags);
            token = strtok(NULL,",");
        }
    }

    if (NULL != g_params.f_target_dir)
    {
        mkdir(g_params.f_target_dir, 0000);
    }

    if ((NULL != g_params.f_target_dir) && 
        (NULL != g_params.f_flags) &&
        (NULL != g_params.f_type) &&
        (NULL != g_params.f_source_dir))
    {
        printf("Flags: %d\n",g_params.mount_flags);
        int ret = smount(
            g_params.f_source_dir,
            g_params.f_target_dir,
            g_params.f_type, 
            g_params.mount_flags, 
            "");
        int err = errno;
        printf("Mount exit code %d\n",ret);
        if (ret==-1)
        {
            printf("Strerr: %d %s\n",err,strerror(err));
        }
    }

    return 0;
}