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