swapctl.c revision 1.10
1/* $OpenBSD: swapctl.c,v 1.10 2002/07/03 22:32:33 deraadt 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(char *); 109static void add_swap(char *); 110static void del_swap(char *); 111 int main(int, char *[]); 112static void do_fstab(void); 113static void usage(void); 114static int swapon_command(int, char **); 115 116extern char *__progname; /* from crt0.o */ 117 118int 119main(int argc, char *argv[]) 120{ 121 int c; 122 123 if (strcmp(__progname, "swapon") == 0) 124 return swapon_command(argc, argv); 125 126 while ((c = getopt(argc, argv, "Aacdlkp:st:")) != -1) { 127 switch (c) { 128 case 'A': 129 SET_COMMAND(CMD_A); 130 break; 131 132 case 'a': 133 SET_COMMAND(CMD_a); 134 break; 135 136 case 'c': 137 SET_COMMAND(CMD_c); 138 break; 139 140 case 'd': 141 SET_COMMAND(CMD_d); 142 break; 143 144 case 'l': 145 SET_COMMAND(CMD_l); 146 break; 147 148 case 'k': 149 kflag = 1; 150 break; 151 152 case 'p': 153 pflag = 1; 154 /* XXX strtol() */ 155 pri = atoi(optarg); 156 break; 157 158 case 's': 159 SET_COMMAND(CMD_s); 160 break; 161 162 case 't': 163 if (tflag != NULL) 164 usage(); 165 tflag = optarg; 166 break; 167 168 default: 169 usage(); 170 /* NOTREACHED */ 171 } 172 } 173 174 /* Did the user specify a command? */ 175 if (command == 0) 176 usage(); 177 178 argv += optind; 179 argc -= optind; 180 181 switch (argc) { 182 case 0: 183 if (command & REQUIRE_PATH) 184 usage(); 185 break; 186 187 case 1: 188 if (command & REQUIRE_NOPATH) 189 usage(); 190 break; 191 192 default: 193 usage(); 194 } 195 196 /* To change priority, you have to specify one. */ 197 if ((command == CMD_c) && pflag == 0) 198 usage(); 199 200 /* Sanity-check -t */ 201 if (tflag != NULL) { 202 if (command != CMD_A) 203 usage(); 204 if (strcmp(tflag, "blk") != 0 && 205 strcmp(tflag, "noblk") != 0) 206 usage(); 207 } 208 209 /* Dispatch the command. */ 210 switch (command) { 211 case CMD_l: 212 list_swap(pri, kflag, pflag, 0, 1); 213 break; 214 215 case CMD_s: 216 list_swap(pri, kflag, pflag, 0, 0); 217 break; 218 219 case CMD_c: 220 change_priority(argv[0]); 221 break; 222 223 case CMD_a: 224 add_swap(argv[0]); 225 break; 226 227 case CMD_d: 228 del_swap(argv[0]); 229 break; 230 231 case CMD_A: 232 do_fstab(); 233 break; 234 } 235 236 return (0); 237} 238 239/* 240 * swapon_command: emulate the old swapon(8) program. 241 */ 242int 243swapon_command(int argc, char **argv) 244{ 245 int ch, fiztab = 0; 246 247 while ((ch = getopt(argc, argv, "at:")) != -1) { 248 switch (ch) { 249 case 'a': 250 fiztab = 1; 251 break; 252 case 't': 253 if (tflag != NULL) 254 usage(); 255 tflag = optarg; 256 break; 257 default: 258 goto swapon_usage; 259 } 260 } 261 argc -= optind; 262 argv += optind; 263 264 if (fiztab) { 265 if (argc) 266 goto swapon_usage; 267 /* Sanity-check -t */ 268 if (tflag != NULL) { 269 if (strcmp(tflag, "blk") != 0 && 270 strcmp(tflag, "noblk") != 0) 271 usage(); 272 } 273 do_fstab(); 274 return (0); 275 } else if (argc == 0 || tflag != NULL) 276 goto swapon_usage; 277 278 while (argc) { 279 add_swap(argv[0]); 280 argc--; 281 argv++; 282 } 283 return (0); 284 285 swapon_usage: 286 fprintf(stderr, "usage: %s -a [-t blk|noblk]\n" 287 " %s <path> ...\n", 288 __progname, __progname); 289 return (1); 290} 291 292/* 293 * change_priority: change the priority of a swap device. 294 */ 295void 296change_priority(char *path) 297{ 298 299 if (swapctl(SWAP_CTL, path, pri) < 0) 300 warn("%s", path); 301} 302 303/* 304 * add_swap: add the pathname to the list of swap devices. 305 */ 306void 307add_swap(char *path) 308{ 309 310 if (swapctl(SWAP_ON, path, pri) < 0) 311 if (errno != EBUSY) 312 err(1, "%s", path); 313} 314 315/* 316 * del_swap: remove the pathname from the list of swap devices. 317 */ 318void 319del_swap(char *path) 320{ 321 322 if (swapctl(SWAP_OFF, path, pri) < 0) 323 err(1, "%s", path); 324} 325 326void 327do_fstab(void) 328{ 329 struct fstab *fp; 330 char *s; 331 long priority; 332 struct stat st; 333 mode_t rejecttype; 334 int gotone = 0; 335 336 /* 337 * Select which mount point types to reject, depending on the 338 * value of the -t parameter. 339 */ 340 if (tflag != NULL) { 341 if (strcmp(tflag, "blk") == 0) 342 rejecttype = S_IFREG; 343 else if (strcmp(tflag, "noblk") == 0) 344 rejecttype = S_IFBLK; 345 } else 346 rejecttype = 0; 347 348#define PRIORITYEQ "priority=" 349#define NFSMNTPT "nfsmntpt=" 350#define PATH_MOUNT "/sbin/mount_nfs" 351 while ((fp = getfsent()) != NULL) { 352 const char *spec; 353 354 if (strcmp(fp->fs_type, "sw") != 0) 355 continue; 356 357 spec = fp->fs_spec; 358 359 if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) { 360 s += sizeof(PRIORITYEQ) - 1; 361 priority = atol(s); 362 } else 363 priority = pri; 364 365 if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) { 366 char *t, cmd[strlen(PATH_MOUNT)+1+PATH_MAX+1+PATH_MAX+1]; 367 368 /* 369 * Skip this song and dance if we're only 370 * doing block devices. 371 */ 372 if (rejecttype == S_IFREG) 373 continue; 374 375 t = strpbrk(s, ","); 376 if (t != 0) 377 *t = '\0'; 378 spec = strdup(s + strlen(NFSMNTPT)); 379 if (t != 0) 380 *t = ','; 381 382 if (spec == NULL) 383 errx(1, "Out of memory"); 384 385 if (strlen(spec) == 0) { 386 warnx("empty mountpoint"); 387 free((char *)spec); 388 continue; 389 } 390 snprintf(cmd, sizeof(cmd), "%s %s %s", 391 PATH_MOUNT, fp->fs_spec, spec); 392 if (system(cmd) != 0) { 393 warnx("%s: mount failed", fp->fs_spec); 394 continue; 395 } 396 } else { 397 /* 398 * Determine blk-ness. Don't even consider a 399 * mountpoint outside /dev as a block device. 400 */ 401 if (rejecttype == S_IFREG) { 402 if (strncmp("/dev/", spec, 5) != 0) 403 continue; 404 } 405 if (stat(spec, &st) < 0) { 406 warn("%s", spec); 407 continue; 408 } 409 if ((st.st_mode & S_IFMT) == rejecttype) 410 continue; 411 412 /* 413 * Do not allow fancy objects to be swap areas. 414 */ 415 if (!S_ISREG(st.st_mode) && 416 !S_ISBLK(st.st_mode)) 417 continue; 418 } 419 420 if (swapctl(SWAP_ON, spec, (int)priority) < 0) { 421 if (errno != EBUSY) 422 warn("%s", spec); 423 } else { 424 gotone = 1; 425 printf("%s: adding %s as swap device at priority %d\n", 426 __progname, fp->fs_spec, (int)priority); 427 } 428 429 if (spec != fp->fs_spec) 430 free((char *)spec); 431 } 432 if (gotone == 0) 433 exit(1); 434} 435 436void 437usage(void) 438{ 439 440 fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n", 441 __progname); 442 fprintf(stderr, " %s -a [-p priority] path\n", __progname); 443 fprintf(stderr, " %s -c -p priority path\n", __progname); 444 fprintf(stderr, " %s -d path\n", __progname); 445 fprintf(stderr, " %s -l | -s [-k]\n", __progname); 446 exit(1); 447} 448