aboutsummaryrefslogblamecommitdiffstats
path: root/nmount.c
blob: 94ed76d4e50bdfa08601bc139b9ad141beaeee4b (plain) (tree)
1
2
3
4
5
6
7
8







                      




                        



                                                                                         























                                
 

                                                               




                                 




















                                                                       
                              



                                

                                                                                                                          


                    
 
                         




                                                                                    
              







                                                                            
  
 

















                               
                

                       


                                          
                              


                    







                                 

                               
                              




                                          



















                                                                                              













                                                                                            












                                                                                            












                                                                          






                               
               
 











                                                                          
 



                               
              
                  







                                                   

                                         

                       
                                                          


                  
                     
                                        
                                      
                      

















                                               





                                




                                                                  

























                                                                     
                             









                                                            




                                                                                                       
         
        
     
 












                                                       
                                     


                                           



                                                          

     


                                                                              

                                           
                                  














                                                        


             

 
#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;           //filesystem name
    long flags;           //dunno
    char *mountpoint;     //default mount point if mount dir is not set
    int nosource;         //does fs have source or not
    vfs_options *options; //hellpers to show all possible options to config fs mount
} vfstab[] = {
    {"devtmpfs",  0, "/Device",  0, NULL},
    {"mqueue",    0, "/MQueue/", 0, NULL},
    {"proc",      0, "/Process", 1, (struct vfs_options*)&vfs_options_proc},
    {"tmpfs",     0, "/Ram",     0, NULL},
    {"sysfs",     0, "/System",  0, NULL},
    {"ext4",      0, NULL,       0, NULL},
    {"vfat",      0, NULL,       0, NULL},
    {NULL,        0, NULL,       0, 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;
         }
    }