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