xref: /onnv-gate/usr/src/cmd/dumpadm/dconf.c (revision 12967:ab9ae749152f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54801Seschrock  * Common Development and Distribution License (the "License").
64801Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12967Sgavin.maltby@oracle.com  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/stat.h>
270Sstevel@tonic-gate #include <sys/swap.h>
280Sstevel@tonic-gate #include <sys/dumpadm.h>
290Sstevel@tonic-gate #include <sys/utsname.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <fcntl.h>
360Sstevel@tonic-gate #include <errno.h>
37767Ssjelinek #include <libdiskmgt.h>
386423Sgw25295 #include <libzfs.h>
39*12967Sgavin.maltby@oracle.com #include <uuid/uuid.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "dconf.h"
420Sstevel@tonic-gate #include "minfree.h"
430Sstevel@tonic-gate #include "utils.h"
440Sstevel@tonic-gate #include "swap.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate typedef struct dc_token {
470Sstevel@tonic-gate 	const char *tok_name;
480Sstevel@tonic-gate 	int (*tok_parse)(dumpconf_t *, char *);
490Sstevel@tonic-gate 	int (*tok_print)(const dumpconf_t *, FILE *);
500Sstevel@tonic-gate } dc_token_t;
510Sstevel@tonic-gate 
52767Ssjelinek 
530Sstevel@tonic-gate static int print_device(const dumpconf_t *, FILE *);
540Sstevel@tonic-gate static int print_savdir(const dumpconf_t *, FILE *);
550Sstevel@tonic-gate static int print_content(const dumpconf_t *, FILE *);
560Sstevel@tonic-gate static int print_enable(const dumpconf_t *, FILE *);
5710843SDave.Plauger@Sun.COM static int print_csave(const dumpconf_t *, FILE *);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static const dc_token_t tokens[] = {
600Sstevel@tonic-gate 	{ "DUMPADM_DEVICE", dconf_str2device, print_device },
610Sstevel@tonic-gate 	{ "DUMPADM_SAVDIR", dconf_str2savdir, print_savdir },
620Sstevel@tonic-gate 	{ "DUMPADM_CONTENT", dconf_str2content, print_content },
630Sstevel@tonic-gate 	{ "DUMPADM_ENABLE", dconf_str2enable, print_enable },
6410843SDave.Plauger@Sun.COM 	{ "DUMPADM_CSAVE", dconf_str2csave, print_csave },
650Sstevel@tonic-gate 	{ NULL, NULL, NULL }
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
6810843SDave.Plauger@Sun.COM static const char DC_STR_ON[] = "on";		/* On string */
6910843SDave.Plauger@Sun.COM static const char DC_STR_OFF[] = "off";		/* Off string */
700Sstevel@tonic-gate static const char DC_STR_YES[] = "yes";		/* Enable on string */
710Sstevel@tonic-gate static const char DC_STR_NO[] = "no";		/* Enable off string */
720Sstevel@tonic-gate static const char DC_STR_SWAP[] = "swap";	/* Default dump device */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* The pages included in the dump */
750Sstevel@tonic-gate static const char DC_STR_KERNEL[] = "kernel";	/* Kernel only */
760Sstevel@tonic-gate static const char DC_STR_CURPROC[] = "curproc";	/* Kernel + current process */
770Sstevel@tonic-gate static const char DC_STR_ALL[] = "all";		/* All pages */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Permissions and ownership for the configuration file:
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate #define	DC_OWNER	0				/* Uid 0 (root) */
830Sstevel@tonic-gate #define	DC_GROUP	1				/* Gid 1 (other) */
840Sstevel@tonic-gate #define	DC_PERM	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)	/* Mode 0644 */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static void
dconf_init(dumpconf_t * dcp,int dcmode)870Sstevel@tonic-gate dconf_init(dumpconf_t *dcp, int dcmode)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	struct utsname ut;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/*
920Sstevel@tonic-gate 	 * Default device for dumps is 'swap' (appropriate swap device),
930Sstevel@tonic-gate 	 * and default savecore directory is /var/crash/`uname -n`,
940Sstevel@tonic-gate 	 * which is compatible with pre-dumpadm behavior.
950Sstevel@tonic-gate 	 */
960Sstevel@tonic-gate 	(void) strcpy(dcp->dc_device, DC_STR_SWAP);
970Sstevel@tonic-gate 	(void) strcpy(dcp->dc_savdir, "/var/crash");
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (uname(&ut) != -1) {
1000Sstevel@tonic-gate 		(void) strcat(dcp->dc_savdir, "/");
1010Sstevel@tonic-gate 		(void) strcat(dcp->dc_savdir, ut.nodename);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	/*
10510843SDave.Plauger@Sun.COM 	 * Default is contents kernel, savecore enabled on reboot,
10610843SDave.Plauger@Sun.COM 	 * savecore saves compressed core files.
1070Sstevel@tonic-gate 	 */
1080Sstevel@tonic-gate 	dcp->dc_cflags = DUMP_KERNEL;
1090Sstevel@tonic-gate 	dcp->dc_enable = DC_ON;
11010843SDave.Plauger@Sun.COM 	dcp->dc_csave = DC_COMPRESSED;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	dcp->dc_mode = dcmode;
1130Sstevel@tonic-gate 	dcp->dc_conf_fp = NULL;
1140Sstevel@tonic-gate 	dcp->dc_conf_fd = -1;
1150Sstevel@tonic-gate 	dcp->dc_dump_fd = -1;
1164801Seschrock 	dcp->dc_readonly = B_FALSE;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate int
dconf_open(dumpconf_t * dcp,const char * dpath,const char * fpath,int dcmode)1200Sstevel@tonic-gate dconf_open(dumpconf_t *dcp, const char *dpath, const char *fpath, int dcmode)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	char buf[BUFSIZ];
1230Sstevel@tonic-gate 	int line;
1244801Seschrock 	const char *fpmode = "r+";
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	dconf_init(dcp, dcmode);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if ((dcp->dc_dump_fd = open(dpath, O_RDWR)) == -1) {
1290Sstevel@tonic-gate 		warn(gettext("failed to open %s"), dpath);
1300Sstevel@tonic-gate 		return (-1);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if ((dcp->dc_conf_fd = open(fpath, O_RDWR | O_CREAT, DC_PERM)) == -1) {
1344801Seschrock 		/*
1354801Seschrock 		 * Attempt to open the file read-only.
1364801Seschrock 		 */
1374801Seschrock 		if ((dcp->dc_conf_fd = open(fpath, O_RDONLY)) == -1) {
1384801Seschrock 			warn(gettext("failed to open %s"), fpath);
1394801Seschrock 			return (-1);
1404801Seschrock 		}
1414801Seschrock 
1424801Seschrock 		dcp->dc_readonly = B_TRUE;
1434801Seschrock 		fpmode = "r";
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1464801Seschrock 	if ((dcp->dc_conf_fp = fdopen(dcp->dc_conf_fd, fpmode)) == NULL) {
1470Sstevel@tonic-gate 		warn(gettext("failed to open stream for %s"), fpath);
1480Sstevel@tonic-gate 		return (-1);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * If we're in override mode, the current kernel settings override the
1530Sstevel@tonic-gate 	 * default settings and anything invalid in the configuration file.
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	if (dcmode == DC_OVERRIDE)
1560Sstevel@tonic-gate 		(void) dconf_getdev(dcp);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	for (line = 1; fgets(buf, BUFSIZ, dcp->dc_conf_fp) != NULL; line++) {
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		char name[BUFSIZ], value[BUFSIZ];
1610Sstevel@tonic-gate 		const dc_token_t *tokp;
1620Sstevel@tonic-gate 		int len;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		if (buf[0] == '#' || buf[0] == '\n')
1650Sstevel@tonic-gate 			continue;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * Look for "name=value", with optional whitespace on either
1690Sstevel@tonic-gate 		 * side, terminated by a newline, and consuming the whole line.
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		/* LINTED - unbounded string specifier */
1720Sstevel@tonic-gate 		if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 &&
1730Sstevel@tonic-gate 		    name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) {
1740Sstevel@tonic-gate 			/*
1750Sstevel@tonic-gate 			 * Locate a matching token in the tokens[] table,
1760Sstevel@tonic-gate 			 * and invoke its parsing function.
1770Sstevel@tonic-gate 			 */
1780Sstevel@tonic-gate 			for (tokp = tokens; tokp->tok_name != NULL; tokp++) {
1790Sstevel@tonic-gate 				if (strcmp(name, tokp->tok_name) == 0) {
1800Sstevel@tonic-gate 					if (tokp->tok_parse(dcp, value) == -1) {
1810Sstevel@tonic-gate 						warn(gettext("\"%s\", line %d: "
1820Sstevel@tonic-gate 						    "warning: invalid %s\n"),
1830Sstevel@tonic-gate 						    fpath, line, name);
1840Sstevel@tonic-gate 					}
1850Sstevel@tonic-gate 					break;
1860Sstevel@tonic-gate 				}
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 			/*
1900Sstevel@tonic-gate 			 * If we hit the end of the tokens[] table,
1910Sstevel@tonic-gate 			 * no matching token was found.
1920Sstevel@tonic-gate 			 */
1930Sstevel@tonic-gate 			if (tokp->tok_name == NULL) {
1940Sstevel@tonic-gate 				warn(gettext("\"%s\", line %d: warning: "
1950Sstevel@tonic-gate 				    "invalid token: %s\n"), fpath, line, name);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		} else {
1990Sstevel@tonic-gate 			warn(gettext("\"%s\", line %d: syntax error\n"),
2000Sstevel@tonic-gate 			    fpath, line);
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * If we're not in override mode, the current kernel settings
2060Sstevel@tonic-gate 	 * override the settings read from the configuration file.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	if (dcmode == DC_CURRENT)
2090Sstevel@tonic-gate 		return (dconf_getdev(dcp));
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	return (0);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate int
dconf_getdev(dumpconf_t * dcp)2150Sstevel@tonic-gate dconf_getdev(dumpconf_t *dcp)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	int status = 0;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) {
2200Sstevel@tonic-gate 		warn(gettext("failed to get kernel dump settings"));
2210Sstevel@tonic-gate 		status = -1;
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if (ioctl(dcp->dc_dump_fd, DIOCGETDEV, dcp->dc_device) == -1) {
2250Sstevel@tonic-gate 		if (errno != ENODEV) {
2260Sstevel@tonic-gate 			warn(gettext("failed to get dump device"));
2270Sstevel@tonic-gate 			status = -1;
2280Sstevel@tonic-gate 		} else
2290Sstevel@tonic-gate 			dcp->dc_device[0] = '\0';
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	return (status);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate int
dconf_close(dumpconf_t * dcp)2360Sstevel@tonic-gate dconf_close(dumpconf_t *dcp)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	if (fclose(dcp->dc_conf_fp) == 0) {
2390Sstevel@tonic-gate 		(void) close(dcp->dc_dump_fd);
2400Sstevel@tonic-gate 		return (0);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	return (-1);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate int
dconf_write(dumpconf_t * dcp)2460Sstevel@tonic-gate dconf_write(dumpconf_t *dcp)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	const dc_token_t *tokp;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (fseeko(dcp->dc_conf_fp, (off_t)0, SEEK_SET) == -1) {
2510Sstevel@tonic-gate 		warn(gettext("failed to seek config file"));
2520Sstevel@tonic-gate 		return (-1);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (ftruncate(dcp->dc_conf_fd, (off_t)0) == -1) {
2560Sstevel@tonic-gate 		warn(gettext("failed to truncate config file"));
2570Sstevel@tonic-gate 		return (-1);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	(void) fputs("#\n# dumpadm.conf\n#\n"
2610Sstevel@tonic-gate 	    "# Configuration parameters for system crash dump.\n"
2620Sstevel@tonic-gate 	    "# Do NOT edit this file by hand -- use dumpadm(1m) instead.\n"
2630Sstevel@tonic-gate 	    "#\n", dcp->dc_conf_fp);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	for (tokp = tokens; tokp->tok_name != NULL; tokp++) {
2660Sstevel@tonic-gate 		if (fprintf(dcp->dc_conf_fp, "%s=", tokp->tok_name) == -1 ||
2670Sstevel@tonic-gate 		    tokp->tok_print(dcp, dcp->dc_conf_fp) == -1) {
2680Sstevel@tonic-gate 			warn(gettext("failed to write token"));
2690Sstevel@tonic-gate 			return (-1);
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (fflush(dcp->dc_conf_fp) != 0)
2740Sstevel@tonic-gate 		warn(gettext("warning: failed to flush config file"));
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (fsync(dcp->dc_conf_fd) == -1)
2770Sstevel@tonic-gate 		warn(gettext("warning: failed to sync config file to disk"));
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (fchmod(dcp->dc_conf_fd, DC_PERM) == -1)
2800Sstevel@tonic-gate 		warn(gettext("warning: failed to reset mode on config file"));
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (fchown(dcp->dc_conf_fd, DC_OWNER, DC_GROUP) == -1)
2830Sstevel@tonic-gate 		warn(gettext("warning: failed to reset owner on config file"));
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	return (0);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static int
open_stat64(const char * path,struct stat64 * stp)2890Sstevel@tonic-gate open_stat64(const char *path, struct stat64 *stp)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	int fd = open64(path, O_RDONLY);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	if (fd >= 0) {
2940Sstevel@tonic-gate 		int status = fstat64(fd, stp);
2950Sstevel@tonic-gate 		(void) close(fd);
2960Sstevel@tonic-gate 		return (status);
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (-1);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate static int
dconf_swap_compare(const swapent_t * s1,const swapent_t * s2)3030Sstevel@tonic-gate dconf_swap_compare(const swapent_t *s1, const swapent_t *s2)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	struct stat64 st1, st2;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	int prefer_s1 = -1;	/* Return value to move s1 left (s1 < s2) */
3080Sstevel@tonic-gate 	int prefer_s2 = 1;	/* Return value to move s2 left (s1 > s2) */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/*
3110Sstevel@tonic-gate 	 * First try: open and fstat each swap entry.  If either system
3120Sstevel@tonic-gate 	 * call fails, arbitrarily prefer the other entry.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	if (open_stat64(s1->ste_path, &st1) == -1)
3150Sstevel@tonic-gate 		return (prefer_s2);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (open_stat64(s2->ste_path, &st2) == -1)
3180Sstevel@tonic-gate 		return (prefer_s1);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Second try: if both entries are block devices, or if
3220Sstevel@tonic-gate 	 * neither is a block device, prefer the larger.
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	if (S_ISBLK(st1.st_mode) == S_ISBLK(st2.st_mode)) {
3250Sstevel@tonic-gate 		if (st2.st_size > st1.st_size)
3260Sstevel@tonic-gate 			return (prefer_s2);
3270Sstevel@tonic-gate 		return (prefer_s1);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * Third try: prefer the entry that is a block device.
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	if (S_ISBLK(st2.st_mode))
3340Sstevel@tonic-gate 		return (prefer_s2);
3350Sstevel@tonic-gate 	return (prefer_s1);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static int
dconf_dev_ioctl(dumpconf_t * dcp,int cmd)3390Sstevel@tonic-gate dconf_dev_ioctl(dumpconf_t *dcp, int cmd)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	if (ioctl(dcp->dc_dump_fd, cmd, dcp->dc_device) == 0)
3420Sstevel@tonic-gate 		return (0);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	switch (errno) {
3450Sstevel@tonic-gate 	case ENOTSUP:
3460Sstevel@tonic-gate 		warn(gettext("dumps not supported on %s\n"), dcp->dc_device);
3470Sstevel@tonic-gate 		break;
3480Sstevel@tonic-gate 	case EBUSY:
3490Sstevel@tonic-gate 		warn(gettext("device %s is already in use\n"), dcp->dc_device);
3500Sstevel@tonic-gate 		break;
3516423Sgw25295 	case EBADR:
3526423Sgw25295 		/* ZFS pool is too fragmented to support a dump device */
3536423Sgw25295 		warn(gettext("device %s is too fragmented to be used as "
3546423Sgw25295 		    "a dump device\n"), dcp->dc_device);
3556423Sgw25295 		break;
3560Sstevel@tonic-gate 	default:
3570Sstevel@tonic-gate 		/*
3580Sstevel@tonic-gate 		 * NOTE: The stmsboot(1M) command's boot-up script parses this
3590Sstevel@tonic-gate 		 * error to get the dump device name. If you change the format
3600Sstevel@tonic-gate 		 * of this message, make sure that stmsboot(1M) is in sync.
3610Sstevel@tonic-gate 		 */
3620Sstevel@tonic-gate 		warn(gettext("cannot use %s as dump device"), dcp->dc_device);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	return (-1);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate int
dconf_update(dumpconf_t * dcp,int checkinuse)368767Ssjelinek dconf_update(dumpconf_t *dcp, int checkinuse)
3690Sstevel@tonic-gate {
370767Ssjelinek 	int 		oconf;
371767Ssjelinek 	int		error;
372767Ssjelinek 	char		*msg;
373767Ssjelinek 
374767Ssjelinek 	error = 0;
375767Ssjelinek 
376767Ssjelinek 	if (checkinuse && (dm_inuse(dcp->dc_device, &msg, DM_WHO_DUMP,
3774801Seschrock 	    &error) || error)) {
378767Ssjelinek 		if (error != 0) {
379767Ssjelinek 			warn(gettext("failed to determine if %s is"
380767Ssjelinek 			    " in use"), dcp->dc_device);
381767Ssjelinek 		} else {
382767Ssjelinek 			warn(msg);
383767Ssjelinek 			free(msg);
384767Ssjelinek 			return (-1);
385767Ssjelinek 		}
386767Ssjelinek 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/*
3890Sstevel@tonic-gate 	 * Save the existing dump configuration in case something goes wrong.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	if ((oconf = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) {
3920Sstevel@tonic-gate 		warn(gettext("failed to get kernel dump configuration"));
3930Sstevel@tonic-gate 		return (-1);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	oconf &= DUMP_CONTENT;
3970Sstevel@tonic-gate 	dcp->dc_cflags &= DUMP_CONTENT;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	if (ioctl(dcp->dc_dump_fd, DIOCSETCONF, dcp->dc_cflags) == -1) {
4000Sstevel@tonic-gate 		warn(gettext("failed to update kernel dump configuration"));
4010Sstevel@tonic-gate 		return (-1);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	if (strcmp(dcp->dc_device, DC_STR_SWAP) == 0) {
4050Sstevel@tonic-gate 		swaptbl_t *swt;
4060Sstevel@tonic-gate 		int i;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		if ((swt = swap_list()) == NULL)
4090Sstevel@tonic-gate 			goto err;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		if (swt->swt_n == 0) {
4120Sstevel@tonic-gate 			warn(gettext("no swap devices are available\n"));
4130Sstevel@tonic-gate 			free(swt);
4140Sstevel@tonic-gate 			goto err;
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		qsort(&swt->swt_ent[0], swt->swt_n, sizeof (swapent_t),
4180Sstevel@tonic-gate 		    (int (*)(const void *, const void *))dconf_swap_compare);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		/*
4210Sstevel@tonic-gate 		 * Iterate through the prioritized list of swap entries,
4220Sstevel@tonic-gate 		 * trying to configure one as the dump device.
4230Sstevel@tonic-gate 		 */
4240Sstevel@tonic-gate 		for (i = 0; i < swt->swt_n; i++) {
4250Sstevel@tonic-gate 			if (ioctl(dcp->dc_dump_fd, DIOCSETDEV,
4260Sstevel@tonic-gate 			    swt->swt_ent[i].ste_path) == 0) {
4270Sstevel@tonic-gate 				(void) strcpy(dcp->dc_device,
4280Sstevel@tonic-gate 				    swt->swt_ent[i].ste_path);
4290Sstevel@tonic-gate 				break;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		if (i == swt->swt_n) {
4340Sstevel@tonic-gate 			warn(gettext("no swap devices could be configured "
4350Sstevel@tonic-gate 			    "as the dump device\n"));
4360Sstevel@tonic-gate 			free(swt);
4370Sstevel@tonic-gate 			goto err;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 		free(swt);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	} else if (dcp->dc_device[0] != '\0') {
4420Sstevel@tonic-gate 		/*
4430Sstevel@tonic-gate 		 * If we're not in forcible update mode, then fail the change
4440Sstevel@tonic-gate 		 * if the selected device cannot be used as the dump device,
4450Sstevel@tonic-gate 		 * or if it is not big enough to hold the dump.
4460Sstevel@tonic-gate 		 */
4470Sstevel@tonic-gate 		if (dcp->dc_mode == DC_CURRENT) {
4480Sstevel@tonic-gate 			struct stat64 st;
4490Sstevel@tonic-gate 			uint64_t d;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 			if (dconf_dev_ioctl(dcp, DIOCTRYDEV) == -1)
4520Sstevel@tonic-gate 				goto err;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 			if (open_stat64(dcp->dc_device, &st) == -1) {
4550Sstevel@tonic-gate 				warn(gettext("failed to access %s"),
4560Sstevel@tonic-gate 				    dcp->dc_device);
4570Sstevel@tonic-gate 				goto err;
4580Sstevel@tonic-gate 			}
4590Sstevel@tonic-gate 
4606423Sgw25295 			if ((error = zvol_check_dump_config(
4616423Sgw25295 			    dcp->dc_device)) > 0)
4626423Sgw25295 				goto err;
4630Sstevel@tonic-gate 			if (ioctl(dcp->dc_dump_fd, DIOCGETDUMPSIZE, &d) == -1) {
4640Sstevel@tonic-gate 				warn(gettext("failed to get kernel dump size"));
4650Sstevel@tonic-gate 				goto err;
4660Sstevel@tonic-gate 			}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 			if (st.st_size < d) {
4690Sstevel@tonic-gate 				warn(gettext("dump device %s is too small to "
4700Sstevel@tonic-gate 				    "hold a system dump\ndump size %llu "
4710Sstevel@tonic-gate 				    "bytes, device size %lld bytes\n"),
4720Sstevel@tonic-gate 				    dcp->dc_device, d, st.st_size);
4730Sstevel@tonic-gate 				goto err;
4740Sstevel@tonic-gate 			}
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (dconf_dev_ioctl(dcp, DIOCSETDEV) == -1)
4780Sstevel@tonic-gate 			goto err;
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * Now that we've updated the dump device, we need to issue another
4830Sstevel@tonic-gate 	 * ioctl to re-read the config flags to determine whether we
4840Sstevel@tonic-gate 	 * obtained DUMP_EXCL access on our dump device.
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) {
4870Sstevel@tonic-gate 		warn(gettext("failed to re-read kernel dump configuration"));
4880Sstevel@tonic-gate 		return (-1);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	return (0);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate err:
4940Sstevel@tonic-gate 	(void) ioctl(dcp->dc_dump_fd, DIOCSETCONF, oconf);
4950Sstevel@tonic-gate 	return (-1);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
498*12967Sgavin.maltby@oracle.com int
dconf_write_uuid(dumpconf_t * dcp)499*12967Sgavin.maltby@oracle.com dconf_write_uuid(dumpconf_t *dcp)
500*12967Sgavin.maltby@oracle.com {
501*12967Sgavin.maltby@oracle.com 	char uuidstr[36 + 1];
502*12967Sgavin.maltby@oracle.com 	uuid_t uu;
503*12967Sgavin.maltby@oracle.com 	int err;
504*12967Sgavin.maltby@oracle.com 
505*12967Sgavin.maltby@oracle.com 	uuid_generate(uu);
506*12967Sgavin.maltby@oracle.com 	uuid_unparse(uu, uuidstr);
507*12967Sgavin.maltby@oracle.com 
508*12967Sgavin.maltby@oracle.com 	err = ioctl(dcp->dc_dump_fd, DIOCSETUUID, uuidstr);
509*12967Sgavin.maltby@oracle.com 
510*12967Sgavin.maltby@oracle.com 	if (err)
511*12967Sgavin.maltby@oracle.com 		warn(gettext("kernel image uuid write failed"));
512*12967Sgavin.maltby@oracle.com 
513*12967Sgavin.maltby@oracle.com 	return (err == 0);
514*12967Sgavin.maltby@oracle.com }
515*12967Sgavin.maltby@oracle.com 
5160Sstevel@tonic-gate void
dconf_print(dumpconf_t * dcp,FILE * fp)5170Sstevel@tonic-gate dconf_print(dumpconf_t *dcp, FILE *fp)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	u_longlong_t min;
5200Sstevel@tonic-gate 	char *content;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (dcp->dc_cflags & DUMP_ALL)
5230Sstevel@tonic-gate 		content = gettext("all");
5240Sstevel@tonic-gate 	else if (dcp->dc_cflags & DUMP_CURPROC)
5250Sstevel@tonic-gate 		content = gettext("kernel and current process");
5260Sstevel@tonic-gate 	else
5270Sstevel@tonic-gate 		content = gettext("kernel");
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	(void) fprintf(fp, gettext("      Dump content: %s pages\n"), content);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (dcp->dc_device[0] != '\0') {
5320Sstevel@tonic-gate 		(void) fprintf(fp, gettext("       Dump device: %s (%s)\n"),
5330Sstevel@tonic-gate 		    dcp->dc_device, (dcp->dc_cflags & DUMP_EXCL) ?
5340Sstevel@tonic-gate 		    gettext("dedicated") : gettext("swap"));
5350Sstevel@tonic-gate 	} else {
5360Sstevel@tonic-gate 		(void) fprintf(fp, gettext("       Dump device: none "
5370Sstevel@tonic-gate 		    "(dumps disabled)\n"));
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	(void) fprintf(fp, gettext("Savecore directory: %s"), dcp->dc_savdir);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (minfree_read(dcp->dc_savdir, &min) == 0) {
5430Sstevel@tonic-gate 		if (min < 1024 || (min % 1024) != 0)
5440Sstevel@tonic-gate 			(void) fprintf(fp, gettext(" (minfree = %lluKB)"), min);
5450Sstevel@tonic-gate 		else
5460Sstevel@tonic-gate 			(void) fprintf(fp, gettext(" (minfree = %lluMB)"),
5470Sstevel@tonic-gate 			    min / 1024);
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	(void) fprintf(fp, gettext("\n"));
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	(void) fprintf(fp, gettext("  Savecore enabled: %s\n"),
5530Sstevel@tonic-gate 	    (dcp->dc_enable == DC_OFF) ? gettext("no") : gettext("yes"));
55410843SDave.Plauger@Sun.COM 	(void) fprintf(fp, gettext("   Save compressed: %s\n"),
55510843SDave.Plauger@Sun.COM 	    (dcp->dc_csave == DC_UNCOMPRESSED) ? gettext("off") :
55610843SDave.Plauger@Sun.COM 	    gettext("on"));
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate int
dconf_str2device(dumpconf_t * dcp,char * buf)5600Sstevel@tonic-gate dconf_str2device(dumpconf_t *dcp, char *buf)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_SWAP) == 0) {
5630Sstevel@tonic-gate 		(void) strcpy(dcp->dc_device, DC_STR_SWAP);
5640Sstevel@tonic-gate 		return (0);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (valid_abspath(buf)) {
5680Sstevel@tonic-gate 		(void) strcpy(dcp->dc_device, buf);
5690Sstevel@tonic-gate 		return (0);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	return (-1);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate int
dconf_str2savdir(dumpconf_t * dcp,char * buf)5760Sstevel@tonic-gate dconf_str2savdir(dumpconf_t *dcp, char *buf)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	if (valid_abspath(buf)) {
5790Sstevel@tonic-gate 		(void) strcpy(dcp->dc_savdir, buf);
5800Sstevel@tonic-gate 		return (0);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	return (-1);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate int
dconf_str2content(dumpconf_t * dcp,char * buf)5870Sstevel@tonic-gate dconf_str2content(dumpconf_t *dcp, char *buf)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_KERNEL) == 0) {
5900Sstevel@tonic-gate 		dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_KERNEL;
5910Sstevel@tonic-gate 		return (0);
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_CURPROC) == 0) {
5950Sstevel@tonic-gate 		dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) |
5960Sstevel@tonic-gate 		    DUMP_CURPROC;
5970Sstevel@tonic-gate 		return (0);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_ALL) == 0) {
6010Sstevel@tonic-gate 		dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_ALL;
6020Sstevel@tonic-gate 		return (0);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	warn(gettext("invalid dump content type -- %s\n"), buf);
6060Sstevel@tonic-gate 	return (-1);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate int
dconf_str2enable(dumpconf_t * dcp,char * buf)6100Sstevel@tonic-gate dconf_str2enable(dumpconf_t *dcp, char *buf)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_YES) == 0) {
6130Sstevel@tonic-gate 		dcp->dc_enable = DC_ON;
6140Sstevel@tonic-gate 		return (0);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	if (strcasecmp(buf, DC_STR_NO) == 0) {
6180Sstevel@tonic-gate 		dcp->dc_enable = DC_OFF;
6190Sstevel@tonic-gate 		return (0);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	warn(gettext("invalid enable value -- %s\n"), buf);
6230Sstevel@tonic-gate 	return (-1);
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
62610843SDave.Plauger@Sun.COM int
dconf_str2csave(dumpconf_t * dcp,char * buf)62710843SDave.Plauger@Sun.COM dconf_str2csave(dumpconf_t *dcp, char *buf)
62810843SDave.Plauger@Sun.COM {
62910843SDave.Plauger@Sun.COM 	if (strcasecmp(buf, DC_STR_ON) == 0) {
63010843SDave.Plauger@Sun.COM 		dcp->dc_csave = DC_COMPRESSED;
63110843SDave.Plauger@Sun.COM 		return (0);
63210843SDave.Plauger@Sun.COM 	}
63310843SDave.Plauger@Sun.COM 
63410843SDave.Plauger@Sun.COM 	if (strcasecmp(buf, DC_STR_OFF) == 0) {
63510843SDave.Plauger@Sun.COM 		dcp->dc_csave = DC_UNCOMPRESSED;
63610843SDave.Plauger@Sun.COM 		return (0);
63710843SDave.Plauger@Sun.COM 	}
63810843SDave.Plauger@Sun.COM 
63910843SDave.Plauger@Sun.COM 	warn(gettext("invalid save compressed value -- %s\n"), buf);
64010843SDave.Plauger@Sun.COM 	return (-1);
64110843SDave.Plauger@Sun.COM }
64210843SDave.Plauger@Sun.COM 
6430Sstevel@tonic-gate static int
print_content(const dumpconf_t * dcp,FILE * fp)6440Sstevel@tonic-gate print_content(const dumpconf_t *dcp, FILE *fp)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	const char *content;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (dcp->dc_cflags & DUMP_ALL)
6490Sstevel@tonic-gate 		content = DC_STR_ALL;
6500Sstevel@tonic-gate 	else if (dcp->dc_cflags & DUMP_CURPROC)
6510Sstevel@tonic-gate 		content = DC_STR_CURPROC;
6520Sstevel@tonic-gate 	else
6530Sstevel@tonic-gate 		content = DC_STR_KERNEL;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", content));
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate static int
print_device(const dumpconf_t * dcp,FILE * fp)6590Sstevel@tonic-gate print_device(const dumpconf_t *dcp, FILE *fp)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", (dcp->dc_device[0] != '\0') ?
6620Sstevel@tonic-gate 	    dcp->dc_device : DC_STR_SWAP));
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate static int
print_enable(const dumpconf_t * dcp,FILE * fp)6660Sstevel@tonic-gate print_enable(const dumpconf_t *dcp, FILE *fp)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", (dcp->dc_enable == DC_OFF) ?
6690Sstevel@tonic-gate 	    DC_STR_NO : DC_STR_YES));
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate static int
print_csave(const dumpconf_t * dcp,FILE * fp)67310843SDave.Plauger@Sun.COM print_csave(const dumpconf_t *dcp, FILE *fp)
67410843SDave.Plauger@Sun.COM {
67510843SDave.Plauger@Sun.COM 	return (fprintf(fp, "%s\n", (dcp->dc_csave == DC_COMPRESSED) ?
67610843SDave.Plauger@Sun.COM 	    DC_STR_ON : DC_STR_OFF));
67710843SDave.Plauger@Sun.COM }
67810843SDave.Plauger@Sun.COM 
67910843SDave.Plauger@Sun.COM static int
print_savdir(const dumpconf_t * dcp,FILE * fp)6800Sstevel@tonic-gate print_savdir(const dumpconf_t *dcp, FILE *fp)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	return (fprintf(fp, "%s\n", dcp->dc_savdir));
6830Sstevel@tonic-gate }
684