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