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 #define MOPT_TMPFSOPTS \
62 { "gid=", 0, MNT_GID, 1}, \
63 { "uid=", 0, MNT_UID, 1}, \
64 { "mode=", 0, MNT_MODE, 1}, \
65 { "inodes=", 0, MNT_INODES, 1}, \
66 { "size=", 0, MNT_SIZE, 1}, \
67 { "maxfilesize=", 0, MNT_MAXFSIZE, 1}
70 static const struct mntopt mopts[] = {
76 /* --------------------------------------------------------------------- */
78 static gid_t a_gid(char *);
79 static uid_t a_uid(char *);
80 static mode_t a_mask(char *);
81 static int64_t a_number(char *s);
82 static void usage(void) __dead2;
84 /* --------------------------------------------------------------------- */
87 mount_tmpfs_parseargs(int argc, char *argv[],
88 struct tmpfs_args *args, int *mntflags,
89 char *canon_dev, char *canon_dir)
91 int gidset, modeset, uidset; /* Ought to be 'bool'. */
100 /* Set default values for mount point arguments. */
101 memset(args, 0, sizeof(*args));
102 args->ta_version = TMPFS_ARGS_VERSION;
103 args->ta_size_max = 0;
104 args->ta_nodes_max = 0;
105 args->ta_maxfsize_max = 0;
110 modeset = 0; mode = 0;
112 optind = optreset = 1;
113 while ((ch = getopt(argc, argv, "f:g:m:n:o:s:u:")) != -1 ) {
116 args->ta_maxfsize_max = a_number(optarg);
125 mode = a_mask(optarg);
130 args->ta_nodes_max = a_number(optarg);
134 getmntopts(optarg, mopts, mntflags, &extend_flags);
135 if (extend_flags & MNT_GID) {
136 ptr = strstr(optarg, "gid=");
138 delim = strstr(ptr, ",");
141 gid = a_gid(ptr + 4);
144 gid = a_gid(ptr + 4);
147 extend_flags ^= MNT_GID;
149 if (extend_flags & MNT_UID) {
150 ptr = strstr(optarg, "uid=");
152 delim = strstr(ptr, ",");
155 uid = a_uid(ptr + 4);
158 uid = a_uid(ptr + 4);
161 extend_flags ^= MNT_UID;
163 if (extend_flags & MNT_MODE) {
164 ptr = strstr(optarg, "mode=");
166 delim = strstr(ptr, ",");
169 mode = a_mask(ptr + 5);
172 mode = a_mask(ptr + 5);
175 extend_flags ^= MNT_MODE;
177 if (extend_flags & MNT_INODES) {
178 ptr = strstr(optarg, "inodes=");
180 delim = strstr(ptr, ",");
183 args->ta_nodes_max = a_number(ptr + 7);
186 args->ta_nodes_max = a_number(ptr + 7);
188 extend_flags ^= MNT_INODES;
190 if (extend_flags & MNT_SIZE) {
191 ptr = strstr(optarg, "size=");
193 delim = strstr(ptr, ",");
196 args->ta_size_max = a_number(ptr + 5);
199 args->ta_size_max = a_number(ptr + 5);
201 extend_flags ^= MNT_SIZE;
203 if (extend_flags & MNT_MAXFSIZE) {
204 ptr = strstr(optarg, "maxfilesize=");
206 delim = strstr(ptr, ",");
209 args->ta_maxfsize_max = a_number(ptr + 12);
212 args->ta_maxfsize_max = a_number(ptr + 12);
214 extend_flags ^= MNT_MAXFSIZE;
219 args->ta_size_max = a_number(optarg);
238 strlcpy(canon_dev, argv[0], MAXPATHLEN);
239 strlcpy(canon_dir, argv[1], MAXPATHLEN);
241 if (stat(canon_dir, &sb) == -1)
242 err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
244 args->ta_root_uid = uidset ? uid : sb.st_uid;
245 args->ta_root_gid = gidset ? gid : sb.st_gid;
246 args->ta_root_mode = modeset ? mode : sb.st_mode;
249 /* --------------------------------------------------------------------- */
258 if ((gr = getgrnam(s)) != NULL)
261 for (gname = s; *s && isdigit(*s); ++s);
265 errx(EX_NOUSER, "unknown group id: %s", gname);
277 if ((pw = getpwnam(s)) != NULL)
280 for (uname = s; *s && isdigit(*s); ++s);
284 errx(EX_NOUSER, "unknown user id: %s", uname);
296 if (*s >= '0' && *s <= '7') {
298 rv = strtol(s, &ep, 8);
300 if (!done || rv < 0 || *ep)
301 errx(EX_USAGE, "invalid file mode: %s", s);
310 if (dehumanize_number(s, &rv) < 0 || rv < 0)
311 errx(EX_USAGE, "bad number for option: %s", s);
312 fprintf(stderr, "%ld", rv);
319 (void)fprintf(stderr,
320 "Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
321 " [-u user] [-f maxfilesize] tmpfs mountpoint\n", getprogname());
325 /* --------------------------------------------------------------------- */
328 mount_tmpfs(int argc, char *argv[])
330 struct tmpfs_args args;
331 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
336 mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
337 canon_dev, canon_dir);
340 error = getvfsbyname("tmpfs", &vfc);
341 if (error && vfsisloadable("tmpfs")) {
343 err(EX_OSERR, "vfsload(%s)", "tmpfs");
345 error = getvfsbyname("tmpfs", &vfc);
348 errx(EX_OSERR, "%s filesystem not available", "tmpfs");
350 if (mount(vfc.vfc_name, canon_dir, mntflags, &args) == -1)
351 err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
358 main(int argc, char *argv[])
361 setprogname(argv[0]);
362 return mount_tmpfs(argc, argv);