#include "../common/sw_impl.h"
#define ID2IDX(id) ((int)((id) & 0xff0000) >> 16)
#define IDX2ID(i) ((id_t)((i) << 16) | 0x1d000000)
#define SUBIDVALID(msinfo, id) (((int)(id) & 0xff00ffff) == 0x1d000000 && \
ID2IDX(id) < (msinfo)->swms_dispcnt)
static struct {
fmd_stat_t sw_recv_total;
fmd_stat_t sw_recv_match;
fmd_stat_t sw_recv_callback;
} sw_stats = {
{ "sw_recv_total", FMD_TYPE_UINT64,
"total events received" },
{ "sw_recv_match", FMD_TYPE_UINT64,
"events matching some subsidiary" },
{ "sw_recv_callback", FMD_TYPE_UINT64,
"callbacks to all subsidiaries" },
};
#define BUMPSTAT(stat) sw_stats.stat.fmds_value.ui64++
#define BUMPSTATN(stat, n) sw_stats.stat.fmds_value.ui64 += (n)
void
sw_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
{
struct sw_modspecific *msinfo;
int calls = 0;
int mod;
BUMPSTAT(sw_recv_total);
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
for (mod = 0; mod < msinfo->swms_dispcnt; mod++) {
const struct sw_disp *dp;
sw_dispfunc_t *dispf = NULL;
for (dp = (*msinfo->swms_disptbl)[mod];
dp != NULL && dp->swd_classpat != NULL; dp++) {
if (fmd_nvl_class_match(hdl, nvl, dp->swd_classpat)) {
dispf = dp->swd_func;
break;
}
}
if (dispf != NULL) {
calls++;
(*dispf)(hdl, ep, nvl, class, dp->swd_arg);
}
}
BUMPSTAT(sw_recv_match);
if (calls)
BUMPSTATN(sw_recv_callback, calls);
}
id_t
sw_timer_install(fmd_hdl_t *hdl, id_t who, void *arg, fmd_event_t *ep,
hrtime_t hrt)
{
struct sw_modspecific *msinfo;
const struct sw_subinfo **subinfo;
const struct sw_subinfo *sip;
int slot, chosen = -1;
id_t timerid;
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
if (!SUBIDVALID(msinfo, who))
fmd_hdl_abort(hdl, "sw_timer_install: invalid subid %d\n", who);
subinfo = *msinfo->swms_subinfo;
sip = subinfo[ID2IDX(who)];
if (sip-> swsub_timeout == NULL)
fmd_hdl_abort(hdl, "sw_timer_install: no swsub_timeout\n");
(void) pthread_mutex_lock(&msinfo->swms_timerlock);
for (slot = 0; slot < SW_TIMER_MAX; slot++) {
if (msinfo->swms_timers[slot].swt_state != SW_TMR_INUSE) {
chosen = slot;
break;
}
}
if (chosen == -1)
fmd_hdl_abort(hdl, "timer slots exhausted\n");
msinfo->swms_timers[chosen].swt_state = SW_TMR_INUSE;
msinfo->swms_timers[chosen].swt_ownerid = who;
msinfo->swms_timers[chosen].swt_timerid = timerid =
fmd_timer_install(hdl, arg, ep, hrt);
(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
return (timerid);
}
static int
subtimer_find(struct sw_modspecific *msinfo, id_t who, id_t timerid)
{
int slot;
for (slot = 0; slot < SW_TIMER_MAX; slot++) {
if (msinfo->swms_timers[slot].swt_state == SW_TMR_INUSE &&
(who == -1 ||
msinfo->swms_timers[slot].swt_ownerid == who) &&
msinfo->swms_timers[slot].swt_timerid == timerid)
return (slot);
}
return (-1);
}
void
sw_timer_remove(fmd_hdl_t *hdl, id_t who, id_t timerid)
{
struct sw_modspecific *msinfo;
const struct sw_subinfo **subinfo;
const struct sw_subinfo *sip;
int slot;
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
if (!SUBIDVALID(msinfo, who))
fmd_hdl_abort(hdl, "sw_timer_remove: invalid subid\n");
subinfo = *msinfo->swms_subinfo;
sip = subinfo[ID2IDX(who)];
(void) pthread_mutex_lock(&msinfo->swms_timerlock);
if ((slot = subtimer_find(msinfo, who, timerid)) == -1)
fmd_hdl_abort(hdl, "sw_timer_remove: timerid %d not found "
"for %s\n", timerid, sip->swsub_name);
fmd_timer_remove(hdl, timerid);
msinfo->swms_timers[slot].swt_state = SW_TMR_RMVD;
(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
}
void
sw_timeout(fmd_hdl_t *hdl, id_t timerid, void *arg)
{
struct sw_modspecific *msinfo;
const struct sw_subinfo **subinfo;
const struct sw_subinfo *sip;
id_t owner;
int slot;
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
(void) pthread_mutex_lock(&msinfo->swms_timerlock);
if ((slot = subtimer_find(msinfo, -1, timerid)) == -1)
fmd_hdl_abort(hdl, "sw_timeout: timerid %d not found\n");
(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
owner = msinfo->swms_timers[slot].swt_ownerid;
if (!SUBIDVALID(msinfo, owner))
fmd_hdl_abort(hdl, "sw_timeout: invalid subid\n");
subinfo = *msinfo->swms_subinfo;
sip = subinfo[ID2IDX(owner)];
sip->swsub_timeout(hdl, timerid, arg);
}
enum sw_casetype
sw_id_to_casetype(fmd_hdl_t *hdl, id_t who)
{
struct sw_modspecific *msinfo;
const struct sw_subinfo **subinfo;
const struct sw_subinfo *sip;
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
if (!SUBIDVALID(msinfo, who))
fmd_hdl_abort(hdl, "sw_id_to_casetype: invalid subid %d\n",
who);
subinfo = *msinfo->swms_subinfo;
sip = subinfo[ID2IDX(who)];
if ((sip->swsub_casetype & SW_CASE_NONE) != SW_CASE_NONE)
fmd_hdl_abort(hdl, "sw_id_to_casetype: bad case type %d "
"for %s\n", sip->swsub_casetype, sip->swsub_name);
return (sip->swsub_casetype);
}
static const struct sw_subinfo *
sw_subinfo_bycase(fmd_hdl_t *hdl, enum sw_casetype type)
{
struct sw_modspecific *msinfo;
const struct sw_subinfo **subinfo;
const struct sw_subinfo *sip;
int i;
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
subinfo = *msinfo->swms_subinfo;
for (i = 0; i < SW_SUB_MAX; i++) {
sip = subinfo[i];
if (sip->swsub_casetype == type)
return (sip);
}
return (NULL);
}
swsub_case_close_func_t *
sw_sub_case_close_func(fmd_hdl_t *hdl, enum sw_casetype type)
{
const struct sw_subinfo *sip;
if ((sip = sw_subinfo_bycase(hdl, type)) == NULL)
fmd_hdl_abort(hdl, "sw_sub_case_close_func: case type "
"%d not found\n", type);
return (sip->swsub_case_close);
}
sw_case_vrfy_func_t *
sw_sub_case_vrfy_func(fmd_hdl_t *hdl, enum sw_casetype type)
{
const struct sw_subinfo *sip;
if ((sip = sw_subinfo_bycase(hdl, type)) == NULL)
fmd_hdl_abort(hdl, "sw_sub_case_vrfy_func: case type "
"%d not found\n", type);
return (sip->swsub_case_verify);
}
static void
sw_add_callbacks(fmd_hdl_t *hdl, const char *who,
const struct sw_disp *dp, int nelem, struct sw_modspecific *msinfo)
{
int i;
(*msinfo->swms_disptbl)[msinfo->swms_dispcnt++] = dp;
if (dp == NULL)
return;
if (dp[nelem - 1].swd_classpat != NULL ||
dp[nelem - 1].swd_func != NULL || dp[nelem - 1].swd_arg != NULL)
fmd_hdl_abort(hdl, "subsidiary %s dispatch table not NULL-"
"terminated\n", who);
for (i = 0; i < nelem - 1; i++) {
if (dp[i].swd_classpat == NULL)
fmd_hdl_abort(hdl, "subsidiary %s dispatch table entry "
"%d has a NULL pattern or function\n", who, i);
}
}
int
sw_fmd_init(fmd_hdl_t *hdl, const fmd_hdl_info_t *hdlinfo,
const struct sw_subinfo *(*subsid)[SW_SUB_MAX])
{
struct sw_modspecific *msinfo;
int i;
if (fmd_hdl_register(hdl, FMD_API_VERSION, hdlinfo) != 0)
return (0);
if (fmd_prop_get_int32(hdl, "enable") != B_TRUE) {
fmd_hdl_debug(hdl, "%s disabled though .conf file setting\n",
hdlinfo->fmdi_desc);
fmd_hdl_unregister(hdl);
return (0);
}
msinfo = fmd_hdl_zalloc(hdl, sizeof (*msinfo), FMD_SLEEP);
msinfo->swms_subinfo = subsid;
msinfo->swms_disptbl = fmd_hdl_zalloc(hdl,
SW_SUB_MAX * sizeof (struct sw_disp *), FMD_SLEEP);
(void) pthread_mutex_init(&msinfo->swms_timerlock, NULL);
for (i = 0; i < SW_TIMER_MAX; i++)
msinfo->swms_timers[i].swt_state = SW_TMR_UNTOUCHED;
fmd_hdl_setspecific(hdl, (void *)msinfo);
(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (sw_stats) /
sizeof (fmd_stat_t), (fmd_stat_t *)&sw_stats);
for (i = 0; i < SW_SUB_MAX; i++) {
const struct sw_subinfo *sip = (*subsid)[i];
const struct sw_disp *dp;
char dbgbuf[80];
int nelem = -1;
int initrslt;
if (!sip || sip->swsub_name == NULL)
break;
initrslt = (*sip->swsub_init)(hdl, IDX2ID(i), &dp, &nelem);
(void) snprintf(dbgbuf, sizeof (dbgbuf),
"subsidiary %d (id 0x%lx) '%s'",
i, IDX2ID(i), sip->swsub_name);
switch (initrslt) {
case SW_SUB_INIT_SUCCESS:
if (dp == NULL || nelem < 1)
fmd_hdl_abort(hdl, "%s returned dispatch "
"table 0x%p and nelem %d\n",
dbgbuf, dp, nelem);
fmd_hdl_debug(hdl, "%s initialized\n", dbgbuf);
sw_add_callbacks(hdl, sip->swsub_name, dp, nelem,
msinfo);
break;
case SW_SUB_INIT_FAIL_VOLUNTARY:
fmd_hdl_debug(hdl, "%s chose not to initialize\n",
dbgbuf);
sw_add_callbacks(hdl, sip->swsub_name, NULL, -1,
msinfo);
break;
case SW_SUB_INIT_FAIL_ERROR:
fmd_hdl_debug(hdl, "%s failed to initialize "
"because of an error\n", dbgbuf);
sw_add_callbacks(hdl, sip->swsub_name, NULL, -1,
msinfo);
break;
default:
fmd_hdl_abort(hdl, "%s returned out-of-range result "
"%d\n", dbgbuf, initrslt);
break;
}
}
return (1);
}
void
sw_fmd_fini(fmd_hdl_t *hdl)
{
const struct sw_subinfo **subinfo;
struct sw_modspecific *msinfo;
int i;
msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
subinfo = *msinfo->swms_subinfo;
(void) pthread_mutex_lock(&msinfo->swms_timerlock);
for (i = 0; i < SW_TIMER_MAX; i++) {
if (msinfo->swms_timers[i].swt_state != SW_TMR_INUSE)
continue;
fmd_timer_remove(hdl, msinfo->swms_timers[i].swt_timerid);
msinfo->swms_timers[i].swt_state = SW_TMR_RMVD;
}
(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
(void) pthread_mutex_destroy(&msinfo->swms_timerlock);
for (i = 0; i < msinfo->swms_dispcnt; i++) {
const struct sw_subinfo *sip = subinfo[i];
if ((*msinfo->swms_disptbl)[i] == NULL)
continue;
if (sip->swsub_fini != NULL)
(*sip->swsub_fini)(hdl);
}
fmd_hdl_free(hdl, msinfo->swms_disptbl,
SW_SUB_MAX * sizeof (struct sw_disp *));
fmd_hdl_setspecific(hdl, NULL);
fmd_hdl_free(hdl, msinfo, sizeof (*msinfo));
}