Home | History | Annotate | Line # | Download | only in swapctl
swapctl.c revision 1.3
      1 /*	$NetBSD: swapctl.c,v 1.3 1997/06/24 05:22:38 mikel 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 <err.h>
     58 #include <errno.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <fstab.h>
     63 
     64 #include "swapctl.h"
     65 
     66 int	command;
     67 
     68 /*
     69  * Commands for swapctl(8).  These are mutually exclusive.
     70  */
     71 #define	CMD_A		0x01	/* process /etc/fstab */
     72 #define	CMD_a		0x02	/* add a swap file/device */
     73 #define	CMD_c		0x04	/* change priority of a swap file/device */
     74 #define	CMD_d		0x08	/* delete a swap file/device */
     75 #define	CMD_l		0x10	/* list swap files/devices */
     76 #define	CMD_s		0x20	/* summary of swap files/devices */
     77 
     78 #define	SET_COMMAND(cmd) \
     79 do { \
     80 	if (command) \
     81 		usage(); \
     82 	command = (cmd); \
     83 } while (0)
     84 
     85 /*
     86  * Commands that require a "path" argument at the end of the command
     87  * line, and the ones which require that none exist.
     88  */
     89 #define	REQUIRE_PATH	(CMD_a | CMD_c | CMD_d)
     90 #define	REQUIRE_NOPATH	(CMD_A | CMD_l | CMD_s)
     91 
     92 /*
     93  * Option flags, and the commands with which they are valid.
     94  */
     95 int	kflag;		/* display in 1K blocks */
     96 #define	KFLAG_CMDS	(CMD_l | CMD_s)
     97 
     98 int	pflag;		/* priority was specified */
     99 #define	PFLAG_CMDS	(CMD_A | CMD_a | CMD_c)
    100 
    101 int	pri;		/* uses 0 as default pri */
    102 
    103 static	void change_priority __P((char *));
    104 static	void add_swap __P((char *));
    105 static	void del_swap __P((char *));
    106 static	void do_fstab __P((void));
    107 static	void usage __P((void));
    108 static	void swapon_command __P((int, char **));
    109 #if 0
    110 static	void swapoff_command __P((int, char **));
    111 #endif
    112 
    113 extern	char *__progname;	/* from crt0.o */
    114 
    115 int
    116 main(argc, argv)
    117 	int	argc;
    118 	char	*argv[];
    119 {
    120 	int	c;
    121 
    122 	if (strcmp(__progname, "swapon") == 0) {
    123 		swapon_command(argc, argv);
    124 		/* NOTREACHED */
    125 	}
    126 
    127 #if 0
    128 	if (strcmp(__progname, "swapoff") == 0) {
    129 		swapoff_command(argc, argv);
    130 		/* NOTREACHED */
    131 	}
    132 #endif
    133 
    134 	while ((c = getopt(argc, argv, "Aacdlkp:s")) != -1) {
    135 		switch (c) {
    136 		case 'A':
    137 			SET_COMMAND(CMD_A);
    138 			break;
    139 
    140 		case 'a':
    141 			SET_COMMAND(CMD_a);
    142 			break;
    143 
    144 		case 'c':
    145 			SET_COMMAND(CMD_c);
    146 			break;
    147 
    148 		case 'd':
    149 			SET_COMMAND(CMD_d);
    150 			break;
    151 
    152 		case 'l':
    153 			SET_COMMAND(CMD_l);
    154 			break;
    155 
    156 		case 'k':
    157 			kflag = 1;
    158 			break;
    159 
    160 		case 'p':
    161 			pflag = 1;
    162 			/* XXX strtol() */
    163 			pri = atoi(optarg);
    164 			break;
    165 
    166 		case 's':
    167 			SET_COMMAND(CMD_s);
    168 			break;
    169 
    170 		default:
    171 			usage();
    172 			/* NOTREACHED */
    173 		}
    174 	}
    175 
    176 	/* Did the user specify a command? */
    177 	if (command == 0)
    178 		usage();
    179 
    180 	argv += optind;
    181 	argc -= optind;
    182 
    183 	switch (argc) {
    184 	case 0:
    185 		if (command & REQUIRE_PATH)
    186 			usage();
    187 		break;
    188 
    189 	case 1:
    190 		if (command & REQUIRE_NOPATH)
    191 			usage();
    192 		break;
    193 
    194 	default:
    195 		usage();
    196 	}
    197 
    198 	/* To change priority, you have to specify one. */
    199 	if ((command == CMD_c) && pflag == 0)
    200 		usage();
    201 
    202 	/* Dispatch the command. */
    203 	switch (command) {
    204 	case CMD_l:
    205 		list_swap(pri, kflag, pflag, 0, 1);
    206 		break;
    207 
    208 	case CMD_s:
    209 		list_swap(pri, kflag, pflag, 0, 0);
    210 		break;
    211 
    212 	case CMD_c:
    213 		change_priority(argv[0]);
    214 		break;
    215 
    216 	case CMD_a:
    217 		add_swap(argv[0]);
    218 		break;
    219 
    220 	case CMD_d:
    221 		del_swap(argv[0]);
    222 		break;
    223 
    224 	case CMD_A:
    225 		do_fstab();
    226 		break;
    227 	}
    228 
    229 	exit(0);
    230 }
    231 
    232 /*
    233  * swapon_command: emulate the old swapon(8) program.
    234  */
    235 void
    236 swapon_command(argc, argv)
    237 	int argc;
    238 	char **argv;
    239 {
    240 	int ch, fiztab = 0;
    241 
    242 	while ((ch = getopt(argc, argv, "a")) != -1) {
    243 		switch (ch) {
    244 		case 'a':
    245 			fiztab = 1;
    246 			break;
    247 		default:
    248 			goto swapon_usage;
    249 		}
    250 	}
    251 	argc -= optind;
    252 	argv += optind;
    253 
    254 	if (fiztab) {
    255 		if (argc)
    256 			goto swapon_usage;
    257 		do_fstab();
    258 		exit(0);
    259 	} else if (argc == 0)
    260 		goto swapon_usage;
    261 
    262 	while (argc) {
    263 		add_swap(argv[0]);
    264 		argc--;
    265 		argv++;
    266 	}
    267 	exit(0);
    268 	/* NOTREACHED */
    269 
    270  swapon_usage:
    271 	fprintf(stderr, "usage: %s -a\n", __progname);
    272 	fprintf(stderr, "       %s <path> ...\n", __progname);
    273 	exit(1);
    274 }
    275 
    276 /*
    277  * change_priority:  change the priority of a swap device.
    278  */
    279 void
    280 change_priority(path)
    281 	char	*path;
    282 {
    283 
    284 	if (swapctl(SWAP_CTL, path, pri) < 0)
    285 		warn("%s", path);
    286 }
    287 
    288 /*
    289  * add_swap:  add the pathname to the list of swap devices.
    290  */
    291 void
    292 add_swap(path)
    293 	char *path;
    294 {
    295 
    296 	if (swapctl(SWAP_ON, path, pri) < 0)
    297 		err(1, "%s", path);
    298 }
    299 
    300 /*
    301  * del_swap:  remove the pathname to the list of swap devices.
    302  */
    303 void
    304 del_swap(path)
    305 	char *path;
    306 {
    307 
    308 	if (swapctl(SWAP_OFF, path, pri) < 0)
    309 		err(1, "%s", path);
    310 }
    311 
    312 void
    313 do_fstab()
    314 {
    315 	struct	fstab *fp;
    316 	char	*s;
    317 	long	priority;
    318 
    319 #define PRIORITYEQ	"priority="
    320 #define NFSMNTPT	"nfsmntpt="
    321 #define PATH_MOUNT	"/sbin/mount_nfs"
    322 	while ((fp = getfsent())) {
    323 		char *spec;
    324 
    325 		if (strcmp(fp->fs_type, "sw") != 0)
    326 			continue;
    327 
    328 		spec = fp->fs_spec;
    329 
    330 		if ((s = strstr(fp->fs_mntops, PRIORITYEQ))) {
    331 			s += sizeof(PRIORITYEQ) - 1;
    332 			priority = atol(s);
    333 		} else
    334 			priority = pri;
    335 
    336 		if ((s = strstr(fp->fs_mntops, NFSMNTPT))) {
    337 			char *t, cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
    338 
    339 			t = strpbrk(s, ",");
    340 			if (t != 0)
    341 				*t = '\0';
    342 			spec = strdup(s + strlen(NFSMNTPT));
    343 			if (t != 0)
    344 				*t = ',';
    345 
    346 			if (spec == NULL)
    347 				errx(1, "Out of memory");
    348 
    349 			if (strlen(spec) == 0) {
    350 				warnx("empty mountpoint");
    351 				free(spec);
    352 				continue;
    353 			}
    354 			snprintf(cmd, sizeof(cmd), "%s %s %s",
    355 				PATH_MOUNT, fp->fs_spec, spec);
    356 			if (system(cmd) != 0) {
    357 				warnx("%s: mount failed", fp->fs_spec);
    358 				continue;
    359 			}
    360 		}
    361 
    362 		if (swapctl(SWAP_ON, spec, (int)priority) < 0)
    363 			warn("%s", spec);
    364 		else
    365 			printf("%s: adding %s as swap device at priority %d\n",
    366 			    __progname, fp->fs_spec, (int)priority);
    367 
    368 		if (spec != fp->fs_spec)
    369 			free(spec);
    370 	}
    371 }
    372 
    373 void
    374 usage()
    375 {
    376 
    377 	fprintf(stderr, "usage: %s -A [-p priority]\n", __progname);
    378 	fprintf(stderr, "       %s -a [-p priority] path\n", __progname);
    379 	fprintf(stderr, "       %s -c -p priority path\n", __progname);
    380 	fprintf(stderr, "       %s -d path\n", __progname);
    381 	fprintf(stderr, "       %s -l | -s [-k]\n", __progname);
    382 	exit(1);
    383 }
    384