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
5a10acbd6Seschrock * Common Development and Distribution License (the "License").
6a10acbd6Seschrock * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22f6e214c7SGavin Maltby * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23*f41740d9SAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/stat.h>
277c478bd9Sstevel@tonic-gate #include <locale.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
31f6e214c7SGavin Maltby #include <string.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "dconf.h"
347c478bd9Sstevel@tonic-gate #include "minfree.h"
357c478bd9Sstevel@tonic-gate #include "utils.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate static const char USAGE[] = "\
38*f41740d9SAlexander Eremin Usage: %s [-enuy] [-c kernel | curproc | all ] [-d dump-device | swap ]\n\
39ca3e8d88SDave Plauger [-m min {k|m|%%} ] [-s savecore-dir] [-r root-dir] [-z on|off]\n";
407c478bd9Sstevel@tonic-gate
41*f41740d9SAlexander Eremin static const char OPTS[] = "einuyc:d:m:s:r:z:";
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static const char PATH_DEVICE[] = "/dev/dump";
447c478bd9Sstevel@tonic-gate static const char PATH_CONFIG[] = "/etc/dumpadm.conf";
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])477c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate const char *pname = getpname(argv[0]);
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate u_longlong_t minf;
527c478bd9Sstevel@tonic-gate struct stat st;
537c478bd9Sstevel@tonic-gate int c;
543e1bd7a2Ssjelinek int dflag = 0; /* for checking in use during -d ops */
55*f41740d9SAlexander Eremin int eflag = 0; /* print estimated dump size */
567c478bd9Sstevel@tonic-gate int dcmode = DC_CURRENT; /* kernel settings override unless -u */
577c478bd9Sstevel@tonic-gate int modified = 0; /* have we modified the dump config? */
587c478bd9Sstevel@tonic-gate char *minfstr = NULL; /* string value of -m argument */
597c478bd9Sstevel@tonic-gate dumpconf_t dc; /* current configuration */
60f6e214c7SGavin Maltby int chrooted = 0;
61f6e214c7SGavin Maltby int douuid = 0;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
647c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Take an initial lap through argv hunting for -r root-dir,
687c478bd9Sstevel@tonic-gate * so that we can chroot before opening the configuration file.
697c478bd9Sstevel@tonic-gate * We also handle -u and any bad options at this point.
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate while (optind < argc) {
727c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
73f6e214c7SGavin Maltby if (c == 'r' && ++chrooted && chroot(optarg) == -1)
747c478bd9Sstevel@tonic-gate die(gettext("failed to chroot to %s"), optarg);
757c478bd9Sstevel@tonic-gate else if (c == 'u')
767c478bd9Sstevel@tonic-gate dcmode = DC_OVERRIDE;
777c478bd9Sstevel@tonic-gate else if (c == '?') {
787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(USAGE), pname);
797c478bd9Sstevel@tonic-gate return (E_USAGE);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if (optind < argc) {
847c478bd9Sstevel@tonic-gate warn(gettext("illegal argument -- %s\n"), argv[optind]);
857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(USAGE), pname);
867c478bd9Sstevel@tonic-gate return (E_USAGE);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (geteuid() != 0)
917c478bd9Sstevel@tonic-gate die(gettext("you must be root to use %s\n"), pname);
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * If no config file exists yet, we're going to create an empty one,
957c478bd9Sstevel@tonic-gate * so set the modified flag to force writing out the file.
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate if (access(PATH_CONFIG, F_OK) == -1)
987c478bd9Sstevel@tonic-gate modified++;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Now open and read in the initial values from the config file.
1027c478bd9Sstevel@tonic-gate * If it doesn't exist, we create an empty file and dc is
1037c478bd9Sstevel@tonic-gate * initialized with the default values.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate if (dconf_open(&dc, PATH_DEVICE, PATH_CONFIG, dcmode) == -1)
1067c478bd9Sstevel@tonic-gate return (E_ERROR);
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate * Take another lap through argv, processing options and
1107c478bd9Sstevel@tonic-gate * modifying the dumpconf_t as appropriate.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate for (optind = 1; optind < argc; optind++) {
1137c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, OPTS)) != (int)EOF) {
1147c478bd9Sstevel@tonic-gate switch (c) {
1157c478bd9Sstevel@tonic-gate case 'c':
1167c478bd9Sstevel@tonic-gate if (dconf_str2content(&dc, optarg) == -1)
1177c478bd9Sstevel@tonic-gate return (E_USAGE);
1187c478bd9Sstevel@tonic-gate modified++;
1197c478bd9Sstevel@tonic-gate break;
1207c478bd9Sstevel@tonic-gate case 'd':
1217c478bd9Sstevel@tonic-gate if (dconf_str2device(&dc, optarg) == -1)
1227c478bd9Sstevel@tonic-gate return (E_USAGE);
1233e1bd7a2Ssjelinek dflag++;
1247c478bd9Sstevel@tonic-gate modified++;
1257c478bd9Sstevel@tonic-gate break;
126*f41740d9SAlexander Eremin case 'e':
127*f41740d9SAlexander Eremin eflag++;
128*f41740d9SAlexander Eremin break;
129f6e214c7SGavin Maltby case 'i':
130f6e214c7SGavin Maltby /* undocumented option */
131f6e214c7SGavin Maltby if (chrooted) {
132f6e214c7SGavin Maltby warn(gettext("-i and -r cannot be "
133f6e214c7SGavin Maltby "used together\n"));
134f6e214c7SGavin Maltby return (E_USAGE);
135f6e214c7SGavin Maltby }
136f6e214c7SGavin Maltby douuid++;
137f6e214c7SGavin Maltby break;
138f6e214c7SGavin Maltby
1397c478bd9Sstevel@tonic-gate case 'm':
1407c478bd9Sstevel@tonic-gate minfstr = optarg;
1417c478bd9Sstevel@tonic-gate break;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate case 'n':
1447c478bd9Sstevel@tonic-gate dc.dc_enable = DC_OFF;
1457c478bd9Sstevel@tonic-gate modified++;
1467c478bd9Sstevel@tonic-gate break;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate case 's':
1497c478bd9Sstevel@tonic-gate if (stat(optarg, &st) == -1 ||
1507c478bd9Sstevel@tonic-gate !S_ISDIR(st.st_mode)) {
1517c478bd9Sstevel@tonic-gate warn(gettext("%s is missing or not a "
1527c478bd9Sstevel@tonic-gate "directory\n"), optarg);
1537c478bd9Sstevel@tonic-gate return (E_USAGE);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (dconf_str2savdir(&dc, optarg) == -1)
1577c478bd9Sstevel@tonic-gate return (E_USAGE);
1587c478bd9Sstevel@tonic-gate modified++;
1597c478bd9Sstevel@tonic-gate break;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate case 'y':
1627c478bd9Sstevel@tonic-gate dc.dc_enable = DC_ON;
1637c478bd9Sstevel@tonic-gate modified++;
1647c478bd9Sstevel@tonic-gate break;
165ca3e8d88SDave Plauger
166ca3e8d88SDave Plauger case 'z':
167ca3e8d88SDave Plauger if (dconf_str2csave(&dc, optarg) == -1)
168ca3e8d88SDave Plauger return (E_USAGE);
169ca3e8d88SDave Plauger modified++;
170ca3e8d88SDave Plauger break;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
175*f41740d9SAlexander Eremin if (eflag) {
176*f41740d9SAlexander Eremin if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'e' &&
177*f41740d9SAlexander Eremin !argv[1][2])
178*f41740d9SAlexander Eremin return (dconf_get_dumpsize(&dc) ? E_SUCCESS : E_ERROR);
179*f41740d9SAlexander Eremin else
180*f41740d9SAlexander Eremin die(gettext("-e cannot be used with other options\n"));
181*f41740d9SAlexander Eremin }
182*f41740d9SAlexander Eremin
183f6e214c7SGavin Maltby if (douuid)
184f6e214c7SGavin Maltby return (dconf_write_uuid(&dc) ? E_SUCCESS : E_ERROR);
185f6e214c7SGavin Maltby
1867c478bd9Sstevel@tonic-gate if (minfstr != NULL) {
1877c478bd9Sstevel@tonic-gate if (minfree_compute(dc.dc_savdir, minfstr, &minf) == -1)
1887c478bd9Sstevel@tonic-gate return (E_USAGE);
1897c478bd9Sstevel@tonic-gate if (minfree_write(dc.dc_savdir, minf) == -1)
1907c478bd9Sstevel@tonic-gate return (E_ERROR);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate if (dcmode == DC_OVERRIDE) {
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate * In override mode, we try to force an update. If this
1967c478bd9Sstevel@tonic-gate * fails, we re-load the kernel configuration and write that
1977c478bd9Sstevel@tonic-gate * out to the file in order to force the file in sync.
198a10acbd6Seschrock *
199a10acbd6Seschrock * We allow the file to be read-only but print a warning to the
200a10acbd6Seschrock * user that indicates it hasn't been updated.
2017c478bd9Sstevel@tonic-gate */
2023e1bd7a2Ssjelinek if (dconf_update(&dc, 0) == -1)
2037c478bd9Sstevel@tonic-gate (void) dconf_getdev(&dc);
204a10acbd6Seschrock if (dc.dc_readonly)
205a10acbd6Seschrock warn(gettext("kernel settings updated, but "
206a10acbd6Seschrock "%s is read-only\n"), PATH_CONFIG);
207a10acbd6Seschrock else if (dconf_write(&dc) == -1)
2087c478bd9Sstevel@tonic-gate return (E_ERROR);
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate } else if (modified) {
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate * If we're modifying the configuration, then try
2137c478bd9Sstevel@tonic-gate * to update it, and write out the file if successful.
2147c478bd9Sstevel@tonic-gate */
215a10acbd6Seschrock if (dc.dc_readonly) {
216a10acbd6Seschrock warn(gettext("failed to update settings: %s is "
217a10acbd6Seschrock "read-only\n"), PATH_CONFIG);
218a10acbd6Seschrock return (E_ERROR);
219a10acbd6Seschrock }
220a10acbd6Seschrock
2213e1bd7a2Ssjelinek if (dconf_update(&dc, dflag) == -1 ||
2223e1bd7a2Ssjelinek dconf_write(&dc) == -1)
2237c478bd9Sstevel@tonic-gate return (E_ERROR);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if (dcmode == DC_CURRENT)
2277c478bd9Sstevel@tonic-gate dconf_print(&dc, stdout);
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate if (dconf_close(&dc) == -1)
2307c478bd9Sstevel@tonic-gate warn(gettext("failed to close configuration file"));
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate return (E_SUCCESS);
2337c478bd9Sstevel@tonic-gate }
234