Home | History | Annotate | Line # | Download | only in swapctl
swapctl.c revision 1.2
      1 /*	$NetBSD: swapctl.c,v 1.2 1997/06/15 03:47:53 thorpej 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Matthew R. Green for
     18  *      The NetBSD Foundation.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * swapctl command:
     37  *	-A		add all devices listed as `sw' in /etc/fstab
     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  *	<dev>		add this device
     50  */
     51 
     52 #include <sys/param.h>
     53 
     54 #include <vm/vm_swap.h>
     55 
     56 #include <unistd.h>
     57 #include <errno.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <fstab.h>
     62 
     63 #include "swapctl.h"
     64 
     65 int	command;
     66 
     67 /*
     68  * Commands for swapctl(8).  These are mutually exclusive.
     69  */
     70 #define	CMD_A		0x01	/* process /etc/fstab */
     71 #define	CMD_a		0x02	/* add a swap file/device */
     72 #define	CMD_c		0x04	/* change priority of a swap file/device */
     73 #define	CMD_d		0x08	/* delete a swap file/device */
     74 #define	CMD_l		0x10	/* list swap files/devices */
     75 #define	CMD_s		0x20	/* summary of swap files/devices */
     76 
     77 #define	SET_COMMAND(cmd) \
     78 do { \
     79 	if (command) \
     80 		usage(); \
     81 	command = (cmd); \
     82 } while (0)
     83 
     84 /*
     85  * Commands that require a "path" argument at the end of the command
     86  * line, and the ones which require that none exist.
     87  */
     88 #define	REQUIRE_PATH	(CMD_a | CMD_c | CMD_d)
     89 #define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s)
     90 
     91 /*
     92  * Option flags, and the commands with which they are valid.
     93  */
     94 int	kflag;		/* display in 1K blocks */
     95 #define	KFLAG_CMDS	(CMD_l | CMD_s)
     96 
     97 int	pflag;		/* priority was specified */
     98 #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
     99 
    100 int	pri;		/* uses 0 as default pri */
    101 
    102 static	void change_priority __P((char *));
    103 static	void add_swap __P((char *));
    104 static	void del_swap __P((char *));
    105 static	void do_fstab __P((void));
    106 static	void usage __P((void));
    107 static	void swapon_command __P((int, char **));
    108 #if 0
    109 static	void swapoff_command __P((int, char **));
    110 #endif
    111 
    112 extern	char *__progname;	/* from crt0.o */
    113 
    114 int
    115 main(argc, argv)
    116 	int	argc;
    117 	char	*argv[];
    118 {
    119 	int	c;
    120 
    121 	if (strcmp(__progname, "swapon") == 0) {
    122 		swapon_command(argc, argv);
    123 		/* NOTREACHED */
    124 	}
    125 
    126 #if 0
    127 	if (strcmp(__progname, "swapoff") == 0) {
    128 		swapoff_command(argc, argv);
    129 		/* NOTREACHED */
    130 	}
    131 #endif
    132 
    133 	while ((c = getopt(argc, argv, "Aacdlkp:s")) != -1) {
    134 		switch (c) {
    135 		case 'A':
    136 			SET_COMMAND(CMD_A);
    137 			break;
    138 
    139 		case 'a':
    140 			SET_COMMAND(CMD_a);
    141 			break;
    142 
    143 		case 'c':
    144 			SET_COMMAND(CMD_c);
    145 			break;
    146 
    147 		case 'd':
    148 			SET_COMMAND(CMD_d);
    149 			break;
    150 
    151 		case 'l':
    152 			SET_COMMAND(CMD_l);
    153 			break;
    154 
    155 		case 'k':
    156 			kflag = 1;
    157 			break;
    158 
    159 		case 'p':
    160 			pflag = 1;
    161 			/* XXX strtol() */
    162 			pri = atoi(optarg);
    163 			break;
    164 
    165 		case 's':
    166 			SET_COMMAND(CMD_s);
    167 			break;
    168 
    169 		default:
    170 			usage();
    171 			/* NOTREACHED */
    172 		}
    173 	}
    174 
    175 	/* Did the user specify a command? */
    176 	if (command == 0)
    177 		usage();
    178 
    179 	argv += optind;
    180 	argc -= optind;
    181 
    182 	switch (argc) {
    183 	case 0:
    184 		if (command & REQUIRE_PATH)
    185 			usage();
    186 		break;
    187 
    188 	case 1:
    189 		if (command & REQUIRE_NOPATH)
    190 			usage();
    191 		break;
    192 
    193 	default:
    194 		usage();
    195 	}
    196 
    197 	/* To change priority, you have to specify one. */
    198 	if ((command == CMD_c) && pflag == 0)
    199 		usage();
    200 
    201 	/* Dispatch the command. */
    202 	switch (command) {
    203 	case CMD_l:
    204 		list_swap(pri, kflag, pflag, 0, 1);
    205 		break;
    206 
    207 	case CMD_s:
    208 		list_swap(pri, kflag, pflag, 0, 0);
    209 		break;
    210 
    211 	case CMD_c:
    212 		change_priority(argv[0]);
    213 		break;
    214 
    215 	case CMD_a:
    216 		add_swap(argv[0]);
    217 		break;
    218 
    219 	case CMD_d:
    220 		del_swap(argv[0]);
    221 		break;
    222 
    223 	case CMD_A:
    224 		do_fstab();
    225 		break;
    226 	}
    227 
    228 	exit(0);
    229 }
    230 
    231 /*
    232  * swapon_command: emulate the old swapon(8) program.
    233  */
    234 void
    235 swapon_command(argc, argv)
    236 	int argc;
    237 	char **argv;
    238 {
    239 	int ch, fiztab = 0;
    240 
    241 	while ((ch = getopt(argc, argv, "a")) != -1) {
    242 		switch (ch) {
    243 		case 'a':
    244 			fiztab = 1;
    245 			break;
    246 		default:
    247 			goto swapon_usage;
    248 		}
    249 	}
    250 	argc -= optind;
    251 	argv += optind;
    252 
    253 	if (fiztab) {
    254 		if (argc)
    255 			goto swapon_usage;
    256 		do_fstab();
    257 		exit(0);
    258 	} else if (argc == 0)
    259 		goto swapon_usage;
    260 
    261 	while (argc) {
    262 		add_swap(argv[0]);
    263 		argc--;
    264 		argv++;
    265 	}
    266 	exit(0);
    267 	/* NOTREACHED */
    268 
    269  swapon_usage:
    270 	fprintf(stderr, "usage: %s -a\n", __progname);
    271 	fprintf(stderr, "       %s <path> ...\n", __progname);
    272 	exit(1);
    273 }
    274 
    275 /*
    276  * change_priority:  change the priority of a swap device.
    277  */
    278 void
    279 change_priority(path)
    280 	char	*path;
    281 {
    282 
    283 	if (swapctl(SWAP_CTL, path, pri) < 0)
    284 		warn("%s", path);
    285 }
    286 
    287 /*
    288  * add_swap:  add the pathname to the list of swap devices.
    289  */
    290 void
    291 add_swap(path)
    292 	char *path;
    293 {
    294 
    295 	if (swapctl(SWAP_ON, path, pri) < 0)
    296 		err(1, "%s", path);
    297 }
    298 
    299 /*
    300  * del_swap:  remove the pathname to the list of swap devices.
    301  */
    302 void
    303 del_swap(path)
    304 	char *path;
    305 {
    306 
    307 	if (swapctl(SWAP_OFF, path, pri) < 0)
    308 		err(1, "%s", path);
    309 }
    310 
    311 void
    312 do_fstab()
    313 {
    314 	struct	fstab *fp;
    315 	char	*s;
    316 	long	priority;
    317 
    318 #define PRIORITYEQ	"priority="
    319 #define NFSMNTPT	"nfsmntpt="
    320 #define PATH_MOUNT	"/sbin/mount_nfs"
    321 	while (fp = getfsent()) {
    322 		char *spec;
    323 
    324 		if (strcmp(fp->fs_type, "sw") != 0)
    325 			continue;
    326 
    327 		spec = fp->fs_spec;
    328 
    329 		if (s = strstr(fp->fs_mntops, PRIORITYEQ)) {
    330 			s += sizeof(PRIORITYEQ) - 1;
    331 			priority = atol(s);
    332 		} else
    333 			priority = pri;
    334 
    335 		if (s = strstr(fp->fs_mntops, NFSMNTPT)) {
    336 			char *t, cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
    337 
    338 			t = strpbrk(s, ",");
    339 			if (t != 0)
    340 				*t = '\0';
    341 			spec = strdup(s + strlen(NFSMNTPT));
    342 			if (t != 0)
    343 				*t = ',';
    344 
    345 			if (spec == NULL)
    346 				errx(1, "Out of memory");
    347 
    348 			if (strlen(spec) == 0) {
    349 				warnx("empty mountpoint");
    350 				free(spec);
    351 				continue;
    352 			}
    353 			snprintf(cmd, sizeof(cmd), "%s %s %s",
    354 				PATH_MOUNT, fp->fs_spec, spec);
    355 			if (system(cmd) != 0) {
    356 				warnx("%s: mount failed", fp->fs_spec);
    357 				continue;
    358 			}
    359 		}
    360 
    361 		if (swapctl(SWAP_ON, spec, (int)priority) < 0)
    362 			warn("%s", spec);
    363 		else
    364 			printf("%s: adding %s as swap device at priority %d\n",
    365 			    __progname, fp->fs_spec, priority);
    366 
    367 		if (spec != fp->fs_spec)
    368 			free(spec);
    369 	}
    370 }
    371 
    372 void
    373 usage()
    374 {
    375 
    376 	fprintf(stderr, "usage: %s -A [-p priority]\n", __progname);
    377 	fprintf(stderr, "       %s -a [-p priority] path\n", __progname);
    378 	fprintf(stderr, "       %s -c -p priority path\n", __progname);
    379 	fprintf(stderr, "       %s -d path\n", __progname);
    380 	fprintf(stderr, "       %s -l | -s [-k]\n", __progname);
    381 	exit(1);
    382 }
    383