xref: /titanic_50/usr/src/cmd/dumpadm/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
30*7c478bd9Sstevel@tonic-gate #include <locale.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "dconf.h"
36*7c478bd9Sstevel@tonic-gate #include "minfree.h"
37*7c478bd9Sstevel@tonic-gate #include "utils.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate static const char USAGE[] = "\
40*7c478bd9Sstevel@tonic-gate Usage: %s [-nuy] [-c kernel | curproc | all ] [-d dump-device | swap ]\n\
41*7c478bd9Sstevel@tonic-gate 	[-m min {k|m|%%} ] [-s savecore-dir] [-r root-dir]\n";
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static const char OPTS[] = "nuyc:d:m:s:r:";
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static const char PATH_DEVICE[] = "/dev/dump";
46*7c478bd9Sstevel@tonic-gate static const char PATH_CONFIG[] = "/etc/dumpadm.conf";
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int
49*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	const char *pname = getpname(argv[0]);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	u_longlong_t minf;
54*7c478bd9Sstevel@tonic-gate 	struct stat st;
55*7c478bd9Sstevel@tonic-gate 	int c;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	int dcmode = DC_CURRENT;	/* kernel settings override unless -u */
58*7c478bd9Sstevel@tonic-gate 	int modified = 0;		/* have we modified the dump config? */
59*7c478bd9Sstevel@tonic-gate 	char *minfstr = NULL;		/* string value of -m argument */
60*7c478bd9Sstevel@tonic-gate 	dumpconf_t dc;			/* current configuration */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
63*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	/*
66*7c478bd9Sstevel@tonic-gate 	 * Take an initial lap through argv hunting for -r root-dir,
67*7c478bd9Sstevel@tonic-gate 	 * so that we can chroot before opening the configuration file.
68*7c478bd9Sstevel@tonic-gate 	 * We also handle -u and any bad options at this point.
69*7c478bd9Sstevel@tonic-gate 	 */
70*7c478bd9Sstevel@tonic-gate 	while (optind < argc) {
71*7c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
72*7c478bd9Sstevel@tonic-gate 			if (c == 'r' && chroot(optarg) == -1)
73*7c478bd9Sstevel@tonic-gate 				die(gettext("failed to chroot to %s"), optarg);
74*7c478bd9Sstevel@tonic-gate 			else if (c == 'u')
75*7c478bd9Sstevel@tonic-gate 				dcmode = DC_OVERRIDE;
76*7c478bd9Sstevel@tonic-gate 			else if (c == '?') {
77*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(USAGE), pname);
78*7c478bd9Sstevel@tonic-gate 				return (E_USAGE);
79*7c478bd9Sstevel@tonic-gate 			}
80*7c478bd9Sstevel@tonic-gate 		}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		if (optind < argc) {
83*7c478bd9Sstevel@tonic-gate 			warn(gettext("illegal argument -- %s\n"), argv[optind]);
84*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(USAGE), pname);
85*7c478bd9Sstevel@tonic-gate 			return (E_USAGE);
86*7c478bd9Sstevel@tonic-gate 		}
87*7c478bd9Sstevel@tonic-gate 	}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (geteuid() != 0)
90*7c478bd9Sstevel@tonic-gate 		die(gettext("you must be root to use %s\n"), pname);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * If no config file exists yet, we're going to create an empty one,
94*7c478bd9Sstevel@tonic-gate 	 * so set the modified flag to force writing out the file.
95*7c478bd9Sstevel@tonic-gate 	 */
96*7c478bd9Sstevel@tonic-gate 	if (access(PATH_CONFIG, F_OK) == -1)
97*7c478bd9Sstevel@tonic-gate 		modified++;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/*
100*7c478bd9Sstevel@tonic-gate 	 * Now open and read in the initial values from the config file.
101*7c478bd9Sstevel@tonic-gate 	 * If it doesn't exist, we create an empty file and dc is
102*7c478bd9Sstevel@tonic-gate 	 * initialized with the default values.
103*7c478bd9Sstevel@tonic-gate 	 */
104*7c478bd9Sstevel@tonic-gate 	if (dconf_open(&dc, PATH_DEVICE, PATH_CONFIG, dcmode) == -1)
105*7c478bd9Sstevel@tonic-gate 		return (E_ERROR);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	/*
108*7c478bd9Sstevel@tonic-gate 	 * Take another lap through argv, processing options and
109*7c478bd9Sstevel@tonic-gate 	 * modifying the dumpconf_t as appropriate.
110*7c478bd9Sstevel@tonic-gate 	 */
111*7c478bd9Sstevel@tonic-gate 	for (optind = 1; optind < argc; optind++) {
112*7c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
113*7c478bd9Sstevel@tonic-gate 			switch (c) {
114*7c478bd9Sstevel@tonic-gate 			case 'c':
115*7c478bd9Sstevel@tonic-gate 				if (dconf_str2content(&dc, optarg) == -1)
116*7c478bd9Sstevel@tonic-gate 					return (E_USAGE);
117*7c478bd9Sstevel@tonic-gate 				modified++;
118*7c478bd9Sstevel@tonic-gate 				break;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 			case 'd':
121*7c478bd9Sstevel@tonic-gate 				if (dconf_str2device(&dc, optarg) == -1)
122*7c478bd9Sstevel@tonic-gate 					return (E_USAGE);
123*7c478bd9Sstevel@tonic-gate 				modified++;
124*7c478bd9Sstevel@tonic-gate 				break;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 			case 'm':
127*7c478bd9Sstevel@tonic-gate 				minfstr = optarg;
128*7c478bd9Sstevel@tonic-gate 				break;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 			case 'n':
131*7c478bd9Sstevel@tonic-gate 				dc.dc_enable = DC_OFF;
132*7c478bd9Sstevel@tonic-gate 				modified++;
133*7c478bd9Sstevel@tonic-gate 				break;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 			case 's':
136*7c478bd9Sstevel@tonic-gate 				if (stat(optarg, &st) == -1 ||
137*7c478bd9Sstevel@tonic-gate 				    !S_ISDIR(st.st_mode)) {
138*7c478bd9Sstevel@tonic-gate 					warn(gettext("%s is missing or not a "
139*7c478bd9Sstevel@tonic-gate 					    "directory\n"), optarg);
140*7c478bd9Sstevel@tonic-gate 					return (E_USAGE);
141*7c478bd9Sstevel@tonic-gate 				}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 				if (dconf_str2savdir(&dc, optarg) == -1)
144*7c478bd9Sstevel@tonic-gate 					return (E_USAGE);
145*7c478bd9Sstevel@tonic-gate 				modified++;
146*7c478bd9Sstevel@tonic-gate 				break;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 			case 'y':
149*7c478bd9Sstevel@tonic-gate 				dc.dc_enable = DC_ON;
150*7c478bd9Sstevel@tonic-gate 				modified++;
151*7c478bd9Sstevel@tonic-gate 				break;
152*7c478bd9Sstevel@tonic-gate 			}
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if (minfstr != NULL) {
157*7c478bd9Sstevel@tonic-gate 		if (minfree_compute(dc.dc_savdir, minfstr, &minf) == -1)
158*7c478bd9Sstevel@tonic-gate 			return (E_USAGE);
159*7c478bd9Sstevel@tonic-gate 		if (minfree_write(dc.dc_savdir, minf) == -1)
160*7c478bd9Sstevel@tonic-gate 			return (E_ERROR);
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (dcmode == DC_OVERRIDE) {
164*7c478bd9Sstevel@tonic-gate 		/*
165*7c478bd9Sstevel@tonic-gate 		 * In override mode, we try to force an update.  If this
166*7c478bd9Sstevel@tonic-gate 		 * fails, we re-load the kernel configuration and write that
167*7c478bd9Sstevel@tonic-gate 		 * out to the file in order to force the file in sync.
168*7c478bd9Sstevel@tonic-gate 		 */
169*7c478bd9Sstevel@tonic-gate 		if (dconf_update(&dc) == -1)
170*7c478bd9Sstevel@tonic-gate 			(void) dconf_getdev(&dc);
171*7c478bd9Sstevel@tonic-gate 		if (dconf_write(&dc) == -1)
172*7c478bd9Sstevel@tonic-gate 			return (E_ERROR);
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	} else if (modified) {
175*7c478bd9Sstevel@tonic-gate 		/*
176*7c478bd9Sstevel@tonic-gate 		 * If we're modifying the configuration, then try
177*7c478bd9Sstevel@tonic-gate 		 * to update it, and write out the file if successful.
178*7c478bd9Sstevel@tonic-gate 		 */
179*7c478bd9Sstevel@tonic-gate 		if (dconf_update(&dc) == -1 || dconf_write(&dc) == -1)
180*7c478bd9Sstevel@tonic-gate 			return (E_ERROR);
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	if (dcmode == DC_CURRENT)
184*7c478bd9Sstevel@tonic-gate 		dconf_print(&dc, stdout);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	if (dconf_close(&dc) == -1)
187*7c478bd9Sstevel@tonic-gate 		warn(gettext("failed to close configuration file"));
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	return (E_SUCCESS);
190*7c478bd9Sstevel@tonic-gate }
191