2013-08-12 07:32:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-30 14:03:41 -04:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
2014-02-24 08:39:05 -05:00
|
|
|
#include <mntent.h>
|
2013-08-12 07:32:34 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-24 08:39:05 -05:00
|
|
|
#include <unistd.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
2013-08-12 07:32:34 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *opt;
|
|
|
|
const char *notopt;
|
|
|
|
unsigned long v;
|
|
|
|
} optnames[] = {
|
2014-04-04 13:03:54 -04:00
|
|
|
{ "defaults", NULL, 0 },
|
2014-03-15 09:26:32 -04: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 07:32:34 -04:00
|
|
|
};
|
|
|
|
|
2014-03-15 09:26:32 -04:00
|
|
|
static void
|
2014-04-04 13:39:34 -04:00
|
|
|
parseopts(char *popts, unsigned long *flags, char *data, size_t datasiz)
|
2014-03-15 09:26:32 -04:00
|
|
|
{
|
|
|
|
unsigned int i, validopt;
|
|
|
|
size_t optlen, dlen = 0;
|
2013-09-03 09:33:52 -04:00
|
|
|
char *name;
|
2014-03-15 09:26:32 -04:00
|
|
|
|
|
|
|
data[0] = '\0';
|
2014-07-09 11:39:32 -04:00
|
|
|
for (name = strtok(popts, ","); name; name = strtok(NULL, ",")) {
|
2014-03-15 09:26:32 -04:00
|
|
|
validopt = 0;
|
2014-07-09 11:39:32 -04:00
|
|
|
for (i = 0; optnames[i].opt; i++) {
|
|
|
|
if (optnames[i].opt && strcmp(name, optnames[i].opt) == 0) {
|
2014-03-15 09:26:32 -04:00
|
|
|
*flags |= optnames[i].v;
|
|
|
|
validopt = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-07-09 11:39:32 -04:00
|
|
|
if (optnames[i].notopt && strcmp(name, optnames[i].notopt) == 0) {
|
2014-03-15 09:26:32 -04:00
|
|
|
*flags &= ~optnames[i].v;
|
|
|
|
validopt = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!validopt) {
|
2014-03-15 09:26:32 -04:00
|
|
|
/* unknown option, pass as data option to mount() */
|
2014-07-09 11:39:32 -04:00
|
|
|
if ((optlen = strlen(name))) {
|
|
|
|
if (dlen + optlen + 2 >= datasiz)
|
2014-03-15 09:26:32 -04:00
|
|
|
return; /* prevent overflow */
|
2014-07-09 11:39:32 -04:00
|
|
|
if (dlen)
|
2014-03-15 09:26:32 -04:00
|
|
|
data[dlen++] = ',';
|
|
|
|
memcpy(&data[dlen], name, optlen);
|
|
|
|
dlen += optlen;
|
|
|
|
data[dlen] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 09:33:52 -04:00
|
|
|
|
2014-03-18 10:38:31 -04:00
|
|
|
static int
|
|
|
|
mounted(const char *dir)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct mntent *me;
|
|
|
|
struct stat st1, st2;
|
|
|
|
|
2014-03-22 07:29:41 -04:00
|
|
|
if (stat(dir, &st1) < 0) {
|
|
|
|
weprintf("stat %s:", dir);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-18 10:38:31 -04:00
|
|
|
fp = setmntent("/proc/mounts", "r");
|
|
|
|
if (!fp)
|
|
|
|
eprintf("setmntent %s:", "/proc/mounts");
|
|
|
|
while ((me = getmntent(fp)) != NULL) {
|
2014-03-22 07:29:41 -04:00
|
|
|
if (stat(me->mnt_dir, &st2) < 0) {
|
|
|
|
weprintf("stat %s:", me->mnt_dir);
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-18 10:38:31 -04:00
|
|
|
if (st1.st_dev == st2.st_dev &&
|
|
|
|
st1.st_ino == st2.st_ino)
|
2014-03-22 07:29:41 -04:00
|
|
|
return 1;
|
2014-03-18 10:38:31 -04:00
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-12 07:32:34 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-03-15 09:26:32 -04:00
|
|
|
eprintf("usage: %s [-BMRan] [-t fstype] [-o options] [source] [target]\n",
|
2013-08-12 07:32:34 -04:00
|
|
|
argv0);
|
|
|
|
}
|
|
|
|
|
2014-04-04 13:41:37 -04:00
|
|
|
static int
|
|
|
|
catfile(FILE *in, FILE *out)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
size_t bytesread;
|
|
|
|
|
2014-07-09 11:39:32 -04:00
|
|
|
while (!feof(in)) {
|
2014-04-04 13:41:37 -04:00
|
|
|
bytesread = fread(buf, 1, sizeof(buf), in);
|
2014-07-09 11:39:32 -04:00
|
|
|
if (ferror(in))
|
2014-04-04 13:41:37 -04:00
|
|
|
return 0;
|
|
|
|
fwrite(buf, 1, bytesread, out);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-12 07:32:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-03-15 12:10:23 -04:00
|
|
|
int aflag = 0, oflag = 0, status = EXIT_SUCCESS, i;
|
2013-08-12 07:32:34 -04:00
|
|
|
unsigned long flags = 0;
|
2014-06-07 13:21:16 -04:00
|
|
|
char *types = NULL, data[512] = "", *resolvpath = NULL;
|
2014-03-15 09:26:32 -04:00
|
|
|
char *files[] = { "/proc/mounts", "/etc/fstab", NULL };
|
|
|
|
size_t datasiz = sizeof(data);
|
|
|
|
const char *source, *target;
|
2014-02-24 08:39:05 -05:00
|
|
|
struct mntent *me = NULL;
|
|
|
|
FILE *fp;
|
2014-03-15 09:26:32 -04:00
|
|
|
|
2013-08-12 07:32:34 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'B':
|
|
|
|
flags |= MS_BIND;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
flags |= MS_MOVE;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
flags |= MS_REC;
|
|
|
|
break;
|
2014-03-07 08:43:42 -05:00
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
2013-08-12 07:32:34 -04:00
|
|
|
case 'o':
|
2014-03-15 12:11:30 -04:00
|
|
|
oflag = 1;
|
2014-03-15 09:26:32 -04:00
|
|
|
parseopts(EARGF(usage()), &flags, data, datasiz);
|
2013-08-12 07:32:34 -04:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
types = EARGF(usage());
|
|
|
|
break;
|
2013-09-06 06:01:03 -04:00
|
|
|
case 'n':
|
|
|
|
break;
|
2013-08-12 07:32:34 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-07-09 11:39:32 -04:00
|
|
|
if (argc < 1 && aflag == 0) {
|
|
|
|
if (!(fp = fopen(files[0], "r")))
|
2014-04-04 13:41:37 -04:00
|
|
|
eprintf("fopen %s:", files[0]);
|
2014-07-09 11:39:32 -04:00
|
|
|
if (catfile(fp, stdout) != 1) {
|
2014-04-04 13:41:37 -04:00
|
|
|
weprintf("error while reading %s:", files[0]);
|
|
|
|
status = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return status;
|
|
|
|
}
|
2013-08-12 07:32:34 -04:00
|
|
|
|
2014-07-09 11:39:32 -04:00
|
|
|
if (aflag == 1)
|
2014-03-15 09:26:32 -04:00
|
|
|
goto mountall;
|
2014-03-07 08:43:42 -05:00
|
|
|
|
2013-08-12 07:32:34 -04:00
|
|
|
source = argv[0];
|
|
|
|
target = argv[1];
|
|
|
|
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!target) {
|
2013-09-03 09:01:01 -04:00
|
|
|
target = argv[0];
|
2013-08-12 07:32:34 -04:00
|
|
|
source = NULL;
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!(resolvpath = realpath(target, NULL)))
|
2014-06-07 13:21:16 -04:00
|
|
|
eprintf("realpath %s:", target);
|
|
|
|
target = resolvpath;
|
2014-03-15 09:26:32 -04:00
|
|
|
}
|
|
|
|
|
2014-07-09 11:39:32 -04:00
|
|
|
for (i = 0; files[i]; i++) {
|
|
|
|
if (!(fp = setmntent(files[i], "r"))) {
|
2014-03-15 15:25:27 -04:00
|
|
|
if (strcmp(files[i], "/proc/mounts") != 0)
|
|
|
|
weprintf("setmntent %s:", files[i]);
|
2014-03-17 08:46:35 -04:00
|
|
|
continue;
|
2013-08-31 12:39:17 -04:00
|
|
|
}
|
2014-07-09 11:39:32 -04:00
|
|
|
while ((me = getmntent(fp))) {
|
|
|
|
if (strcmp(me->mnt_dir, target) == 0 ||
|
2014-03-17 08:46:35 -04:00
|
|
|
strcmp(me->mnt_fsname, target) == 0 ||
|
|
|
|
(source && strcmp(me->mnt_dir, source) == 0) ||
|
|
|
|
(source && strcmp(me->mnt_fsname, source) == 0)) {
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!source) {
|
2014-03-17 08:46:35 -04:00
|
|
|
target = me->mnt_dir;
|
|
|
|
source = me->mnt_fsname;
|
|
|
|
}
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!oflag)
|
2014-03-17 08:46:35 -04:00
|
|
|
parseopts(me->mnt_opts, &flags, data, datasiz);
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!types)
|
2014-03-17 08:46:35 -04:00
|
|
|
types = me->mnt_type;
|
|
|
|
goto mountsingle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endmntent(fp);
|
|
|
|
fp = NULL;
|
2013-08-12 07:32:34 -04:00
|
|
|
}
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!source)
|
2014-03-15 14:45:56 -04:00
|
|
|
eprintf("can't find %s in /etc/fstab\n", target);
|
2013-08-12 07:32:34 -04:00
|
|
|
|
2014-03-15 09:26:32 -04:00
|
|
|
mountsingle:
|
2014-07-09 11:39:32 -04:00
|
|
|
if (mount(source, target, types, flags, data) < 0) {
|
2014-03-15 14:47:18 -04:00
|
|
|
weprintf("mount: %s:", source);
|
2014-03-22 06:49:57 -04:00
|
|
|
status = EXIT_FAILURE;
|
2014-03-15 14:47:18 -04:00
|
|
|
}
|
2014-07-09 11:39:32 -04:00
|
|
|
if (fp)
|
2014-03-07 08:43:42 -05:00
|
|
|
endmntent(fp);
|
2014-06-07 13:21:16 -04:00
|
|
|
free(resolvpath);
|
2014-03-15 12:10:23 -04:00
|
|
|
return status;
|
2014-03-07 08:43:42 -05:00
|
|
|
|
2014-03-15 09:26:32 -04:00
|
|
|
mountall:
|
2014-07-09 11:39:32 -04:00
|
|
|
if (!(fp = setmntent("/etc/fstab", "r")))
|
2014-03-15 09:26:32 -04:00
|
|
|
eprintf("setmntent %s:", "/etc/fstab");
|
2014-07-09 11:39:32 -04:00
|
|
|
while ((me = getmntent(fp))) {
|
2014-03-15 09:26:32 -04:00
|
|
|
flags = 0;
|
|
|
|
parseopts(me->mnt_opts, &flags, data, datasiz);
|
2014-07-09 11:39:32 -04:00
|
|
|
if (mount(me->mnt_fsname, me->mnt_dir, me->mnt_type, flags, data) < 0) {
|
|
|
|
if (mounted(me->mnt_dir) == 0) {
|
2014-03-18 10:38:31 -04:00
|
|
|
weprintf("mount: %s:", me->mnt_fsname);
|
2014-03-22 06:49:57 -04:00
|
|
|
status = EXIT_FAILURE;
|
|
|
|
}
|
2014-03-15 12:10:23 -04:00
|
|
|
}
|
2013-09-03 09:33:52 -04:00
|
|
|
}
|
2014-03-15 09:26:32 -04:00
|
|
|
endmntent(fp);
|
2013-09-03 09:33:52 -04:00
|
|
|
|
2014-03-15 12:10:23 -04:00
|
|
|
return status;
|
2013-08-12 07:32:34 -04:00
|
|
|
}
|