aboutsummaryrefslogtreecommitdiffstats
path: root/res
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-06-10 15:12:17 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-06-10 15:12:17 +0000
commit6195ff1afd2d86d16ce6179327b6ccd8862c898e (patch)
treed87634ac06f4e43877c9790c9f70d61d70b246f1 /res
parent639a4bf7e4bf9917deb652e7b0469e33095a0596 (diff)
Merge another big set of changes from team/russell/events
This commit merges in the rest of the code needed to support distributed device state. There are two main parts to this commit. Core changes: - The device state handling in the core has been updated to understand device state across a cluster of Asterisk servers. Every time the state of a device changes, it looks at all of the device states on each node, and determines the aggregate device state. That resulting device state is what is provided to modules in Asterisk that take actions based on the state of a device. New module, res_ais: - A module has been written to facilitate the communication of events between nodes in a cluster of Asterisk servers. This module uses the SAForum AIS (Service Availability Forum Application Interface Specification) CLM and EVT services (Cluster Management and Event) to handle this task. This module currently supports sharing Voicemail MWI (Message Waiting Indication) and device state events between servers. It has been tested with openais, though other implementations of the spec do exist. For more information on testing distributed device state, see the following doc: - doc/distributed_devstate.txt git-svn-id: http://svn.digium.com/svn/asterisk/trunk@121559 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'res')
-rw-r--r--res/Makefile5
-rw-r--r--res/ais/ais.h48
-rw-r--r--res/ais/amf.c89
-rw-r--r--res/ais/ckpt.c78
-rw-r--r--res/ais/clm.c165
-rw-r--r--res/ais/evt.c588
-rw-r--r--res/ais/lck.c551
-rw-r--r--res/res_ais.c193
8 files changed, 1715 insertions, 2 deletions
diff --git a/res/Makefile b/res/Makefile
index 92f0f336e..a7ba0833f 100644
--- a/res/Makefile
+++ b/res/Makefile
@@ -35,6 +35,8 @@ ael/ael_lex.o: ASTCFLAGS+=-I. -Iael -Wno-unused
ael/ael.tab.o: ael/ael.tab.c ael/ael.tab.h ../include/asterisk/ael_structs.h
ael/ael.tab.o: ASTCFLAGS+=-I. -Iael -DYYENABLE_NLS=0
+$(if $(filter res_ais,$(EMBEDDED_MODS)),modules.link,res_ais.so): ais/clm.o ais/evt.o
+
$(if $(filter res_snmp,$(EMBEDDED_MODS)),modules.link,res_snmp.so): snmp/agent.o
$(if $(filter res_ael_share,$(EMBEDDED_MODS)),modules.link,res_ael_share.so): ael/ael_lex.o ael/ael.tab.o ael/pval.o
@@ -49,5 +51,4 @@ ael/ael.tab.c ael/ael.tab.h:
ael/pval.o: ael/pval.c
clean::
- rm -f snmp/*.o
- rm -f ael/*.o
+ rm -f snmp/*.o ael/*.o ais/*.o
diff --git a/res/ais/ais.h b/res/ais/ais.h
new file mode 100644
index 000000000..2c4c18a87
--- /dev/null
+++ b/res/ais/ais.h
@@ -0,0 +1,48 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ */
+
+#ifndef AST_AIS_H
+#define AST_AIS_H
+
+#include <openais/saAis.h>
+#include <openais/saClm.h>
+#include <openais/saEvt.h>
+
+extern SaVersionT ais_version;
+
+extern SaClmHandleT clm_handle;
+extern SaEvtHandleT evt_handle;
+
+int ast_ais_clm_load_module(void);
+int ast_ais_clm_unload_module(void);
+
+int ast_ais_evt_load_module(void);
+int ast_ais_evt_unload_module(void);
+
+const char *ais_err2str(SaAisErrorT error);
+
+#endif /* AST_AIS_H */
diff --git a/res/ais/amf.c b/res/ais/amf.c
new file mode 100644
index 000000000..fec9af6e0
--- /dev/null
+++ b/res/ais/amf.c
@@ -0,0 +1,89 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ *
+ * This file contains the code specific to the use of the AMF (Application
+ * Management Framework).
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "ais.h"
+
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+#include "asterisk/logger.h"
+
+SaAmfHandleT amf_handle;
+
+static const SaAmfCallbacksT amf_callbacks = {
+ .saAmfHealthcheckCallback = NULL,
+ .saAmfComponentTerminateCallback = NULL,
+ .saAmfCSISetCallback = NULL,
+ .saAmfProtectionGroupTrackCallback = NULL,
+#if 0
+ /*! XXX \todo These appear to be define in the B.02.01 spec, but this won't
+ * compile with them in there. Look into it some more ... */
+ .saAmfProxiedComponentInstantiateCallback = NULL,
+ .saAmfProxiedComponentCleanupCallback = NULL,
+#endif
+};
+
+int ast_ais_amf_load_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ais_res = saAmfInitialize(&amf_handle, &amf_callbacks, &ais_version);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Could not initialize AMF: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ return 0;
+}
+
+int ast_ais_amf_unload_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ais_res = saAmfFinalize(amf_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Problem stopping AMF: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/res/ais/ckpt.c b/res/ais/ckpt.c
new file mode 100644
index 000000000..bdf4b312c
--- /dev/null
+++ b/res/ais/ckpt.c
@@ -0,0 +1,78 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ *
+ * This file contains the code specific to the use of the CKPT (Checkpoint)
+ * service.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "ais.h"
+
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+#include "asterisk/logger.h"
+
+SaCkptHandleT ckpt_handle;
+
+static const SaCkptCallbacksT ckpt_callbacks;
+
+int ast_ais_ckpt_load_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ais_res = saCkptInitialize(&ckpt_handle, &ckpt_callbacks, &ais_version);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Could not initialize CKPT service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ return 0;
+}
+
+int ast_ais_ckpt_unload_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ais_res = saCkptFinalize(amf_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Problem stopping CKPT service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/res/ais/clm.c b/res/ais/clm.c
new file mode 100644
index 000000000..5d7a356be
--- /dev/null
+++ b/res/ais/clm.c
@@ -0,0 +1,165 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ *
+ * This file contains the code specific to the use of the CLM
+ * (Cluster Membership) Service.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "ais.h"
+
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+#include "asterisk/logger.h"
+
+SaClmHandleT clm_handle;
+
+static void clm_node_get_cb(SaInvocationT invocation,
+ const SaClmClusterNodeT *cluster_node, SaAisErrorT error);
+static void clm_track_cb(const SaClmClusterNotificationBufferT *notif_buffer,
+ SaUint32T num_members, SaAisErrorT error);
+
+static const SaClmCallbacksT clm_callbacks = {
+ .saClmClusterNodeGetCallback = clm_node_get_cb,
+ .saClmClusterTrackCallback = clm_track_cb,
+};
+
+static void clm_node_get_cb(SaInvocationT invocation,
+ const SaClmClusterNodeT *cluster_node, SaAisErrorT error)
+{
+
+}
+
+static void clm_track_cb(const SaClmClusterNotificationBufferT *notif_buffer,
+ SaUint32T num_members, SaAisErrorT error)
+{
+
+}
+
+static char *ais_clm_show_members(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ int i;
+ SaClmClusterNotificationBufferT buf;
+ SaClmClusterNotificationT notif[64];
+ SaAisErrorT ais_res;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "ais clm show members";
+ e->usage =
+ "Usage: ais clm show members\n"
+ " List members of the cluster using the CLM (Cluster Membership) service.\n";
+ return NULL;
+
+ case CLI_GENERATE:
+ return NULL; /* no completion */
+ }
+
+ if (a->argc != e->args)
+ return CLI_SHOWUSAGE;
+
+ buf.notification = notif;
+ buf.numberOfItems = ARRAY_LEN(notif);
+
+ ais_res = saClmClusterTrack(clm_handle, SA_TRACK_CURRENT, &buf);
+ if (ais_res != SA_AIS_OK) {
+ ast_cli(a->fd, "Error retrieving current cluster members.\n");
+ return CLI_FAILURE;
+ }
+
+ ast_cli(a->fd, "\n"
+ "=============================================================\n"
+ "=== Cluster Members =========================================\n"
+ "=============================================================\n"
+ "===\n");
+
+ for (i = 0; i < buf.numberOfItems; i++) {
+ SaClmClusterNodeT *node = &buf.notification[i].clusterNode;
+
+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"
+ "=== Node Name: %s\n"
+ "=== ==> ID: 0x%x\n"
+ "=== ==> Address: %s\n"
+ "=== ==> Member: %s\n",
+ (char *) node->nodeName.value, (int) node->nodeId,
+ (char *) node->nodeAddress.value,
+ node->member ? "Yes" : "No");
+
+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"
+ "===\n");
+ }
+
+ ast_cli(a->fd, "=============================================================\n"
+ "\n");
+
+ return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry ais_cli[] = {
+ AST_CLI_DEFINE(ais_clm_show_members, "List current members of the cluster"),
+};
+
+int ast_ais_clm_load_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ais_res = saClmInitialize(&clm_handle, &clm_callbacks, &ais_version);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Could not initialize cluster membership service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ ast_cli_register_multiple(ais_cli, ARRAY_LEN(ais_cli));
+
+ return 0;
+}
+
+int ast_ais_clm_unload_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ast_cli_unregister_multiple(ais_cli, ARRAY_LEN(ais_cli));
+
+ ais_res = saClmFinalize(clm_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Problem stopping cluster membership service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/res/ais/evt.c b/res/ais/evt.c
new file mode 100644
index 000000000..0057f0481
--- /dev/null
+++ b/res/ais/evt.c
@@ -0,0 +1,588 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ *
+ * This file contains the code specific to the use of the EVT
+ * (Event) Service.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "ais.h"
+
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+#include "asterisk/logger.h"
+#include "asterisk/event.h"
+#include "asterisk/config.h"
+#include "asterisk/linkedlists.h"
+
+#ifndef AST_MODULE
+/* XXX HACK */
+#define AST_MODULE "res_ais"
+#endif
+
+SaEvtHandleT evt_handle;
+
+void evt_channel_open_cb(SaInvocationT invocation, SaEvtChannelHandleT channel_handle,
+ SaAisErrorT error);
+void evt_event_deliver_cb(SaEvtSubscriptionIdT subscription_id,
+ const SaEvtEventHandleT event_handle, const SaSizeT event_datalen);
+
+static const SaEvtCallbacksT evt_callbacks = {
+ .saEvtChannelOpenCallback = evt_channel_open_cb,
+ .saEvtEventDeliverCallback = evt_event_deliver_cb,
+};
+
+static const struct {
+ const char *str;
+ enum ast_event_type type;
+} supported_event_types[] = {
+ { "mwi", AST_EVENT_MWI },
+ { "device_state", AST_EVENT_DEVICE_STATE_CHANGE },
+};
+
+/*! Used to provide unique id's to egress subscriptions */
+static int unique_id;
+
+struct subscribe_event {
+ AST_LIST_ENTRY(subscribe_event) entry;
+ /*! This is a unique identifier to identify this subscription in the event
+ * channel through the different API calls, subscribe, unsubscribe, and
+ * the event deliver callback. */
+ SaEvtSubscriptionIdT id;
+ enum ast_event_type type;
+};
+
+struct publish_event {
+ AST_LIST_ENTRY(publish_event) entry;
+ /*! We subscribe to events internally so that we can publish them
+ * on this event channel. */
+ struct ast_event_sub *sub;
+ enum ast_event_type type;
+};
+
+struct event_channel {
+ AST_RWLIST_ENTRY(event_channel) entry;
+ AST_LIST_HEAD_NOLOCK(, subscribe_event) subscribe_events;
+ AST_LIST_HEAD_NOLOCK(, publish_event) publish_events;
+ SaEvtChannelHandleT handle;
+ char name[1];
+};
+
+static AST_RWLIST_HEAD_STATIC(event_channels, event_channel);
+
+void evt_channel_open_cb(SaInvocationT invocation, SaEvtChannelHandleT channel_handle,
+ SaAisErrorT error)
+{
+
+}
+
+static void queue_event(struct ast_event *ast_event)
+{
+ enum ast_event_type type;
+
+ /*!
+ * \todo This hack macks me sad. I need to come up with a better way to
+ * figure out whether an event should be cached or not, and what
+ * parameters to cache on.
+ *
+ * As long as the types of events that are supported is limited,
+ * this isn't *terrible*, I guess. Perhaps we should just define
+ * caching rules in the core, and make them configurable, and not
+ * have it be the job of the event publishers.
+ */
+
+ type = ast_event_get_type(ast_event);
+
+ if (type == AST_EVENT_MWI) {
+ ast_event_queue_and_cache(ast_event,
+ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
+ AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
+ AST_EVENT_IE_END);
+ } else if (type == AST_EVENT_DEVICE_STATE_CHANGE) {
+ ast_event_queue_and_cache(ast_event,
+ AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
+ AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, sizeof(struct ast_eid),
+ AST_EVENT_IE_END);
+ } else {
+ ast_event_queue(ast_event);
+ }
+}
+
+void evt_event_deliver_cb(SaEvtSubscriptionIdT sub_id,
+ const SaEvtEventHandleT event_handle, const SaSizeT event_datalen)
+{
+ /* It is important to note that this works because we *know* that this
+ * function will only be called by a single thread, the dispatch_thread.
+ * If this module gets changed such that this is no longer the case, this
+ * should get changed to a thread-local buffer, instead. */
+ static unsigned char buf[4096];
+ struct ast_event *event_dup, *event = (void *) buf;
+ SaAisErrorT ais_res;
+ SaSizeT len = sizeof(buf);
+
+ if (event_datalen > len) {
+ ast_log(LOG_ERROR, "Event received with size %u, which is too big\n"
+ "for the allocated size %u. Change the code to increase the size.\n",
+ (unsigned int) event_datalen, (unsigned int) len);
+ return;
+ }
+
+ ais_res = saEvtEventDataGet(event_handle, event, &len);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error retrieving event payload: %s\n",
+ ais_err2str(ais_res));
+ return;
+ }
+
+ if (!ast_eid_cmp(&g_eid, ast_event_get_ie_raw(event, AST_EVENT_IE_EID))) {
+ /* Don't feed events back in that originated locally. */
+ return;
+ }
+
+ if (!(event_dup = ast_malloc(len)))
+ return;
+
+ memcpy(event_dup, event, len);
+
+ queue_event(event_dup);
+}
+
+static const char *type_to_filter_str(enum ast_event_type type)
+{
+ const char *filter_str = NULL;
+ int i;
+
+ for (i = 0; i < ARRAY_LEN(supported_event_types); i++) {
+ if (supported_event_types[i].type == type) {
+ filter_str = supported_event_types[i].str;
+ break;
+ }
+ }
+
+ return filter_str;
+}
+
+static void ast_event_cb(const struct ast_event *ast_event, void *data)
+{
+ SaEvtEventHandleT event_handle;
+ SaAisErrorT ais_res;
+ struct event_channel *event_channel = data;
+ SaClmClusterNodeT local_node;
+ SaEvtEventPatternArrayT pattern_array;
+ SaEvtEventPatternT pattern;
+ SaSizeT len;
+ const char *filter_str;
+ SaEvtEventIdT event_id;
+
+ ast_log(LOG_DEBUG, "Got an event to forward\n");
+
+ if (ast_eid_cmp(&g_eid, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID))) {
+ /* If the event didn't originate from this server, don't send it back out. */
+ ast_log(LOG_DEBUG, "Returning here\n");
+ return;
+ }
+
+ ais_res = saEvtEventAllocate(event_channel->handle, &event_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error allocating event: %s\n", ais_err2str(ais_res));
+ ast_log(LOG_DEBUG, "Returning here\n");
+ return;
+ }
+
+ ais_res = saClmClusterNodeGet(clm_handle, SA_CLM_LOCAL_NODE_ID,
+ SA_TIME_ONE_SECOND, &local_node);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error getting local node name: %s\n", ais_err2str(ais_res));
+ goto return_event_free;
+ }
+
+ filter_str = type_to_filter_str(ast_event_get_type(ast_event));
+ len = strlen(filter_str) + 1;
+ pattern.pattern = (SaUint8T *) filter_str;
+ pattern.patternSize = len;
+ pattern.allocatedSize = len;
+
+ pattern_array.allocatedNumber = 1;
+ pattern_array.patternsNumber = 1;
+ pattern_array.patterns = &pattern;
+
+ /*!
+ * /todo Make retention time configurable
+ * /todo Make event priorities configurable
+ */
+ ais_res = saEvtEventAttributesSet(event_handle, &pattern_array,
+ SA_EVT_LOWEST_PRIORITY, SA_TIME_ONE_MINUTE, &local_node.nodeName);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error setting event attributes: %s\n", ais_err2str(ais_res));
+ goto return_event_free;
+ }
+
+ ais_res = saEvtEventPublish(event_handle,
+ ast_event, ast_event_get_size(ast_event), &event_id);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error publishing event: %s\n", ais_err2str(ais_res));
+ goto return_event_free;
+ }
+
+return_event_free:
+ ais_res = saEvtEventFree(event_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error freeing allocated event: %s\n", ais_err2str(ais_res));
+ }
+ ast_log(LOG_DEBUG, "Returning here (event_free)\n");
+}
+
+static char *ais_evt_show_event_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct event_channel *event_channel;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "ais evt show event channels";
+ e->usage =
+ "Usage: ais evt show event channels\n"
+ " List configured event channels for the (EVT) Eventing service.\n";
+ return NULL;
+
+ case CLI_GENERATE:
+ return NULL; /* no completion */
+ }
+
+ if (a->argc != e->args)
+ return CLI_SHOWUSAGE;
+
+ ast_cli(a->fd, "\n"
+ "=============================================================\n"
+ "=== Event Channels ==========================================\n"
+ "=============================================================\n"
+ "===\n");
+
+ AST_RWLIST_RDLOCK(&event_channels);
+ AST_RWLIST_TRAVERSE(&event_channels, event_channel, entry) {
+ struct publish_event *publish_event;
+ struct subscribe_event *subscribe_event;
+
+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"
+ "=== Event Channel Name: %s\n", event_channel->name);
+
+ AST_LIST_TRAVERSE(&event_channel->publish_events, publish_event, entry) {
+ ast_cli(a->fd, "=== ==> Publishing Event Type: %s\n",
+ type_to_filter_str(publish_event->type));
+ }
+
+ AST_LIST_TRAVERSE(&event_channel->subscribe_events, subscribe_event, entry) {
+ ast_cli(a->fd, "=== ==> Subscribing to Event Type: %s\n",
+ type_to_filter_str(subscribe_event->type));
+ }
+
+ ast_cli(a->fd, "=== ---------------------------------------------------------\n"
+ "===\n");
+ }
+ AST_RWLIST_UNLOCK(&event_channels);
+
+ ast_cli(a->fd, "=============================================================\n"
+ "\n");
+
+ return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry ais_cli[] = {
+ AST_CLI_DEFINE(ais_evt_show_event_channels, "Show configured event channels"),
+};
+
+static void add_publish_event(struct event_channel *event_channel, const char *event_type)
+{
+ int i;
+ enum ast_event_type type = -1;
+ struct publish_event *publish_event;
+
+ for (i = 0; i < ARRAY_LEN(supported_event_types); i++) {
+ if (!strcasecmp(event_type, supported_event_types[i].str)) {
+ type = supported_event_types[i].type;
+ break;
+ }
+ }
+
+ if (type == -1) {
+ ast_log(LOG_WARNING, "publish_event option given with invalid value '%s'\n", event_type);
+ return;
+ }
+
+ if (!(publish_event = ast_calloc(1, sizeof(*publish_event))))
+ return;
+
+ publish_event->type = type;
+ ast_log(LOG_DEBUG, "Subscribing to event type %d\n", type);
+ publish_event->sub = ast_event_subscribe(type, ast_event_cb, event_channel,
+ AST_EVENT_IE_END);
+ ast_event_dump_cache(publish_event->sub);
+
+ AST_LIST_INSERT_TAIL(&event_channel->publish_events, publish_event, entry);
+}
+
+static SaAisErrorT set_egress_subscription(struct event_channel *event_channel,
+ struct subscribe_event *subscribe_event)
+{
+ SaAisErrorT ais_res;
+ SaEvtEventFilterArrayT filter_array;
+ SaEvtEventFilterT filter;
+ const char *filter_str = NULL;
+ SaSizeT len;
+
+ /* We know it's going to be valid. It was checked earlier. */
+ filter_str = type_to_filter_str(subscribe_event->type);
+
+ filter.filterType = SA_EVT_EXACT_FILTER;
+ len = strlen(filter_str) + 1;
+ filter.filter.allocatedSize = len;
+ filter.filter.patternSize = len;
+ filter.filter.pattern = (SaUint8T *) filter_str;
+
+ filter_array.filtersNumber = 1;
+ filter_array.filters = &filter;
+
+ ais_res = saEvtEventSubscribe(event_channel->handle, &filter_array,
+ subscribe_event->id);
+
+ return ais_res;
+}
+
+static void add_subscribe_event(struct event_channel *event_channel, const char *event_type)
+{
+ int i;
+ enum ast_event_type type = -1;
+ struct subscribe_event *subscribe_event;
+ SaAisErrorT ais_res;
+
+ for (i = 0; i < ARRAY_LEN(supported_event_types); i++) {
+ if (!strcasecmp(event_type, supported_event_types[i].str)) {
+ type = supported_event_types[i].type;
+ break;
+ }
+ }
+
+ if (type == -1) {
+ ast_log(LOG_WARNING, "subscribe_event option given with invalid value '%s'\n", event_type);
+ return;
+ }
+
+ if (!(subscribe_event = ast_calloc(1, sizeof(*subscribe_event))))
+ return;
+
+ subscribe_event->type = type;
+ subscribe_event->id = ast_atomic_fetchadd_int(&unique_id, +1);
+
+ ais_res = set_egress_subscription(event_channel, subscribe_event);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error setting up egress subscription: %s\n",
+ ais_err2str(ais_res));
+ free(subscribe_event);
+ return;
+ }
+
+ AST_LIST_INSERT_TAIL(&event_channel->subscribe_events, subscribe_event, entry);
+}
+
+static void build_event_channel(struct ast_config *cfg, const char *cat)
+{
+ struct ast_variable *var;
+ struct event_channel *event_channel;
+ SaAisErrorT ais_res;
+ SaNameT sa_name = { 0, };
+
+ AST_RWLIST_WRLOCK(&event_channels);
+ AST_RWLIST_TRAVERSE(&event_channels, event_channel, entry) {
+ if (!strcasecmp(event_channel->name, cat))
+ break;
+ }
+ AST_RWLIST_UNLOCK(&event_channels);
+ if (event_channel) {
+ ast_log(LOG_WARNING, "Event channel '%s' was specified twice in "
+ "configuration. Second instance ignored.\n", cat);
+ return;
+ }
+
+ if (!(event_channel = ast_calloc(1, sizeof(*event_channel) + strlen(cat))))
+ return;
+
+ strcpy(event_channel->name, cat);
+ ast_copy_string((char *) sa_name.value, cat, sizeof(sa_name.value));
+ sa_name.length = strlen((char *) sa_name.value);
+ ais_res = saEvtChannelOpen(evt_handle, &sa_name,
+ SA_EVT_CHANNEL_PUBLISHER | SA_EVT_CHANNEL_SUBSCRIBER | SA_EVT_CHANNEL_CREATE,
+ SA_TIME_MAX, &event_channel->handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error opening event channel: %s\n", ais_err2str(ais_res));
+ free(event_channel);
+ return;
+ }
+
+ for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
+ if (!strcasecmp(var->name, "type")) {
+ continue;
+ } else if (!strcasecmp(var->name, "publish_event")) {
+ add_publish_event(event_channel, var->value);
+ } else if (!strcasecmp(var->name, "subscribe_event")) {
+ add_subscribe_event(event_channel, var->value);
+ } else {
+ ast_log(LOG_WARNING, "Event channel '%s' contains invalid option '%s'\n",
+ event_channel->name, var->name);
+ }
+ }
+
+ AST_RWLIST_WRLOCK(&event_channels);
+ AST_RWLIST_INSERT_TAIL(&event_channels, event_channel, entry);
+ AST_RWLIST_UNLOCK(&event_channels);
+}
+
+static void load_config(void)
+{
+ static const char filename[] = "ais.conf";
+ struct ast_config *cfg;
+ const char *cat = NULL;
+ struct ast_flags config_flags = { 0 };
+
+ if (!(cfg = ast_config_load(filename, config_flags)))
+ return;
+
+ while ((cat = ast_category_browse(cfg, cat))) {
+ const char *type;
+
+ if (!strcasecmp(cat, "general"))
+ continue;
+
+ if (!(type = ast_variable_retrieve(cfg, cat, "type"))) {
+ ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n",
+ filename);
+ continue;
+ }
+
+ if (!strcasecmp(type, "event_channel")) {
+ build_event_channel(cfg, cat);
+ } else {
+ ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'\n",
+ filename, type);
+ }
+ }
+
+ ast_config_destroy(cfg);
+}
+
+static void publish_event_destroy(struct publish_event *publish_event)
+{
+ ast_event_unsubscribe(publish_event->sub);
+
+ free(publish_event);
+}
+
+static void subscribe_event_destroy(const struct event_channel *event_channel,
+ struct subscribe_event *subscribe_event)
+{
+ SaAisErrorT ais_res;
+
+ /* saEvtChannelClose() will actually do this automatically, but it just
+ * feels cleaner to go ahead and do it manually ... */
+ ais_res = saEvtEventUnsubscribe(event_channel->handle, subscribe_event->id);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error unsubscribing: %s\n", ais_err2str(ais_res));
+ }
+
+ free(subscribe_event);
+}
+
+static void event_channel_destroy(struct event_channel *event_channel)
+{
+ struct publish_event *publish_event;
+ struct subscribe_event *subscribe_event;
+ SaAisErrorT ais_res;
+
+ while ((publish_event = AST_LIST_REMOVE_HEAD(&event_channel->publish_events, entry)))
+ publish_event_destroy(publish_event);
+ while ((subscribe_event = AST_LIST_REMOVE_HEAD(&event_channel->subscribe_events, entry)))
+ subscribe_event_destroy(event_channel, subscribe_event);
+
+ ais_res = saEvtChannelClose(event_channel->handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error closing event channel '%s': %s\n",
+ event_channel->name, ais_err2str(ais_res));
+ }
+
+ free(event_channel);
+}
+
+static void destroy_event_channels(void)
+{
+ struct event_channel *event_channel;
+
+ AST_RWLIST_WRLOCK(&event_channels);
+ while ((event_channel = AST_RWLIST_REMOVE_HEAD(&event_channels, entry)))
+ event_channel_destroy(event_channel);
+ AST_RWLIST_UNLOCK(&event_channels);
+}
+
+int ast_ais_evt_load_module(void)
+{
+ SaAisErrorT ais_res;
+
+ ais_res = saEvtInitialize(&evt_handle, &evt_callbacks, &ais_version);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Could not initialize eventing service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ load_config();
+
+ ast_cli_register_multiple(ais_cli, ARRAY_LEN(ais_cli));
+
+ return 0;
+}
+
+int ast_ais_evt_unload_module(void)
+{
+ SaAisErrorT ais_res;
+
+ destroy_event_channels();
+
+ ais_res = saEvtFinalize(evt_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Problem stopping eventing service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/res/ais/lck.c b/res/ais/lck.c
new file mode 100644
index 000000000..7e7533dc6
--- /dev/null
+++ b/res/ais/lck.c
@@ -0,0 +1,551 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ *
+ * This file contains the code specific to the use of the LCK
+ * (Distributed Locks) Service.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "ais.h"
+
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+#include "asterisk/logger.h"
+#include "asterisk/pbx.h"
+#include "asterisk/app.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/strings.h"
+#include "asterisk/channel.h"
+
+SaLckHandleT lck_handle;
+
+/*!
+ * \brief Callbacks available in the Lock Service
+ *
+ * None of these are actually required if only synchronous locking is used.
+ * However, some of them must be implemented should the asynchronous locks
+ * be used.
+ */
+static SaLckCallbacksT lck_callbacks = {
+ /*! Get notified when a cluster-wide lock gets created */
+ .saLckResourceOpenCallback = NULL,
+ /*! Get notified when an asynchronous lock request gets granted */
+ .saLckLockGrantCallback = NULL,
+ /*! Be informed when a currently held lock is blocking another node */
+ .saLckLockWaiterCallback = NULL,
+ /*! Get notified when an asynchronous unlock request is done */
+ .saLckResourceUnlockCallback = NULL,
+};
+
+enum lock_type {
+ RDLOCK,
+ WRLOCK,
+ TRY_RDLOCK,
+ TRY_WRLOCK,
+};
+
+#define LOCK_BUCKETS 101
+
+/*!
+ * Every thread that wants to use a distributed lock must open its own handle
+ * to the lock. So, a thread-local container of opened locks is used to keep
+ * track of what locks have been opened.
+ *
+ * \todo It would be nice to be able to have a thread-local container, instead
+ * of using a thread-local wrapper like this.
+ */
+struct lock_resources {
+ struct ao2_container *locks;
+};
+
+static int lock_resources_init(void *);
+static void lock_resources_destroy(void *);
+
+AST_THREADSTORAGE_CUSTOM(locks_ts_key,
+ lock_resources_init, lock_resources_destroy);
+
+struct lock_resource {
+ SaLckResourceHandleT handle;
+ SaLckLockIdT id;
+ SaNameT ais_name;
+ const char *name;
+};
+
+static int lock_hash_cb(const void *obj, int flags)
+{
+ const struct lock_resource *lock = obj;
+
+ return ast_str_hash(lock->name);
+}
+
+static int lock_cmp_cb(void *obj, void *arg, int flags)
+{
+ struct lock_resource *lock1 = obj, *lock2 = arg;
+
+ return !strcasecmp(lock1->name, lock2->name) ? CMP_MATCH : 0;
+}
+
+static int lock_resources_init(void *data)
+{
+ struct lock_resources *lock_resources = data;
+
+ if (!(lock_resources->locks = ao2_container_alloc(LOCK_BUCKETS,
+ lock_hash_cb, lock_cmp_cb))) {
+ return -1;
+ }
+
+ return 0;
+
+}
+
+static void lock_resources_destroy(void *data)
+{
+ struct lock_resources *lock_resources = data;
+
+ ao2_ref(lock_resources->locks, -1);
+
+ ast_free(lock_resources);
+}
+
+static void lock_destructor(void *obj)
+{
+ struct lock_resource *lock = obj;
+
+ if (lock->name)
+ ast_free((void *) lock->name);
+}
+
+static inline struct lock_resource *lock_ref(struct lock_resource *lock)
+{
+ ao2_ref(lock, +1);
+ return lock;
+}
+
+static inline struct lock_resource *lock_unref(struct lock_resource *lock)
+{
+ ao2_ref(lock, -1);
+ return NULL;
+}
+
+static void lock_datastore_destroy(void *data)
+{
+ struct lock_resource *lock = data;
+ SaAisErrorT ais_res;
+
+ ais_res = saLckResourceUnlock(lock->id, SA_TIME_ONE_SECOND * 3);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error unlocking '%s': %s\n", lock->name,
+ ais_err2str(ais_res));
+ }
+
+ lock_unref(lock);
+}
+
+static struct lock_resource *find_lock(const char *name)
+{
+ struct lock_resource *lock, tmp_lock = {
+ .name = name,
+ };
+ SaAisErrorT ais_res;
+ struct lock_resources *lock_resources;
+
+ if (!(lock_resources = ast_threadstorage_get(&locks_ts_key,
+ sizeof(*lock_resources)))) {
+ return NULL;
+ }
+
+ /* Return the lock if it has already been opened by this thread */
+ if ((lock = ao2_find(lock_resources->locks, &tmp_lock, OBJ_POINTER)))
+ return lock;
+
+ /* Allocate and open the lock */
+ if (!(lock = ao2_alloc(sizeof(*lock), lock_destructor)))
+ return NULL;
+
+ if (!(lock->name = ast_strdup(name)))
+ return lock_unref(lock);
+
+ /* Map the name into the SaNameT for convenience */
+ ast_copy_string((char *) lock->ais_name.value, lock->name,
+ sizeof(lock->ais_name.value));
+ lock->ais_name.length = strlen(lock->name);
+
+ ais_res = saLckResourceOpen(lck_handle, &lock->ais_name,
+ SA_LCK_RESOURCE_CREATE, SA_TIME_ONE_SECOND * 3, &lock->handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Failed to open lock: %s\n", ais_err2str(ais_res));
+ return lock_unref(lock);
+ }
+
+ return lock;
+}
+
+const struct ast_datastore_info dlock_datastore_info = {
+ .type = "DLOCK",
+ .destroy = lock_datastore_destroy,
+};
+
+static void add_lock_to_chan(struct ast_channel *chan, struct lock_resource *lock,
+ enum lock_type lock_type, double timeout, char *buf, size_t len)
+{
+ struct ast_datastore *datastore;
+ SaAisErrorT ais_res;
+ SaLckLockModeT mode = 0;
+ SaLckLockFlagsT flags = 0;
+ SaLckLockStatusT status;
+
+ ast_channel_lock(chan);
+ datastore = ast_channel_datastore_find(chan, &dlock_datastore_info, lock->name);
+
+ if (datastore) {
+ ast_log(LOG_ERROR, "The DLOCK '%s' is already locked by channel '%s'\n",
+ lock->name, chan->name);
+ ast_channel_unlock(chan);
+ ast_copy_string(buf, "FAILURE", len);
+ return;
+ }
+ ast_channel_unlock(chan);
+
+ switch (lock_type) {
+ case TRY_RDLOCK:
+ flags = SA_LCK_LOCK_NO_QUEUE;
+ mode = SA_LCK_PR_LOCK_MODE;
+ break;
+ case RDLOCK:
+ flags = SA_LCK_LOCK_NO_QUEUE;
+ mode = SA_LCK_PR_LOCK_MODE;
+ break;
+ case TRY_WRLOCK:
+ flags = SA_LCK_LOCK_NO_QUEUE;
+ mode = SA_LCK_EX_LOCK_MODE;
+ break;
+ case WRLOCK:
+ flags = SA_LCK_LOCK_NO_QUEUE;
+ mode = SA_LCK_EX_LOCK_MODE;
+ break;
+ }
+
+ /* Actually acquire the lock now */
+ ais_res = saLckResourceLock(lock->handle, &lock->id, mode, flags, 0,
+ (SaTimeT) timeout * SA_TIME_ONE_SECOND, &status);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Problem acquiring lock '%s': %s\n",
+ lock->name, ais_err2str(ais_res));
+ ast_copy_string(buf, (ais_res == SA_AIS_ERR_TIMEOUT) ? "TIMEOUT" :
+ "FAILURE", len);
+ return;
+ }
+
+ switch (status) {
+ case SA_LCK_LOCK_GRANTED:
+ ast_copy_string(buf, "SUCCESS", len);
+ break;
+ case SA_LCK_LOCK_DEADLOCK:
+ ast_copy_string(buf, "DEADLOCK", len);
+ return;
+ /*! XXX \todo Need to look at handling these other cases in a different way */
+ case SA_LCK_LOCK_NOT_QUEUED:
+ case SA_LCK_LOCK_ORPHANED:
+ case SA_LCK_LOCK_NO_MORE:
+ case SA_LCK_LOCK_DUPLICATE_EX:
+ ast_copy_string(buf, "FAILURE", len);
+ return;
+ }
+
+ if (!(datastore = ast_channel_datastore_alloc(&dlock_datastore_info,
+ lock->name))) {
+ ast_copy_string(buf, "FAILURE", len);
+ return;
+ }
+
+ datastore->data = lock_ref(lock);
+
+ ast_channel_lock(chan);
+ ast_channel_datastore_add(chan, datastore);
+ ast_channel_unlock(chan);
+}
+
+static int handle_lock(struct ast_channel *chan, enum lock_type lock_type,
+ char *data, char *buf, size_t len)
+{
+ AST_DECLARE_APP_ARGS(args,
+ AST_APP_ARG(name);
+ AST_APP_ARG(timeout);
+ );
+ int res = 0;
+ double timeout = 3;
+ struct lock_resource *lock = NULL;
+
+ ast_autoservice_start(chan);
+
+ AST_STANDARD_APP_ARGS(args, data);
+ if (ast_strlen_zero(args.name)) {
+ ast_log(LOG_ERROR, "The DLOCK functions require a lock name\n");
+ res = -1;
+ goto return_cleanup;
+ }
+ switch (lock_type) {
+ case RDLOCK:
+ case WRLOCK:
+ if (!ast_strlen_zero(args.timeout) && ((timeout = atof(args.timeout)) < 0)) {
+ ast_log(LOG_ERROR, "Timeout value '%s' not valid\n", args.timeout);
+ res = -1;
+ goto return_cleanup;
+ }
+ break;
+ case TRY_RDLOCK:
+ case TRY_WRLOCK:
+ if (!ast_strlen_zero(args.timeout)) {
+ ast_log(LOG_ERROR, "The trylock functions only take one argument\n");
+ res = -1;
+ goto return_cleanup;
+ }
+ }
+
+ if (!(lock = find_lock(args.name))) {
+ ast_copy_string(buf, "FAILURE", len);
+ res = -1;
+ goto return_cleanup;
+ }
+
+ add_lock_to_chan(chan, lock, lock_type, timeout, buf, len);
+
+ lock = lock_unref(lock);
+
+return_cleanup:
+ ast_autoservice_stop(chan);
+
+ return res;
+}
+
+static int handle_rdlock(struct ast_channel *chan, const char *cmd, char *data,
+ char *buf, size_t len)
+{
+ return handle_lock(chan, RDLOCK, data, buf, len);
+}
+
+static int handle_wrlock(struct ast_channel *chan, const char *cmd, char *data,
+ char *buf, size_t len)
+{
+ return handle_lock(chan, WRLOCK, data, buf, len);
+}
+
+static int handle_tryrdlock(struct ast_channel *chan, const char *cmd, char *data,
+ char *buf, size_t len)
+{
+ return handle_lock(chan, TRY_RDLOCK, data, buf, len);
+}
+
+static int handle_trywrlock(struct ast_channel *chan, const char *cmd, char *data,
+ char *buf, size_t len)
+{
+ return handle_lock(chan, TRY_WRLOCK, data, buf, len);
+}
+
+static int handle_unlock(struct ast_channel *chan, const char *cmd, char *data,
+ char *buf, size_t len)
+{
+ struct ast_datastore *datastore;
+ struct lock_resource *lock;
+ SaAisErrorT ais_res;
+ int res = 0;
+
+ if (ast_strlen_zero(data)) {
+ ast_log(LOG_ERROR, "DLOCK_UNLOCK requires a lock name\n");
+ ast_copy_string(buf, "FAILURE", len);
+ return -1;
+ }
+
+ ast_autoservice_start(chan);
+
+ ast_channel_lock(chan);
+ datastore = ast_channel_datastore_find(chan, &dlock_datastore_info, data);
+ if (!datastore) {
+ ast_log(LOG_ERROR, "The DLOCK '%s' is not locked by channel '%s'\n",
+ data, chan->name);
+ ast_channel_unlock(chan);
+ ast_copy_string(buf, "FAILURE", len);
+ return -1;
+ }
+ ast_channel_datastore_remove(chan, datastore);
+ ast_channel_unlock(chan);
+
+ lock = datastore->data;
+ ais_res = saLckResourceUnlock(lock->id, SA_TIME_ONE_SECOND * 3);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Error unlocking '%s': %s\n", lock->name,
+ ais_err2str(ais_res));
+ res = -1;
+ ast_copy_string(buf, (ais_res == SA_AIS_ERR_TIMEOUT) ? "TIMEOUT" :
+ "FAILURE", len);
+ } else {
+ ast_copy_string(buf, "SUCCESS", len);
+ }
+
+ datastore->data = lock_unref(lock);
+ ast_channel_datastore_free(datastore);
+
+ ast_autoservice_stop(chan);
+
+ return res;
+}
+
+#define LOCK_DESC_COMMON \
+" The name of the lock can be anything. The first time a named lock gets\n" \
+"used, it will be automatically created and maintained amongst the cluster.\n" \
+" The result of this function will be one of the following:\n" \
+" SUCCESS | TIMEOUT | FAILURE | DEADLOCK\n" DEADLOCK_DESC
+
+#define DEADLOCK_DESC \
+" The result, DEADLOCK, can only be provided if the AIS implementation in\n" \
+"use provides the optional feature of deadlock detection. If the lock fails\n" \
+"with the result of DEADLOCK, it means that the AIS implementation has\n" \
+"determined that if this lock were acquired, it would cause a deadlock.\n"
+
+static struct ast_custom_function dlock_rdlock = {
+ .name = "DLOCK_RDLOCK",
+ .synopsis = "Read-lock a distributed lock",
+ .desc =
+" This function will read-lock a distributed lock provided by the locking\n"
+"service of AIS. This is a blocking operation. However, a timeout can be\n"
+"specified to avoid deadlocks. The default timeout used if one is not\n"
+"provided as an argument is 3 seconds.\n"
+LOCK_DESC_COMMON
+"",
+ .syntax = "DLOCK_RDLOCK(<lock_name>,[timeout])",
+ .read = handle_rdlock,
+};
+
+static struct ast_custom_function dlock_wrlock = {
+ .name = "DLOCK_WRLOCK",
+ .synopsis = "Write-lock a distributed lock",
+ .desc =
+" This function will write-lock a distributed lock provided by the locking\n"
+"service of AIS. This is a blocking operation. However, a timeout can be\n"
+"specified to avoid deadlocks. The default timeout used if one is not\n"
+"provided as an argument is 3 seconds.\n"
+LOCK_DESC_COMMON
+"",
+ .syntax = "DLOCK_WRLOCK(<lock_name>,[timeout])",
+ .read = handle_wrlock,
+};
+
+static struct ast_custom_function dlock_tryrdlock = {
+ .name = "DLOCK_TRYRDLOCK",
+ .synopsis = "Try to read-lock a distributed lock",
+ .desc =
+" This function will attempt to read-lock a distributed lock provided by the\n"
+"locking service of AIS. This is a non-blocking operation.\n"
+" The name of the lock can be anything. The first time a named lock gets\n"
+"used, it will be automatically created and maintained amongst the cluster.\n"
+" The result of this function will be one of the following:\n"
+" SUCCESS | FAILURE | DEADLOCK\n"
+DEADLOCK_DESC
+"",
+ .syntax = "DLOCK_TRYRDLOCK(<lock_name>)",
+ .read = handle_tryrdlock,
+};
+
+static struct ast_custom_function dlock_trywrlock = {
+ .name = "DLOCK_TRYWRLOCK",
+ .synopsis = "Try to write-lock a distributed lock",
+ .desc =
+" This function will attempt to write-lock a distributed lock provided by\n"
+"the locking service of AIS. This is a non-blocking operation.\n"
+" The name of the lock can be anything. The first time a named lock gets\n"
+"used, it will be automatically created and maintained amongst the cluster.\n"
+" The result of this function will be one of the following:\n"
+" SUCCESS | FAILURE | DEADLOCK\n"
+DEADLOCK_DESC
+"",
+ .syntax = "DLOCK_TRYWRLOCK(<lock_name>)",
+ .read = handle_trywrlock,
+};
+
+static struct ast_custom_function dlock_unlock = {
+ .name = "DLOCK_UNLOCK",
+ .synopsis = "Unlock a distributed lock",
+ .desc =
+" This function will unlock a currently held distributed lock. This should\n"
+"be used regardless of the lock was read or write locked. The result of\n"
+"this funtion will be one of the following:\n"
+" SUCCESS | TIMEOUT | FAILURE\n"
+"",
+ .syntax = "DLOCK_UNLOCK(<lock_name>)",
+ .read = handle_unlock,
+};
+
+int ast_ais_lck_load_module(void)
+{
+ SaAisErrorT ais_res;
+ int res;
+
+ ais_res = saLckInitialize(&lck_handle, &lck_callbacks, &ais_version);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Could not initialize distributed locking service: %s\n",
+ ais_err2str(ais_res));
+ return -1;
+ }
+
+ res = ast_custom_function_register(&dlock_rdlock);
+ res |= ast_custom_function_register(&dlock_wrlock);
+ res |= ast_custom_function_register(&dlock_tryrdlock);
+ res |= ast_custom_function_register(&dlock_trywrlock);
+ res |= ast_custom_function_register(&dlock_unlock);
+
+ return res;
+}
+
+int ast_ais_lck_unload_module(void)
+{
+ SaAisErrorT ais_res;
+ int res = 0;
+
+ ast_custom_function_unregister(&dlock_rdlock);
+ ast_custom_function_unregister(&dlock_wrlock);
+ ast_custom_function_unregister(&dlock_tryrdlock);
+ ast_custom_function_unregister(&dlock_trywrlock);
+ ast_custom_function_unregister(&dlock_unlock);
+
+ ais_res = saLckFinalize(lck_handle);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Problem stopping distributed locking service: %s\n",
+ ais_err2str(ais_res));
+ res = -1;
+ }
+
+ return res;
+}
diff --git a/res/res_ais.c b/res/res_ais.c
new file mode 100644
index 000000000..884eb0d23
--- /dev/null
+++ b/res/res_ais.c
@@ -0,0 +1,193 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007 - 2008, Digium, Inc.
+ *
+ * Russell Bryant <russell@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell@digium.com>
+ *
+ * \brief Usage of the SAForum AIS (Application Interface Specification)
+ *
+ * \arg http://www.openais.org/
+ *
+ * This file contains the common code between the uses of the different AIS
+ * services.
+ */
+
+/*** MODULEINFO
+ <depend>SaClm</depend>
+ <depend>SaEvt</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+#include <pthread.h>
+
+#include "ais/ais.h"
+
+#include "asterisk/module.h"
+#include "asterisk/options.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+
+static struct {
+ pthread_t id;
+ unsigned int stop:1;
+} dispatch_thread = {
+ .id = AST_PTHREADT_NULL,
+};
+
+SaVersionT ais_version = { 'B', 1, 1 };
+
+static const struct ais_error {
+ SaAisErrorT error;
+ const char *desc;
+} ais_errors[] = {
+ { SA_AIS_OK, "OK" },
+ { SA_AIS_ERR_LIBRARY, "Library Error" },
+ { SA_AIS_ERR_VERSION, "Version Not Compatible" },
+ { SA_AIS_ERR_INIT, "Callback Not Registered" },
+ { SA_AIS_ERR_TIMEOUT, "Timeout" },
+ { SA_AIS_ERR_TRY_AGAIN , "Try Again" },
+ { SA_AIS_ERR_INVALID_PARAM, "Invalid Parameter" },
+ { SA_AIS_ERR_NO_MEMORY, "No Memory" },
+ { SA_AIS_ERR_BAD_HANDLE, "Invalid Handle" },
+ { SA_AIS_ERR_BUSY, "Resource Already In Use" },
+ { SA_AIS_ERR_ACCESS, "Access Denied" },
+ { SA_AIS_ERR_NOT_EXIST, "Does Not Exist" },
+ { SA_AIS_ERR_NAME_TOO_LONG, "Name Too Long" },
+ { SA_AIS_ERR_EXIST, "Already Exists" },
+ { SA_AIS_ERR_NO_SPACE, "Buffer Too Small" },
+ { SA_AIS_ERR_INTERRUPT, "Request Interrupted" },
+ { SA_AIS_ERR_NAME_NOT_FOUND, "Name Not Found" },
+ { SA_AIS_ERR_NO_RESOURCES, "Not Enough Resources" },
+ { SA_AIS_ERR_NOT_SUPPORTED, "Requested Function Not Supported" },
+ { SA_AIS_ERR_BAD_OPERATION, "Operation Not Allowed" },
+ { SA_AIS_ERR_FAILED_OPERATION, "Operation Failed" },
+ { SA_AIS_ERR_MESSAGE_ERROR, "Communication Error" },
+ { SA_AIS_ERR_QUEUE_FULL, "Destination Queue Full" },
+ { SA_AIS_ERR_QUEUE_NOT_AVAILABLE, "Destination Queue Not Available" },
+ { SA_AIS_ERR_BAD_FLAGS, "Invalid Flags" },
+ { SA_AIS_ERR_TOO_BIG, "Value Too Large" },
+ { SA_AIS_ERR_NO_SECTIONS, "No More Sections to Initialize" },
+};
+
+const char *ais_err2str(SaAisErrorT error)
+{
+ int x;
+
+ for (x = 0; x < ARRAY_LEN(ais_errors); x++) {
+ if (ais_errors[x].error == error)
+ return ais_errors[x].desc;
+ }
+
+ return "Unknown";
+}
+
+static void *dispatch_thread_handler(void *data)
+{
+ SaSelectionObjectT clm_fd, evt_fd, max_fd;
+ int res;
+ fd_set read_fds;
+ SaAisErrorT ais_res;
+
+ ais_res = saClmSelectionObjectGet(clm_handle, &clm_fd);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Failed to retrieve select fd for CLM service. "
+ "This module will not operate.\n");
+ return NULL;
+ }
+
+ ais_res = saEvtSelectionObjectGet(evt_handle, &evt_fd);
+ if (ais_res != SA_AIS_OK) {
+ ast_log(LOG_ERROR, "Failed to retrieve select fd for EVT service. "
+ "This module will not operate.\n");
+ return NULL;
+ }
+
+ max_fd = clm_fd > evt_fd ? clm_fd : evt_fd;
+
+ while (!dispatch_thread.stop) {
+ FD_ZERO(&read_fds);
+ FD_SET(clm_fd, &read_fds);
+ FD_SET(evt_fd, &read_fds);
+
+ res = ast_select(max_fd + 1, &read_fds, NULL, NULL, NULL);
+ if (res == -1 && errno != EINTR && errno != EAGAIN) {
+ ast_log(LOG_ERROR, "Select error (%s) dispatch thread going away now, "
+ "and the module will no longer operate.\n", strerror(errno));
+ break;
+ }
+
+ if (FD_ISSET(clm_fd, &read_fds))
+ saClmDispatch(clm_handle, SA_DISPATCH_ALL);
+ if (FD_ISSET(evt_fd, &read_fds))
+ saEvtDispatch(evt_handle, SA_DISPATCH_ALL);
+ }
+
+ return NULL;
+}
+
+static int load_module(void)
+{
+ if (ast_ais_clm_load_module())
+ goto return_error;
+
+ if (ast_ais_evt_load_module())
+ goto evt_failed;
+
+ if (ast_pthread_create_background(&dispatch_thread.id, NULL,
+ dispatch_thread_handler, NULL)) {
+ ast_log(LOG_ERROR, "Error starting AIS dispatch thread.\n");
+ goto dispatch_failed;
+ }
+
+ return AST_MODULE_LOAD_SUCCESS;
+
+dispatch_failed:
+ ast_ais_evt_unload_module();
+evt_failed:
+ ast_ais_clm_unload_module();
+return_error:
+ return AST_MODULE_LOAD_DECLINE;
+}
+
+static int unload_module(void)
+{
+ ast_ais_clm_unload_module();
+ ast_ais_evt_unload_module();
+
+ if (dispatch_thread.id != AST_PTHREADT_NULL) {
+ dispatch_thread.stop = 1;
+ pthread_kill(dispatch_thread.id, SIGURG); /* poke! */
+ pthread_join(dispatch_thread.id, NULL);
+ }
+
+ return 0;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SAForum AIS");