Home | History | Annotate | Line # | Download | only in swapctl
swapctl.c revision 1.30
      1 /*	$NetBSD: swapctl.c,v 1.30 2006/08/22 14:08:36 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997, 1999 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 dump device, if listed in fstab)
     35  *	-D [<dev>|none]	set dumpdev to <dev> or disable dumps
     36  *	-z		show dumpdev
     37  *	-U		remove all devices listed as `sw' in /etc/fstab.
     38  *	-t [blk|noblk]	if -A or -U , add (remove) either all block device
     39  *			or all non-block devices
     40  *	-a <dev>	add this device
     41  *	-d <dev>	remove this swap device
     42  *	-g		use gigabytes
     43  *	-h		use humanize_number(3) for listing
     44  *	-l		list swap devices
     45  *	-m		use megabytes
     46  *	-s		short listing of swap devices
     47  *	-k		use kilobytes
     48  *	-p <pri>	use this priority
     49  *	-c		change priority
     50  *
     51  * or, if invoked as "swapon" (compatibility mode):
     52  *
     53  *	-a		all devices listed as `sw' in /etc/fstab
     54  *	-t		same as -t above (feature not present in old
     55  *			swapon(8) command)
     56  *	<dev>		add this device
     57  */
     58 #include <sys/cdefs.h>
     59 
     60 #ifndef lint
     61 __RCSID("$NetBSD: swapctl.c,v 1.30 2006/08/22 14:08:36 martin Exp $");
     62 #endif
     63 
     64 
     65 #include <sys/param.h>
     66 #include <sys/stat.h>
     67 #include <sys/swap.h>
     68 
     69 #include <unistd.h>
     70 #include <err.h>
     71 #include <errno.h>
     72 #include <stdio.h>
     73 #include <stdlib.h>
     74 #include <string.h>
     75 #include <fstab.h>
     76 
     77 #include "swapctl.h"
     78 
     79 int	command;
     80 
     81 /*
     82  * Commands for swapctl(8).  These are mutually exclusive.
     83  */
     84 #define	CMD_A		0x01	/* process /etc/fstab for adding */
     85 #define	CMD_D		0x02	/* set dumpdev */
     86 #define	CMD_U		0x04	/* process /etc/fstab for removing */
     87 #define	CMD_a		0x08	/* add a swap file/device */
     88 #define	CMD_c		0x10	/* change priority of a swap file/device */
     89 #define	CMD_d		0x20	/* delete a swap file/device */
     90 #define	CMD_l		0x40	/* list swap files/devices */
     91 #define	CMD_s		0x80	/* summary of swap files/devices */
     92 #define	CMD_z		0x100	/* show dump device */
     93 
     94 #define	SET_COMMAND(cmd) \
     95 do { \
     96 	if (command) \
     97 		usage(); \
     98 	command = (cmd); \
     99 } while (0)
    100 
    101 /*
    102  * Commands that require a "path" argument at the end of the command
    103  * line, and the ones which require that none exist.
    104  */
    105 #define	REQUIRE_PATH	(CMD_D | CMD_a | CMD_c | CMD_d)
    106 #define	REQUIRE_NOPATH	(CMD_A | CMD_U | CMD_l | CMD_s | CMD_z)
    107 
    108 /*
    109  * Option flags, and the commands with which they are valid.
    110  */
    111 int	kflag;		/* display in 1K^x blocks */
    112 #define	KFLAG_CMDS	(CMD_l | CMD_s)
    113 #define MFLAG_CMDS	(CMD_l | CMD_s)
    114 #define GFLAG_CMDS	(CMD_l | CMD_s)
    115 
    116 int	hflag;		/* display with humanize_number */
    117 #define HFLAG_CMDS	(CMD_l | CMD_s)
    118 
    119 int	pflag;		/* priority was specified */
    120 #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
    121 
    122 char	*tflag;		/* swap device type (blk or noblk) */
    123 #define	TFLAG_CMDS	(CMD_A | CMD_U)
    124 
    125 int	pri;		/* uses 0 as default pri */
    126 
    127 static	void change_priority(char *);
    128 static	int  add_swap(char *, int);
    129 static	int  delete_swap(char *);
    130 static	void set_dumpdev(char *);
    131 static	int get_dumpdev(void);
    132 static	void do_fstab(int);
    133 static	void usage(void);
    134 static	void swapon_command(int, char **);
    135 #if 0
    136 static	void swapoff_command(int, char **);
    137 #endif
    138 
    139 int
    140 main(int argc, char *argv[])
    141 {
    142 	int	c;
    143 
    144 	if (strcmp(getprogname(), "swapon") == 0) {
    145 		swapon_command(argc, argv);
    146 		/* NOTREACHED */
    147 	}
    148 
    149 #if 0
    150 	if (strcmp(getprogname(), "swapoff") == 0) {
    151 		swapoff_command(argc, argv);
    152 		/* NOTREACHED */
    153 	}
    154 #endif
    155 
    156 	while ((c = getopt(argc, argv, "ADUacdghklmp:st:z")) != -1) {
    157 		switch (c) {
    158 		case 'A':
    159 			SET_COMMAND(CMD_A);
    160 			break;
    161 
    162 		case 'D':
    163 			SET_COMMAND(CMD_D);
    164 			break;
    165 
    166 		case 'U':
    167 			SET_COMMAND(CMD_U);
    168 			break;
    169 
    170 		case 'a':
    171 			SET_COMMAND(CMD_a);
    172 			break;
    173 
    174 		case 'c':
    175 			SET_COMMAND(CMD_c);
    176 			break;
    177 
    178 		case 'd':
    179 			SET_COMMAND(CMD_d);
    180 			break;
    181 
    182 		case 'g':
    183 			kflag = 3; /* 1k ^ 3 */
    184 			break;
    185 
    186 		case 'h':
    187 			hflag = 1;
    188 			break;
    189 
    190 		case 'k':
    191 			kflag = 1;
    192 			break;
    193 
    194 		case 'l':
    195 			SET_COMMAND(CMD_l);
    196 			break;
    197 
    198 		case 'm':
    199 			kflag = 2; /* 1k ^ 2 */
    200 			break;
    201 
    202 		case 'p':
    203 			pflag = 1;
    204 			/* XXX strtol() */
    205 			pri = atoi(optarg);
    206 			break;
    207 
    208 		case 's':
    209 			SET_COMMAND(CMD_s);
    210 			break;
    211 
    212 		case 't':
    213 			if (tflag != NULL)
    214 				usage();
    215 			tflag = optarg;
    216 			break;
    217 
    218 		case 'z':
    219 			SET_COMMAND(CMD_z);
    220 			break;
    221 
    222 		default:
    223 			usage();
    224 			/* NOTREACHED */
    225 		}
    226 	}
    227 
    228 	/* Did the user specify a command? */
    229 	if (command == 0)
    230 		usage();
    231 
    232 	argv += optind;
    233 	argc -= optind;
    234 
    235 	switch (argc) {
    236 	case 0:
    237 		if (command & REQUIRE_PATH)
    238 			usage();
    239 		break;
    240 
    241 	case 1:
    242 		if (command & REQUIRE_NOPATH)
    243 			usage();
    244 		break;
    245 
    246 	default:
    247 		usage();
    248 	}
    249 
    250 	/* To change priority, you have to specify one. */
    251 	if ((command == CMD_c) && pflag == 0)
    252 		usage();
    253 
    254 	/* Sanity-check -t */
    255 	if (tflag != NULL) {
    256 		if (command != CMD_A && command != CMD_U)
    257 			usage();
    258 		if (strcmp(tflag, "blk") != 0 &&
    259 		    strcmp(tflag, "noblk") != 0)
    260 			usage();
    261 	}
    262 
    263 	/* Dispatch the command. */
    264 	switch (command) {
    265 	case CMD_l:
    266 		if (!list_swap(pri, kflag, pflag, 0, 1, hflag))
    267 			exit(1);
    268 		break;
    269 
    270 	case CMD_s:
    271 		list_swap(pri, kflag, pflag, 0, 0, hflag);
    272 		break;
    273 
    274 	case CMD_c:
    275 		change_priority(argv[0]);
    276 		break;
    277 
    278 	case CMD_a:
    279 		if (! add_swap(argv[0], pri))
    280 			exit(1);
    281 		break;
    282 
    283 	case CMD_d:
    284 		if (! delete_swap(argv[0]))
    285 			exit(1);
    286 		break;
    287 
    288 	case CMD_A:
    289 		do_fstab(1);
    290 		break;
    291 
    292 	case CMD_D:
    293 		set_dumpdev(argv[0]);
    294 		break;
    295 
    296 	case CMD_z:
    297 		if (!get_dumpdev())
    298 			exit(1);
    299 		break;
    300 
    301 	case CMD_U:
    302 		do_fstab(0);
    303 		break;
    304 	}
    305 
    306 	exit(0);
    307 }
    308 
    309 /*
    310  * swapon_command: emulate the old swapon(8) program.
    311  */
    312 static void
    313 swapon_command(int argc, char **argv)
    314 {
    315 	int ch, fiztab = 0;
    316 
    317 	while ((ch = getopt(argc, argv, "at:")) != -1) {
    318 		switch (ch) {
    319 		case 'a':
    320 			fiztab = 1;
    321 			break;
    322 		case 't':
    323 			if (tflag != NULL)
    324 				usage();
    325 			tflag = optarg;
    326 			break;
    327 		default:
    328 			goto swapon_usage;
    329 		}
    330 	}
    331 	argc -= optind;
    332 	argv += optind;
    333 
    334 	if (fiztab) {
    335 		if (argc)
    336 			goto swapon_usage;
    337 		/* Sanity-check -t */
    338 		if (tflag != NULL) {
    339 			if (strcmp(tflag, "blk") != 0 &&
    340 			    strcmp(tflag, "noblk") != 0)
    341 				usage();
    342 		}
    343 		do_fstab(1);
    344 		exit(0);
    345 	} else if (argc == 0 || tflag != NULL)
    346 		goto swapon_usage;
    347 
    348 	while (argc) {
    349 		if (! add_swap(argv[0], pri))
    350 			exit(1);
    351 		argc--;
    352 		argv++;
    353 	}
    354 	exit(0);
    355 	/* NOTREACHED */
    356 
    357  swapon_usage:
    358 	fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname());
    359 	fprintf(stderr, "       %s <path> ...\n", getprogname());
    360 	exit(1);
    361 }
    362 
    363 /*
    364  * change_priority:  change the priority of a swap device.
    365  */
    366 static void
    367 change_priority(char *path)
    368 {
    369 
    370 	if (swapctl(SWAP_CTL, path, pri) < 0)
    371 		err(1, "%s", path);
    372 }
    373 
    374 /*
    375  * add_swap:  add the pathname to the list of swap devices.
    376  */
    377 static int
    378 add_swap(char *path, int priority)
    379 {
    380 	struct stat sb;
    381 
    382 	if (stat(path, &sb) < 0)
    383 		goto oops;
    384 
    385 	if (sb.st_mode & S_IROTH)
    386 		warnx("WARNING: %s is readable by the world", path);
    387 	if (sb.st_mode & S_IWOTH)
    388 		warnx("WARNING: %s is writable by the world", path);
    389 
    390 	if (swapctl(SWAP_ON, path, priority) < 0) {
    391 oops:
    392 		err(1, "%s", path);
    393 	}
    394 	return (1);
    395 }
    396 
    397 /*
    398  * delete_swap:  remove the pathname to the list of swap devices.
    399  */
    400 static int
    401 delete_swap(char *path)
    402 {
    403 
    404 	if (swapctl(SWAP_OFF, path, pri) < 0)
    405 		err(1, "%s", path);
    406 	return (1);
    407 }
    408 
    409 static void
    410 set_dumpdev(char *path)
    411 {
    412 	int rv;
    413 
    414 	if (strcmp(path, "none") == 0)
    415 		rv = swapctl(SWAP_DUMPOFF, NULL, 0);
    416 	else
    417 		rv = swapctl(SWAP_DUMPDEV, path, 0);
    418 
    419 	if (rv == -1)
    420 		err(1, "could not set dump device to %s", path);
    421 	else
    422 		printf("%s: setting dump device to %s\n", getprogname(), path);
    423 }
    424 
    425 static int
    426 get_dumpdev(void)
    427 {
    428 	dev_t	dev;
    429 	char 	*name;
    430 
    431 	if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1) {
    432 		warn("could not get dump device");
    433 		return 0;
    434 	} else if (dev == NODEV) {
    435 		printf("no dump device set\n");
    436 		return 0;
    437 	} else {
    438 		name = devname(dev, S_IFBLK);
    439 		printf("dump device is ");
    440 		if (name)
    441 			printf("%s\n", name);
    442 		else
    443 			printf("major %d minor %d\n", major(dev), minor(dev));
    444 	}
    445 	return 1;
    446 }
    447 
    448 static void
    449 do_fstab(int add)
    450 {
    451 	struct	fstab *fp;
    452 	char	*s;
    453 	long	priority;
    454 	struct	stat st;
    455 	int	isblk;
    456 	int	gotone = 0;
    457 
    458 #ifdef RESCUEDIR
    459 #define PATH_MOUNT	RESCUEDIR "/mount_nfs"
    460 #define PATH_UMOUNT	RESCUEDIR "/umount"
    461 #else
    462 #define PATH_MOUNT	"/sbin/mount_nfs"
    463 #define PATH_UMOUNT	"/sbin/umount"
    464 #endif
    465 
    466 	char	cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
    467 
    468 #define PRIORITYEQ	"priority="
    469 #define NFSMNTPT	"nfsmntpt="
    470 	while ((fp = getfsent()) != NULL) {
    471 		char *spec;
    472 
    473 		spec = fp->fs_spec;
    474 		cmd[0] = '\0';
    475 
    476 		if (strcmp(fp->fs_type, "dp") == 0 && add) {
    477 			set_dumpdev(spec);
    478 			continue;
    479 		}
    480 
    481 		if (strcmp(fp->fs_type, "sw") != 0)
    482 			continue;
    483 
    484 		/* handle dp as mnt option */
    485 		if (strstr(fp->fs_mntops, "dp") && add)
    486 			set_dumpdev(spec);
    487 
    488 		isblk = 0;
    489 
    490 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
    491 			s += sizeof(PRIORITYEQ) - 1;
    492 			priority = atol(s);
    493 		} else
    494 			priority = pri;
    495 
    496 		if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
    497 			char *t;
    498 
    499 			/*
    500 			 * Skip this song and dance if we're only
    501 			 * doing block devices.
    502 			 */
    503 			if (tflag != NULL && strcmp(tflag, "blk") == 0)
    504 				continue;
    505 
    506 			t = strpbrk(s, ",");
    507 			if (t != 0)
    508 				*t = '\0';
    509 			spec = strdup(s + strlen(NFSMNTPT));
    510 			if (t != 0)
    511 				*t = ',';
    512 
    513 			if (spec == NULL)
    514 				errx(1, "Out of memory");
    515 
    516 			if (strlen(spec) == 0) {
    517 				warnx("empty mountpoint");
    518 				free(spec);
    519 				continue;
    520 			}
    521 			if (add) {
    522 				snprintf(cmd, sizeof(cmd), "%s %s %s",
    523 					PATH_MOUNT, fp->fs_spec, spec);
    524 				if (system(cmd) != 0) {
    525 					warnx("%s: mount failed", fp->fs_spec);
    526 					continue;
    527 				}
    528 			} else {
    529 				snprintf(cmd, sizeof(cmd), "%s %s",
    530 					PATH_UMOUNT, fp->fs_spec);
    531 			}
    532 		} else {
    533 			/*
    534 			 * Determine blk-ness.
    535 			 */
    536 			if (stat(spec, &st) < 0) {
    537 				warn("%s", spec);
    538 				continue;
    539 			}
    540 			if (S_ISBLK(st.st_mode))
    541 				isblk = 1;
    542 		}
    543 
    544 		/*
    545 		 * Skip this type if we're told to.
    546 		 */
    547 		if (tflag != NULL) {
    548 			if (strcmp(tflag, "blk") == 0 && isblk == 0)
    549 				continue;
    550 			if (strcmp(tflag, "noblk") == 0 && isblk == 1)
    551 				continue;
    552 		}
    553 
    554 		if (add) {
    555 			if (add_swap(spec, (int)priority)) {
    556 				gotone = 1;
    557 				printf(
    558 			    	"%s: adding %s as swap device at priority %d\n",
    559 				    getprogname(), fp->fs_spec, (int)priority);
    560 			}
    561 		} else {
    562 			if (delete_swap(spec)) {
    563 				gotone = 1;
    564 				printf(
    565 				    "%s: removing %s as swap device\n",
    566 				    getprogname(), fp->fs_spec);
    567 			}
    568 			if (cmd[0]) {
    569 				if (system(cmd) != 0) {
    570 					warnx("%s: umount failed", fp->fs_spec);
    571 					continue;
    572 				}
    573 			}
    574 		}
    575 
    576 		if (spec != fp->fs_spec)
    577 			free(spec);
    578 	}
    579 	if (gotone == 0)
    580 		exit(1);
    581 }
    582 
    583 static void
    584 usage(void)
    585 {
    586 	const char *progname = getprogname();
    587 
    588 	fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
    589 	    progname);
    590 	fprintf(stderr, "       %s -D dumppath\n", progname);
    591 	fprintf(stderr, "       %s -U [-t blk|noblk]\n", progname);
    592 	fprintf(stderr, "       %s -a [-p priority] path\n", progname);
    593 	fprintf(stderr, "       %s -c -p priority path\n", progname);
    594 	fprintf(stderr, "       %s -d path\n", progname);
    595 	fprintf(stderr, "       %s -l | -s [-k|-m|-g|-h]\n", progname);
    596 	fprintf(stderr, "       %s -z\n", progname);
    597 	exit(1);
    598 }
    599