mount_tmpfs.c revision 1.18 1 1.18 pooka /* $NetBSD: mount_tmpfs.c,v 1.18 2007/07/16 17:12:03 pooka Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.13 jmmv * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.3 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.3 jmmv * 2005 program.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv * 3. All advertising materials mentioning features or use of this software
20 1.1 jmmv * must display the following acknowledgement:
21 1.1 jmmv * This product includes software developed by the NetBSD
22 1.1 jmmv * Foundation, Inc. and its contributors.
23 1.1 jmmv * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 jmmv * contributors may be used to endorse or promote products derived
25 1.1 jmmv * from this software without specific prior written permission.
26 1.1 jmmv *
27 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
38 1.1 jmmv */
39 1.1 jmmv
40 1.1 jmmv #include <sys/cdefs.h>
41 1.1 jmmv #ifndef lint
42 1.18 pooka __RCSID("$NetBSD: mount_tmpfs.c,v 1.18 2007/07/16 17:12:03 pooka Exp $");
43 1.1 jmmv #endif /* not lint */
44 1.1 jmmv
45 1.1 jmmv #include <sys/param.h>
46 1.1 jmmv #include <sys/mount.h>
47 1.4 jmmv #include <sys/stat.h>
48 1.1 jmmv
49 1.1 jmmv #include <fs/tmpfs/tmpfs.h>
50 1.1 jmmv
51 1.1 jmmv #include <ctype.h>
52 1.1 jmmv #include <err.h>
53 1.1 jmmv #include <errno.h>
54 1.1 jmmv #include <grp.h>
55 1.1 jmmv #include <mntopts.h>
56 1.1 jmmv #include <pwd.h>
57 1.1 jmmv #include <stdio.h>
58 1.1 jmmv #include <stdlib.h>
59 1.1 jmmv #include <string.h>
60 1.1 jmmv #include <unistd.h>
61 1.1 jmmv
62 1.1 jmmv /* --------------------------------------------------------------------- */
63 1.1 jmmv
64 1.1 jmmv static const struct mntopt mopts[] = {
65 1.1 jmmv MOPT_STDOPTS,
66 1.8 jmmv MOPT_GETARGS,
67 1.15 christos MOPT_NULL,
68 1.1 jmmv };
69 1.1 jmmv
70 1.1 jmmv /* --------------------------------------------------------------------- */
71 1.1 jmmv
72 1.1 jmmv static int mount_tmpfs(int argc, char **argv);
73 1.1 jmmv static void usage(void);
74 1.1 jmmv static int dehumanize_group(const char *str, gid_t *gid);
75 1.1 jmmv static int dehumanize_mode(const char *str, mode_t *mode);
76 1.1 jmmv static int dehumanize_off(const char *str, off_t *size);
77 1.1 jmmv static int dehumanize_user(const char *str, uid_t *uid);
78 1.1 jmmv
79 1.1 jmmv /* --------------------------------------------------------------------- */
80 1.1 jmmv
81 1.1 jmmv int
82 1.1 jmmv mount_tmpfs(int argc, char *argv[])
83 1.1 jmmv {
84 1.1 jmmv char canon_dir[MAXPATHLEN];
85 1.10 jmmv int gidset, modeset, uidset; /* Ought to be 'bool'. */
86 1.1 jmmv int ch, mntflags;
87 1.4 jmmv gid_t gid;
88 1.4 jmmv uid_t uid;
89 1.4 jmmv mode_t mode;
90 1.1 jmmv off_t offtmp;
91 1.12 christos mntoptparse_t mp;
92 1.1 jmmv struct tmpfs_args args;
93 1.4 jmmv struct stat sb;
94 1.1 jmmv
95 1.1 jmmv setprogname(argv[0]);
96 1.1 jmmv
97 1.1 jmmv /* Set default values for mount point arguments. */
98 1.1 jmmv args.ta_version = TMPFS_ARGS_VERSION;
99 1.1 jmmv args.ta_size_max = 0;
100 1.1 jmmv args.ta_nodes_max = 0;
101 1.1 jmmv mntflags = 0;
102 1.1 jmmv
103 1.10 jmmv gidset = 0; gid = 0;
104 1.10 jmmv uidset = 0; uid = 0;
105 1.10 jmmv modeset = 0; mode = 0;
106 1.4 jmmv
107 1.1 jmmv optind = optreset = 1;
108 1.1 jmmv while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
109 1.1 jmmv switch (ch) {
110 1.1 jmmv case 'g':
111 1.4 jmmv if (!dehumanize_group(optarg, &gid)) {
112 1.1 jmmv errx(EXIT_FAILURE, "failed to parse group "
113 1.1 jmmv "'%s'", optarg);
114 1.1 jmmv /* NOTREACHED */
115 1.1 jmmv }
116 1.10 jmmv gidset = 1;
117 1.1 jmmv break;
118 1.1 jmmv
119 1.1 jmmv case 'm':
120 1.4 jmmv if (!dehumanize_mode(optarg, &mode)) {
121 1.1 jmmv errx(EXIT_FAILURE, "failed to parse mode "
122 1.1 jmmv "'%s'", optarg);
123 1.1 jmmv /* NOTREACHED */
124 1.1 jmmv }
125 1.10 jmmv modeset = 1;
126 1.1 jmmv break;
127 1.1 jmmv
128 1.1 jmmv case 'n':
129 1.1 jmmv if (!dehumanize_off(optarg, &offtmp)) {
130 1.1 jmmv errx(EXIT_FAILURE, "failed to parse size "
131 1.1 jmmv "'%s'", optarg);
132 1.1 jmmv /* NOTREACHED */
133 1.1 jmmv }
134 1.1 jmmv args.ta_nodes_max = offtmp;
135 1.1 jmmv break;
136 1.1 jmmv
137 1.1 jmmv case 'o':
138 1.12 christos mp = getmntopts(optarg, mopts, &mntflags, 0);
139 1.12 christos if (mp == NULL)
140 1.12 christos err(1, "getmntopts");
141 1.12 christos freemntopts(mp);
142 1.1 jmmv break;
143 1.1 jmmv
144 1.1 jmmv case 's':
145 1.1 jmmv if (!dehumanize_off(optarg, &offtmp)) {
146 1.1 jmmv errx(EXIT_FAILURE, "failed to parse size "
147 1.1 jmmv "'%s'", optarg);
148 1.1 jmmv /* NOTREACHED */
149 1.1 jmmv }
150 1.1 jmmv args.ta_size_max = offtmp;
151 1.1 jmmv break;
152 1.1 jmmv
153 1.1 jmmv case 'u':
154 1.4 jmmv if (!dehumanize_user(optarg, &uid)) {
155 1.1 jmmv errx(EXIT_FAILURE, "failed to parse user "
156 1.1 jmmv "'%s'", optarg);
157 1.1 jmmv /* NOTREACHED */
158 1.1 jmmv }
159 1.10 jmmv uidset = 1;
160 1.1 jmmv break;
161 1.1 jmmv
162 1.1 jmmv case '?':
163 1.1 jmmv default:
164 1.1 jmmv usage();
165 1.1 jmmv /* NOTREACHED */
166 1.1 jmmv }
167 1.1 jmmv }
168 1.1 jmmv argc -= optind;
169 1.1 jmmv argv += optind;
170 1.1 jmmv
171 1.1 jmmv if (argc != 2) {
172 1.1 jmmv usage();
173 1.1 jmmv /* NOTREACHED */
174 1.1 jmmv }
175 1.1 jmmv
176 1.1 jmmv if (realpath(argv[1], canon_dir) == NULL) {
177 1.1 jmmv err(EXIT_FAILURE, "realpath %s", argv[0]);
178 1.1 jmmv /* NOTREACHED */
179 1.1 jmmv }
180 1.1 jmmv
181 1.1 jmmv if (strncmp(argv[1], canon_dir, MAXPATHLEN) != 0) {
182 1.1 jmmv warnx("\"%s\" is a relative path", argv[0]);
183 1.1 jmmv warnx("using \"%s\" instead", canon_dir);
184 1.1 jmmv }
185 1.1 jmmv
186 1.4 jmmv if (stat(canon_dir, &sb) == -1) {
187 1.4 jmmv err(EXIT_FAILURE, "cannot stat");
188 1.4 jmmv /* NOTREACHED */
189 1.4 jmmv }
190 1.4 jmmv args.ta_root_uid = uidset ? uid : sb.st_uid;
191 1.4 jmmv args.ta_root_gid = gidset ? gid : sb.st_gid;
192 1.4 jmmv args.ta_root_mode = modeset ? mode : sb.st_mode;
193 1.4 jmmv
194 1.17 pooka if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args, sizeof args) == -1) {
195 1.1 jmmv err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
196 1.1 jmmv /* NOTREACHED */
197 1.1 jmmv }
198 1.8 jmmv if (mntflags & MNT_GETARGS) {
199 1.8 jmmv struct passwd *pw;
200 1.8 jmmv struct group *gr;
201 1.8 jmmv
202 1.18 pooka (void)printf("version=%d, ", args.ta_version);
203 1.18 pooka (void)printf("size_max=%" PRIuMAX ", ",
204 1.8 jmmv (uintmax_t)args.ta_size_max);
205 1.18 pooka (void)printf("nodes_max=%" PRIuMAX ", ",
206 1.8 jmmv (uintmax_t)args.ta_nodes_max);
207 1.8 jmmv
208 1.8 jmmv pw = getpwuid(args.ta_root_uid);
209 1.8 jmmv if (pw == NULL)
210 1.18 pooka (void)printf("root_uid=%" PRIuMAX ", ",
211 1.8 jmmv (uintmax_t)args.ta_root_uid);
212 1.8 jmmv else
213 1.18 pooka (void)printf("root_uid=%s, ", pw->pw_name);
214 1.8 jmmv
215 1.8 jmmv gr = getgrgid(args.ta_root_gid);
216 1.8 jmmv if (gr == NULL)
217 1.18 pooka (void)printf("root_gid=%" PRIuMAX ", ",
218 1.8 jmmv (uintmax_t)args.ta_root_gid);
219 1.8 jmmv else
220 1.18 pooka (void)printf("root_gid=%s, ", gr->gr_name);
221 1.8 jmmv
222 1.8 jmmv (void)printf("root_mode=%o\n", args.ta_root_mode);
223 1.8 jmmv }
224 1.1 jmmv
225 1.1 jmmv return EXIT_SUCCESS;
226 1.1 jmmv }
227 1.1 jmmv
228 1.1 jmmv /* --------------------------------------------------------------------- */
229 1.1 jmmv
230 1.1 jmmv static void
231 1.1 jmmv usage(void)
232 1.1 jmmv {
233 1.1 jmmv (void)fprintf(stderr,
234 1.1 jmmv "Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
235 1.1 jmmv " [-u user] tmpfs mountpoint\n", getprogname());
236 1.1 jmmv exit(1);
237 1.1 jmmv }
238 1.1 jmmv
239 1.1 jmmv /* --------------------------------------------------------------------- */
240 1.1 jmmv
241 1.1 jmmv /*
242 1.1 jmmv * Obtains a GID number based on the contents of 'str'. If it is a string
243 1.1 jmmv * and is found in group(5), its corresponding ID is used. Otherwise, an
244 1.1 jmmv * attempt is made to convert the string to a number and use that as a GID.
245 1.1 jmmv * In case of success, true is returned and *gid holds the GID value.
246 1.1 jmmv * Otherwise, false is returned and *gid is untouched.
247 1.1 jmmv */
248 1.1 jmmv static int
249 1.1 jmmv dehumanize_group(const char *str, gid_t *gid)
250 1.1 jmmv {
251 1.1 jmmv int error;
252 1.1 jmmv struct group *gr;
253 1.1 jmmv
254 1.1 jmmv gr = getgrnam(str);
255 1.1 jmmv if (gr != NULL) {
256 1.1 jmmv *gid = gr->gr_gid;
257 1.1 jmmv error = 1;
258 1.1 jmmv } else {
259 1.1 jmmv char *ep;
260 1.7 jmmv unsigned long tmp;
261 1.1 jmmv
262 1.1 jmmv errno = 0;
263 1.7 jmmv tmp = strtoul(str, &ep, 0);
264 1.1 jmmv if (str[0] == '\0' || *ep != '\0')
265 1.1 jmmv error = 0; /* Not a number. */
266 1.13 jmmv else if (errno == ERANGE)
267 1.1 jmmv error = 0; /* Out of range. */
268 1.1 jmmv else {
269 1.1 jmmv *gid = (gid_t)tmp;
270 1.1 jmmv error = 1;
271 1.1 jmmv }
272 1.1 jmmv }
273 1.1 jmmv
274 1.1 jmmv return error;
275 1.1 jmmv }
276 1.1 jmmv
277 1.1 jmmv /* --------------------------------------------------------------------- */
278 1.1 jmmv
279 1.1 jmmv /*
280 1.1 jmmv * Obtains a mode number based on the contents of 'str'.
281 1.1 jmmv * In case of success, true is returned and *mode holds the mode value.
282 1.1 jmmv * Otherwise, false is returned and *mode is untouched.
283 1.1 jmmv */
284 1.1 jmmv static int
285 1.1 jmmv dehumanize_mode(const char *str, mode_t *mode)
286 1.1 jmmv {
287 1.1 jmmv char *ep;
288 1.1 jmmv int error;
289 1.1 jmmv long tmp;
290 1.1 jmmv
291 1.1 jmmv errno = 0;
292 1.1 jmmv tmp = strtol(str, &ep, 8);
293 1.1 jmmv if (str[0] == '\0' || *ep != '\0')
294 1.1 jmmv error = 0; /* Not a number. */
295 1.13 jmmv else if (errno == ERANGE)
296 1.1 jmmv error = 0; /* Out of range. */
297 1.1 jmmv else {
298 1.1 jmmv *mode = (mode_t)tmp;
299 1.1 jmmv error = 1;
300 1.1 jmmv }
301 1.1 jmmv
302 1.1 jmmv return error;
303 1.1 jmmv }
304 1.1 jmmv
305 1.1 jmmv /* --------------------------------------------------------------------- */
306 1.1 jmmv
307 1.1 jmmv /*
308 1.1 jmmv * Converts the number given in 'str', which may be given in a humanized
309 1.1 jmmv * form (as described in humanize_number(3), but with some limitations),
310 1.1 jmmv * to a file size (off_t) without units.
311 1.1 jmmv * In case of success, true is returned and *size holds the value.
312 1.1 jmmv * Otherwise, false is returned and *size is untouched.
313 1.1 jmmv */
314 1.1 jmmv static int
315 1.1 jmmv dehumanize_off(const char *str, off_t *size)
316 1.1 jmmv {
317 1.1 jmmv char *ep, unit;
318 1.1 jmmv const char *delimit;
319 1.9 jmmv long multiplier;
320 1.9 jmmv long long tmp, tmp2;
321 1.6 jmmv size_t len;
322 1.1 jmmv
323 1.1 jmmv len = strlen(str);
324 1.1 jmmv if (len < 1)
325 1.1 jmmv return 0;
326 1.1 jmmv
327 1.1 jmmv multiplier = 1;
328 1.1 jmmv
329 1.1 jmmv unit = str[len - 1];
330 1.1 jmmv if (isalpha((int)unit)) {
331 1.5 jmmv switch (tolower((int)unit)) {
332 1.1 jmmv case 'b':
333 1.1 jmmv multiplier = 1;
334 1.1 jmmv break;
335 1.1 jmmv
336 1.1 jmmv case 'k':
337 1.1 jmmv multiplier = 1024;
338 1.1 jmmv break;
339 1.1 jmmv
340 1.5 jmmv case 'm':
341 1.1 jmmv multiplier = 1024 * 1024;
342 1.1 jmmv break;
343 1.1 jmmv
344 1.5 jmmv case 'g':
345 1.1 jmmv multiplier = 1024 * 1024 * 1024;
346 1.1 jmmv break;
347 1.1 jmmv
348 1.1 jmmv default:
349 1.1 jmmv return 0; /* Invalid suffix. */
350 1.1 jmmv }
351 1.1 jmmv
352 1.1 jmmv delimit = &str[len - 1];
353 1.1 jmmv } else
354 1.1 jmmv delimit = NULL;
355 1.1 jmmv
356 1.1 jmmv errno = 0;
357 1.9 jmmv tmp = strtoll(str, &ep, 10);
358 1.1 jmmv if (str[0] == '\0' || (ep != delimit && *ep != '\0'))
359 1.1 jmmv return 0; /* Not a number. */
360 1.13 jmmv else if (errno == ERANGE)
361 1.1 jmmv return 0; /* Out of range. */
362 1.1 jmmv
363 1.9 jmmv tmp2 = tmp * multiplier;
364 1.9 jmmv tmp2 = tmp2 / multiplier;
365 1.9 jmmv if (tmp != tmp2)
366 1.9 jmmv return 0; /* Out of range. */
367 1.1 jmmv *size = tmp * multiplier;
368 1.1 jmmv
369 1.1 jmmv return 1;
370 1.1 jmmv }
371 1.1 jmmv
372 1.1 jmmv /* --------------------------------------------------------------------- */
373 1.1 jmmv
374 1.1 jmmv /*
375 1.1 jmmv * Obtains a UID number based on the contents of 'str'. If it is a string
376 1.1 jmmv * and is found in passwd(5), its corresponding ID is used. Otherwise, an
377 1.1 jmmv * attempt is made to convert the string to a number and use that as a UID.
378 1.1 jmmv * In case of success, true is returned and *uid holds the UID value.
379 1.1 jmmv * Otherwise, false is returned and *uid is untouched.
380 1.1 jmmv */
381 1.1 jmmv static int
382 1.1 jmmv dehumanize_user(const char *str, uid_t *uid)
383 1.1 jmmv {
384 1.1 jmmv int error;
385 1.1 jmmv struct passwd *pw;
386 1.1 jmmv
387 1.1 jmmv pw = getpwnam(str);
388 1.1 jmmv if (pw != NULL) {
389 1.1 jmmv *uid = pw->pw_uid;
390 1.1 jmmv error = 1;
391 1.1 jmmv } else {
392 1.1 jmmv char *ep;
393 1.7 jmmv unsigned long tmp;
394 1.1 jmmv
395 1.1 jmmv errno = 0;
396 1.7 jmmv tmp = strtoul(str, &ep, 0);
397 1.1 jmmv if (str[0] == '\0' || *ep != '\0')
398 1.1 jmmv error = 0; /* Not a number. */
399 1.13 jmmv else if (errno == ERANGE)
400 1.1 jmmv error = 0; /* Out of range. */
401 1.1 jmmv else {
402 1.1 jmmv *uid = (uid_t)tmp;
403 1.1 jmmv error = 1;
404 1.1 jmmv }
405 1.1 jmmv }
406 1.1 jmmv
407 1.1 jmmv return error;
408 1.1 jmmv }
409 1.1 jmmv
410 1.1 jmmv /* --------------------------------------------------------------------- */
411 1.1 jmmv
412 1.1 jmmv #ifndef MOUNT_NOMAIN
413 1.1 jmmv int
414 1.1 jmmv main(int argc, char *argv[])
415 1.1 jmmv {
416 1.1 jmmv
417 1.1 jmmv return mount_tmpfs(argc, argv);
418 1.1 jmmv }
419 1.1 jmmv #endif
420