Home | History | Annotate | Line # | Download | only in mount_union
mount_union.c revision 1.2
      1 /*	$NetBSD: mount_union.c,v 1.2 1995/03/18 14:58:24 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 char copyright[] =
     41 "@(#) Copyright (c) 1992, 1993, 1994\n\
     42 	The Regents of the University of California.  All rights reserved.\n";
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)mount_union.c	8.5 (Berkeley) 3/27/94";
     48 #else
     49 static char rcsid[] = "$NetBSD: mount_union.c,v 1.2 1995/03/18 14:58:24 cgd Exp $";
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include <sys/param.h>
     54 #include <sys/mount.h>
     55 
     56 #include <miscfs/union/union.h>
     57 
     58 #include <err.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 
     64 #include "mntopts.h"
     65 
     66 struct mntopt mopts[] = {
     67 	MOPT_STDOPTS,
     68 	{ NULL }
     69 };
     70 
     71 int	subdir __P((const char *, const char *));
     72 void	usage __P((void));
     73 
     74 int
     75 main(argc, argv)
     76 	int argc;
     77 	char *argv[];
     78 {
     79 	struct union_args args;
     80 	int ch, mntflags;
     81 	char target[MAXPATHLEN];
     82 
     83 	mntflags = 0;
     84 	args.mntflags = UNMNT_ABOVE;
     85 	while ((ch = getopt(argc, argv, "bo:r")) != EOF)
     86 		switch (ch) {
     87 		case 'b':
     88 			args.mntflags &= ~UNMNT_OPMASK;
     89 			args.mntflags |= UNMNT_BELOW;
     90 			break;
     91 		case 'o':
     92 			getmntopts(optarg, mopts, &mntflags);
     93 			break;
     94 		case 'r':
     95 			args.mntflags &= ~UNMNT_OPMASK;
     96 			args.mntflags |= UNMNT_REPLACE;
     97 			break;
     98 		case '?':
     99 		default:
    100 			usage();
    101 			/* NOTREACHED */
    102 		}
    103 	argc -= optind;
    104 	argv += optind;
    105 
    106 	if (argc != 2)
    107 		usage();
    108 
    109 	if (realpath(argv[0], target) == 0)
    110 		err(1, "%s", target);
    111 
    112 	if (subdir(target, argv[1]) || subdir(argv[1], target))
    113 		errx(1, "%s (%s) and %s are not distinct paths",
    114 		    argv[0], target, argv[1]);
    115 
    116 	args.target = target;
    117 
    118 	if (mount(MOUNT_UNION, argv[1], mntflags, &args))
    119 		err(1, NULL);
    120 	exit(0);
    121 }
    122 
    123 int
    124 subdir(p, dir)
    125 	const char *p;
    126 	const char *dir;
    127 {
    128 	int l;
    129 
    130 	l = strlen(dir);
    131 	if (l <= 1)
    132 		return (1);
    133 
    134 	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
    135 		return (1);
    136 
    137 	return (0);
    138 }
    139 
    140 void
    141 usage()
    142 {
    143 	(void)fprintf(stderr,
    144 		"usage: mount_union [-br] [-o options] target_fs mount_point\n");
    145 	exit(1);
    146 }
    147