dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / fminject / common / inj_main.c
blobbf4713ee3ef1768b8eab2e3eb773a796ef79d93c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/sysevent/eventdefs.h>
27 #include <sys/fm/util.h>
28 #include <fm/fmd_log.h>
29 #include <libsysevent.h>
31 #include <inj.h>
32 #include <inj_err.h>
33 #include <inj_string.h>
35 int verbose;
36 int quiet;
38 static int
39 usage(void)
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());
48 return (E_USAGE);
52 * Sysevent-based event delivery
54 static void *
55 sev_open(const char *chan)
57 evchan_t *hdl;
59 if (chan == NULL)
60 chan = FM_ERROR_CHAN;
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);
66 return (hdl);
69 static void
70 sev_send(void *arg, nvlist_t *msg)
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");
77 static void
78 sev_close(void *arg)
80 (void) sysevent_evc_unbind(arg);
83 static inj_mode_ops_t sysevent_ops = {
84 sev_open,
85 sev_send,
86 sev_close
90 * Simulated delivery
92 /*ARGSUSED*/
93 static void *
94 sim_open(const char *arg)
96 return ((void *)1);
99 /*ARGSUSED*/
100 static void
101 sim_send(void *arg, nvlist_t *msg)
105 /*ARGSUSED*/
106 static void
107 sim_close(void *arg)
111 static inj_mode_ops_t simulate_ops = {
112 sim_open,
113 sim_send,
114 sim_close
118 main(int argc, char *argv[])
120 const inj_mode_ops_t *mode = NULL;
121 void *mode_arg = NULL;
122 int c;
124 const char *file;
125 inj_list_t *program;
126 fmd_log_t *lp;
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());
134 mode = &sysevent_ops;
135 mode_arg = optarg;
136 break;
138 case 'n':
139 if (mode != NULL)
140 return (usage());
142 mode = &simulate_ops;
143 break;
145 case 'q':
146 quiet = 1;
147 break;
149 case 'v':
150 verbose = 1;
151 break;
153 default:
154 return (usage());
158 if (mode == NULL)
159 mode = &sysevent_ops;
161 argc -= optind;
162 argv += optind;
164 if (argc == 0)
165 file = "-";
166 else if (argc == 1)
167 file = argv[0];
168 else
169 return (usage());
171 srand48(gethrtime());
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);
178 inj_program_run(program, mode, mode_arg);
179 return (0);