xref: /titanic_41/usr/src/cmd/fm/fmadm/common/fmadm.c (revision b02e9a2d4d2071d770e5aa9ae8f83f2bbe1f2ced)
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 <strings.h>
29 #include <limits.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <errno.h>
34 
35 #include <fmadm.h>
36 
37 static const char *g_pname;
38 static fmd_adm_t *g_adm;
39 static int g_quiet;
40 
41 /*PRINTFLIKE1*/
42 void
43 note(const char *format, ...)
44 {
45 	va_list ap;
46 
47 	if (g_quiet)
48 		return; /* suppress notices if -q specified */
49 
50 	(void) fprintf(stdout, "%s: ", g_pname);
51 	va_start(ap, format);
52 	(void) vfprintf(stdout, format, ap);
53 	va_end(ap);
54 }
55 
56 static void
57 vwarn(const char *format, va_list ap)
58 {
59 	int err = errno;
60 
61 	(void) fprintf(stderr, "%s: ", g_pname);
62 
63 	if (format != NULL)
64 		(void) vfprintf(stderr, format, ap);
65 
66 	errno = err; /* restore errno for fmd_adm_errmsg() */
67 
68 	if (format == NULL)
69 		(void) fprintf(stderr, "%s\n", fmd_adm_errmsg(g_adm));
70 	else if (strchr(format, '\n') == NULL)
71 		(void) fprintf(stderr, ": %s\n", fmd_adm_errmsg(g_adm));
72 }
73 
74 /*PRINTFLIKE1*/
75 void
76 warn(const char *format, ...)
77 {
78 	va_list ap;
79 
80 	va_start(ap, format);
81 	vwarn(format, ap);
82 	va_end(ap);
83 }
84 
85 /*PRINTFLIKE1*/
86 void
87 die(const char *format, ...)
88 {
89 	va_list ap;
90 
91 	va_start(ap, format);
92 	vwarn(format, ap);
93 	va_end(ap);
94 
95 	fmd_adm_close(g_adm);
96 	exit(FMADM_EXIT_ERROR);
97 }
98 
99 static const struct cmd {
100 	int (*cmd_func)(fmd_adm_t *, int, char *[]);
101 	const char *cmd_name;
102 	const char *cmd_usage;
103 	const char *cmd_desc;
104 } cmds[] = {
105 { cmd_config, "config", NULL, "display fault manager configuration" },
106 { cmd_faulty, "faulty", "[-afgiprsv] [-u <uuid>] [-n <max_fault>]",
107 	"display list of faulty resources" },
108 { cmd_flush, "flush", "<fmri> ...", "flush cached state for resource" },
109 { cmd_gc, "gc", "<module>", NULL },
110 { cmd_load, "load", "<path>", "load specified fault manager module" },
111 { cmd_repair, "repair", "<fmri>|<uuid>", "record repair to resource(s)" },
112 { cmd_reset, "reset", "[-s serd] <module>", "reset module or sub-component" },
113 { cmd_rotate, "rotate", "<logname>", "rotate log file" },
114 { cmd_unload, "unload", "<module>", "unload specified fault manager module" },
115 { NULL, NULL, NULL }
116 };
117 
118 static int
119 usage(FILE *fp)
120 {
121 	const struct cmd *cp;
122 	char buf[256];
123 
124 	(void) fprintf(fp,
125 	    "Usage: %s [-P prog] [-q] [cmd [args ... ]]\n\n", g_pname);
126 
127 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
128 		if (cp->cmd_desc == NULL)
129 			continue;
130 
131 		if (cp->cmd_usage != NULL) {
132 			(void) snprintf(buf, sizeof (buf), "%s %s %s",
133 			    g_pname, cp->cmd_name, cp->cmd_usage);
134 		} else {
135 			(void) snprintf(buf, sizeof (buf), "%s %s",
136 			    g_pname, cp->cmd_name);
137 		}
138 		(void) fprintf(fp, "\t%-30s - %s\n", buf, cp->cmd_desc);
139 	}
140 
141 	return (FMADM_EXIT_USAGE);
142 }
143 
144 static uint32_t
145 getu32(const char *name, const char *s)
146 {
147 	u_longlong_t val;
148 	char *p;
149 
150 	errno = 0;
151 	val = strtoull(s, &p, 0);
152 
153 	if (errno != 0 || p == s || *p != '\0' || val > UINT32_MAX) {
154 		(void) fprintf(stderr, "%s: invalid %s argument -- %s\n",
155 		    g_pname, name, s);
156 		exit(FMADM_EXIT_USAGE);
157 	}
158 
159 	return ((uint32_t)val);
160 }
161 
162 int
163 main(int argc, char *argv[])
164 {
165 	const struct cmd *cp;
166 	uint32_t program;
167 	const char *p;
168 	int c, err;
169 
170 	if ((p = strrchr(argv[0], '/')) == NULL)
171 		g_pname = argv[0];
172 	else
173 		g_pname = p + 1;
174 
175 	if ((p = getenv("FMD_PROGRAM")) != NULL)
176 		program = getu32("$FMD_PROGRAM", p);
177 	else
178 		program = FMD_ADM_PROGRAM;
179 
180 	while ((c = getopt(argc, argv, "P:q")) != EOF) {
181 		switch (c) {
182 		case 'P':
183 			program = getu32("program", optarg);
184 			break;
185 		case 'q':
186 			g_quiet++;
187 			break;
188 		default:
189 			return (usage(stderr));
190 		}
191 	}
192 
193 	if (optind >= argc)
194 		return (usage(stdout));
195 
196 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
197 		if (strcmp(cp->cmd_name, argv[optind]) == 0)
198 			break;
199 	}
200 
201 	if (cp->cmd_name == NULL) {
202 		(void) fprintf(stderr, "%s: illegal subcommand -- %s\n",
203 		    g_pname, argv[optind]);
204 		return (usage(stderr));
205 	}
206 
207 	if ((g_adm = fmd_adm_open(NULL, program, FMD_ADM_VERSION)) == NULL)
208 		die(NULL); /* fmd_adm_errmsg() has enough info */
209 
210 	argc -= optind;
211 	argv += optind;
212 
213 	optind = 1; /* reset optind so subcommands can getopt() */
214 
215 	err = cp->cmd_func(g_adm, argc, argv);
216 	fmd_adm_close(g_adm);
217 	return (err == FMADM_EXIT_USAGE ? usage(stderr) : err);
218 }
219