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 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 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/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/swap.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/dumpadm.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #include <unistd.h> 36*7c478bd9Sstevel@tonic-gate #include <string.h> 37*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 38*7c478bd9Sstevel@tonic-gate #include <stdio.h> 39*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 40*7c478bd9Sstevel@tonic-gate #include <errno.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include "dconf.h" 43*7c478bd9Sstevel@tonic-gate #include "minfree.h" 44*7c478bd9Sstevel@tonic-gate #include "utils.h" 45*7c478bd9Sstevel@tonic-gate #include "swap.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate typedef struct dc_token { 48*7c478bd9Sstevel@tonic-gate const char *tok_name; 49*7c478bd9Sstevel@tonic-gate int (*tok_parse)(dumpconf_t *, char *); 50*7c478bd9Sstevel@tonic-gate int (*tok_print)(const dumpconf_t *, FILE *); 51*7c478bd9Sstevel@tonic-gate } dc_token_t; 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate static int print_device(const dumpconf_t *, FILE *); 54*7c478bd9Sstevel@tonic-gate static int print_savdir(const dumpconf_t *, FILE *); 55*7c478bd9Sstevel@tonic-gate static int print_content(const dumpconf_t *, FILE *); 56*7c478bd9Sstevel@tonic-gate static int print_enable(const dumpconf_t *, FILE *); 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate static const dc_token_t tokens[] = { 59*7c478bd9Sstevel@tonic-gate { "DUMPADM_DEVICE", dconf_str2device, print_device }, 60*7c478bd9Sstevel@tonic-gate { "DUMPADM_SAVDIR", dconf_str2savdir, print_savdir }, 61*7c478bd9Sstevel@tonic-gate { "DUMPADM_CONTENT", dconf_str2content, print_content }, 62*7c478bd9Sstevel@tonic-gate { "DUMPADM_ENABLE", dconf_str2enable, print_enable }, 63*7c478bd9Sstevel@tonic-gate { NULL, NULL, NULL } 64*7c478bd9Sstevel@tonic-gate }; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate static const char DC_STR_YES[] = "yes"; /* Enable on string */ 67*7c478bd9Sstevel@tonic-gate static const char DC_STR_NO[] = "no"; /* Enable off string */ 68*7c478bd9Sstevel@tonic-gate static const char DC_STR_SWAP[] = "swap"; /* Default dump device */ 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate /* The pages included in the dump */ 71*7c478bd9Sstevel@tonic-gate static const char DC_STR_KERNEL[] = "kernel"; /* Kernel only */ 72*7c478bd9Sstevel@tonic-gate static const char DC_STR_CURPROC[] = "curproc"; /* Kernel + current process */ 73*7c478bd9Sstevel@tonic-gate static const char DC_STR_ALL[] = "all"; /* All pages */ 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* 76*7c478bd9Sstevel@tonic-gate * Permissions and ownership for the configuration file: 77*7c478bd9Sstevel@tonic-gate */ 78*7c478bd9Sstevel@tonic-gate #define DC_OWNER 0 /* Uid 0 (root) */ 79*7c478bd9Sstevel@tonic-gate #define DC_GROUP 1 /* Gid 1 (other) */ 80*7c478bd9Sstevel@tonic-gate #define DC_PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* Mode 0644 */ 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate static void 83*7c478bd9Sstevel@tonic-gate dconf_init(dumpconf_t *dcp, int dcmode) 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate struct utsname ut; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate /* 88*7c478bd9Sstevel@tonic-gate * Default device for dumps is 'swap' (appropriate swap device), 89*7c478bd9Sstevel@tonic-gate * and default savecore directory is /var/crash/`uname -n`, 90*7c478bd9Sstevel@tonic-gate * which is compatible with pre-dumpadm behavior. 91*7c478bd9Sstevel@tonic-gate */ 92*7c478bd9Sstevel@tonic-gate (void) strcpy(dcp->dc_device, DC_STR_SWAP); 93*7c478bd9Sstevel@tonic-gate (void) strcpy(dcp->dc_savdir, "/var/crash"); 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if (uname(&ut) != -1) { 96*7c478bd9Sstevel@tonic-gate (void) strcat(dcp->dc_savdir, "/"); 97*7c478bd9Sstevel@tonic-gate (void) strcat(dcp->dc_savdir, ut.nodename); 98*7c478bd9Sstevel@tonic-gate } 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate /* 101*7c478bd9Sstevel@tonic-gate * Default is contents kernel, and savecore enabled on reboot. 102*7c478bd9Sstevel@tonic-gate */ 103*7c478bd9Sstevel@tonic-gate dcp->dc_cflags = DUMP_KERNEL; 104*7c478bd9Sstevel@tonic-gate dcp->dc_enable = DC_ON; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate dcp->dc_mode = dcmode; 107*7c478bd9Sstevel@tonic-gate dcp->dc_conf_fp = NULL; 108*7c478bd9Sstevel@tonic-gate dcp->dc_conf_fd = -1; 109*7c478bd9Sstevel@tonic-gate dcp->dc_dump_fd = -1; 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate int 113*7c478bd9Sstevel@tonic-gate dconf_open(dumpconf_t *dcp, const char *dpath, const char *fpath, int dcmode) 114*7c478bd9Sstevel@tonic-gate { 115*7c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 116*7c478bd9Sstevel@tonic-gate int line; 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate dconf_init(dcp, dcmode); 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate if ((dcp->dc_dump_fd = open(dpath, O_RDWR)) == -1) { 121*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open %s"), dpath); 122*7c478bd9Sstevel@tonic-gate return (-1); 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if ((dcp->dc_conf_fd = open(fpath, O_RDWR | O_CREAT, DC_PERM)) == -1) { 126*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open %s"), fpath); 127*7c478bd9Sstevel@tonic-gate return (-1); 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate if ((dcp->dc_conf_fp = fdopen(dcp->dc_conf_fd, "r+")) == NULL) { 131*7c478bd9Sstevel@tonic-gate warn(gettext("failed to open stream for %s"), fpath); 132*7c478bd9Sstevel@tonic-gate return (-1); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* 136*7c478bd9Sstevel@tonic-gate * If we're in override mode, the current kernel settings override the 137*7c478bd9Sstevel@tonic-gate * default settings and anything invalid in the configuration file. 138*7c478bd9Sstevel@tonic-gate */ 139*7c478bd9Sstevel@tonic-gate if (dcmode == DC_OVERRIDE) 140*7c478bd9Sstevel@tonic-gate (void) dconf_getdev(dcp); 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate for (line = 1; fgets(buf, BUFSIZ, dcp->dc_conf_fp) != NULL; line++) { 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate char name[BUFSIZ], value[BUFSIZ]; 145*7c478bd9Sstevel@tonic-gate const dc_token_t *tokp; 146*7c478bd9Sstevel@tonic-gate int len; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate if (buf[0] == '#' || buf[0] == '\n') 149*7c478bd9Sstevel@tonic-gate continue; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate /* 152*7c478bd9Sstevel@tonic-gate * Look for "name=value", with optional whitespace on either 153*7c478bd9Sstevel@tonic-gate * side, terminated by a newline, and consuming the whole line. 154*7c478bd9Sstevel@tonic-gate */ 155*7c478bd9Sstevel@tonic-gate /* LINTED - unbounded string specifier */ 156*7c478bd9Sstevel@tonic-gate if (sscanf(buf, " %[^=]=%s \n%n", name, value, &len) == 2 && 157*7c478bd9Sstevel@tonic-gate name[0] != '\0' && value[0] != '\0' && len == strlen(buf)) { 158*7c478bd9Sstevel@tonic-gate /* 159*7c478bd9Sstevel@tonic-gate * Locate a matching token in the tokens[] table, 160*7c478bd9Sstevel@tonic-gate * and invoke its parsing function. 161*7c478bd9Sstevel@tonic-gate */ 162*7c478bd9Sstevel@tonic-gate for (tokp = tokens; tokp->tok_name != NULL; tokp++) { 163*7c478bd9Sstevel@tonic-gate if (strcmp(name, tokp->tok_name) == 0) { 164*7c478bd9Sstevel@tonic-gate if (tokp->tok_parse(dcp, value) == -1) { 165*7c478bd9Sstevel@tonic-gate warn(gettext("\"%s\", line %d: " 166*7c478bd9Sstevel@tonic-gate "warning: invalid %s\n"), 167*7c478bd9Sstevel@tonic-gate fpath, line, name); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate break; 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate /* 174*7c478bd9Sstevel@tonic-gate * If we hit the end of the tokens[] table, 175*7c478bd9Sstevel@tonic-gate * no matching token was found. 176*7c478bd9Sstevel@tonic-gate */ 177*7c478bd9Sstevel@tonic-gate if (tokp->tok_name == NULL) { 178*7c478bd9Sstevel@tonic-gate warn(gettext("\"%s\", line %d: warning: " 179*7c478bd9Sstevel@tonic-gate "invalid token: %s\n"), fpath, line, name); 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate } else { 183*7c478bd9Sstevel@tonic-gate warn(gettext("\"%s\", line %d: syntax error\n"), 184*7c478bd9Sstevel@tonic-gate fpath, line); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate /* 189*7c478bd9Sstevel@tonic-gate * If we're not in override mode, the current kernel settings 190*7c478bd9Sstevel@tonic-gate * override the settings read from the configuration file. 191*7c478bd9Sstevel@tonic-gate */ 192*7c478bd9Sstevel@tonic-gate if (dcmode == DC_CURRENT) 193*7c478bd9Sstevel@tonic-gate return (dconf_getdev(dcp)); 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate return (0); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate int 199*7c478bd9Sstevel@tonic-gate dconf_getdev(dumpconf_t *dcp) 200*7c478bd9Sstevel@tonic-gate { 201*7c478bd9Sstevel@tonic-gate int status = 0; 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) { 204*7c478bd9Sstevel@tonic-gate warn(gettext("failed to get kernel dump settings")); 205*7c478bd9Sstevel@tonic-gate status = -1; 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate if (ioctl(dcp->dc_dump_fd, DIOCGETDEV, dcp->dc_device) == -1) { 209*7c478bd9Sstevel@tonic-gate if (errno != ENODEV) { 210*7c478bd9Sstevel@tonic-gate warn(gettext("failed to get dump device")); 211*7c478bd9Sstevel@tonic-gate status = -1; 212*7c478bd9Sstevel@tonic-gate } else 213*7c478bd9Sstevel@tonic-gate dcp->dc_device[0] = '\0'; 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate return (status); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate int 220*7c478bd9Sstevel@tonic-gate dconf_close(dumpconf_t *dcp) 221*7c478bd9Sstevel@tonic-gate { 222*7c478bd9Sstevel@tonic-gate if (fclose(dcp->dc_conf_fp) == 0) { 223*7c478bd9Sstevel@tonic-gate (void) close(dcp->dc_dump_fd); 224*7c478bd9Sstevel@tonic-gate return (0); 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate return (-1); 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate int 230*7c478bd9Sstevel@tonic-gate dconf_write(dumpconf_t *dcp) 231*7c478bd9Sstevel@tonic-gate { 232*7c478bd9Sstevel@tonic-gate const dc_token_t *tokp; 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate if (fseeko(dcp->dc_conf_fp, (off_t)0, SEEK_SET) == -1) { 235*7c478bd9Sstevel@tonic-gate warn(gettext("failed to seek config file")); 236*7c478bd9Sstevel@tonic-gate return (-1); 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate if (ftruncate(dcp->dc_conf_fd, (off_t)0) == -1) { 240*7c478bd9Sstevel@tonic-gate warn(gettext("failed to truncate config file")); 241*7c478bd9Sstevel@tonic-gate return (-1); 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate (void) fputs("#\n# dumpadm.conf\n#\n" 245*7c478bd9Sstevel@tonic-gate "# Configuration parameters for system crash dump.\n" 246*7c478bd9Sstevel@tonic-gate "# Do NOT edit this file by hand -- use dumpadm(1m) instead.\n" 247*7c478bd9Sstevel@tonic-gate "#\n", dcp->dc_conf_fp); 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate for (tokp = tokens; tokp->tok_name != NULL; tokp++) { 250*7c478bd9Sstevel@tonic-gate if (fprintf(dcp->dc_conf_fp, "%s=", tokp->tok_name) == -1 || 251*7c478bd9Sstevel@tonic-gate tokp->tok_print(dcp, dcp->dc_conf_fp) == -1) { 252*7c478bd9Sstevel@tonic-gate warn(gettext("failed to write token")); 253*7c478bd9Sstevel@tonic-gate return (-1); 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate if (fflush(dcp->dc_conf_fp) != 0) 258*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to flush config file")); 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate if (fsync(dcp->dc_conf_fd) == -1) 261*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to sync config file to disk")); 262*7c478bd9Sstevel@tonic-gate 263*7c478bd9Sstevel@tonic-gate if (fchmod(dcp->dc_conf_fd, DC_PERM) == -1) 264*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to reset mode on config file")); 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate if (fchown(dcp->dc_conf_fd, DC_OWNER, DC_GROUP) == -1) 267*7c478bd9Sstevel@tonic-gate warn(gettext("warning: failed to reset owner on config file")); 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate return (0); 270*7c478bd9Sstevel@tonic-gate } 271*7c478bd9Sstevel@tonic-gate 272*7c478bd9Sstevel@tonic-gate static int 273*7c478bd9Sstevel@tonic-gate open_stat64(const char *path, struct stat64 *stp) 274*7c478bd9Sstevel@tonic-gate { 275*7c478bd9Sstevel@tonic-gate int fd = open64(path, O_RDONLY); 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate if (fd >= 0) { 278*7c478bd9Sstevel@tonic-gate int status = fstat64(fd, stp); 279*7c478bd9Sstevel@tonic-gate (void) close(fd); 280*7c478bd9Sstevel@tonic-gate return (status); 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate return (-1); 284*7c478bd9Sstevel@tonic-gate } 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate static int 287*7c478bd9Sstevel@tonic-gate dconf_swap_compare(const swapent_t *s1, const swapent_t *s2) 288*7c478bd9Sstevel@tonic-gate { 289*7c478bd9Sstevel@tonic-gate struct stat64 st1, st2; 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate int prefer_s1 = -1; /* Return value to move s1 left (s1 < s2) */ 292*7c478bd9Sstevel@tonic-gate int prefer_s2 = 1; /* Return value to move s2 left (s1 > s2) */ 293*7c478bd9Sstevel@tonic-gate 294*7c478bd9Sstevel@tonic-gate /* 295*7c478bd9Sstevel@tonic-gate * First try: open and fstat each swap entry. If either system 296*7c478bd9Sstevel@tonic-gate * call fails, arbitrarily prefer the other entry. 297*7c478bd9Sstevel@tonic-gate */ 298*7c478bd9Sstevel@tonic-gate if (open_stat64(s1->ste_path, &st1) == -1) 299*7c478bd9Sstevel@tonic-gate return (prefer_s2); 300*7c478bd9Sstevel@tonic-gate 301*7c478bd9Sstevel@tonic-gate if (open_stat64(s2->ste_path, &st2) == -1) 302*7c478bd9Sstevel@tonic-gate return (prefer_s1); 303*7c478bd9Sstevel@tonic-gate 304*7c478bd9Sstevel@tonic-gate /* 305*7c478bd9Sstevel@tonic-gate * Second try: if both entries are block devices, or if 306*7c478bd9Sstevel@tonic-gate * neither is a block device, prefer the larger. 307*7c478bd9Sstevel@tonic-gate */ 308*7c478bd9Sstevel@tonic-gate if (S_ISBLK(st1.st_mode) == S_ISBLK(st2.st_mode)) { 309*7c478bd9Sstevel@tonic-gate if (st2.st_size > st1.st_size) 310*7c478bd9Sstevel@tonic-gate return (prefer_s2); 311*7c478bd9Sstevel@tonic-gate return (prefer_s1); 312*7c478bd9Sstevel@tonic-gate } 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate /* 315*7c478bd9Sstevel@tonic-gate * Third try: prefer the entry that is a block device. 316*7c478bd9Sstevel@tonic-gate */ 317*7c478bd9Sstevel@tonic-gate if (S_ISBLK(st2.st_mode)) 318*7c478bd9Sstevel@tonic-gate return (prefer_s2); 319*7c478bd9Sstevel@tonic-gate return (prefer_s1); 320*7c478bd9Sstevel@tonic-gate } 321*7c478bd9Sstevel@tonic-gate 322*7c478bd9Sstevel@tonic-gate static int 323*7c478bd9Sstevel@tonic-gate dconf_dev_ioctl(dumpconf_t *dcp, int cmd) 324*7c478bd9Sstevel@tonic-gate { 325*7c478bd9Sstevel@tonic-gate if (ioctl(dcp->dc_dump_fd, cmd, dcp->dc_device) == 0) 326*7c478bd9Sstevel@tonic-gate return (0); 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate switch (errno) { 329*7c478bd9Sstevel@tonic-gate case ENOTSUP: 330*7c478bd9Sstevel@tonic-gate warn(gettext("dumps not supported on %s\n"), dcp->dc_device); 331*7c478bd9Sstevel@tonic-gate break; 332*7c478bd9Sstevel@tonic-gate case EBUSY: 333*7c478bd9Sstevel@tonic-gate warn(gettext("device %s is already in use\n"), dcp->dc_device); 334*7c478bd9Sstevel@tonic-gate break; 335*7c478bd9Sstevel@tonic-gate default: 336*7c478bd9Sstevel@tonic-gate /* 337*7c478bd9Sstevel@tonic-gate * NOTE: The stmsboot(1M) command's boot-up script parses this 338*7c478bd9Sstevel@tonic-gate * error to get the dump device name. If you change the format 339*7c478bd9Sstevel@tonic-gate * of this message, make sure that stmsboot(1M) is in sync. 340*7c478bd9Sstevel@tonic-gate */ 341*7c478bd9Sstevel@tonic-gate warn(gettext("cannot use %s as dump device"), dcp->dc_device); 342*7c478bd9Sstevel@tonic-gate } 343*7c478bd9Sstevel@tonic-gate return (-1); 344*7c478bd9Sstevel@tonic-gate } 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate int 347*7c478bd9Sstevel@tonic-gate dconf_update(dumpconf_t *dcp) 348*7c478bd9Sstevel@tonic-gate { 349*7c478bd9Sstevel@tonic-gate int oconf; 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate /* 352*7c478bd9Sstevel@tonic-gate * Save the existing dump configuration in case something goes wrong. 353*7c478bd9Sstevel@tonic-gate */ 354*7c478bd9Sstevel@tonic-gate if ((oconf = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) { 355*7c478bd9Sstevel@tonic-gate warn(gettext("failed to get kernel dump configuration")); 356*7c478bd9Sstevel@tonic-gate return (-1); 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate 359*7c478bd9Sstevel@tonic-gate oconf &= DUMP_CONTENT; 360*7c478bd9Sstevel@tonic-gate dcp->dc_cflags &= DUMP_CONTENT; 361*7c478bd9Sstevel@tonic-gate 362*7c478bd9Sstevel@tonic-gate if (ioctl(dcp->dc_dump_fd, DIOCSETCONF, dcp->dc_cflags) == -1) { 363*7c478bd9Sstevel@tonic-gate warn(gettext("failed to update kernel dump configuration")); 364*7c478bd9Sstevel@tonic-gate return (-1); 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate if (strcmp(dcp->dc_device, DC_STR_SWAP) == 0) { 368*7c478bd9Sstevel@tonic-gate swaptbl_t *swt; 369*7c478bd9Sstevel@tonic-gate int i; 370*7c478bd9Sstevel@tonic-gate 371*7c478bd9Sstevel@tonic-gate if ((swt = swap_list()) == NULL) 372*7c478bd9Sstevel@tonic-gate goto err; 373*7c478bd9Sstevel@tonic-gate 374*7c478bd9Sstevel@tonic-gate if (swt->swt_n == 0) { 375*7c478bd9Sstevel@tonic-gate warn(gettext("no swap devices are available\n")); 376*7c478bd9Sstevel@tonic-gate free(swt); 377*7c478bd9Sstevel@tonic-gate goto err; 378*7c478bd9Sstevel@tonic-gate } 379*7c478bd9Sstevel@tonic-gate 380*7c478bd9Sstevel@tonic-gate qsort(&swt->swt_ent[0], swt->swt_n, sizeof (swapent_t), 381*7c478bd9Sstevel@tonic-gate (int (*)(const void *, const void *))dconf_swap_compare); 382*7c478bd9Sstevel@tonic-gate 383*7c478bd9Sstevel@tonic-gate /* 384*7c478bd9Sstevel@tonic-gate * Iterate through the prioritized list of swap entries, 385*7c478bd9Sstevel@tonic-gate * trying to configure one as the dump device. 386*7c478bd9Sstevel@tonic-gate */ 387*7c478bd9Sstevel@tonic-gate for (i = 0; i < swt->swt_n; i++) { 388*7c478bd9Sstevel@tonic-gate if (ioctl(dcp->dc_dump_fd, DIOCSETDEV, 389*7c478bd9Sstevel@tonic-gate swt->swt_ent[i].ste_path) == 0) { 390*7c478bd9Sstevel@tonic-gate (void) strcpy(dcp->dc_device, 391*7c478bd9Sstevel@tonic-gate swt->swt_ent[i].ste_path); 392*7c478bd9Sstevel@tonic-gate break; 393*7c478bd9Sstevel@tonic-gate } 394*7c478bd9Sstevel@tonic-gate } 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate if (i == swt->swt_n) { 397*7c478bd9Sstevel@tonic-gate warn(gettext("no swap devices could be configured " 398*7c478bd9Sstevel@tonic-gate "as the dump device\n")); 399*7c478bd9Sstevel@tonic-gate free(swt); 400*7c478bd9Sstevel@tonic-gate goto err; 401*7c478bd9Sstevel@tonic-gate } 402*7c478bd9Sstevel@tonic-gate free(swt); 403*7c478bd9Sstevel@tonic-gate 404*7c478bd9Sstevel@tonic-gate } else if (dcp->dc_device[0] != '\0') { 405*7c478bd9Sstevel@tonic-gate /* 406*7c478bd9Sstevel@tonic-gate * If we're not in forcible update mode, then fail the change 407*7c478bd9Sstevel@tonic-gate * if the selected device cannot be used as the dump device, 408*7c478bd9Sstevel@tonic-gate * or if it is not big enough to hold the dump. 409*7c478bd9Sstevel@tonic-gate */ 410*7c478bd9Sstevel@tonic-gate if (dcp->dc_mode == DC_CURRENT) { 411*7c478bd9Sstevel@tonic-gate struct stat64 st; 412*7c478bd9Sstevel@tonic-gate uint64_t d; 413*7c478bd9Sstevel@tonic-gate 414*7c478bd9Sstevel@tonic-gate if (dconf_dev_ioctl(dcp, DIOCTRYDEV) == -1) 415*7c478bd9Sstevel@tonic-gate goto err; 416*7c478bd9Sstevel@tonic-gate 417*7c478bd9Sstevel@tonic-gate if (open_stat64(dcp->dc_device, &st) == -1) { 418*7c478bd9Sstevel@tonic-gate warn(gettext("failed to access %s"), 419*7c478bd9Sstevel@tonic-gate dcp->dc_device); 420*7c478bd9Sstevel@tonic-gate goto err; 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate 423*7c478bd9Sstevel@tonic-gate if (ioctl(dcp->dc_dump_fd, DIOCGETDUMPSIZE, &d) == -1) { 424*7c478bd9Sstevel@tonic-gate warn(gettext("failed to get kernel dump size")); 425*7c478bd9Sstevel@tonic-gate goto err; 426*7c478bd9Sstevel@tonic-gate } 427*7c478bd9Sstevel@tonic-gate 428*7c478bd9Sstevel@tonic-gate if (st.st_size < d) { 429*7c478bd9Sstevel@tonic-gate warn(gettext("dump device %s is too small to " 430*7c478bd9Sstevel@tonic-gate "hold a system dump\ndump size %llu " 431*7c478bd9Sstevel@tonic-gate "bytes, device size %lld bytes\n"), 432*7c478bd9Sstevel@tonic-gate dcp->dc_device, d, st.st_size); 433*7c478bd9Sstevel@tonic-gate goto err; 434*7c478bd9Sstevel@tonic-gate } 435*7c478bd9Sstevel@tonic-gate } 436*7c478bd9Sstevel@tonic-gate 437*7c478bd9Sstevel@tonic-gate if (dconf_dev_ioctl(dcp, DIOCSETDEV) == -1) 438*7c478bd9Sstevel@tonic-gate goto err; 439*7c478bd9Sstevel@tonic-gate } 440*7c478bd9Sstevel@tonic-gate 441*7c478bd9Sstevel@tonic-gate /* 442*7c478bd9Sstevel@tonic-gate * Now that we've updated the dump device, we need to issue another 443*7c478bd9Sstevel@tonic-gate * ioctl to re-read the config flags to determine whether we 444*7c478bd9Sstevel@tonic-gate * obtained DUMP_EXCL access on our dump device. 445*7c478bd9Sstevel@tonic-gate */ 446*7c478bd9Sstevel@tonic-gate if ((dcp->dc_cflags = ioctl(dcp->dc_dump_fd, DIOCGETCONF, 0)) == -1) { 447*7c478bd9Sstevel@tonic-gate warn(gettext("failed to re-read kernel dump configuration")); 448*7c478bd9Sstevel@tonic-gate return (-1); 449*7c478bd9Sstevel@tonic-gate } 450*7c478bd9Sstevel@tonic-gate 451*7c478bd9Sstevel@tonic-gate return (0); 452*7c478bd9Sstevel@tonic-gate 453*7c478bd9Sstevel@tonic-gate err: 454*7c478bd9Sstevel@tonic-gate (void) ioctl(dcp->dc_dump_fd, DIOCSETCONF, oconf); 455*7c478bd9Sstevel@tonic-gate return (-1); 456*7c478bd9Sstevel@tonic-gate } 457*7c478bd9Sstevel@tonic-gate 458*7c478bd9Sstevel@tonic-gate void 459*7c478bd9Sstevel@tonic-gate dconf_print(dumpconf_t *dcp, FILE *fp) 460*7c478bd9Sstevel@tonic-gate { 461*7c478bd9Sstevel@tonic-gate u_longlong_t min; 462*7c478bd9Sstevel@tonic-gate char *content; 463*7c478bd9Sstevel@tonic-gate 464*7c478bd9Sstevel@tonic-gate if (dcp->dc_cflags & DUMP_ALL) 465*7c478bd9Sstevel@tonic-gate content = gettext("all"); 466*7c478bd9Sstevel@tonic-gate else if (dcp->dc_cflags & DUMP_CURPROC) 467*7c478bd9Sstevel@tonic-gate content = gettext("kernel and current process"); 468*7c478bd9Sstevel@tonic-gate else 469*7c478bd9Sstevel@tonic-gate content = gettext("kernel"); 470*7c478bd9Sstevel@tonic-gate 471*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext(" Dump content: %s pages\n"), content); 472*7c478bd9Sstevel@tonic-gate 473*7c478bd9Sstevel@tonic-gate if (dcp->dc_device[0] != '\0') { 474*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext(" Dump device: %s (%s)\n"), 475*7c478bd9Sstevel@tonic-gate dcp->dc_device, (dcp->dc_cflags & DUMP_EXCL) ? 476*7c478bd9Sstevel@tonic-gate gettext("dedicated") : gettext("swap")); 477*7c478bd9Sstevel@tonic-gate } else { 478*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext(" Dump device: none " 479*7c478bd9Sstevel@tonic-gate "(dumps disabled)\n")); 480*7c478bd9Sstevel@tonic-gate } 481*7c478bd9Sstevel@tonic-gate 482*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext("Savecore directory: %s"), dcp->dc_savdir); 483*7c478bd9Sstevel@tonic-gate 484*7c478bd9Sstevel@tonic-gate if (minfree_read(dcp->dc_savdir, &min) == 0) { 485*7c478bd9Sstevel@tonic-gate if (min < 1024 || (min % 1024) != 0) 486*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext(" (minfree = %lluKB)"), min); 487*7c478bd9Sstevel@tonic-gate else 488*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext(" (minfree = %lluMB)"), 489*7c478bd9Sstevel@tonic-gate min / 1024); 490*7c478bd9Sstevel@tonic-gate } 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext("\n")); 493*7c478bd9Sstevel@tonic-gate 494*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, gettext(" Savecore enabled: %s\n"), 495*7c478bd9Sstevel@tonic-gate (dcp->dc_enable == DC_OFF) ? gettext("no") : gettext("yes")); 496*7c478bd9Sstevel@tonic-gate } 497*7c478bd9Sstevel@tonic-gate 498*7c478bd9Sstevel@tonic-gate int 499*7c478bd9Sstevel@tonic-gate dconf_str2device(dumpconf_t *dcp, char *buf) 500*7c478bd9Sstevel@tonic-gate { 501*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, DC_STR_SWAP) == 0) { 502*7c478bd9Sstevel@tonic-gate (void) strcpy(dcp->dc_device, DC_STR_SWAP); 503*7c478bd9Sstevel@tonic-gate return (0); 504*7c478bd9Sstevel@tonic-gate } 505*7c478bd9Sstevel@tonic-gate 506*7c478bd9Sstevel@tonic-gate if (valid_abspath(buf)) { 507*7c478bd9Sstevel@tonic-gate (void) strcpy(dcp->dc_device, buf); 508*7c478bd9Sstevel@tonic-gate return (0); 509*7c478bd9Sstevel@tonic-gate } 510*7c478bd9Sstevel@tonic-gate 511*7c478bd9Sstevel@tonic-gate return (-1); 512*7c478bd9Sstevel@tonic-gate } 513*7c478bd9Sstevel@tonic-gate 514*7c478bd9Sstevel@tonic-gate int 515*7c478bd9Sstevel@tonic-gate dconf_str2savdir(dumpconf_t *dcp, char *buf) 516*7c478bd9Sstevel@tonic-gate { 517*7c478bd9Sstevel@tonic-gate if (valid_abspath(buf)) { 518*7c478bd9Sstevel@tonic-gate (void) strcpy(dcp->dc_savdir, buf); 519*7c478bd9Sstevel@tonic-gate return (0); 520*7c478bd9Sstevel@tonic-gate } 521*7c478bd9Sstevel@tonic-gate 522*7c478bd9Sstevel@tonic-gate return (-1); 523*7c478bd9Sstevel@tonic-gate } 524*7c478bd9Sstevel@tonic-gate 525*7c478bd9Sstevel@tonic-gate int 526*7c478bd9Sstevel@tonic-gate dconf_str2content(dumpconf_t *dcp, char *buf) 527*7c478bd9Sstevel@tonic-gate { 528*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, DC_STR_KERNEL) == 0) { 529*7c478bd9Sstevel@tonic-gate dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_KERNEL; 530*7c478bd9Sstevel@tonic-gate return (0); 531*7c478bd9Sstevel@tonic-gate } 532*7c478bd9Sstevel@tonic-gate 533*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, DC_STR_CURPROC) == 0) { 534*7c478bd9Sstevel@tonic-gate dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | 535*7c478bd9Sstevel@tonic-gate DUMP_CURPROC; 536*7c478bd9Sstevel@tonic-gate return (0); 537*7c478bd9Sstevel@tonic-gate } 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, DC_STR_ALL) == 0) { 540*7c478bd9Sstevel@tonic-gate dcp->dc_cflags = (dcp->dc_cflags & ~DUMP_CONTENT) | DUMP_ALL; 541*7c478bd9Sstevel@tonic-gate return (0); 542*7c478bd9Sstevel@tonic-gate } 543*7c478bd9Sstevel@tonic-gate 544*7c478bd9Sstevel@tonic-gate warn(gettext("invalid dump content type -- %s\n"), buf); 545*7c478bd9Sstevel@tonic-gate return (-1); 546*7c478bd9Sstevel@tonic-gate } 547*7c478bd9Sstevel@tonic-gate 548*7c478bd9Sstevel@tonic-gate int 549*7c478bd9Sstevel@tonic-gate dconf_str2enable(dumpconf_t *dcp, char *buf) 550*7c478bd9Sstevel@tonic-gate { 551*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, DC_STR_YES) == 0) { 552*7c478bd9Sstevel@tonic-gate dcp->dc_enable = DC_ON; 553*7c478bd9Sstevel@tonic-gate return (0); 554*7c478bd9Sstevel@tonic-gate } 555*7c478bd9Sstevel@tonic-gate 556*7c478bd9Sstevel@tonic-gate if (strcasecmp(buf, DC_STR_NO) == 0) { 557*7c478bd9Sstevel@tonic-gate dcp->dc_enable = DC_OFF; 558*7c478bd9Sstevel@tonic-gate return (0); 559*7c478bd9Sstevel@tonic-gate } 560*7c478bd9Sstevel@tonic-gate 561*7c478bd9Sstevel@tonic-gate warn(gettext("invalid enable value -- %s\n"), buf); 562*7c478bd9Sstevel@tonic-gate return (-1); 563*7c478bd9Sstevel@tonic-gate } 564*7c478bd9Sstevel@tonic-gate 565*7c478bd9Sstevel@tonic-gate static int 566*7c478bd9Sstevel@tonic-gate print_content(const dumpconf_t *dcp, FILE *fp) 567*7c478bd9Sstevel@tonic-gate { 568*7c478bd9Sstevel@tonic-gate const char *content; 569*7c478bd9Sstevel@tonic-gate 570*7c478bd9Sstevel@tonic-gate if (dcp->dc_cflags & DUMP_ALL) 571*7c478bd9Sstevel@tonic-gate content = DC_STR_ALL; 572*7c478bd9Sstevel@tonic-gate else if (dcp->dc_cflags & DUMP_CURPROC) 573*7c478bd9Sstevel@tonic-gate content = DC_STR_CURPROC; 574*7c478bd9Sstevel@tonic-gate else 575*7c478bd9Sstevel@tonic-gate content = DC_STR_KERNEL; 576*7c478bd9Sstevel@tonic-gate 577*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", content)); 578*7c478bd9Sstevel@tonic-gate } 579*7c478bd9Sstevel@tonic-gate 580*7c478bd9Sstevel@tonic-gate static int 581*7c478bd9Sstevel@tonic-gate print_device(const dumpconf_t *dcp, FILE *fp) 582*7c478bd9Sstevel@tonic-gate { 583*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", (dcp->dc_device[0] != '\0') ? 584*7c478bd9Sstevel@tonic-gate dcp->dc_device : DC_STR_SWAP)); 585*7c478bd9Sstevel@tonic-gate } 586*7c478bd9Sstevel@tonic-gate 587*7c478bd9Sstevel@tonic-gate static int 588*7c478bd9Sstevel@tonic-gate print_enable(const dumpconf_t *dcp, FILE *fp) 589*7c478bd9Sstevel@tonic-gate { 590*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", (dcp->dc_enable == DC_OFF) ? 591*7c478bd9Sstevel@tonic-gate DC_STR_NO : DC_STR_YES)); 592*7c478bd9Sstevel@tonic-gate } 593*7c478bd9Sstevel@tonic-gate 594*7c478bd9Sstevel@tonic-gate static int 595*7c478bd9Sstevel@tonic-gate print_savdir(const dumpconf_t *dcp, FILE *fp) 596*7c478bd9Sstevel@tonic-gate { 597*7c478bd9Sstevel@tonic-gate return (fprintf(fp, "%s\n", dcp->dc_savdir)); 598*7c478bd9Sstevel@tonic-gate } 599