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 <limits.h> 31 #include <stdlib.h> 32 #include <stdarg.h> 33 #include <stdio.h> 34 #include <errno.h> 35 36 #include <fmadm.h> 37 38 static const char *g_pname; 39 static fmd_adm_t *g_adm; 40 static int g_quiet; 41 42 /*PRINTFLIKE1*/ 43 void 44 note(const char *format, ...) 45 { 46 va_list ap; 47 48 if (g_quiet) 49 return; /* suppress notices if -q specified */ 50 51 (void) fprintf(stdout, "%s: ", g_pname); 52 va_start(ap, format); 53 (void) vfprintf(stdout, format, ap); 54 va_end(ap); 55 } 56 57 static void 58 vwarn(const char *format, va_list ap) 59 { 60 int err = errno; 61 62 (void) fprintf(stderr, "%s: ", g_pname); 63 64 if (format != NULL) 65 (void) vfprintf(stderr, format, ap); 66 67 errno = err; /* restore errno for fmd_adm_errmsg() */ 68 69 if (format == NULL) 70 (void) fprintf(stderr, "%s\n", fmd_adm_errmsg(g_adm)); 71 else if (strchr(format, '\n') == NULL) 72 (void) fprintf(stderr, ": %s\n", fmd_adm_errmsg(g_adm)); 73 } 74 75 /*PRINTFLIKE1*/ 76 void 77 warn(const char *format, ...) 78 { 79 va_list ap; 80 81 va_start(ap, format); 82 vwarn(format, ap); 83 va_end(ap); 84 } 85 86 /*PRINTFLIKE1*/ 87 void 88 die(const char *format, ...) 89 { 90 va_list ap; 91 92 va_start(ap, format); 93 vwarn(format, ap); 94 va_end(ap); 95 96 fmd_adm_close(g_adm); 97 exit(FMADM_EXIT_ERROR); 98 } 99 100 static const struct cmd { 101 int (*cmd_func)(fmd_adm_t *, int, char *[]); 102 const char *cmd_name; 103 const char *cmd_usage; 104 const char *cmd_desc; 105 } cmds[] = { 106 { cmd_config, "config", NULL, "display fault manager configuration" }, 107 { cmd_faulty, "faulty", "[-ai]", "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