1 /* $OpenBSD: mount_tmpfs.c,v 1.4 2014/01/21 21:58:27 jsg Exp $ */ 2 /* $NetBSD: mount_tmpfs.c,v 1.24 2008/08/05 20:57:45 pooka Exp $ */ 3 4 /* 5 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 10 * 2005 program. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #if 0 36 __RCSID("$NetBSD: mount_tmpfs.c,v 1.24 2008/08/05 20:57:45 pooka Exp $"); 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/mount.h> 41 #include <sys/stat.h> 42 43 #include <ctype.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <grp.h> 47 #include <mntopts.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <util.h> 54 55 #include "mount_tmpfs.h" 56 57 /* --------------------------------------------------------------------- */ 58 59 static const struct mntopt mopts[] = { 60 MOPT_STDOPTS, 61 MOPT_UPDATE, 62 { NULL }, 63 }; 64 65 /* --------------------------------------------------------------------- */ 66 67 static void usage(void) __dead; 68 static uid_t a_uid(const char *); 69 static gid_t a_gid(const char *); 70 static uid_t a_gid(const char *); 71 static int a_num(const char *, const char *); 72 static mode_t a_mask(const char *); 73 static void pathadj(const char *, char *); 74 75 /* --------------------------------------------------------------------- */ 76 77 void 78 mount_tmpfs_parseargs(int argc, char *argv[], 79 struct tmpfs_args *args, int *mntflags, 80 char *canon_dev, char *canon_dir) 81 { 82 int gidset, modeset, uidset; /* Ought to be 'bool'. */ 83 int ch; 84 gid_t gid; 85 uid_t uid; 86 mode_t mode; 87 int64_t tmpnumber; 88 struct stat sb; 89 90 /* Set default values for mount point arguments. */ 91 memset(args, 0, sizeof(*args)); 92 args->ta_version = TMPFS_ARGS_VERSION; 93 args->ta_size_max = 0; 94 args->ta_nodes_max = 0; 95 *mntflags = 0; 96 97 gidset = 0; gid = 0; 98 uidset = 0; uid = 0; 99 modeset = 0; mode = 0; 100 101 optind = optreset = 1; 102 while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) { 103 switch (ch) { 104 case 'g': 105 gid = a_gid(optarg); 106 gidset = 1; 107 break; 108 109 case 'm': 110 mode = a_mask(optarg); 111 modeset = 1; 112 break; 113 114 case 'n': 115 116 if (scan_scaled(optarg, &tmpnumber) == -1) 117 err(EXIT_FAILURE, "failed to parse nodes `%s'", 118 optarg); 119 args->ta_nodes_max = tmpnumber; 120 break; 121 122 case 'o': 123 getmntopts(optarg, mopts, mntflags); 124 break; 125 126 case 's': 127 if (scan_scaled(optarg, &tmpnumber) == -1) 128 err(EXIT_FAILURE, "failed to parse size `%s'", 129 optarg); 130 args->ta_size_max = tmpnumber; 131 break; 132 133 case 'u': 134 uid = a_uid(optarg); 135 uidset = 1; 136 break; 137 138 case '?': 139 default: 140 usage(); 141 } 142 } 143 argc -= optind; 144 argv += optind; 145 146 if (argc != 2) 147 usage(); 148 149 strlcpy(canon_dev, argv[0], MAXPATHLEN); 150 pathadj(argv[1], canon_dir); 151 152 if (stat(canon_dir, &sb) == -1) 153 err(EXIT_FAILURE, "cannot stat `%s'", canon_dir); 154 155 args->ta_root_uid = uidset ? uid : sb.st_uid; 156 args->ta_root_gid = gidset ? gid : sb.st_gid; 157 args->ta_root_mode = modeset ? mode : sb.st_mode; 158 } 159 160 /* --------------------------------------------------------------------- */ 161 162 static void 163 usage(void) 164 { 165 extern char *__progname; 166 (void)fprintf(stderr, 167 "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n" 168 " [-u user] tmpfs mount_point\n", __progname); 169 exit(1); 170 } 171 172 /* --------------------------------------------------------------------- */ 173 174 int 175 mount_tmpfs(int argc, char *argv[]) 176 { 177 struct tmpfs_args args; 178 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN]; 179 int mntflags; 180 181 mount_tmpfs_parseargs(argc, argv, &args, &mntflags, 182 canon_dev, canon_dir); 183 184 if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args) == -1) 185 err(EXIT_FAILURE, "tmpfs on %s", canon_dir); 186 187 return EXIT_SUCCESS; 188 } 189 190 int 191 main(int argc, char *argv[]) 192 { 193 194 /* setprogname(argv[0]); */ 195 return mount_tmpfs(argc, argv); 196 } 197 198 static uid_t 199 a_uid(const char *s) 200 { 201 struct passwd *pw; 202 203 if ((pw = getpwnam(s)) != NULL) 204 return pw->pw_uid; 205 return a_num(s, "user"); 206 } 207 208 static gid_t 209 a_gid(const char *s) 210 { 211 struct group *gr; 212 213 if ((gr = getgrnam(s)) != NULL) 214 return gr->gr_gid; 215 return a_num(s, "group"); 216 } 217 218 static int 219 a_num(const char *s, const char *id_type) 220 { 221 int id; 222 char *ep; 223 224 id = strtol(s, &ep, 0); 225 if (*ep || s == ep || id < 0) 226 errx(1, "unknown %s id: %s", id_type, s); 227 return id; 228 } 229 230 static mode_t 231 a_mask(const char *s) 232 { 233 int rv; 234 char *ep; 235 236 rv = strtol(s, &ep, 8); 237 if (s == ep || *ep || rv < 0) 238 errx(1, "invalid file mode: %s", s); 239 return rv; 240 } 241 242 static void 243 pathadj(const char *input, char *adjusted) 244 { 245 246 if (realpath(input, adjusted) == NULL) 247 warn("Warning: realpath %s", input); 248 if (strncmp(input, adjusted, MAXPATHLEN)) { 249 warnx("\"%s\" is a non-resolved or relative path.", input); 250 warnx("using \"%s\" instead.", adjusted); 251 } 252 } 253