xref: /illumos-gate/usr/src/cmd/fm/fminject/common/inj.h (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*7aec1d6eScindi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _INJ_H
287c478bd9Sstevel@tonic-gate #define	_INJ_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * FMA Error injector
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <libnvpair.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <inj_list.h>
397c478bd9Sstevel@tonic-gate #include <inj_hash.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <fm/fmd_log.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifdef __cplusplus
447c478bd9Sstevel@tonic-gate extern "C" {
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
48*7aec1d6eScindi  * The injector allows for the declaration, definition, and injection of four
49*7aec1d6eScindi  * types of things - Events, FMRIs, Authorities, and lists.  The first three
50*7aec1d6eScindi  * are essentially lists with extra membership requirements (FMRIs, for
51*7aec1d6eScindi  * example, must include a member called `scheme').  So while each has a
52*7aec1d6eScindi  * different function within the FMA framework, we can use a single struct to
53*7aec1d6eScindi  * store all three.  The inj_itemtype_t enum is used to describe which of the
54*7aec1d6eScindi  * four types is being represented by a given object.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate typedef enum inj_itemtype {
577c478bd9Sstevel@tonic-gate 	ITEMTYPE_EVENT,
587c478bd9Sstevel@tonic-gate 	ITEMTYPE_FMRI,
59*7aec1d6eScindi 	ITEMTYPE_AUTH,
60*7aec1d6eScindi 	ITEMTYPE_LIST
617c478bd9Sstevel@tonic-gate } inj_itemtype_t;
627c478bd9Sstevel@tonic-gate 
63*7aec1d6eScindi #define	ITEMTYPE_NITEMS		4
64*7aec1d6eScindi 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * The member name-value pairs of Events, FMRIs, and Authorities are typed.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate typedef enum inj_memtype {
697c478bd9Sstevel@tonic-gate 	MEMTYPE_UNKNOWN,
707c478bd9Sstevel@tonic-gate 	MEMTYPE_INT8,
717c478bd9Sstevel@tonic-gate 	MEMTYPE_INT16,
727c478bd9Sstevel@tonic-gate 	MEMTYPE_INT32,
737c478bd9Sstevel@tonic-gate 	MEMTYPE_INT64,
747c478bd9Sstevel@tonic-gate 	MEMTYPE_UINT8,
757c478bd9Sstevel@tonic-gate 	MEMTYPE_UINT16,
767c478bd9Sstevel@tonic-gate 	MEMTYPE_UINT32,
777c478bd9Sstevel@tonic-gate 	MEMTYPE_UINT64,
787c478bd9Sstevel@tonic-gate 	MEMTYPE_BOOL,
797c478bd9Sstevel@tonic-gate 	MEMTYPE_STRING,
807c478bd9Sstevel@tonic-gate 	MEMTYPE_ENUM,
817c478bd9Sstevel@tonic-gate 	MEMTYPE_EVENT,
827c478bd9Sstevel@tonic-gate 	MEMTYPE_FMRI,
83*7aec1d6eScindi 	MEMTYPE_AUTH,
84*7aec1d6eScindi 	MEMTYPE_LIST
857c478bd9Sstevel@tonic-gate } inj_memtype_t;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Declarations
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * Each declared item, be it an event, an fmri, or an authority, consists of
917c478bd9Sstevel@tonic-gate  * an inj_decl_t and a string of inj_declmem_t's, one of the latter for each
927c478bd9Sstevel@tonic-gate  * declared member.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	DECL_F_AUTOENA	0x1	/* ENA member to be auto-generated for event */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate typedef struct inj_decl {
987c478bd9Sstevel@tonic-gate 	inj_list_t decl_members;	/* List of declared members */
997c478bd9Sstevel@tonic-gate 	inj_hash_t decl_memhash;	/* Hash of said members */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	const char *decl_name;		/* Name of declared item */
1027c478bd9Sstevel@tonic-gate 	inj_itemtype_t decl_type;	/* Type of declared item */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	uint_t decl_lineno;		/* Line # of first member declared */
1057c478bd9Sstevel@tonic-gate 	uint_t decl_flags;		/* DECL_F_* */
1067c478bd9Sstevel@tonic-gate } inj_decl_t;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	DECLMEM_F_ARRAY	0x1	/* This member is an array of the given type */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate typedef struct inj_declmem {
1117c478bd9Sstevel@tonic-gate 	inj_list_t dlm_memlist;		/* List of declared members */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	const char *dlm_name;		/* Name of this member */
1147c478bd9Sstevel@tonic-gate 	inj_memtype_t dlm_type;		/* Type of this member */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	uint_t dlm_flags;		/* DECLMEM_F_* */
1177c478bd9Sstevel@tonic-gate 	uint_t dlm_arrdim;		/* If arr flag set, dim of array */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	union {
1207c478bd9Sstevel@tonic-gate 		inj_hash_t *_dlm_enumvals; /* If enum, hash of poss. values */
1217c478bd9Sstevel@tonic-gate 		inj_decl_t *_dlm_decl;	/* If evt, etc., ptr to decl for same */
1227c478bd9Sstevel@tonic-gate 	} _dlm_u;
1237c478bd9Sstevel@tonic-gate } inj_declmem_t;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	dlm_enumvals	_dlm_u._dlm_enumvals
1267c478bd9Sstevel@tonic-gate #define	dlm_decl	_dlm_u._dlm_decl
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Definitions
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * Each defined item consists of an inj_defn_t and a string of inj_defnmem_t's,
1327c478bd9Sstevel@tonic-gate  * one of the latter for each defined member.  The inj_defn_t also contains a
1337c478bd9Sstevel@tonic-gate  * pointer to the corresponding declaration, thus allowing for correctness
1347c478bd9Sstevel@tonic-gate  * checking.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate typedef struct inj_defn {
1387c478bd9Sstevel@tonic-gate 	inj_list_t defn_members;	/* List of defined members */
1397c478bd9Sstevel@tonic-gate 	const char *defn_name;		/* Name of this definition */
1407c478bd9Sstevel@tonic-gate 	inj_decl_t *defn_decl;		/* Ptr to decl this defn instantiates */
1417c478bd9Sstevel@tonic-gate 	uint_t defn_lineno;		/* Line # of first member defined */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	nvlist_t *defn_nvl;		/* Built from validated members */
1447c478bd9Sstevel@tonic-gate } inj_defn_t;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Embodiment of the information that we know about a given defined member at
1487c478bd9Sstevel@tonic-gate  * the time of definition.  These values are assigned before the individual
1497c478bd9Sstevel@tonic-gate  * definition members are paired with their corresponding declarations, so we
1507c478bd9Sstevel@tonic-gate  * don't know whether a given IDENT is, for example, an enum or an fmri
1517c478bd9Sstevel@tonic-gate  * reference.  Without these values, we wouldn't be able to distinguish between
1527c478bd9Sstevel@tonic-gate  * a quoted string and an identifier, for example, and thus would have a harder
1537c478bd9Sstevel@tonic-gate  * time with syntactic validation.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate typedef enum inj_defnmemtype {
1567c478bd9Sstevel@tonic-gate 	DEFNMEM_IMM,
1577c478bd9Sstevel@tonic-gate 	DEFNMEM_IDENT,
1587c478bd9Sstevel@tonic-gate 	DEFNMEM_QSTRING,
1597c478bd9Sstevel@tonic-gate 	DEFNMEM_EVENT,
1607c478bd9Sstevel@tonic-gate 	DEFNMEM_FMRI,
1617c478bd9Sstevel@tonic-gate 	DEFNMEM_AUTH,
1627c478bd9Sstevel@tonic-gate 	DEFNMEM_ARRAY,
163*7aec1d6eScindi 	DEFNMEM_LIST
1647c478bd9Sstevel@tonic-gate } inj_defnmemtype_t;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate typedef struct inj_defnmem {
1677c478bd9Sstevel@tonic-gate 	inj_list_t dfm_memlist;		/* List of defined members */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	inj_defnmemtype_t dfm_type;	/* Type of this member, from parser */
1707c478bd9Sstevel@tonic-gate 	uint_t dfm_lineno;		/* Last line of this member's defn */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	union {
1737c478bd9Sstevel@tonic-gate 		const char *_dfm_str;	/* String value of member */
174*7aec1d6eScindi 		inj_list_t _dfm_list;	/* Enum, evt, auth, arr, list vals */
1757c478bd9Sstevel@tonic-gate 	} _dfm_u;
1767c478bd9Sstevel@tonic-gate } inj_defnmem_t;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #define	dfm_str		_dfm_u._dfm_str
1797c478bd9Sstevel@tonic-gate #define	dfm_list	_dfm_u._dfm_list
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Operations performed by the injector (aside from declarations and
1837c478bd9Sstevel@tonic-gate  * definitions)
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* events and priorities list for the randomize command */
1877c478bd9Sstevel@tonic-gate typedef struct inj_randelem {
1887c478bd9Sstevel@tonic-gate 	struct inj_randelem *re_next;
1897c478bd9Sstevel@tonic-gate 	inj_defn_t *re_event;
1907c478bd9Sstevel@tonic-gate 	uint_t re_prob;
1917c478bd9Sstevel@tonic-gate } inj_randelem_t;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * Operations themselves are structured as a tree of inj_cmd_t's.  Each one has
1957c478bd9Sstevel@tonic-gate  * a command type and type-specific command data.  The "program" is run via
1967c478bd9Sstevel@tonic-gate  * iteration through the tree, with the injector performing the operation
1977c478bd9Sstevel@tonic-gate  * requested by a given node.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate typedef enum inj_cmd_type {
2007c478bd9Sstevel@tonic-gate 	CMD_SEND_EVENT,
2017c478bd9Sstevel@tonic-gate 	CMD_SLEEP,
2027c478bd9Sstevel@tonic-gate 	CMD_REPEAT,
2037c478bd9Sstevel@tonic-gate 	CMD_RANDOM
2047c478bd9Sstevel@tonic-gate } inj_cmd_type_t;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate typedef struct inj_cmd {
2077c478bd9Sstevel@tonic-gate 	inj_list_t cmd_list;		/* List of commands */
2087c478bd9Sstevel@tonic-gate 	inj_cmd_type_t cmd_type;	/* Type of this command */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	union {
2117c478bd9Sstevel@tonic-gate 		inj_defn_t *_cmd_event;	/* If send_event, evt to send */
2127c478bd9Sstevel@tonic-gate 		inj_randelem_t **_cmd_rand;	/* List of evts & probs */
2137c478bd9Sstevel@tonic-gate 		struct inj_cmd *_cmd_subcmd;	/* If repeat, cmd to be rpt'd */
2147c478bd9Sstevel@tonic-gate 	} _cmd_u;
2157c478bd9Sstevel@tonic-gate 	uint_t		cmd_num;	/* If repeat, repeat count */
2167c478bd9Sstevel@tonic-gate } inj_cmd_t;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #define	cmd_event	_cmd_u._cmd_event
2197c478bd9Sstevel@tonic-gate #define	cmd_rand	_cmd_u._cmd_rand
2207c478bd9Sstevel@tonic-gate #define	cmd_subcmd	_cmd_u._cmd_subcmd
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * We support retargetable event-delivery mechanisms.  Each method implements
2247c478bd9Sstevel@tonic-gate  * a copy of the following ops vector, thus allowing us to switch mechanisms
2257c478bd9Sstevel@tonic-gate  * simply by switching the structure.
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate typedef struct inj_mode_ops {
2287c478bd9Sstevel@tonic-gate 	void *(*mo_open)(const char *);		/* Init mechanism */
2297c478bd9Sstevel@tonic-gate 	void (*mo_send)(void *, nvlist_t *);	/* Send a single nvlist */
2307c478bd9Sstevel@tonic-gate 	void (*mo_close)(void *);		/* Shut down mechanism */
2317c478bd9Sstevel@tonic-gate } inj_mode_ops_t;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate extern int verbose;
2347c478bd9Sstevel@tonic-gate extern int quiet;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate extern inj_list_t *inj_logfile_read(fmd_log_t *);
2377c478bd9Sstevel@tonic-gate extern inj_list_t *inj_program_read(const char *);
2387c478bd9Sstevel@tonic-gate extern void inj_program_run(inj_list_t *, const inj_mode_ops_t *, void *);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate extern void *inj_alloc(size_t);
2417c478bd9Sstevel@tonic-gate extern void *inj_zalloc(size_t);
2427c478bd9Sstevel@tonic-gate extern void inj_free(void *, size_t);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #ifdef __cplusplus
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #endif /* _INJ_H */
249