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