#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