xref: /onnv-gate/usr/src/cmd/dumpadm/main.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/stat.h>
260Sstevel@tonic-gate #include <locale.h>
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <stdio.h>
30*12967Sgavin.maltby@oracle.com #include <string.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "dconf.h"
330Sstevel@tonic-gate #include "minfree.h"
340Sstevel@tonic-gate #include "utils.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate static const char USAGE[] = "\
370Sstevel@tonic-gate Usage: %s [-nuy] [-c kernel | curproc | all ] [-d dump-device | swap ]\n\
3810843SDave.Plauger@Sun.COM 	[-m min {k|m|%%} ] [-s savecore-dir] [-r root-dir] [-z on|off]\n";
390Sstevel@tonic-gate 
40*12967Sgavin.maltby@oracle.com static const char OPTS[] = "inuyc:d:m:s:r:z:";
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static const char PATH_DEVICE[] = "/dev/dump";
430Sstevel@tonic-gate static const char PATH_CONFIG[] = "/etc/dumpadm.conf";
440Sstevel@tonic-gate 
450Sstevel@tonic-gate int
main(int argc,char * argv[])460Sstevel@tonic-gate main(int argc, char *argv[])
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	const char *pname = getpname(argv[0]);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	u_longlong_t minf;
510Sstevel@tonic-gate 	struct stat st;
520Sstevel@tonic-gate 	int c;
53767Ssjelinek 	int dflag = 0;			/* for checking in use during -d ops */
540Sstevel@tonic-gate 	int dcmode = DC_CURRENT;	/* kernel settings override unless -u */
550Sstevel@tonic-gate 	int modified = 0;		/* have we modified the dump config? */
560Sstevel@tonic-gate 	char *minfstr = NULL;		/* string value of -m argument */
570Sstevel@tonic-gate 	dumpconf_t dc;			/* current configuration */
58*12967Sgavin.maltby@oracle.com 	int chrooted = 0;
59*12967Sgavin.maltby@oracle.com 	int douuid = 0;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
620Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/*
650Sstevel@tonic-gate 	 * Take an initial lap through argv hunting for -r root-dir,
660Sstevel@tonic-gate 	 * so that we can chroot before opening the configuration file.
670Sstevel@tonic-gate 	 * We also handle -u and any bad options at this point.
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	while (optind < argc) {
700Sstevel@tonic-gate 		while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
71*12967Sgavin.maltby@oracle.com 			if (c == 'r' && ++chrooted && chroot(optarg) == -1)
720Sstevel@tonic-gate 				die(gettext("failed to chroot to %s"), optarg);
730Sstevel@tonic-gate 			else if (c == 'u')
740Sstevel@tonic-gate 				dcmode = DC_OVERRIDE;
750Sstevel@tonic-gate 			else if (c == '?') {
760Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(USAGE), pname);
770Sstevel@tonic-gate 				return (E_USAGE);
780Sstevel@tonic-gate 			}
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		if (optind < argc) {
820Sstevel@tonic-gate 			warn(gettext("illegal argument -- %s\n"), argv[optind]);
830Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(USAGE), pname);
840Sstevel@tonic-gate 			return (E_USAGE);
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (geteuid() != 0)
890Sstevel@tonic-gate 		die(gettext("you must be root to use %s\n"), pname);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/*
920Sstevel@tonic-gate 	 * If no config file exists yet, we're going to create an empty one,
930Sstevel@tonic-gate 	 * so set the modified flag to force writing out the file.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	if (access(PATH_CONFIG, F_OK) == -1)
960Sstevel@tonic-gate 		modified++;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * Now open and read in the initial values from the config file.
1000Sstevel@tonic-gate 	 * If it doesn't exist, we create an empty file and dc is
1010Sstevel@tonic-gate 	 * initialized with the default values.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	if (dconf_open(&dc, PATH_DEVICE, PATH_CONFIG, dcmode) == -1)
1040Sstevel@tonic-gate 		return (E_ERROR);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * Take another lap through argv, processing options and
1080Sstevel@tonic-gate 	 * modifying the dumpconf_t as appropriate.
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
1110Sstevel@tonic-gate 		while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
1120Sstevel@tonic-gate 			switch (c) {
1130Sstevel@tonic-gate 			case 'c':
1140Sstevel@tonic-gate 				if (dconf_str2content(&dc, optarg) == -1)
1150Sstevel@tonic-gate 					return (E_USAGE);
1160Sstevel@tonic-gate 				modified++;
1170Sstevel@tonic-gate 				break;
1180Sstevel@tonic-gate 			case 'd':
1190Sstevel@tonic-gate 				if (dconf_str2device(&dc, optarg) == -1)
1200Sstevel@tonic-gate 					return (E_USAGE);
121767Ssjelinek 				dflag++;
1220Sstevel@tonic-gate 				modified++;
1230Sstevel@tonic-gate 				break;
1240Sstevel@tonic-gate 
125*12967Sgavin.maltby@oracle.com 			case 'i':
126*12967Sgavin.maltby@oracle.com 				/* undocumented option */
127*12967Sgavin.maltby@oracle.com 				if (chrooted) {
128*12967Sgavin.maltby@oracle.com 					warn(gettext("-i and -r cannot be "
129*12967Sgavin.maltby@oracle.com 					    "used together\n"));
130*12967Sgavin.maltby@oracle.com 					return (E_USAGE);
131*12967Sgavin.maltby@oracle.com 				}
132*12967Sgavin.maltby@oracle.com 				douuid++;
133*12967Sgavin.maltby@oracle.com 				break;
134*12967Sgavin.maltby@oracle.com 
1350Sstevel@tonic-gate 			case 'm':
1360Sstevel@tonic-gate 				minfstr = optarg;
1370Sstevel@tonic-gate 				break;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 			case 'n':
1400Sstevel@tonic-gate 				dc.dc_enable = DC_OFF;
1410Sstevel@tonic-gate 				modified++;
1420Sstevel@tonic-gate 				break;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 			case 's':
1450Sstevel@tonic-gate 				if (stat(optarg, &st) == -1 ||
1460Sstevel@tonic-gate 				    !S_ISDIR(st.st_mode)) {
1470Sstevel@tonic-gate 					warn(gettext("%s is missing or not a "
1480Sstevel@tonic-gate 					    "directory\n"), optarg);
1490Sstevel@tonic-gate 					return (E_USAGE);
1500Sstevel@tonic-gate 				}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 				if (dconf_str2savdir(&dc, optarg) == -1)
1530Sstevel@tonic-gate 					return (E_USAGE);
1540Sstevel@tonic-gate 				modified++;
1550Sstevel@tonic-gate 				break;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 			case 'y':
1580Sstevel@tonic-gate 				dc.dc_enable = DC_ON;
1590Sstevel@tonic-gate 				modified++;
1600Sstevel@tonic-gate 				break;
16110843SDave.Plauger@Sun.COM 
16210843SDave.Plauger@Sun.COM 			case 'z':
16310843SDave.Plauger@Sun.COM 				if (dconf_str2csave(&dc, optarg) == -1)
16410843SDave.Plauger@Sun.COM 					return (E_USAGE);
16510843SDave.Plauger@Sun.COM 				modified++;
16610843SDave.Plauger@Sun.COM 				break;
1670Sstevel@tonic-gate 			}
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
171*12967Sgavin.maltby@oracle.com 	if (douuid)
172*12967Sgavin.maltby@oracle.com 		return (dconf_write_uuid(&dc) ? E_SUCCESS : E_ERROR);
173*12967Sgavin.maltby@oracle.com 
1740Sstevel@tonic-gate 	if (minfstr != NULL) {
1750Sstevel@tonic-gate 		if (minfree_compute(dc.dc_savdir, minfstr, &minf) == -1)
1760Sstevel@tonic-gate 			return (E_USAGE);
1770Sstevel@tonic-gate 		if (minfree_write(dc.dc_savdir, minf) == -1)
1780Sstevel@tonic-gate 			return (E_ERROR);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (dcmode == DC_OVERRIDE) {
1820Sstevel@tonic-gate 		/*
1830Sstevel@tonic-gate 		 * In override mode, we try to force an update.  If this
1840Sstevel@tonic-gate 		 * fails, we re-load the kernel configuration and write that
1850Sstevel@tonic-gate 		 * out to the file in order to force the file in sync.
1864801Seschrock 		 *
1874801Seschrock 		 * We allow the file to be read-only but print a warning to the
1884801Seschrock 		 * user that indicates it hasn't been updated.
1890Sstevel@tonic-gate 		 */
190767Ssjelinek 		if (dconf_update(&dc, 0) == -1)
1910Sstevel@tonic-gate 			(void) dconf_getdev(&dc);
1924801Seschrock 		if (dc.dc_readonly)
1934801Seschrock 			warn(gettext("kernel settings updated, but "
1944801Seschrock 			    "%s is read-only\n"), PATH_CONFIG);
1954801Seschrock 		else if (dconf_write(&dc) == -1)
1960Sstevel@tonic-gate 			return (E_ERROR);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	} else if (modified) {
1990Sstevel@tonic-gate 		/*
2000Sstevel@tonic-gate 		 * If we're modifying the configuration, then try
2010Sstevel@tonic-gate 		 * to update it, and write out the file if successful.
2020Sstevel@tonic-gate 		 */
2034801Seschrock 		if (dc.dc_readonly) {
2044801Seschrock 			warn(gettext("failed to update settings: %s is "
2054801Seschrock 			    "read-only\n"), PATH_CONFIG);
2064801Seschrock 			return (E_ERROR);
2074801Seschrock 		}
2084801Seschrock 
209767Ssjelinek 		if (dconf_update(&dc, dflag) == -1 ||
210767Ssjelinek 		    dconf_write(&dc) == -1)
2110Sstevel@tonic-gate 			return (E_ERROR);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (dcmode == DC_CURRENT)
2150Sstevel@tonic-gate 		dconf_print(&dc, stdout);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (dconf_close(&dc) == -1)
2180Sstevel@tonic-gate 		warn(gettext("failed to close configuration file"));
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	return (E_SUCCESS);
2210Sstevel@tonic-gate }
222