fusermount.c revision 1.1
11.1Sagc/*
21.1Sagc * Copyright � 2007 Alistair Crooks.  All rights reserved.
31.1Sagc *
41.1Sagc * Redistribution and use in source and binary forms, with or without
51.1Sagc * modification, are permitted provided that the following conditions
61.1Sagc * are met:
71.1Sagc * 1. Redistributions of source code must retain the above copyright
81.1Sagc *    notice, this list of conditions and the following disclaimer.
91.1Sagc * 2. Redistributions in binary form must reproduce the above copyright
101.1Sagc *    notice, this list of conditions and the following disclaimer in the
111.1Sagc *    documentation and/or other materials provided with the distribution.
121.1Sagc * 3. The name of the author may not be used to endorse or promote
131.1Sagc *    products derived from this software without specific prior written
141.1Sagc *    permission.
151.1Sagc *
161.1Sagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
171.1Sagc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
181.1Sagc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Sagc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
201.1Sagc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
221.1Sagc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
241.1Sagc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
251.1Sagc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
261.1Sagc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271.1Sagc */
281.1Sagc#include <sys/cdefs.h>
291.1Sagc
301.1Sagc#ifndef lint
311.1Sagc__COPYRIGHT("@(#) Copyright � (c) 2007 \
321.1Sagc                The NetBSD Foundation, Inc.  All rights reserved.");
331.1Sagc__RCSID("$NetBSD: fusermount.c,v 1.1 2007/04/26 21:13:39 agc Exp $");
341.1Sagc#endif
351.1Sagc
361.1Sagc#include <sys/types.h>
371.1Sagc#include <sys/param.h>
381.1Sagc#include <sys/mount.h>
391.1Sagc
401.1Sagc#include <err.h>
411.1Sagc#include <stdio.h>
421.1Sagc#include <stdlib.h>
431.1Sagc#include <string.h>
441.1Sagc#include <unistd.h>
451.1Sagc
461.1Sagcenum {
471.1Sagc	FlagCheckPerm = 1,
481.1Sagc	FlagKernelCache,
491.1Sagc	FlagNonrootUsers,
501.1Sagc
511.1Sagc	ActionMount,
521.1Sagc	ActionUnmount
531.1Sagc};
541.1Sagc
551.1Sagc/* unmount mount point(s) */
561.1Sagcstatic int
571.1Sagcrefuse_unmount(int argc, char **argv)
581.1Sagc{
591.1Sagc	int	ret;
601.1Sagc	int	i;
611.1Sagc
621.1Sagc	for (ret = 1, i = 0 ; i < argc ; i++) {
631.1Sagc		if (unmount(argv[i], 0) < 0) {
641.1Sagc			warn("can't unmount `%s'", argv[i]);
651.1Sagc			ret = 0;
661.1Sagc		}
671.1Sagc	}
681.1Sagc	return ret;
691.1Sagc}
701.1Sagc
711.1Sagc/* print the usage meessage */
721.1Sagcstatic void
731.1Sagcusage(const char *prog)
741.1Sagc{
751.1Sagc	(void) fprintf(stderr,
761.1Sagc		"Usage: %s [-c] [-d name] [-h] [-p] [-u] [-x] mountpoint...\n",
771.1Sagc		prog);
781.1Sagc	(void) fprintf(stderr, "\t-c\tuse kernel cache\n");
791.1Sagc	(void) fprintf(stderr, "\t-d name\tuse name in mount information\n");
801.1Sagc	(void) fprintf(stderr, "\t-h\tprint help information\n");
811.1Sagc	(void) fprintf(stderr, "\t-p\tcheck file permissions\n");
821.1Sagc	(void) fprintf(stderr, "\t-u\tunmount mount point(s)\n");
831.1Sagc	(void) fprintf(stderr,
841.1Sagc		"\t-x\tallow access to mortal (non-root) users\n");
851.1Sagc}
861.1Sagc
871.1Sagcint
881.1Sagcmain(int argc, char **argv)
891.1Sagc{
901.1Sagc	char	*progname;
911.1Sagc	char	*execme;
921.1Sagc	int	 flags;
931.1Sagc	int	 action;
941.1Sagc	int	 i;
951.1Sagc
961.1Sagc	progname = NULL;
971.1Sagc	flags = 0;
981.1Sagc	action = ActionMount;
991.1Sagc	while ((i = getopt(argc, argv, "cd:hpux")) != -1) {
1001.1Sagc		switch(i) {
1011.1Sagc		case 'c':
1021.1Sagc			flags |= FlagKernelCache;
1031.1Sagc			break;
1041.1Sagc		case 'd':
1051.1Sagc			progname = optarg;
1061.1Sagc			break;
1071.1Sagc		case 'h':
1081.1Sagc			usage(*argv);
1091.1Sagc			exit(EXIT_SUCCESS);
1101.1Sagc		case 'p':
1111.1Sagc			flags |= FlagCheckPerm;
1121.1Sagc			break;
1131.1Sagc		case 'u':
1141.1Sagc			action = ActionUnmount;
1151.1Sagc			break;
1161.1Sagc		case 'x':
1171.1Sagc			if (geteuid() != 0) {
1181.1Sagc				err(EXIT_FAILURE,
1191.1Sagc				"-x option is only allowed for use by root");
1201.1Sagc			}
1211.1Sagc			flags |= FlagNonrootUsers;
1221.1Sagc			break;
1231.1Sagc		default:
1241.1Sagc			warnx("Unrecognised argument `%c'", i);
1251.1Sagc			usage(*argv);
1261.1Sagc			exit(EXIT_FAILURE);
1271.1Sagc		}
1281.1Sagc	}
1291.1Sagc	if (optind >= argc - 2) {
1301.1Sagc		warnx("Not enough command line arguments");
1311.1Sagc		usage(*argv);
1321.1Sagc		exit(EXIT_FAILURE);
1331.1Sagc	}
1341.1Sagc	execme = argv[optind + 1];
1351.1Sagc	if (progname) {
1361.1Sagc		argv[optind + 1] = progname;
1371.1Sagc	}
1381.1Sagc	/* mountpoint = argv[optind]; */
1391.1Sagc	switch(action) {
1401.1Sagc	case ActionMount:
1411.1Sagc		execvp(execme, &argv[optind + 1]);
1421.1Sagc		break;
1431.1Sagc	case ActionUnmount:
1441.1Sagc		if (!refuse_unmount(argc - optind, argv + optind)) {
1451.1Sagc			exit(EXIT_FAILURE);
1461.1Sagc		}
1471.1Sagc		break;
1481.1Sagc	}
1491.1Sagc	exit(EXIT_SUCCESS);
1501.1Sagc}
151