mirror of
git://git.suckless.org/ubase
synced 2025-04-26 04:47:58 +00:00
Major mount(8) refactor
This commit is contained in:
parent
14716af472
commit
35e3f401ab
196
mount.c
196
mount.c
@ -15,46 +15,79 @@ struct {
|
|||||||
const char *notopt;
|
const char *notopt;
|
||||||
unsigned long v;
|
unsigned long v;
|
||||||
} optnames[] = {
|
} optnames[] = {
|
||||||
{ "remount", NULL, MS_REMOUNT },
|
{ "remount", NULL, MS_REMOUNT },
|
||||||
{ "ro", "rw", MS_RDONLY },
|
{ "ro", "rw", MS_RDONLY },
|
||||||
{ "sync", "async", MS_SYNCHRONOUS },
|
{ "sync", "async", MS_SYNCHRONOUS },
|
||||||
{ "dirsync", NULL, MS_DIRSYNC },
|
{ "dirsync", NULL, MS_DIRSYNC },
|
||||||
{ "nodev", "dev", MS_NODEV },
|
{ "nodev", "dev", MS_NODEV },
|
||||||
{ "noatime", "atime", MS_NOATIME },
|
{ "noatime", "atime", MS_NOATIME },
|
||||||
{ "nodiratime", "diratime", MS_NODIRATIME },
|
{ "nodiratime", "diratime", MS_NODIRATIME },
|
||||||
{ "noexec", "exec", MS_NOEXEC },
|
{ "noexec", "exec", MS_NOEXEC },
|
||||||
{ "nosuid", "suid", MS_NOSUID },
|
{ "nosuid", "suid", MS_NOSUID },
|
||||||
{ "mand", "nomand", MS_MANDLOCK },
|
{ "mand", "nomand", MS_MANDLOCK },
|
||||||
{ "relatime", "norelatime", MS_RELATIME },
|
{ "relatime", "norelatime", MS_RELATIME },
|
||||||
{ NULL, NULL, 0 }
|
{ "bind", NULL, MS_BIND },
|
||||||
|
{ NULL, NULL, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct option {
|
static void
|
||||||
|
parseopts(char *popts, unsigned long *flags, char *data, size_t bufsiz)
|
||||||
|
{
|
||||||
|
unsigned int i, validopt;
|
||||||
|
size_t optlen, dlen = 0;
|
||||||
char *name;
|
char *name;
|
||||||
struct option *next;
|
|
||||||
} *opthead;
|
data[0] = '\0';
|
||||||
|
for(name = strtok(popts, ","); name; name = strtok(NULL, ",")) {
|
||||||
|
validopt = 0;
|
||||||
|
for(i = 0; optnames[i].v; i++) {
|
||||||
|
if(optnames[i].opt && strcmp(name, optnames[i].opt) == 0) {
|
||||||
|
*flags |= optnames[i].v;
|
||||||
|
validopt = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(optnames[i].notopt && strcmp(name, optnames[i].notopt) == 0) {
|
||||||
|
*flags &= ~optnames[i].v;
|
||||||
|
validopt = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!validopt) {
|
||||||
|
/* unknown option, pass as data option to mount() */
|
||||||
|
if((optlen = strlen(name))) {
|
||||||
|
if(dlen + optlen + 2 >= bufsiz)
|
||||||
|
return; /* prevent overflow */
|
||||||
|
if(dlen)
|
||||||
|
data[dlen++] = ',';
|
||||||
|
memcpy(&data[dlen], name, optlen);
|
||||||
|
dlen += optlen;
|
||||||
|
data[dlen] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-BMRadn] [-t fstype] [-o options] [source] [target]\n",
|
eprintf("usage: %s [-BMRan] [-t fstype] [-o options] [source] [target]\n",
|
||||||
argv0);
|
argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i, validopt;
|
int aflag = 0, i;
|
||||||
int oflag = 0, aflag = 0;
|
|
||||||
unsigned long flags = 0;
|
unsigned long flags = 0;
|
||||||
char *types = NULL, *arg = NULL, *p;
|
char *types = NULL, data[512];
|
||||||
const char *source;
|
char *files[] = { "/proc/mounts", "/etc/fstab", NULL };
|
||||||
const char *target;
|
size_t datasiz = sizeof(data);
|
||||||
struct stat st1, st2;
|
const char *source, *target;
|
||||||
void *data = NULL;
|
struct stat st;
|
||||||
struct mntent *me = NULL;
|
struct mntent *me = NULL;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
struct option *opt, *tmp;
|
|
||||||
|
data[0] = '\0';
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'B':
|
case 'B':
|
||||||
@ -69,22 +102,8 @@ main(int argc, char *argv[])
|
|||||||
case 'a':
|
case 'a':
|
||||||
aflag = 1;
|
aflag = 1;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
|
||||||
data = EARGF(usage());
|
|
||||||
break;
|
|
||||||
case 'o':
|
case 'o':
|
||||||
oflag = 1;
|
parseopts(EARGF(usage()), &flags, data, datasiz);
|
||||||
arg = EARGF(usage());
|
|
||||||
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
|
|
||||||
opt = malloc(sizeof(*opt));
|
|
||||||
if (!opt)
|
|
||||||
eprintf("malloc:");
|
|
||||||
opt->name = strdup(p);
|
|
||||||
if (!opt->name)
|
|
||||||
eprintf("strdup:");
|
|
||||||
opt->next = opthead;
|
|
||||||
opthead = opt;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
types = EARGF(usage());
|
types = EARGF(usage());
|
||||||
@ -95,85 +114,60 @@ main(int argc, char *argv[])
|
|||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if (argc < 1 && aflag == 0)
|
if(argc < 1 && aflag == 0)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (opt = opthead; opt; opt = opt->next) {
|
if(aflag == 1)
|
||||||
validopt = 0;
|
goto mountall;
|
||||||
for (i = 0; optnames[i].v; i++) {
|
|
||||||
if (optnames[i].opt) {
|
|
||||||
if (!strcmp(opt->name,
|
|
||||||
optnames[i].opt)) {
|
|
||||||
flags |= optnames[i].v;
|
|
||||||
validopt = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (optnames[i].notopt) {
|
|
||||||
if (!strcmp(opt->name,
|
|
||||||
optnames[i].notopt)) {
|
|
||||||
flags &= ~optnames[i].v;
|
|
||||||
validopt = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!validopt)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (oflag && !validopt)
|
|
||||||
eprintf("unknown option: %s\n", opt->name);
|
|
||||||
|
|
||||||
if (aflag == 1)
|
|
||||||
goto domount;
|
|
||||||
|
|
||||||
source = argv[0];
|
source = argv[0];
|
||||||
target = argv[1];
|
target = argv[1];
|
||||||
|
|
||||||
if (!target) {
|
if(!target) {
|
||||||
target = argv[0];
|
target = argv[0];
|
||||||
source = NULL;
|
source = NULL;
|
||||||
if (stat(target, &st1) < 0)
|
if(stat(target, &st) < 0)
|
||||||
eprintf("stat %s:", target);
|
eprintf("stat %s:", target);
|
||||||
fp = setmntent("/proc/mounts", "r");
|
}
|
||||||
if (!fp)
|
|
||||||
eprintf("setmntent %s:", "/proc/mounts");
|
for(i = 0; files[i]; i++) {
|
||||||
while ((me = getmntent(fp))) {
|
if((fp = setmntent(files[i], "r"))) {
|
||||||
if (stat(me->mnt_dir, &st2) < 0)
|
while((me = getmntent(fp))) {
|
||||||
eprintf("stat %s:", me->mnt_dir);
|
if(strcmp(me->mnt_dir, target) == 0 ||
|
||||||
if (st1.st_dev == st2.st_dev &&
|
strcmp(me->mnt_fsname, target) == 0) {
|
||||||
st1.st_ino == st2.st_ino) {
|
source = me->mnt_fsname;
|
||||||
source = strdup(me->mnt_fsname);
|
target = me->mnt_dir;
|
||||||
break;
|
if(!types)
|
||||||
|
types = me->mnt_type;
|
||||||
|
goto mountsingle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
endmntent(fp);
|
||||||
|
fp = NULL;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "setmntent %s: %s\n", files[i], strerror(errno));
|
||||||
}
|
}
|
||||||
endmntent(fp);
|
|
||||||
if (!source)
|
|
||||||
eprintf("can't find %s mountpoint\n", target);
|
|
||||||
}
|
}
|
||||||
|
if(!source)
|
||||||
|
eprintf("can't find %s mountpoint\n", target);
|
||||||
|
|
||||||
if (mount(source, target, types, flags, data) < 0)
|
mountsingle:
|
||||||
|
if(mount(source, target, types, flags, data) < 0)
|
||||||
eprintf("mount:");
|
eprintf("mount:");
|
||||||
|
if(fp)
|
||||||
domount:
|
|
||||||
if (aflag == 1) {
|
|
||||||
fp = setmntent("/etc/fstab", "r");
|
|
||||||
if (!fp)
|
|
||||||
eprintf("setmntent %s:", "/etc/fstab");
|
|
||||||
while ((me = getmntent(fp)))
|
|
||||||
if (mount(me->mnt_fsname, me->mnt_dir, me->mnt_type,
|
|
||||||
0, me->mnt_opts) < 0)
|
|
||||||
fprintf(stderr, "mount: %s\n", strerror(errno));
|
|
||||||
endmntent(fp);
|
endmntent(fp);
|
||||||
}
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
opt = opthead;
|
mountall:
|
||||||
while (opt) {
|
if(!(fp = setmntent("/etc/fstab", "r")))
|
||||||
tmp = opt->next;
|
eprintf("setmntent %s:", "/etc/fstab");
|
||||||
free(opt->name);
|
while((me = getmntent(fp))) {
|
||||||
free(opt);
|
flags = 0;
|
||||||
opt = tmp;
|
parseopts(me->mnt_opts, &flags, data, datasiz);
|
||||||
|
if(mount(me->mnt_fsname, me->mnt_dir, me->mnt_type, flags, data) < 0)
|
||||||
|
fprintf(stderr, "mount: %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
|
endmntent(fp);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user