libnwam_events.c revision 2
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <assert.h>
2N/A#include <dirent.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <pthread.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <strings.h>
2N/A#include <string.h>
2N/A#include <syslog.h>
2N/A#include <sys/msg.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/types.h>
2N/A#include <unistd.h>
2N/A
2N/A#include "libnwam_impl.h"
2N/A#include <libnwam_priv.h>
2N/A#include <libnwam.h>
2N/A
2N/A/*
2N/A * Implementation of event notification mechanism used by the GUI and
2N/A * netadm. Clients register for events via nwam_events_init() and
2N/A * unregister via nwam_events_fini(). nwamd sends events via nwam_event_send()
2N/A * and applications block waiting for a new event to be delivered in
2N/A * nwam_event_wait(). Events are implemented as System V message queues,
2N/A * one per event client. The event mechanism has to be resilient to
2N/A * nwamd restarts so that clients do not lose the event connection.
2N/A */
2N/A
2N/A#define NWAM_EVENT_MSG_DIR _PATH_SYSVOL "/netcfg/"
2N/A#define NWAM_EVENT_MSG_FILE "nwam_event_msgs"
2N/A#define NWAM_EVENT_MSG_FILE_PREFIX NWAM_EVENT_MSG_DIR NWAM_EVENT_MSG_FILE
2N/A#define NWAM_EVENT_MAX_SIZE (sizeof (struct nwam_event) + \
2N/A (NWAMD_MAX_NUM_WLANS * sizeof (nwam_wlan_t)))
2N/A#define NWAM_EVENT_WAIT_TIME 10
2N/A#define NWAM_EVENT_MAX_NUM_PENDING 25
2N/A
2N/A/*
2N/A * This is protecting simultaneous access to the msqid and its configuration.
2N/A */
2N/Astatic pthread_mutex_t event_mutex = PTHREAD_MUTEX_INITIALIZER;
2N/Astatic int event_msqid = -1;
2N/A
2N/Astatic nwam_error_t
2N/Anwam_event_alloc(nwam_event_t *eventp)
2N/A{
2N/A assert(eventp != NULL);
2N/A
2N/A *eventp = calloc(1, NWAM_EVENT_MAX_SIZE);
2N/A if (*eventp == NULL)
2N/A return (NWAM_NO_MEMORY);
2N/A return (NWAM_SUCCESS);
2N/A}
2N/A
2N/Avoid
2N/Anwam_event_free(nwam_event_t event)
2N/A{
2N/A if (event != NULL)
2N/A free(event);
2N/A}
2N/A
2N/A/*
2N/A * Get next event in queue.
2N/A */
2N/Anwam_error_t
2N/Anwam_event_wait(nwam_event_t *eventp)
2N/A{
2N/A nwam_error_t err;
2N/A nwam_event_t event;
2N/A
2N/A assert(eventp != NULL);
2N/A
2N/A if ((err = nwam_event_alloc(&event)) != NWAM_SUCCESS)
2N/A return (err);
2N/A while (msgrcv(event_msqid, (struct msgbuf *)event, NWAM_EVENT_MAX_SIZE,
2N/A 0, 0) == -1) {
2N/A switch (errno) {
2N/A case EAGAIN:
2N/A case EBUSY:
2N/A /*
2N/A * We see this errno eventhough it isn't
2N/A * documented. Try again. If this causes
2N/A * a busy loop then grab a trace otherwise
2N/A * it's a brace 'til we can figure out why it
2N/A * happens.
2N/A */
2N/A continue;
2N/A
2N/A default:
2N/A nwam_event_free(event);
2N/A return (nwam_errno_to_nwam_error(errno));
2N/A }
2N/A }
2N/A
2N/A /* Resize event down from maximum size */
2N/A if ((*eventp = realloc(event, event->nwe_size)) == NULL)
2N/A return (NWAM_NO_MEMORY);
2N/A
2N/A return (NWAM_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Register for receipt of events from nwamd. Event delivery is
2N/A * done via a System V message queue.
2N/A */
2N/Anwam_error_t
2N/Anwam_events_init(void)
2N/A{
2N/A char eventmsgfile[MAXPATHLEN];
2N/A nwam_error_t err;
2N/A nwam_error_t rc = NWAM_SUCCESS;
2N/A key_t key;
2N/A
2N/A (void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s.%d",
2N/A NWAM_EVENT_MSG_FILE_PREFIX, getpid());
2N/A
2N/A (void) pthread_mutex_lock(&event_mutex);
2N/A
2N/A if (event_msqid != -1) {
2N/A rc = NWAM_ENTITY_IN_USE;
2N/A goto exit;
2N/A }
2N/A
2N/A if ((err = nwam_request_register_unregister
2N/A (NWAM_REQUEST_TYPE_EVENT_REGISTER, eventmsgfile)) != NWAM_SUCCESS) {
2N/A rc = err;
2N/A goto exit;
2N/A }
2N/A
2N/A if ((key = ftok(eventmsgfile, 0)) == -1) {
2N/A rc = nwam_errno_to_nwam_error(errno);
2N/A goto exit;
2N/A }
2N/A
2N/A /* Get system-wide message queue ID */
2N/A if ((event_msqid = msgget(key, 0444)) == -1) {
2N/A rc = nwam_errno_to_nwam_error(errno);
2N/A goto exit;
2N/A }
2N/A
2N/Aexit:
2N/A (void) pthread_mutex_unlock(&event_mutex);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Un-register for receipt of events from nwamd. Make a request to nwamd
2N/A * to destroy the message queue.
2N/A */
2N/Avoid
2N/Anwam_events_fini(void)
2N/A{
2N/A char eventmsgfile[MAXPATHLEN];
2N/A
2N/A (void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s.%d",
2N/A NWAM_EVENT_MSG_FILE_PREFIX, getpid());
2N/A
2N/A (void) pthread_mutex_lock(&event_mutex);
2N/A
2N/A (void) nwam_request_register_unregister
2N/A (NWAM_REQUEST_TYPE_EVENT_UNREGISTER, eventmsgfile);
2N/A
2N/A event_msqid = -1;
2N/A
2N/A (void) pthread_mutex_unlock(&event_mutex);
2N/A}
2N/A
2N/A/*
2N/A * Create an event queue. Called by nwamd to create System V message queues
2N/A * for clients to listen for events.
2N/A */
2N/Anwam_error_t
2N/Anwam_event_queue_init(const char *eventmsgfile)
2N/A{
2N/A int fd;
2N/A key_t key;
2N/A
2N/A if ((fd = open(eventmsgfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
2N/A return (nwam_errno_to_nwam_error(errno));
2N/A (void) close(fd);
2N/A
2N/A if ((key = ftok(eventmsgfile, 0)) == -1)
2N/A return (nwam_errno_to_nwam_error(errno));
2N/A
2N/A if (msgget(key, 0644 | IPC_CREAT) == -1)
2N/A return (nwam_errno_to_nwam_error(errno));
2N/A
2N/A return (NWAM_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Send event to registered listeners via the set of registered System V
2N/A * message queues.
2N/A */
2N/Anwam_error_t
2N/Anwam_event_send(nwam_event_t event)
2N/A{
2N/A DIR *dirp;
2N/A struct dirent *dp;
2N/A struct msqid_ds buf;
2N/A key_t key;
2N/A int msqid;
2N/A char eventmsgfile[MAXPATHLEN];
2N/A nwam_error_t err = NWAM_SUCCESS;
2N/A
2N/A if ((dirp = opendir(NWAM_EVENT_MSG_DIR)) == NULL) {
2N/A return (nwam_errno_to_nwam_error(errno));
2N/A }
2N/A
2N/A /*
2N/A * For each file matching our event message queue file prefix,
2N/A * check the queue is still being read, and if so send the message.
2N/A */
2N/A while ((dp = readdir(dirp)) != NULL) {
2N/A if (strncmp(dp->d_name, NWAM_EVENT_MSG_FILE,
2N/A strlen(NWAM_EVENT_MSG_FILE)) != 0)
2N/A continue;
2N/A
2N/A (void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s/%s",
2N/A NWAM_EVENT_MSG_DIR, dp->d_name);
2N/A
2N/A if ((key = ftok(eventmsgfile, 0)) == -1) {
2N/A int errno_save = errno;
2N/A syslog(LOG_INFO, "nwam_event_send: ftok: %s",
2N/A strerror(errno_save));
2N/A err = nwam_errno_to_nwam_error(errno_save);
2N/A continue;
2N/A }
2N/A
2N/A if ((msqid = msgget(key, 0644)) == -1) {
2N/A int errno_save = errno;
2N/A syslog(LOG_INFO, "nwam_event_send: msgget: %s",
2N/A strerror(errno_save));
2N/A err = nwam_errno_to_nwam_error(errno_save);
2N/A continue;
2N/A }
2N/A
2N/A /* Retrieve stats to analyse queue activity */
2N/A if (msgctl(msqid, IPC_STAT, &buf) == -1) {
2N/A int errno_save = errno;
2N/A syslog(LOG_INFO, "nwam_event_send: msgctl: %s",
2N/A strerror(errno_save));
2N/A err = nwam_errno_to_nwam_error(errno_save);
2N/A continue;
2N/A }
2N/A /*
2N/A * If buf.msg_qnum > NWAM_EVENT_MAX_NUM_PENDING
2N/A * _and_ msg_stime is more than 10s after msg_rtime -
2N/A * indicating message(s) have been hanging around unclaimed -
2N/A * we destroy the queue as the client has most likely gone
2N/A * away. This can happen if a registered client hits Ctrl^C.
2N/A */
2N/A if (buf.msg_qnum > NWAM_EVENT_MAX_NUM_PENDING &&
2N/A ((buf.msg_stime + NWAM_EVENT_WAIT_TIME) > buf.msg_rtime)) {
2N/A nwam_event_queue_fini(eventmsgfile);
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * This shouldn't ever block. If it does then log an error and
2N/A * clean up the queue.
2N/A */
2N/A if (msgsnd(msqid, (struct msgbuf *)event, event->nwe_size,
2N/A IPC_NOWAIT) == -1) {
2N/A int errno_save = errno;
2N/A syslog(LOG_ERR, "nwam_event_send: msgsnd: %s, "
2N/A "destroying message queue %s", strerror(errno_save),
2N/A eventmsgfile);
2N/A nwam_event_queue_fini(eventmsgfile);
2N/A err = nwam_errno_to_nwam_error(errno_save);
2N/A continue;
2N/A }
2N/A
2N/A }
2N/A (void) closedir(dirp);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A/*
2N/A * Destroy an event queue. Called by nwamd to destroy the associated message
2N/A * queue.
2N/A */
2N/Avoid
2N/Anwam_event_queue_fini(const char *eventmsgfile)
2N/A{
2N/A key_t key;
2N/A int msqid;
2N/A
2N/A if ((key = ftok(eventmsgfile, 0)) != -1 &&
2N/A (msqid = msgget(key, 0644)) != -1 &&
2N/A msgctl(msqid, IPC_RMID, NULL) != -1)
2N/A (void) unlink(eventmsgfile);
2N/A}
2N/A
2N/A/*
2N/A * Stop sending events. Called by nwamd to destroy each System V message queue
2N/A * registered.
2N/A */
2N/Avoid
2N/Anwam_event_send_fini(void)
2N/A{
2N/A DIR *dirp;
2N/A struct dirent *dp;
2N/A char eventmsgfile[MAXPATHLEN];
2N/A
2N/A (void) pthread_mutex_lock(&event_mutex);
2N/A
2N/A if ((dirp = opendir(NWAM_EVENT_MSG_DIR)) == NULL) {
2N/A (void) pthread_mutex_unlock(&event_mutex);
2N/A return;
2N/A }
2N/A
2N/A /*
2N/A * For each file matching our event message queue file prefix,
2N/A * destroy the queue and message file.
2N/A */
2N/A while ((dp = readdir(dirp)) != NULL) {
2N/A if (strncmp(dp->d_name, NWAM_EVENT_MSG_FILE,
2N/A strlen(NWAM_EVENT_MSG_FILE)) != 0)
2N/A continue;
2N/A
2N/A (void) snprintf(eventmsgfile, sizeof (eventmsgfile), "%s/%s",
2N/A NWAM_EVENT_MSG_DIR, dp->d_name);
2N/A
2N/A nwam_event_queue_fini(eventmsgfile);
2N/A }
2N/A (void) pthread_mutex_unlock(&event_mutex);
2N/A}