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