xref: /titanic_50/usr/src/cmd/dumpadm/main.c (revision 3e1bd7a2aaeb6188caef90679b98088cfef1edc6)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*3e1bd7a2Ssjelinek  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*3e1bd7a2Ssjelinek  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/stat.h>
307c478bd9Sstevel@tonic-gate #include <locale.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "dconf.h"
367c478bd9Sstevel@tonic-gate #include "minfree.h"
377c478bd9Sstevel@tonic-gate #include "utils.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static const char USAGE[] = "\
407c478bd9Sstevel@tonic-gate Usage: %s [-nuy] [-c kernel | curproc | all ] [-d dump-device | swap ]\n\
417c478bd9Sstevel@tonic-gate 	[-m min {k|m|%%} ] [-s savecore-dir] [-r root-dir]\n";
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static const char OPTS[] = "nuyc:d:m:s:r:";
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static const char PATH_DEVICE[] = "/dev/dump";
467c478bd9Sstevel@tonic-gate static const char PATH_CONFIG[] = "/etc/dumpadm.conf";
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int
497c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	const char *pname = getpname(argv[0]);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	u_longlong_t minf;
547c478bd9Sstevel@tonic-gate 	struct stat st;
557c478bd9Sstevel@tonic-gate 	int c;
56*3e1bd7a2Ssjelinek 	int dflag = 0;			/* for checking in use during -d ops */
577c478bd9Sstevel@tonic-gate 	int dcmode = DC_CURRENT;	/* kernel settings override unless -u */
587c478bd9Sstevel@tonic-gate 	int modified = 0;		/* have we modified the dump config? */
597c478bd9Sstevel@tonic-gate 	char *minfstr = NULL;		/* string value of -m argument */
607c478bd9Sstevel@tonic-gate 	dumpconf_t dc;			/* current configuration */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
637c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	/*
667c478bd9Sstevel@tonic-gate 	 * Take an initial lap through argv hunting for -r root-dir,
677c478bd9Sstevel@tonic-gate 	 * so that we can chroot before opening the configuration file.
687c478bd9Sstevel@tonic-gate 	 * We also handle -u and any bad options at this point.
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 	while (optind < argc) {
717c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
727c478bd9Sstevel@tonic-gate 			if (c == 'r' && chroot(optarg) == -1)
737c478bd9Sstevel@tonic-gate 				die(gettext("failed to chroot to %s"), optarg);
747c478bd9Sstevel@tonic-gate 			else if (c == 'u')
757c478bd9Sstevel@tonic-gate 				dcmode = DC_OVERRIDE;
767c478bd9Sstevel@tonic-gate 			else if (c == '?') {
777c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(USAGE), pname);
787c478bd9Sstevel@tonic-gate 				return (E_USAGE);
797c478bd9Sstevel@tonic-gate 			}
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		if (optind < argc) {
837c478bd9Sstevel@tonic-gate 			warn(gettext("illegal argument -- %s\n"), argv[optind]);
847c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(USAGE), pname);
857c478bd9Sstevel@tonic-gate 			return (E_USAGE);
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (geteuid() != 0)
907c478bd9Sstevel@tonic-gate 		die(gettext("you must be root to use %s\n"), pname);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * If no config file exists yet, we're going to create an empty one,
947c478bd9Sstevel@tonic-gate 	 * so set the modified flag to force writing out the file.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	if (access(PATH_CONFIG, F_OK) == -1)
977c478bd9Sstevel@tonic-gate 		modified++;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * Now open and read in the initial values from the config file.
1017c478bd9Sstevel@tonic-gate 	 * If it doesn't exist, we create an empty file and dc is
1027c478bd9Sstevel@tonic-gate 	 * initialized with the default values.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 	if (dconf_open(&dc, PATH_DEVICE, PATH_CONFIG, dcmode) == -1)
1057c478bd9Sstevel@tonic-gate 		return (E_ERROR);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Take another lap through argv, processing options and
1097c478bd9Sstevel@tonic-gate 	 * modifying the dumpconf_t as appropriate.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
1127c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
1137c478bd9Sstevel@tonic-gate 			switch (c) {
1147c478bd9Sstevel@tonic-gate 			case 'c':
1157c478bd9Sstevel@tonic-gate 				if (dconf_str2content(&dc, optarg) == -1)
1167c478bd9Sstevel@tonic-gate 					return (E_USAGE);
1177c478bd9Sstevel@tonic-gate 				modified++;
1187c478bd9Sstevel@tonic-gate 				break;
1197c478bd9Sstevel@tonic-gate 			case 'd':
1207c478bd9Sstevel@tonic-gate 				if (dconf_str2device(&dc, optarg) == -1)
1217c478bd9Sstevel@tonic-gate 					return (E_USAGE);
122*3e1bd7a2Ssjelinek 				dflag++;
1237c478bd9Sstevel@tonic-gate 				modified++;
1247c478bd9Sstevel@tonic-gate 				break;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 			case 'm':
1277c478bd9Sstevel@tonic-gate 				minfstr = optarg;
1287c478bd9Sstevel@tonic-gate 				break;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 			case 'n':
1317c478bd9Sstevel@tonic-gate 				dc.dc_enable = DC_OFF;
1327c478bd9Sstevel@tonic-gate 				modified++;
1337c478bd9Sstevel@tonic-gate 				break;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 			case 's':
1367c478bd9Sstevel@tonic-gate 				if (stat(optarg, &st) == -1 ||
1377c478bd9Sstevel@tonic-gate 				    !S_ISDIR(st.st_mode)) {
1387c478bd9Sstevel@tonic-gate 					warn(gettext("%s is missing or not a "
1397c478bd9Sstevel@tonic-gate 					    "directory\n"), optarg);
1407c478bd9Sstevel@tonic-gate 					return (E_USAGE);
1417c478bd9Sstevel@tonic-gate 				}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 				if (dconf_str2savdir(&dc, optarg) == -1)
1447c478bd9Sstevel@tonic-gate 					return (E_USAGE);
1457c478bd9Sstevel@tonic-gate 				modified++;
1467c478bd9Sstevel@tonic-gate 				break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 			case 'y':
1497c478bd9Sstevel@tonic-gate 				dc.dc_enable = DC_ON;
1507c478bd9Sstevel@tonic-gate 				modified++;
1517c478bd9Sstevel@tonic-gate 				break;
1527c478bd9Sstevel@tonic-gate 			}
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (minfstr != NULL) {
1577c478bd9Sstevel@tonic-gate 		if (minfree_compute(dc.dc_savdir, minfstr, &minf) == -1)
1587c478bd9Sstevel@tonic-gate 			return (E_USAGE);
1597c478bd9Sstevel@tonic-gate 		if (minfree_write(dc.dc_savdir, minf) == -1)
1607c478bd9Sstevel@tonic-gate 			return (E_ERROR);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (dcmode == DC_OVERRIDE) {
1647c478bd9Sstevel@tonic-gate 		/*
1657c478bd9Sstevel@tonic-gate 		 * In override mode, we try to force an update.  If this
1667c478bd9Sstevel@tonic-gate 		 * fails, we re-load the kernel configuration and write that
1677c478bd9Sstevel@tonic-gate 		 * out to the file in order to force the file in sync.
1687c478bd9Sstevel@tonic-gate 		 */
169*3e1bd7a2Ssjelinek 		if (dconf_update(&dc, 0) == -1)
1707c478bd9Sstevel@tonic-gate 			(void) dconf_getdev(&dc);
1717c478bd9Sstevel@tonic-gate 		if (dconf_write(&dc) == -1)
1727c478bd9Sstevel@tonic-gate 			return (E_ERROR);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	} else if (modified) {
1757c478bd9Sstevel@tonic-gate 		/*
1767c478bd9Sstevel@tonic-gate 		 * If we're modifying the configuration, then try
1777c478bd9Sstevel@tonic-gate 		 * to update it, and write out the file if successful.
1787c478bd9Sstevel@tonic-gate 		 */
179*3e1bd7a2Ssjelinek 		if (dconf_update(&dc, dflag) == -1 ||
180*3e1bd7a2Ssjelinek 		    dconf_write(&dc) == -1)
1817c478bd9Sstevel@tonic-gate 			return (E_ERROR);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (dcmode == DC_CURRENT)
1857c478bd9Sstevel@tonic-gate 		dconf_print(&dc, stdout);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (dconf_close(&dc) == -1)
1887c478bd9Sstevel@tonic-gate 		warn(gettext("failed to close configuration file"));
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (E_SUCCESS);
1917c478bd9Sstevel@tonic-gate }
192