fmdump.c (602ca9ea) fmdump.c (c93c462e)
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

--- 5 unchanged lines hidden (view full) ---

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/*
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

--- 5 unchanged lines hidden (view full) ---

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.
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
23 * Use is subject to license terms.
24 */
25
26#pragma ident "%Z%%M% %I% %E% SMI"
27
28#include <alloca.h>
29#include <unistd.h>
30#include <limits.h>
31#include <strings.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdio.h>
35#include <errno.h>
36#include <time.h>
37#include <ctype.h>
38#include <regex.h>
26#include <alloca.h>
27#include <unistd.h>
28#include <limits.h>
29#include <strings.h>
30#include <stdlib.h>
31#include <stdarg.h>
32#include <stdio.h>
33#include <errno.h>
34#include <time.h>
35#include <ctype.h>
36#include <regex.h>
37#include <dirent.h>
39
40#include <fmdump.h>
41
42#define FMDUMP_EXIT_SUCCESS 0
43#define FMDUMP_EXIT_FATAL 1
44#define FMDUMP_EXIT_USAGE 2
45#define FMDUMP_EXIT_ERROR 3
46

--- 393 unchanged lines hidden (view full) ---

440log_filter_silent(fmd_log_t *lp, const fmd_log_record_t *rp, void *arg)
441{
442 boolean_t msg;
443
444 return (nvlist_lookup_boolean_value(rp->rec_nvl,
445 FM_SUSPECT_MESSAGE, &msg) != 0 || msg != 0);
446}
447
38
39#include <fmdump.h>
40
41#define FMDUMP_EXIT_SUCCESS 0
42#define FMDUMP_EXIT_FATAL 1
43#define FMDUMP_EXIT_USAGE 2
44#define FMDUMP_EXIT_ERROR 3
45

--- 393 unchanged lines hidden (view full) ---

439log_filter_silent(fmd_log_t *lp, const fmd_log_record_t *rp, void *arg)
440{
441 boolean_t msg;
442
443 return (nvlist_lookup_boolean_value(rp->rec_nvl,
444 FM_SUSPECT_MESSAGE, &msg) != 0 || msg != 0);
445}
446
447struct loglink {
448 char *path;
449 long suffix;
450 struct loglink *next;
451};
452
453static void
454addlink(struct loglink **llp, char *dirname, char *logname, long suffix)
455{
456 struct loglink *newp;
457 size_t len;
458 char *str;
459
460 newp = malloc(sizeof (struct loglink));
461 len = strlen(dirname) + strlen(logname) + 2;
462 str = malloc(len);
463 if (newp == NULL || str == NULL) {
464 (void) fprintf(stderr, "%s: failed to allocate memory: %s\n",
465 g_pname, strerror(errno));
466 exit(FMDUMP_EXIT_FATAL);
467 }
468
469 (void) snprintf(str, len, "%s/%s", dirname, logname);
470 newp->path = str;
471 newp->suffix = suffix;
472
473 while (*llp != NULL && suffix < (*llp)->suffix)
474 llp = &(*llp)->next;
475
476 newp->next = *llp;
477 *llp = newp;
478}
479
480/*
481 * Find and return all the rotated logs.
482 */
483static struct loglink *
484get_rotated_logs(char *logpath)
485{
486 char dirname[PATH_MAX], *logname, *endptr;
487 DIR *dirp;
488 struct dirent *dp;
489 long len, suffix;
490 struct loglink *head = NULL;
491
492 (void) strlcpy(dirname, logpath, sizeof (dirname));
493 logname = strrchr(dirname, '/');
494 *logname++ = '\0';
495 len = strlen(logname);
496
497 if ((dirp = opendir(dirname)) == NULL) {
498 (void) fprintf(stderr, "%s: failed to opendir `%s': %s\n",
499 g_pname, dirname, strerror(errno));
500 return (NULL);
501 }
502
503 while ((dp = readdir(dirp)) != NULL) {
504 /*
505 * Search the log directory for logs named "<logname>.0",
506 * "<logname>.1", etc and add to the link in the
507 * reverse numeric order.
508 */
509 if (strlen(dp->d_name) < len + 2 ||
510 strncmp(dp->d_name, logname, len) != 0 ||
511 dp->d_name[len] != '.')
512 continue;
513
514 /*
515 * "*.0-" file normally should not be seen. It may
516 * exist when user manually run 'fmadm rotate'.
517 * In such case, we put it at the end of the list so
518 * it'll be dumped after all the rotated logs, before
519 * the current one.
520 */
521 if (strcmp(dp->d_name + len + 1, "0-") == 0)
522 addlink(&head, dirname, dp->d_name, -1);
523 else if ((suffix = strtol(dp->d_name + len + 1,
524 &endptr, 10)) >= 0 && *endptr == '\0')
525 addlink(&head, dirname, dp->d_name, suffix);
526 }
527
528 (void) closedir(dirp);
529
530 return (head);
531}
532
448int
449main(int argc, char *argv[])
450{
451 int opt_a = 0, opt_e = 0, opt_f = 0, opt_H = 0;
452 int opt_u = 0, opt_v = 0, opt_V = 0;
453
454 char ifile[PATH_MAX] = "";
455 int iflags = 0;

--- 8 unchanged lines hidden (view full) ---

464 uint_t errfc = 0, fltfc = 0, allfc = 0;
465
466 fmd_log_header_t log;
467 fmd_log_rec_f *func;
468 void *farg;
469 fmd_log_t *lp;
470 int c, err;
471 off64_t off = 0;
533int
534main(int argc, char *argv[])
535{
536 int opt_a = 0, opt_e = 0, opt_f = 0, opt_H = 0;
537 int opt_u = 0, opt_v = 0, opt_V = 0;
538
539 char ifile[PATH_MAX] = "";
540 int iflags = 0;

--- 8 unchanged lines hidden (view full) ---

549 uint_t errfc = 0, fltfc = 0, allfc = 0;
550
551 fmd_log_header_t log;
552 fmd_log_rec_f *func;
553 void *farg;
554 fmd_log_t *lp;
555 int c, err;
556 off64_t off = 0;
557 ulong_t recs;
558 struct loglink *rotated_logs = NULL, *llp;
472
473 g_pname = argv[0];
474
475 errfv = alloca(sizeof (fmd_log_filter_t) * argc);
476 fltfv = alloca(sizeof (fmd_log_filter_t) * argc);
477 allfv = alloca(sizeof (fmd_log_filter_t) * argc);
478
479 while (optind < argc) {

--- 68 unchanged lines hidden (view full) ---

548 argv[optind++], sizeof (ifile));
549 }
550 }
551 }
552
553 if (*ifile == '\0') {
554 (void) snprintf(ifile, sizeof (ifile), "%s/var/fm/fmd/%slog",
555 g_root ? g_root : "", opt_e && !opt_u ? "err" : "flt");
559
560 g_pname = argv[0];
561
562 errfv = alloca(sizeof (fmd_log_filter_t) * argc);
563 fltfv = alloca(sizeof (fmd_log_filter_t) * argc);
564 allfv = alloca(sizeof (fmd_log_filter_t) * argc);
565
566 while (optind < argc) {

--- 68 unchanged lines hidden (view full) ---

635 argv[optind++], sizeof (ifile));
636 }
637 }
638 }
639
640 if (*ifile == '\0') {
641 (void) snprintf(ifile, sizeof (ifile), "%s/var/fm/fmd/%slog",
642 g_root ? g_root : "", opt_e && !opt_u ? "err" : "flt");
643 /*
644 * logadm may rotate the logs. When no input file is specified,
645 * we try to dump all the rotated logs as well in the right
646 * order.
647 */
648 if (!opt_H && off == 0)
649 rotated_logs = get_rotated_logs(ifile);
556 } else if (g_root != NULL) {
557 (void) fprintf(stderr, "%s: -R option is not appropriate "
558 "when file operand is present\n", g_pname);
559 return (FMDUMP_EXIT_USAGE);
560 }
561
562 if ((lp = fmd_log_open(FMD_LOG_VERSION, ifile, &err)) == NULL) {
563 (void) fprintf(stderr, "%s: failed to open %s: %s\n",

--- 71 unchanged lines hidden (view full) ---

635 if (iflags & FMD_LOG_XITER_OFFS) {
636 lyr.dy_func = func;
637 lyr.dy_arg = farg;
638 lyr.dy_fp = arg.da_fp;
639 func = xoff_iter;
640 farg = &lyr;
641 }
642
650 } else if (g_root != NULL) {
651 (void) fprintf(stderr, "%s: -R option is not appropriate "
652 "when file operand is present\n", g_pname);
653 return (FMDUMP_EXIT_USAGE);
654 }
655
656 if ((lp = fmd_log_open(FMD_LOG_VERSION, ifile, &err)) == NULL) {
657 (void) fprintf(stderr, "%s: failed to open %s: %s\n",

--- 71 unchanged lines hidden (view full) ---

729 if (iflags & FMD_LOG_XITER_OFFS) {
730 lyr.dy_func = func;
731 lyr.dy_arg = farg;
732 lyr.dy_fp = arg.da_fp;
733 func = xoff_iter;
734 farg = &lyr;
735 }
736
737 for (llp = rotated_logs; llp != NULL; llp = llp->next) {
738 fmd_log_t *rlp;
739
740 if ((rlp = fmd_log_open(FMD_LOG_VERSION, llp->path, &err))
741 == NULL) {
742 (void) fprintf(stderr, "%s: failed to open %s: %s\n",
743 g_pname, llp->path, fmd_log_errmsg(NULL, err));
744 g_errs++;
745 continue;
746 }
747
748 recs = 0;
749 if (fmd_log_xiter(rlp, iflags, filtc, filtv,
750 func, error, farg, &recs) != 0) {
751 (void) fprintf(stderr,
752 "%s: failed to dump %s: %s\n", g_pname, llp->path,
753 fmd_log_errmsg(rlp, fmd_log_errno(rlp)));
754 g_errs++;
755 }
756 g_recs += recs;
757
758 fmd_log_close(rlp);
759 }
760
643 do {
761 do {
762 recs = 0;
644 if (fmd_log_xiter(lp, iflags, filtc, filtv,
763 if (fmd_log_xiter(lp, iflags, filtc, filtv,
645 func, error, farg, &g_recs) != 0) {
764 func, error, farg, &recs) != 0) {
646 (void) fprintf(stderr,
647 "%s: failed to dump %s: %s\n", g_pname, ifile,
648 fmd_log_errmsg(lp, fmd_log_errno(lp)));
649 g_errs++;
650 }
765 (void) fprintf(stderr,
766 "%s: failed to dump %s: %s\n", g_pname, ifile,
767 fmd_log_errmsg(lp, fmd_log_errno(lp)));
768 g_errs++;
769 }
770 g_recs += recs;
651
652 if (opt_f)
653 (void) sleep(1);
654
655 } while (opt_f);
656
657 if (!opt_f && g_recs == 0 && isatty(STDOUT_FILENO))
658 (void) fprintf(stderr, "%s: %s is empty\n", g_pname, ifile);
659
660 fmd_log_close(lp);
661 return (g_errs ? FMDUMP_EXIT_ERROR : FMDUMP_EXIT_SUCCESS);
662}
771
772 if (opt_f)
773 (void) sleep(1);
774
775 } while (opt_f);
776
777 if (!opt_f && g_recs == 0 && isatty(STDOUT_FILENO))
778 (void) fprintf(stderr, "%s: %s is empty\n", g_pname, ifile);
779
780 fmd_log_close(lp);
781 return (g_errs ? FMDUMP_EXIT_ERROR : FMDUMP_EXIT_SUCCESS);
782}