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