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