1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 24 * Copyright 2020 Joyent, Inc. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 #include <sys/swap.h> 30 #include <sys/dumpadm.h> 31 #include <sys/utsname.h> 32 33 #include <unistd.h> 34 #include <string.h> 35 #include <stdlib.h> 36 #include <stdio.h> 37 #include <fcntl.h> 38 #include <errno.h> 39 #include <libdiskmgt.h> 40 #include <libzfs.h> 41 #include <libzutil.h> 42 #include <uuid/uuid.h> 43 44 #include "dconf.h" 45 #include "minfree.h" 46 #include "utils.h" 47 #include "swap.h" 48 49 typedef struct dc_token { 50 const char *tok_name; 51 int (*tok_parse)(dumpconf_t *, char *); 52 int (*tok_print)(const dumpconf_t *, FILE *); 53 } dc_token_t; 54 55 56 static int print_device(const dumpconf_t *, FILE *); 57 static int print_savdir(const dumpconf_t *, FILE *); 58 static int print_content(const dumpconf_t *, FILE *); 59 static int print_enable(const dumpconf_t *, FILE *); 60 static int print_csave(const dumpconf_t *, FILE *); 61 62 static const dc_token_t tokens[] = { 63 { "DUMPADM_DEVICE", dconf_str2device, print_device }, 64 { "DUMPADM_SAVDIR", dconf_str2savdir, print_savdir }, 65 { "DUMPADM_CONTENT", dconf_str2content, print_content }, 66 { "DUMPADM_ENABLE", dconf_str2enable, print_enable }, 67 { "DUMPADM_CSAVE", dconf_str2csave, print_csave }, 68 { NULL, NULL, NULL } 69 }; 70 71 static const char DC_STR_ON[] = "on"; /* On string */ 72 static const char DC_STR_OFF[] = "off"; /* Off string */ 73 static const char DC_STR_YES[] = "yes"; /* Enable on string */ 74 static const char DC_STR_NO[] = "no"; /* Enable off string */ 75 static const char DC_STR_SWAP[] = "swap"; /* Default dump device */ 76 static const char DC_STR_NONE[] = "none"; 77 78 /* The pages included in the dump */ 79 static const char DC_STR_KERNEL[] = "kernel"; /* Kernel only */ 80 static const char DC_STR_CURPROC[] = "curproc"; /* Kernel + current process */ 81 static const char DC_STR_ALL[] = "all"; /* All pages */ 82 83 /* 84 * Permissions and ownership for the configuration file: 85 */ 86 #define DC_OWNER 0 /* Uid 0 (root) */ 87 #define DC_GROUP 1 /* Gid 1 (other) */ 88 #define DC_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* Mode 0644 */ 89 90 static void 91 dconf_init(dumpconf_t *dcp, int dcmode) 92 { 93 struct utsname ut; 94 95 /* 96 * Default device for dumps is 'swap' (appropriate swap device), 97 * and default savecore directory is /var/crash/`uname -n`, 98 * which is compatible with pre-dumpadm behavior. 99 */ 100 (void) strcpy(dcp->dc_device, DC_STR_SWAP); 101 (void) strcpy(dcp->dc_savdir, "/var/crash"); 102 103 if (uname(&ut) != -1) { 104 (void) strcat(dcp->dc_savdir, "/"); 105 (void) strcat(dcp->dc_savdir, ut.nodename); 106 } 107 108 /* 109 * Default is contents kernel, savecore enabled on reboot, 110 * savecore saves compressed core files. 111 */ 112 dcp->dc_cflags = DUMP_KERNEL; 113 dcp->dc_enable = DC_ON; 114 dcp->dc_csave = DC_COMPRESSED; 115 116 dcp->dc_mode = dcmode; 117 dcp->dc_conf_fp = NULL; 118 dcp->dc_conf_fd = -1; 119 dcp->dc_dump_fd = -1; 120 dcp->dc_readonly = B_FALSE; 121 } 122 123 int 124 dconf_open(dumpconf_t *dcp, const char *dpath, const char *fpath, int dcmode) 125 { 126 char buf[BUFSIZ]; 127 int line; 128 const char *fpmode = "r+"; 129 130 dconf_init(dcp, dcmode); 131 132 if ((dcp->dc_dump_fd = open(dpath, O_RDWR)) == -1) { 133 warn(gettext("failed to open %s"), dpath); 134 return (-1); 135 } 136 137 if ((dcp->dc_conf_fd = open(fpath, O_RDWR | O_CREAT, DC_PERM)) == -1) { 138 /* 139 * Attempt to open the file read-only. 140 */ 141 if ((dcp->dc_conf_fd = open(fpath, O_RDONLY)) == -1) { 142 warn(gettext("failed to open %s"), fpath); 143 return (-1); 144 } 145 146 dcp->dc_readonly = B_TRUE; 147 fpmode = "r"; 148 } 149 150 if ((dcp->dc_conf_fp = fdopen(dcp->dc_conf_fd, fpmode)) == NULL) { 151 warn(gettext("failed to open stream for %s"), fpath); 152 return (-1); 153 } 154 155 /* 156 * If we're in override mode, the current kernel settings override the 157 * default settings and anything invalid in the configuration file. 158 */ 159 if (dcmode == DC_OVERRIDE) 160 (void) dconf_getdev(dcp); 161 162 for (line = 1; fgets(buf, BUFSIZ, dcp->dc_conf_fp) != NULL; line++) { 163 164 char name[BUFSIZ], value[BUFSIZ]; 165 const dc_token_t *tokp; 166 int len; 167 168 if (buf[0] == '#' || buf[0] == '\n') 169 continue; 170 171 /* 172 * Look for "name=value", with optional whitespace on either 173 * side, terminated by a newline, and consuming the whole line. 174 */ 175 /* LINTED - unbounded string specifier */ 176 if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 && 177 name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) { 178 /* 179 * Locate a matching token in the tokens[] table, 180 * and invoke its parsing function. 181 */ 182 for (tokp = tokens; tokp->tok_name != NULL; tokp++) { 183 if (strcmp(name, tokp->tok_name) == 0) { 184 if (tokp->tok_parse(dcp, value) == -1) { 185 warn(gettext("\"%s\", line %d: " 186 "warning: invalid %s\n"), 187 fpath, line, name); 188 } 189 break; 190 } 191 } 192 193 /* 194 * If we hit the end of the tokens[] table, 195 * no matching token was found. 196 */ 197 if (tokp->tok_name == NULL) { 198 warn(gettext("\"%s\", line %d: warning: " 199 "invalid token: %s\n"), fpath, line, name); 200 } 201 202 } else { 203 warn(gettext("\"%s\", line %d: syntax error\n"), 204 fpath, line); 205 } 206 } 207 208 /* 209 * If we're not in override mode, the current kernel settings 210 * override the settings read from the configuration file. 211 */ 212 if (dcmode == DC_CURRENT) 213 return (dconf_getdev(dcp)); 214 215 return (0); 216 } 217 218 int 219 dconf_getdev(dumpconf_t *dcp) 220 { 221 int status = 0; 222 223 if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) { 224 warn(gettext("failed to get kernel dump settings")); 225 status = -1; 226 } 227 228 if (ioctl(dcp->dc_dump_fd, DIOCGETDEV, dcp->dc_device) == -1) { 229 if (errno != ENODEV) { 230 warn(gettext("failed to get dump device")); 231 status = -1; 232 } else 233 dcp->dc_device[0] = '\0'; 234 } 235 236 return (status); 237 } 238 239 int 240 dconf_close(dumpconf_t *dcp) 241 { 242 if (fclose(dcp->dc_conf_fp) == 0) { 243 (void) close(dcp->dc_dump_fd); 244 return (0); 245 } 246 return (-1); 247 } 248 249 int 250 dconf_write(dumpconf_t *dcp) 251 { 252 const dc_token_t *tokp; 253 254 if (fseeko(dcp->dc_conf_fp, (off_t)0, SEEK_SET) == -1) { 255 warn(gettext("failed to seek config file")); 256 return (-1); 257 } 258 259 if (ftruncate(dcp->dc_conf_fd, (off_t)0) == -1) { 260 warn(gettext("failed to truncate config file")); 261 return (-1); 262 } 263 264 (void) fputs("#\n# dumpadm.conf\n#\n" 265 "# Configuration parameters for system crash dump.\n" 266 "# Do NOT edit this file by hand -- use dumpadm(8) instead.\n" 267 "#\n", dcp->dc_conf_fp); 268 269 for (tokp = tokens; tokp->tok_name != NULL; tokp++) { 270 if (fprintf(dcp->dc_conf_fp, "%s=", tokp->tok_name) == -1 || 271 tokp->tok_print(dcp, dcp->dc_conf_fp) == -1) { 272 warn(gettext("failed to write token")); 273 return (-1); 274 } 275 } 276 277 if (fflush(dcp->dc_conf_fp) != 0) 278 warn(gettext("warning: failed to flush config file")); 279 280 if (fsync(dcp->dc_conf_fd) == -1) 281 warn(gettext("warning: failed to sync config file to disk")); 282 283 if (fchmod(dcp->dc_conf_fd, DC_PERM) == -1) 284 warn(gettext("warning: failed to reset mode on config file")); 285 286 if (fchown(dcp->dc_conf_fd, DC_OWNER, DC_GROUP) == -1) 287 warn(gettext("warning: failed to reset owner on config file")); 288 289 return (0); 290 } 291 292 static int 293 open_stat64(const char *path, struct stat64 *stp) 294 { 295 int fd = open64(path, O_RDONLY); 296 297 if (fd >= 0) { 298 int status = fstat64(fd, stp); 299 (void) close(fd); 300 return (status); 301 } 302 303 return (-1); 304 } 305 306 static int 307 dconf_swap_compare(const swapent_t *s1, const swapent_t *s2) 308 { 309 struct stat64 st1, st2; 310 311 int prefer_s1 = -1; /* Return value to move s1 left (s1 < s2) */ 312 int prefer_s2 = 1; /* Return value to move s2 left (s1 > s2) */ 313 314 /* 315 * First try: open and fstat each swap entry. If either system 316 * call fails, arbitrarily prefer the other entry. 317 */ 318 if (open_stat64(s1->ste_path, &st1) == -1) 319 return (prefer_s2); 320 321 if (open_stat64(s2->ste_path, &st2) == -1) 322 return (prefer_s1); 323 324 /* 325 * Second try: if both entries are block devices, or if 326 * neither is a block device, prefer the larger. 327 */ 328 if (S_ISBLK(st1.st_mode) == S_ISBLK(st2.st_mode)) { 329 if (st2.st_size > st1.st_size) 330 return (prefer_s2); 331 return (prefer_s1); 332 } 333 334 /* 335 * Third try: prefer the entry that is a block device. 336 */ 337 if (S_ISBLK(st2.st_mode)) 338 return (prefer_s2); 339 return (prefer_s1); 340 } 341 342 static int 343 dconf_dev_ioctl(dumpconf_t *dcp, int cmd) 344 { 345 if (ioctl(dcp->dc_dump_fd, cmd, dcp->dc_device) == 0) 346 return (0); 347 348 switch (errno) { 349 case ENOTSUP: 350 warn(gettext("dumps not supported on %s\n"), dcp->dc_device); 351 break; 352 case EBUSY: 353 warn(gettext("device %s is already in use\n"), dcp->dc_device); 354 break; 355 case EBADR: 356 /* ZFS pool is too fragmented to support a dump device */ 357 warn(gettext("device %s is too fragmented to be used as " 358 "a dump device\n"), dcp->dc_device); 359 break; 360 default: 361 /* 362 * NOTE: The stmsboot(8) command's boot-up script parses this 363 * error to get the dump device name. If you change the format 364 * of this message, make sure that stmsboot(8) is in sync. 365 */ 366 warn(gettext("cannot use %s as dump device"), dcp->dc_device); 367 } 368 return (-1); 369 } 370 371 int 372 dconf_update(dumpconf_t *dcp, int checkinuse) 373 { 374 int oconf; 375 int error; 376 char *msg; 377 378 error = 0; 379 380 if (checkinuse && (dm_inuse(dcp->dc_device, &msg, DM_WHO_DUMP, 381 &error) || error)) { 382 if (error != 0) { 383 warn(gettext("failed to determine if %s is" 384 " in use"), dcp->dc_device); 385 } else { 386 warn(msg); 387 free(msg); 388 return (-1); 389 } 390 } 391 392 /* 393 * Save the existing dump configuration in case something goes wrong. 394 */ 395 if ((oconf = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) { 396 warn(gettext("failed to get kernel dump configuration")); 397 return (-1); 398 } 399 400 oconf &= DUMP_CONTENT; 401 dcp->dc_cflags &= DUMP_CONTENT; 402 403 if (ioctl(dcp->dc_dump_fd, DIOCSETCONF, dcp->dc_cflags) == -1) { 404 warn(gettext("failed to update kernel dump configuration")); 405 return (-1); 406 } 407 408 if (strcmp(dcp->dc_device, DC_STR_SWAP) == 0) { 409 swaptbl_t *swt; 410 int i; 411 412 if ((swt = swap_list()) == NULL) 413 goto err; 414 415 if (swt->swt_n == 0) { 416 warn(gettext("no swap devices are available\n")); 417 free(swt); 418 goto err; 419 } 420 421 qsort(&swt->swt_ent[0], swt->swt_n, sizeof (swapent_t), 422 (int (*)(const void *, const void *))dconf_swap_compare); 423 424 /* 425 * Iterate through the prioritized list of swap entries, 426 * trying to configure one as the dump device. 427 */ 428 for (i = 0; i < swt->swt_n; i++) { 429 if (ioctl(dcp->dc_dump_fd, DIOCSETDEV, 430 swt->swt_ent[i].ste_path) == 0) { 431 (void) strcpy(dcp->dc_device, 432 swt->swt_ent[i].ste_path); 433 break; 434 } 435 } 436 437 if (i == swt->swt_n) { 438 warn(gettext("no swap devices could be configured " 439 "as the dump device\n")); 440 free(swt); 441 goto err; 442 } 443 free(swt); 444 445 } else if (strcmp(dcp->dc_device, DC_STR_NONE) == 0) { 446 if (ioctl(dcp->dc_dump_fd, DIOCRMDEV, NULL) == -1) { 447 warn(gettext("failed to remove dump device")); 448 return (-1); 449 } 450 } else if (dcp->dc_device[0] != '\0') { 451 /* 452 * If we're not in forcible update mode, then fail the change 453 * if the selected device cannot be used as the dump device, 454 * or if it is not big enough to hold the dump. 455 */ 456 if (dcp->dc_mode == DC_CURRENT) { 457 struct stat64 st; 458 uint64_t d; 459 460 if (dconf_dev_ioctl(dcp, DIOCTRYDEV) == -1) 461 goto err; 462 463 if (open_stat64(dcp->dc_device, &st) == -1) { 464 warn(gettext("failed to access %s"), 465 dcp->dc_device); 466 goto err; 467 } 468 469 if ((error = zvol_check_dump_config( 470 dcp->dc_device)) > 0) 471 goto err; 472 if (ioctl(dcp->dc_dump_fd, DIOCGETDUMPSIZE, &d) == -1) { 473 warn(gettext("failed to get kernel dump size")); 474 goto err; 475 } 476 477 if (st.st_size < d) { 478 warn(gettext("dump device %s is too small to " 479 "hold a system dump\ndump size %llu " 480 "bytes, device size %lld bytes\n"), 481 dcp->dc_device, d, st.st_size); 482 goto err; 483 } 484 } 485 486 if (dconf_dev_ioctl(dcp, DIOCSETDEV) == -1) 487 goto err; 488 } 489 490 /* 491 * Now that we've updated the dump device, we need to issue another 492 * ioctl to re-read the config flags to determine whether we 493 * obtained DUMP_EXCL access on our dump device. 494 */ 495 if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) { 496 warn(gettext("failed to re-read kernel dump configuration")); 497 return (-1); 498 } 499 500 return (0); 501 502 err: 503 (void) ioctl(dcp->dc_dump_fd, DIOCSETCONF, oconf); 504 return (-1); 505 } 506 507 int 508 dconf_write_uuid(dumpconf_t *dcp) 509 { 510 char uuidstr[36 + 1]; 511 uuid_t uu; 512 int err; 513 514 uuid_generate(uu); 515 uuid_unparse(uu, uuidstr); 516 517 err = ioctl(dcp->dc_dump_fd, DIOCSETUUID, uuidstr); 518 519 if (err) 520 warn(gettext("kernel image uuid write failed")); 521 522 return (err == 0); 523 } 524 525 int 526 dconf_get_dumpsize(dumpconf_t *dcp) 527 { 528 char buf[32]; 529 uint64_t d; 530 531 if (ioctl(dcp->dc_dump_fd, DIOCGETDUMPSIZE, &d) == -1) { 532 warn(gettext("failed to get kernel dump size")); 533 return (-1); 534 } 535 536 zfs_nicenum(d, buf, sizeof (buf)); 537 538 (void) printf(gettext("Estimated dump size: %s\n"), buf); 539 return (0); 540 } 541 542 void 543 dconf_print(dumpconf_t *dcp, FILE *fp) 544 { 545 u_longlong_t min; 546 char *content; 547 548 if (dcp->dc_cflags & DUMP_ALL) 549 content = gettext("all"); 550 else if (dcp->dc_cflags & DUMP_CURPROC) 551 content = gettext("kernel and current process"); 552 else 553 content = gettext("kernel"); 554 555 (void) fprintf(fp, gettext(" Dump content: %s pages\n"), content); 556 557 if (dcp->dc_device[0] != '\0') { 558 (void) fprintf(fp, gettext(" Dump device: %s (%s)\n"), 559 dcp->dc_device, (dcp->dc_cflags & DUMP_EXCL) ? 560 gettext("dedicated") : gettext("swap")); 561 } else { 562 (void) fprintf(fp, gettext(" Dump device: none " 563 "(dumps disabled)\n")); 564 } 565 566 (void) fprintf(fp, gettext("Savecore directory: %s"), dcp->dc_savdir); 567 568 if (minfree_read(dcp->dc_savdir, &min) == 0) { 569 if (min < 1024 || (min % 1024) != 0) 570 (void) fprintf(fp, gettext(" (minfree = %lluKB)"), min); 571 else 572 (void) fprintf(fp, gettext(" (minfree = %lluMB)"), 573 min / 1024); 574 } 575 576 (void) fprintf(fp, gettext("\n")); 577 578 (void) fprintf(fp, gettext(" Savecore enabled: %s\n"), 579 (dcp->dc_enable == DC_OFF) ? gettext("no") : gettext("yes")); 580 (void) fprintf(fp, gettext(" Save compressed: %s\n"), 581 (dcp->dc_csave == DC_UNCOMPRESSED) ? gettext("off") : 582 gettext("on")); 583 } 584 585 int 586 dconf_str2device(dumpconf_t *dcp, char *buf) 587 { 588 if (strcasecmp(buf, DC_STR_SWAP) == 0) { 589 (void) strcpy(dcp->dc_device, DC_STR_SWAP); 590 return (0); 591 } 592 593 if (strcasecmp(buf, DC_STR_NONE) == 0) { 594 (void) strcpy(dcp->dc_device, DC_STR_NONE); 595 return (0); 596 } 597 598 if (valid_abspath(buf)) { 599 (void) strcpy(dcp->dc_device, buf); 600 return (0); 601 } 602 603 return (-1); 604 } 605 606 int 607 dconf_str2savdir(dumpconf_t *dcp, char *buf) 608 { 609 if (valid_abspath(buf)) { 610 (void) strcpy(dcp->dc_savdir, buf); 611 return (0); 612 } 613 614 return (-1); 615 } 616 617 int 618 dconf_str2content(dumpconf_t *dcp, char *buf) 619 { 620 if (strcasecmp(buf, DC_STR_KERNEL) == 0) { 621 dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_KERNEL; 622 return (0); 623 } 624 625 if (strcasecmp(buf, DC_STR_CURPROC) == 0) { 626 dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | 627 DUMP_CURPROC; 628 return (0); 629 } 630 631 if (strcasecmp(buf, DC_STR_ALL) == 0) { 632 dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_ALL; 633 return (0); 634 } 635 636 warn(gettext("invalid dump content type -- %s\n"), buf); 637 return (-1); 638 } 639 640 int 641 dconf_str2enable(dumpconf_t *dcp, char *buf) 642 { 643 if (strcasecmp(buf, DC_STR_YES) == 0) { 644 dcp->dc_enable = DC_ON; 645 return (0); 646 } 647 648 if (strcasecmp(buf, DC_STR_NO) == 0) { 649 dcp->dc_enable = DC_OFF; 650 return (0); 651 } 652 653 warn(gettext("invalid enable value -- %s\n"), buf); 654 return (-1); 655 } 656 657 int 658 dconf_str2csave(dumpconf_t *dcp, char *buf) 659 { 660 if (strcasecmp(buf, DC_STR_ON) == 0) { 661 dcp->dc_csave = DC_COMPRESSED; 662 return (0); 663 } 664 665 if (strcasecmp(buf, DC_STR_OFF) == 0) { 666 dcp->dc_csave = DC_UNCOMPRESSED; 667 return (0); 668 } 669 670 warn(gettext("invalid save compressed value -- %s\n"), buf); 671 return (-1); 672 } 673 674 static int 675 print_content(const dumpconf_t *dcp, FILE *fp) 676 { 677 const char *content; 678 679 if (dcp->dc_cflags & DUMP_ALL) 680 content = DC_STR_ALL; 681 else if (dcp->dc_cflags & DUMP_CURPROC) 682 content = DC_STR_CURPROC; 683 else 684 content = DC_STR_KERNEL; 685 686 return (fprintf(fp, "%s\n", content)); 687 } 688 689 static int 690 print_device(const dumpconf_t *dcp, FILE *fp) 691 { 692 return (fprintf(fp, "%s\n", (dcp->dc_device[0] != '\0') ? 693 dcp->dc_device : DC_STR_SWAP)); 694 } 695 696 static int 697 print_enable(const dumpconf_t *dcp, FILE *fp) 698 { 699 return (fprintf(fp, "%s\n", (dcp->dc_enable == DC_OFF) ? 700 DC_STR_NO : DC_STR_YES)); 701 } 702 703 static int 704 print_csave(const dumpconf_t *dcp, FILE *fp) 705 { 706 return (fprintf(fp, "%s\n", (dcp->dc_csave == DC_COMPRESSED) ? 707 DC_STR_ON : DC_STR_OFF)); 708 } 709 710 static int 711 print_savdir(const dumpconf_t *dcp, FILE *fp) 712 { 713 return (fprintf(fp, "%s\n", dcp->dc_savdir)); 714 } 715