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