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