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.
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.
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]
22 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 #include <sys/types.h>
29 #include <sys/dumpadm.h>
30 #include <sys/utsname.h>
38 #include <libdiskmgt.h>
40 #include <uuid/uuid.h>
47 typedef struct dc_token
{
49 int (*tok_parse
)(dumpconf_t
*, char *);
50 int (*tok_print
)(const dumpconf_t
*, FILE *);
54 static int print_device(const dumpconf_t
*, FILE *);
55 static int print_savdir(const dumpconf_t
*, FILE *);
56 static int print_content(const dumpconf_t
*, FILE *);
57 static int print_enable(const dumpconf_t
*, FILE *);
58 static int print_csave(const dumpconf_t
*, FILE *);
60 static const dc_token_t tokens
[] = {
61 { "DUMPADM_DEVICE", dconf_str2device
, print_device
},
62 { "DUMPADM_SAVDIR", dconf_str2savdir
, print_savdir
},
63 { "DUMPADM_CONTENT", dconf_str2content
, print_content
},
64 { "DUMPADM_ENABLE", dconf_str2enable
, print_enable
},
65 { "DUMPADM_CSAVE", dconf_str2csave
, print_csave
},
69 static const char DC_STR_ON
[] = "on"; /* On string */
70 static const char DC_STR_OFF
[] = "off"; /* Off string */
71 static const char DC_STR_YES
[] = "yes"; /* Enable on string */
72 static const char DC_STR_NO
[] = "no"; /* Enable off string */
73 static const char DC_STR_SWAP
[] = "swap"; /* Default dump device */
74 static const char DC_STR_NONE
[] = "none";
76 /* The pages included in the dump */
77 static const char DC_STR_KERNEL
[] = "kernel"; /* Kernel only */
78 static const char DC_STR_CURPROC
[] = "curproc"; /* Kernel + current process */
79 static const char DC_STR_ALL
[] = "all"; /* All pages */
82 * Permissions and ownership for the configuration file:
84 #define DC_OWNER 0 /* Uid 0 (root) */
85 #define DC_GROUP 1 /* Gid 1 (other) */
86 #define DC_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* Mode 0644 */
89 dconf_init(dumpconf_t
*dcp
, int dcmode
)
94 * Default device for dumps is 'swap' (appropriate swap device),
95 * and default savecore directory is /var/crash/`uname -n`,
96 * which is compatible with pre-dumpadm behavior.
98 (void) strcpy(dcp
->dc_device
, DC_STR_SWAP
);
99 (void) strcpy(dcp
->dc_savdir
, "/var/crash");
101 if (uname(&ut
) != -1) {
102 (void) strcat(dcp
->dc_savdir
, "/");
103 (void) strcat(dcp
->dc_savdir
, ut
.nodename
);
107 * Default is contents kernel, savecore enabled on reboot,
108 * savecore saves compressed core files.
110 dcp
->dc_cflags
= DUMP_KERNEL
;
111 dcp
->dc_enable
= DC_ON
;
112 dcp
->dc_csave
= DC_COMPRESSED
;
114 dcp
->dc_mode
= dcmode
;
115 dcp
->dc_conf_fp
= NULL
;
116 dcp
->dc_conf_fd
= -1;
117 dcp
->dc_dump_fd
= -1;
118 dcp
->dc_readonly
= B_FALSE
;
122 dconf_open(dumpconf_t
*dcp
, const char *dpath
, const char *fpath
, int dcmode
)
126 const char *fpmode
= "r+";
128 dconf_init(dcp
, dcmode
);
130 if ((dcp
->dc_dump_fd
= open(dpath
, O_RDWR
)) == -1) {
131 warn(gettext("failed to open %s"), dpath
);
135 if ((dcp
->dc_conf_fd
= open(fpath
, O_RDWR
| O_CREAT
, DC_PERM
)) == -1) {
137 * Attempt to open the file read-only.
139 if ((dcp
->dc_conf_fd
= open(fpath
, O_RDONLY
)) == -1) {
140 warn(gettext("failed to open %s"), fpath
);
144 dcp
->dc_readonly
= B_TRUE
;
148 if ((dcp
->dc_conf_fp
= fdopen(dcp
->dc_conf_fd
, fpmode
)) == NULL
) {
149 warn(gettext("failed to open stream for %s"), fpath
);
154 * If we're in override mode, the current kernel settings override the
155 * default settings and anything invalid in the configuration file.
157 if (dcmode
== DC_OVERRIDE
)
158 (void) dconf_getdev(dcp
);
160 for (line
= 1; fgets(buf
, BUFSIZ
, dcp
->dc_conf_fp
) != NULL
; line
++) {
162 char name
[BUFSIZ
], value
[BUFSIZ
];
163 const dc_token_t
*tokp
;
166 if (buf
[0] == '#' || buf
[0] == '\n')
170 * Look for "name=value", with optional whitespace on either
171 * side, terminated by a newline, and consuming the whole line.
173 /* LINTED - unbounded string specifier */
174 if (sscanf(buf
, " %[^=]=%s \n%n", name
, value
, &len
) == 2 &&
175 name
[0] != '\0' && value
[0] != '\0' && len
== strlen(buf
)) {
177 * Locate a matching token in the tokens[] table,
178 * and invoke its parsing function.
180 for (tokp
= tokens
; tokp
->tok_name
!= NULL
; tokp
++) {
181 if (strcmp(name
, tokp
->tok_name
) == 0) {
182 if (tokp
->tok_parse(dcp
, value
) == -1) {
183 warn(gettext("\"%s\", line %d: "
184 "warning: invalid %s\n"),
192 * If we hit the end of the tokens[] table,
193 * no matching token was found.
195 if (tokp
->tok_name
== NULL
) {
196 warn(gettext("\"%s\", line %d: warning: "
197 "invalid token: %s\n"), fpath
, line
, name
);
201 warn(gettext("\"%s\", line %d: syntax error\n"),
207 * If we're not in override mode, the current kernel settings
208 * override the settings read from the configuration file.
210 if (dcmode
== DC_CURRENT
)
211 return (dconf_getdev(dcp
));
217 dconf_getdev(dumpconf_t
*dcp
)
221 if ((dcp
->dc_cflags
= ioctl(dcp
->dc_dump_fd
, DIOCGETCONF
, 0)) == -1) {
222 warn(gettext("failed to get kernel dump settings"));
226 if (ioctl(dcp
->dc_dump_fd
, DIOCGETDEV
, dcp
->dc_device
) == -1) {
227 if (errno
!= ENODEV
) {
228 warn(gettext("failed to get dump device"));
231 dcp
->dc_device
[0] = '\0';
238 dconf_close(dumpconf_t
*dcp
)
240 if (fclose(dcp
->dc_conf_fp
) == 0) {
241 (void) close(dcp
->dc_dump_fd
);
248 dconf_write(dumpconf_t
*dcp
)
250 const dc_token_t
*tokp
;
252 if (fseeko(dcp
->dc_conf_fp
, (off_t
)0, SEEK_SET
) == -1) {
253 warn(gettext("failed to seek config file"));
257 if (ftruncate(dcp
->dc_conf_fd
, (off_t
)0) == -1) {
258 warn(gettext("failed to truncate config file"));
262 (void) fputs("#\n# dumpadm.conf\n#\n"
263 "# Configuration parameters for system crash dump.\n"
264 "# Do NOT edit this file by hand -- use dumpadm(1m) instead.\n"
265 "#\n", dcp
->dc_conf_fp
);
267 for (tokp
= tokens
; tokp
->tok_name
!= NULL
; tokp
++) {
268 if (fprintf(dcp
->dc_conf_fp
, "%s=", tokp
->tok_name
) == -1 ||
269 tokp
->tok_print(dcp
, dcp
->dc_conf_fp
) == -1) {
270 warn(gettext("failed to write token"));
275 if (fflush(dcp
->dc_conf_fp
) != 0)
276 warn(gettext("warning: failed to flush config file"));
278 if (fsync(dcp
->dc_conf_fd
) == -1)
279 warn(gettext("warning: failed to sync config file to disk"));
281 if (fchmod(dcp
->dc_conf_fd
, DC_PERM
) == -1)
282 warn(gettext("warning: failed to reset mode on config file"));
284 if (fchown(dcp
->dc_conf_fd
, DC_OWNER
, DC_GROUP
) == -1)
285 warn(gettext("warning: failed to reset owner on config file"));
291 open_stat64(const char *path
, struct stat64
*stp
)
293 int fd
= open64(path
, O_RDONLY
);
296 int status
= fstat64(fd
, stp
);
305 dconf_swap_compare(const swapent_t
*s1
, const swapent_t
*s2
)
307 struct stat64 st1
, st2
;
309 int prefer_s1
= -1; /* Return value to move s1 left (s1 < s2) */
310 int prefer_s2
= 1; /* Return value to move s2 left (s1 > s2) */
313 * First try: open and fstat each swap entry. If either system
314 * call fails, arbitrarily prefer the other entry.
316 if (open_stat64(s1
->ste_path
, &st1
) == -1)
319 if (open_stat64(s2
->ste_path
, &st2
) == -1)
323 * Second try: if both entries are block devices, or if
324 * neither is a block device, prefer the larger.
326 if (S_ISBLK(st1
.st_mode
) == S_ISBLK(st2
.st_mode
)) {
327 if (st2
.st_size
> st1
.st_size
)
333 * Third try: prefer the entry that is a block device.
335 if (S_ISBLK(st2
.st_mode
))
341 dconf_dev_ioctl(dumpconf_t
*dcp
, int cmd
)
343 if (ioctl(dcp
->dc_dump_fd
, cmd
, dcp
->dc_device
) == 0)
348 warn(gettext("dumps not supported on %s\n"), dcp
->dc_device
);
351 warn(gettext("device %s is already in use\n"), dcp
->dc_device
);
354 /* ZFS pool is too fragmented to support a dump device */
355 warn(gettext("device %s is too fragmented to be used as "
356 "a dump device\n"), dcp
->dc_device
);
360 * NOTE: The stmsboot(1M) command's boot-up script parses this
361 * error to get the dump device name. If you change the format
362 * of this message, make sure that stmsboot(1M) is in sync.
364 warn(gettext("cannot use %s as dump device"), dcp
->dc_device
);
370 dconf_update(dumpconf_t
*dcp
, int checkinuse
)
378 if (checkinuse
&& (dm_inuse(dcp
->dc_device
, &msg
, DM_WHO_DUMP
,
381 warn(gettext("failed to determine if %s is"
382 " in use"), dcp
->dc_device
);
391 * Save the existing dump configuration in case something goes wrong.
393 if ((oconf
= ioctl(dcp
->dc_dump_fd
, DIOCGETCONF
, 0)) == -1) {
394 warn(gettext("failed to get kernel dump configuration"));
398 oconf
&= DUMP_CONTENT
;
399 dcp
->dc_cflags
&= DUMP_CONTENT
;
401 if (ioctl(dcp
->dc_dump_fd
, DIOCSETCONF
, dcp
->dc_cflags
) == -1) {
402 warn(gettext("failed to update kernel dump configuration"));
406 if (strcmp(dcp
->dc_device
, DC_STR_SWAP
) == 0) {
410 if ((swt
= swap_list()) == NULL
)
413 if (swt
->swt_n
== 0) {
414 warn(gettext("no swap devices are available\n"));
419 qsort(&swt
->swt_ent
[0], swt
->swt_n
, sizeof (swapent_t
),
420 (int (*)(const void *, const void *))dconf_swap_compare
);
423 * Iterate through the prioritized list of swap entries,
424 * trying to configure one as the dump device.
426 for (i
= 0; i
< swt
->swt_n
; i
++) {
427 if (ioctl(dcp
->dc_dump_fd
, DIOCSETDEV
,
428 swt
->swt_ent
[i
].ste_path
) == 0) {
429 (void) strcpy(dcp
->dc_device
,
430 swt
->swt_ent
[i
].ste_path
);
435 if (i
== swt
->swt_n
) {
436 warn(gettext("no swap devices could be configured "
437 "as the dump device\n"));
443 } else if (strcmp(dcp
->dc_device
, DC_STR_NONE
) == 0) {
444 if (ioctl(dcp
->dc_dump_fd
, DIOCRMDEV
, NULL
) == -1) {
445 warn(gettext("failed to remove dump device"));
448 } else if (dcp
->dc_device
[0] != '\0') {
450 * If we're not in forcible update mode, then fail the change
451 * if the selected device cannot be used as the dump device,
452 * or if it is not big enough to hold the dump.
454 if (dcp
->dc_mode
== DC_CURRENT
) {
458 if (dconf_dev_ioctl(dcp
, DIOCTRYDEV
) == -1)
461 if (open_stat64(dcp
->dc_device
, &st
) == -1) {
462 warn(gettext("failed to access %s"),
467 if ((error
= zvol_check_dump_config(
468 dcp
->dc_device
)) > 0)
470 if (ioctl(dcp
->dc_dump_fd
, DIOCGETDUMPSIZE
, &d
) == -1) {
471 warn(gettext("failed to get kernel dump size"));
475 if (st
.st_size
< d
) {
476 warn(gettext("dump device %s is too small to "
477 "hold a system dump\ndump size %llu "
478 "bytes, device size %lld bytes\n"),
479 dcp
->dc_device
, d
, st
.st_size
);
484 if (dconf_dev_ioctl(dcp
, DIOCSETDEV
) == -1)
489 * Now that we've updated the dump device, we need to issue another
490 * ioctl to re-read the config flags to determine whether we
491 * obtained DUMP_EXCL access on our dump device.
493 if ((dcp
->dc_cflags
= ioctl(dcp
->dc_dump_fd
, DIOCGETCONF
, 0)) == -1) {
494 warn(gettext("failed to re-read kernel dump configuration"));
501 (void) ioctl(dcp
->dc_dump_fd
, DIOCSETCONF
, oconf
);
506 dconf_write_uuid(dumpconf_t
*dcp
)
508 char uuidstr
[36 + 1];
513 uuid_unparse(uu
, uuidstr
);
515 err
= ioctl(dcp
->dc_dump_fd
, DIOCSETUUID
, uuidstr
);
518 warn(gettext("kernel image uuid write failed"));
524 dconf_get_dumpsize(dumpconf_t
*dcp
)
529 if (ioctl(dcp
->dc_dump_fd
, DIOCGETDUMPSIZE
, &d
) == -1) {
530 warn(gettext("failed to get kernel dump size"));
534 zfs_nicenum(d
, buf
, sizeof (buf
));
536 (void) printf(gettext("Estimated dump size: %s\n"), buf
);
541 dconf_print(dumpconf_t
*dcp
, FILE *fp
)
546 if (dcp
->dc_cflags
& DUMP_ALL
)
547 content
= gettext("all");
548 else if (dcp
->dc_cflags
& DUMP_CURPROC
)
549 content
= gettext("kernel and current process");
551 content
= gettext("kernel");
553 (void) fprintf(fp
, gettext(" Dump content: %s pages\n"), content
);
555 if (dcp
->dc_device
[0] != '\0') {
556 (void) fprintf(fp
, gettext(" Dump device: %s (%s)\n"),
557 dcp
->dc_device
, (dcp
->dc_cflags
& DUMP_EXCL
) ?
558 gettext("dedicated") : gettext("swap"));
560 (void) fprintf(fp
, gettext(" Dump device: none "
561 "(dumps disabled)\n"));
564 (void) fprintf(fp
, gettext("Savecore directory: %s"), dcp
->dc_savdir
);
566 if (minfree_read(dcp
->dc_savdir
, &min
) == 0) {
567 if (min
< 1024 || (min
% 1024) != 0)
568 (void) fprintf(fp
, gettext(" (minfree = %lluKB)"), min
);
570 (void) fprintf(fp
, gettext(" (minfree = %lluMB)"),
574 (void) fprintf(fp
, gettext("\n"));
576 (void) fprintf(fp
, gettext(" Savecore enabled: %s\n"),
577 (dcp
->dc_enable
== DC_OFF
) ? gettext("no") : gettext("yes"));
578 (void) fprintf(fp
, gettext(" Save compressed: %s\n"),
579 (dcp
->dc_csave
== DC_UNCOMPRESSED
) ? gettext("off") :
584 dconf_str2device(dumpconf_t
*dcp
, char *buf
)
586 if (strcasecmp(buf
, DC_STR_SWAP
) == 0) {
587 (void) strcpy(dcp
->dc_device
, DC_STR_SWAP
);
591 if (strcasecmp(buf
, DC_STR_NONE
) == 0) {
592 (void) strcpy(dcp
->dc_device
, DC_STR_NONE
);
596 if (valid_abspath(buf
)) {
597 (void) strcpy(dcp
->dc_device
, buf
);
605 dconf_str2savdir(dumpconf_t
*dcp
, char *buf
)
607 if (valid_abspath(buf
)) {
608 (void) strcpy(dcp
->dc_savdir
, buf
);
616 dconf_str2content(dumpconf_t
*dcp
, char *buf
)
618 if (strcasecmp(buf
, DC_STR_KERNEL
) == 0) {
619 dcp
->dc_cflags
= (dcp
->dc_cflags
& ~DUMP_CONTENT
) | DUMP_KERNEL
;
623 if (strcasecmp(buf
, DC_STR_CURPROC
) == 0) {
624 dcp
->dc_cflags
= (dcp
->dc_cflags
& ~DUMP_CONTENT
) |
629 if (strcasecmp(buf
, DC_STR_ALL
) == 0) {
630 dcp
->dc_cflags
= (dcp
->dc_cflags
& ~DUMP_CONTENT
) | DUMP_ALL
;
634 warn(gettext("invalid dump content type -- %s\n"), buf
);
639 dconf_str2enable(dumpconf_t
*dcp
, char *buf
)
641 if (strcasecmp(buf
, DC_STR_YES
) == 0) {
642 dcp
->dc_enable
= DC_ON
;
646 if (strcasecmp(buf
, DC_STR_NO
) == 0) {
647 dcp
->dc_enable
= DC_OFF
;
651 warn(gettext("invalid enable value -- %s\n"), buf
);
656 dconf_str2csave(dumpconf_t
*dcp
, char *buf
)
658 if (strcasecmp(buf
, DC_STR_ON
) == 0) {
659 dcp
->dc_csave
= DC_COMPRESSED
;
663 if (strcasecmp(buf
, DC_STR_OFF
) == 0) {
664 dcp
->dc_csave
= DC_UNCOMPRESSED
;
668 warn(gettext("invalid save compressed value -- %s\n"), buf
);
673 print_content(const dumpconf_t
*dcp
, FILE *fp
)
677 if (dcp
->dc_cflags
& DUMP_ALL
)
678 content
= DC_STR_ALL
;
679 else if (dcp
->dc_cflags
& DUMP_CURPROC
)
680 content
= DC_STR_CURPROC
;
682 content
= DC_STR_KERNEL
;
684 return (fprintf(fp
, "%s\n", content
));
688 print_device(const dumpconf_t
*dcp
, FILE *fp
)
690 return (fprintf(fp
, "%s\n", (dcp
->dc_device
[0] != '\0') ?
691 dcp
->dc_device
: DC_STR_SWAP
));
695 print_enable(const dumpconf_t
*dcp
, FILE *fp
)
697 return (fprintf(fp
, "%s\n", (dcp
->dc_enable
== DC_OFF
) ?
698 DC_STR_NO
: DC_STR_YES
));
702 print_csave(const dumpconf_t
*dcp
, FILE *fp
)
704 return (fprintf(fp
, "%s\n", (dcp
->dc_csave
== DC_COMPRESSED
) ?
705 DC_STR_ON
: DC_STR_OFF
));
709 print_savdir(const dumpconf_t
*dcp
, FILE *fp
)
711 return (fprintf(fp
, "%s\n", dcp
->dc_savdir
));