xref: /titanic_41/usr/src/cmd/fm/fmadm/common/faulty.c (revision 2449e17f82f6097fd2c665b64723e31ceecbeca6)
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 2004 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 <strings.h>
30 #include <fmadm.h>
31 
32 static const char *
33 rsrc_state_name(const fmd_adm_rsrcinfo_t *ari)
34 {
35 	switch (ari->ari_flags & (FMD_ADM_RSRC_FAULTY|FMD_ADM_RSRC_UNUSABLE)) {
36 	default:
37 		return ("ok");
38 	case FMD_ADM_RSRC_FAULTY:
39 		return ("degraded");
40 	case FMD_ADM_RSRC_UNUSABLE:
41 		return ("unknown");
42 	case FMD_ADM_RSRC_FAULTY | FMD_ADM_RSRC_UNUSABLE:
43 		return ("faulted");
44 	}
45 }
46 
47 static const char faulty_line[] = "-------- "
48 "----------------------------------------------------------------------";
49 
50 #define	IS_FAULTY(ari)	\
51 	(((ari)->ari_flags & (FMD_ADM_RSRC_FAULTY | \
52 	FMD_ADM_RSRC_INVISIBLE)) == FMD_ADM_RSRC_FAULTY)
53 
54 /*ARGSUSED*/
55 static int
56 faulty_fmri(const fmd_adm_rsrcinfo_t *ari, void *opt_a)
57 {
58 	if (opt_a || IS_FAULTY(ari)) {
59 		(void) printf("%s\n%8s %s\n%8s %s\n",
60 		    faulty_line, rsrc_state_name(ari), ari->ari_fmri, "",
61 		    ari->ari_case ? ari->ari_case : "-");
62 	}
63 
64 	return (0);
65 }
66 
67 /*ARGSUSED*/
68 static int
69 faulty_uuid(const fmd_adm_rsrcinfo_t *ari, void *opt_a)
70 {
71 	if (opt_a || IS_FAULTY(ari)) {
72 		(void) printf("%s\n%8s %s\n%8s %s\n",
73 		    faulty_line, rsrc_state_name(ari), ari->ari_fmri, "",
74 		    ari->ari_uuid);
75 	}
76 
77 	return (0);
78 }
79 
80 int
81 cmd_faulty(fmd_adm_t *adm, int argc, char *argv[])
82 {
83 	fmd_adm_rsrc_f *func = faulty_fmri;
84 	int c, opt_a = 0;
85 
86 	while ((c = getopt(argc, argv, "ai")) != EOF) {
87 		switch (c) {
88 		case 'a':
89 			opt_a++;
90 			break;
91 		case 'i':
92 			func = faulty_uuid;
93 			break;
94 		default:
95 			return (FMADM_EXIT_USAGE);
96 		}
97 	}
98 
99 	if (optind < argc)
100 		return (FMADM_EXIT_USAGE);
101 
102 	if (func == faulty_fmri)
103 		(void) printf("%8s %s\n", "STATE", "RESOURCE / UUID");
104 	else
105 		(void) printf("%8s %s\n", "STATE", "RESOURCE / CACHE-ID");
106 
107 	if (fmd_adm_rsrc_iter(adm, opt_a, func, (void *)opt_a) != 0)
108 		die("failed to retrieve resource data");
109 
110 	(void) printf("%s\n", faulty_line);
111 	return (FMADM_EXIT_SUCCESS);
112 }
113 
114 int
115 cmd_flush(fmd_adm_t *adm, int argc, char *argv[])
116 {
117 	int i, status = FMADM_EXIT_SUCCESS;
118 
119 	if (argc < 2 || (i = getopt(argc, argv, "")) != EOF)
120 		return (FMADM_EXIT_USAGE);
121 
122 	for (i = 1; i < argc; i++) {
123 		if (fmd_adm_rsrc_flush(adm, argv[i]) != 0) {
124 			warn("failed to flush %s", argv[i]);
125 			status = FMADM_EXIT_ERROR;
126 		} else
127 			note("flushed resource history for %s\n", argv[i]);
128 	}
129 
130 	return (status);
131 }
132 
133 int
134 cmd_repair(fmd_adm_t *adm, int argc, char *argv[])
135 {
136 	int err;
137 
138 	if (getopt(argc, argv, "") != EOF)
139 		return (FMADM_EXIT_USAGE);
140 
141 	if (argc - optind != 1)
142 		return (FMADM_EXIT_USAGE);
143 
144 	/*
145 	 * For now, we assume that if the input string contains a colon, it is
146 	 * an FMRI and if it does not it is a UUID.  If things get more complex
147 	 * in the future with multiple UUID formats, an FMRI parser can be
148 	 * added here to differentiate the input argument appropriately.
149 	 */
150 	if (strchr(argv[optind], ':') != NULL)
151 		err = fmd_adm_rsrc_repair(adm, argv[optind]);
152 	else
153 		err = fmd_adm_case_repair(adm, argv[optind]);
154 
155 	if (err != 0)
156 		die("failed to record repair to %s", argv[optind]);
157 
158 	note("recorded repair to %s\n", argv[optind]);
159 	return (FMADM_EXIT_SUCCESS);
160 }
161