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