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