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