Home | History | Annotate | Line # | Download | only in mount_autofs
mount_autofs.c revision 1.3.4.1
      1  1.3.4.1    martin /*	$NetBSD: mount_autofs.c,v 1.3.4.1 2020/04/08 14:07:19 martin Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5      1.1  christos  * All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  christos  * by Christos Zoulas.
      9      1.1  christos  *
     10      1.1  christos  * Redistribution and use in source and binary forms, with or without
     11      1.1  christos  * modification, are permitted provided that the following conditions
     12      1.1  christos  * are met:
     13      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  christos  *    documentation and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  christos  */
     31      1.1  christos #include <sys/cdefs.h>
     32      1.1  christos #ifndef lint
     33  1.3.4.1    martin __RCSID("$NetBSD: mount_autofs.c,v 1.3.4.1 2020/04/08 14:07:19 martin Exp $");
     34      1.1  christos #endif /* not lint */
     35      1.1  christos 
     36      1.1  christos #include <sys/param.h>
     37      1.1  christos #include <sys/mount.h>
     38      1.1  christos 
     39      1.1  christos #include <err.h>
     40      1.1  christos #include <unistd.h>
     41      1.1  christos #include <stdio.h>
     42      1.1  christos #include <stdlib.h>
     43      1.1  christos #include <string.h>
     44      1.1  christos 
     45      1.1  christos #include <mntopts.h>
     46      1.1  christos 
     47      1.1  christos #include <fs/autofs/autofs_mount.h>
     48      1.1  christos 
     49      1.1  christos #include "mount_autofs.h"
     50      1.1  christos 
     51      1.1  christos static const struct mntopt mopts[] = {
     52      1.1  christos 	MOPT_STDOPTS,
     53      1.1  christos 	MOPT_GETARGS,
     54      1.1  christos 	MOPT_NULL,
     55      1.1  christos };
     56      1.1  christos 
     57      1.1  christos int	mount_autofs(int argc, char **argv);
     58      1.1  christos __dead static void	usage(void);
     59      1.1  christos 
     60      1.1  christos #ifndef MOUNT_NOMAIN
     61      1.1  christos int
     62      1.1  christos main(int argc, char **argv)
     63      1.1  christos {
     64      1.1  christos 	return mount_autofs(argc, argv);
     65      1.1  christos }
     66      1.1  christos #endif
     67      1.1  christos 
     68      1.1  christos void
     69      1.1  christos mount_autofs_parseargs(int argc, char *argv[], void *v, int *mntflags,
     70      1.1  christos 	char *canon_dev, char *canon_dir)
     71      1.1  christos {
     72      1.1  christos 	int ch;
     73      1.1  christos 	mntoptparse_t mp;
     74      1.1  christos 	struct autofs_args *am = v;
     75      1.1  christos 
     76      1.1  christos 	*mntflags = 0;
     77      1.1  christos 	while ((ch = getopt(argc, argv, "f:o:O:p:")) != -1)
     78      1.1  christos 		switch (ch) {
     79      1.1  christos 		case 'f':
     80      1.1  christos 			strlcpy(am->from, optarg, MAXPATHLEN);
     81      1.1  christos 			break;
     82      1.1  christos 		case 'o':
     83      1.1  christos 			mp = getmntopts(optarg, mopts, mntflags, 0);
     84      1.1  christos 			if (mp == NULL)
     85      1.1  christos 				err(EXIT_FAILURE, "getmntopts");
     86      1.1  christos 			freemntopts(mp);
     87      1.1  christos 			break;
     88      1.1  christos 		case 'O':
     89      1.1  christos 			strlcpy(am->master_options, optarg, MAXPATHLEN);
     90      1.1  christos 			break;
     91      1.1  christos 		case 'p':
     92      1.1  christos 			strlcpy(am->master_prefix, optarg, MAXPATHLEN);
     93      1.1  christos 			break;
     94      1.1  christos 		case '?':
     95      1.1  christos 		default:
     96      1.1  christos 			usage();
     97      1.1  christos 		}
     98      1.1  christos 	argc -= optind;
     99      1.1  christos 	argv += optind;
    100      1.1  christos 
    101      1.1  christos 	if (argc != 2)
    102      1.1  christos 		usage();
    103      1.1  christos 
    104      1.1  christos 	strlcpy(canon_dev, argv[0], MAXPATHLEN);
    105      1.1  christos 	if (realpath(argv[1], canon_dir) == NULL)    /* Check mounton path */
    106      1.1  christos 		err(EXIT_FAILURE, "realpath %s", canon_dir);
    107      1.1  christos 	if (strncmp(argv[1], canon_dir, MAXPATHLEN)) {
    108      1.1  christos 		warnx("\"%s\" is a relative path.", argv[1]);
    109      1.1  christos 		warnx("using \"%s\" instead.", canon_dir);
    110      1.1  christos 	}
    111      1.1  christos }
    112      1.1  christos 
    113      1.1  christos int
    114      1.1  christos mount_autofs(int argc, char *argv[])
    115      1.1  christos {
    116      1.1  christos 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    117      1.1  christos 	char from[MAXPATHLEN], master_options[MAXPATHLEN];
    118      1.1  christos 	char master_prefix[MAXPATHLEN];
    119      1.1  christos 	int mntflags;
    120      1.1  christos 	struct autofs_args am = {
    121      1.1  christos 		.from = from,
    122      1.1  christos 		.master_options = master_options,
    123      1.1  christos 		.master_prefix = master_prefix,
    124      1.1  christos 	};
    125      1.1  christos 
    126      1.1  christos 	mount_autofs_parseargs(argc, argv, &am, &mntflags,
    127      1.1  christos 	    canon_dev, canon_dir);
    128      1.1  christos 	if (mount(MOUNT_KERNFS, canon_dir, mntflags, &am, sizeof(am)) == -1)
    129      1.3       wiz 		err(EXIT_FAILURE, "autofs on %s", canon_dir);
    130      1.1  christos         if (mntflags & MNT_GETARGS) {
    131      1.1  christos 		printf("from=%s, master_options=%s, master_prefix=%s\n",
    132      1.1  christos 		    am.from, am.master_options, am.master_prefix);
    133      1.1  christos 	}
    134      1.1  christos 
    135      1.1  christos 	return EXIT_SUCCESS;
    136      1.1  christos }
    137      1.1  christos 
    138      1.1  christos static void
    139      1.1  christos usage(void)
    140      1.1  christos {
    141      1.1  christos 	(void)fprintf(stderr,
    142      1.2       wiz 	    "Usage: %s [-f from] [-O autofs_options] [-o options] [-p prefix] "
    143      1.2       wiz 		"autofs mount_point\n", getprogname());
    144      1.1  christos 	exit(EXIT_FAILURE);
    145      1.1  christos }
    146