mount_puffs.c revision 1.4.24.1       1  1.4.24.1  pgoyette /*	$NetBSD: mount_puffs.c,v 1.4.24.1 2017/01/07 08:56:06 pgoyette Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4       1.1     pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5       1.1     pooka  *
      6       1.1     pooka  * Redistribution and use in source and binary forms, with or without
      7       1.1     pooka  * modification, are permitted provided that the following conditions
      8       1.1     pooka  * are met:
      9       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     10       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     11       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     13       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     14       1.1     pooka  *
     15       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25       1.1     pooka  * SUCH DAMAGE.
     26       1.1     pooka  */
     27       1.1     pooka 
     28       1.1     pooka /*
     29  1.4.24.1  pgoyette  * This is to support -o getargs without having to replicate it in
     30  1.4.24.1  pgoyette  * every file server. It also allows puffs filesystems to be mounted
     31  1.4.24.1  pgoyette  * via "mount -a".
     32       1.1     pooka  */
     33       1.1     pooka 
     34       1.1     pooka #include <sys/cdefs.h>
     35       1.1     pooka #ifndef lint
     36  1.4.24.1  pgoyette __RCSID("$NetBSD: mount_puffs.c,v 1.4.24.1 2017/01/07 08:56:06 pgoyette Exp $");
     37       1.1     pooka #endif /* !lint */
     38       1.1     pooka 
     39       1.1     pooka #include <sys/param.h>
     40       1.1     pooka #include <sys/mount.h>
     41       1.1     pooka 
     42       1.1     pooka #include <fs/puffs/puffs_msgif.h>
     43       1.1     pooka 
     44       1.1     pooka #include <err.h>
     45       1.1     pooka #include <stdio.h>
     46       1.1     pooka #include <stdlib.h>
     47  1.4.24.1  pgoyette #include <string.h>
     48  1.4.24.1  pgoyette #include <sys/cdefs.h>
     49       1.1     pooka #include <unistd.h>
     50  1.4.24.1  pgoyette #include <util.h>
     51       1.1     pooka 
     52  1.4.24.1  pgoyette static int
     53       1.1     pooka usage(void)
     54       1.1     pooka {
     55       1.1     pooka 
     56  1.4.24.1  pgoyette 	fprintf(stderr, "usage: %s [-o options] program[#source] mountpoint\n", getprogname());
     57  1.4.24.1  pgoyette 	return 1;
     58       1.1     pooka }
     59       1.1     pooka 
     60  1.4.24.1  pgoyette static int show_puffs_mount_args(const char *mountpoint)
     61       1.1     pooka {
     62       1.1     pooka 	const char *vtypes[] = { VNODE_TYPES };
     63       1.1     pooka 	struct puffs_kargs kargs;
     64       1.1     pooka 
     65  1.4.24.1  pgoyette 	if (mount(MOUNT_PUFFS, mountpoint, MNT_GETARGS, &kargs, sizeof(kargs)) == -1)
     66       1.1     pooka 		err(1, "mount");
     67  1.4.24.1  pgoyette 
     68       1.3     pooka 	printf("version=%d, ", kargs.pa_vers);
     69       1.1     pooka 	printf("flags=0x%x, ", kargs.pa_flags);
     70       1.1     pooka 
     71       1.1     pooka 	printf("root cookie=%p, ", kargs.pa_root_cookie);
     72       1.1     pooka 	printf("root type=%s", vtypes[kargs.pa_root_vtype]);
     73       1.1     pooka 
     74       1.1     pooka 	if (kargs.pa_root_vtype != VDIR)
     75       1.1     pooka 		printf(", root size=%llu",
     76       1.1     pooka 		    (unsigned long long)kargs.pa_root_vsize);
     77       1.1     pooka 	if (kargs.pa_root_vtype == VCHR || kargs.pa_root_vtype == VBLK)
     78       1.1     pooka 		printf(", root rdev=0x%" PRIx64, (uint64_t)kargs.pa_root_rdev);
     79       1.1     pooka 	printf("\n");
     80       1.2     pooka 
     81       1.2     pooka 	return 0;
     82       1.1     pooka }
     83  1.4.24.1  pgoyette 
     84  1.4.24.1  pgoyette static int
     85  1.4.24.1  pgoyette mount_puffs_filesystem(const char *program, const char *opts,
     86  1.4.24.1  pgoyette 					const char *source, const char *mountpoint)
     87  1.4.24.1  pgoyette {
     88  1.4.24.1  pgoyette 	int argc = 0;
     89  1.4.24.1  pgoyette 	const char **argv;
     90  1.4.24.1  pgoyette 	int rv = 0;
     91  1.4.24.1  pgoyette 
     92  1.4.24.1  pgoyette 	/* Construct an argument vector:
     93  1.4.24.1  pgoyette 	 * program [-o opts] [source] mountpoint */
     94  1.4.24.1  pgoyette 	argv = ecalloc(1 + 2 + 1 + 1, sizeof(*argv));
     95  1.4.24.1  pgoyette 	argv[argc++] = program;
     96  1.4.24.1  pgoyette 	if (opts != NULL) {
     97  1.4.24.1  pgoyette 		argv[argc++] = "-o";
     98  1.4.24.1  pgoyette 		argv[argc++] = opts;
     99  1.4.24.1  pgoyette 	}
    100  1.4.24.1  pgoyette 	if (source != NULL) {
    101  1.4.24.1  pgoyette 		argv[argc++] = source;
    102  1.4.24.1  pgoyette 	}
    103  1.4.24.1  pgoyette 	argv[argc++] = mountpoint;
    104  1.4.24.1  pgoyette 	argv[argc] = NULL;
    105  1.4.24.1  pgoyette 
    106  1.4.24.1  pgoyette 	/* We intentionally use execvp(3) here because the program can
    107  1.4.24.1  pgoyette 	 * actually be a basename. */
    108  1.4.24.1  pgoyette 	if (execvp(program, __UNCONST(argv)) == -1) {
    109  1.4.24.1  pgoyette 		warn("Cannot execute %s", program);
    110  1.4.24.1  pgoyette 		rv = 1;
    111  1.4.24.1  pgoyette 	}
    112  1.4.24.1  pgoyette 
    113  1.4.24.1  pgoyette 	free(argv);
    114  1.4.24.1  pgoyette 	return rv;
    115  1.4.24.1  pgoyette }
    116  1.4.24.1  pgoyette 
    117  1.4.24.1  pgoyette static void add_opt(char **opts, const char *opt)
    118  1.4.24.1  pgoyette {
    119  1.4.24.1  pgoyette 	const size_t orig_len = *opts == NULL ? 0 : strlen(*opts);
    120  1.4.24.1  pgoyette 
    121  1.4.24.1  pgoyette 	*opts = erealloc(*opts, orig_len + 1 + strlen(opt) + 1);
    122  1.4.24.1  pgoyette 
    123  1.4.24.1  pgoyette 	if (orig_len == 0) {
    124  1.4.24.1  pgoyette 		strcpy(*opts, opt);
    125  1.4.24.1  pgoyette 	}
    126  1.4.24.1  pgoyette 	else {
    127  1.4.24.1  pgoyette 		strcat(*opts, ",");
    128  1.4.24.1  pgoyette 		strcat(*opts, opt);
    129  1.4.24.1  pgoyette 	}
    130  1.4.24.1  pgoyette }
    131  1.4.24.1  pgoyette 
    132  1.4.24.1  pgoyette int
    133  1.4.24.1  pgoyette main(int argc, char *argv[])
    134  1.4.24.1  pgoyette {
    135  1.4.24.1  pgoyette 	int mntflags = 0;
    136  1.4.24.1  pgoyette 	int ch;
    137  1.4.24.1  pgoyette 	char *opts = NULL;
    138  1.4.24.1  pgoyette 	int rv = 0;
    139  1.4.24.1  pgoyette 
    140  1.4.24.1  pgoyette 	while ((ch = getopt(argc, argv, "o:")) != -1) {
    141  1.4.24.1  pgoyette 		switch (ch) {
    142  1.4.24.1  pgoyette 		case 'o':
    143  1.4.24.1  pgoyette 			for (char *opt = optarg; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    144  1.4.24.1  pgoyette 				if (strcmp(opt, "getargs") == 0) {
    145  1.4.24.1  pgoyette 					mntflags |= MNT_GETARGS;
    146  1.4.24.1  pgoyette 					break; /* No need to parse it any further. */
    147  1.4.24.1  pgoyette 				}
    148  1.4.24.1  pgoyette 				else {
    149  1.4.24.1  pgoyette 					add_opt(&opts, opt);
    150  1.4.24.1  pgoyette 				}
    151  1.4.24.1  pgoyette 			}
    152  1.4.24.1  pgoyette 			break;
    153  1.4.24.1  pgoyette 		default:
    154  1.4.24.1  pgoyette 			rv = usage();
    155  1.4.24.1  pgoyette 			goto free_opts;
    156  1.4.24.1  pgoyette 		}
    157  1.4.24.1  pgoyette 	}
    158  1.4.24.1  pgoyette 	argc -= optind;
    159  1.4.24.1  pgoyette 	argv += optind;
    160  1.4.24.1  pgoyette 
    161  1.4.24.1  pgoyette 	if (argc != 2) {
    162  1.4.24.1  pgoyette 		rv = usage();
    163  1.4.24.1  pgoyette 		goto free_opts;
    164  1.4.24.1  pgoyette 	}
    165  1.4.24.1  pgoyette 
    166  1.4.24.1  pgoyette 	if (mntflags & MNT_GETARGS) {
    167  1.4.24.1  pgoyette 		/* Special case for -o getargs: retrieve kernel arguments for
    168  1.4.24.1  pgoyette 		 * an already mounted filesystem. */
    169  1.4.24.1  pgoyette 		rv = show_puffs_mount_args(argv[1]);
    170  1.4.24.1  pgoyette 	}
    171  1.4.24.1  pgoyette 	else {
    172  1.4.24.1  pgoyette 		/* Split the program name and source. This is to allow
    173  1.4.24.1  pgoyette 		 * filesystems to be mounted via "mount -a" i.e. /etc/fstab */
    174  1.4.24.1  pgoyette 		char *source  = argv[0];
    175  1.4.24.1  pgoyette 		char *program = strsep(&source, "#");
    176  1.4.24.1  pgoyette 
    177  1.4.24.1  pgoyette 		rv = mount_puffs_filesystem(program, opts, source, argv[1]);
    178  1.4.24.1  pgoyette 	}
    179  1.4.24.1  pgoyette 
    180  1.4.24.1  pgoyette free_opts:
    181  1.4.24.1  pgoyette 	free(opts);
    182  1.4.24.1  pgoyette 	return rv;
    183  1.4.24.1  pgoyette }
    184