1 /* $NetBSD: mount_tmpfs.c,v 1.24 2008/08/05 20:57:45 pooka Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/mount.h>
41 #include <vfs/tmpfs/tmpfs_args.h>
57 #include "mount_tmpfs.h"
59 /* --------------------------------------------------------------------- */
61 static const struct mntopt mopts[] = {
66 /* --------------------------------------------------------------------- */
68 static gid_t a_gid(char *);
69 static uid_t a_uid(char *);
70 static mode_t a_mask(char *);
71 static void usage(void) __dead2;
73 /* --------------------------------------------------------------------- */
76 mount_tmpfs_parseargs(int argc, char *argv[],
77 struct tmpfs_args *args, int *mntflags,
78 char *canon_dev, char *canon_dir)
80 int gidset, modeset, uidset; /* Ought to be 'bool'. */
88 /* Set default values for mount point arguments. */
89 memset(args, 0, sizeof(*args));
90 args->ta_version = TMPFS_ARGS_VERSION;
91 args->ta_size_max = 0;
92 args->ta_nodes_max = 0;
97 modeset = 0; mode = 0;
99 optind = optreset = 1;
100 while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
108 mode = a_mask(optarg);
113 if (dehumanize_number(optarg, &tmpnumber) < 0) {
114 fprintf(stderr, "bad number for -n\n");
117 args->ta_nodes_max = tmpnumber;
121 getmntopts(optarg, mopts, mntflags, 0);
125 if (dehumanize_number(optarg, &tmpnumber) < 0) {
126 fprintf(stderr, "bad number for -s\n");
129 args->ta_size_max = tmpnumber;
148 strlcpy(canon_dev, argv[0], MAXPATHLEN);
149 strlcpy(canon_dir, argv[1], MAXPATHLEN);
151 if (stat(canon_dir, &sb) == -1)
152 err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
154 args->ta_root_uid = uidset ? uid : sb.st_uid;
155 args->ta_root_gid = gidset ? gid : sb.st_gid;
156 args->ta_root_mode = modeset ? mode : sb.st_mode;
159 /* --------------------------------------------------------------------- */
168 if ((gr = getgrnam(s)) != NULL)
171 for (gname = s; *s && isdigit(*s); ++s);
175 errx(EX_NOUSER, "unknown group id: %s", gname);
187 if ((pw = getpwnam(s)) != NULL)
190 for (uname = s; *s && isdigit(*s); ++s);
194 errx(EX_NOUSER, "unknown user id: %s", uname);
206 if (*s >= '0' && *s <= '7') {
208 rv = strtol(optarg, &ep, 8);
210 if (!done || rv < 0 || *ep)
211 errx(EX_USAGE, "invalid file mode: %s", s);
218 (void)fprintf(stderr,
219 "Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
220 " [-u user] tmpfs mountpoint\n", getprogname());
224 /* --------------------------------------------------------------------- */
227 mount_tmpfs(int argc, char *argv[])
229 struct tmpfs_args args;
230 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
235 mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
236 canon_dev, canon_dir);
239 error = getvfsbyname("tmpfs", &vfc);
240 if (error && vfsisloadable("tmpfs")) {
242 err(EX_OSERR, "vfsload(%s)", "tmpfs");
244 error = getvfsbyname("tmpfs", &vfc);
247 errx(EX_OSERR, "%s filesystem not available", "tmpfs");
249 if (mount(vfc.vfc_name, canon_dir, mntflags, &args) == -1)
250 err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
257 main(int argc, char *argv[])
260 setprogname(argv[0]);
261 return mount_tmpfs(argc, argv);