2013-08-12 11:32:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
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[] = {
|
2014-03-15 13:26:32 +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 },
|
|
|
|
{ "bind", NULL, MS_BIND },
|
|
|
|
{ NULL, NULL, 0 }
|
2013-08-12 11:32:34 +00:00
|
|
|
};
|
|
|
|
|
2014-03-15 13:26:32 +00:00
|
|
|
static void
|
|
|
|
parseopts(char *popts, unsigned long *flags, char *data, size_t bufsiz)
|
|
|
|
{
|
|
|
|
unsigned int i, validopt;
|
|
|
|
size_t optlen, dlen = 0;
|
2013-09-03 13:33:52 +00:00
|
|
|
char *name;
|
2014-03-15 13:26:32 +00:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 13:33:52 +00:00
|
|
|
|
2013-08-12 11:32:34 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-03-15 13:26:32 +00:00
|
|
|
eprintf("usage: %s [-BMRan] [-t fstype] [-o options] [source] [target]\n",
|
2013-08-12 11:32:34 +00:00
|
|
|
argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-03-15 16:10:23 +00:00
|
|
|
int aflag = 0, oflag = 0, status = EXIT_SUCCESS, i;
|
2013-08-12 11:32:34 +00:00
|
|
|
unsigned long flags = 0;
|
2014-03-15 16:08:53 +00:00
|
|
|
char *types = NULL, data[512] = "";
|
2014-03-15 13:26:32 +00:00
|
|
|
char *files[] = { "/proc/mounts", "/etc/fstab", NULL };
|
|
|
|
size_t datasiz = sizeof(data);
|
|
|
|
const char *source, *target;
|
|
|
|
struct stat st;
|
2014-02-24 13:39:05 +00:00
|
|
|
struct mntent *me = NULL;
|
|
|
|
FILE *fp;
|
2014-03-15 13:26:32 +00:00
|
|
|
|
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 'o':
|
2014-03-15 16:11:30 +00:00
|
|
|
oflag = 1;
|
2014-03-15 13:26:32 +00:00
|
|
|
parseopts(EARGF(usage()), &flags, data, datasiz);
|
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-15 13:26:32 +00:00
|
|
|
if(argc < 1 && aflag == 0)
|
2013-08-12 11:32:34 +00:00
|
|
|
usage();
|
|
|
|
|
2014-03-15 13:26:32 +00:00
|
|
|
if(aflag == 1)
|
|
|
|
goto mountall;
|
2014-03-07 13:43:42 +00:00
|
|
|
|
2013-08-12 11:32:34 +00:00
|
|
|
source = argv[0];
|
|
|
|
target = argv[1];
|
|
|
|
|
2014-03-15 13:26:32 +00:00
|
|
|
if(!target) {
|
2013-09-03 13:01:01 +00:00
|
|
|
target = argv[0];
|
2013-08-12 11:32:34 +00:00
|
|
|
source = NULL;
|
2014-03-15 13:26:32 +00:00
|
|
|
if(stat(target, &st) < 0)
|
2013-08-31 16:39:17 +00:00
|
|
|
eprintf("stat %s:", target);
|
2014-03-15 13:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; files[i]; i++) {
|
2014-03-17 12:46:35 +00:00
|
|
|
if(!(fp = setmntent(files[i], "r"))) {
|
2014-03-15 19:25:27 +00:00
|
|
|
if (strcmp(files[i], "/proc/mounts") != 0)
|
|
|
|
weprintf("setmntent %s:", files[i]);
|
2014-03-17 12:46:35 +00:00
|
|
|
continue;
|
2013-08-31 16:39:17 +00:00
|
|
|
}
|
2014-03-17 12:46:35 +00:00
|
|
|
while((me = getmntent(fp))) {
|
|
|
|
if(strcmp(me->mnt_dir, target) == 0 ||
|
|
|
|
strcmp(me->mnt_fsname, target) == 0 ||
|
|
|
|
(source && strcmp(me->mnt_dir, source) == 0) ||
|
|
|
|
(source && strcmp(me->mnt_fsname, source) == 0)) {
|
|
|
|
if(!source) {
|
|
|
|
target = me->mnt_dir;
|
|
|
|
source = me->mnt_fsname;
|
|
|
|
}
|
|
|
|
if(!oflag)
|
|
|
|
parseopts(me->mnt_opts, &flags, data, datasiz);
|
|
|
|
if(!types)
|
|
|
|
types = me->mnt_type;
|
|
|
|
goto mountsingle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
fp = NULL;
|
2013-08-12 11:32:34 +00:00
|
|
|
}
|
2014-03-15 13:26:32 +00:00
|
|
|
if(!source)
|
2014-03-15 18:45:56 +00:00
|
|
|
eprintf("can't find %s in /etc/fstab\n", target);
|
2013-08-12 11:32:34 +00:00
|
|
|
|
2014-03-15 13:26:32 +00:00
|
|
|
mountsingle:
|
2014-03-15 18:47:18 +00:00
|
|
|
if(mount(source, target, types, flags, data) < 0) {
|
|
|
|
weprintf("mount: %s:", source);
|
|
|
|
status = 32; /* all failed */
|
|
|
|
}
|
2014-03-15 13:26:32 +00:00
|
|
|
if(fp)
|
2014-03-07 13:43:42 +00:00
|
|
|
endmntent(fp);
|
2014-03-15 16:10:23 +00:00
|
|
|
return status;
|
2014-03-07 13:43:42 +00:00
|
|
|
|
2014-03-15 13:26:32 +00:00
|
|
|
mountall:
|
|
|
|
if(!(fp = setmntent("/etc/fstab", "r")))
|
|
|
|
eprintf("setmntent %s:", "/etc/fstab");
|
|
|
|
while((me = getmntent(fp))) {
|
|
|
|
flags = 0;
|
|
|
|
parseopts(me->mnt_opts, &flags, data, datasiz);
|
2014-03-15 16:10:23 +00:00
|
|
|
if(mount(me->mnt_fsname, me->mnt_dir, me->mnt_type, flags, data) < 0) {
|
2014-03-15 18:28:23 +00:00
|
|
|
weprintf("mount: %s:", me->mnt_fsname);
|
2014-03-15 16:10:23 +00:00
|
|
|
if(status != 64)
|
|
|
|
status = 32; /* all failed */
|
|
|
|
} else {
|
|
|
|
status = 64; /* some failed */
|
|
|
|
}
|
2013-09-03 13:33:52 +00:00
|
|
|
}
|
2014-03-15 13:26:32 +00:00
|
|
|
endmntent(fp);
|
2013-09-03 13:33:52 +00:00
|
|
|
|
2014-03-15 16:10:23 +00:00
|
|
|
return status;
|
2013-08-12 11:32:34 +00:00
|
|
|
}
|