summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnwam/common/libnwam_events.c
blob: 3bb6adf4a4a931b742c73c4d6345df7f8de654b0 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * CDDL HEADER START
 *
 * 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]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <syslog.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "libnwam_impl.h"
#include <libnwam_priv.h>
#include <libnwam.h>

/*
 * Implementation of event notification mechanism used by the GUI and
 * nwamadm.  Clients register for events via nwam_events_init() and
 * unregister via nwam_events_fini().  nwamd sends events via nwam_event_send()
 * and applications block waiting for a new event to be delivered in
 * nwam_event_wait().  Events are implemented as System V message queues,
 * one per event client.  The event mechanism has to be resilient to
 * nwamd restarts so that clients do not lose the event connection.
 */

#define	NWAM_EVENT_MSG_DIR		"/etc/svc/volatile/nwam/"
#define	NWAM_EVENT_MSG_FILE		"nwam_event_msgs"
#define	NWAM_EVENT_MSG_FILE_PREFIX	NWAM_EVENT_MSG_DIR NWAM_EVENT_MSG_FILE
#define	NWAM_EVENT_MAX_SIZE		(sizeof (struct nwam_event) + \
	(NWAMD_MAX_NUM_WLANS * sizeof (nwam_wlan_t)))
#define	NWAM_EVENT_WAIT_TIME		10
#define	NWAM_EVENT_MAX_NUM_PENDING	25

/*
 * This is protecting simultaneous access to the msqid and its configuration.
 */
static pthread_mutex_t event_mutex = PTHREAD_MUTEX_INITIALIZER;
static int event_msqid = -1;

static nwam_error_t
nwam_event_alloc(nwam_event_t *eventp)
{
	assert(eventp != NULL);

	*eventp = calloc(1, NWAM_EVENT_MAX_SIZE);
	if (*eventp == NULL)
		return (NWAM_NO_MEMORY);
	return (NWAM_SUCCESS);
}

void
nwam_event_free(nwam_event_t event)
{
	if (event != NULL)
		free(event);
}

/*
 * Get next event in queue.
 */
nwam_error_t
nwam_event_wait(nwam_event_t *eventp)
{
	nwam_error_t err;
	nwam_event_t event;

	assert(eventp != NULL);

	if ((err = nwam_event_alloc(&event)) != NWAM_SUCCESS)
		return (err);
	while (msgrcv(event_msqid, (struct msgbuf *)event, NWAM_EVENT_MAX_SIZE,
	    0, 0) == -1) {
		switch (errno) {
			case EAGAIN:
			case EBUSY:
				/*
				 * We see this errno eventhough it isn't
				 * documented.  Try again.  If this causes
				 * a busy loop then grab a trace otherwise
				 * it's a brace 'til we can figure out why it
				 * happens.
				 */
				continue;

			default:
				nwam_event_free(event);
				return (nwam_errno_to_nwam_error(errno));
		}
	}

	/* Resize event down from maximum size */
	if ((*eventp = realloc(event, event->nwe_size)) == NULL)
		return (NWAM_NO_MEMORY);

	return (NWAM_SUCCESS);
}

/*
 * Register for receipt of events from nwamd.  Event delivery is
 * done via a System V message queue.
 */
nwam_error_t
nwam_events_init(void)
{
	char eventmsgfile[MAXPATHLEN];
	nwam_error_t err;
	nwam_error_t rc = NWAM_SUCCESS;
	key_t key;

	(void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s.%d",
	    NWAM_EVENT_MSG_FILE_PREFIX, getpid());

	(void) pthread_mutex_lock(&event_mutex);

	if (event_msqid != -1) {
		rc = NWAM_ENTITY_IN_USE;
		goto exit;
	}

	if ((err = nwam_request_register_unregister
	    (NWAM_REQUEST_TYPE_EVENT_REGISTER, eventmsgfile)) != NWAM_SUCCESS) {
		rc = err;
		goto exit;
	}

	if ((key = ftok(eventmsgfile, 0)) == -1) {
		rc = nwam_errno_to_nwam_error(errno);
		goto exit;
	}

	/* Get system-wide message queue ID */
	if ((event_msqid = msgget(key, 0444)) == -1) {
		rc = nwam_errno_to_nwam_error(errno);
		goto exit;
	}

exit:
	(void) pthread_mutex_unlock(&event_mutex);

	return (rc);
}

/*
 * Un-register for receipt of events from nwamd.  Make a request to nwamd
 * to destroy the message queue.
 */
void
nwam_events_fini(void)
{
	char eventmsgfile[MAXPATHLEN];

	(void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s.%d",
	    NWAM_EVENT_MSG_FILE_PREFIX, getpid());

	(void) pthread_mutex_lock(&event_mutex);

	(void) nwam_request_register_unregister
	    (NWAM_REQUEST_TYPE_EVENT_UNREGISTER, eventmsgfile);

	event_msqid = -1;

	(void) pthread_mutex_unlock(&event_mutex);
}

/*
 * Create an event queue.  Called by nwamd to create System V message queues
 * for clients to listen for events.
 */
nwam_error_t
nwam_event_queue_init(const char *eventmsgfile)
{
	int fd;
	key_t key;

	if ((fd = open(eventmsgfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
		return (nwam_errno_to_nwam_error(errno));
	(void) close(fd);

	if ((key = ftok(eventmsgfile, 0)) == -1)
		return (nwam_errno_to_nwam_error(errno));

	if (msgget(key, 0644 | IPC_CREAT) == -1)
		return (nwam_errno_to_nwam_error(errno));

	return (NWAM_SUCCESS);
}

/*
 * Send event to registered listeners via the set of registered System V
 * message queues.
 */
nwam_error_t
nwam_event_send(nwam_event_t event)
{
	DIR *dirp;
	struct dirent *dp;
	struct msqid_ds buf;
	key_t key;
	int msqid;
	char eventmsgfile[MAXPATHLEN];
	nwam_error_t err = NWAM_SUCCESS;

	if ((dirp = opendir(NWAM_EVENT_MSG_DIR)) == NULL) {
		return (nwam_errno_to_nwam_error(errno));
	}

	/*
	 * For each file matching our event message queue file prefix,
	 * check the queue is still being read, and if so send the message.
	 */
	while ((dp = readdir(dirp)) != NULL) {
		if (strncmp(dp->d_name, NWAM_EVENT_MSG_FILE,
		    strlen(NWAM_EVENT_MSG_FILE)) != 0)
			continue;

		(void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s/%s",
		    NWAM_EVENT_MSG_DIR, dp->d_name);

		if ((key = ftok(eventmsgfile, 0)) == -1) {
			int errno_save = errno;
			syslog(LOG_INFO, "nwam_event_send: ftok: %s",
			    strerror(errno_save));
			err = nwam_errno_to_nwam_error(errno_save);
			continue;
		}

		if ((msqid = msgget(key, 0644)) == -1) {
			int errno_save = errno;
			syslog(LOG_INFO, "nwam_event_send: msgget: %s",
			    strerror(errno_save));
			err = nwam_errno_to_nwam_error(errno_save);
			continue;
		}

		/* Retrieve stats to analyse queue activity */
		if (msgctl(msqid, IPC_STAT, &buf) == -1) {
			int errno_save = errno;
			syslog(LOG_INFO, "nwam_event_send: msgctl: %s",
			    strerror(errno_save));
			err = nwam_errno_to_nwam_error(errno_save);
			continue;
		}
		/*
		 * If buf.msg_qnum > NWAM_EVENT_MAX_NUM_PENDING
		 * _and_ msg_stime is more than 10s after msg_rtime -
		 * indicating message(s) have been hanging around unclaimed -
		 * we destroy the queue as the client has most likely gone
		 * away. This can happen if a registered client hits Ctrl^C.
		 */
		if (buf.msg_qnum > NWAM_EVENT_MAX_NUM_PENDING &&
		    ((buf.msg_stime + NWAM_EVENT_WAIT_TIME) > buf.msg_rtime)) {
			nwam_event_queue_fini(eventmsgfile);
			continue;
		}

		/*
		 * This shouldn't ever block.  If it does then log an error and
		 * clean up the queue.
		 */
		if (msgsnd(msqid, (struct msgbuf *)event, event->nwe_size,
		    IPC_NOWAIT) == -1) {
			int errno_save = errno;
			syslog(LOG_ERR, "nwam_event_send: msgsnd: %s, "
			    "destroying message queue %s", strerror(errno_save),
			    eventmsgfile);
			nwam_event_queue_fini(eventmsgfile);
			err = nwam_errno_to_nwam_error(errno_save);
			continue;
		}

	}
	(void) closedir(dirp);

	return (err);
}

/*
 * Destroy an event queue.  Called by nwamd to destroy the associated message
 * queue.
 */
void
nwam_event_queue_fini(const char *eventmsgfile)
{
	key_t key;
	int msqid;

	if ((key = ftok(eventmsgfile, 0)) != -1 &&
	    (msqid = msgget(key, 0644)) != -1 &&
	    msgctl(msqid, IPC_RMID, NULL) != -1)
		(void) unlink(eventmsgfile);
}

/*
 * Stop sending events.  Called by nwamd to destroy each System V message queue
 * registered.
 */
void
nwam_event_send_fini(void)
{
	DIR *dirp;
	struct dirent *dp;
	char eventmsgfile[MAXPATHLEN];

	(void) pthread_mutex_lock(&event_mutex);

	if ((dirp = opendir(NWAM_EVENT_MSG_DIR)) == NULL) {
		(void) pthread_mutex_unlock(&event_mutex);
		return;
	}

	/*
	 * For each file matching our event message queue file prefix,
	 * destroy the queue and message file.
	 */
	while ((dp = readdir(dirp)) != NULL) {
		if (strncmp(dp->d_name, NWAM_EVENT_MSG_FILE,
		    strlen(NWAM_EVENT_MSG_FILE)) != 0)
			continue;

		(void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s/%s",
		    NWAM_EVENT_MSG_DIR, dp->d_name);

		nwam_event_queue_fini(eventmsgfile);
	}
	(void) pthread_mutex_unlock(&event_mutex);
}