Home | History | Annotate | Line # | Download | only in mount_lfs
mount_lfs.c revision 1.25
      1 /*	$NetBSD: mount_lfs.c,v 1.25 2005/06/27 02:56:20 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1993, 1994\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)mount_lfs.c	8.4 (Berkeley) 4/26/95";
     41 #else
     42 __RCSID("$NetBSD: mount_lfs.c,v 1.25 2005/06/27 02:56:20 christos Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/mount.h>
     48 
     49 #include <ufs/ufs/ufsmount.h>
     50 
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 #include <paths.h>
     58 
     59 #include <signal.h>
     60 
     61 #include <mntopts.h>
     62 #include "pathnames.h"
     63 
     64 static const struct mntopt mopts[] = {
     65 	MOPT_STDOPTS,
     66 	MOPT_UPDATE,
     67 	MOPT_GETARGS,
     68 	MOPT_NOATIME,
     69 	{ NULL }
     70 };
     71 
     72 int		mount_lfs(int, char *[]);
     73 static void	invoke_cleaner(char *);
     74 static void	usage(void);
     75 static void	kill_daemon(char *);
     76 static void	kill_cleaner(char *);
     77 
     78 static int short_rds, cleaner_debug, cleaner_bytes, fs_idle;
     79 static const char *nsegs;
     80 
     81 #ifndef MOUNT_NOMAIN
     82 int
     83 main(int argc, char **argv)
     84 {
     85 	return mount_lfs(argc, argv);
     86 }
     87 #endif
     88 
     89 int
     90 mount_lfs(int argc, char *argv[])
     91 {
     92 	struct ufs_args args;
     93 	int ch, mntflags, noclean, mntsize, oldflags, i;
     94 	char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
     95 	char *options;
     96 
     97 	const char *errcause;
     98 	struct statvfs *mntbuf;
     99 
    100 	options = NULL;
    101 	nsegs = "4";
    102 	mntflags = noclean = 0;
    103 	cleaner_bytes = 1;
    104 	while ((ch = getopt(argc, argv, "bdiN:no:s")) != -1)
    105 		switch (ch) {
    106 		case 'b':
    107 			cleaner_bytes = !cleaner_bytes;
    108 			break;
    109 		case 'd':
    110 			cleaner_debug = 1;
    111 			break;
    112 		case 'i':
    113 			fs_idle = 1;
    114 			break;
    115 		case 'n':
    116 			noclean = 1;
    117 			break;
    118 		case 'N':
    119 			nsegs = optarg;
    120 			break;
    121 		case 'o':
    122 			getmntopts(optarg, mopts, &mntflags, 0);
    123 			break;
    124 		case 's':
    125 			short_rds = 1;
    126 			break;
    127 		case '?':
    128 		default:
    129 			usage();
    130 		}
    131 	argc -= optind;
    132 	argv += optind;
    133 
    134 	if (argc != 2)
    135 		usage();
    136 
    137 	if (realpath(argv[0], canon_dev) == NULL)     /* Check device path */
    138 		err(1, "realpath %s", argv[0]);
    139 	if (strncmp(argv[0], canon_dev, MAXPATHLEN)) {
    140 		warnx("\"%s\" is a relative path.", argv[0]);
    141 		warnx("using \"%s\" instead.", canon_dev);
    142 	}
    143 	args.fspec = canon_dev;
    144 
    145 	if (realpath(argv[1], fs_name) == NULL)      /* Check mounton path */
    146 		err(1, "realpath %s", argv[1]);
    147 	if (strncmp(argv[1], fs_name, MAXPATHLEN)) {
    148 		warnx("\"%s\" is a relative path.", argv[1]);
    149 		warnx("using \"%s\" instead.", fs_name);
    150 	}
    151 
    152 #define DEFAULT_ROOTUID	-2
    153 	args.export.ex_root = DEFAULT_ROOTUID;
    154 	if (mntflags & MNT_RDONLY) {
    155 		args.export.ex_flags = MNT_EXRDONLY;
    156 		noclean = 1;
    157 	} else
    158 		args.export.ex_flags = 0;
    159 
    160 	/*
    161 	 * Record the previous status of this filesystem (if any) before
    162 	 * performing the mount, so we can know whether to start or
    163 	 * kill the cleaner process below.
    164 	 */
    165 	oldflags = MNT_RDONLY; /* If not mounted, pretend r/o */
    166 	if (mntflags & MNT_UPDATE) {
    167 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    168 			err(1, "getmntinfo");
    169 		for (i = 0; i < mntsize; i++) {
    170 			if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
    171 				oldflags = mntbuf[i].f_flag;
    172 				break;
    173 			}
    174 		}
    175 	}
    176 
    177 	if (mount(MOUNT_LFS, fs_name, mntflags, &args)) {
    178 		switch (errno) {
    179 		case EMFILE:
    180 			errcause = "mount table full";
    181 			break;
    182 		case EINVAL:
    183 			if (mntflags & MNT_UPDATE)
    184 				errcause =
    185 			    "specified device does not match mounted device";
    186 			else
    187 				errcause = "incorrect super block";
    188 			break;
    189 		default:
    190 			errcause = strerror(errno);
    191 			break;
    192 		}
    193 		errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
    194 	}
    195 
    196 	/* Not mounting fresh or upgrading to r/w; don't start the cleaner */
    197 	if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY))
    198 		noclean = 1;
    199 	if (!noclean)
    200 		invoke_cleaner(fs_name);
    201 		/* NOTREACHED */
    202 
    203 	/* Downgrade to r/o; kill the cleaner */
    204 	if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
    205 		kill_cleaner(fs_name);
    206 
    207 	exit(0);
    208 }
    209 
    210 static void
    211 kill_daemon(char *pidname)
    212 {
    213 	FILE *fp;
    214 	char s[80];
    215 	pid_t pid;
    216 
    217 	fp = fopen(pidname, "r");
    218 	if (fp) {
    219 		fgets(s, 80, fp);
    220 		pid = atoi(s);
    221 		if (pid)
    222 			kill(pid, SIGINT);
    223 		fclose(fp);
    224 	}
    225 }
    226 
    227 static void
    228 kill_cleaner(char *name)
    229 {
    230 	char *pidname;
    231 	char *cp;
    232 	int off;
    233 
    234 	/* Parent first */
    235 	asprintf(&pidname, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN, name);
    236 	if (!pidname)
    237 		err(1, "malloc");
    238 	off = strlen(_PATH_VARRUN);
    239 	while((cp = strchr(pidname + off, '/')) != NULL)
    240 		*cp = '|';
    241 	kill_daemon(pidname);
    242 	free(pidname);
    243 
    244 	/* Then child */
    245 	asprintf(&pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
    246 	if (!pidname)
    247 		err(1, "malloc");
    248 	off = strlen(_PATH_VARRUN);
    249 	while((cp = strchr(pidname + off, '/')) != NULL)
    250 		*cp = '|';
    251 	kill_daemon(pidname);
    252 	free(pidname);
    253 }
    254 
    255 static void
    256 invoke_cleaner(char *name)
    257 {
    258 	const char *args[7], **ap = args;
    259 
    260 	/* Build the argument list. */
    261 	*ap++ = _PATH_LFS_CLEANERD;
    262 	if (cleaner_bytes)
    263 		*ap++ = "-b";
    264 	if (nsegs) {
    265 		*ap++ = "-n";
    266 		*ap++ = nsegs;
    267 	}
    268 	if (short_rds)
    269 		*ap++ = "-s";
    270 	if (cleaner_debug)
    271 		*ap++ = "-d";
    272 	if (fs_idle)
    273 		*ap++ = "-f";
    274 	*ap++ = name;
    275 	*ap = NULL;
    276 
    277 	execv(args[0], __UNCONST(args));
    278 	err(1, "exec %s", _PATH_LFS_CLEANERD);
    279 }
    280 
    281 static void
    282 usage(void)
    283 {
    284 	(void)fprintf(stderr,
    285 		"usage: %s [-bdins] [-N nsegs] [-o options] special node\n",
    286 		getprogname());
    287 	exit(1);
    288 }
    289