xref: /illumos-gate/usr/src/cmd/fm/fminject/common/inj_main.c (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
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
5*49b225e1SGavin Maltby  * Common Development and Distribution License (the "License").
6*49b225e1SGavin Maltby  * 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*49b225e1SGavin Maltby  * Copyright 2009 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 <sys/sysevent/eventdefs.h>
277c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
287c478bd9Sstevel@tonic-gate #include <fm/fmd_log.h>
297c478bd9Sstevel@tonic-gate #include <libsysevent.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <inj.h>
327c478bd9Sstevel@tonic-gate #include <inj_err.h>
337c478bd9Sstevel@tonic-gate #include <inj_string.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate int verbose;
367c478bd9Sstevel@tonic-gate int quiet;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static int
usage(void)397c478bd9Sstevel@tonic-gate usage(void)
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: %s [-nqv] [-c chan] [file]\n"
427c478bd9Sstevel@tonic-gate 	    "\t-c  specify alternate channel to use for publication\n"
437c478bd9Sstevel@tonic-gate 	    "\t-n  compile program but do not inject any events\n"
447c478bd9Sstevel@tonic-gate 	    "\t-q  enable quiet mode (silence status messages)\n"
457c478bd9Sstevel@tonic-gate 	    "\t-v  enable verbose output (display event details)\n",
467c478bd9Sstevel@tonic-gate 	    getpname());
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	return (E_USAGE);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Sysevent-based event delivery
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate static void *
sev_open(const char * chan)557c478bd9Sstevel@tonic-gate sev_open(const char *chan)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	evchan_t *hdl;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (chan == NULL)
607c478bd9Sstevel@tonic-gate 		chan = FM_ERROR_CHAN;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if ((errno = sysevent_evc_bind(chan, &hdl,
637c478bd9Sstevel@tonic-gate 	    EVCH_CREAT | EVCH_HOLD_PEND)) != 0)
647c478bd9Sstevel@tonic-gate 		die("can't bind to error channel %s", chan);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	return (hdl);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static void
sev_send(void * arg,nvlist_t * msg)707c478bd9Sstevel@tonic-gate sev_send(void *arg, nvlist_t *msg)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	if ((errno = sysevent_evc_publish(arg, EC_FM, ESC_FM_ERROR,
737c478bd9Sstevel@tonic-gate 	    "com.sun", getpname(), msg, EVCH_SLEEP)) != 0)
747c478bd9Sstevel@tonic-gate 		warn("failed to send event");
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void
sev_close(void * arg)787c478bd9Sstevel@tonic-gate sev_close(void *arg)
797c478bd9Sstevel@tonic-gate {
80*49b225e1SGavin Maltby 	(void) sysevent_evc_unbind(arg);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static inj_mode_ops_t sysevent_ops = {
847c478bd9Sstevel@tonic-gate 	sev_open,
857c478bd9Sstevel@tonic-gate 	sev_send,
867c478bd9Sstevel@tonic-gate 	sev_close
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Simulated delivery
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
937c478bd9Sstevel@tonic-gate static void *
sim_open(const char * arg)947c478bd9Sstevel@tonic-gate sim_open(const char *arg)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	return ((void *)1);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1007c478bd9Sstevel@tonic-gate static void
sim_send(void * arg,nvlist_t * msg)1017c478bd9Sstevel@tonic-gate sim_send(void *arg, nvlist_t *msg)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1067c478bd9Sstevel@tonic-gate static void
sim_close(void * arg)1077c478bd9Sstevel@tonic-gate sim_close(void *arg)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static inj_mode_ops_t simulate_ops = {
1127c478bd9Sstevel@tonic-gate 	sim_open,
1137c478bd9Sstevel@tonic-gate 	sim_send,
1147c478bd9Sstevel@tonic-gate 	sim_close
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1187c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	const inj_mode_ops_t *mode = NULL;
1217c478bd9Sstevel@tonic-gate 	void *mode_arg = NULL;
1227c478bd9Sstevel@tonic-gate 	int c;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	const char *file;
1257c478bd9Sstevel@tonic-gate 	inj_list_t *program;
1267c478bd9Sstevel@tonic-gate 	fmd_log_t *lp;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "c:nqv")) != EOF) {
1297c478bd9Sstevel@tonic-gate 		switch (c) {
1307c478bd9Sstevel@tonic-gate 		case 'c':
1317c478bd9Sstevel@tonic-gate 			if (mode != NULL || mode_arg != NULL)
1327c478bd9Sstevel@tonic-gate 				return (usage());
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 			mode = &sysevent_ops;
1357c478bd9Sstevel@tonic-gate 			mode_arg = optarg;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		case 'n':
1397c478bd9Sstevel@tonic-gate 			if (mode != NULL)
1407c478bd9Sstevel@tonic-gate 				return (usage());
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 			mode = &simulate_ops;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		case 'q':
1467c478bd9Sstevel@tonic-gate 			quiet = 1;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		case 'v':
1507c478bd9Sstevel@tonic-gate 			verbose = 1;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		default:
1547c478bd9Sstevel@tonic-gate 			return (usage());
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (mode == NULL)
1597c478bd9Sstevel@tonic-gate 		mode = &sysevent_ops;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	argc -= optind;
1627c478bd9Sstevel@tonic-gate 	argv += optind;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (argc == 0)
1657c478bd9Sstevel@tonic-gate 		file = "-";
1667c478bd9Sstevel@tonic-gate 	else if (argc == 1)
1677c478bd9Sstevel@tonic-gate 		file = argv[0];
1687c478bd9Sstevel@tonic-gate 	else
1697c478bd9Sstevel@tonic-gate 		return (usage());
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	srand48(gethrtime());
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (argc > 0 && (lp = fmd_log_open(FMD_LOG_VERSION, file, &c)) != NULL)
1747c478bd9Sstevel@tonic-gate 		program = inj_logfile_read(lp);
1757c478bd9Sstevel@tonic-gate 	else
1767c478bd9Sstevel@tonic-gate 		program = inj_program_read(file);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	inj_program_run(program, mode, mode_arg);
1797c478bd9Sstevel@tonic-gate 	return (0);
1807c478bd9Sstevel@tonic-gate }
181