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