Blame modules/mount_bind.c

Packit 8480eb
/* ----------------------------------------------------------------------- *
Packit 8480eb
 *   
Packit 8480eb
 *  mount_bind.c      - module to mount a local filesystem if possible;
Packit 8480eb
 *			otherwise create a symlink.
Packit 8480eb
 *
Packit 8480eb
 *   Copyright 2000 Transmeta Corporation - All Rights Reserved
Packit 8480eb
 *
Packit 8480eb
 *   This program is free software; you can redistribute it and/or modify
Packit 8480eb
 *   it under the terms of the GNU General Public License as published by
Packit 8480eb
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
Packit 8480eb
 *   USA; either version 2 of the License, or (at your option) any later
Packit 8480eb
 *   version; incorporated herein by reference.
Packit 8480eb
 *
Packit 8480eb
 * ----------------------------------------------------------------------- */
Packit 8480eb
Packit 8480eb
#include <stdio.h>
Packit 8480eb
#include <malloc.h>
Packit 8480eb
#include <string.h>
Packit 8480eb
#include <stdlib.h>
Packit 8480eb
#include <sys/param.h>
Packit 8480eb
#include <sys/types.h>
Packit 8480eb
#include <sys/stat.h>
Packit 8480eb
Packit 8480eb
#define MODULE_MOUNT
Packit 8480eb
#include "automount.h"
Packit 8480eb
Packit 8480eb
#define MODPREFIX "mount(bind): "
Packit 8480eb
Packit 8480eb
int mount_version = AUTOFS_MOUNT_VERSION;	/* Required by protocol */
Packit 8480eb
Packit 8480eb
static int bind_works = 0;
Packit 8480eb
Packit 8480eb
int mount_init(void **context)
Packit 8480eb
{
Packit 8480eb
	char tmp1[] = "/tmp/autoXXXXXX", *t1_dir;
Packit 8480eb
	char tmp2[] = "/tmp/autoXXXXXX", *t2_dir;
Packit 8480eb
	int err;
Packit 8480eb
	struct stat st1, st2;
Packit 8480eb
Packit 8480eb
	t1_dir = mkdtemp(tmp1);
Packit 8480eb
	t2_dir = mkdtemp(tmp2);
Packit 8480eb
	if (t1_dir == NULL || t2_dir == NULL) {
Packit 8480eb
		if (t1_dir)
Packit 8480eb
			rmdir(t1_dir);
Packit 8480eb
		if (t2_dir)
Packit 8480eb
			rmdir(t2_dir);
Packit 8480eb
		return 0;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	if (lstat(t1_dir, &st1) == -1)
Packit 8480eb
		goto out;
Packit 8480eb
Packit 8480eb
	err = spawn_mount(LOGOPT_NONE, "-n", "--bind", t1_dir, t2_dir, NULL);
Packit 8480eb
	if (err == 0 &&
Packit 8480eb
	    lstat(t2_dir, &st2) == 0 &&
Packit 8480eb
	    st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
Packit 8480eb
		bind_works = 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	if (spawn_umount(LOGOPT_NONE, "-n", t2_dir, NULL) != 0)
Packit 8480eb
		debug(LOGOPT_ANY, MODPREFIX "umount failed for %s", t2_dir);
Packit 8480eb
Packit 8480eb
out:
Packit 8480eb
	rmdir(t1_dir);
Packit 8480eb
	rmdir(t2_dir);
Packit 8480eb
Packit 8480eb
	return 0;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int mount_reinit(void **context)
Packit 8480eb
{
Packit 8480eb
	return 0;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int mount_mount(struct autofs_point *ap, const char *root, const char *name, int name_len,
Packit 8480eb
		const char *what, const char *fstype, const char *options, void *context)
Packit 8480eb
{
Packit 8480eb
	char fullpath[PATH_MAX];
Packit 8480eb
	char buf[MAX_ERR_BUF];
Packit 8480eb
	int err;
Packit 8480eb
	int i, len;
Packit 8480eb
	int symlnk = (*name != '/' && (ap->flags & MOUNT_FLAG_SYMLINK));
Packit 8480eb
Packit 8480eb
	if (ap->flags & MOUNT_FLAG_REMOUNT)
Packit 8480eb
		return 0;
Packit 8480eb
Packit 8480eb
	/* Extract "symlink" pseudo-option which forces local filesystems
Packit 8480eb
	 * to be symlinked instead of bound.
Packit 8480eb
	 */
Packit 8480eb
	if (*name != '/' && !symlnk && options) {
Packit 8480eb
		const char *comma;
Packit 8480eb
		int o_len = strlen(options) + 1;
Packit 8480eb
Packit 8480eb
		for (comma = options; *comma != '\0';) {
Packit 8480eb
			const char *cp;
Packit 8480eb
			const char *end;
Packit 8480eb
Packit 8480eb
			while (*comma == ',')
Packit 8480eb
				comma++;
Packit 8480eb
Packit 8480eb
			/* Skip leading white space */
Packit 8480eb
			while (*comma == ' ' || *comma == '\t')
Packit 8480eb
				comma++;
Packit 8480eb
Packit 8480eb
			cp = comma;
Packit 8480eb
			while (*comma != '\0' && *comma != ',')
Packit 8480eb
				comma++;
Packit 8480eb
Packit 8480eb
			/* Skip trailing white space */
Packit 8480eb
			end = comma - 1;
Packit 8480eb
			while (*comma == ' ' || *comma == '\t')
Packit 8480eb
				end--;
Packit 8480eb
Packit 8480eb
			o_len = end - cp + 1;
Packit 8480eb
			if (_strncmp("symlink", cp, o_len) == 0)
Packit 8480eb
				symlnk = 1;
Packit 8480eb
		}
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	/* Root offset of multi-mount */
Packit 8480eb
	len = strlen(root);
Packit 8480eb
	if (root[len - 1] == '/') {
Packit 8480eb
		len = snprintf(fullpath, len, "%s", root);
Packit 8480eb
	} else if (*name == '/') {
Packit 8480eb
		/*
Packit 8480eb
		 * Direct or offset mount, name is absolute path so
Packit 8480eb
		 * don't use root (but with move mount changes root
Packit 8480eb
		 * is now the same as name).
Packit 8480eb
		 */
Packit 8480eb
		len = sprintf(fullpath, "%s", root);
Packit 8480eb
	} else {
Packit 8480eb
		len = sprintf(fullpath, "%s/%s", root, name);
Packit 8480eb
	}
Packit 8480eb
	fullpath[len] = '\0';
Packit 8480eb
Packit 8480eb
	i = len;
Packit 8480eb
	while (--i > 0 && fullpath[i] == '/')
Packit 8480eb
		fullpath[i] = '\0';
Packit 8480eb
Packit 8480eb
	if (options == NULL || *options == '\0')
Packit 8480eb
		options = "defaults";
Packit 8480eb
Packit 8480eb
	if (!strcmp(what, fullpath)) {
Packit 8480eb
		debug(ap->logopt, MODPREFIX
Packit 8480eb
		     "cannot mount or symlink %s to itself", fullpath);
Packit 8480eb
		return 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	if (!symlnk && bind_works) {
Packit 8480eb
		int status, existed = 1;
Packit 8480eb
Packit 8480eb
		debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath);
Packit 8480eb
Packit Service 2798b2
		status = mkdir_path(fullpath, mp_mode);
Packit 8480eb
		if (status && errno != EEXIST) {
Packit 8480eb
			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Packit 8480eb
			error(ap->logopt,
Packit 8480eb
			      MODPREFIX "mkdir_path %s failed: %s",
Packit 8480eb
			      fullpath, estr);
Packit 8480eb
			return 1;
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		if (!status)
Packit 8480eb
			existed = 0;
Packit 8480eb
Packit Service 145c60
		debug(ap->logopt, MODPREFIX
Packit Service 145c60
		      "calling mount --bind -o %s %s %s",
Packit Service 145c60
		      options, what, fullpath);
Packit 8480eb
Packit 8480eb
		err = spawn_bind_mount(ap->logopt, "-o",
Packit 8480eb
				       options, what, fullpath, NULL);
Packit 8480eb
Packit 8480eb
		if (err) {
Packit 8480eb
			if (ap->type != LKP_INDIRECT)
Packit 8480eb
				return 1;
Packit 8480eb
Packit 8480eb
			if (!existed &&
Packit 8480eb
			   (!(ap->flags & MOUNT_FLAG_GHOST) && name_len))
Packit 8480eb
				rmdir_path(ap, fullpath, ap->dev);
Packit 8480eb
Packit 8480eb
			return err;
Packit 8480eb
		} else {
Packit 8480eb
			debug(ap->logopt,
Packit 8480eb
			      MODPREFIX "mounted %s type %s on %s",
Packit 8480eb
			      what, fstype, fullpath);
Packit Service 145c60
			return 0;
Packit 8480eb
		}
Packit 8480eb
	} else {
Packit 8480eb
		char *cp;
Packit 8480eb
		char basepath[PATH_MAX];
Packit 8480eb
		int status;
Packit 8480eb
		struct stat st;
Packit 8480eb
Packit 8480eb
		strcpy(basepath, fullpath);
Packit 8480eb
		cp = strrchr(basepath, '/');
Packit 8480eb
Packit 8480eb
		if (cp != NULL && cp != basepath)
Packit 8480eb
			*cp = '\0';
Packit 8480eb
Packit 8480eb
		if ((status = stat(fullpath, &st)) == 0) {
Packit 8480eb
			if (S_ISDIR(st.st_mode))
Packit 8480eb
				rmdir(fullpath);
Packit 8480eb
		} else {
Packit 8480eb
			debug(ap->logopt,
Packit 8480eb
			      MODPREFIX "calling mkdir_path %s", basepath);
Packit Service 2798b2
			if (mkdir_path(basepath, mp_mode) && errno != EEXIST) {
Packit 8480eb
				char *estr;
Packit 8480eb
				estr = strerror_r(errno, buf, MAX_ERR_BUF);
Packit 8480eb
				error(ap->logopt,
Packit 8480eb
				      MODPREFIX "mkdir_path %s failed: %s",
Packit 8480eb
				      basepath, estr);
Packit 8480eb
				return 1;
Packit 8480eb
			}
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		if (symlink(what, fullpath) && errno != EEXIST) {
Packit 8480eb
			error(ap->logopt,
Packit 8480eb
			      MODPREFIX
Packit 8480eb
			      "failed to create symlink %s -> %s",
Packit 8480eb
			      fullpath, what);
Packit 8480eb
			if ((ap->flags & MOUNT_FLAG_GHOST) && !status) {
Packit Service 2798b2
				if (mkdir_path(fullpath, mp_mode) && errno != EEXIST) {
Packit 8480eb
					char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Packit 8480eb
					error(ap->logopt,
Packit 8480eb
					      MODPREFIX "mkdir_path %s failed: %s",
Packit 8480eb
					      fullpath, estr);
Packit 8480eb
				}
Packit 8480eb
			} else {
Packit 8480eb
				if (ap->type == LKP_INDIRECT)
Packit 8480eb
					rmdir_path(ap, fullpath, ap->dev);
Packit 8480eb
			}
Packit 8480eb
			return 1;
Packit 8480eb
		} else {
Packit 8480eb
			debug(ap->logopt,
Packit 8480eb
			      MODPREFIX "symlinked %s -> %s", fullpath, what);
Packit 8480eb
			return 0;
Packit 8480eb
		}
Packit 8480eb
	}
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int mount_done(void *context)
Packit 8480eb
{
Packit 8480eb
	return 0;
Packit 8480eb
}