xref: /onnv-gate/usr/src/cmd/fm/fminject/common/inj_main.c (revision 11102:b91faef0c984)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11102SGavin.Maltby@Sun.COM  * Common Development and Distribution License (the "License").
6*11102SGavin.Maltby@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11102SGavin.Maltby@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
270Sstevel@tonic-gate #include <sys/fm/util.h>
280Sstevel@tonic-gate #include <fm/fmd_log.h>
290Sstevel@tonic-gate #include <libsysevent.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <inj.h>
320Sstevel@tonic-gate #include <inj_err.h>
330Sstevel@tonic-gate #include <inj_string.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate int verbose;
360Sstevel@tonic-gate int quiet;
370Sstevel@tonic-gate 
380Sstevel@tonic-gate static int
usage(void)390Sstevel@tonic-gate usage(void)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: %s [-nqv] [-c chan] [file]\n"
420Sstevel@tonic-gate 	    "\t-c  specify alternate channel to use for publication\n"
430Sstevel@tonic-gate 	    "\t-n  compile program but do not inject any events\n"
440Sstevel@tonic-gate 	    "\t-q  enable quiet mode (silence status messages)\n"
450Sstevel@tonic-gate 	    "\t-v  enable verbose output (display event details)\n",
460Sstevel@tonic-gate 	    getpname());
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	return (E_USAGE);
490Sstevel@tonic-gate }
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Sysevent-based event delivery
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate static void *
sev_open(const char * chan)550Sstevel@tonic-gate sev_open(const char *chan)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	evchan_t *hdl;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (chan == NULL)
600Sstevel@tonic-gate 		chan = FM_ERROR_CHAN;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if ((errno = sysevent_evc_bind(chan, &hdl,
630Sstevel@tonic-gate 	    EVCH_CREAT | EVCH_HOLD_PEND)) != 0)
640Sstevel@tonic-gate 		die("can't bind to error channel %s", chan);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	return (hdl);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static void
sev_send(void * arg,nvlist_t * msg)700Sstevel@tonic-gate sev_send(void *arg, nvlist_t *msg)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	if ((errno = sysevent_evc_publish(arg, EC_FM, ESC_FM_ERROR,
730Sstevel@tonic-gate 	    "com.sun", getpname(), msg, EVCH_SLEEP)) != 0)
740Sstevel@tonic-gate 		warn("failed to send event");
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static void
sev_close(void * arg)780Sstevel@tonic-gate sev_close(void *arg)
790Sstevel@tonic-gate {
80*11102SGavin.Maltby@Sun.COM 	(void) sysevent_evc_unbind(arg);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static inj_mode_ops_t sysevent_ops = {
840Sstevel@tonic-gate 	sev_open,
850Sstevel@tonic-gate 	sev_send,
860Sstevel@tonic-gate 	sev_close
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Simulated delivery
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate /*ARGSUSED*/
930Sstevel@tonic-gate static void *
sim_open(const char * arg)940Sstevel@tonic-gate sim_open(const char *arg)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	return ((void *)1);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*ARGSUSED*/
1000Sstevel@tonic-gate static void
sim_send(void * arg,nvlist_t * msg)1010Sstevel@tonic-gate sim_send(void *arg, nvlist_t *msg)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*ARGSUSED*/
1060Sstevel@tonic-gate static void
sim_close(void * arg)1070Sstevel@tonic-gate sim_close(void *arg)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static inj_mode_ops_t simulate_ops = {
1120Sstevel@tonic-gate 	sim_open,
1130Sstevel@tonic-gate 	sim_send,
1140Sstevel@tonic-gate 	sim_close
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate int
main(int argc,char * argv[])1180Sstevel@tonic-gate main(int argc, char *argv[])
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	const inj_mode_ops_t *mode = NULL;
1210Sstevel@tonic-gate 	void *mode_arg = NULL;
1220Sstevel@tonic-gate 	int c;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	const char *file;
1250Sstevel@tonic-gate 	inj_list_t *program;
1260Sstevel@tonic-gate 	fmd_log_t *lp;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "c:nqv")) != EOF) {
1290Sstevel@tonic-gate 		switch (c) {
1300Sstevel@tonic-gate 		case 'c':
1310Sstevel@tonic-gate 			if (mode != NULL || mode_arg != NULL)
1320Sstevel@tonic-gate 				return (usage());
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 			mode = &sysevent_ops;
1350Sstevel@tonic-gate 			mode_arg = optarg;
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		case 'n':
1390Sstevel@tonic-gate 			if (mode != NULL)
1400Sstevel@tonic-gate 				return (usage());
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 			mode = &simulate_ops;
1430Sstevel@tonic-gate 			break;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		case 'q':
1460Sstevel@tonic-gate 			quiet = 1;
1470Sstevel@tonic-gate 			break;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 		case 'v':
1500Sstevel@tonic-gate 			verbose = 1;
1510Sstevel@tonic-gate 			break;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 		default:
1540Sstevel@tonic-gate 			return (usage());
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (mode == NULL)
1590Sstevel@tonic-gate 		mode = &sysevent_ops;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	argc -= optind;
1620Sstevel@tonic-gate 	argv += optind;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (argc == 0)
1650Sstevel@tonic-gate 		file = "-";
1660Sstevel@tonic-gate 	else if (argc == 1)
1670Sstevel@tonic-gate 		file = argv[0];
1680Sstevel@tonic-gate 	else
1690Sstevel@tonic-gate 		return (usage());
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	srand48(gethrtime());
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if (argc > 0 && (lp = fmd_log_open(FMD_LOG_VERSION, file, &c)) != NULL)
1740Sstevel@tonic-gate 		program = inj_logfile_read(lp);
1750Sstevel@tonic-gate 	else
1760Sstevel@tonic-gate 		program = inj_program_read(file);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	inj_program_run(program, mode, mode_arg);
1790Sstevel@tonic-gate 	return (0);
1800Sstevel@tonic-gate }
181