1 1.5 mlelstv /* $NetBSD: mount_autofs.c,v 1.5 2020/07/26 08:20:22 mlelstv 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.5 mlelstv __RCSID("$NetBSD: mount_autofs.c,v 1.5 2020/07/26 08:20:22 mlelstv 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.5 mlelstv #include "mountprog.h" 50 1.1 christos #include "mount_autofs.h" 51 1.1 christos 52 1.1 christos static const struct mntopt mopts[] = { 53 1.1 christos MOPT_STDOPTS, 54 1.1 christos MOPT_GETARGS, 55 1.1 christos MOPT_NULL, 56 1.1 christos }; 57 1.1 christos 58 1.1 christos int mount_autofs(int argc, char **argv); 59 1.1 christos __dead static void usage(void); 60 1.1 christos 61 1.1 christos #ifndef MOUNT_NOMAIN 62 1.1 christos int 63 1.1 christos main(int argc, char **argv) 64 1.1 christos { 65 1.1 christos return mount_autofs(argc, argv); 66 1.1 christos } 67 1.1 christos #endif 68 1.1 christos 69 1.1 christos void 70 1.1 christos mount_autofs_parseargs(int argc, char *argv[], void *v, int *mntflags, 71 1.1 christos char *canon_dev, char *canon_dir) 72 1.1 christos { 73 1.1 christos int ch; 74 1.1 christos mntoptparse_t mp; 75 1.1 christos struct autofs_args *am = v; 76 1.1 christos 77 1.1 christos *mntflags = 0; 78 1.1 christos while ((ch = getopt(argc, argv, "f:o:O:p:")) != -1) 79 1.1 christos switch (ch) { 80 1.1 christos case 'f': 81 1.1 christos strlcpy(am->from, optarg, MAXPATHLEN); 82 1.1 christos break; 83 1.1 christos case 'o': 84 1.1 christos mp = getmntopts(optarg, mopts, mntflags, 0); 85 1.1 christos if (mp == NULL) 86 1.1 christos err(EXIT_FAILURE, "getmntopts"); 87 1.1 christos freemntopts(mp); 88 1.1 christos break; 89 1.1 christos case 'O': 90 1.1 christos strlcpy(am->master_options, optarg, MAXPATHLEN); 91 1.1 christos break; 92 1.1 christos case 'p': 93 1.1 christos strlcpy(am->master_prefix, optarg, MAXPATHLEN); 94 1.1 christos break; 95 1.1 christos case '?': 96 1.1 christos default: 97 1.1 christos usage(); 98 1.1 christos } 99 1.1 christos argc -= optind; 100 1.1 christos argv += optind; 101 1.1 christos 102 1.1 christos if (argc != 2) 103 1.1 christos usage(); 104 1.1 christos 105 1.1 christos strlcpy(canon_dev, argv[0], MAXPATHLEN); 106 1.5 mlelstv pathadj(argv[1], canon_dir); 107 1.1 christos } 108 1.1 christos 109 1.1 christos int 110 1.1 christos mount_autofs(int argc, char *argv[]) 111 1.1 christos { 112 1.1 christos char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN]; 113 1.1 christos char from[MAXPATHLEN], master_options[MAXPATHLEN]; 114 1.1 christos char master_prefix[MAXPATHLEN]; 115 1.1 christos int mntflags; 116 1.1 christos struct autofs_args am = { 117 1.1 christos .from = from, 118 1.1 christos .master_options = master_options, 119 1.1 christos .master_prefix = master_prefix, 120 1.1 christos }; 121 1.1 christos 122 1.1 christos mount_autofs_parseargs(argc, argv, &am, &mntflags, 123 1.1 christos canon_dev, canon_dir); 124 1.1 christos if (mount(MOUNT_KERNFS, canon_dir, mntflags, &am, sizeof(am)) == -1) 125 1.3 wiz err(EXIT_FAILURE, "autofs on %s", canon_dir); 126 1.1 christos if (mntflags & MNT_GETARGS) { 127 1.1 christos printf("from=%s, master_options=%s, master_prefix=%s\n", 128 1.1 christos am.from, am.master_options, am.master_prefix); 129 1.1 christos } 130 1.1 christos 131 1.1 christos return EXIT_SUCCESS; 132 1.1 christos } 133 1.1 christos 134 1.1 christos static void 135 1.1 christos usage(void) 136 1.1 christos { 137 1.1 christos (void)fprintf(stderr, 138 1.2 wiz "Usage: %s [-f from] [-O autofs_options] [-o options] [-p prefix] " 139 1.2 wiz "autofs mount_point\n", getprogname()); 140 1.1 christos exit(EXIT_FAILURE); 141 1.1 christos } 142