xref: /illumos-gate/usr/src/cmd/fm/fmadm/common/fmadm.c (revision 44743693)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*44743693Sstephh  * Common Development and Distribution License (the "License").
6*44743693Sstephh  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*44743693Sstephh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <limits.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <fmadm.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static const char *g_pname;
387c478bd9Sstevel@tonic-gate static fmd_adm_t *g_adm;
397c478bd9Sstevel@tonic-gate static int g_quiet;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
427c478bd9Sstevel@tonic-gate void
437c478bd9Sstevel@tonic-gate note(const char *format, ...)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	va_list ap;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (g_quiet)
487c478bd9Sstevel@tonic-gate 		return; /* suppress notices if -q specified */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s: ", g_pname);
517c478bd9Sstevel@tonic-gate 	va_start(ap, format);
527c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
537c478bd9Sstevel@tonic-gate 	va_end(ap);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static void
577c478bd9Sstevel@tonic-gate vwarn(const char *format, va_list ap)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	int err = errno;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (format != NULL)
647c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, ap);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	errno = err; /* restore errno for fmd_adm_errmsg() */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (format == NULL)
697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", fmd_adm_errmsg(g_adm));
707c478bd9Sstevel@tonic-gate 	else if (strchr(format, '\n') == NULL)
717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", fmd_adm_errmsg(g_adm));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
757c478bd9Sstevel@tonic-gate void
767c478bd9Sstevel@tonic-gate warn(const char *format, ...)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	va_list ap;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	va_start(ap, format);
817c478bd9Sstevel@tonic-gate 	vwarn(format, ap);
827c478bd9Sstevel@tonic-gate 	va_end(ap);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
867c478bd9Sstevel@tonic-gate void
877c478bd9Sstevel@tonic-gate die(const char *format, ...)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	va_list ap;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	va_start(ap, format);
927c478bd9Sstevel@tonic-gate 	vwarn(format, ap);
937c478bd9Sstevel@tonic-gate 	va_end(ap);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	fmd_adm_close(g_adm);
967c478bd9Sstevel@tonic-gate 	exit(FMADM_EXIT_ERROR);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static const struct cmd {
1007c478bd9Sstevel@tonic-gate 	int (*cmd_func)(fmd_adm_t *, int, char *[]);
1017c478bd9Sstevel@tonic-gate 	const char *cmd_name;
1027c478bd9Sstevel@tonic-gate 	const char *cmd_usage;
1037c478bd9Sstevel@tonic-gate 	const char *cmd_desc;
1047c478bd9Sstevel@tonic-gate } cmds[] = {
1057c478bd9Sstevel@tonic-gate { cmd_config, "config", NULL, "display fault manager configuration" },
106*44743693Sstephh { cmd_faulty, "faulty", "[-afgiprsv] [-u <uuid>] [-n <max_fault>]",
107*44743693Sstephh 	"display list of faulty resources" },
1087c478bd9Sstevel@tonic-gate { cmd_flush, "flush", "<fmri> ...", "flush cached state for resource" },
1097c478bd9Sstevel@tonic-gate { cmd_gc, "gc", "<module>", NULL },
1107c478bd9Sstevel@tonic-gate { cmd_load, "load", "<path>", "load specified fault manager module" },
1117c478bd9Sstevel@tonic-gate { cmd_repair, "repair", "<fmri>|<uuid>", "record repair to resource(s)" },
1127c478bd9Sstevel@tonic-gate { cmd_reset, "reset", "[-s serd] <module>", "reset module or sub-component" },
1137c478bd9Sstevel@tonic-gate { cmd_rotate, "rotate", "<logname>", "rotate log file" },
1147c478bd9Sstevel@tonic-gate { cmd_unload, "unload", "<module>", "unload specified fault manager module" },
1157c478bd9Sstevel@tonic-gate { NULL, NULL, NULL }
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static int
1197c478bd9Sstevel@tonic-gate usage(FILE *fp)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	const struct cmd *cp;
1227c478bd9Sstevel@tonic-gate 	char buf[256];
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) fprintf(fp,
1257c478bd9Sstevel@tonic-gate 	    "Usage: %s [-P prog] [-q] [cmd [args ... ]]\n\n", g_pname);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
1287c478bd9Sstevel@tonic-gate 		if (cp->cmd_desc == NULL)
1297c478bd9Sstevel@tonic-gate 			continue;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		if (cp->cmd_usage != NULL) {
1327c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s %s %s",
1337c478bd9Sstevel@tonic-gate 			    g_pname, cp->cmd_name, cp->cmd_usage);
1347c478bd9Sstevel@tonic-gate 		} else {
1357c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s %s",
1367c478bd9Sstevel@tonic-gate 			    g_pname, cp->cmd_name);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%-30s - %s\n", buf, cp->cmd_desc);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	return (FMADM_EXIT_USAGE);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static uint32_t
1457c478bd9Sstevel@tonic-gate getu32(const char *name, const char *s)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	u_longlong_t val;
1487c478bd9Sstevel@tonic-gate 	char *p;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	errno = 0;
1517c478bd9Sstevel@tonic-gate 	val = strtoull(s, &p, 0);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (errno != 0 || p == s || *p != '\0' || val > UINT32_MAX) {
1547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: invalid %s argument -- %s\n",
1557c478bd9Sstevel@tonic-gate 		    g_pname, name, s);
1567c478bd9Sstevel@tonic-gate 		exit(FMADM_EXIT_USAGE);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	return ((uint32_t)val);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate int
1637c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	const struct cmd *cp;
1667c478bd9Sstevel@tonic-gate 	uint32_t program;
1677c478bd9Sstevel@tonic-gate 	const char *p;
1687c478bd9Sstevel@tonic-gate 	int c, err;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if ((p = strrchr(argv[0], '/')) == NULL)
1717c478bd9Sstevel@tonic-gate 		g_pname = argv[0];
1727c478bd9Sstevel@tonic-gate 	else
1737c478bd9Sstevel@tonic-gate 		g_pname = p + 1;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if ((p = getenv("FMD_PROGRAM")) != NULL)
1767c478bd9Sstevel@tonic-gate 		program = getu32("$FMD_PROGRAM", p);
1777c478bd9Sstevel@tonic-gate 	else
1787c478bd9Sstevel@tonic-gate 		program = FMD_ADM_PROGRAM;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "P:q")) != EOF) {
1817c478bd9Sstevel@tonic-gate 		switch (c) {
1827c478bd9Sstevel@tonic-gate 		case 'P':
1837c478bd9Sstevel@tonic-gate 			program = getu32("program", optarg);
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		case 'q':
1867c478bd9Sstevel@tonic-gate 			g_quiet++;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		default:
1897c478bd9Sstevel@tonic-gate 			return (usage(stderr));
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (optind >= argc)
1947c478bd9Sstevel@tonic-gate 		return (usage(stdout));
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
1977c478bd9Sstevel@tonic-gate 		if (strcmp(cp->cmd_name, argv[optind]) == 0)
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (cp->cmd_name == NULL) {
2027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: illegal subcommand -- %s\n",
2037c478bd9Sstevel@tonic-gate 		    g_pname, argv[optind]);
2047c478bd9Sstevel@tonic-gate 		return (usage(stderr));
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if ((g_adm = fmd_adm_open(NULL, program, FMD_ADM_VERSION)) == NULL)
2087c478bd9Sstevel@tonic-gate 		die(NULL); /* fmd_adm_errmsg() has enough info */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	argc -= optind;
2117c478bd9Sstevel@tonic-gate 	argv += optind;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	optind = 1; /* reset optind so subcommands can getopt() */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	err = cp->cmd_func(g_adm, argc, argv);
2167c478bd9Sstevel@tonic-gate 	fmd_adm_close(g_adm);
2177c478bd9Sstevel@tonic-gate 	return (err == FMADM_EXIT_USAGE ? usage(stderr) : err);
2187c478bd9Sstevel@tonic-gate }
219