1 1.43 kre /* $NetBSD: swapctl.c,v 1.43 2023/03/01 15:18:18 kre Exp $ */ 2 1.1 mrg 3 1.1 mrg /* 4 1.40 mrg * Copyright (c) 1996, 1997, 1999, 2015 Matthew R. Green 5 1.1 mrg * All rights reserved. 6 1.1 mrg * 7 1.1 mrg * Redistribution and use in source and binary forms, with or without 8 1.1 mrg * modification, are permitted provided that the following conditions 9 1.1 mrg * are met: 10 1.1 mrg * 1. Redistributions of source code must retain the above copyright 11 1.1 mrg * notice, this list of conditions and the following disclaimer. 12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 mrg * notice, this list of conditions and the following disclaimer in the 14 1.1 mrg * documentation and/or other materials provided with the distribution. 15 1.1 mrg * 16 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 mrg * SUCH DAMAGE. 27 1.1 mrg */ 28 1.1 mrg 29 1.1 mrg /* 30 1.1 mrg * swapctl command: 31 1.10 mrg * -A add all devices listed as `sw' in /etc/fstab (also 32 1.13 soren * (sets the dump device, if listed in fstab) 33 1.30 martin * -D [<dev>|none] set dumpdev to <dev> or disable dumps 34 1.16 mrg * -z show dumpdev 35 1.12 lukem * -U remove all devices listed as `sw' in /etc/fstab. 36 1.32 martin * -t [blk|noblk|auto] 37 1.32 martin * if -A or -U , add (remove) either all block device 38 1.32 martin * or all non-block devices, or all swap partitions 39 1.32 martin * -q check if any swap or dump devices are defined in 40 1.32 martin * /etc/fstab 41 1.1 mrg * -a <dev> add this device 42 1.17 lukem * -d <dev> remove this swap device 43 1.32 martin * -f with -A -t auto, use the first swap as dump device 44 1.25 mrg * -g use gigabytes 45 1.24 mrg * -h use humanize_number(3) for listing 46 1.1 mrg * -l list swap devices 47 1.25 mrg * -m use megabytes 48 1.32 martin * -n print actions, but do not add/remove swap or 49 1.32 martin * with -A/-U 50 1.32 martin * -o with -A -t auto only configure the first swap as dump, 51 1.32 martin * (similar to -f), but do not add any further swap devs 52 1.1 mrg * -s short listing of swap devices 53 1.1 mrg * -k use kilobytes 54 1.1 mrg * -p <pri> use this priority 55 1.1 mrg * -c change priority 56 1.2 thorpej * 57 1.2 thorpej * or, if invoked as "swapon" (compatibility mode): 58 1.2 thorpej * 59 1.2 thorpej * -a all devices listed as `sw' in /etc/fstab 60 1.4 thorpej * -t same as -t above (feature not present in old 61 1.4 thorpej * swapon(8) command) 62 1.2 thorpej * <dev> add this device 63 1.1 mrg */ 64 1.22 agc #include <sys/cdefs.h> 65 1.22 agc 66 1.22 agc #ifndef lint 67 1.43 kre __RCSID("$NetBSD: swapctl.c,v 1.43 2023/03/01 15:18:18 kre Exp $"); 68 1.22 agc #endif 69 1.22 agc 70 1.1 mrg 71 1.1 mrg #include <sys/param.h> 72 1.39 dsl #include <sys/ioctl.h> 73 1.4 thorpej #include <sys/stat.h> 74 1.14 mrg #include <sys/swap.h> 75 1.32 martin #include <sys/sysctl.h> 76 1.32 martin #include <sys/disk.h> 77 1.32 martin #include <sys/disklabel.h> 78 1.1 mrg 79 1.1 mrg #include <unistd.h> 80 1.3 mikel #include <err.h> 81 1.1 mrg #include <errno.h> 82 1.1 mrg #include <stdio.h> 83 1.1 mrg #include <stdlib.h> 84 1.1 mrg #include <string.h> 85 1.1 mrg #include <fstab.h> 86 1.32 martin #include <fcntl.h> 87 1.32 martin #include <util.h> 88 1.32 martin #include <paths.h> 89 1.1 mrg 90 1.1 mrg #include "swapctl.h" 91 1.1 mrg 92 1.36 joerg static int command; 93 1.2 thorpej 94 1.2 thorpej /* 95 1.2 thorpej * Commands for swapctl(8). These are mutually exclusive. 96 1.2 thorpej */ 97 1.12 lukem #define CMD_A 0x01 /* process /etc/fstab for adding */ 98 1.10 mrg #define CMD_D 0x02 /* set dumpdev */ 99 1.12 lukem #define CMD_U 0x04 /* process /etc/fstab for removing */ 100 1.12 lukem #define CMD_a 0x08 /* add a swap file/device */ 101 1.12 lukem #define CMD_c 0x10 /* change priority of a swap file/device */ 102 1.12 lukem #define CMD_d 0x20 /* delete a swap file/device */ 103 1.12 lukem #define CMD_l 0x40 /* list swap files/devices */ 104 1.12 lukem #define CMD_s 0x80 /* summary of swap files/devices */ 105 1.16 mrg #define CMD_z 0x100 /* show dump device */ 106 1.32 martin #define CMD_q 0x200 /* check for dump/swap in /etc/fstab */ 107 1.2 thorpej 108 1.2 thorpej #define SET_COMMAND(cmd) \ 109 1.2 thorpej do { \ 110 1.2 thorpej if (command) \ 111 1.2 thorpej usage(); \ 112 1.2 thorpej command = (cmd); \ 113 1.2 thorpej } while (0) 114 1.2 thorpej 115 1.2 thorpej /* 116 1.2 thorpej * Commands that require a "path" argument at the end of the command 117 1.2 thorpej * line, and the ones which require that none exist. 118 1.2 thorpej */ 119 1.10 mrg #define REQUIRE_PATH (CMD_D | CMD_a | CMD_c | CMD_d) 120 1.32 martin #define REQUIRE_NOPATH (CMD_A | CMD_U | CMD_l | CMD_s | CMD_z | CMD_q) 121 1.2 thorpej 122 1.2 thorpej /* 123 1.2 thorpej * Option flags, and the commands with which they are valid. 124 1.2 thorpej */ 125 1.36 joerg static int kflag; /* display in 1K^x blocks */ 126 1.2 thorpej #define KFLAG_CMDS (CMD_l | CMD_s) 127 1.25 mrg #define MFLAG_CMDS (CMD_l | CMD_s) 128 1.25 mrg #define GFLAG_CMDS (CMD_l | CMD_s) 129 1.2 thorpej 130 1.36 joerg static int hflag; /* display with humanize_number */ 131 1.24 mrg #define HFLAG_CMDS (CMD_l | CMD_s) 132 1.24 mrg 133 1.36 joerg static int pflag; /* priority was specified */ 134 1.2 thorpej #define PFLAG_CMDS (CMD_A | CMD_a | CMD_c) 135 1.2 thorpej 136 1.36 joerg static char *tflag; /* swap device type (blk, noblk, auto) */ 137 1.36 joerg static int autoflag; /* 1, if tflag is "auto" */ 138 1.16 mrg #define TFLAG_CMDS (CMD_A | CMD_U) 139 1.4 thorpej 140 1.36 joerg static int fflag; /* first swap becomes dump */ 141 1.32 martin #define FFLAG_CMDS (CMD_A) 142 1.32 martin 143 1.36 joerg static int oflag; /* only autoset dump device */ 144 1.32 martin #define OFLAG_CMDS (CMD_A) 145 1.32 martin 146 1.36 joerg static int nflag; /* no execute, just print actions */ 147 1.32 martin #define NFLAG_CMDS (CMD_A | CMD_U) 148 1.32 martin 149 1.36 joerg static int pri; /* uses 0 as default pri */ 150 1.1 mrg 151 1.29 christos static void change_priority(char *); 152 1.29 christos static int add_swap(char *, int); 153 1.29 christos static int delete_swap(char *); 154 1.40 mrg static int set_dumpdev1(char *); 155 1.29 christos static void set_dumpdev(char *); 156 1.30 martin static int get_dumpdev(void); 157 1.36 joerg __dead static void do_fstab(int); 158 1.32 martin static int check_fstab(void); 159 1.32 martin static void do_localdevs(int); 160 1.32 martin static void do_localdisk(const char *, int); 161 1.32 martin static int do_wedgesofdisk(int fd, int); 162 1.32 martin static int do_partitionsofdisk(const char *, int fd, int); 163 1.36 joerg __dead static void usage(void); 164 1.36 joerg __dead static void swapon_command(int, char **); 165 1.2 thorpej #if 0 166 1.28 xtraeme static void swapoff_command(int, char **); 167 1.2 thorpej #endif 168 1.2 thorpej 169 1.1 mrg int 170 1.28 xtraeme main(int argc, char *argv[]) 171 1.1 mrg { 172 1.1 mrg int c; 173 1.2 thorpej 174 1.18 cgd if (strcmp(getprogname(), "swapon") == 0) { 175 1.2 thorpej swapon_command(argc, argv); 176 1.2 thorpej /* NOTREACHED */ 177 1.2 thorpej } 178 1.2 thorpej 179 1.2 thorpej #if 0 180 1.18 cgd if (strcmp(getprogname(), "swapoff") == 0) { 181 1.2 thorpej swapoff_command(argc, argv); 182 1.2 thorpej /* NOTREACHED */ 183 1.2 thorpej } 184 1.2 thorpej #endif 185 1.2 thorpej 186 1.32 martin while ((c = getopt(argc, argv, "ADUacdfghklmnop:qst:z")) != -1) { 187 1.2 thorpej switch (c) { 188 1.1 mrg case 'A': 189 1.2 thorpej SET_COMMAND(CMD_A); 190 1.1 mrg break; 191 1.2 thorpej 192 1.10 mrg case 'D': 193 1.10 mrg SET_COMMAND(CMD_D); 194 1.10 mrg break; 195 1.10 mrg 196 1.12 lukem case 'U': 197 1.12 lukem SET_COMMAND(CMD_U); 198 1.12 lukem break; 199 1.12 lukem 200 1.1 mrg case 'a': 201 1.2 thorpej SET_COMMAND(CMD_a); 202 1.1 mrg break; 203 1.2 thorpej 204 1.1 mrg case 'c': 205 1.2 thorpej SET_COMMAND(CMD_c); 206 1.1 mrg break; 207 1.2 thorpej 208 1.1 mrg case 'd': 209 1.2 thorpej SET_COMMAND(CMD_d); 210 1.1 mrg break; 211 1.2 thorpej 212 1.32 martin case 'f': 213 1.32 martin fflag = 1; 214 1.32 martin break; 215 1.32 martin 216 1.25 mrg case 'g': 217 1.25 mrg kflag = 3; /* 1k ^ 3 */ 218 1.25 mrg break; 219 1.25 mrg 220 1.24 mrg case 'h': 221 1.24 mrg hflag = 1; 222 1.24 mrg break; 223 1.24 mrg 224 1.25 mrg case 'k': 225 1.25 mrg kflag = 1; 226 1.25 mrg break; 227 1.25 mrg 228 1.1 mrg case 'l': 229 1.2 thorpej SET_COMMAND(CMD_l); 230 1.1 mrg break; 231 1.2 thorpej 232 1.25 mrg case 'm': 233 1.25 mrg kflag = 2; /* 1k ^ 2 */ 234 1.1 mrg break; 235 1.2 thorpej 236 1.32 martin case 'n': 237 1.32 martin nflag = 1; 238 1.32 martin break; 239 1.32 martin 240 1.32 martin case 'o': 241 1.32 martin oflag = 1; 242 1.32 martin break; 243 1.32 martin 244 1.1 mrg case 'p': 245 1.1 mrg pflag = 1; 246 1.2 thorpej /* XXX strtol() */ 247 1.1 mrg pri = atoi(optarg); 248 1.1 mrg break; 249 1.2 thorpej 250 1.32 martin case 'q': 251 1.32 martin SET_COMMAND(CMD_q); 252 1.32 martin break; 253 1.32 martin 254 1.1 mrg case 's': 255 1.2 thorpej SET_COMMAND(CMD_s); 256 1.1 mrg break; 257 1.2 thorpej 258 1.4 thorpej case 't': 259 1.4 thorpej if (tflag != NULL) 260 1.4 thorpej usage(); 261 1.4 thorpej tflag = optarg; 262 1.32 martin if (strcmp(tflag, "auto") == 0) 263 1.32 martin autoflag = 1; 264 1.4 thorpej break; 265 1.4 thorpej 266 1.16 mrg case 'z': 267 1.16 mrg SET_COMMAND(CMD_z); 268 1.16 mrg break; 269 1.16 mrg 270 1.2 thorpej default: 271 1.2 thorpej usage(); 272 1.2 thorpej /* NOTREACHED */ 273 1.1 mrg } 274 1.1 mrg } 275 1.2 thorpej 276 1.2 thorpej /* Did the user specify a command? */ 277 1.2 thorpej if (command == 0) 278 1.1 mrg usage(); 279 1.1 mrg 280 1.1 mrg argv += optind; 281 1.2 thorpej argc -= optind; 282 1.2 thorpej 283 1.2 thorpej switch (argc) { 284 1.2 thorpej case 0: 285 1.2 thorpej if (command & REQUIRE_PATH) 286 1.2 thorpej usage(); 287 1.2 thorpej break; 288 1.2 thorpej 289 1.2 thorpej case 1: 290 1.2 thorpej if (command & REQUIRE_NOPATH) 291 1.2 thorpej usage(); 292 1.2 thorpej break; 293 1.2 thorpej 294 1.2 thorpej default: 295 1.1 mrg usage(); 296 1.2 thorpej } 297 1.2 thorpej 298 1.2 thorpej /* To change priority, you have to specify one. */ 299 1.2 thorpej if ((command == CMD_c) && pflag == 0) 300 1.1 mrg usage(); 301 1.1 mrg 302 1.41 andvar /* -f and -o are mutually exclusive */ 303 1.32 martin if (fflag && oflag) 304 1.32 martin usage(); 305 1.32 martin 306 1.4 thorpej /* Sanity-check -t */ 307 1.4 thorpej if (tflag != NULL) { 308 1.12 lukem if (command != CMD_A && command != CMD_U) 309 1.4 thorpej usage(); 310 1.4 thorpej if (strcmp(tflag, "blk") != 0 && 311 1.32 martin strcmp(tflag, "noblk") != 0 && 312 1.32 martin strcmp(tflag, "auto") != 0) 313 1.4 thorpej usage(); 314 1.4 thorpej } 315 1.4 thorpej 316 1.2 thorpej /* Dispatch the command. */ 317 1.2 thorpej switch (command) { 318 1.2 thorpej case CMD_l: 319 1.30 martin if (!list_swap(pri, kflag, pflag, 0, 1, hflag)) 320 1.30 martin exit(1); 321 1.2 thorpej break; 322 1.2 thorpej 323 1.2 thorpej case CMD_s: 324 1.24 mrg list_swap(pri, kflag, pflag, 0, 0, hflag); 325 1.2 thorpej break; 326 1.2 thorpej 327 1.2 thorpej case CMD_c: 328 1.1 mrg change_priority(argv[0]); 329 1.2 thorpej break; 330 1.2 thorpej 331 1.2 thorpej case CMD_a: 332 1.12 lukem if (! add_swap(argv[0], pri)) 333 1.12 lukem exit(1); 334 1.2 thorpej break; 335 1.2 thorpej 336 1.2 thorpej case CMD_d: 337 1.12 lukem if (! delete_swap(argv[0])) 338 1.12 lukem exit(1); 339 1.2 thorpej break; 340 1.2 thorpej 341 1.2 thorpej case CMD_A: 342 1.32 martin if (autoflag) 343 1.32 martin do_localdevs(1); 344 1.32 martin else 345 1.32 martin do_fstab(1); 346 1.2 thorpej break; 347 1.10 mrg 348 1.10 mrg case CMD_D: 349 1.10 mrg set_dumpdev(argv[0]); 350 1.10 mrg break; 351 1.12 lukem 352 1.16 mrg case CMD_z: 353 1.30 martin if (!get_dumpdev()) 354 1.30 martin exit(1); 355 1.16 mrg break; 356 1.16 mrg 357 1.12 lukem case CMD_U: 358 1.32 martin if (autoflag) 359 1.32 martin do_localdevs(0); 360 1.32 martin else 361 1.32 martin do_fstab(0); 362 1.12 lukem break; 363 1.32 martin case CMD_q: 364 1.32 martin if (check_fstab()) { 365 1.32 martin printf("%s: there are swap or dump devices defined in " 366 1.32 martin _PATH_FSTAB "\n", getprogname()); 367 1.32 martin exit(0); 368 1.32 martin } else { 369 1.32 martin printf("%s: no swap or dump devices in " 370 1.32 martin _PATH_FSTAB "\n", getprogname()); 371 1.32 martin exit(1); 372 1.32 martin } 373 1.2 thorpej } 374 1.2 thorpej 375 1.2 thorpej exit(0); 376 1.2 thorpej } 377 1.2 thorpej 378 1.2 thorpej /* 379 1.2 thorpej * swapon_command: emulate the old swapon(8) program. 380 1.2 thorpej */ 381 1.12 lukem static void 382 1.28 xtraeme swapon_command(int argc, char **argv) 383 1.2 thorpej { 384 1.2 thorpej int ch, fiztab = 0; 385 1.2 thorpej 386 1.4 thorpej while ((ch = getopt(argc, argv, "at:")) != -1) { 387 1.2 thorpej switch (ch) { 388 1.2 thorpej case 'a': 389 1.2 thorpej fiztab = 1; 390 1.2 thorpej break; 391 1.4 thorpej case 't': 392 1.4 thorpej if (tflag != NULL) 393 1.4 thorpej usage(); 394 1.4 thorpej tflag = optarg; 395 1.4 thorpej break; 396 1.2 thorpej default: 397 1.2 thorpej goto swapon_usage; 398 1.2 thorpej } 399 1.2 thorpej } 400 1.2 thorpej argc -= optind; 401 1.2 thorpej argv += optind; 402 1.2 thorpej 403 1.2 thorpej if (fiztab) { 404 1.2 thorpej if (argc) 405 1.2 thorpej goto swapon_usage; 406 1.4 thorpej /* Sanity-check -t */ 407 1.4 thorpej if (tflag != NULL) { 408 1.4 thorpej if (strcmp(tflag, "blk") != 0 && 409 1.4 thorpej strcmp(tflag, "noblk") != 0) 410 1.4 thorpej usage(); 411 1.4 thorpej } 412 1.12 lukem do_fstab(1); 413 1.2 thorpej exit(0); 414 1.4 thorpej } else if (argc == 0 || tflag != NULL) 415 1.2 thorpej goto swapon_usage; 416 1.2 thorpej 417 1.2 thorpej while (argc) { 418 1.12 lukem if (! add_swap(argv[0], pri)) 419 1.12 lukem exit(1); 420 1.2 thorpej argc--; 421 1.2 thorpej argv++; 422 1.2 thorpej } 423 1.1 mrg exit(0); 424 1.2 thorpej /* NOTREACHED */ 425 1.2 thorpej 426 1.2 thorpej swapon_usage: 427 1.18 cgd fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname()); 428 1.18 cgd fprintf(stderr, " %s <path> ...\n", getprogname()); 429 1.2 thorpej exit(1); 430 1.1 mrg } 431 1.1 mrg 432 1.1 mrg /* 433 1.1 mrg * change_priority: change the priority of a swap device. 434 1.1 mrg */ 435 1.12 lukem static void 436 1.29 christos change_priority(char *path) 437 1.1 mrg { 438 1.1 mrg 439 1.1 mrg if (swapctl(SWAP_CTL, path, pri) < 0) 440 1.30 martin err(1, "%s", path); 441 1.1 mrg } 442 1.1 mrg 443 1.1 mrg /* 444 1.1 mrg * add_swap: add the pathname to the list of swap devices. 445 1.1 mrg */ 446 1.12 lukem static int 447 1.29 christos add_swap(char *path, int priority) 448 1.1 mrg { 449 1.10 mrg struct stat sb; 450 1.38 mlelstv char buf[MAXPATHLEN]; 451 1.38 mlelstv char *spec; 452 1.10 mrg 453 1.38 mlelstv if (getfsspecname(buf, sizeof(buf), path) == NULL) 454 1.38 mlelstv goto oops; 455 1.38 mlelstv spec = buf; 456 1.38 mlelstv 457 1.38 mlelstv if (stat(spec, &sb) < 0) 458 1.10 mrg goto oops; 459 1.10 mrg 460 1.10 mrg if (sb.st_mode & S_IROTH) 461 1.19 pooka warnx("WARNING: %s is readable by the world", path); 462 1.10 mrg if (sb.st_mode & S_IWOTH) 463 1.19 pooka warnx("WARNING: %s is writable by the world", path); 464 1.1 mrg 465 1.32 martin if (fflag || oflag) { 466 1.38 mlelstv set_dumpdev1(spec); 467 1.32 martin if (oflag) 468 1.32 martin exit(0); 469 1.32 martin else 470 1.32 martin fflag = 0; 471 1.32 martin } 472 1.32 martin 473 1.32 martin if (nflag) 474 1.32 martin return 1; 475 1.32 martin 476 1.38 mlelstv if (swapctl(SWAP_ON, spec, priority) < 0) { 477 1.10 mrg oops: 478 1.40 mrg warn("%s", path); 479 1.40 mrg return 0; 480 1.12 lukem } 481 1.40 mrg return 1; 482 1.1 mrg } 483 1.1 mrg 484 1.1 mrg /* 485 1.11 mrg * delete_swap: remove the pathname to the list of swap devices. 486 1.1 mrg */ 487 1.12 lukem static int 488 1.29 christos delete_swap(char *path) 489 1.1 mrg { 490 1.38 mlelstv char buf[MAXPATHLEN]; 491 1.38 mlelstv char *spec; 492 1.38 mlelstv 493 1.40 mrg if (getfsspecname(buf, sizeof(buf), path) == NULL) { 494 1.40 mrg warn("%s", path); 495 1.40 mrg return 0; 496 1.40 mrg } 497 1.38 mlelstv spec = buf; 498 1.1 mrg 499 1.32 martin if (nflag) 500 1.32 martin return 1; 501 1.32 martin 502 1.40 mrg if (swapctl(SWAP_OFF, spec, pri) < 0) { 503 1.40 mrg warn("%s", path); 504 1.40 mrg return 0; 505 1.40 mrg } 506 1.40 mrg return 1; 507 1.1 mrg } 508 1.1 mrg 509 1.40 mrg static int 510 1.38 mlelstv set_dumpdev1(char *spec) 511 1.10 mrg { 512 1.32 martin int rv = 0; 513 1.10 mrg 514 1.32 martin if (!nflag) { 515 1.38 mlelstv if (strcmp(spec, "none") == 0) 516 1.32 martin rv = swapctl(SWAP_DUMPOFF, NULL, 0); 517 1.32 martin else 518 1.38 mlelstv rv = swapctl(SWAP_DUMPDEV, spec, 0); 519 1.32 martin } 520 1.30 martin 521 1.30 martin if (rv == -1) 522 1.40 mrg warn("could not set dump device to %s", spec); 523 1.10 mrg else 524 1.38 mlelstv printf("%s: setting dump device to %s\n", getprogname(), spec); 525 1.40 mrg 526 1.40 mrg return rv == -1 ? 0 : 1; 527 1.38 mlelstv } 528 1.38 mlelstv 529 1.38 mlelstv static void 530 1.38 mlelstv set_dumpdev(char *path) 531 1.38 mlelstv { 532 1.38 mlelstv char buf[MAXPATHLEN]; 533 1.38 mlelstv char *spec; 534 1.38 mlelstv 535 1.38 mlelstv if (getfsspecname(buf, sizeof(buf), path) == NULL) 536 1.38 mlelstv err(1, "%s", path); 537 1.38 mlelstv spec = buf; 538 1.38 mlelstv 539 1.40 mrg if (! set_dumpdev1(spec)) 540 1.40 mrg exit(1); 541 1.16 mrg } 542 1.16 mrg 543 1.30 martin static int 544 1.28 xtraeme get_dumpdev(void) 545 1.16 mrg { 546 1.16 mrg dev_t dev; 547 1.16 mrg char *name; 548 1.16 mrg 549 1.30 martin if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1) { 550 1.16 mrg warn("could not get dump device"); 551 1.30 martin return 0; 552 1.30 martin } else if (dev == NODEV) { 553 1.21 drochner printf("no dump device set\n"); 554 1.30 martin return 0; 555 1.30 martin } else { 556 1.16 mrg name = devname(dev, S_IFBLK); 557 1.16 mrg printf("dump device is "); 558 1.16 mrg if (name) 559 1.16 mrg printf("%s\n", name); 560 1.16 mrg else 561 1.34 christos printf("major %llu minor %llu\n", 562 1.34 christos (unsigned long long)major(dev), 563 1.34 christos (unsigned long long)minor(dev)); 564 1.16 mrg } 565 1.30 martin return 1; 566 1.10 mrg } 567 1.10 mrg 568 1.12 lukem static void 569 1.32 martin do_localdevs(int add) 570 1.32 martin { 571 1.32 martin size_t ressize; 572 1.32 martin char *disknames, *disk; 573 1.32 martin static const char mibname[] = "hw.disknames"; 574 1.32 martin 575 1.32 martin ressize = 0; 576 1.32 martin if (sysctlbyname(mibname, NULL, &ressize, NULL, 0)) 577 1.32 martin return; 578 1.32 martin ressize += 200; /* add some arbitrary slope */ 579 1.32 martin disknames = malloc(ressize); 580 1.32 martin if (sysctlbyname(mibname, disknames, &ressize, NULL, 0) == 0) { 581 1.32 martin for (disk = strtok(disknames, " "); disk; 582 1.32 martin disk = strtok(NULL, " ")) 583 1.32 martin do_localdisk(disk, add); 584 1.32 martin } 585 1.32 martin free(disknames); 586 1.32 martin } 587 1.32 martin 588 1.32 martin static void 589 1.32 martin do_localdisk(const char *disk, int add) 590 1.32 martin { 591 1.32 martin int fd; 592 1.32 martin char dvname[MAXPATHLEN]; 593 1.32 martin 594 1.32 martin if ((fd = opendisk(disk, O_RDONLY, dvname, sizeof(dvname), 0)) == -1) 595 1.32 martin return; 596 1.32 martin 597 1.32 martin if (!do_wedgesofdisk(fd, add)) 598 1.32 martin do_partitionsofdisk(disk, fd, add); 599 1.32 martin 600 1.32 martin close(fd); 601 1.32 martin } 602 1.32 martin 603 1.32 martin static int 604 1.32 martin do_wedgesofdisk(int fd, int add) 605 1.32 martin { 606 1.32 martin char devicename[MAXPATHLEN]; 607 1.32 martin struct dkwedge_info *dkw; 608 1.32 martin struct dkwedge_list dkwl; 609 1.32 martin size_t bufsize; 610 1.32 martin u_int i; 611 1.32 martin 612 1.32 martin dkw = NULL; 613 1.32 martin dkwl.dkwl_buf = dkw; 614 1.32 martin dkwl.dkwl_bufsize = 0; 615 1.32 martin 616 1.32 martin for (;;) { 617 1.32 martin if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1) 618 1.32 martin return 0; 619 1.32 martin if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied) 620 1.32 martin break; 621 1.32 martin bufsize = dkwl.dkwl_nwedges * sizeof(*dkw); 622 1.32 martin if (dkwl.dkwl_bufsize < bufsize) { 623 1.32 martin dkw = realloc(dkwl.dkwl_buf, bufsize); 624 1.32 martin if (dkw == NULL) 625 1.32 martin return 0; 626 1.32 martin dkwl.dkwl_buf = dkw; 627 1.32 martin dkwl.dkwl_bufsize = bufsize; 628 1.32 martin } 629 1.32 martin } 630 1.32 martin 631 1.32 martin for (i = 0; i < dkwl.dkwl_ncopied; i++) { 632 1.32 martin if (strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) != 0) 633 1.32 martin continue; 634 1.32 martin snprintf(devicename, sizeof(devicename), "%s%s", _PATH_DEV, 635 1.32 martin dkw[i].dkw_devname); 636 1.32 martin devicename[sizeof(devicename)-1] = '\0'; 637 1.32 martin 638 1.32 martin if (add) { 639 1.32 martin if (add_swap(devicename, pri)) { 640 1.32 martin printf( 641 1.32 martin "%s: adding %s as swap device at priority 0\n", 642 1.32 martin getprogname(), devicename); 643 1.32 martin } 644 1.32 martin } else { 645 1.32 martin if (delete_swap(devicename)) { 646 1.32 martin printf( 647 1.32 martin "%s: removing %s as swap device\n", 648 1.32 martin getprogname(), devicename); 649 1.32 martin } 650 1.32 martin } 651 1.32 martin 652 1.32 martin } 653 1.32 martin 654 1.32 martin free(dkw); 655 1.32 martin return dkwl.dkwl_nwedges != 0; 656 1.32 martin } 657 1.32 martin 658 1.32 martin static int 659 1.32 martin do_partitionsofdisk(const char *prefix, int fd, int add) 660 1.32 martin { 661 1.32 martin char devicename[MAXPATHLEN]; 662 1.32 martin struct disklabel lab; 663 1.32 martin uint i; 664 1.32 martin 665 1.32 martin if (ioctl(fd, DIOCGDINFO, &lab) != 0) 666 1.32 martin return 0; 667 1.32 martin 668 1.32 martin for (i = 0; i < lab.d_npartitions; i++) { 669 1.32 martin if (lab.d_partitions[i].p_fstype != FS_SWAP) 670 1.32 martin continue; 671 1.32 martin snprintf(devicename, sizeof(devicename), "%s%s%c", _PATH_DEV, 672 1.32 martin prefix, 'a'+i); 673 1.32 martin devicename[sizeof(devicename)-1] = '\0'; 674 1.32 martin 675 1.32 martin if (add) { 676 1.32 martin if (add_swap(devicename, pri)) { 677 1.32 martin printf( 678 1.32 martin "%s: adding %s as swap device at priority 0\n", 679 1.32 martin getprogname(), devicename); 680 1.32 martin } 681 1.32 martin } else { 682 1.32 martin if (delete_swap(devicename)) { 683 1.32 martin printf( 684 1.32 martin "%s: removing %s as swap device\n", 685 1.32 martin getprogname(), devicename); 686 1.32 martin } 687 1.32 martin } 688 1.32 martin } 689 1.32 martin 690 1.32 martin return 1; 691 1.32 martin } 692 1.32 martin 693 1.32 martin static int 694 1.32 martin check_fstab(void) 695 1.32 martin { 696 1.32 martin struct fstab *fp; 697 1.32 martin 698 1.32 martin while ((fp = getfsent()) != NULL) { 699 1.32 martin if (strcmp(fp->fs_type, "dp") == 0) 700 1.32 martin return 1; 701 1.32 martin 702 1.32 martin if (strcmp(fp->fs_type, "sw") == 0) 703 1.32 martin return 1; 704 1.32 martin } 705 1.32 martin 706 1.32 martin return 0; 707 1.32 martin } 708 1.32 martin 709 1.32 martin static void 710 1.28 xtraeme do_fstab(int add) 711 1.1 mrg { 712 1.1 mrg struct fstab *fp; 713 1.1 mrg char *s; 714 1.1 mrg long priority; 715 1.4 thorpej struct stat st; 716 1.4 thorpej int isblk; 717 1.35 apb int success = 0; /* set to 1 after a successful operation */ 718 1.35 apb int error = 0; /* set to 1 after an error */ 719 1.27 lukem 720 1.27 lukem #ifdef RESCUEDIR 721 1.27 lukem #define PATH_MOUNT RESCUEDIR "/mount_nfs" 722 1.27 lukem #define PATH_UMOUNT RESCUEDIR "/umount" 723 1.27 lukem #else 724 1.12 lukem #define PATH_MOUNT "/sbin/mount_nfs" 725 1.12 lukem #define PATH_UMOUNT "/sbin/umount" 726 1.27 lukem #endif 727 1.27 lukem 728 1.12 lukem char cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2]; 729 1.1 mrg 730 1.1 mrg #define PRIORITYEQ "priority=" 731 1.1 mrg #define NFSMNTPT "nfsmntpt=" 732 1.5 mikel while ((fp = getfsent()) != NULL) { 733 1.37 christos char buf[MAXPATHLEN]; 734 1.37 christos char *spec, *fsspec; 735 1.1 mrg 736 1.42 kre /* 737 1.43 kre * Ignore any entries which are not related to swapping 738 1.42 kre */ 739 1.42 kre if (strcmp(fp->fs_type, "sw") != 0 && 740 1.42 kre strcmp(fp->fs_type, "dp") != 0) 741 1.42 kre continue; 742 1.42 kre 743 1.37 christos if (getfsspecname(buf, sizeof(buf), fp->fs_spec) == NULL) { 744 1.37 christos warn("%s", buf); 745 1.37 christos continue; 746 1.37 christos } 747 1.37 christos fsspec = spec = buf; 748 1.12 lukem cmd[0] = '\0'; 749 1.10 mrg 750 1.12 lukem if (strcmp(fp->fs_type, "dp") == 0 && add) { 751 1.38 mlelstv set_dumpdev1(spec); 752 1.10 mrg continue; 753 1.10 mrg } 754 1.10 mrg 755 1.20 jdolecek /* handle dp as mnt option */ 756 1.20 jdolecek if (strstr(fp->fs_mntops, "dp") && add) 757 1.38 mlelstv set_dumpdev1(spec); 758 1.20 jdolecek 759 1.4 thorpej isblk = 0; 760 1.1 mrg 761 1.5 mikel if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) { 762 1.1 mrg s += sizeof(PRIORITYEQ) - 1; 763 1.1 mrg priority = atol(s); 764 1.1 mrg } else 765 1.1 mrg priority = pri; 766 1.1 mrg 767 1.5 mikel if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) { 768 1.12 lukem char *t; 769 1.1 mrg 770 1.4 thorpej /* 771 1.4 thorpej * Skip this song and dance if we're only 772 1.4 thorpej * doing block devices. 773 1.4 thorpej */ 774 1.12 lukem if (tflag != NULL && strcmp(tflag, "blk") == 0) 775 1.4 thorpej continue; 776 1.4 thorpej 777 1.1 mrg t = strpbrk(s, ","); 778 1.1 mrg if (t != 0) 779 1.1 mrg *t = '\0'; 780 1.1 mrg spec = strdup(s + strlen(NFSMNTPT)); 781 1.1 mrg if (t != 0) 782 1.1 mrg *t = ','; 783 1.1 mrg 784 1.1 mrg if (spec == NULL) 785 1.1 mrg errx(1, "Out of memory"); 786 1.1 mrg 787 1.1 mrg if (strlen(spec) == 0) { 788 1.1 mrg warnx("empty mountpoint"); 789 1.28 xtraeme free(spec); 790 1.1 mrg continue; 791 1.1 mrg } 792 1.12 lukem if (add) { 793 1.12 lukem snprintf(cmd, sizeof(cmd), "%s %s %s", 794 1.37 christos PATH_MOUNT, fsspec, spec); 795 1.12 lukem if (system(cmd) != 0) { 796 1.37 christos warnx("%s: mount failed", fsspec); 797 1.12 lukem continue; 798 1.12 lukem } 799 1.12 lukem } else { 800 1.12 lukem snprintf(cmd, sizeof(cmd), "%s %s", 801 1.37 christos PATH_UMOUNT, fsspec); 802 1.1 mrg } 803 1.4 thorpej } else { 804 1.4 thorpej /* 805 1.4 thorpej * Determine blk-ness. 806 1.4 thorpej */ 807 1.4 thorpej if (stat(spec, &st) < 0) { 808 1.15 itojun warn("%s", spec); 809 1.4 thorpej continue; 810 1.4 thorpej } 811 1.4 thorpej if (S_ISBLK(st.st_mode)) 812 1.4 thorpej isblk = 1; 813 1.4 thorpej } 814 1.4 thorpej 815 1.4 thorpej /* 816 1.4 thorpej * Skip this type if we're told to. 817 1.4 thorpej */ 818 1.4 thorpej if (tflag != NULL) { 819 1.4 thorpej if (strcmp(tflag, "blk") == 0 && isblk == 0) 820 1.4 thorpej continue; 821 1.4 thorpej if (strcmp(tflag, "noblk") == 0 && isblk == 1) 822 1.4 thorpej continue; 823 1.1 mrg } 824 1.1 mrg 825 1.12 lukem if (add) { 826 1.12 lukem if (add_swap(spec, (int)priority)) { 827 1.35 apb success = 1; 828 1.12 lukem printf( 829 1.12 lukem "%s: adding %s as swap device at priority %d\n", 830 1.37 christos getprogname(), fsspec, (int)priority); 831 1.35 apb } else { 832 1.35 apb error = 1; 833 1.35 apb fprintf(stderr, 834 1.35 apb "%s: failed to add %s as swap device\n", 835 1.37 christos getprogname(), fsspec); 836 1.12 lukem } 837 1.12 lukem } else { 838 1.12 lukem if (delete_swap(spec)) { 839 1.35 apb success = 1; 840 1.12 lukem printf( 841 1.12 lukem "%s: removing %s as swap device\n", 842 1.37 christos getprogname(), fsspec); 843 1.35 apb } else { 844 1.35 apb error = 1; 845 1.35 apb fprintf(stderr, 846 1.35 apb "%s: failed to remove %s as swap device\n", 847 1.37 christos getprogname(), fsspec); 848 1.12 lukem } 849 1.12 lukem if (cmd[0]) { 850 1.12 lukem if (system(cmd) != 0) { 851 1.37 christos warnx("%s: umount failed", fsspec); 852 1.35 apb error = 1; 853 1.12 lukem continue; 854 1.12 lukem } 855 1.12 lukem } 856 1.8 mrg } 857 1.1 mrg 858 1.37 christos if (spec != fsspec) 859 1.28 xtraeme free(spec); 860 1.1 mrg } 861 1.35 apb if (error) 862 1.8 mrg exit(1); 863 1.35 apb else if (success) 864 1.35 apb exit(0); 865 1.35 apb else 866 1.35 apb exit(2); /* not really an error, but no swap devices found */ 867 1.1 mrg } 868 1.1 mrg 869 1.12 lukem static void 870 1.28 xtraeme usage(void) 871 1.1 mrg { 872 1.18 cgd const char *progname = getprogname(); 873 1.1 mrg 874 1.32 martin fprintf(stderr, "usage: %s -A [-f|-o] [-n] [-p priority] " 875 1.32 martin "[-t blk|noblk|auto]\n", progname); 876 1.18 cgd fprintf(stderr, " %s -a [-p priority] path\n", progname); 877 1.32 martin fprintf(stderr, " %s -q\n", progname); 878 1.18 cgd fprintf(stderr, " %s -c -p priority path\n", progname); 879 1.31 wiz fprintf(stderr, " %s -D dumpdev|none\n", progname); 880 1.18 cgd fprintf(stderr, " %s -d path\n", progname); 881 1.25 mrg fprintf(stderr, " %s -l | -s [-k|-m|-g|-h]\n", progname); 882 1.32 martin fprintf(stderr, " %s -U [-n] [-t blk|noblk|auto]\n", progname); 883 1.26 cjep fprintf(stderr, " %s -z\n", progname); 884 1.1 mrg exit(1); 885 1.1 mrg } 886