xref: /titanic_41/usr/src/cmd/fm/fmadm/common/fmadm.c (revision 25c6ff4b77fcddf4097ce78a8277275ca603b46c)
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
544743693Sstephh  * Common Development and Distribution License (the "License").
644743693Sstephh  * 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*25c6ff4bSstephh  * Copyright 2008 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
note(const char * format,...)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
vwarn(const char * format,va_list ap)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
warn(const char * format,...)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
die(const char * format,...)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" },
10644743693Sstephh { cmd_faulty, "faulty", "[-afgiprsv] [-u <uuid>] [-n <max_fault>]",
10744743693Sstephh 	"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" },
111*25c6ff4bSstephh { cmd_repair, "repair", "<fmri>|label|<uuid>", NULL },
112*25c6ff4bSstephh { cmd_repaired, "repaired", "<fmri>|label>",
113*25c6ff4bSstephh 	"notify fault manager that resource has been repaired" },
114*25c6ff4bSstephh { cmd_acquit, "acquit", "<fmri> [<uuid>] | label [<uuid>] | <uuid>",
115*25c6ff4bSstephh 	"acquit resource or acquit case" },
116*25c6ff4bSstephh { cmd_replaced, "replaced", "<fmri>|label",
117*25c6ff4bSstephh 	"notify fault manager that resource has been replaced" },
1187c478bd9Sstevel@tonic-gate { cmd_reset, "reset", "[-s serd] <module>", "reset module or sub-component" },
1197c478bd9Sstevel@tonic-gate { cmd_rotate, "rotate", "<logname>", "rotate log file" },
1207c478bd9Sstevel@tonic-gate { cmd_unload, "unload", "<module>", "unload specified fault manager module" },
1217c478bd9Sstevel@tonic-gate { NULL, NULL, NULL }
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int
usage(FILE * fp)1257c478bd9Sstevel@tonic-gate usage(FILE *fp)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	const struct cmd *cp;
1287c478bd9Sstevel@tonic-gate 	char buf[256];
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	(void) fprintf(fp,
1317c478bd9Sstevel@tonic-gate 	    "Usage: %s [-P prog] [-q] [cmd [args ... ]]\n\n", g_pname);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
1347c478bd9Sstevel@tonic-gate 		if (cp->cmd_desc == NULL)
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		if (cp->cmd_usage != NULL) {
1387c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s %s %s",
1397c478bd9Sstevel@tonic-gate 			    g_pname, cp->cmd_name, cp->cmd_usage);
1407c478bd9Sstevel@tonic-gate 		} else {
1417c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s %s",
1427c478bd9Sstevel@tonic-gate 			    g_pname, cp->cmd_name);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%-30s - %s\n", buf, cp->cmd_desc);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (FMADM_EXIT_USAGE);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static uint32_t
getu32(const char * name,const char * s)1517c478bd9Sstevel@tonic-gate getu32(const char *name, const char *s)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	u_longlong_t val;
1547c478bd9Sstevel@tonic-gate 	char *p;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	errno = 0;
1577c478bd9Sstevel@tonic-gate 	val = strtoull(s, &p, 0);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (errno != 0 || p == s || *p != '\0' || val > UINT32_MAX) {
1607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: invalid %s argument -- %s\n",
1617c478bd9Sstevel@tonic-gate 		    g_pname, name, s);
1627c478bd9Sstevel@tonic-gate 		exit(FMADM_EXIT_USAGE);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return ((uint32_t)val);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1697c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	const struct cmd *cp;
1727c478bd9Sstevel@tonic-gate 	uint32_t program;
1737c478bd9Sstevel@tonic-gate 	const char *p;
1747c478bd9Sstevel@tonic-gate 	int c, err;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if ((p = strrchr(argv[0], '/')) == NULL)
1777c478bd9Sstevel@tonic-gate 		g_pname = argv[0];
1787c478bd9Sstevel@tonic-gate 	else
1797c478bd9Sstevel@tonic-gate 		g_pname = p + 1;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if ((p = getenv("FMD_PROGRAM")) != NULL)
1827c478bd9Sstevel@tonic-gate 		program = getu32("$FMD_PROGRAM", p);
1837c478bd9Sstevel@tonic-gate 	else
1847c478bd9Sstevel@tonic-gate 		program = FMD_ADM_PROGRAM;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "P:q")) != EOF) {
1877c478bd9Sstevel@tonic-gate 		switch (c) {
1887c478bd9Sstevel@tonic-gate 		case 'P':
1897c478bd9Sstevel@tonic-gate 			program = getu32("program", optarg);
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		case 'q':
1927c478bd9Sstevel@tonic-gate 			g_quiet++;
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		default:
1957c478bd9Sstevel@tonic-gate 			return (usage(stderr));
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (optind >= argc)
2007c478bd9Sstevel@tonic-gate 		return (usage(stdout));
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
2037c478bd9Sstevel@tonic-gate 		if (strcmp(cp->cmd_name, argv[optind]) == 0)
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (cp->cmd_name == NULL) {
2087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: illegal subcommand -- %s\n",
2097c478bd9Sstevel@tonic-gate 		    g_pname, argv[optind]);
2107c478bd9Sstevel@tonic-gate 		return (usage(stderr));
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ((g_adm = fmd_adm_open(NULL, program, FMD_ADM_VERSION)) == NULL)
2147c478bd9Sstevel@tonic-gate 		die(NULL); /* fmd_adm_errmsg() has enough info */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	argc -= optind;
2177c478bd9Sstevel@tonic-gate 	argv += optind;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	optind = 1; /* reset optind so subcommands can getopt() */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	err = cp->cmd_func(g_adm, argc, argv);
2227c478bd9Sstevel@tonic-gate 	fmd_adm_close(g_adm);
2237c478bd9Sstevel@tonic-gate 	return (err == FMADM_EXIT_USAGE ? usage(stderr) : err);
2247c478bd9Sstevel@tonic-gate }
225