Watching system events

System events comprise a mechanism for kernel code to signal up the tree to anyone who might be listening: other kernel agents, userland code, etc. Here's a very stupid demo program (Solaris-10-or-later only), just to save you some typing, that demonstrates what an event looks like. Try running this and then plugging/unplugging a USB device.

See libsysevent(3LIB) for a description of the userland interface used in this demo program, and syseventd(1M) for a description of the userland daemon.

If this little sample piques your interest, check out what you could do with syseventadm(1m) and some shell scripts, perhaps involving zenity(1)...

Compile with cc evprint.c -o evprint -lsysevent -lnvpair

#include <unistd.h>
#include <libsysevent.h>
#include <libnvpair.h>

typedef void (sehfn)(sysevent_t \*ev);

sehfn handler;

int
main(int argc, char \*\*argv)
{
        sysevent_handle_t \*seh;
        const char \*subclass_list[] = {"EC_SUB_ALL"};

        seh = sysevent_bind_handle(handler);
        if (seh == NULL) {
                perror("sysevent_bind_handle");
                exit(1);
        }

        if (sysevent_subscribe_event(seh, EC_ALL, subclass_list, 1) != 0) {
                perror("sysevent_subscribe_event");
                exit(1);
        }
        while (1)
                pause();
}
void
handler(sysevent_t \*ev)
{
        nvlist_t \*nvlist;
        nvpair_t \*nvpp;
        char \*class, \*subclass;
        unsigned int n, i;

        boolean_t bv, \*ba;
        int8_t i8v, \*i8a;
        int16_t i16v, \*i16a;
        int32_t i32v, \*i32a;
        int64_t i64v, \*i64a;
        uint8_t ui8v, \*ui8a;
        uint16_t ui16v, \*ui16a;
        uint32_t ui32v, \*ui32a;
        uint64_t ui64v, \*ui64a;
        char \*str, \*\*sa;

        str = (char \*)malloc(100);

        class = sysevent_get_class_name(ev);
        subclass = sysevent_get_subclass_name(ev);
        printf("\\n\*\*\* event: class '%s', subclass '%s'\\n", class, subclass);
        if (sysevent_get_attr_list(ev, &nvlist) != 0) {
                printf("no nvlist\\n");
                return;
        }
        nvpp = NULL;
        while ((nvpp = nvlist_next_nvpair(nvlist, nvpp)) != NULL) {
                printf("%s: ", nvpair_name(nvpp));
                switch (nvpair_type(nvpp)) {
                case DATA_TYPE_BOOLEAN:
                        printf("true\\n");
                        break;

                case DATA_TYPE_BOOLEAN_VALUE:
                        nvpair_value_boolean_value(nvpp, &bv);
                        printf("boolean %s\\n", bv ? "false" : "true");
                        break;

                case DATA_TYPE_INT8:
                        nvpair_value_int8(nvpp, &i8v);
                        printf("int8 %d\\n", i8v);
                        break;

                case DATA_TYPE_BYTE:
                case DATA_TYPE_UINT8:
                        nvpair_value_uint8(nvpp, &ui8v);
                        printf("uint8 %d\\n", ui8v);
                        break;

                case DATA_TYPE_INT16:
                        nvpair_value_int16(nvpp, &i16v);
                        printf("int16 %d\\n", i16v);
                        break;

                case DATA_TYPE_UINT16:
                        nvpair_value_uint16(nvpp, &ui16v);
                        printf("uint16 %d\\n", ui16v);
                        break;

                case DATA_TYPE_INT32:
                        nvpair_value_int32(nvpp, &i32v);
                        printf("int32 %d\\n", i32v);
                        break;

                case DATA_TYPE_UINT32:
                        nvpair_value_uint32(nvpp, &ui32v);
                        printf("uint32 %d\\n", ui32v);
                        break;

                case DATA_TYPE_INT64:
                        nvpair_value_int64(nvpp, &i64v);
                        printf("int64 %d\\n", i64v);
                        break;

                case DATA_TYPE_UINT64:
                        nvpair_value_uint64(nvpp, &ui64v);
                        printf("uint64 %d\\n", ui64v);
                        break;

                case DATA_TYPE_STRING:
                        nvpair_value_string(nvpp, &str);
                        printf("string '%s'\\n", str);
                        break;

                case DATA_TYPE_NVLIST:
                        printf("nvlist\\n");
                        break;


                case DATA_TYPE_BOOLEAN_ARRAY:
                        printf("boolean array: {");
                        nvpair_value_boolean_array(nvpp, &ba, &n);
                        for (i = 0; i < n; i++)
                                printf("%s ", ba[i] ? "true" : "false");
                        printf("\\n");
                        break;

                case DATA_TYPE_BYTE_ARRAY:
                        printf("byte array: {");
                        nvpair_value_byte_array(nvpp, &ui8a, &n);
                        for (i = 0; i < n; i++)
                                printf("0x%x ", ui8a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_INT8_ARRAY:
                        printf("int8 array: {");
                        nvpair_value_int8_array(nvpp, &i8a, &n);
                        for (i = 0; i < n; i++)
                                printf("%d ", i8a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_UINT8_ARRAY:
                        printf("uint8 array: {");
                        nvpair_value_uint8_array(nvpp, &ui8a, &n);
                        for (i = 0; i < n; i++)
                                printf("0x%x ", ui8a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_INT16_ARRAY:
                        printf("int16 array: {");
                        nvpair_value_int16_array(nvpp, &i16a, &n);
                        for (i = 0; i < n; i++)
                                printf("%d ", i16a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_UINT16_ARRAY:
                        printf("uint16 array: {");
                        nvpair_value_uint16_array(nvpp, &ui16a, &n);
                        for (i = 0; i < n; i++)
                                printf("%d ", ui16a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_INT32_ARRAY:
                        printf("int32 array: {");
                        nvpair_value_int32_array(nvpp, &i32a, &n);
                        for (i = 0; i < n; i++) 
                                printf("%d, ", i32a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_UINT32_ARRAY:
                        printf("uint32 array: {");
                        nvpair_value_uint32_array(nvpp, &ui32a, &n);
                        for (i = 0; i < n; i++) 
                                printf("%d, ", ui32a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_INT64_ARRAY:
                        printf("int64 array: {");
                        nvpair_value_int64_array(nvpp, &iamp;64a, &n);
                        for (i = 0; i < n; i++) 
                                printf("%lld, ", i64a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_UINT64_ARRAY:
                        printf("uint64 array: {");
                        nvpair_value_uint64_array(nvpp, &ui64a, &n);
                        for (i = 0; i < n; i++) 
                                printf("%lld, ", ui64a[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_STRING_ARRAY:
                        printf("string array: {");
                        nvpair_value_string_array(nvpp, &sa, &n);
                        for (i = 0; i < n; i++) 
                                printf("'%s', ", sa[i]);
                        printf("}\\n");
                        break;

                case DATA_TYPE_NVLIST_ARRAY:
                        printf("nvlist_array\\n");
                        break;

                default:
                        printf("type unknown\\n");
                        break;

                }
        }
}
Comments:

If you don't mind me pointing it out, but you havea typo causing your href tag to remain open. syseventd(1M)<A> so you just need to change that <a> to

Posted by tony : frosty on March 31, 2005 at 03:40 PM PST #

Thanks; I wondered why it was doing that odd thing with the
 text...

                

Posted by Dan Mick on April 01, 2005 at 03:32 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

user12614486

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today