xref: /illumos-gate/usr/src/cmd/fm/fmadm/common/fmadm.c (revision 2a8bcb4e)
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 #include <strings.h>
277c478bd9Sstevel@tonic-gate #include <limits.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <fmadm.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static const char *g_pname;
367c478bd9Sstevel@tonic-gate static fmd_adm_t *g_adm;
377c478bd9Sstevel@tonic-gate static int g_quiet;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
407c478bd9Sstevel@tonic-gate void
note(const char * format,...)417c478bd9Sstevel@tonic-gate note(const char *format, ...)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	va_list ap;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	if (g_quiet)
467c478bd9Sstevel@tonic-gate 		return; /* suppress notices if -q specified */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s: ", g_pname);
497c478bd9Sstevel@tonic-gate 	va_start(ap, format);
507c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, format, ap);
517c478bd9Sstevel@tonic-gate 	va_end(ap);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static void
vwarn(const char * format,va_list ap)557c478bd9Sstevel@tonic-gate vwarn(const char *format, va_list ap)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	int err = errno;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", g_pname);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (format != NULL)
627c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, ap);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	errno = err; /* restore errno for fmd_adm_errmsg() */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (format == NULL)
677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s\n", fmd_adm_errmsg(g_adm));
687c478bd9Sstevel@tonic-gate 	else if (strchr(format, '\n') == NULL)
697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", fmd_adm_errmsg(g_adm));
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
737c478bd9Sstevel@tonic-gate void
warn(const char * format,...)747c478bd9Sstevel@tonic-gate warn(const char *format, ...)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	va_list ap;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	va_start(ap, format);
797c478bd9Sstevel@tonic-gate 	vwarn(format, ap);
807c478bd9Sstevel@tonic-gate 	va_end(ap);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
847c478bd9Sstevel@tonic-gate void
die(const char * format,...)857c478bd9Sstevel@tonic-gate die(const char *format, ...)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	va_list ap;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	va_start(ap, format);
907c478bd9Sstevel@tonic-gate 	vwarn(format, ap);
917c478bd9Sstevel@tonic-gate 	va_end(ap);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	fmd_adm_close(g_adm);
947c478bd9Sstevel@tonic-gate 	exit(FMADM_EXIT_ERROR);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static const struct cmd {
987c478bd9Sstevel@tonic-gate 	int (*cmd_func)(fmd_adm_t *, int, char *[]);
997c478bd9Sstevel@tonic-gate 	const char *cmd_name;
1007c478bd9Sstevel@tonic-gate 	const char *cmd_usage;
1017c478bd9Sstevel@tonic-gate 	const char *cmd_desc;
1027c478bd9Sstevel@tonic-gate } cmds[] = {
1037c478bd9Sstevel@tonic-gate { cmd_config, "config", NULL, "display fault manager configuration" },
10444743693Sstephh { cmd_faulty, "faulty", "[-afgiprsv] [-u <uuid>] [-n <max_fault>]",
10544743693Sstephh 	"display list of faulty resources" },
1067c478bd9Sstevel@tonic-gate { cmd_flush, "flush", "<fmri> ...", "flush cached state for resource" },
1077c478bd9Sstevel@tonic-gate { cmd_gc, "gc", "<module>", NULL },
1087c478bd9Sstevel@tonic-gate { cmd_load, "load", "<path>", "load specified fault manager module" },
109*25c6ff4bSstephh { cmd_repair, "repair", "<fmri>|label|<uuid>", NULL },
110*25c6ff4bSstephh { cmd_repaired, "repaired", "<fmri>|label>",
111*25c6ff4bSstephh 	"notify fault manager that resource has been repaired" },
112*25c6ff4bSstephh { cmd_acquit, "acquit", "<fmri> [<uuid>] | label [<uuid>] | <uuid>",
113*25c6ff4bSstephh 	"acquit resource or acquit case" },
114*25c6ff4bSstephh { cmd_replaced, "replaced", "<fmri>|label",
115*25c6ff4bSstephh 	"notify fault manager that resource has been replaced" },
1167c478bd9Sstevel@tonic-gate { cmd_reset, "reset", "[-s serd] <module>", "reset module or sub-component" },
1177c478bd9Sstevel@tonic-gate { cmd_rotate, "rotate", "<logname>", "rotate log file" },
1187c478bd9Sstevel@tonic-gate { cmd_unload, "unload", "<module>", "unload specified fault manager module" },
1197c478bd9Sstevel@tonic-gate { NULL, NULL, NULL }
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static int
usage(FILE * fp)1237c478bd9Sstevel@tonic-gate usage(FILE *fp)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	const struct cmd *cp;
1267c478bd9Sstevel@tonic-gate 	char buf[256];
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) fprintf(fp,
1297c478bd9Sstevel@tonic-gate 	    "Usage: %s [-P prog] [-q] [cmd [args ... ]]\n\n", g_pname);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
1327c478bd9Sstevel@tonic-gate 		if (cp->cmd_desc == NULL)
1337c478bd9Sstevel@tonic-gate 			continue;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		if (cp->cmd_usage != NULL) {
1367c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s %s %s",
1377c478bd9Sstevel@tonic-gate 			    g_pname, cp->cmd_name, cp->cmd_usage);
1387c478bd9Sstevel@tonic-gate 		} else {
1397c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s %s",
1407c478bd9Sstevel@tonic-gate 			    g_pname, cp->cmd_name);
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%-30s - %s\n", buf, cp->cmd_desc);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (FMADM_EXIT_USAGE);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static uint32_t
getu32(const char * name,const char * s)1497c478bd9Sstevel@tonic-gate getu32(const char *name, const char *s)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	u_longlong_t val;
1527c478bd9Sstevel@tonic-gate 	char *p;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	errno = 0;
1557c478bd9Sstevel@tonic-gate 	val = strtoull(s, &p, 0);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (errno != 0 || p == s || *p != '\0' || val > UINT32_MAX) {
1587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: invalid %s argument -- %s\n",
1597c478bd9Sstevel@tonic-gate 		    g_pname, name, s);
1607c478bd9Sstevel@tonic-gate 		exit(FMADM_EXIT_USAGE);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	return ((uint32_t)val);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1677c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	const struct cmd *cp;
1707c478bd9Sstevel@tonic-gate 	uint32_t program;
1717c478bd9Sstevel@tonic-gate 	const char *p;
1727c478bd9Sstevel@tonic-gate 	int c, err;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if ((p = strrchr(argv[0], '/')) == NULL)
1757c478bd9Sstevel@tonic-gate 		g_pname = argv[0];
1767c478bd9Sstevel@tonic-gate 	else
1777c478bd9Sstevel@tonic-gate 		g_pname = p + 1;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if ((p = getenv("FMD_PROGRAM")) != NULL)
1807c478bd9Sstevel@tonic-gate 		program = getu32("$FMD_PROGRAM", p);
1817c478bd9Sstevel@tonic-gate 	else
1827c478bd9Sstevel@tonic-gate 		program = FMD_ADM_PROGRAM;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "P:q")) != EOF) {
1857c478bd9Sstevel@tonic-gate 		switch (c) {
1867c478bd9Sstevel@tonic-gate 		case 'P':
1877c478bd9Sstevel@tonic-gate 			program = getu32("program", optarg);
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		case 'q':
1907c478bd9Sstevel@tonic-gate 			g_quiet++;
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 		default:
1937c478bd9Sstevel@tonic-gate 			return (usage(stderr));
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (optind >= argc)
1987c478bd9Sstevel@tonic-gate 		return (usage(stdout));
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	for (cp = cmds; cp->cmd_name != NULL; cp++) {
2017c478bd9Sstevel@tonic-gate 		if (strcmp(cp->cmd_name, argv[optind]) == 0)
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (cp->cmd_name == NULL) {
2067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: illegal subcommand -- %s\n",
2077c478bd9Sstevel@tonic-gate 		    g_pname, argv[optind]);
2087c478bd9Sstevel@tonic-gate 		return (usage(stderr));
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if ((g_adm = fmd_adm_open(NULL, program, FMD_ADM_VERSION)) == NULL)
2127c478bd9Sstevel@tonic-gate 		die(NULL); /* fmd_adm_errmsg() has enough info */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	argc -= optind;
2157c478bd9Sstevel@tonic-gate 	argv += optind;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	optind = 1; /* reset optind so subcommands can getopt() */
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	err = cp->cmd_func(g_adm, argc, argv);
2207c478bd9Sstevel@tonic-gate 	fmd_adm_close(g_adm);
2217c478bd9Sstevel@tonic-gate 	return (err == FMADM_EXIT_USAGE ? usage(stderr) : err);
2227c478bd9Sstevel@tonic-gate }
223