[BACK]Return to mount_puffs.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / mount_puffs

File: [cvs.NetBSD.org] / src / sbin / mount_puffs / mount_puffs.c (download)

Revision 1.5, Wed Nov 23 14:33:29 2016 UTC (3 years, 3 months ago) by pho
Branch: MAIN
CVS Tags: prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, matt-nb8-mediatek-base, matt-nb8-mediatek, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, HEAD
Changes since 1.4: +115 -45 lines

Major rework on mount_puffs(8) so that it can actually start file servers

Now you can do
  # mount_puffs -o rdonly rot13fs#/home/foo /mnt/rot13

or in fstab
  rot13fs#/home/foo  /mnt/rot13  puffs  rdonly

to start rot13fs with arguments identical to
  # rot13fs -o rdonly /home/foo /mnt/rot13

/*	$NetBSD: mount_puffs.c,v 1.5 2016/11/23 14:33:29 pho Exp $	*/

/*
 * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
 *
 * 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 ``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.
 */

/*
 * This is to support -o getargs without having to replicate it in
 * every file server. It also allows puffs filesystems to be mounted
 * via "mount -a".
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mount_puffs.c,v 1.5 2016/11/23 14:33:29 pho Exp $");
#endif /* !lint */

#include <sys/param.h>
#include <sys/mount.h>

#include <fs/puffs/puffs_msgif.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>
#include <unistd.h>
#include <util.h>

static int
usage(void)
{

	fprintf(stderr, "usage: %s [-o options] program[#source] mountpoint\n", getprogname());
	return 1;
}

static int show_puffs_mount_args(const char *mountpoint)
{
	const char *vtypes[] = { VNODE_TYPES };
	struct puffs_kargs kargs;

	if (mount(MOUNT_PUFFS, mountpoint, MNT_GETARGS, &kargs, sizeof(kargs)) == -1)
		err(1, "mount");

	printf("version=%d, ", kargs.pa_vers);
	printf("flags=0x%x, ", kargs.pa_flags);

	printf("root cookie=%p, ", kargs.pa_root_cookie);
	printf("root type=%s", vtypes[kargs.pa_root_vtype]);

	if (kargs.pa_root_vtype != VDIR)
		printf(", root size=%llu",
		    (unsigned long long)kargs.pa_root_vsize);
	if (kargs.pa_root_vtype == VCHR || kargs.pa_root_vtype == VBLK)
		printf(", root rdev=0x%" PRIx64, (uint64_t)kargs.pa_root_rdev);
	printf("\n");

	return 0;
}

static int
mount_puffs_filesystem(const char *program, const char *opts,
					const char *source, const char *mountpoint)
{
	int argc = 0;
	const char **argv;
	int rv = 0;

	/* Construct an argument vector:
	 * program [-o opts] [source] mountpoint */
	argv = ecalloc(1 + 2 + 1 + 1, sizeof(*argv));
	argv[argc++] = program;
	if (opts != NULL) {
		argv[argc++] = "-o";
		argv[argc++] = opts;
	}
	if (source != NULL) {
		argv[argc++] = source;
	}
	argv[argc++] = mountpoint;
	argv[argc] = NULL;

	/* We intentionally use execvp(3) here because the program can
	 * actually be a basename. */
	if (execvp(program, __UNCONST(argv)) == -1) {
		warn("Cannot execute %s", program);
		rv = 1;
	}

	free(argv);
	return rv;
}

static void add_opt(char **opts, const char *opt)
{
	const size_t orig_len = *opts == NULL ? 0 : strlen(*opts);

	*opts = erealloc(*opts, orig_len + 1 + strlen(opt) + 1);

	if (orig_len == 0) {
		strcpy(*opts, opt);
	}
	else {
		strcat(*opts, ",");
		strcat(*opts, opt);
	}
}

int
main(int argc, char *argv[])
{
	int mntflags = 0;
	int ch;
	char *opts = NULL;
	int rv = 0;

	while ((ch = getopt(argc, argv, "o:")) != -1) {
		switch (ch) {
		case 'o':
			for (char *opt = optarg; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
				if (strcmp(opt, "getargs") == 0) {
					mntflags |= MNT_GETARGS;
					break; /* No need to parse it any further. */
				}
				else {
					add_opt(&opts, opt);
				}
			}
			break;
		default:
			rv = usage();
			goto free_opts;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 2) {
		rv = usage();
		goto free_opts;
	}

	if (mntflags & MNT_GETARGS) {
		/* Special case for -o getargs: retrieve kernel arguments for
		 * an already mounted filesystem. */
		rv = show_puffs_mount_args(argv[1]);
	}
	else {
		/* Split the program name and source. This is to allow
		 * filesystems to be mounted via "mount -a" i.e. /etc/fstab */
		char *source  = argv[0];
		char *program = strsep(&source, "#");

		rv = mount_puffs_filesystem(program, opts, source, argv[1]);
	}

free_opts:
	free(opts);
	return rv;
}