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