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