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[PATH_MAX]; 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 basename_r(name, base); 231 232 /* Swap on encrypted device by GEOM_BDE. */ 233 if (fnmatch("*.bde", base, 0) == 0) 234 return (swap_on_off_gbde(name, doingall)); 235 236 /* Swap on encrypted device by GEOM_ELI. */ 237 if (fnmatch("*.eli", base, 0) == 0) 238 return (swap_on_off_geli(name, mntops, doingall)); 239 240 /* Swap on special file. */ 241 return (swap_on_off_sfile(name, doingall)); 242 } 243 244 /* Strip off .bde or .eli suffix from swap device name */ 245 static char * 246 swap_basename(const char *name) 247 { 248 char *dname, *p; 249 250 dname = strdup(name); 251 p = strrchr(dname, '.'); 252 /* assert(p != NULL); */ 253 *p = '\0'; 254 255 return (dname); 256 } 257 258 static const char * 259 swap_on_off_gbde(const char *name, int doingall) 260 { 261 const char *ret; 262 char pass[64 * 2 + 1]; 263 unsigned char bpass[64]; 264 char *dname; 265 int i, error; 266 267 dname = swap_basename(name); 268 if (dname == NULL) 269 return (NULL); 270 271 if (which_prog == SWAPON) { 272 arc4random_buf(bpass, sizeof(bpass)); 273 for (i = 0; i < (int)sizeof(bpass); i++) 274 sprintf(&pass[2 * i], "%02x", bpass[i]); 275 pass[sizeof(pass) - 1] = '\0'; 276 277 error = run_cmd(NULL, "%s init %s -P %s", _PATH_GBDE, 278 dname, pass); 279 if (error) { 280 /* bde device found. Ignore it. */ 281 free(dname); 282 if (qflag == 0) 283 warnx("%s: Device already in use", name); 284 return (NULL); 285 } 286 error = run_cmd(NULL, "%s attach %s -p %s", _PATH_GBDE, 287 dname, pass); 288 free(dname); 289 if (error) { 290 warnx("gbde (attach) error: %s", name); 291 return (NULL); 292 } 293 } 294 295 ret = swap_on_off_sfile(name, doingall); 296 297 if (which_prog == SWAPOFF) { 298 error = run_cmd(NULL, "%s detach %s", _PATH_GBDE, dname); 299 free(dname); 300 if (error) { 301 /* bde device not found. Ignore it. */ 302 if (qflag == 0) 303 warnx("%s: Device not found", name); 304 return (NULL); 305 } 306 } 307 308 return (ret); 309 } 310 311 /* Build geli(8) arguments from mntops */ 312 static char * 313 swap_on_geli_args(const char *mntops) 314 { 315 const char *aalgo, *ealgo, *keylen_str, *sectorsize_str; 316 const char *aflag, *eflag, *lflag, *Tflag, *sflag; 317 char *p, *args, *token, *string, *ops; 318 int argsize, pagesize; 319 size_t pagesize_len; 320 u_long ul; 321 322 /* Use built-in defaults for geli(8). */ 323 aalgo = ealgo = keylen_str = ""; 324 aflag = eflag = lflag = Tflag = ""; 325 326 /* We will always specify sectorsize. */ 327 sflag = " -s "; 328 sectorsize_str = NULL; 329 330 if (mntops != NULL) { 331 string = ops = strdup(mntops); 332 333 while ((token = strsep(&string, ",")) != NULL) { 334 if ((p = strstr(token, "aalgo=")) == token) { 335 aalgo = p + sizeof("aalgo=") - 1; 336 aflag = " -a "; 337 } else if ((p = strstr(token, "ealgo=")) == token) { 338 ealgo = p + sizeof("ealgo=") - 1; 339 eflag = " -e "; 340 } else if ((p = strstr(token, "keylen=")) == token) { 341 keylen_str = p + sizeof("keylen=") - 1; 342 errno = 0; 343 ul = strtoul(keylen_str, &p, 10); 344 if (errno == 0) { 345 if (*p != '\0' || ul > INT_MAX) 346 errno = EINVAL; 347 } 348 if (errno) { 349 warn("Invalid keylen: %s", keylen_str); 350 free(ops); 351 return (NULL); 352 } 353 lflag = " -l "; 354 } else if ((p = strstr(token, "sectorsize=")) == token) { 355 sectorsize_str = p + sizeof("sectorsize=") - 1; 356 errno = 0; 357 ul = strtoul(sectorsize_str, &p, 10); 358 if (errno == 0) { 359 if (*p != '\0' || ul > INT_MAX) 360 errno = EINVAL; 361 } 362 if (errno) { 363 warn("Invalid sectorsize: %s", 364 sectorsize_str); 365 free(ops); 366 return (NULL); 367 } 368 } else if ((p = strstr(token, "notrim")) == token) { 369 Tflag = " -T "; 370 } else if (strcmp(token, "sw") != 0) { 371 warnx("Invalid option: %s", token); 372 free(ops); 373 return (NULL); 374 } 375 } 376 } else 377 ops = NULL; 378 379 /* 380 * If we do not have a sector size at this point, fill in 381 * pagesize as sector size. 382 */ 383 if (sectorsize_str == NULL) { 384 /* Use pagesize as default sectorsize. */ 385 pagesize = getpagesize(); 386 pagesize_len = snprintf(NULL, 0, "%d", pagesize) + 1; 387 p = alloca(pagesize_len); 388 snprintf(p, pagesize_len, "%d", pagesize); 389 sectorsize_str = p; 390 } 391 392 argsize = asprintf(&args, "%s%s%s%s%s%s%s%s%s -d", 393 aflag, aalgo, eflag, ealgo, lflag, keylen_str, Tflag, 394 sflag, sectorsize_str); 395 396 free(ops); 397 return (args); 398 } 399 400 static const char * 401 swap_on_off_geli(const char *name, char *mntops, int doingall) 402 { 403 struct stat sb; 404 char *dname, *args; 405 int error; 406 407 error = stat(name, &sb); 408 409 if (which_prog == SWAPON) do { 410 /* Skip if the .eli device already exists. */ 411 if (error == 0) 412 break; 413 414 args = swap_on_geli_args(mntops); 415 if (args == NULL) 416 return (NULL); 417 418 dname = swap_basename(name); 419 if (dname == NULL) { 420 free(args); 421 return (NULL); 422 } 423 424 error = run_cmd(NULL, "%s onetime%s %s", _PATH_GELI, args, 425 dname); 426 427 free(dname); 428 free(args); 429 430 if (error) { 431 /* error occured during creation. */ 432 if (qflag == 0) 433 warnx("%s: Invalid parameters", name); 434 return (NULL); 435 } 436 } while (0); 437 438 return (swap_on_off_sfile(name, doingall)); 439 } 440 441 static const char * 442 swap_on_off_md(const char *name, char *mntops, int doingall) 443 { 444 FILE *sfd; 445 int fd, mdunit, error; 446 const char *ret; 447 static char mdpath[PATH_MAX], linebuf[PATH_MAX]; 448 char *p, *vnodefile; 449 size_t linelen; 450 u_long ul; 451 452 fd = -1; 453 sfd = NULL; 454 if (strlen(name) == (sizeof(MD_NAME) - 1)) 455 mdunit = -1; 456 else { 457 errno = 0; 458 ul = strtoul(name + 2, &p, 10); 459 if (errno == 0) { 460 if (*p != '\0' || ul > INT_MAX) 461 errno = EINVAL; 462 } 463 if (errno) { 464 warn("Bad device unit: %s", name); 465 return (NULL); 466 } 467 mdunit = (int)ul; 468 } 469 470 vnodefile = NULL; 471 if ((p = strstr(mntops, "file=")) != NULL) { 472 vnodefile = strdup(p + sizeof("file=") - 1); 473 p = strchr(vnodefile, ','); 474 if (p != NULL) 475 *p = '\0'; 476 } 477 if (vnodefile == NULL) { 478 warnx("file option not found for %s", name); 479 return (NULL); 480 } 481 482 if (which_prog == SWAPON) { 483 if (mdunit == -1) { 484 error = run_cmd(&fd, "%s -l -n -f %s", 485 _PATH_MDCONFIG, vnodefile); 486 if (error == 0) { 487 /* md device found. Ignore it. */ 488 close(fd); 489 if (!qflag) 490 warnx("%s: Device already in use", 491 vnodefile); 492 free(vnodefile); 493 return (NULL); 494 } 495 error = run_cmd(&fd, "%s -a -t vnode -n -f %s", 496 _PATH_MDCONFIG, vnodefile); 497 if (error) { 498 warnx("mdconfig (attach) error: file=%s", 499 vnodefile); 500 free(vnodefile); 501 return (NULL); 502 } 503 sfd = fdopen(fd, "r"); 504 if (sfd == NULL) { 505 warn("mdconfig (attach) fdopen error"); 506 ret = NULL; 507 goto err; 508 } 509 p = fgetln(sfd, &linelen); 510 if (p == NULL && 511 (linelen < 2 || linelen > sizeof(linebuf))) { 512 warn("mdconfig (attach) unexpected output"); 513 ret = NULL; 514 goto err; 515 } 516 strncpy(linebuf, p, linelen); 517 linebuf[linelen - 1] = '\0'; 518 errno = 0; 519 ul = strtoul(linebuf, &p, 10); 520 if (errno == 0) { 521 if (*p != '\0' || ul > INT_MAX) 522 errno = EINVAL; 523 } 524 if (errno) { 525 warn("mdconfig (attach) unexpected output: %s", 526 linebuf); 527 ret = NULL; 528 goto err; 529 } 530 mdunit = (int)ul; 531 } else { 532 error = run_cmd(&fd, "%s -l -n -f %s -u %d", 533 _PATH_MDCONFIG, vnodefile, mdunit); 534 if (error == 0) { 535 /* md device found. Ignore it. */ 536 close(fd); 537 if (qflag == 0) 538 warnx("md%d on %s: Device already " 539 "in use", mdunit, vnodefile); 540 free(vnodefile); 541 return (NULL); 542 } 543 error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s", 544 _PATH_MDCONFIG, mdunit, vnodefile); 545 if (error) { 546 warnx("mdconfig (attach) error: " 547 "md%d on file=%s", mdunit, vnodefile); 548 free(vnodefile); 549 return (NULL); 550 } 551 } 552 } else /* SWAPOFF */ { 553 if (mdunit == -1) { 554 error = run_cmd(&fd, "%s -l -n -f %s", 555 _PATH_MDCONFIG, vnodefile); 556 if (error) { 557 /* md device not found. Ignore it. */ 558 close(fd); 559 if (!qflag) 560 warnx("md on %s: Device not found", 561 vnodefile); 562 free(vnodefile); 563 return (NULL); 564 } 565 sfd = fdopen(fd, "r"); 566 if (sfd == NULL) { 567 warn("mdconfig (list) fdopen error"); 568 ret = NULL; 569 goto err; 570 } 571 p = fgetln(sfd, &linelen); 572 if (p == NULL && 573 (linelen < 2 || linelen > sizeof(linebuf) - 1)) { 574 warn("mdconfig (list) unexpected output"); 575 ret = NULL; 576 goto err; 577 } 578 strncpy(linebuf, p, linelen); 579 linebuf[linelen - 1] = '\0'; 580 p = strchr(linebuf, ' '); 581 if (p != NULL) 582 *p = '\0'; 583 errno = 0; 584 ul = strtoul(linebuf, &p, 10); 585 if (errno == 0) { 586 if (*p != '\0' || ul > INT_MAX) 587 errno = EINVAL; 588 } 589 if (errno) { 590 warn("mdconfig (list) unexpected output: %s", 591 linebuf); 592 ret = NULL; 593 goto err; 594 } 595 mdunit = (int)ul; 596 } else { 597 error = run_cmd(&fd, "%s -l -n -f %s -u %d", 598 _PATH_MDCONFIG, vnodefile, mdunit); 599 if (error) { 600 /* md device not found. Ignore it. */ 601 close(fd); 602 if (!qflag) 603 warnx("md%d on %s: Device not found", 604 mdunit, vnodefile); 605 free(vnodefile); 606 return (NULL); 607 } 608 } 609 } 610 snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV, 611 MD_NAME, mdunit); 612 mdpath[sizeof(mdpath) - 1] = '\0'; 613 ret = swap_on_off_sfile(mdpath, doingall); 614 615 if (which_prog == SWAPOFF) { 616 if (ret != NULL) { 617 error = run_cmd(NULL, "%s -d -u %d", 618 _PATH_MDCONFIG, mdunit); 619 if (error) 620 warn("mdconfig (detach) detach failed: %s%s%d", 621 _PATH_DEV, MD_NAME, mdunit); 622 } 623 } 624 err: 625 if (sfd != NULL) 626 fclose(sfd); 627 if (fd != -1) 628 close(fd); 629 free(vnodefile); 630 return (ret); 631 } 632 633 static int 634 run_cmd(int *ofd, const char *cmdline, ...) 635 { 636 va_list ap; 637 char **argv, **argvp, *cmd, *p; 638 int argc, pid, status, rv; 639 int pfd[2], nfd, dup2dn; 640 641 va_start(ap, cmdline); 642 rv = vasprintf(&cmd, cmdline, ap); 643 if (rv == -1) { 644 warn("%s", __func__); 645 return (rv); 646 } 647 va_end(ap); 648 649 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++) 650 argc++; 651 argv = (char **)malloc(sizeof(*argv) * (argc + 1)); 652 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;) 653 if (**argvp != '\0' && (++argvp > &argv[argc])) { 654 *argvp = NULL; 655 break; 656 } 657 /* The argv array ends up NULL-terminated here. */ 658 #if 0 659 { 660 int i; 661 662 fprintf(stderr, "DEBUG: running:"); 663 /* Should be equivalent to 'cmd' (before strsep, of course). */ 664 for (i = 0; argv[i] != NULL; i++) 665 fprintf(stderr, " %s", argv[i]); 666 fprintf(stderr, "\n"); 667 } 668 #endif 669 dup2dn = 1; 670 if (ofd != NULL) { 671 if (pipe(&pfd[0]) == -1) { 672 warn("%s: pipe", __func__); 673 return (-1); 674 } 675 *ofd = pfd[0]; 676 dup2dn = 0; 677 } 678 pid = fork(); 679 switch (pid) { 680 case 0: 681 /* Child process. */ 682 if (ofd != NULL) 683 if (dup2(pfd[1], STDOUT_FILENO) < 0) 684 err(1, "dup2 in %s", __func__); 685 nfd = open(_PATH_DEVNULL, O_RDWR); 686 if (nfd == -1) 687 err(1, "%s: open %s", __func__, _PATH_DEVNULL); 688 if (dup2(nfd, STDIN_FILENO) < 0) 689 err(1, "%s: dup2", __func__); 690 if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0) 691 err(1, "%s: dup2", __func__); 692 if (dup2(nfd, STDERR_FILENO) < 0) 693 err(1, "%s: dup2", __func__); 694 execv(argv[0], argv); 695 warn("exec: %s", argv[0]); 696 _exit(-1); 697 case -1: 698 err(1, "%s: fork", __func__); 699 } 700 free(cmd); 701 free(argv); 702 while (waitpid(pid, &status, 0) != pid) 703 ; 704 return (WEXITSTATUS(status)); 705 } 706 707 static const char * 708 swap_on_off_sfile(const char *name, int doingall) 709 { 710 int error; 711 712 if (which_prog == SWAPON) 713 error = swapon(name); 714 else /* SWAPOFF */ 715 error = swapoff(name); 716 717 if (error == -1) { 718 switch (errno) { 719 case EBUSY: 720 if (doingall == 0) 721 warnx("%s: Device already in use", name); 722 break; 723 case EINVAL: 724 if (which_prog == SWAPON) 725 warnx("%s: NSWAPDEV limit reached", name); 726 else if (doingall == 0) 727 warn("%s", name); 728 break; 729 default: 730 warn("%s", name); 731 break; 732 } 733 return (NULL); 734 } 735 return (name); 736 } 737 738 static void 739 usage(void) 740 { 741 742 fprintf(stderr, "usage: %s ", getprogname()); 743 switch(orig_prog) { 744 case SWAPON: 745 case SWAPOFF: 746 fprintf(stderr, "[-F fstab] -aLq | file ...\n"); 747 break; 748 case SWAPCTL: 749 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n"); 750 break; 751 } 752 exit(1); 753 } 754 755 static void 756 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen, 757 long blocksize) 758 { 759 char tmp[16]; 760 761 if (hflag == 'H') { 762 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE, 763 HN_B | HN_NOSPACE | HN_DECIMAL); 764 snprintf(buf, bufsize, "%*s", hlen, tmp); 765 } else 766 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize); 767 } 768 769 static void 770 swaplist(int lflag, int sflag, int hflag) 771 { 772 size_t mibsize, size; 773 struct xswdev xsw; 774 int hlen, mib[16], n, pagesize; 775 long blocksize; 776 long long total = 0; 777 long long used = 0; 778 long long tmp_total; 779 long long tmp_used; 780 char buf[32]; 781 782 pagesize = getpagesize(); 783 switch(hflag) { 784 case 'G': 785 blocksize = 1024 * 1024 * 1024; 786 strlcpy(buf, "1GB-blocks", sizeof(buf)); 787 hlen = 10; 788 break; 789 case 'H': 790 blocksize = -1; 791 strlcpy(buf, "Bytes", sizeof(buf)); 792 hlen = 10; 793 break; 794 case 'K': 795 blocksize = 1024; 796 strlcpy(buf, "1kB-blocks", sizeof(buf)); 797 hlen = 10; 798 break; 799 case 'M': 800 blocksize = 1024 * 1024; 801 strlcpy(buf, "1MB-blocks", sizeof(buf)); 802 hlen = 10; 803 break; 804 default: 805 getbsize(&hlen, &blocksize); 806 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); 807 break; 808 } 809 810 mibsize = nitems(mib); 811 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 812 err(1, "sysctlnametomib()"); 813 814 if (lflag) { 815 printf("%-13s %*s %*s\n", 816 "Device:", 817 hlen, buf, 818 hlen, "Used:"); 819 } 820 821 for (n = 0; ; ++n) { 822 mib[mibsize] = n; 823 size = sizeof xsw; 824 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 825 break; 826 if (xsw.xsw_version != XSWDEV_VERSION) 827 errx(1, "xswdev version mismatch"); 828 829 tmp_total = (long long)xsw.xsw_nblks * pagesize; 830 tmp_used = (long long)xsw.xsw_used * pagesize; 831 total += tmp_total; 832 used += tmp_used; 833 if (lflag) { 834 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen, 835 blocksize); 836 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR), 837 buf); 838 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen, 839 blocksize); 840 printf("%s\n", buf); 841 } 842 } 843 if (errno != ENOENT) 844 err(1, "sysctl()"); 845 846 if (sflag) { 847 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize); 848 printf("Total: %s ", buf); 849 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize); 850 printf("%s\n", buf); 851 } 852 } 853 854