Home | History | Annotate | Line # | Download | only in swapctl
swapctl.c revision 1.10
      1 /*	$NetBSD: swapctl.c,v 1.10 1999/02/23 17:00:53 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997 Matthew R. Green
      5  * 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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * swapctl command:
     33  *	-A		add all devices listed as `sw' in /etc/fstab (also
     34  *			(sets the the dump device, if listed in fstab)
     35  *	-D <dev>	set dumpdev to <dev>
     36  *	-t [blk|noblk]	if -A, add either all block device or all non-block
     37  *			devices
     38  *	-a <dev>	add this device
     39  *	-d <dev>	remove this swap device (not supported yet)
     40  *	-l		list swap devices
     41  *	-s		short listing of swap devices
     42  *	-k		use kilobytes
     43  *	-p <pri>	use this priority
     44  *	-c		change priority
     45  *
     46  * or, if invoked as "swapon" (compatibility mode):
     47  *
     48  *	-a		all devices listed as `sw' in /etc/fstab
     49  *	-t		same as -t above (feature not present in old
     50  *			swapon(8) command)
     51  *	<dev>		add this device
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/stat.h>
     56 
     57 #include <vm/vm_swap.h>
     58 
     59 #include <unistd.h>
     60 #include <err.h>
     61 #include <errno.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <fstab.h>
     66 
     67 #include "swapctl.h"
     68 
     69 int	command;
     70 
     71 /*
     72  * Commands for swapctl(8).  These are mutually exclusive.
     73  */
     74 #define	CMD_A		0x01	/* process /etc/fstab */
     75 #define	CMD_D		0x02	/* set dumpdev */
     76 #define	CMD_a		0x04	/* add a swap file/device */
     77 #define	CMD_c		0x08	/* change priority of a swap file/device */
     78 #define	CMD_d		0x10	/* delete a swap file/device */
     79 #define	CMD_l		0x20	/* list swap files/devices */
     80 #define	CMD_s		0x40	/* summary of swap files/devices */
     81 
     82 #define	SET_COMMAND(cmd) \
     83 do { \
     84 	if (command) \
     85 		usage(); \
     86 	command = (cmd); \
     87 } while (0)
     88 
     89 /*
     90  * Commands that require a "path" argument at the end of the command
     91  * line, and the ones which require that none exist.
     92  */
     93 #define	REQUIRE_PATH	(CMD_D | CMD_a | CMD_c | CMD_d)
     94 #define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s)
     95 
     96 /*
     97  * Option flags, and the commands with which they are valid.
     98  */
     99 int	kflag;		/* display in 1K blocks */
    100 #define	KFLAG_CMDS	(CMD_l | CMD_s)
    101 
    102 int	pflag;		/* priority was specified */
    103 #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
    104 
    105 char	*tflag;		/* swap device type (blk or noblk) */
    106 #define	TFLAG_CMDS	(CMD_A)
    107 
    108 int	pri;		/* uses 0 as default pri */
    109 
    110 static	void change_priority __P((const char *));
    111 static	void add_swap __P((const char *));
    112 static	void del_swap __P((const char *));
    113 static	void set_dumpdev __P((const char *));
    114 	int  main __P((int, char *[]));
    115 static	void do_fstab __P((void));
    116 static	void usage __P((void));
    117 static	void swapon_command __P((int, char **));
    118 #if 0
    119 static	void swapoff_command __P((int, char **));
    120 #endif
    121 
    122 extern	char *__progname;	/* from crt0.o */
    123 
    124 int
    125 main(argc, argv)
    126 	int	argc;
    127 	char	*argv[];
    128 {
    129 	int	c;
    130 
    131 	if (strcmp(__progname, "swapon") == 0) {
    132 		swapon_command(argc, argv);
    133 		/* NOTREACHED */
    134 	}
    135 
    136 #if 0
    137 	if (strcmp(__progname, "swapoff") == 0) {
    138 		swapoff_command(argc, argv);
    139 		/* NOTREACHED */
    140 	}
    141 #endif
    142 
    143 	while ((c = getopt(argc, argv, "ADacdlkp:st:")) != -1) {
    144 		switch (c) {
    145 		case 'A':
    146 			SET_COMMAND(CMD_A);
    147 			break;
    148 
    149 		case 'D':
    150 			SET_COMMAND(CMD_D);
    151 			break;
    152 
    153 		case 'a':
    154 			SET_COMMAND(CMD_a);
    155 			break;
    156 
    157 		case 'c':
    158 			SET_COMMAND(CMD_c);
    159 			break;
    160 
    161 		case 'd':
    162 			SET_COMMAND(CMD_d);
    163 			break;
    164 
    165 		case 'l':
    166 			SET_COMMAND(CMD_l);
    167 			break;
    168 
    169 		case 'k':
    170 			kflag = 1;
    171 			break;
    172 
    173 		case 'p':
    174 			pflag = 1;
    175 			/* XXX strtol() */
    176 			pri = atoi(optarg);
    177 			break;
    178 
    179 		case 's':
    180 			SET_COMMAND(CMD_s);
    181 			break;
    182 
    183 		case 't':
    184 			if (tflag != NULL)
    185 				usage();
    186 			tflag = optarg;
    187 			break;
    188 
    189 		default:
    190 			usage();
    191 			/* NOTREACHED */
    192 		}
    193 	}
    194 
    195 	/* Did the user specify a command? */
    196 	if (command == 0)
    197 		usage();
    198 
    199 	argv += optind;
    200 	argc -= optind;
    201 
    202 	switch (argc) {
    203 	case 0:
    204 		if (command & REQUIRE_PATH)
    205 			usage();
    206 		break;
    207 
    208 	case 1:
    209 		if (command & REQUIRE_NOPATH)
    210 			usage();
    211 		break;
    212 
    213 	default:
    214 		usage();
    215 	}
    216 
    217 	/* To change priority, you have to specify one. */
    218 	if ((command == CMD_c) && pflag == 0)
    219 		usage();
    220 
    221 	/* Sanity-check -t */
    222 	if (tflag != NULL) {
    223 		if (command != CMD_A)
    224 			usage();
    225 		if (strcmp(tflag, "blk") != 0 &&
    226 		    strcmp(tflag, "noblk") != 0)
    227 			usage();
    228 	}
    229 
    230 	/* Dispatch the command. */
    231 	switch (command) {
    232 	case CMD_l:
    233 		list_swap(pri, kflag, pflag, 0, 1);
    234 		break;
    235 
    236 	case CMD_s:
    237 		list_swap(pri, kflag, pflag, 0, 0);
    238 		break;
    239 
    240 	case CMD_c:
    241 		change_priority(argv[0]);
    242 		break;
    243 
    244 	case CMD_a:
    245 		add_swap(argv[0]);
    246 		break;
    247 
    248 	case CMD_d:
    249 		del_swap(argv[0]);
    250 		break;
    251 
    252 	case CMD_A:
    253 		do_fstab();
    254 		break;
    255 
    256 	case CMD_D:
    257 		set_dumpdev(argv[0]);
    258 		break;
    259 	}
    260 
    261 	exit(0);
    262 }
    263 
    264 /*
    265  * swapon_command: emulate the old swapon(8) program.
    266  */
    267 void
    268 swapon_command(argc, argv)
    269 	int argc;
    270 	char **argv;
    271 {
    272 	int ch, fiztab = 0;
    273 
    274 	while ((ch = getopt(argc, argv, "at:")) != -1) {
    275 		switch (ch) {
    276 		case 'a':
    277 			fiztab = 1;
    278 			break;
    279 		case 't':
    280 			if (tflag != NULL)
    281 				usage();
    282 			tflag = optarg;
    283 			break;
    284 		default:
    285 			goto swapon_usage;
    286 		}
    287 	}
    288 	argc -= optind;
    289 	argv += optind;
    290 
    291 	if (fiztab) {
    292 		if (argc)
    293 			goto swapon_usage;
    294 		/* Sanity-check -t */
    295 		if (tflag != NULL) {
    296 			if (strcmp(tflag, "blk") != 0 &&
    297 			    strcmp(tflag, "noblk") != 0)
    298 				usage();
    299 		}
    300 		do_fstab();
    301 		exit(0);
    302 	} else if (argc == 0 || tflag != NULL)
    303 		goto swapon_usage;
    304 
    305 	while (argc) {
    306 		add_swap(argv[0]);
    307 		argc--;
    308 		argv++;
    309 	}
    310 	exit(0);
    311 	/* NOTREACHED */
    312 
    313  swapon_usage:
    314 	fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", __progname);
    315 	fprintf(stderr, "       %s <path> ...\n", __progname);
    316 	exit(1);
    317 }
    318 
    319 /*
    320  * change_priority:  change the priority of a swap device.
    321  */
    322 void
    323 change_priority(path)
    324 	const char	*path;
    325 {
    326 
    327 	if (swapctl(SWAP_CTL, path, pri) < 0)
    328 		warn("%s", path);
    329 }
    330 
    331 /*
    332  * add_swap:  add the pathname to the list of swap devices.
    333  */
    334 void
    335 add_swap(path)
    336 	const char *path;
    337 {
    338 	struct stat sb;
    339 
    340 	if (stat(path, &sb) < 0)
    341 		goto oops;
    342 
    343 	if (sb.st_mode & S_IROTH)
    344 		warnx("%s is readable by the world", path);
    345 	if (sb.st_mode & S_IWOTH)
    346 		warnx("%s is writable by the world", path);
    347 
    348 	if (swapctl(SWAP_ON, path, pri) < 0)
    349 oops:
    350 		err(1, "%s", path);
    351 }
    352 
    353 /*
    354  * del_swap:  remove the pathname to the list of swap devices.
    355  */
    356 void
    357 del_swap(path)
    358 	const char *path;
    359 {
    360 
    361 	if (swapctl(SWAP_OFF, path, pri) < 0)
    362 		err(1, "%s", path);
    363 }
    364 
    365 void
    366 set_dumpdev(path)
    367 	const char *path;
    368 {
    369 
    370 	if (swapctl(SWAP_DUMPDEV, path, NULL) == -1)
    371 		warn("could not set dump device to %s", path);
    372 	else
    373 		printf("%s: setting dump device to %s\n", __progname, path);
    374 }
    375 
    376 void
    377 do_fstab()
    378 {
    379 	struct	fstab *fp;
    380 	char	*s;
    381 	long	priority;
    382 	struct	stat st;
    383 	int	isblk;
    384 	int	gotone = 0;
    385 
    386 #define PRIORITYEQ	"priority="
    387 #define NFSMNTPT	"nfsmntpt="
    388 #define PATH_MOUNT	"/sbin/mount_nfs"
    389 	while ((fp = getfsent()) != NULL) {
    390 		const char *spec;
    391 
    392 		spec = fp->fs_spec;
    393 
    394 		if (strcmp(fp->fs_type, "dp") == 0) {
    395 			set_dumpdev(spec);
    396 			continue;
    397 		}
    398 
    399 		if (strcmp(fp->fs_type, "sw") != 0)
    400 			continue;
    401 		isblk = 0;
    402 
    403 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
    404 			s += sizeof(PRIORITYEQ) - 1;
    405 			priority = atol(s);
    406 		} else
    407 			priority = pri;
    408 
    409 		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
    410 			char *t, cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
    411 
    412 			/*
    413 			 * Skip this song and dance if we're only
    414 			 * doing block devices.
    415 			 */
    416 			if (tflag != NULL &&
    417 			    strcmp(tflag, "blk") == 0)
    418 				continue;
    419 
    420 			t = strpbrk(s, ",");
    421 			if (t != 0)
    422 				*t = '\0';
    423 			spec = strdup(s + strlen(NFSMNTPT));
    424 			if (t != 0)
    425 				*t = ',';
    426 
    427 			if (spec == NULL)
    428 				errx(1, "Out of memory");
    429 
    430 			if (strlen(spec) == 0) {
    431 				warnx("empty mountpoint");
    432 				free((char *)spec);
    433 				continue;
    434 			}
    435 			snprintf(cmd, sizeof(cmd), "%s %s %s",
    436 				PATH_MOUNT, fp->fs_spec, spec);
    437 			if (system(cmd) != 0) {
    438 				warnx("%s: mount failed", fp->fs_spec);
    439 				continue;
    440 			}
    441 		} else {
    442 			/*
    443 			 * Determine blk-ness.
    444 			 */
    445 			if (stat(spec, &st) < 0) {
    446 				warn(spec);
    447 				continue;
    448 			}
    449 			if (S_ISBLK(st.st_mode))
    450 				isblk = 1;
    451 		}
    452 
    453 		/*
    454 		 * Skip this type if we're told to.
    455 		 */
    456 		if (tflag != NULL) {
    457 			if (strcmp(tflag, "blk") == 0 && isblk == 0)
    458 				continue;
    459 			if (strcmp(tflag, "noblk") == 0 && isblk == 1)
    460 				continue;
    461 		}
    462 
    463 		if (swapctl(SWAP_ON, spec, (int)priority) < 0)
    464 			warn("%s", spec);
    465 		else {
    466 			gotone = 1;
    467 			printf("%s: adding %s as swap device at priority %d\n",
    468 			    __progname, fp->fs_spec, (int)priority);
    469 		}
    470 
    471 		if (spec != fp->fs_spec)
    472 			free((char *)spec);
    473 	}
    474 	if (gotone == 0)
    475 		exit(1);
    476 }
    477 
    478 void
    479 usage()
    480 {
    481 
    482 	fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
    483 	    __progname);
    484 	fprintf(stderr, "       %s -a [-p priority] path\n", __progname);
    485 	fprintf(stderr, "       %s -c -p priority path\n", __progname);
    486 	fprintf(stderr, "       %s -d path\n", __progname);
    487 	fprintf(stderr, "       %s -l | -s [-k]\n", __progname);
    488 	exit(1);
    489 }
    490