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