2014-08-17 09:44:42 +00:00
|
|
|
/*-
|
2018-01-24 16:39:02 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2014-08-17 09:44:42 +00:00
|
|
|
* Copyright (c) 2014 The FreeBSD Foundation
|
|
|
|
*
|
|
|
|
* This software was developed by Edward Tomasz Napierala under sponsorship
|
|
|
|
* from the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-21 15:07:25 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2014-08-17 09:44:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2016-01-24 18:11:36 +00:00
|
|
|
#include <libutil.h>
|
2014-08-17 09:44:42 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "mntopts.h"
|
|
|
|
|
|
|
|
static int
|
|
|
|
unmount_by_statfs(const struct statfs *sb, bool force)
|
|
|
|
{
|
|
|
|
char *fsid_str;
|
|
|
|
int error, ret, flags;
|
|
|
|
|
|
|
|
ret = asprintf(&fsid_str, "FSID:%d:%d",
|
|
|
|
sb->f_fsid.val[0], sb->f_fsid.val[1]);
|
|
|
|
if (ret < 0)
|
|
|
|
log_err(1, "asprintf");
|
|
|
|
|
|
|
|
log_debugx("unmounting %s using %s", sb->f_mntonname, fsid_str);
|
|
|
|
|
|
|
|
flags = MNT_BYFSID;
|
|
|
|
if (force)
|
|
|
|
flags |= MNT_FORCE;
|
|
|
|
error = unmount(fsid_str, flags);
|
|
|
|
free(fsid_str);
|
|
|
|
if (error != 0)
|
|
|
|
log_warn("cannot unmount %s", sb->f_mntonname);
|
2021-02-17 07:51:38 +00:00
|
|
|
else
|
|
|
|
rpc_umntall();
|
2014-08-17 09:44:42 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct statfs *
|
|
|
|
find_statfs(const struct statfs *mntbuf, int nitems, const char *mountpoint)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
if (strcmp(mntbuf[i].f_mntonname, mountpoint) == 0)
|
|
|
|
return (mntbuf + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mount_autofs(const char *from, const char *fspath, const char *options,
|
|
|
|
const char *prefix)
|
|
|
|
{
|
|
|
|
struct iovec *iov = NULL;
|
|
|
|
char errmsg[255];
|
|
|
|
int error, iovlen = 0;
|
|
|
|
|
|
|
|
create_directory(fspath);
|
|
|
|
|
|
|
|
log_debugx("mounting %s on %s, prefix \"%s\", options \"%s\"",
|
|
|
|
from, fspath, prefix, options);
|
|
|
|
memset(errmsg, 0, sizeof(errmsg));
|
|
|
|
|
|
|
|
build_iovec(&iov, &iovlen, "fstype",
|
|
|
|
__DECONST(void *, "autofs"), (size_t)-1);
|
|
|
|
build_iovec(&iov, &iovlen, "fspath",
|
|
|
|
__DECONST(void *, fspath), (size_t)-1);
|
|
|
|
build_iovec(&iov, &iovlen, "from",
|
|
|
|
__DECONST(void *, from), (size_t)-1);
|
|
|
|
build_iovec(&iov, &iovlen, "errmsg",
|
|
|
|
errmsg, sizeof(errmsg));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append the options and mountpoint defined in auto_master(5);
|
|
|
|
* this way automountd(8) does not need to parse it.
|
|
|
|
*/
|
|
|
|
build_iovec(&iov, &iovlen, "master_options",
|
|
|
|
__DECONST(void *, options), (size_t)-1);
|
|
|
|
build_iovec(&iov, &iovlen, "master_prefix",
|
|
|
|
__DECONST(void *, prefix), (size_t)-1);
|
|
|
|
|
|
|
|
error = nmount(iov, iovlen, 0);
|
|
|
|
if (error != 0) {
|
|
|
|
if (*errmsg != '\0') {
|
|
|
|
log_err(1, "cannot mount %s on %s: %s",
|
|
|
|
from, fspath, errmsg);
|
|
|
|
} else {
|
|
|
|
log_err(1, "cannot mount %s on %s", from, fspath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-10 12:25:05 +00:00
|
|
|
mount_if_not_already(const struct node *n, const char *map, const char *options,
|
|
|
|
const char *prefix, const struct statfs *mntbuf, int nitems)
|
2014-08-17 09:44:42 +00:00
|
|
|
{
|
|
|
|
const struct statfs *sb;
|
|
|
|
char *mountpoint;
|
|
|
|
char *from;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = asprintf(&from, "map %s", map);
|
|
|
|
if (ret < 0)
|
|
|
|
log_err(1, "asprintf");
|
|
|
|
|
|
|
|
mountpoint = node_path(n);
|
|
|
|
sb = find_statfs(mntbuf, nitems, mountpoint);
|
|
|
|
if (sb != NULL) {
|
|
|
|
if (strcmp(sb->f_fstypename, "autofs") != 0) {
|
|
|
|
log_debugx("unknown filesystem mounted "
|
|
|
|
"on %s; mounting", mountpoint);
|
|
|
|
/*
|
|
|
|
* XXX: Compare options and 'from',
|
|
|
|
* and update the mount if necessary.
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
log_debugx("autofs already mounted "
|
|
|
|
"on %s", mountpoint);
|
|
|
|
free(from);
|
|
|
|
free(mountpoint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_debugx("nothing mounted on %s; mounting",
|
|
|
|
mountpoint);
|
|
|
|
}
|
|
|
|
|
2015-03-10 12:25:05 +00:00
|
|
|
mount_autofs(from, mountpoint, options, prefix);
|
2014-08-17 09:44:42 +00:00
|
|
|
free(from);
|
|
|
|
free(mountpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mount_unmount(struct node *root)
|
|
|
|
{
|
|
|
|
struct statfs *mntbuf;
|
2015-03-10 12:25:05 +00:00
|
|
|
struct node *n, *n2;
|
2014-08-17 09:44:42 +00:00
|
|
|
int i, nitems;
|
|
|
|
|
|
|
|
nitems = getmntinfo(&mntbuf, MNT_WAIT);
|
|
|
|
if (nitems <= 0)
|
|
|
|
log_err(1, "getmntinfo");
|
|
|
|
|
|
|
|
log_debugx("unmounting stale autofs mounts");
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
|
|
|
|
log_debugx("skipping %s, filesystem type is not autofs",
|
|
|
|
mntbuf[i].f_mntonname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = node_find(root, mntbuf[i].f_mntonname);
|
|
|
|
if (n != NULL) {
|
|
|
|
log_debugx("leaving autofs mounted on %s",
|
|
|
|
mntbuf[i].f_mntonname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debugx("autofs mounted on %s not found "
|
|
|
|
"in new configuration; unmounting", mntbuf[i].f_mntonname);
|
|
|
|
unmount_by_statfs(&(mntbuf[i]), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debugx("mounting new autofs mounts");
|
|
|
|
|
|
|
|
TAILQ_FOREACH(n, &root->n_children, n_next) {
|
|
|
|
if (!node_is_direct_map(n)) {
|
2015-03-10 12:25:05 +00:00
|
|
|
mount_if_not_already(n, n->n_map, n->n_options,
|
|
|
|
n->n_key, mntbuf, nitems);
|
2014-08-17 09:44:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TAILQ_FOREACH(n2, &n->n_children, n_next) {
|
2015-03-10 12:25:05 +00:00
|
|
|
mount_if_not_already(n2, n->n_map, n->n_options,
|
|
|
|
"/", mntbuf, nitems);
|
2014-08-17 09:44:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-22 16:48:29 +00:00
|
|
|
static void
|
|
|
|
flush_autofs(const char *fspath)
|
|
|
|
{
|
|
|
|
struct iovec *iov = NULL;
|
|
|
|
char errmsg[255];
|
|
|
|
int error, iovlen = 0;
|
|
|
|
|
|
|
|
log_debugx("flushing %s", fspath);
|
|
|
|
memset(errmsg, 0, sizeof(errmsg));
|
|
|
|
|
|
|
|
build_iovec(&iov, &iovlen, "fstype",
|
|
|
|
__DECONST(void *, "autofs"), (size_t)-1);
|
|
|
|
build_iovec(&iov, &iovlen, "fspath",
|
|
|
|
__DECONST(void *, fspath), (size_t)-1);
|
|
|
|
build_iovec(&iov, &iovlen, "errmsg",
|
|
|
|
errmsg, sizeof(errmsg));
|
|
|
|
|
|
|
|
error = nmount(iov, iovlen, MNT_UPDATE);
|
|
|
|
if (error != 0) {
|
|
|
|
if (*errmsg != '\0') {
|
|
|
|
log_err(1, "cannot flush %s: %s",
|
|
|
|
fspath, errmsg);
|
|
|
|
} else {
|
|
|
|
log_err(1, "cannot flush %s", fspath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flush_caches(void)
|
|
|
|
{
|
|
|
|
struct statfs *mntbuf;
|
|
|
|
int i, nitems;
|
|
|
|
|
|
|
|
nitems = getmntinfo(&mntbuf, MNT_WAIT);
|
|
|
|
if (nitems <= 0)
|
|
|
|
log_err(1, "getmntinfo");
|
|
|
|
|
|
|
|
log_debugx("flushing autofs caches");
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
|
|
|
|
log_debugx("skipping %s, filesystem type is not autofs",
|
|
|
|
mntbuf[i].f_mntonname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_autofs(mntbuf[i].f_mntonname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-17 09:44:42 +00:00
|
|
|
static void
|
|
|
|
unmount_automounted(bool force)
|
|
|
|
{
|
|
|
|
struct statfs *mntbuf;
|
|
|
|
int i, nitems;
|
|
|
|
|
|
|
|
nitems = getmntinfo(&mntbuf, MNT_WAIT);
|
|
|
|
if (nitems <= 0)
|
|
|
|
log_err(1, "getmntinfo");
|
|
|
|
|
|
|
|
log_debugx("unmounting automounted filesystems");
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
|
|
|
|
log_debugx("skipping %s, filesystem type is autofs",
|
|
|
|
mntbuf[i].f_mntonname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
|
|
|
|
log_debugx("skipping %s, not automounted",
|
|
|
|
mntbuf[i].f_mntonname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount_by_statfs(&(mntbuf[i]), force);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage_automount(void)
|
|
|
|
{
|
|
|
|
|
2014-11-22 16:48:29 +00:00
|
|
|
fprintf(stderr, "usage: automount [-D name=value][-o opts][-Lcfuv]\n");
|
2014-08-17 09:44:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main_automount(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct node *root;
|
|
|
|
int ch, debug = 0, show_maps = 0;
|
|
|
|
char *options = NULL;
|
2014-11-22 16:48:29 +00:00
|
|
|
bool do_unmount = false, force_unmount = false, flush = false;
|
2014-08-17 09:44:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that in automount(8), the only purpose of variable
|
|
|
|
* handling is to aid in debugging maps (automount -L).
|
|
|
|
*/
|
|
|
|
defined_init();
|
|
|
|
|
2014-11-22 16:48:29 +00:00
|
|
|
while ((ch = getopt(argc, argv, "D:Lfco:uv")) != -1) {
|
2014-08-17 09:44:42 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'D':
|
|
|
|
defined_parse_and_add(optarg);
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
show_maps++;
|
|
|
|
break;
|
2014-11-22 16:48:29 +00:00
|
|
|
case 'c':
|
|
|
|
flush = true;
|
|
|
|
break;
|
2014-08-17 09:44:42 +00:00
|
|
|
case 'f':
|
|
|
|
force_unmount = true;
|
|
|
|
break;
|
|
|
|
case 'o':
|
2015-03-12 12:17:15 +00:00
|
|
|
options = concat(options, ',', optarg);
|
2014-08-17 09:44:42 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
do_unmount = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
debug++;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage_automount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
if (argc != 0)
|
|
|
|
usage_automount();
|
|
|
|
|
|
|
|
if (force_unmount && !do_unmount)
|
|
|
|
usage_automount();
|
|
|
|
|
|
|
|
log_init(debug);
|
|
|
|
|
2014-11-22 16:48:29 +00:00
|
|
|
if (flush) {
|
|
|
|
flush_caches();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-08-17 09:44:42 +00:00
|
|
|
if (do_unmount) {
|
|
|
|
unmount_automounted(force_unmount);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
root = node_new_root();
|
|
|
|
parse_master(root, AUTO_MASTER_PATH);
|
|
|
|
|
|
|
|
if (show_maps) {
|
|
|
|
if (show_maps > 1) {
|
|
|
|
node_expand_indirect_maps(root);
|
|
|
|
node_expand_ampersand(root, NULL);
|
|
|
|
}
|
|
|
|
node_expand_defined(root);
|
2015-03-12 12:36:08 +00:00
|
|
|
node_print(root, options);
|
2014-08-17 09:44:42 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
mount_unmount(root);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|