2013-08-12 11:32:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-03-07 13:43:42 +00:00
|
|
|
#include <errno.h>
|
2014-02-24 13:39:05 +00:00
|
|
|
#include <mntent.h>
|
2013-08-12 11:32:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-24 13:39:05 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2013-08-12 11:32:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *opt;
|
|
|
|
const char *notopt;
|
|
|
|
unsigned long v;
|
|
|
|
} optnames[] = {
|
2013-10-28 16:51:58 +00:00
|
|
|
{ "remount", NULL, MS_REMOUNT },
|
|
|
|
{ "ro", "rw", MS_RDONLY },
|
|
|
|
{ "sync", "async", MS_SYNCHRONOUS },
|
|
|
|
{ "dirsync", NULL, MS_DIRSYNC },
|
|
|
|
{ "nodev", "dev", MS_NODEV },
|
|
|
|
{ "noatime", "atime", MS_NOATIME },
|
|
|
|
{ "nodiratime", "diratime", MS_NODIRATIME },
|
|
|
|
{ "noexec", "exec", MS_NOEXEC },
|
|
|
|
{ "nosuid", "suid", MS_NOSUID },
|
|
|
|
{ "mand", "nomand", MS_MANDLOCK },
|
|
|
|
{ "relatime", "norelatime", MS_RELATIME },
|
|
|
|
{ NULL, NULL, 0 }
|
2013-08-12 11:32:34 +00:00
|
|
|
};
|
|
|
|
|
2013-09-03 13:33:52 +00:00
|
|
|
static struct option {
|
|
|
|
char *name;
|
|
|
|
struct option *next;
|
|
|
|
} *opthead;
|
|
|
|
|
2013-08-12 11:32:34 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-03-07 13:43:42 +00:00
|
|
|
eprintf("usage: %s [-BMRadn] [-t fstype] [-o options] [source] [target]\n",
|
2013-08-12 11:32:34 +00:00
|
|
|
argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-02-24 13:39:05 +00:00
|
|
|
int i, validopt;
|
2014-03-07 13:43:42 +00:00
|
|
|
int oflag = 0, aflag = 0;
|
2013-08-12 11:32:34 +00:00
|
|
|
unsigned long flags = 0;
|
2013-09-03 13:33:52 +00:00
|
|
|
char *types = NULL, *arg = NULL, *p;
|
2013-08-12 11:32:34 +00:00
|
|
|
const char *source;
|
|
|
|
const char *target;
|
2013-08-31 16:39:17 +00:00
|
|
|
struct stat st1, st2;
|
2013-08-12 11:32:34 +00:00
|
|
|
void *data = NULL;
|
2014-02-24 13:39:05 +00:00
|
|
|
struct mntent *me = NULL;
|
|
|
|
FILE *fp;
|
2013-09-03 13:33:52 +00:00
|
|
|
struct option *opt, *tmp;
|
2013-08-12 11:32:34 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'B':
|
|
|
|
flags |= MS_BIND;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
flags |= MS_MOVE;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
flags |= MS_REC;
|
|
|
|
break;
|
2014-03-07 13:43:42 +00:00
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
2013-08-12 11:32:34 +00:00
|
|
|
case 'd':
|
|
|
|
data = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'o':
|
2013-09-03 13:33:52 +00:00
|
|
|
oflag = 1;
|
|
|
|
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;
|
2013-08-12 11:32:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
types = EARGF(usage());
|
|
|
|
break;
|
2013-09-06 10:01:03 +00:00
|
|
|
case 'n':
|
|
|
|
break;
|
2013-08-12 11:32:34 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-03-07 13:43:42 +00:00
|
|
|
if (argc < 1 && aflag == 0)
|
2013-08-12 11:32:34 +00:00
|
|
|
usage();
|
|
|
|
|
2013-09-03 13:33:52 +00:00
|
|
|
for (opt = opthead; opt; opt = opt->next) {
|
|
|
|
validopt = 0;
|
|
|
|
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)
|
2014-02-27 15:08:17 +00:00
|
|
|
eprintf("unknown option: %s\n", opt->name);
|
2013-09-03 13:33:52 +00:00
|
|
|
|
2014-03-07 13:43:42 +00:00
|
|
|
if (aflag == 1)
|
|
|
|
goto domount;
|
|
|
|
|
2013-08-12 11:32:34 +00:00
|
|
|
source = argv[0];
|
|
|
|
target = argv[1];
|
|
|
|
|
|
|
|
if (!target) {
|
2013-09-03 13:01:01 +00:00
|
|
|
target = argv[0];
|
2013-08-12 11:32:34 +00:00
|
|
|
source = NULL;
|
2013-08-31 16:39:17 +00:00
|
|
|
if (stat(target, &st1) < 0)
|
|
|
|
eprintf("stat %s:", target);
|
2014-02-24 13:39:05 +00:00
|
|
|
fp = setmntent("/proc/mounts", "r");
|
|
|
|
if (!fp)
|
|
|
|
eprintf("setmntent %s:", "/proc/mounts");
|
2014-03-07 13:43:42 +00:00
|
|
|
while ((me = getmntent(fp))) {
|
2014-02-24 13:39:05 +00:00
|
|
|
if (stat(me->mnt_dir, &st2) < 0)
|
|
|
|
eprintf("stat %s:", me->mnt_dir);
|
2013-08-31 16:39:17 +00:00
|
|
|
if (st1.st_dev == st2.st_dev &&
|
|
|
|
st1.st_ino == st2.st_ino) {
|
2014-02-24 13:39:05 +00:00
|
|
|
source = strdup(me->mnt_fsname);
|
2013-08-31 16:39:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 13:39:05 +00:00
|
|
|
endmntent(fp);
|
2013-08-12 11:32:34 +00:00
|
|
|
if (!source)
|
2014-02-27 15:08:17 +00:00
|
|
|
eprintf("can't find %s mountpoint\n", target);
|
2013-08-12 11:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mount(source, target, types, flags, data) < 0)
|
|
|
|
eprintf("mount:");
|
|
|
|
|
2014-03-07 13:43:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-09-03 13:33:52 +00:00
|
|
|
opt = opthead;
|
|
|
|
while (opt) {
|
|
|
|
tmp = opt->next;
|
|
|
|
free(opt->name);
|
|
|
|
free(opt);
|
|
|
|
opt = tmp;
|
|
|
|
}
|
|
|
|
|
2013-10-07 18:11:40 +00:00
|
|
|
return EXIT_SUCCESS;
|
2013-08-12 11:32:34 +00:00
|
|
|
}
|