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