Home | History | Annotate | Line # | Download | only in mount_tmpfs
mount_tmpfs.c revision 1.21.4.1
      1  1.21.4.1       mjf /*	$NetBSD: mount_tmpfs.c,v 1.21.4.1 2008/06/02 13:21:23 mjf 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  *
     20       1.1      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1      jmmv  */
     32       1.1      jmmv 
     33       1.1      jmmv #include <sys/cdefs.h>
     34       1.1      jmmv #ifndef lint
     35  1.21.4.1       mjf __RCSID("$NetBSD: mount_tmpfs.c,v 1.21.4.1 2008/06/02 13:21:23 mjf Exp $");
     36       1.1      jmmv #endif /* not lint */
     37       1.1      jmmv 
     38       1.1      jmmv #include <sys/param.h>
     39       1.1      jmmv #include <sys/mount.h>
     40       1.4      jmmv #include <sys/stat.h>
     41       1.1      jmmv 
     42       1.1      jmmv #include <fs/tmpfs/tmpfs.h>
     43       1.1      jmmv 
     44       1.1      jmmv #include <ctype.h>
     45       1.1      jmmv #include <err.h>
     46       1.1      jmmv #include <errno.h>
     47       1.1      jmmv #include <grp.h>
     48       1.1      jmmv #include <mntopts.h>
     49       1.1      jmmv #include <pwd.h>
     50       1.1      jmmv #include <stdio.h>
     51       1.1      jmmv #include <stdlib.h>
     52       1.1      jmmv #include <string.h>
     53       1.1      jmmv #include <unistd.h>
     54      1.19  christos #include "fattr.h"
     55       1.1      jmmv 
     56       1.1      jmmv /* --------------------------------------------------------------------- */
     57       1.1      jmmv 
     58       1.1      jmmv static const struct mntopt mopts[] = {
     59       1.1      jmmv 	MOPT_STDOPTS,
     60       1.8      jmmv 	MOPT_GETARGS,
     61      1.15  christos 	MOPT_NULL,
     62       1.1      jmmv };
     63       1.1      jmmv 
     64       1.1      jmmv /* --------------------------------------------------------------------- */
     65       1.1      jmmv 
     66       1.1      jmmv static int	mount_tmpfs(int argc, char **argv);
     67      1.20     perry static void	usage(void) __dead;
     68       1.1      jmmv 
     69       1.1      jmmv /* --------------------------------------------------------------------- */
     70       1.1      jmmv 
     71       1.1      jmmv int
     72       1.1      jmmv mount_tmpfs(int argc, char *argv[])
     73       1.1      jmmv {
     74       1.1      jmmv 	char canon_dir[MAXPATHLEN];
     75      1.10      jmmv 	int gidset, modeset, uidset; /* Ought to be 'bool'. */
     76       1.1      jmmv 	int ch, mntflags;
     77       1.4      jmmv 	gid_t gid;
     78       1.4      jmmv 	uid_t uid;
     79       1.4      jmmv 	mode_t mode;
     80      1.19  christos 	int64_t tmpnumber;
     81      1.12  christos 	mntoptparse_t mp;
     82       1.1      jmmv 	struct tmpfs_args args;
     83       1.4      jmmv 	struct stat sb;
     84       1.1      jmmv 
     85       1.1      jmmv 	setprogname(argv[0]);
     86       1.1      jmmv 
     87       1.1      jmmv 	/* Set default values for mount point arguments. */
     88       1.1      jmmv 	args.ta_version = TMPFS_ARGS_VERSION;
     89       1.1      jmmv 	args.ta_size_max = 0;
     90       1.1      jmmv 	args.ta_nodes_max = 0;
     91       1.1      jmmv 	mntflags = 0;
     92       1.1      jmmv 
     93      1.10      jmmv 	gidset = 0; gid = 0;
     94      1.10      jmmv 	uidset = 0; uid = 0;
     95      1.10      jmmv 	modeset = 0; mode = 0;
     96       1.4      jmmv 
     97       1.1      jmmv 	optind = optreset = 1;
     98       1.1      jmmv 	while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
     99       1.1      jmmv 		switch (ch) {
    100       1.1      jmmv 		case 'g':
    101      1.19  christos 			gid = a_gid(optarg);
    102      1.10      jmmv 			gidset = 1;
    103       1.1      jmmv 			break;
    104       1.1      jmmv 
    105       1.1      jmmv 		case 'm':
    106      1.19  christos 			mode = a_mask(optarg);
    107      1.10      jmmv 			modeset = 1;
    108       1.1      jmmv 			break;
    109       1.1      jmmv 
    110       1.1      jmmv 		case 'n':
    111      1.19  christos 			if (dehumanize_number(optarg, &tmpnumber) == -1)
    112      1.19  christos 				err(EXIT_FAILURE, "failed to parse nodes `%s'",
    113      1.19  christos 				    optarg);
    114      1.19  christos 			args.ta_nodes_max = tmpnumber;
    115       1.1      jmmv 			break;
    116       1.1      jmmv 
    117       1.1      jmmv 		case 'o':
    118      1.12  christos 			mp = getmntopts(optarg, mopts, &mntflags, 0);
    119      1.12  christos 			if (mp == NULL)
    120      1.19  christos 				err(EXIT_FAILURE, "getmntopts");
    121      1.12  christos 			freemntopts(mp);
    122       1.1      jmmv 			break;
    123       1.1      jmmv 
    124       1.1      jmmv 		case 's':
    125      1.19  christos 			if (dehumanize_number(optarg, &tmpnumber) == -1)
    126      1.19  christos 				err(EXIT_FAILURE, "failed to parse size `%s'",
    127      1.19  christos 				    optarg);
    128      1.19  christos 			args.ta_size_max = tmpnumber;
    129       1.1      jmmv 			break;
    130       1.1      jmmv 
    131       1.1      jmmv 		case 'u':
    132      1.19  christos 			uid = a_uid(optarg);
    133      1.10      jmmv 			uidset = 1;
    134       1.1      jmmv 			break;
    135       1.1      jmmv 
    136       1.1      jmmv 		case '?':
    137       1.1      jmmv 		default:
    138       1.1      jmmv 			usage();
    139       1.1      jmmv 		}
    140       1.1      jmmv 	}
    141       1.1      jmmv 	argc -= optind;
    142       1.1      jmmv 	argv += optind;
    143       1.1      jmmv 
    144      1.19  christos 	if (argc != 2)
    145       1.1      jmmv 		usage();
    146       1.1      jmmv 
    147      1.19  christos 	if (realpath(argv[1], canon_dir) == NULL)
    148      1.21    rillig 		err(EXIT_FAILURE, "realpath %s", argv[1]);
    149       1.1      jmmv 
    150       1.1      jmmv 	if (strncmp(argv[1], canon_dir, MAXPATHLEN) != 0) {
    151      1.21    rillig 		warnx("\"%s\" is a relative path", argv[1]);
    152       1.1      jmmv 		warnx("using \"%s\" instead", canon_dir);
    153       1.1      jmmv 	}
    154       1.1      jmmv 
    155      1.19  christos 	if (stat(canon_dir, &sb) == -1)
    156      1.19  christos 		err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
    157      1.19  christos 
    158       1.4      jmmv 	args.ta_root_uid = uidset ? uid : sb.st_uid;
    159       1.4      jmmv 	args.ta_root_gid = gidset ? gid : sb.st_gid;
    160       1.4      jmmv 	args.ta_root_mode = modeset ? mode : sb.st_mode;
    161       1.4      jmmv 
    162      1.19  christos 	if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args, sizeof args) == -1)
    163       1.1      jmmv 		err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
    164      1.19  christos 
    165       1.8      jmmv 	if (mntflags & MNT_GETARGS) {
    166       1.8      jmmv 		struct passwd *pw;
    167       1.8      jmmv 		struct group *gr;
    168       1.8      jmmv 
    169      1.18     pooka 		(void)printf("version=%d, ", args.ta_version);
    170      1.18     pooka 		(void)printf("size_max=%" PRIuMAX ", ",
    171       1.8      jmmv 		    (uintmax_t)args.ta_size_max);
    172      1.18     pooka 		(void)printf("nodes_max=%" PRIuMAX ", ",
    173       1.8      jmmv 		    (uintmax_t)args.ta_nodes_max);
    174       1.8      jmmv 
    175       1.8      jmmv 		pw = getpwuid(args.ta_root_uid);
    176       1.8      jmmv 		if (pw == NULL)
    177      1.18     pooka 			(void)printf("root_uid=%" PRIuMAX ", ",
    178       1.8      jmmv 			    (uintmax_t)args.ta_root_uid);
    179       1.8      jmmv 		else
    180      1.18     pooka 			(void)printf("root_uid=%s, ", pw->pw_name);
    181       1.8      jmmv 
    182       1.8      jmmv 		gr = getgrgid(args.ta_root_gid);
    183       1.8      jmmv 		if (gr == NULL)
    184      1.18     pooka 			(void)printf("root_gid=%" PRIuMAX ", ",
    185       1.8      jmmv 			    (uintmax_t)args.ta_root_gid);
    186       1.8      jmmv 		else
    187      1.18     pooka 			(void)printf("root_gid=%s, ", gr->gr_name);
    188       1.8      jmmv 
    189       1.8      jmmv 		(void)printf("root_mode=%o\n", args.ta_root_mode);
    190       1.8      jmmv 	}
    191       1.1      jmmv 
    192       1.1      jmmv 	return EXIT_SUCCESS;
    193       1.1      jmmv }
    194       1.1      jmmv 
    195       1.1      jmmv /* --------------------------------------------------------------------- */
    196       1.1      jmmv 
    197       1.1      jmmv static void
    198       1.1      jmmv usage(void)
    199       1.1      jmmv {
    200       1.1      jmmv 	(void)fprintf(stderr,
    201       1.1      jmmv 	    "Usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
    202       1.1      jmmv 	    "           [-u user] tmpfs mountpoint\n", getprogname());
    203       1.1      jmmv 	exit(1);
    204       1.1      jmmv }
    205       1.1      jmmv 
    206       1.1      jmmv /* --------------------------------------------------------------------- */
    207       1.1      jmmv 
    208       1.1      jmmv #ifndef MOUNT_NOMAIN
    209       1.1      jmmv int
    210       1.1      jmmv main(int argc, char *argv[])
    211       1.1      jmmv {
    212       1.1      jmmv 
    213       1.1      jmmv 	return mount_tmpfs(argc, argv);
    214       1.1      jmmv }
    215       1.1      jmmv #endif
    216