xref: /titanic_44/usr/src/cmd/dumpadm/dconf.c (revision 7d593912b33208f97b6a9b5aa18d6bc0227c8a67)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a10acbd6Seschrock  * Common Development and Distribution License (the "License").
6a10acbd6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22f6e214c7SGavin Maltby  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23*7d593912SAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <sys/swap.h>
297c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h>
307c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
383e1bd7a2Ssjelinek #include <libdiskmgt.h>
39e7cbe64fSgw25295 #include <libzfs.h>
40f6e214c7SGavin Maltby #include <uuid/uuid.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "dconf.h"
437c478bd9Sstevel@tonic-gate #include "minfree.h"
447c478bd9Sstevel@tonic-gate #include "utils.h"
457c478bd9Sstevel@tonic-gate #include "swap.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate typedef struct dc_token {
487c478bd9Sstevel@tonic-gate 	const char *tok_name;
497c478bd9Sstevel@tonic-gate 	int (*tok_parse)(dumpconf_t *, char *);
507c478bd9Sstevel@tonic-gate 	int (*tok_print)(const dumpconf_t *, FILE *);
517c478bd9Sstevel@tonic-gate } dc_token_t;
527c478bd9Sstevel@tonic-gate 
533e1bd7a2Ssjelinek 
547c478bd9Sstevel@tonic-gate static int print_device(const dumpconf_t *, FILE *);
557c478bd9Sstevel@tonic-gate static int print_savdir(const dumpconf_t *, FILE *);
567c478bd9Sstevel@tonic-gate static int print_content(const dumpconf_t *, FILE *);
577c478bd9Sstevel@tonic-gate static int print_enable(const dumpconf_t *, FILE *);
58ca3e8d88SDave Plauger static int print_csave(const dumpconf_t *, FILE *);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static const dc_token_t tokens[] = {
617c478bd9Sstevel@tonic-gate 	{ "DUMPADM_DEVICE", dconf_str2device, print_device },
627c478bd9Sstevel@tonic-gate 	{ "DUMPADM_SAVDIR", dconf_str2savdir, print_savdir },
637c478bd9Sstevel@tonic-gate 	{ "DUMPADM_CONTENT", dconf_str2content, print_content },
647c478bd9Sstevel@tonic-gate 	{ "DUMPADM_ENABLE", dconf_str2enable, print_enable },
65ca3e8d88SDave Plauger 	{ "DUMPADM_CSAVE", dconf_str2csave, print_csave },
667c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
69ca3e8d88SDave Plauger static const char DC_STR_ON[] = "on";		/* On string */
70ca3e8d88SDave Plauger static const char DC_STR_OFF[] = "off";		/* Off string */
717c478bd9Sstevel@tonic-gate static const char DC_STR_YES[] = "yes";		/* Enable on string */
727c478bd9Sstevel@tonic-gate static const char DC_STR_NO[] = "no";		/* Enable off string */
737c478bd9Sstevel@tonic-gate static const char DC_STR_SWAP[] = "swap";	/* Default dump device */
74c61ea566SGeorge Wilson static const char DC_STR_NONE[] = "none";
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* The pages included in the dump */
777c478bd9Sstevel@tonic-gate static const char DC_STR_KERNEL[] = "kernel";	/* Kernel only */
787c478bd9Sstevel@tonic-gate static const char DC_STR_CURPROC[] = "curproc";	/* Kernel + current process */
797c478bd9Sstevel@tonic-gate static const char DC_STR_ALL[] = "all";		/* All pages */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Permissions and ownership for the configuration file:
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate #define	DC_OWNER	0				/* Uid 0 (root) */
857c478bd9Sstevel@tonic-gate #define	DC_GROUP	1				/* Gid 1 (other) */
867c478bd9Sstevel@tonic-gate #define	DC_PERM	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)	/* Mode 0644 */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void
dconf_init(dumpconf_t * dcp,int dcmode)897c478bd9Sstevel@tonic-gate dconf_init(dumpconf_t *dcp, int dcmode)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	struct utsname ut;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * Default device for dumps is 'swap' (appropriate swap device),
957c478bd9Sstevel@tonic-gate 	 * and default savecore directory is /var/crash/`uname -n`,
967c478bd9Sstevel@tonic-gate 	 * which is compatible with pre-dumpadm behavior.
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	(void) strcpy(dcp->dc_device, DC_STR_SWAP);
997c478bd9Sstevel@tonic-gate 	(void) strcpy(dcp->dc_savdir, "/var/crash");
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (uname(&ut) != -1) {
1027c478bd9Sstevel@tonic-gate 		(void) strcat(dcp->dc_savdir, "/");
1037c478bd9Sstevel@tonic-gate 		(void) strcat(dcp->dc_savdir, ut.nodename);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
107ca3e8d88SDave Plauger 	 * Default is contents kernel, savecore enabled on reboot,
108ca3e8d88SDave Plauger 	 * savecore saves compressed core files.
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	dcp->dc_cflags = DUMP_KERNEL;
1117c478bd9Sstevel@tonic-gate 	dcp->dc_enable = DC_ON;
112ca3e8d88SDave Plauger 	dcp->dc_csave = DC_COMPRESSED;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	dcp->dc_mode = dcmode;
1157c478bd9Sstevel@tonic-gate 	dcp->dc_conf_fp = NULL;
1167c478bd9Sstevel@tonic-gate 	dcp->dc_conf_fd = -1;
1177c478bd9Sstevel@tonic-gate 	dcp->dc_dump_fd = -1;
118a10acbd6Seschrock 	dcp->dc_readonly = B_FALSE;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int
dconf_open(dumpconf_t * dcp,const char * dpath,const char * fpath,int dcmode)1227c478bd9Sstevel@tonic-gate dconf_open(dumpconf_t *dcp, const char *dpath, const char *fpath, int dcmode)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
1257c478bd9Sstevel@tonic-gate 	int line;
126a10acbd6Seschrock 	const char *fpmode = "r+";
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	dconf_init(dcp, dcmode);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if ((dcp->dc_dump_fd = open(dpath, O_RDWR)) == -1) {
1317c478bd9Sstevel@tonic-gate 		warn(gettext("failed to open %s"), dpath);
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if ((dcp->dc_conf_fd = open(fpath, O_RDWR | O_CREAT, DC_PERM)) == -1) {
136a10acbd6Seschrock 		/*
137a10acbd6Seschrock 		 * Attempt to open the file read-only.
138a10acbd6Seschrock 		 */
139a10acbd6Seschrock 		if ((dcp->dc_conf_fd = open(fpath, O_RDONLY)) == -1) {
1407c478bd9Sstevel@tonic-gate 			warn(gettext("failed to open %s"), fpath);
1417c478bd9Sstevel@tonic-gate 			return (-1);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 
144a10acbd6Seschrock 		dcp->dc_readonly = B_TRUE;
145a10acbd6Seschrock 		fpmode = "r";
146a10acbd6Seschrock 	}
147a10acbd6Seschrock 
148a10acbd6Seschrock 	if ((dcp->dc_conf_fp = fdopen(dcp->dc_conf_fd, fpmode)) == NULL) {
1497c478bd9Sstevel@tonic-gate 		warn(gettext("failed to open stream for %s"), fpath);
1507c478bd9Sstevel@tonic-gate 		return (-1);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * If we're in override mode, the current kernel settings override the
1557c478bd9Sstevel@tonic-gate 	 * default settings and anything invalid in the configuration file.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	if (dcmode == DC_OVERRIDE)
1587c478bd9Sstevel@tonic-gate 		(void) dconf_getdev(dcp);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	for (line = 1; fgets(buf, BUFSIZ, dcp->dc_conf_fp) != NULL; line++) {
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		char name[BUFSIZ], value[BUFSIZ];
1637c478bd9Sstevel@tonic-gate 		const dc_token_t *tokp;
1647c478bd9Sstevel@tonic-gate 		int len;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		if (buf[0] == '#' || buf[0] == '\n')
1677c478bd9Sstevel@tonic-gate 			continue;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * Look for "name=value", with optional whitespace on either
1717c478bd9Sstevel@tonic-gate 		 * side, terminated by a newline, and consuming the whole line.
1727c478bd9Sstevel@tonic-gate 		 */
1737c478bd9Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
1747c478bd9Sstevel@tonic-gate 		if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 &&
1757c478bd9Sstevel@tonic-gate 		    name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) {
1767c478bd9Sstevel@tonic-gate 			/*
1777c478bd9Sstevel@tonic-gate 			 * Locate a matching token in the tokens[] table,
1787c478bd9Sstevel@tonic-gate 			 * and invoke its parsing function.
1797c478bd9Sstevel@tonic-gate 			 */
1807c478bd9Sstevel@tonic-gate 			for (tokp = tokens; tokp->tok_name != NULL; tokp++) {
1817c478bd9Sstevel@tonic-gate 				if (strcmp(name, tokp->tok_name) == 0) {
1827c478bd9Sstevel@tonic-gate 					if (tokp->tok_parse(dcp, value) == -1) {
1837c478bd9Sstevel@tonic-gate 						warn(gettext("\"%s\", line %d: "
1847c478bd9Sstevel@tonic-gate 						    "warning: invalid %s\n"),
1857c478bd9Sstevel@tonic-gate 						    fpath, line, name);
1867c478bd9Sstevel@tonic-gate 					}
1877c478bd9Sstevel@tonic-gate 					break;
1887c478bd9Sstevel@tonic-gate 				}
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 			/*
1927c478bd9Sstevel@tonic-gate 			 * If we hit the end of the tokens[] table,
1937c478bd9Sstevel@tonic-gate 			 * no matching token was found.
1947c478bd9Sstevel@tonic-gate 			 */
1957c478bd9Sstevel@tonic-gate 			if (tokp->tok_name == NULL) {
1967c478bd9Sstevel@tonic-gate 				warn(gettext("\"%s\", line %d: warning: "
1977c478bd9Sstevel@tonic-gate 				    "invalid token: %s\n"), fpath, line, name);
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		} else {
2017c478bd9Sstevel@tonic-gate 			warn(gettext("\"%s\", line %d: syntax error\n"),
2027c478bd9Sstevel@tonic-gate 			    fpath, line);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * If we're not in override mode, the current kernel settings
2087c478bd9Sstevel@tonic-gate 	 * override the settings read from the configuration file.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if (dcmode == DC_CURRENT)
2117c478bd9Sstevel@tonic-gate 		return (dconf_getdev(dcp));
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (0);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate int
dconf_getdev(dumpconf_t * dcp)2177c478bd9Sstevel@tonic-gate dconf_getdev(dumpconf_t *dcp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	int status = 0;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) {
2227c478bd9Sstevel@tonic-gate 		warn(gettext("failed to get kernel dump settings"));
2237c478bd9Sstevel@tonic-gate 		status = -1;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (ioctl(dcp->dc_dump_fd, DIOCGETDEV, dcp->dc_device) == -1) {
2277c478bd9Sstevel@tonic-gate 		if (errno != ENODEV) {
2287c478bd9Sstevel@tonic-gate 			warn(gettext("failed to get dump device"));
2297c478bd9Sstevel@tonic-gate 			status = -1;
2307c478bd9Sstevel@tonic-gate 		} else
2317c478bd9Sstevel@tonic-gate 			dcp->dc_device[0] = '\0';
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	return (status);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate int
dconf_close(dumpconf_t * dcp)2387c478bd9Sstevel@tonic-gate dconf_close(dumpconf_t *dcp)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	if (fclose(dcp->dc_conf_fp) == 0) {
2417c478bd9Sstevel@tonic-gate 		(void) close(dcp->dc_dump_fd);
2427c478bd9Sstevel@tonic-gate 		return (0);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	return (-1);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate int
dconf_write(dumpconf_t * dcp)2487c478bd9Sstevel@tonic-gate dconf_write(dumpconf_t *dcp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	const dc_token_t *tokp;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (fseeko(dcp->dc_conf_fp, (off_t)0, SEEK_SET) == -1) {
2537c478bd9Sstevel@tonic-gate 		warn(gettext("failed to seek config file"));
2547c478bd9Sstevel@tonic-gate 		return (-1);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (ftruncate(dcp->dc_conf_fd, (off_t)0) == -1) {
2587c478bd9Sstevel@tonic-gate 		warn(gettext("failed to truncate config file"));
2597c478bd9Sstevel@tonic-gate 		return (-1);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	(void) fputs("#\n# dumpadm.conf\n#\n"
2637c478bd9Sstevel@tonic-gate 	    "# Configuration parameters for system crash dump.\n"
2647c478bd9Sstevel@tonic-gate 	    "# Do NOT edit this file by hand -- use dumpadm(1m) instead.\n"
2657c478bd9Sstevel@tonic-gate 	    "#\n", dcp->dc_conf_fp);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	for (tokp = tokens; tokp->tok_name != NULL; tokp++) {
2687c478bd9Sstevel@tonic-gate 		if (fprintf(dcp->dc_conf_fp, "%s=", tokp->tok_name) == -1 ||
2697c478bd9Sstevel@tonic-gate 		    tokp->tok_print(dcp, dcp->dc_conf_fp) == -1) {
2707c478bd9Sstevel@tonic-gate 			warn(gettext("failed to write token"));
2717c478bd9Sstevel@tonic-gate 			return (-1);
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (fflush(dcp->dc_conf_fp) != 0)
2767c478bd9Sstevel@tonic-gate 		warn(gettext("warning: failed to flush config file"));
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (fsync(dcp->dc_conf_fd) == -1)
2797c478bd9Sstevel@tonic-gate 		warn(gettext("warning: failed to sync config file to disk"));
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (fchmod(dcp->dc_conf_fd, DC_PERM) == -1)
2827c478bd9Sstevel@tonic-gate 		warn(gettext("warning: failed to reset mode on config file"));
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (fchown(dcp->dc_conf_fd, DC_OWNER, DC_GROUP) == -1)
2857c478bd9Sstevel@tonic-gate 		warn(gettext("warning: failed to reset owner on config file"));
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	return (0);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static int
open_stat64(const char * path,struct stat64 * stp)2917c478bd9Sstevel@tonic-gate open_stat64(const char *path, struct stat64 *stp)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	int fd = open64(path, O_RDONLY);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
2967c478bd9Sstevel@tonic-gate 		int status = fstat64(fd, stp);
2977c478bd9Sstevel@tonic-gate 		(void) close(fd);
2987c478bd9Sstevel@tonic-gate 		return (status);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	return (-1);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate static int
dconf_swap_compare(const swapent_t * s1,const swapent_t * s2)3057c478bd9Sstevel@tonic-gate dconf_swap_compare(const swapent_t *s1, const swapent_t *s2)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	struct stat64 st1, st2;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	int prefer_s1 = -1;	/* Return value to move s1 left (s1 < s2) */
3107c478bd9Sstevel@tonic-gate 	int prefer_s2 = 1;	/* Return value to move s2 left (s1 > s2) */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * First try: open and fstat each swap entry.  If either system
3147c478bd9Sstevel@tonic-gate 	 * call fails, arbitrarily prefer the other entry.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if (open_stat64(s1->ste_path, &st1) == -1)
3177c478bd9Sstevel@tonic-gate 		return (prefer_s2);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (open_stat64(s2->ste_path, &st2) == -1)
3207c478bd9Sstevel@tonic-gate 		return (prefer_s1);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Second try: if both entries are block devices, or if
3247c478bd9Sstevel@tonic-gate 	 * neither is a block device, prefer the larger.
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	if (S_ISBLK(st1.st_mode) == S_ISBLK(st2.st_mode)) {
3277c478bd9Sstevel@tonic-gate 		if (st2.st_size > st1.st_size)
3287c478bd9Sstevel@tonic-gate 			return (prefer_s2);
3297c478bd9Sstevel@tonic-gate 		return (prefer_s1);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Third try: prefer the entry that is a block device.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	if (S_ISBLK(st2.st_mode))
3367c478bd9Sstevel@tonic-gate 		return (prefer_s2);
3377c478bd9Sstevel@tonic-gate 	return (prefer_s1);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static int
dconf_dev_ioctl(dumpconf_t * dcp,int cmd)3417c478bd9Sstevel@tonic-gate dconf_dev_ioctl(dumpconf_t *dcp, int cmd)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	if (ioctl(dcp->dc_dump_fd, cmd, dcp->dc_device) == 0)
3447c478bd9Sstevel@tonic-gate 		return (0);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	switch (errno) {
3477c478bd9Sstevel@tonic-gate 	case ENOTSUP:
3487c478bd9Sstevel@tonic-gate 		warn(gettext("dumps not supported on %s\n"), dcp->dc_device);
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 	case EBUSY:
3517c478bd9Sstevel@tonic-gate 		warn(gettext("device %s is already in use\n"), dcp->dc_device);
3527c478bd9Sstevel@tonic-gate 		break;
353e7cbe64fSgw25295 	case EBADR:
354e7cbe64fSgw25295 		/* ZFS pool is too fragmented to support a dump device */
355e7cbe64fSgw25295 		warn(gettext("device %s is too fragmented to be used as "
356e7cbe64fSgw25295 		    "a dump device\n"), dcp->dc_device);
357e7cbe64fSgw25295 		break;
3587c478bd9Sstevel@tonic-gate 	default:
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * NOTE: The stmsboot(1M) command's boot-up script parses this
3617c478bd9Sstevel@tonic-gate 		 * error to get the dump device name. If you change the format
3627c478bd9Sstevel@tonic-gate 		 * of this message, make sure that stmsboot(1M) is in sync.
3637c478bd9Sstevel@tonic-gate 		 */
3647c478bd9Sstevel@tonic-gate 		warn(gettext("cannot use %s as dump device"), dcp->dc_device);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	return (-1);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate int
dconf_update(dumpconf_t * dcp,int checkinuse)3703e1bd7a2Ssjelinek dconf_update(dumpconf_t *dcp, int checkinuse)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	int		oconf;
3733e1bd7a2Ssjelinek 	int		error;
3743e1bd7a2Ssjelinek 	char		*msg;
3753e1bd7a2Ssjelinek 
3763e1bd7a2Ssjelinek 	error = 0;
3773e1bd7a2Ssjelinek 
3783e1bd7a2Ssjelinek 	if (checkinuse && (dm_inuse(dcp->dc_device, &msg, DM_WHO_DUMP,
3793e1bd7a2Ssjelinek 	    &error) || error)) {
3803e1bd7a2Ssjelinek 		if (error != 0) {
3813e1bd7a2Ssjelinek 			warn(gettext("failed to determine if %s is"
3823e1bd7a2Ssjelinek 			    " in use"), dcp->dc_device);
3833e1bd7a2Ssjelinek 		} else {
3843e1bd7a2Ssjelinek 			warn(msg);
3853e1bd7a2Ssjelinek 			free(msg);
3863e1bd7a2Ssjelinek 			return (-1);
3873e1bd7a2Ssjelinek 		}
3883e1bd7a2Ssjelinek 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/*
3917c478bd9Sstevel@tonic-gate 	 * Save the existing dump configuration in case something goes wrong.
3927c478bd9Sstevel@tonic-gate 	 */
3937c478bd9Sstevel@tonic-gate 	if ((oconf = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) {
3947c478bd9Sstevel@tonic-gate 		warn(gettext("failed to get kernel dump configuration"));
3957c478bd9Sstevel@tonic-gate 		return (-1);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	oconf &= DUMP_CONTENT;
3997c478bd9Sstevel@tonic-gate 	dcp->dc_cflags &= DUMP_CONTENT;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (ioctl(dcp->dc_dump_fd, DIOCSETCONF, dcp->dc_cflags) == -1) {
4027c478bd9Sstevel@tonic-gate 		warn(gettext("failed to update kernel dump configuration"));
4037c478bd9Sstevel@tonic-gate 		return (-1);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (strcmp(dcp->dc_device, DC_STR_SWAP) == 0) {
4077c478bd9Sstevel@tonic-gate 		swaptbl_t *swt;
4087c478bd9Sstevel@tonic-gate 		int i;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		if ((swt = swap_list()) == NULL)
4117c478bd9Sstevel@tonic-gate 			goto err;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		if (swt->swt_n == 0) {
4147c478bd9Sstevel@tonic-gate 			warn(gettext("no swap devices are available\n"));
4157c478bd9Sstevel@tonic-gate 			free(swt);
4167c478bd9Sstevel@tonic-gate 			goto err;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		qsort(&swt->swt_ent[0], swt->swt_n, sizeof (swapent_t),
4207c478bd9Sstevel@tonic-gate 		    (int (*)(const void *, const void *))dconf_swap_compare);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		/*
4237c478bd9Sstevel@tonic-gate 		 * Iterate through the prioritized list of swap entries,
4247c478bd9Sstevel@tonic-gate 		 * trying to configure one as the dump device.
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		for (i = 0; i < swt->swt_n; i++) {
4277c478bd9Sstevel@tonic-gate 			if (ioctl(dcp->dc_dump_fd, DIOCSETDEV,
4287c478bd9Sstevel@tonic-gate 			    swt->swt_ent[i].ste_path) == 0) {
4297c478bd9Sstevel@tonic-gate 				(void) strcpy(dcp->dc_device,
4307c478bd9Sstevel@tonic-gate 				    swt->swt_ent[i].ste_path);
4317c478bd9Sstevel@tonic-gate 				break;
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (i == swt->swt_n) {
4367c478bd9Sstevel@tonic-gate 			warn(gettext("no swap devices could be configured "
4377c478bd9Sstevel@tonic-gate 			    "as the dump device\n"));
4387c478bd9Sstevel@tonic-gate 			free(swt);
4397c478bd9Sstevel@tonic-gate 			goto err;
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 		free(swt);
4427c478bd9Sstevel@tonic-gate 
443c61ea566SGeorge Wilson 	} else if (strcmp(dcp->dc_device, DC_STR_NONE) == 0) {
444c61ea566SGeorge Wilson 		if (ioctl(dcp->dc_dump_fd, DIOCRMDEV, NULL) == -1) {
445c61ea566SGeorge Wilson 			warn(gettext("failed to remove dump device"));
446c61ea566SGeorge Wilson 			return (-1);
447c61ea566SGeorge Wilson 		}
4487c478bd9Sstevel@tonic-gate 	} else if (dcp->dc_device[0] != '\0') {
4497c478bd9Sstevel@tonic-gate 		/*
4507c478bd9Sstevel@tonic-gate 		 * If we're not in forcible update mode, then fail the change
4517c478bd9Sstevel@tonic-gate 		 * if the selected device cannot be used as the dump device,
4527c478bd9Sstevel@tonic-gate 		 * or if it is not big enough to hold the dump.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		if (dcp->dc_mode == DC_CURRENT) {
4557c478bd9Sstevel@tonic-gate 			struct stat64 st;
4567c478bd9Sstevel@tonic-gate 			uint64_t d;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 			if (dconf_dev_ioctl(dcp, DIOCTRYDEV) == -1)
4597c478bd9Sstevel@tonic-gate 				goto err;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			if (open_stat64(dcp->dc_device, &st) == -1) {
4627c478bd9Sstevel@tonic-gate 				warn(gettext("failed to access %s"),
4637c478bd9Sstevel@tonic-gate 				    dcp->dc_device);
4647c478bd9Sstevel@tonic-gate 				goto err;
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 
467e7cbe64fSgw25295 			if ((error = zvol_check_dump_config(
468e7cbe64fSgw25295 			    dcp->dc_device)) > 0)
469e7cbe64fSgw25295 				goto err;
4707c478bd9Sstevel@tonic-gate 			if (ioctl(dcp->dc_dump_fd, DIOCGETDUMPSIZE, &d) == -1) {
4717c478bd9Sstevel@tonic-gate 				warn(gettext("failed to get kernel dump size"));
4727c478bd9Sstevel@tonic-gate 				goto err;
4737c478bd9Sstevel@tonic-gate 			}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			if (st.st_size < d) {
4767c478bd9Sstevel@tonic-gate 				warn(gettext("dump device %s is too small to "
4777c478bd9Sstevel@tonic-gate 				    "hold a system dump\ndump size %llu "
4787c478bd9Sstevel@tonic-gate 				    "bytes, device size %lld bytes\n"),
4797c478bd9Sstevel@tonic-gate 				    dcp->dc_device, d, st.st_size);
4807c478bd9Sstevel@tonic-gate 				goto err;
4817c478bd9Sstevel@tonic-gate 			}
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		if (dconf_dev_ioctl(dcp, DIOCSETDEV) == -1)
4857c478bd9Sstevel@tonic-gate 			goto err;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Now that we've updated the dump device, we need to issue another
4907c478bd9Sstevel@tonic-gate 	 * ioctl to re-read the config flags to determine whether we
4917c478bd9Sstevel@tonic-gate 	 * obtained DUMP_EXCL access on our dump device.
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) {
4947c478bd9Sstevel@tonic-gate 		warn(gettext("failed to re-read kernel dump configuration"));
4957c478bd9Sstevel@tonic-gate 		return (-1);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	return (0);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate err:
5017c478bd9Sstevel@tonic-gate 	(void) ioctl(dcp->dc_dump_fd, DIOCSETCONF, oconf);
5027c478bd9Sstevel@tonic-gate 	return (-1);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
505f6e214c7SGavin Maltby int
dconf_write_uuid(dumpconf_t * dcp)506f6e214c7SGavin Maltby dconf_write_uuid(dumpconf_t *dcp)
507f6e214c7SGavin Maltby {
508f6e214c7SGavin Maltby 	char uuidstr[36 + 1];
509f6e214c7SGavin Maltby 	uuid_t uu;
510f6e214c7SGavin Maltby 	int err;
511f6e214c7SGavin Maltby 
512f6e214c7SGavin Maltby 	uuid_generate(uu);
513f6e214c7SGavin Maltby 	uuid_unparse(uu, uuidstr);
514f6e214c7SGavin Maltby 
515f6e214c7SGavin Maltby 	err = ioctl(dcp->dc_dump_fd, DIOCSETUUID, uuidstr);
516f6e214c7SGavin Maltby 
517f6e214c7SGavin Maltby 	if (err)
518f6e214c7SGavin Maltby 		warn(gettext("kernel image uuid write failed"));
519f6e214c7SGavin Maltby 
520f6e214c7SGavin Maltby 	return (err == 0);
521f6e214c7SGavin Maltby }
522f6e214c7SGavin Maltby 
523*7d593912SAlexander Eremin int
dconf_get_dumpsize(dumpconf_t * dcp)524*7d593912SAlexander Eremin dconf_get_dumpsize(dumpconf_t *dcp)
525*7d593912SAlexander Eremin {
526*7d593912SAlexander Eremin 	char buf[32];
527*7d593912SAlexander Eremin 	uint64_t d;
528*7d593912SAlexander Eremin 
529*7d593912SAlexander Eremin 	if (ioctl(dcp->dc_dump_fd, DIOCGETDUMPSIZE, &d) == -1) {
530*7d593912SAlexander Eremin 		warn(gettext("failed to get kernel dump size"));
531*7d593912SAlexander Eremin 		return (-1);
532*7d593912SAlexander Eremin 	}
533*7d593912SAlexander Eremin 
534*7d593912SAlexander Eremin 	zfs_nicenum(d, buf, sizeof (buf));
535*7d593912SAlexander Eremin 
536*7d593912SAlexander Eremin 	(void) printf(gettext("Estimated dump size: %s\n"), buf);
537*7d593912SAlexander Eremin 	return (0);
538*7d593912SAlexander Eremin }
539*7d593912SAlexander Eremin 
5407c478bd9Sstevel@tonic-gate void
dconf_print(dumpconf_t * dcp,FILE * fp)5417c478bd9Sstevel@tonic-gate dconf_print(dumpconf_t *dcp, FILE *fp)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	u_longlong_t min;
5447c478bd9Sstevel@tonic-gate 	char *content;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if (dcp->dc_cflags & DUMP_ALL)
5477c478bd9Sstevel@tonic-gate 		content = gettext("all");
5487c478bd9Sstevel@tonic-gate 	else if (dcp->dc_cflags & DUMP_CURPROC)
5497c478bd9Sstevel@tonic-gate 		content = gettext("kernel and current process");
5507c478bd9Sstevel@tonic-gate 	else
5517c478bd9Sstevel@tonic-gate 		content = gettext("kernel");
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, gettext("      Dump content: %s pages\n"), content);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (dcp->dc_device[0] != '\0') {
5567c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("       Dump device: %s (%s)\n"),
5577c478bd9Sstevel@tonic-gate 		    dcp->dc_device, (dcp->dc_cflags & DUMP_EXCL) ?
5587c478bd9Sstevel@tonic-gate 		    gettext("dedicated") : gettext("swap"));
5597c478bd9Sstevel@tonic-gate 	} else {
5607c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("       Dump device: none "
5617c478bd9Sstevel@tonic-gate 		    "(dumps disabled)\n"));
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, gettext("Savecore directory: %s"), dcp->dc_savdir);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (minfree_read(dcp->dc_savdir, &min) == 0) {
5677c478bd9Sstevel@tonic-gate 		if (min < 1024 || (min % 1024) != 0)
5687c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext(" (minfree = %lluKB)"), min);
5697c478bd9Sstevel@tonic-gate 		else
5707c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext(" (minfree = %lluMB)"),
5717c478bd9Sstevel@tonic-gate 			    min / 1024);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, gettext("\n"));
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, gettext("  Savecore enabled: %s\n"),
5777c478bd9Sstevel@tonic-gate 	    (dcp->dc_enable == DC_OFF) ? gettext("no") : gettext("yes"));
578ca3e8d88SDave Plauger 	(void) fprintf(fp, gettext("   Save compressed: %s\n"),
579ca3e8d88SDave Plauger 	    (dcp->dc_csave == DC_UNCOMPRESSED) ? gettext("off") :
580ca3e8d88SDave Plauger 	    gettext("on"));
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate int
dconf_str2device(dumpconf_t * dcp,char * buf)5847c478bd9Sstevel@tonic-gate dconf_str2device(dumpconf_t *dcp, char *buf)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_SWAP) == 0) {
5877c478bd9Sstevel@tonic-gate 		(void) strcpy(dcp->dc_device, DC_STR_SWAP);
5887c478bd9Sstevel@tonic-gate 		return (0);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
591c61ea566SGeorge Wilson 	if (strcasecmp(buf, DC_STR_NONE) == 0) {
592c61ea566SGeorge Wilson 		(void) strcpy(dcp->dc_device, DC_STR_NONE);
593c61ea566SGeorge Wilson 		return (0);
594c61ea566SGeorge Wilson 	}
595c61ea566SGeorge Wilson 
5967c478bd9Sstevel@tonic-gate 	if (valid_abspath(buf)) {
5977c478bd9Sstevel@tonic-gate 		(void) strcpy(dcp->dc_device, buf);
5987c478bd9Sstevel@tonic-gate 		return (0);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	return (-1);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate int
dconf_str2savdir(dumpconf_t * dcp,char * buf)6057c478bd9Sstevel@tonic-gate dconf_str2savdir(dumpconf_t *dcp, char *buf)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	if (valid_abspath(buf)) {
6087c478bd9Sstevel@tonic-gate 		(void) strcpy(dcp->dc_savdir, buf);
6097c478bd9Sstevel@tonic-gate 		return (0);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	return (-1);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate int
dconf_str2content(dumpconf_t * dcp,char * buf)6167c478bd9Sstevel@tonic-gate dconf_str2content(dumpconf_t *dcp, char *buf)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_KERNEL) == 0) {
6197c478bd9Sstevel@tonic-gate 		dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_KERNEL;
6207c478bd9Sstevel@tonic-gate 		return (0);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_CURPROC) == 0) {
6247c478bd9Sstevel@tonic-gate 		dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) |
6257c478bd9Sstevel@tonic-gate 		    DUMP_CURPROC;
6267c478bd9Sstevel@tonic-gate 		return (0);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_ALL) == 0) {
6307c478bd9Sstevel@tonic-gate 		dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_ALL;
6317c478bd9Sstevel@tonic-gate 		return (0);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	warn(gettext("invalid dump content type -- %s\n"), buf);
6357c478bd9Sstevel@tonic-gate 	return (-1);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate int
dconf_str2enable(dumpconf_t * dcp,char * buf)6397c478bd9Sstevel@tonic-gate dconf_str2enable(dumpconf_t *dcp, char *buf)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_YES) == 0) {
6427c478bd9Sstevel@tonic-gate 		dcp->dc_enable = DC_ON;
6437c478bd9Sstevel@tonic-gate 		return (0);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_NO) == 0) {
6477c478bd9Sstevel@tonic-gate 		dcp->dc_enable = DC_OFF;
6487c478bd9Sstevel@tonic-gate 		return (0);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	warn(gettext("invalid enable value -- %s\n"), buf);
6527c478bd9Sstevel@tonic-gate 	return (-1);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
655ca3e8d88SDave Plauger int
dconf_str2csave(dumpconf_t * dcp,char * buf)656ca3e8d88SDave Plauger dconf_str2csave(dumpconf_t *dcp, char *buf)
657ca3e8d88SDave Plauger {
658ca3e8d88SDave Plauger 	if (strcasecmp(buf, DC_STR_ON) == 0) {
659ca3e8d88SDave Plauger 		dcp->dc_csave = DC_COMPRESSED;
660ca3e8d88SDave Plauger 		return (0);
661ca3e8d88SDave Plauger 	}
662ca3e8d88SDave Plauger 
663ca3e8d88SDave Plauger 	if (strcasecmp(buf, DC_STR_OFF) == 0) {
664ca3e8d88SDave Plauger 		dcp->dc_csave = DC_UNCOMPRESSED;
665ca3e8d88SDave Plauger 		return (0);
666ca3e8d88SDave Plauger 	}
667ca3e8d88SDave Plauger 
668ca3e8d88SDave Plauger 	warn(gettext("invalid save compressed value -- %s\n"), buf);
669ca3e8d88SDave Plauger 	return (-1);
670ca3e8d88SDave Plauger }
671ca3e8d88SDave Plauger 
6727c478bd9Sstevel@tonic-gate static int
print_content(const dumpconf_t * dcp,FILE * fp)6737c478bd9Sstevel@tonic-gate print_content(const dumpconf_t *dcp, FILE *fp)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	const char *content;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (dcp->dc_cflags & DUMP_ALL)
6787c478bd9Sstevel@tonic-gate 		content = DC_STR_ALL;
6797c478bd9Sstevel@tonic-gate 	else if (dcp->dc_cflags & DUMP_CURPROC)
6807c478bd9Sstevel@tonic-gate 		content = DC_STR_CURPROC;
6817c478bd9Sstevel@tonic-gate 	else
6827c478bd9Sstevel@tonic-gate 		content = DC_STR_KERNEL;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", content));
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate static int
print_device(const dumpconf_t * dcp,FILE * fp)6887c478bd9Sstevel@tonic-gate print_device(const dumpconf_t *dcp, FILE *fp)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", (dcp->dc_device[0] != '\0') ?
6917c478bd9Sstevel@tonic-gate 	    dcp->dc_device : DC_STR_SWAP));
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate static int
print_enable(const dumpconf_t * dcp,FILE * fp)6957c478bd9Sstevel@tonic-gate print_enable(const dumpconf_t *dcp, FILE *fp)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", (dcp->dc_enable == DC_OFF) ?
6987c478bd9Sstevel@tonic-gate 	    DC_STR_NO : DC_STR_YES));
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate static int
print_csave(const dumpconf_t * dcp,FILE * fp)702ca3e8d88SDave Plauger print_csave(const dumpconf_t *dcp, FILE *fp)
703ca3e8d88SDave Plauger {
704ca3e8d88SDave Plauger 	return (fprintf(fp, "%s\n", (dcp->dc_csave == DC_COMPRESSED) ?
705ca3e8d88SDave Plauger 	    DC_STR_ON : DC_STR_OFF));
706ca3e8d88SDave Plauger }
707ca3e8d88SDave Plauger 
708ca3e8d88SDave Plauger static int
print_savdir(const dumpconf_t * dcp,FILE * fp)7097c478bd9Sstevel@tonic-gate print_savdir(const dumpconf_t *dcp, FILE *fp)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", dcp->dc_savdir));
7127c478bd9Sstevel@tonic-gate }
713