1 /*- 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char copyright[] = 33 "@(#) Copyright (c) 1980, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/types.h> 46 #include <sys/mdioctl.h> 47 #include <sys/stat.h> 48 #include <sys/sysctl.h> 49 #include <sys/wait.h> 50 #include <vm/vm_param.h> 51 52 #include <err.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <fnmatch.h> 56 #include <fstab.h> 57 #include <libgen.h> 58 #include <libutil.h> 59 #include <limits.h> 60 #include <paths.h> 61 #include <stdarg.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 67 static void usage(void); 68 static const char *swap_on_off(const char *, int, char *); 69 static const char *swap_on_off_gbde(const char *, int); 70 static const char *swap_on_off_geli(const char *, char *, int); 71 static const char *swap_on_off_md(const char *, char *, int); 72 static const char *swap_on_off_sfile(const char *, int); 73 static void swaplist(int, int, int); 74 static int run_cmd(int *, const char *, ...) __printflike(2, 3); 75 76 static enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL; 77 78 static int qflag; 79 80 int 81 main(int argc, char **argv) 82 { 83 struct fstab *fsp; 84 const char *swfile; 85 char *ptr; 86 int ret, ch, doall; 87 int sflag, lflag, late, hflag; 88 const char *etc_fstab; 89 90 sflag = lflag = late = hflag = 0; 91 if ((ptr = strrchr(argv[0], '/')) == NULL) 92 ptr = argv[0]; 93 if (strstr(ptr, "swapon") != NULL) 94 which_prog = SWAPON; 95 else if (strstr(ptr, "swapoff") != NULL) 96 which_prog = SWAPOFF; 97 orig_prog = which_prog; 98 99 doall = 0; 100 etc_fstab = NULL; 101 while ((ch = getopt(argc, argv, "AadghklLmqsUF:")) != -1) { 102 switch(ch) { 103 case 'A': 104 if (which_prog == SWAPCTL) { 105 doall = 1; 106 which_prog = SWAPON; 107 } else 108 usage(); 109 break; 110 case 'a': 111 if (which_prog == SWAPON || which_prog == SWAPOFF) 112 doall = 1; 113 else 114 which_prog = SWAPON; 115 break; 116 case 'd': 117 if (which_prog == SWAPCTL) 118 which_prog = SWAPOFF; 119 else 120 usage(); 121 break; 122 case 'g': 123 hflag = 'G'; 124 break; 125 case 'h': 126 hflag = 'H'; 127 break; 128 case 'k': 129 hflag = 'K'; 130 break; 131 case 'l': 132 lflag = 1; 133 break; 134 case 'L': 135 late = 1; 136 break; 137 case 'm': 138 hflag = 'M'; 139 break; 140 case 'q': 141 if (which_prog == SWAPON || which_prog == SWAPOFF) 142 qflag = 1; 143 break; 144 case 's': 145 sflag = 1; 146 break; 147 case 'U': 148 if (which_prog == SWAPCTL) { 149 doall = 1; 150 which_prog = SWAPOFF; 151 } else 152 usage(); 153 break; 154 case 'F': 155 etc_fstab = optarg; 156 break; 157 case '?': 158 default: 159 usage(); 160 } 161 } 162 argv += optind; 163 164 ret = 0; 165 swfile = NULL; 166 if (etc_fstab != NULL) 167 setfstab(etc_fstab); 168 if (which_prog == SWAPON || which_prog == SWAPOFF) { 169 if (doall) { 170 while ((fsp = getfsent()) != NULL) { 171 if (strcmp(fsp->fs_type, FSTAB_SW) != 0) 172 continue; 173 if (strstr(fsp->fs_mntops, "noauto") != NULL) 174 continue; 175 if (which_prog != SWAPOFF && 176 strstr(fsp->fs_mntops, "late") && 177 late == 0) 178 continue; 179 swfile = swap_on_off(fsp->fs_spec, 1, 180 fsp->fs_mntops); 181 if (swfile == NULL) { 182 ret = 1; 183 continue; 184 } 185 if (qflag == 0) { 186 printf("%s: %sing %s as swap device\n", 187 getprogname(), 188 (which_prog == SWAPOFF) ? 189 "remov" : "add", swfile); 190 } 191 } 192 } else if (*argv == NULL) 193 usage(); 194 for (; *argv; ++argv) { 195 swfile = swap_on_off(*argv, 0, NULL); 196 if (swfile == NULL) { 197 ret = 1; 198 continue; 199 } 200 if (orig_prog == SWAPCTL) { 201 printf("%s: %sing %s as swap device\n", 202 getprogname(), 203 (which_prog == SWAPOFF) ? "remov" : "add", 204 swfile); 205 } 206 } 207 } else { 208 if (lflag || sflag) 209 swaplist(lflag, sflag, hflag); 210 else 211 usage(); 212 } 213 exit(ret); 214 } 215 216 static const char * 217 swap_on_off(const char *name, int doingall, char *mntops) 218 { 219 char *base, *basebuf; 220 221 /* Swap on vnode-backed md(4) device. */ 222 if (mntops != NULL && 223 (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) == 0 || 224 fnmatch(MD_NAME "[0-9]*", name, 0) == 0 || 225 strncmp(_PATH_DEV MD_NAME, name, 226 sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 || 227 strncmp(MD_NAME, name, sizeof(MD_NAME)) == 0)) 228 return (swap_on_off_md(name, mntops, doingall)); 229 230 basebuf = strdup(name); 231 base = basename(basebuf); 232 233 /* Swap on encrypted device by GEOM_BDE. */ 234 if (fnmatch("*.bde", base, 0) == 0) { 235 free(basebuf); 236 return (swap_on_off_gbde(name, doingall)); 237 } 238 239 /* Swap on encrypted device by GEOM_ELI. */ 240 if (fnmatch("*.eli", base, 0) == 0) { 241 free(basebuf); 242 return (swap_on_off_geli(name, mntops, doingall)); 243 } 244 245 /* Swap on special file. */ 246 free(basebuf); 247 return (swap_on_off_sfile(name, doingall)); 248 } 249 250 /* Strip off .bde or .eli suffix from swap device name */ 251 static char * 252 swap_basename(const char *name) 253 { 254 char *dname, *p; 255 256 dname = strdup(name); 257 p = strrchr(dname, '.'); 258 /* assert(p != NULL); */ 259 *p = '\0'; 260 261 return (dname); 262 } 263 264 static const char * 265 swap_on_off_gbde(const char *name, int doingall) 266 { 267 const char *ret; 268 char pass[64 * 2 + 1]; 269 unsigned char bpass[64]; 270 char *dname; 271 int i, error; 272 273 dname = swap_basename(name); 274 if (dname == NULL) 275 return (NULL); 276 277 if (which_prog == SWAPON) { 278 arc4random_buf(bpass, sizeof(bpass)); 279 for (i = 0; i < (int)sizeof(bpass); i++) 280 sprintf(&pass[2 * i], "%02x", bpass[i]); 281 pass[sizeof(pass) - 1] = '\0'; 282 283 error = run_cmd(NULL, "%s init %s -P %s", _PATH_GBDE, 284 dname, pass); 285 if (error) { 286 /* bde device found. Ignore it. */ 287 free(dname); 288 if (qflag == 0) 289 warnx("%s: Device already in use", name); 290 return (NULL); 291 } 292 error = run_cmd(NULL, "%s attach %s -p %s", _PATH_GBDE, 293 dname, pass); 294 free(dname); 295 if (error) { 296 warnx("gbde (attach) error: %s", name); 297 return (NULL); 298 } 299 } 300 301 ret = swap_on_off_sfile(name, doingall); 302 303 if (which_prog == SWAPOFF) { 304 error = run_cmd(NULL, "%s detach %s", _PATH_GBDE, dname); 305 free(dname); 306 if (error) { 307 /* bde device not found. Ignore it. */ 308 if (qflag == 0) 309 warnx("%s: Device not found", name); 310 return (NULL); 311 } 312 } 313 314 return (ret); 315 } 316 317 /* Build geli(8) arguments from mntops */ 318 static char * 319 swap_on_geli_args(const char *mntops) 320 { 321 const char *aalgo, *ealgo, *keylen_str, *sectorsize_str; 322 const char *aflag, *eflag, *lflag, *Tflag, *sflag; 323 char *p, *args, *token, *string, *ops; 324 int pagesize; 325 size_t pagesize_len; 326 u_long ul; 327 328 /* Use built-in defaults for geli(8). */ 329 aalgo = ealgo = keylen_str = ""; 330 aflag = eflag = lflag = Tflag = ""; 331 332 /* We will always specify sectorsize. */ 333 sflag = " -s "; 334 sectorsize_str = NULL; 335 336 if (mntops != NULL) { 337 string = ops = strdup(mntops); 338 339 while ((token = strsep(&string, ",")) != NULL) { 340 if ((p = strstr(token, "aalgo=")) == token) { 341 aalgo = p + sizeof("aalgo=") - 1; 342 aflag = " -a "; 343 } else if ((p = strstr(token, "ealgo=")) == token) { 344 ealgo = p + sizeof("ealgo=") - 1; 345 eflag = " -e "; 346 } else if ((p = strstr(token, "keylen=")) == token) { 347 keylen_str = p + sizeof("keylen=") - 1; 348 errno = 0; 349 ul = strtoul(keylen_str, &p, 10); 350 if (errno == 0) { 351 if (*p != '\0' || ul > INT_MAX) 352 errno = EINVAL; 353 } 354 if (errno) { 355 warn("Invalid keylen: %s", keylen_str); 356 free(ops); 357 return (NULL); 358 } 359 lflag = " -l "; 360 } else if ((p = strstr(token, "sectorsize=")) == token) { 361 sectorsize_str = p + sizeof("sectorsize=") - 1; 362 errno = 0; 363 ul = strtoul(sectorsize_str, &p, 10); 364 if (errno == 0) { 365 if (*p != '\0' || ul > INT_MAX) 366 errno = EINVAL; 367 } 368 if (errno) { 369 warn("Invalid sectorsize: %s", 370 sectorsize_str); 371 free(ops); 372 return (NULL); 373 } 374 } else if ((p = strstr(token, "notrim")) == token) { 375 Tflag = " -T "; 376 } else if (strcmp(token, "sw") != 0) { 377 warnx("Invalid option: %s", token); 378 free(ops); 379 return (NULL); 380 } 381 } 382 } else 383 ops = NULL; 384 385 /* 386 * If we do not have a sector size at this point, fill in 387 * pagesize as sector size. 388 */ 389 if (sectorsize_str == NULL) { 390 /* Use pagesize as default sectorsize. */ 391 pagesize = getpagesize(); 392 pagesize_len = snprintf(NULL, 0, "%d", pagesize) + 1; 393 p = alloca(pagesize_len); 394 snprintf(p, pagesize_len, "%d", pagesize); 395 sectorsize_str = p; 396 } 397 398 (void)asprintf(&args, "%s%s%s%s%s%s%s%s%s -d", 399 aflag, aalgo, eflag, ealgo, lflag, keylen_str, Tflag, 400 sflag, sectorsize_str); 401 402 free(ops); 403 return (args); 404 } 405 406 static const char * 407 swap_on_off_geli(const char *name, char *mntops, int doingall) 408 { 409 struct stat sb; 410 char *dname, *args; 411 int error; 412 413 error = stat(name, &sb); 414 415 if (which_prog == SWAPON) do { 416 /* Skip if the .eli device already exists. */ 417 if (error == 0) 418 break; 419 420 args = swap_on_geli_args(mntops); 421 if (args == NULL) 422 return (NULL); 423 424 dname = swap_basename(name); 425 if (dname == NULL) { 426 free(args); 427 return (NULL); 428 } 429 430 error = run_cmd(NULL, "%s onetime%s %s", _PATH_GELI, args, 431 dname); 432 433 free(dname); 434 free(args); 435 436 if (error) { 437 /* error occurred during creation. */ 438 if (qflag == 0) 439 warnx("%s: Invalid parameters", name); 440 return (NULL); 441 } 442 } while (0); 443 444 return (swap_on_off_sfile(name, doingall)); 445 } 446 447 static const char * 448 swap_on_off_md(const char *name, char *mntops, int doingall) 449 { 450 FILE *sfd; 451 int fd, mdunit, error; 452 const char *ret; 453 static char mdpath[PATH_MAX], linebuf[PATH_MAX]; 454 char *p, *vnodefile; 455 size_t linelen; 456 u_long ul; 457 458 fd = -1; 459 sfd = NULL; 460 if (strlen(name) == (sizeof(MD_NAME) - 1)) 461 mdunit = -1; 462 else { 463 errno = 0; 464 ul = strtoul(name + 2, &p, 10); 465 if (errno == 0) { 466 if (*p != '\0' || ul > INT_MAX) 467 errno = EINVAL; 468 } 469 if (errno) { 470 warn("Bad device unit: %s", name); 471 return (NULL); 472 } 473 mdunit = (int)ul; 474 } 475 476 vnodefile = NULL; 477 if ((p = strstr(mntops, "file=")) != NULL) { 478 vnodefile = strdup(p + sizeof("file=") - 1); 479 p = strchr(vnodefile, ','); 480 if (p != NULL) 481 *p = '\0'; 482 } 483 if (vnodefile == NULL) { 484 warnx("file option not found for %s", name); 485 return (NULL); 486 } 487 488 if (which_prog == SWAPON) { 489 if (mdunit == -1) { 490 error = run_cmd(&fd, "%s -l -n -f %s", 491 _PATH_MDCONFIG, vnodefile); 492 if (error == 0) { 493 /* md device found. Ignore it. */ 494 close(fd); 495 if (!qflag) 496 warnx("%s: Device already in use", 497 vnodefile); 498 free(vnodefile); 499 return (NULL); 500 } 501 error = run_cmd(&fd, "%s -a -t vnode -n -f %s", 502 _PATH_MDCONFIG, vnodefile); 503 if (error) { 504 warnx("mdconfig (attach) error: file=%s", 505 vnodefile); 506 free(vnodefile); 507 return (NULL); 508 } 509 sfd = fdopen(fd, "r"); 510 if (sfd == NULL) { 511 warn("mdconfig (attach) fdopen error"); 512 ret = NULL; 513 goto err; 514 } 515 p = fgetln(sfd, &linelen); 516 if (p == NULL && 517 (linelen < 2 || linelen > sizeof(linebuf))) { 518 warn("mdconfig (attach) unexpected output"); 519 ret = NULL; 520 goto err; 521 } 522 strncpy(linebuf, p, linelen); 523 linebuf[linelen - 1] = '\0'; 524 errno = 0; 525 ul = strtoul(linebuf, &p, 10); 526 if (errno == 0) { 527 if (*p != '\0' || ul > INT_MAX) 528 errno = EINVAL; 529 } 530 if (errno) { 531 warn("mdconfig (attach) unexpected output: %s", 532 linebuf); 533 ret = NULL; 534 goto err; 535 } 536 mdunit = (int)ul; 537 } else { 538 error = run_cmd(&fd, "%s -l -n -f %s -u %d", 539 _PATH_MDCONFIG, vnodefile, mdunit); 540 if (error == 0) { 541 /* md device found. Ignore it. */ 542 close(fd); 543 if (qflag == 0) 544 warnx("md%d on %s: Device already " 545 "in use", mdunit, vnodefile); 546 free(vnodefile); 547 return (NULL); 548 } 549 error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s", 550 _PATH_MDCONFIG, mdunit, vnodefile); 551 if (error) { 552 warnx("mdconfig (attach) error: " 553 "md%d on file=%s", mdunit, vnodefile); 554 free(vnodefile); 555 return (NULL); 556 } 557 } 558 } else /* SWAPOFF */ { 559 if (mdunit == -1) { 560 error = run_cmd(&fd, "%s -l -n -f %s", 561 _PATH_MDCONFIG, vnodefile); 562 if (error) { 563 /* md device not found. Ignore it. */ 564 close(fd); 565 if (!qflag) 566 warnx("md on %s: Device not found", 567 vnodefile); 568 free(vnodefile); 569 return (NULL); 570 } 571 sfd = fdopen(fd, "r"); 572 if (sfd == NULL) { 573 warn("mdconfig (list) fdopen error"); 574 ret = NULL; 575 goto err; 576 } 577 p = fgetln(sfd, &linelen); 578 if (p == NULL && 579 (linelen < 2 || linelen > sizeof(linebuf) - 1)) { 580 warn("mdconfig (list) unexpected output"); 581 ret = NULL; 582 goto err; 583 } 584 strncpy(linebuf, p, linelen); 585 linebuf[linelen - 1] = '\0'; 586 p = strchr(linebuf, ' '); 587 if (p != NULL) 588 *p = '\0'; 589 errno = 0; 590 ul = strtoul(linebuf, &p, 10); 591 if (errno == 0) { 592 if (*p != '\0' || ul > INT_MAX) 593 errno = EINVAL; 594 } 595 if (errno) { 596 warn("mdconfig (list) unexpected output: %s", 597 linebuf); 598 ret = NULL; 599 goto err; 600 } 601 mdunit = (int)ul; 602 } else { 603 error = run_cmd(&fd, "%s -l -n -f %s -u %d", 604 _PATH_MDCONFIG, vnodefile, mdunit); 605 if (error) { 606 /* md device not found. Ignore it. */ 607 close(fd); 608 if (!qflag) 609 warnx("md%d on %s: Device not found", 610 mdunit, vnodefile); 611 free(vnodefile); 612 return (NULL); 613 } 614 } 615 } 616 snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV, 617 MD_NAME, mdunit); 618 mdpath[sizeof(mdpath) - 1] = '\0'; 619 ret = swap_on_off_sfile(mdpath, doingall); 620 621 if (which_prog == SWAPOFF) { 622 if (ret != NULL) { 623 error = run_cmd(NULL, "%s -d -u %d", 624 _PATH_MDCONFIG, mdunit); 625 if (error) 626 warn("mdconfig (detach) detach failed: %s%s%d", 627 _PATH_DEV, MD_NAME, mdunit); 628 } 629 } 630 err: 631 if (sfd != NULL) 632 fclose(sfd); 633 if (fd != -1) 634 close(fd); 635 free(vnodefile); 636 return (ret); 637 } 638 639 static int 640 run_cmd(int *ofd, const char *cmdline, ...) 641 { 642 va_list ap; 643 char **argv, **argvp, *cmd, *p; 644 int argc, pid, status, rv; 645 int pfd[2], nfd, dup2dn; 646 647 va_start(ap, cmdline); 648 rv = vasprintf(&cmd, cmdline, ap); 649 if (rv == -1) { 650 warn("%s", __func__); 651 va_end(ap); 652 return (rv); 653 } 654 va_end(ap); 655 656 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++) 657 argc++; 658 argv = (char **)malloc(sizeof(*argv) * (argc + 1)); 659 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;) 660 if (**argvp != '\0' && (++argvp > &argv[argc])) { 661 *argvp = NULL; 662 break; 663 } 664 /* The argv array ends up NULL-terminated here. */ 665 #if 0 666 { 667 int i; 668 669 fprintf(stderr, "DEBUG: running:"); 670 /* Should be equivalent to 'cmd' (before strsep, of course). */ 671 for (i = 0; argv[i] != NULL; i++) 672 fprintf(stderr, " %s", argv[i]); 673 fprintf(stderr, "\n"); 674 } 675 #endif 676 dup2dn = 1; 677 if (ofd != NULL) { 678 if (pipe(&pfd[0]) == -1) { 679 warn("%s: pipe", __func__); 680 return (-1); 681 } 682 *ofd = pfd[0]; 683 dup2dn = 0; 684 } 685 pid = fork(); 686 switch (pid) { 687 case 0: 688 /* Child process. */ 689 if (ofd != NULL) 690 if (dup2(pfd[1], STDOUT_FILENO) < 0) 691 err(1, "dup2 in %s", __func__); 692 nfd = open(_PATH_DEVNULL, O_RDWR); 693 if (nfd == -1) 694 err(1, "%s: open %s", __func__, _PATH_DEVNULL); 695 if (dup2(nfd, STDIN_FILENO) < 0) 696 err(1, "%s: dup2", __func__); 697 if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0) 698 err(1, "%s: dup2", __func__); 699 if (dup2(nfd, STDERR_FILENO) < 0) 700 err(1, "%s: dup2", __func__); 701 execv(argv[0], argv); 702 warn("exec: %s", argv[0]); 703 _exit(-1); 704 case -1: 705 err(1, "%s: fork", __func__); 706 } 707 free(cmd); 708 free(argv); 709 while (waitpid(pid, &status, 0) != pid) 710 ; 711 return (WEXITSTATUS(status)); 712 } 713 714 static const char * 715 swap_on_off_sfile(const char *name, int doingall) 716 { 717 int error; 718 719 if (which_prog == SWAPON) 720 error = swapon(name); 721 else /* SWAPOFF */ 722 error = swapoff(name); 723 724 if (error == -1) { 725 switch (errno) { 726 case EBUSY: 727 if (doingall == 0) 728 warnx("%s: Device already in use", name); 729 break; 730 case EINVAL: 731 if (which_prog == SWAPON) 732 warnx("%s: NSWAPDEV limit reached", name); 733 else if (doingall == 0) 734 warn("%s", name); 735 break; 736 default: 737 warn("%s", name); 738 break; 739 } 740 return (NULL); 741 } 742 return (name); 743 } 744 745 static void 746 usage(void) 747 { 748 749 fprintf(stderr, "usage: %s ", getprogname()); 750 switch(orig_prog) { 751 case SWAPON: 752 case SWAPOFF: 753 fprintf(stderr, "[-F fstab] -aLq | file ...\n"); 754 break; 755 case SWAPCTL: 756 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n"); 757 break; 758 } 759 exit(1); 760 } 761 762 static void 763 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen, 764 long blocksize) 765 { 766 char tmp[16]; 767 768 if (hflag == 'H') { 769 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE, 770 HN_B | HN_NOSPACE | HN_DECIMAL); 771 snprintf(buf, bufsize, "%*s", hlen, tmp); 772 } else 773 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize); 774 } 775 776 static void 777 swaplist(int lflag, int sflag, int hflag) 778 { 779 size_t mibsize, size; 780 struct xswdev xsw; 781 int hlen, mib[16], n, pagesize; 782 long blocksize; 783 long long total = 0; 784 long long used = 0; 785 long long tmp_total; 786 long long tmp_used; 787 char buf[32]; 788 789 pagesize = getpagesize(); 790 switch(hflag) { 791 case 'G': 792 blocksize = 1024 * 1024 * 1024; 793 strlcpy(buf, "1GB-blocks", sizeof(buf)); 794 hlen = 10; 795 break; 796 case 'H': 797 blocksize = -1; 798 strlcpy(buf, "Bytes", sizeof(buf)); 799 hlen = 10; 800 break; 801 case 'K': 802 blocksize = 1024; 803 strlcpy(buf, "1kB-blocks", sizeof(buf)); 804 hlen = 10; 805 break; 806 case 'M': 807 blocksize = 1024 * 1024; 808 strlcpy(buf, "1MB-blocks", sizeof(buf)); 809 hlen = 10; 810 break; 811 default: 812 getbsize(&hlen, &blocksize); 813 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); 814 break; 815 } 816 817 mibsize = nitems(mib); 818 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 819 err(1, "sysctlnametomib()"); 820 821 if (lflag) { 822 printf("%-13s %*s %*s\n", 823 "Device:", 824 hlen, buf, 825 hlen, "Used:"); 826 } 827 828 for (n = 0; ; ++n) { 829 mib[mibsize] = n; 830 size = sizeof xsw; 831 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 832 break; 833 if (xsw.xsw_version != XSWDEV_VERSION) 834 errx(1, "xswdev version mismatch"); 835 836 tmp_total = (long long)xsw.xsw_nblks * pagesize; 837 tmp_used = (long long)xsw.xsw_used * pagesize; 838 total += tmp_total; 839 used += tmp_used; 840 if (lflag) { 841 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen, 842 blocksize); 843 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR), 844 buf); 845 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen, 846 blocksize); 847 printf("%s\n", buf); 848 } 849 } 850 if (errno != ENOENT) 851 err(1, "sysctl()"); 852 853 if (sflag) { 854 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize); 855 printf("Total: %s ", buf); 856 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize); 857 printf("%s\n", buf); 858 } 859 } 860 861