- App:
#include <libsysevent.h>
#include <sys/nvpair.h>
static int32_t attr_int32;
#define CLASS1 EC_ALL
#define CLASS2 "class2"
#define SUBCLASS_1 "subclass_1"
#define SUBCLASS_2 "subclass_2"
#define SUBCLASS_3 "subclass_3"
#define MAX_SUBCLASS 3
static void
event_handler(sysevent_t *ev)
{
nvlist_t *nvlist;
/*
* Special processing for events (CLASS1, SUBCLASS_1) and
* (CLASS2, SUBCLASS_3)
*/
/*
if ((strcmp(CLASS1, sysevent_get_class_name(ev)) == 0 &&
strcmp(SUBCLASS_1, sysevent_get_subclass_name(ev)) == 0) ||
(strcmp(CLASS2, sysevent_get_subclass_name(ev) == 0) &&
strcmp(SUBCLASS_3, sysevent_get_subclass(ev)) == 0)) {
if (sysevent_get_attr_list(ev, &nvlist) != 0)
return;
if (nvlist_lookup_int32(nvlist, "my_int32_attr", &attr_int32)
!= 0)
return;
} else {
}
*/
printf("class name=%s, subclass name=%s\n",sysevent_get_class_name(ev),sysevent_get_subclass_name(ev));
}
int
main(int argc, char **argv)
{
sysevent_handle_t *shp;
const char *subclass_list[MAX_SUBCLASS];
/* Bind event handler and create subscriber handle */
shp = sysevent_bind_handle(event_handler);
if (shp == NULL)
{
printf("bind handle fail\n");
exit(1);
}
/* Subscribe to all CLASS1 event notifications */
subclass_list[0] = EC_SUB_ALL;
if (sysevent_subscribe_event(shp, CLASS1, subclass_list, 1) != 0) {
sysevent_unbind_handle(shp);
printf("subscribe event fail\n");
exit(1);
}
/* Subscribe to CLASS2 events for subclasses: SUBCLASS_1,
* SUBCLASS_2 and SUBCLASS_3
*/
subclass_list[0] = SUBCLASS_1;
subclass_list[1] = SUBCLASS_2;
subclass_list[2] = SUBCLASS_3;
if (sysevent_subscribe_event(shp, CLASS2, subclass_list,
MAX_SUBCLASS) != 0) {
sysevent_unbind_handle(shp);
printf("subscribe event fail 1\n");
exit(1);
}
for (;;) {
(void) pause();
}
}
编译程序:
cc -o event event.c -lsysevent
./event
- 在驱动程序中:
if (ddi_log_sysevent(lsp ->dip, DDI_VENDOR_SUNW, "eric", "eric",
NULL, NULL, DDI_SLEEP) != DDI_SUCCESS) {
cmn_err(CE_WARN, "error logging system event\n");
}
在驱动中执行ddi_log_sysevent时,app可以接收到驱动发的消息,带参数的方式参考http://docs.oracle.com/cd/E19082-01/819-2256/ddi-log-sysevent-9f/index.html,参数需要进一步实验。