xref: /titanic_52/usr/src/cmd/fm/fminject/common/inj_main.c (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/sysevent/eventdefs.h>
27 #include <sys/fm/util.h>
28 #include <fm/fmd_log.h>
29 #include <libsysevent.h>
30 
31 #include <inj.h>
32 #include <inj_err.h>
33 #include <inj_string.h>
34 
35 int verbose;
36 int quiet;
37 
38 static int
39 usage(void)
40 {
41 	(void) fprintf(stderr, "Usage: %s [-nqv] [-c chan] [file]\n"
42 	    "\t-c  specify alternate channel to use for publication\n"
43 	    "\t-n  compile program but do not inject any events\n"
44 	    "\t-q  enable quiet mode (silence status messages)\n"
45 	    "\t-v  enable verbose output (display event details)\n",
46 	    getpname());
47 
48 	return (E_USAGE);
49 }
50 
51 /*
52  * Sysevent-based event delivery
53  */
54 static void *
55 sev_open(const char *chan)
56 {
57 	evchan_t *hdl;
58 
59 	if (chan == NULL)
60 		chan = FM_ERROR_CHAN;
61 
62 	if ((errno = sysevent_evc_bind(chan, &hdl,
63 	    EVCH_CREAT | EVCH_HOLD_PEND)) != 0)
64 		die("can't bind to error channel %s", chan);
65 
66 	return (hdl);
67 }
68 
69 static void
70 sev_send(void *arg, nvlist_t *msg)
71 {
72 	if ((errno = sysevent_evc_publish(arg, EC_FM, ESC_FM_ERROR,
73 	    "com.sun", getpname(), msg, EVCH_SLEEP)) != 0)
74 		warn("failed to send event");
75 }
76 
77 static void
78 sev_close(void *arg)
79 {
80 	(void) sysevent_evc_unbind(arg);
81 }
82 
83 static inj_mode_ops_t sysevent_ops = {
84 	sev_open,
85 	sev_send,
86 	sev_close
87 };
88 
89 /*
90  * Simulated delivery
91  */
92 /*ARGSUSED*/
93 static void *
94 sim_open(const char *arg)
95 {
96 	return ((void *)1);
97 }
98 
99 /*ARGSUSED*/
100 static void
101 sim_send(void *arg, nvlist_t *msg)
102 {
103 }
104 
105 /*ARGSUSED*/
106 static void
107 sim_close(void *arg)
108 {
109 }
110 
111 static inj_mode_ops_t simulate_ops = {
112 	sim_open,
113 	sim_send,
114 	sim_close
115 };
116 
117 int
118 main(int argc, char *argv[])
119 {
120 	const inj_mode_ops_t *mode = NULL;
121 	void *mode_arg = NULL;
122 	int c;
123 
124 	const char *file;
125 	inj_list_t *program;
126 	fmd_log_t *lp;
127 
128 	while ((c = getopt(argc, argv, "c:nqv")) != EOF) {
129 		switch (c) {
130 		case 'c':
131 			if (mode != NULL || mode_arg != NULL)
132 				return (usage());
133 
134 			mode = &sysevent_ops;
135 			mode_arg = optarg;
136 			break;
137 
138 		case 'n':
139 			if (mode != NULL)
140 				return (usage());
141 
142 			mode = &simulate_ops;
143 			break;
144 
145 		case 'q':
146 			quiet = 1;
147 			break;
148 
149 		case 'v':
150 			verbose = 1;
151 			break;
152 
153 		default:
154 			return (usage());
155 		}
156 	}
157 
158 	if (mode == NULL)
159 		mode = &sysevent_ops;
160 
161 	argc -= optind;
162 	argv += optind;
163 
164 	if (argc == 0)
165 		file = "-";
166 	else if (argc == 1)
167 		file = argv[0];
168 	else
169 		return (usage());
170 
171 	srand48(gethrtime());
172 
173 	if (argc > 0 && (lp = fmd_log_open(FMD_LOG_VERSION, file, &c)) != NULL)
174 		program = inj_logfile_read(lp);
175 	else
176 		program = inj_program_read(file);
177 
178 	inj_program_run(program, mode, mode_arg);
179 	return (0);
180 }
181