Home | History | Annotate | Line # | Download | only in mount_lfs
mount_lfs.c revision 1.18
      1 /*	$NetBSD: mount_lfs.c,v 1.18 2003/08/07 10:04:28 agc 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.18 2003/08/07 10:04:28 agc 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		main __P((int, char *[]));
     73 int		mount_lfs __P((int, char *[]));
     74 static void	invoke_cleaner __P((char *));
     75 static void	usage __P((void));
     76 static void	kill_daemon __P((char *));
     77 static void	kill_cleaner __P((char *));
     78 
     79 static int short_rds, cleaner_debug, cleaner_bytes;
     80 static char *nsegs;
     81 
     82 #ifndef MOUNT_NOMAIN
     83 int
     84 main(argc, argv)
     85 	int argc;
     86 	char **argv;
     87 {
     88 	return mount_lfs(argc, argv);
     89 }
     90 #endif
     91 
     92 int
     93 mount_lfs(argc, argv)
     94 	int argc;
     95 	char *argv[];
     96 {
     97 	struct ufs_args args;
     98 	int ch, mntflags, noclean, mntsize, oldflags, i;
     99 	char *fs_name, *options;
    100 
    101 	const char *errcause;
    102 	struct statfs *mntbuf;
    103 
    104 	options = NULL;
    105 	nsegs = "4";
    106 	mntflags = noclean = 0;
    107 	cleaner_bytes = 1;
    108 	while ((ch = getopt(argc, argv, "bdN:no:s")) != -1)
    109 		switch (ch) {
    110 		case 'b':
    111 			cleaner_bytes = !cleaner_bytes;
    112 			break;
    113 		case 'd':
    114 			cleaner_debug = 1;
    115 			break;
    116 		case 'n':
    117 			noclean = 1;
    118 			break;
    119 		case 'N':
    120 			nsegs = optarg;
    121 			break;
    122 		case 'o':
    123 			getmntopts(optarg, mopts, &mntflags, 0);
    124 			break;
    125 		case 's':
    126 			short_rds = 1;
    127 			break;
    128 		case '?':
    129 		default:
    130 			usage();
    131 		}
    132 	argc -= optind;
    133 	argv += optind;
    134 
    135 	if (argc != 2)
    136 		usage();
    137 
    138 	args.fspec = argv[0];	/* the name of the device file */
    139 	fs_name = argv[1];	/* the mount point */
    140 
    141 #define DEFAULT_ROOTUID	-2
    142 	args.export.ex_root = DEFAULT_ROOTUID;
    143 	if (mntflags & MNT_RDONLY) {
    144 		args.export.ex_flags = MNT_EXRDONLY;
    145 		noclean = 1;
    146 	} else
    147 		args.export.ex_flags = 0;
    148 
    149 	/*
    150 	 * Record the previous status of this filesystem (if any) before
    151 	 * performing the mount, so we can know whether to start or
    152 	 * kill the cleaner process below.
    153 	 */
    154 	oldflags = MNT_RDONLY; /* If not mounted, pretend r/o */
    155 	if (mntflags & MNT_UPDATE) {
    156 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    157 			err(1, "getmntinfo");
    158 		for (i = 0; i < mntsize; i++) {
    159 			if (strcmp(mntbuf[i].f_mntfromname, args.fspec) == 0) {
    160 				oldflags = mntbuf[i].f_flags;
    161 				break;
    162 			}
    163 		}
    164 	}
    165 
    166 	if (mount(MOUNT_LFS, fs_name, mntflags, &args)) {
    167 		switch (errno) {
    168 		case EMFILE:
    169 			errcause = "mount table full";
    170 			break;
    171 		case EINVAL:
    172 			if (mntflags & MNT_UPDATE)
    173 				errcause =
    174 			    "specified device does not match mounted device";
    175 			else
    176 				errcause = "incorrect super block";
    177 			break;
    178 		default:
    179 			errcause = strerror(errno);
    180 			break;
    181 		}
    182 		errx(1, "%s on %s: %s", args.fspec, fs_name, errcause);
    183 	}
    184 
    185 	/* Not mounting fresh or upgrading to r/w; don't start the cleaner */
    186 	if (!(oldflags & MNT_RDONLY) || (mntflags & MNT_RDONLY))
    187 		noclean = 1;
    188 	if (!noclean)
    189 		invoke_cleaner(fs_name);
    190 		/* NOTREACHED */
    191 
    192 	/* Downgrade to r/o; kill the cleaner */
    193 	if ((mntflags & MNT_RDONLY) && !(oldflags & MNT_RDONLY))
    194 		kill_cleaner(fs_name);
    195 
    196 	exit(0);
    197 }
    198 
    199 static void
    200 kill_daemon(pidname)
    201 	char *pidname;
    202 {
    203 	FILE *fp;
    204 	char s[80];
    205 	pid_t pid;
    206 
    207 	fp = fopen(pidname, "r");
    208 	if (fp) {
    209 		fgets(s, 80, fp);
    210 		pid = atoi(s);
    211 		if (pid)
    212 			kill(pid, SIGINT);
    213 		fclose(fp);
    214 	}
    215 }
    216 
    217 static void
    218 kill_cleaner(name)
    219 	char *name;
    220 {
    221 	char *pidname;
    222 	char *cp;
    223 	int off;
    224 
    225 	/* Parent first */
    226 	asprintf(&pidname, "%slfs_cleanerd:m:%s.pid", _PATH_VARRUN, name);
    227 	if (!pidname)
    228 		err(1, "malloc");
    229 	off = strlen(_PATH_VARRUN);
    230 	while((cp = strchr(pidname + off, '/')) != NULL)
    231 		*cp = '|';
    232 	kill_daemon(pidname);
    233 	free(pidname);
    234 
    235 	/* Then child */
    236 	asprintf(&pidname, "%slfs_cleanerd:s:%s.pid", _PATH_VARRUN, name);
    237 	if (!pidname)
    238 		err(1, "malloc");
    239 	off = strlen(_PATH_VARRUN);
    240 	while((cp = strchr(pidname + off, '/')) != NULL)
    241 		*cp = '|';
    242 	kill_daemon(pidname);
    243 	free(pidname);
    244 }
    245 
    246 static void
    247 invoke_cleaner(name)
    248 	char *name;
    249 {
    250 	char *args[6], **ap = args;
    251 
    252 	/* Build the argument list. */
    253 	*ap++ = _PATH_LFS_CLEANERD;
    254 	if (cleaner_bytes)
    255 		*ap++ = "-b";
    256 	if (nsegs) {
    257 		*ap++ = "-n";
    258 		*ap++ = nsegs;
    259 	}
    260 	if (short_rds)
    261 		*ap++ = "-s";
    262 	if (cleaner_debug)
    263 		*ap++ = "-d";
    264 	*ap++ = name;
    265 	*ap = NULL;
    266 
    267 	execv(args[0], args);
    268 	err(1, "exec %s", _PATH_LFS_CLEANERD);
    269 }
    270 
    271 static void
    272 usage()
    273 {
    274 	(void)fprintf(stderr,
    275 		"usage: mount_lfs [-dns] [-o options] special node\n");
    276 	exit(1);
    277 }
    278