summaryrefslogtreecommitdiff
path: root/usr/src/man/man3sysevent/sysevent_subscribe_event.3sysevent
blob: 0120ad29c173a4c6d4def6ff6164cb639e9ac83b (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'\" te
.\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
.\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
.\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH SYSEVENT_SUBSCRIBE_EVENT 3SYSEVENT "Jul 24, 2009"
.SH NAME
sysevent_subscribe_event, sysevent_unsubscribe_event \- register or unregister
interest in event receipt
.SH SYNOPSIS
.LP
.nf
\fBcc\fR  [ \fIflag\fR\&.\|.\|. ] \fIfile\fR\&.\|.\|. \fB-lsysevent\fR [ \fIlibrary\fR\&.\|.\|. ]
#include <libsysevent.h>

\fBint\fR \fBsysevent_subscribe_event\fR(\fBsysevent_handle_t *\fR\fIsysevent_hdl\fR,
     \fBchar *\fR\fIevent_class\fR, \fBchar **\fR\fIevent_subclass_list\fR,
     \fBint\fR \fInum_subclasses\fR);
.fi

.LP
.nf
\fBvoid\fR \fBsysevent_unsubscribe_event\fR(\fBsysevent_handle_t *\fR\fIsysevent_hdl\fR,
     \fBchar *\fR\fIevent_class\fR);
.fi

.SH PARAMETERS
.sp
.ne 2
.na
\fB\fIevent_class\fR\fR
.ad
.RS 23n
system event class string
.RE

.sp
.ne 2
.na
\fB\fIevent_subclass_list\fR\fR
.ad
.RS 23n
array of subclass strings
.RE

.sp
.ne 2
.na
\fB\fInum_subclasses\fR\fR
.ad
.RS 23n
number of subclass strings
.RE

.sp
.ne 2
.na
\fB\fIsysevent_hdl\fR\fR
.ad
.RS 23n
\fBsysevent\fR subscriber handle
.RE

.SH DESCRIPTION
.sp
.LP
The \fBsysevent_subscribe_event()\fR function registers the caller's interest
in event notifications belonging to the class \fIevent_class\fR and the
subclasses contained in \fIevent_subclass_list\fR.  The subscriber handle
\fIsysevent_hdl\fR is updated with the new subscription and the calling process
receives event notifications from the event handler specified in
\fIsysevent_bind_handle\fR.
.sp
.LP
System events matching \fIevent_class\fR and a subclass contained in
\fIevent_subclass_list\fR published after the caller returns from
\fBsysevent_subscribe_event()\fR are guaranteed to be delivered to the calling
process.  Matching system events published and queued prior to a call to
\fBsysevent_subscribe_event()\fR may be delivered to the process's event
handler.
.sp
.LP
The \fInum_subclasses\fR argument provides the number of subclass string
elements in \fIevent_subclass_list\fR.
.sp
.LP
A caller can use the event class \fBEC_ALL\fR to subscribe to all event classes
and subclasses. The event class \fBEC_SUB_ALL\fR can be used to subscribe to
all subclasses within a given event class.
.sp
.LP
Subsequent calls to \fBsysevent_subscribe_event()\fR are allowed to add
additional classes or subclasses.  To remove an existing subscription,
\fBsysevent_unsubscribe_event()\fR must be used to remove the subscription.
.sp
.LP
The \fBsysevent_unsubscribe_event()\fR function removes the subscription
described by \fIevent_class\fR for \fIsysevent_hdl\fR.  Event notifications
matching event_class will not be delivered to the calling process upon return.
.sp
.LP
A caller can use the event class \fBEC_ALL\fR to remove all subscriptions for
\fIsysevent_hdl\fR.
.sp
.LP
The library manages all subscription resources.
.SH RETURN VALUES
.sp
.LP
The \fBsysevent_subscribe_event()\fR function returns 0 if the subscription is
successful. Otherwise, \(mi1 is returned and \fBerrno\fR is set to indicate the
error.
.sp
.LP
The \fBsysevent_unsubscribe_event()\fR function returns no value.
.SH ERRORS
.sp
.LP
The \fBsysevent_subscribe_event()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 10n
The calling process has an ID other than the privileged user.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fIsysevent_hdl\fR argument is an invalid \fBsysevent\fR handle.
.RE

.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 10n
There is insufficient memory available to allocate subscription resources.
.RE

.SH EXAMPLES
.LP
\fBExample 1 \fRSubscribing for environmental events
.sp
.in +2
.nf
#include <libsysevent.h>
#include <sys/nvpair.h>

static int32_t attr_int32;

#define    CLASS1        "class1"
#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;

        /* Event Processing */
    } else {
        /* Event Processing */
    }

}


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)
        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);
        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);
        exit(1);
    }

    for (;;) {
        (void) pause();
    }
}
.fi
.in -2

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
.BR sysevent_bind_handle (3SYSEVENT),
.BR sysevent_get_attr_list (3SYSEVENT),
.BR sysevent_get_class_name (3SYSEVENT),
.BR sysevent_get_vendor_name (3SYSEVENT),
.BR attributes (7),
.BR syseventd (8)
.SH NOTES
.sp
.LP
The \fBlibsysevent\fR interfaces do not work at all in non-global zones.