summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.moddir_rules1
-rw-r--r--apps/Makefile2
-rw-r--r--bridges/bridge_native_rtp.c28
-rw-r--r--channels/Makefile8
-rw-r--r--channels/chan_mgcp.c4
-rw-r--r--channels/chan_sip.c2
-rw-r--r--channels/chan_vpb.cc5
-rw-r--r--channels/misdn/Makefile2
-rw-r--r--include/asterisk/res_pjsip_presence_xml.h9
-rw-r--r--include/asterisk/sorcery.h9
-rw-r--r--include/asterisk/threadpool.h16
-rw-r--r--main/astfd.c57
-rw-r--r--main/channel.c6
-rw-r--r--main/channel_internal_api.c11
-rw-r--r--main/config.c1
-rw-r--r--main/pbx.c4
-rw-r--r--main/rtp_engine.c4
-rw-r--r--main/sorcery.c9
-rw-r--r--main/threadpool.c9
-rw-r--r--pbx/Makefile2
-rw-r--r--res/Makefile8
-rw-r--r--res/res_corosync.c4
-rw-r--r--res/res_http_websocket.c23
-rw-r--r--res/res_pjsip.c92
-rw-r--r--res/res_pjsip/pjsip_distributor.c105
-rw-r--r--res/res_pjsip_dialog_info_body_generator.c9
-rw-r--r--res/res_pjsip_mwi.c86
-rw-r--r--res/res_pjsip_nat.c4
-rw-r--r--res/res_pjsip_outbound_registration.c56
-rw-r--r--res/res_pjsip_pidf_body_generator.c11
-rw-r--r--res/res_pjsip_pubsub.c2
-rw-r--r--res/res_pjsip_session.c5
-rw-r--r--res/res_pjsip_t38.c22
-rw-r--r--res/res_pjsip_xpidf_body_generator.c9
-rw-r--r--res/res_rtp_asterisk.c208
-rw-r--r--res/res_sorcery_memory_cache.c2572
-rw-r--r--res/res_sorcery_realtime.c2
-rw-r--r--res/res_timing_kqueue.c4
-rw-r--r--res/res_timing_timerfd.c5
-rw-r--r--tests/test_sorcery_memory_cache_thrash.c618
40 files changed, 3810 insertions, 224 deletions
diff --git a/Makefile.moddir_rules b/Makefile.moddir_rules
index 8d8351678..7f1c8c843 100644
--- a/Makefile.moddir_rules
+++ b/Makefile.moddir_rules
@@ -118,6 +118,7 @@ clean::
rm -f *.so *.o *.oo *.eo *.i *.ii
rm -f .*.d
rm -f *.s *.i
+ rm -f *.gcda *.gcno
rm -f modules.link
install:: all
diff --git a/apps/Makefile b/apps/Makefile
index 1dfe8c838..1e8be04d4 100644
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -28,7 +28,7 @@ all: _all
include $(ASTTOPDIR)/Makefile.moddir_rules
clean::
- rm -f confbridge/*.o confbridge/*.i
+ rm -f confbridge/*.o confbridge/*.i confbridge/*.gcda confbridge/*.gcno
$(if $(filter app_confbridge,$(EMBEDDED_MODS)),modules.link,app_confbridge.so): $(subst .c,.o,$(wildcard confbridge/*.c))
$(subst .c,.o,$(wildcard confbridge/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,app_confbridge)
diff --git a/bridges/bridge_native_rtp.c b/bridges/bridge_native_rtp.c
index d5652bc78..c226dbddf 100644
--- a/bridges/bridge_native_rtp.c
+++ b/bridges/bridge_native_rtp.c
@@ -50,6 +50,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct native_rtp_bridge_data {
/*! \brief Framehook used to intercept certain control frames */
int id;
+ /*! \brief Set when this framehook has been detached */
+ unsigned int detached;
};
/*! \brief Internal helper function which gets all RTP information (glue and instances) relating to the given channels */
@@ -261,6 +263,7 @@ static void native_rtp_bridge_stop(struct ast_bridge *bridge, struct ast_channel
static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
{
RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+ struct native_rtp_bridge_data *native_data = data;
if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
return f;
@@ -272,13 +275,22 @@ static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct a
/* native_rtp_bridge_start/stop are not being called from bridging
core so we need to lock the bridge prior to calling these functions
Unfortunately that means unlocking the channel, but as it
- should not be modified this should be okay...hopefully */
+ should not be modified this should be okay... hopefully...
+ unless this channel is being moved around right now and is in
+ the process of having this framehook removed (which is fine). To
+ ensure we then don't stop or start when we shouldn't we consult
+ the data provided. If this framehook has been detached then the
+ detached variable will be set. This is safe to check as it is only
+ manipulated with the bridge lock held. */
ast_channel_unlock(chan);
ast_bridge_lock(bridge);
- if (f->subclass.integer == AST_CONTROL_HOLD) {
- native_rtp_bridge_stop(bridge, chan);
- } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) || (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
- native_rtp_bridge_start(bridge, chan);
+ if (!native_data->detached) {
+ if (f->subclass.integer == AST_CONTROL_HOLD) {
+ native_rtp_bridge_stop(bridge, chan);
+ } else if ((f->subclass.integer == AST_CONTROL_UNHOLD) ||
+ (f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
+ native_rtp_bridge_start(bridge, chan);
+ }
}
ast_bridge_unlock(bridge);
ast_channel_lock(chan);
@@ -403,6 +415,7 @@ static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_
static struct ast_framehook_interface hook = {
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = native_rtp_framehook,
+ .destroy_cb = __ao2_cleanup,
.consume_cb = native_rtp_framehook_consume,
.disable_inheritance = 1,
};
@@ -412,10 +425,12 @@ static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_
}
ast_channel_lock(bridge_channel->chan);
+ hook.data = ao2_bump(data);
data->id = ast_framehook_attach(bridge_channel->chan, &hook);
ast_channel_unlock(bridge_channel->chan);
if (data->id < 0) {
- ao2_cleanup(data);
+ /* We need to drop both the reference we hold, and the one the framehook would hold */
+ ao2_ref(data, -2);
return -1;
}
@@ -435,6 +450,7 @@ static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge
ast_channel_lock(bridge_channel->chan);
ast_framehook_detach(bridge_channel->chan, data->id);
+ data->detached = 1;
ast_channel_unlock(bridge_channel->chan);
bridge_channel->tech_pvt = NULL;
}
diff --git a/channels/Makefile b/channels/Makefile
index 1f4cff4c7..d1b895edd 100644
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -25,10 +25,10 @@ endif
clean::
$(MAKE) -C misdn clean
- rm -f dahdi/*.o dahdi/*.i
- rm -f sip/*.o sip/*.i
- rm -f iax2/*.o iax2/*.i
- rm -f pjsip/*.o pjsip/*.i
+ rm -f dahdi/*.o dahdi/*.i dahdi/*.gcda dahdi/*.gcno
+ rm -f sip/*.o sip/*.i sip/*.gcda sip/*.gcno
+ rm -f iax2/*.o iax2/*.i iax2/*.gcda iax2/*.gcno
+ rm -f pjsip/*.o pjsip/*.i pjsip/*.gcda pjsip/*.gcno
$(if $(filter chan_iax2,$(EMBEDDED_MODS)),modules.link,chan_iax2.so): $(subst .c,.o,$(wildcard iax2/*.c))
$(subst .c,.o,$(wildcard iax2/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,chan_iax2)
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index b75cd43af..030df89c7 100644
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -4995,7 +4995,9 @@ static int unload_module(void)
return -1;
}
- close(mgcpsock);
+ if (mgcpsock > -1) {
+ close(mgcpsock);
+ }
ast_rtp_glue_unregister(&mgcp_rtp_glue);
ast_cli_unregister_multiple(cli_mgcp, sizeof(cli_mgcp) / sizeof(struct ast_cli_entry));
ast_sched_context_destroy(sched);
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 30cb20de8..1838bdaad 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -24932,10 +24932,12 @@ static int handle_invite_replaces(struct sip_pvt *p, struct sip_request *req,
if (ast_bridge_impart(bridge, c, replaces_chan, NULL,
AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
ast_hangup(c);
+ ast_channel_unref(c);
}
} else {
ast_channel_move(replaces_chan, c);
ast_hangup(c);
+ ast_channel_unref(c);
}
sip_pvt_lock(p);
return 0;
diff --git a/channels/chan_vpb.cc b/channels/chan_vpb.cc
index 181e50aee..f1ed39217 100644
--- a/channels/chan_vpb.cc
+++ b/channels/chan_vpb.cc
@@ -2626,14 +2626,13 @@ static int unload_module(void)
if (bridges) {
ast_mutex_lock(&bridge_lock);
- memset(bridges, 0, sizeof bridges);
- ast_mutex_unlock(&bridge_lock);
- ast_mutex_destroy(&bridge_lock);
for (int i = 0; i < max_bridges; i++) {
ast_mutex_destroy(&bridges[i].lock);
ast_cond_destroy(&bridges[i].cond);
}
ast_free(bridges);
+ bridges = NULL;
+ ast_mutex_unlock(&bridge_lock);
}
ao2_cleanup(vpb_tech.capabilities);
diff --git a/channels/misdn/Makefile b/channels/misdn/Makefile
index 194bef5ae..96d5a2a3d 100644
--- a/channels/misdn/Makefile
+++ b/channels/misdn/Makefile
@@ -14,4 +14,4 @@ portinfo: portinfo.o
$(CC) -o $@ $^ -lisdnnet -lmISDN -lpthread
clean:
- rm -rf *.a *.o *.so portinfo *.i
+ rm -rf *.a *.o *.so portinfo *.i *.gcda *.gcno
diff --git a/include/asterisk/res_pjsip_presence_xml.h b/include/asterisk/res_pjsip_presence_xml.h
index add5f8918..deed0901e 100644
--- a/include/asterisk/res_pjsip_presence_xml.h
+++ b/include/asterisk/res_pjsip_presence_xml.h
@@ -17,14 +17,15 @@
*/
/*!
- * \brief The length of the XML prolog when printing
- * presence or other XML in PJSIP.
+ * \brief Length of the XML prolog when printing presence or other XML in PJSIP.
*
* When calling any variant of pj_xml_print(), the documentation
* claims that it will return -1 if the provided buffer is not
* large enough. However, if the XML prolog is requested to be
- * printed, then the length of the XML prolog is returned upon
- * failure instead of -1.
+ * printed and the buffer is not large enough, then it will
+ * return -1 only if the buffer is not large enough to hold the
+ * XML prolog or return the length of the XML prolog on failure
+ * instead of -1.
*
* This constant is useful to check against when trying to determine
* if printing XML succeeded or failed.
diff --git a/include/asterisk/sorcery.h b/include/asterisk/sorcery.h
index 30fb0dd02..027ec0058 100644
--- a/include/asterisk/sorcery.h
+++ b/include/asterisk/sorcery.h
@@ -1290,6 +1290,15 @@ struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sor
int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
const char *field_name);
+/*!
+ * \brief Get the module that has opened the provided sorcery instance.
+ *
+ * \param sorcery The sorcery instance
+ *
+ * \return The module
+ */
+const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery);
+
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
diff --git a/include/asterisk/threadpool.h b/include/asterisk/threadpool.h
index 942d14fc1..75ce0e4e4 100644
--- a/include/asterisk/threadpool.h
+++ b/include/asterisk/threadpool.h
@@ -218,6 +218,22 @@ struct ast_serializer_shutdown_group *ast_serializer_shutdown_group_alloc(void);
int ast_serializer_shutdown_group_join(struct ast_serializer_shutdown_group *shutdown_group, int timeout);
/*!
+ * \brief Get the threadpool serializer currently associated with this thread.
+ * \since 14.0.0
+ *
+ * \note The returned pointer is valid while the serializer
+ * thread is running.
+ *
+ * \note Use ao2_ref() on serializer if you are going to keep it
+ * for another thread. To unref it you must then use
+ * ast_taskprocessor_unreference().
+ *
+ * \retval serializer on success.
+ * \retval NULL on error or no serializer associated with the thread.
+ */
+struct ast_taskprocessor *ast_threadpool_serializer_get_current(void);
+
+/*!
* \brief Serialized execution of tasks within a \ref ast_threadpool.
*
* \since 12.0.0
diff --git a/main/astfd.c b/main/astfd.c
index d9119c968..d2cb73a6b 100644
--- a/main/astfd.c
+++ b/main/astfd.c
@@ -48,19 +48,24 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/unaligned.h"
static struct fdleaks {
- char file[40];
+ const char *callname;
int line;
+ unsigned int isopen:1;
+ char file[40];
char function[25];
- char callname[10];
char callargs[60];
- unsigned int isopen:1;
} fdleaks[1024] = { { "", }, };
+/* COPY does ast_copy_string(dst, src, sizeof(dst)), except:
+ * - if it doesn't fit, it copies the value after the slash
+ * (possibly truncated)
+ * - if there is no slash, it copies the value with the head
+ * truncated */
#define COPY(dst, src) \
do { \
int dlen = sizeof(dst), slen = strlen(src); \
if (slen + 1 > dlen) { \
- char *slash = strrchr(src, '/'); \
+ const char *slash = strrchr(src, '/'); \
if (slash) { \
ast_copy_string(dst, slash + 1, dlen); \
} else { \
@@ -72,12 +77,15 @@ static struct fdleaks {
} while (0)
#define STORE_COMMON(offset, name, ...) \
- COPY(fdleaks[offset].file, file); \
- fdleaks[offset].line = line; \
- COPY(fdleaks[offset].function, func); \
- strcpy(fdleaks[offset].callname, name); \
- snprintf(fdleaks[offset].callargs, sizeof(fdleaks[offset].callargs), __VA_ARGS__); \
- fdleaks[offset].isopen = 1;
+ do { \
+ struct fdleaks *tmp = &fdleaks[offset]; \
+ COPY(tmp->file, file); \
+ tmp->line = line; \
+ COPY(tmp->function, func); \
+ tmp->callname = name; \
+ snprintf(tmp->callargs, sizeof(tmp->callargs), __VA_ARGS__); \
+ tmp->isopen = 1; \
+ } while (0)
#undef open
int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...)
@@ -91,7 +99,7 @@ int __ast_fdleak_open(const char *file, int line, const char *func, const char *
mode = va_arg(ap, int);
va_end(ap);
res = open(path, flags, mode);
- if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
+ if (res > -1 && res < ARRAY_LEN(fdleaks)) {
char sflags[80];
snprintf(sflags, sizeof(sflags), "O_CREAT%s%s%s%s%s%s%s%s",
flags & O_APPEND ? "|O_APPEND" : "",
@@ -115,7 +123,7 @@ int __ast_fdleak_open(const char *file, int line, const char *func, const char *
}
} else {
res = open(path, flags);
- if (res > -1 && res < (sizeof(fdleaks) / sizeof(fdleaks[0]))) {
+ if (res > -1 && res < ARRAY_LEN(fdleaks)) {
STORE_COMMON(res, "open", "\"%s\",%d", path, flags);
}
}
@@ -130,7 +138,9 @@ int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func)
return res;
}
for (i = 0; i < 2; i++) {
- STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
+ if (fds[i] > -1 && fds[i] < ARRAY_LEN(fdleaks)) {
+ STORE_COMMON(fds[i], "pipe", "{%d,%d}", fds[0], fds[1]);
+ }
}
return 0;
}
@@ -141,7 +151,7 @@ int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, in
char sdomain[20], stype[20], *sproto = NULL;
struct protoent *pe;
int res = socket(domain, type, protocol);
- if (res < 0 || res > 1023) {
+ if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
return res;
}
@@ -183,7 +193,7 @@ int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, in
int __ast_fdleak_close(int fd)
{
int res = close(fd);
- if (!res && fd > -1 && fd < 1024) {
+ if (!res && fd > -1 && fd < ARRAY_LEN(fdleaks)) {
fdleaks[fd].isopen = 0;
}
return res;
@@ -198,7 +208,9 @@ FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, i
return res;
}
fd = fileno(res);
- STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
+ if (fd > -1 && fd < ARRAY_LEN(fdleaks)) {
+ STORE_COMMON(fd, "fopen", "\"%s\",\"%s\"", path, mode);
+ }
return res;
}
@@ -211,7 +223,7 @@ int __ast_fdleak_fclose(FILE *ptr)
}
fd = fileno(ptr);
- if ((res = fclose(ptr)) || fd < 0 || fd > 1023) {
+ if ((res = fclose(ptr)) || fd < 0 || fd >= ARRAY_LEN(fdleaks)) {
return res;
}
fdleaks[fd].isopen = 0;
@@ -222,10 +234,13 @@ int __ast_fdleak_fclose(FILE *ptr)
int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func)
{
int res = dup2(oldfd, newfd);
- if (res < 0 || res > 1023) {
+ if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
return res;
}
- STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd);
+ /* On success, newfd will be closed automatically if it was already
+ * open. We don't need to mention anything about that, we're updating
+ * the value anway. */
+ STORE_COMMON(res, "dup2", "%d,%d", oldfd, newfd); /* res == newfd */
return res;
}
@@ -233,7 +248,7 @@ int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const ch
int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func)
{
int res = dup(oldfd);
- if (res < 0 || res > 1023) {
+ if (res < 0 || res >= ARRAY_LEN(fdleaks)) {
return res;
}
STORE_COMMON(res, "dup2", "%d", oldfd);
@@ -263,7 +278,7 @@ static char *handle_show_fd(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
snprintf(line, sizeof(line), "%d/%d", (int) rl.rlim_cur, (int) rl.rlim_max);
}
ast_cli(a->fd, "Current maxfiles: %s\n", line);
- for (i = 0; i < 1024; i++) {
+ for (i = 0; i < ARRAY_LEN(fdleaks); i++) {
if (fdleaks[i].isopen) {
snprintf(line, sizeof(line), "%d", fdleaks[i].line);
ast_cli(a->fd, "%5d %15s:%-7.7s (%-25s): %s(%s)\n", i, fdleaks[i].file, line, fdleaks[i].function, fdleaks[i].callname, fdleaks[i].callargs);
diff --git a/main/channel.c b/main/channel.c
index fa03f65e6..57523d71a 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -3905,11 +3905,7 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
switch (f->frametype) {
case AST_FRAME_CONTROL:
if (f->subclass.integer == AST_CONTROL_ANSWER) {
- if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
- ast_debug(1, "Ignoring answer on an inbound call!\n");
- ast_frfree(f);
- f = &ast_null_frame;
- } else if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
+ if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
ast_debug(1, "Dropping duplicate answer!\n");
ast_frfree(f);
f = &ast_null_frame;
diff --git a/main/channel_internal_api.c b/main/channel_internal_api.c
index 835b9ce37..db5f3c055 100644
--- a/main/channel_internal_api.c
+++ b/main/channel_internal_api.c
@@ -1210,7 +1210,14 @@ void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_nam
int ast_channel_alert_write(struct ast_channel *chan)
{
char blah = 0x7F;
- return ast_channel_alert_writable(chan) && write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
+
+ if (!ast_channel_alert_writable(chan)) {
+ errno = EBADF;
+ return 0;
+ }
+ /* preset errno in case returned size does not match */
+ errno = EPIPE;
+ return write(chan->alertpipe[1], &blah, sizeof(blah)) != sizeof(blah);
}
ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
@@ -1261,9 +1268,11 @@ void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
{
if (ast_channel_internal_alert_readable(chan)) {
close(chan->alertpipe[0]);
+ chan->alertpipe[0] = -1;
}
if (ast_channel_alert_writable(chan)) {
close(chan->alertpipe[1]);
+ chan->alertpipe[1] = -1;
}
}
diff --git a/main/config.c b/main/config.c
index f59e12164..7484b66ba 100644
--- a/main/config.c
+++ b/main/config.c
@@ -2869,6 +2869,7 @@ int ast_realtime_is_mapping_defined(const char *family)
return 1;
}
}
+ ast_debug(5, "Failed to find a realtime mapping for %s\n", family);
return 0;
}
diff --git a/main/pbx.c b/main/pbx.c
index 7686b4b5d..8f5ee646c 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -1119,7 +1119,7 @@ static int hintdevice_cmp_multiple(void *obj, void *arg, int flags)
right_key = right->hintdevice;
/* Fall through */
case OBJ_SEARCH_KEY:
- cmp = strcmp(left->hintdevice, right_key);
+ cmp = strcasecmp(left->hintdevice, right_key);
break;
case OBJ_SEARCH_PARTIAL_KEY:
/*
@@ -1143,7 +1143,7 @@ static int hintdevice_remove_cb(void *obj, void *arg, void *data, int flags)
char *device = arg;
struct ast_hint *hint = data;
- if (!strcmp(candidate->hintdevice, device)
+ if (!strcasecmp(candidate->hintdevice, device)
&& candidate->hint == hint) {
return CMP_MATCH;
}
diff --git a/main/rtp_engine.c b/main/rtp_engine.c
index e09dcb88c..2d61c89b6 100644
--- a/main/rtp_engine.c
+++ b/main/rtp_engine.c
@@ -1798,7 +1798,9 @@ int ast_rtp_engine_unload_format(struct ast_format *format)
rtp_engine_mime_type_cleanup(x);
continue;
}
- ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
+ if (x != y) {
+ ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
+ }
y++;
}
mime_types_len = y;
diff --git a/main/sorcery.c b/main/sorcery.c
index 063e8c4b0..8e48403d9 100644
--- a/main/sorcery.c
+++ b/main/sorcery.c
@@ -991,7 +991,11 @@ enum ast_sorcery_apply_result __ast_sorcery_insert_wizard_mapping(struct ast_sor
}
}
+ ast_debug(5, "Calling wizard %s open callback on object type %s\n",
+ name, object_type->name);
if (wizard->callbacks.open && !(object_wizard->data = wizard->callbacks.open(data))) {
+ ast_log(LOG_WARNING, "Wizard '%s' failed to open mapping for object type '%s' with data: %s\n",
+ name, object_type->name, S_OR(data, ""));
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
return AST_SORCERY_APPLY_FAIL;
}
@@ -2352,3 +2356,8 @@ int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type
ao2_cleanup(object_field);
return res;
}
+
+const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery)
+{
+ return sorcery->module_name;
+}
diff --git a/main/threadpool.c b/main/threadpool.c
index 479938959..d97a7adb8 100644
--- a/main/threadpool.c
+++ b/main/threadpool.c
@@ -1259,13 +1259,17 @@ static struct serializer *serializer_create(struct ast_threadpool *pool,
return ser;
}
+AST_THREADSTORAGE_RAW(current_serializer);
+
static int execute_tasks(void *data)
{
struct ast_taskprocessor *tps = data;
+ ast_threadstorage_set_ptr(&current_serializer, tps);
while (ast_taskprocessor_execute(tps)) {
/* No-op */
}
+ ast_threadstorage_set_ptr(&current_serializer, NULL);
ast_taskprocessor_unreference(tps);
return 0;
@@ -1305,6 +1309,11 @@ static struct ast_taskprocessor_listener_callbacks serializer_tps_listener_callb
.shutdown = serializer_shutdown,
};
+struct ast_taskprocessor *ast_threadpool_serializer_get_current(void)
+{
+ return ast_threadstorage_get_ptr(&current_serializer);
+}
+
struct ast_taskprocessor *ast_threadpool_serializer_group(const char *name,
struct ast_threadpool *pool, struct ast_serializer_shutdown_group *shutdown_group)
{
diff --git a/pbx/Makefile b/pbx/Makefile
index 0afc4bcad..a031cdfa5 100644
--- a/pbx/Makefile
+++ b/pbx/Makefile
@@ -24,7 +24,7 @@ ifneq ($(findstring $(OSARCH), mingw32 cygwin ),)
endif
clean::
- rm -f ael/*.o ael/*.i
+ rm -f ael/*.o ael/*.i ael/*.gcda ael/*.gcno
dundi-parser.o: dundi-parser.h
dundi-parser.o: _ASTCFLAGS+=-I.
diff --git a/res/Makefile b/res/Makefile
index b98fb8e42..dbab9990c 100644
--- a/res/Makefile
+++ b/res/Makefile
@@ -76,9 +76,11 @@ endif
ael/pval.o: ael/pval.c
clean::
- rm -f snmp/*.[oi] ael/*.[oi] ais/*.[oi] ari/*.[oi]
- rm -f res_pjsip/*.[oi] stasis/*.[oi]
- rm -f parking/*.o parking/*.i stasis_recording/*.[oi]
+ rm -f snmp/*.o snmp/*.i ael/*.o ael/*.i ais/*.o ais/*.i snmp/*.gcda snmp/*.gcno ael/*.gcda ael/*.gcno
+ rm -f res_pjsip/*.[oi] res_pjsip/*.gcda res_pjsip/*.gcno
+ rm -f stasis/*.[oi] stasis/*.gcda stasis/*.gcno
+ rm -f parking/*.[oi] parking/*.gcda parking/*.gcno
+ rm -f stasis_recording/*.[oi] stasis_recording/*.gcda stasis_recording/*.gcno
$(if $(filter res_parking,$(EMBEDDED_MODS)),modules.link,res_parking.so): $(subst .c,.o,$(wildcard parking/*.c))
$(subst .c,.o,$(wildcard parking/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_parking)
diff --git a/res/res_corosync.c b/res/res_corosync.c
index e2b0596d1..72da3f129 100644
--- a/res/res_corosync.c
+++ b/res/res_corosync.c
@@ -863,7 +863,6 @@ static void cleanup_module(void)
static int load_module(void)
{
cs_error_t cs_err;
- enum ast_module_load_result res = AST_MODULE_LOAD_FAILURE;
struct cpg_name name;
corosync_aggregate_topic = stasis_topic_create("corosync_aggregate_topic");
@@ -885,7 +884,6 @@ static int load_module(void)
if (load_config(0)) {
/* simply not configured is not a fatal error */
- res = AST_MODULE_LOAD_DECLINE;
goto failed;
}
@@ -926,7 +924,7 @@ static int load_module(void)
failed:
cleanup_module();
- return res;
+ return AST_MODULE_LOAD_DECLINE;
}
static int unload_module(void)
diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c
index 9e8b680a9..1f1f77ce5 100644
--- a/res/res_http_websocket.c
+++ b/res/res_http_websocket.c
@@ -781,13 +781,6 @@ int AST_OPTIONAL_API_NAME(ast_websocket_uri_cb)(struct ast_tcptls_session_instan
return 0;
}
- fprintf(ser->f, "HTTP/1.1 101 Switching Protocols\r\n"
- "Upgrade: %s\r\n"
- "Connection: Upgrade\r\n"
- "Sec-WebSocket-Accept: %s\r\n",
- upgrade,
- websocket_combine_key(key, base64, sizeof(base64)));
-
/* RFC 6455, Section 4.1:
*
* 6. If the response includes a |Sec-WebSocket-Protocol| header
@@ -798,11 +791,23 @@ int AST_OPTIONAL_API_NAME(ast_websocket_uri_cb)(struct ast_tcptls_session_instan
* Connection_.
*/
if (protocol) {
- fprintf(ser->f, "Sec-WebSocket-Protocol: %s\r\n",
+ fprintf(ser->f, "HTTP/1.1 101 Switching Protocols\r\n"
+ "Upgrade: %s\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: %s\r\n"
+ "Sec-WebSocket-Protocol: %s\r\n\r\n",
+ upgrade,
+ websocket_combine_key(key, base64, sizeof(base64)),
protocol);
+ } else {
+ fprintf(ser->f, "HTTP/1.1 101 Switching Protocols\r\n"
+ "Upgrade: %s\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: %s\r\n\r\n",
+ upgrade,
+ websocket_combine_key(key, base64, sizeof(base64)));
}
- fprintf(ser->f, "\r\n");
fflush(ser->f);
} else {
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index e92de51bb..658a55e88 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -1867,6 +1867,15 @@
#define MOD_DATA_CONTACT "contact"
+/*! Number of serializers in pool if one not supplied. */
+#define SERIALIZER_POOL_SIZE 8
+
+/*! Next serializer pool index to use. */
+static int serializer_pool_pos;
+
+/*! Pool of serializers to use if not supplied. */
+static struct ast_taskprocessor *serializer_pool[SERIALIZER_POOL_SIZE];
+
static pjsip_endpoint *ast_pjsip_endpoint;
static struct ast_threadpool *sip_threadpool;
@@ -3341,8 +3350,62 @@ struct ast_taskprocessor *ast_sip_create_serializer(void)
return ast_sip_create_serializer_group(NULL);
}
+/*!
+ * \internal
+ * \brief Shutdown the serializers in the default pool.
+ * \since 14.0.0
+ *
+ * \return Nothing
+ */
+static void serializer_pool_shutdown(void)
+{
+ int idx;
+
+ for (idx = 0; idx < SERIALIZER_POOL_SIZE; ++idx) {
+ ast_taskprocessor_unreference(serializer_pool[idx]);
+ serializer_pool[idx] = NULL;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Setup the serializers in the default pool.
+ * \since 14.0.0
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+static int serializer_pool_setup(void)
+{
+ int idx;
+
+ for (idx = 0; idx < SERIALIZER_POOL_SIZE; ++idx) {
+ serializer_pool[idx] = ast_sip_create_serializer();
+ if (!serializer_pool[idx]) {
+ serializer_pool_shutdown();
+ return -1;
+ }
+ }
+ return 0;
+}
+
int ast_sip_push_task(struct ast_taskprocessor *serializer, int (*sip_task)(void *), void *task_data)
{
+ if (!serializer) {
+ unsigned int pos;
+
+ /*
+ * Pick a serializer to use from the pool.
+ *
+ * Note: We don't care about any reentrancy behavior
+ * when incrementing serializer_pool_pos. If it gets
+ * incorrectly incremented it doesn't matter.
+ */
+ pos = serializer_pool_pos++;
+ pos %= SERIALIZER_POOL_SIZE;
+ serializer = serializer_pool[pos];
+ }
+
if (serializer) {
return ast_taskprocessor_push(serializer, sip_task, task_data);
} else {
@@ -3395,18 +3458,10 @@ int ast_sip_push_task_synchronous(struct ast_taskprocessor *serializer, int (*si
std.task = sip_task;
std.task_data = task_data;
- if (serializer) {
- if (ast_taskprocessor_push(serializer, sync_task, &std)) {
- ast_mutex_destroy(&std.lock);
- ast_cond_destroy(&std.cond);
- return -1;
- }
- } else {
- if (ast_threadpool_push(sip_threadpool, sync_task, &std)) {
- ast_mutex_destroy(&std.lock);
- ast_cond_destroy(&std.cond);
- return -1;
- }
+ if (ast_sip_push_task(serializer, sync_task, &std)) {
+ ast_mutex_destroy(&std.lock);
+ ast_cond_destroy(&std.cond);
+ return -1;
}
ast_mutex_lock(&std.lock);
@@ -3697,6 +3752,18 @@ static int load_module(void)
return AST_MODULE_LOAD_DECLINE;
}
+ if (serializer_pool_setup()) {
+ ast_log(LOG_ERROR, "Failed to create SIP serializer pool. Aborting load\n");
+ ast_threadpool_shutdown(sip_threadpool);
+ ast_sip_destroy_system();
+ pj_pool_release(memory_pool);
+ memory_pool = NULL;
+ pjsip_endpt_destroy(ast_pjsip_endpoint);
+ ast_pjsip_endpoint = NULL;
+ pj_caching_pool_destroy(&caching_pool);
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
ast_sip_initialize_dns();
pjsip_tsx_layer_init_module(ast_pjsip_endpoint);
@@ -3826,6 +3893,7 @@ static int unload_module(void)
*/
ast_sip_push_task_synchronous(NULL, unload_pjsip, NULL);
+ serializer_pool_shutdown();
ast_threadpool_shutdown(sip_threadpool);
ast_sip_destroy_cli();
diff --git a/res/res_pjsip/pjsip_distributor.c b/res/res_pjsip/pjsip_distributor.c
index e32f02833..9b052603a 100644
--- a/res/res_pjsip/pjsip_distributor.c
+++ b/res/res_pjsip/pjsip_distributor.c
@@ -22,22 +22,106 @@
#include "asterisk/res_pjsip.h"
#include "include/res_pjsip_private.h"
+#include "asterisk/taskprocessor.h"
+#include "asterisk/threadpool.h"
static int distribute(void *data);
static pj_bool_t distributor(pjsip_rx_data *rdata);
+static pj_status_t record_serializer(pjsip_tx_data *tdata);
static pjsip_module distributor_mod = {
.name = {"Request Distributor", 19},
.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 6,
+ .on_tx_request = record_serializer,
.on_rx_request = distributor,
.on_rx_response = distributor,
};
+/*!
+ * \internal
+ * \brief Record the task's serializer name on the tdata structure.
+ * \since 14.0.0
+ *
+ * \param tdata The outgoing message.
+ *
+ * \retval PJ_SUCCESS.
+ */
+static pj_status_t record_serializer(pjsip_tx_data *tdata)
+{
+ struct ast_taskprocessor *serializer;
+
+ serializer = ast_threadpool_serializer_get_current();
+ if (serializer) {
+ const char *name;
+
+ name = ast_taskprocessor_name(serializer);
+ if (!ast_strlen_zero(name)
+ && (!tdata->mod_data[distributor_mod.id]
+ || strcmp(tdata->mod_data[distributor_mod.id], name))) {
+ char *tdata_name;
+
+ /* The serializer in use changed. */
+ tdata_name = pj_pool_alloc(tdata->pool, strlen(name) + 1);
+ strcpy(tdata_name, name);/* Safe */
+
+ tdata->mod_data[distributor_mod.id] = tdata_name;
+ }
+ }
+
+ return PJ_SUCCESS;
+}
+
+/*!
+ * \internal
+ * \brief Find the request tdata to get the serializer it used.
+ * \since 14.0.0
+ *
+ * \param rdata The incoming message.
+ *
+ * \retval serializer on success.
+ * \retval NULL on error or could not find the serializer.
+ */
+static struct ast_taskprocessor *find_request_serializer(pjsip_rx_data *rdata)
+{
+ struct ast_taskprocessor *serializer = NULL;
+ pj_str_t tsx_key;
+ pjsip_transaction *tsx;
+
+ pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAC,
+ &rdata->msg_info.cseq->method, rdata);
+
+ tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
+ if (!tsx) {
+ ast_debug(1, "Could not find %.*s transaction for %d response.\n",
+ (int) pj_strlen(&rdata->msg_info.cseq->method.name),
+ pj_strbuf(&rdata->msg_info.cseq->method.name),
+ rdata->msg_info.msg->line.status.code);
+ return NULL;
+ }
+
+ if (tsx->last_tx) {
+ const char *serializer_name;
+
+ serializer_name = tsx->last_tx->mod_data[distributor_mod.id];
+ if (!ast_strlen_zero(serializer_name)) {
+ serializer = ast_taskprocessor_get(serializer_name, TPS_REF_IF_EXISTS);
+ }
+ }
+
+#ifdef HAVE_PJ_TRANSACTION_GRP_LOCK
+ pj_grp_lock_release(tsx->grp_lock);
+#else
+ pj_mutex_unlock(tsx->mutex);
+#endif
+
+ return serializer;
+}
+
/*! Dialog-specific information the distributor uses */
struct distributor_dialog_data {
- /* Serializer to distribute tasks to for this dialog */
+ /*! Serializer to distribute tasks to for this dialog */
struct ast_taskprocessor *serializer;
- /* Endpoint associated with this dialog */
+ /*! Endpoint associated with this dialog */
struct ast_sip_endpoint *endpoint;
};
@@ -167,6 +251,7 @@ static pj_bool_t distributor(pjsip_rx_data *rdata)
pjsip_dialog *dlg = find_dialog(rdata);
struct distributor_dialog_data *dist = NULL;
struct ast_taskprocessor *serializer = NULL;
+ struct ast_taskprocessor *req_serializer = NULL;
pjsip_rx_data *clone;
if (dlg) {
@@ -176,11 +261,16 @@ static pj_bool_t distributor(pjsip_rx_data *rdata)
}
}
- if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG && (
- !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) ||
- !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method)) &&
- !serializer) {
- pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 481, NULL, NULL, NULL);
+ if (serializer) {
+ /* We have a serializer so we know where to send the message. */
+ } else if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG) {
+ req_serializer = find_request_serializer(rdata);
+ serializer = req_serializer;
+ } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)
+ || !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method)) {
+ /* We have a BYE or CANCEL request without a serializer. */
+ pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
+ PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, NULL, NULL, NULL);
goto end;
}
@@ -196,6 +286,7 @@ end:
if (dlg) {
pjsip_dlg_dec_lock(dlg);
}
+ ast_taskprocessor_unreference(req_serializer);
return PJ_TRUE;
}
diff --git a/res/res_pjsip_dialog_info_body_generator.c b/res/res_pjsip_dialog_info_body_generator.c
index d9725f4c5..48ac60f98 100644
--- a/res/res_pjsip_dialog_info_body_generator.c
+++ b/res/res_pjsip_dialog_info_body_generator.c
@@ -163,14 +163,13 @@ static void dialog_info_to_string(void *body, struct ast_str **str)
int size;
do {
- size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str), PJ_TRUE);
- if (size == AST_PJSIP_XML_PROLOG_LEN) {
+ size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
+ if (size <= AST_PJSIP_XML_PROLOG_LEN) {
ast_str_make_space(str, ast_str_size(*str) * 2);
++growths;
}
- } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
-
- if (size == AST_PJSIP_XML_PROLOG_LEN) {
+ } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
+ if (size <= AST_PJSIP_XML_PROLOG_LEN) {
ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
return;
}
diff --git a/res/res_pjsip_mwi.c b/res/res_pjsip_mwi.c
index eae029376..06587daf7 100644
--- a/res/res_pjsip_mwi.c
+++ b/res/res_pjsip_mwi.c
@@ -138,9 +138,17 @@ static struct mwi_stasis_subscription *mwi_stasis_subscription_alloc(const char
/* Safe strcpy */
strcpy(mwi_stasis_sub->mailbox, mailbox);
+
+ ast_debug(3, "Creating stasis MWI subscription to mailbox %s for endpoint %s\n",
+ mailbox, mwi_sub->id);
ao2_ref(mwi_sub, +1);
- ast_debug(3, "Creating stasis MWI subscription to mailbox %s for endpoint %s\n", mailbox, mwi_sub->id);
mwi_stasis_sub->stasis_sub = stasis_subscribe_pool(topic, mwi_stasis_cb, mwi_sub);
+ if (!mwi_stasis_sub->stasis_sub) {
+ /* Failed to subscribe. */
+ ao2_ref(mwi_stasis_sub, -1);
+ ao2_ref(mwi_sub, -1);
+ mwi_stasis_sub = NULL;
+ }
return mwi_stasis_sub;
}
@@ -491,25 +499,41 @@ static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
mwi_sub = mwi_datastore->data;
ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
+ ast_sip_subscription_remove_datastore(sub, MWI_DATASTORE);
ao2_ref(mwi_datastore, -1);
}
-static struct ast_datastore_info mwi_ds_info = { };
+static void mwi_ds_destroy(void *data)
+{
+ struct mwi_subscription *sub = data;
+
+ ao2_ref(sub, -1);
+}
+
+static struct ast_datastore_info mwi_ds_info = {
+ .destroy = mwi_ds_destroy,
+};
static int add_mwi_datastore(struct mwi_subscription *sub)
{
struct ast_datastore *mwi_datastore;
+ int res;
mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, MWI_DATASTORE);
if (!mwi_datastore) {
return -1;
}
+ ao2_ref(sub, +1);
mwi_datastore->data = sub;
- ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
+ /*
+ * NOTE: Adding the datastore to the subscription creates a ref loop
+ * that must be manually broken.
+ */
+ res = ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
ao2_ref(mwi_datastore, -1);
- return 0;
+ return res;
}
/*!
@@ -621,8 +645,8 @@ static struct mwi_subscription *mwi_create_subscription(
}
if (add_mwi_datastore(sub)) {
- ast_log(LOG_WARNING, "Unable to allocate datastore on MWI "
- "subscription from %s\n", sub->id);
+ ast_log(LOG_WARNING, "Unable to add datastore for MWI subscription to %s\n",
+ sub->id);
ao2_ref(sub, -1);
return NULL;
}
@@ -633,25 +657,26 @@ static struct mwi_subscription *mwi_create_subscription(
static struct mwi_subscription *mwi_subscribe_single(
struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
{
- RAII_VAR(struct ast_sip_aor *, aor,
- ast_sip_location_retrieve_aor(name), ao2_cleanup);
+ struct ast_sip_aor *aor;
struct mwi_subscription *sub;
+ aor = ast_sip_location_retrieve_aor(name);
if (!aor) {
/*! I suppose it's possible for the AOR to disappear on us
* between accepting the subscription and sending the first
* NOTIFY...
*/
- ast_log(LOG_WARNING, "Unable to locate aor %s. MWI "
- "subscription failed.\n", name);
+ ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n",
+ name);
return NULL;
}
- if (!(sub = mwi_create_subscription(endpoint, sip_sub))) {
- return NULL;
+ sub = mwi_create_subscription(endpoint, sip_sub);
+ if (sub) {
+ mwi_on_aor(aor, sub, 0);
}
- mwi_on_aor(aor, sub, 0);
+ ao2_ref(aor, -1);
return sub;
}
@@ -661,7 +686,6 @@ static struct mwi_subscription *mwi_subscribe_all(
struct mwi_subscription *sub;
sub = mwi_create_subscription(endpoint, sip_sub);
-
if (!sub) {
return NULL;
}
@@ -683,16 +707,15 @@ static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
}
aor = ast_sip_location_retrieve_aor(resource);
-
if (!aor) {
- ast_log(LOG_WARNING, "Unable to locate aor %s. MWI "
- "subscription failed.\n", resource);
+ ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n",
+ resource);
return 404;
}
if (ast_strlen_zero(aor->mailboxes)) {
- ast_log(LOG_NOTICE, "AOR %s has no configured mailboxes. "
- "MWI subscription failed\n", resource);
+ ast_log(LOG_NOTICE, "AOR %s has no configured mailboxes. MWI subscription failed.\n",
+ resource);
return 404;
}
@@ -715,12 +738,19 @@ static int mwi_subscription_established(struct ast_sip_subscription *sip_sub)
} else {
sub = mwi_subscribe_single(endpoint, sip_sub, resource);
}
-
if (!sub) {
ao2_cleanup(endpoint);
return -1;
}
+ if (!ao2_container_count(sub->stasis_subs)) {
+ /*
+ * We setup no MWI subscriptions so remove the MWI datastore
+ * to break the ref loop.
+ */
+ ast_sip_subscription_remove_datastore(sip_sub, MWI_DATASTORE);
+ }
+
ao2_cleanup(sub);
ao2_cleanup(endpoint);
return 0;
@@ -752,16 +782,16 @@ static void *mwi_get_notify_data(struct ast_sip_subscription *sub)
static void mwi_subscription_mailboxes_str(struct ao2_container *stasis_subs,
struct ast_str **str)
{
- int num = ao2_container_count(stasis_subs);
-
+ int is_first = 1;
struct mwi_stasis_subscription *node;
struct ao2_iterator i = ao2_iterator_init(stasis_subs, 0);
while ((node = ao2_iterator_next(&i))) {
- if (--num) {
- ast_str_append(str, 0, "%s,", node->mailbox);
- } else {
+ if (is_first) {
+ is_first = 0;
ast_str_append(str, 0, "%s", node->mailbox);
+ } else {
+ ast_str_append(str, 0, ",%s", node->mailbox);
}
ao2_ref(node, -1);
}
@@ -816,7 +846,9 @@ static int serialized_cleanup(void *userdata)
static int send_notify(void *obj, void *arg, int flags)
{
struct mwi_subscription *mwi_sub = obj;
- struct ast_taskprocessor *serializer = mwi_sub->is_solicited ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub) : NULL;
+ struct ast_taskprocessor *serializer = mwi_sub->is_solicited
+ ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub)
+ : NULL;
if (ast_sip_push_task(serializer, serialized_notify, ao2_bump(mwi_sub))) {
ao2_ref(mwi_sub, -1);
@@ -842,6 +874,7 @@ static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
}
}
+/*! \note Called with the unsolicited_mwi conainer lock held. */
static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags)
{
RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
@@ -982,7 +1015,6 @@ static void mwi_contact_added(const void *object)
ao2_lock(unsolicited_mwi);
mwi_subs = ao2_find(unsolicited_mwi, endpoint_id, OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK | OBJ_UNLINK);
-
if (mwi_subs) {
for (; (mwi_sub = ao2_iterator_next(mwi_subs)); ao2_cleanup(mwi_sub)) {
unsubscribe(mwi_sub, NULL, 0);
diff --git a/res/res_pjsip_nat.c b/res/res_pjsip_nat.c
index e47dd542c..c32b71d76 100644
--- a/res/res_pjsip_nat.c
+++ b/res/res_pjsip_nat.c
@@ -63,7 +63,7 @@ static int rewrite_route_set(pjsip_rx_data *rdata, pjsip_dialog *dlg)
if (rr) {
uri = pjsip_uri_get_uri(&rr->name_addr);
rewrite_uri(rdata, uri);
- if (dlg && dlg->route_set.next && !dlg->route_set_frozen) {
+ if (dlg && !pj_list_empty(&dlg->route_set) && !dlg->route_set_frozen) {
pjsip_routing_hdr *route = dlg->route_set.next;
uri = pjsip_uri_get_uri(&route->name_addr);
rewrite_uri(rdata, uri);
@@ -85,7 +85,7 @@ static int rewrite_contact(pjsip_rx_data *rdata, pjsip_dialog *dlg)
rewrite_uri(rdata, uri);
- if (dlg && !dlg->route_set_frozen && (!dlg->remote.contact
+ if (dlg && pj_list_empty(&dlg->route_set) && (!dlg->remote.contact
|| pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI, dlg->remote.contact->uri, contact->uri))) {
dlg->remote.contact = (pjsip_contact_hdr*)pjsip_hdr_clone(dlg->pool, contact);
dlg->target = dlg->remote.contact->uri;
diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c
index 633166cb5..26d4a608e 100644
--- a/res/res_pjsip_outbound_registration.c
+++ b/res/res_pjsip_outbound_registration.c
@@ -34,6 +34,7 @@
#include "asterisk/cli.h"
#include "asterisk/stasis_system.h"
#include "asterisk/threadstorage.h"
+#include "asterisk/threadpool.h"
#include "res_pjsip/include/res_pjsip_private.h"
/*** DOCUMENTATION
@@ -333,6 +334,12 @@ struct sip_outbound_registration_state {
struct sip_outbound_registration_client_state *client_state;
};
+/*! Time needs to be long enough for a transaction to timeout if nothing replies. */
+#define MAX_UNLOAD_TIMEOUT_TIME 35 /* Seconds */
+
+/*! Shutdown group to monitor sip_outbound_registration_client_state serializers. */
+static struct ast_serializer_shutdown_group *shutdown_group;
+
/*! \brief Default number of state container buckets */
#define DEFAULT_STATE_BUCKETS 53
static AO2_GLOBAL_OBJ_STATIC(current_states);
@@ -550,12 +557,15 @@ static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, stru
entry->id = 0;
- ao2_ref(client_state, +1);
+ /*
+ * Transfer client_state reference to serializer task so the
+ * nominal path will not dec the client_state ref in this
+ * pjproject callback thread.
+ */
if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) {
ast_log(LOG_WARNING, "Scheduled outbound registration could not be executed.\n");
ao2_ref(client_state, -1);
}
- ao2_ref(client_state, -1);
}
/*! \brief Helper function which sets up the timer to re-register in a specific amount of time */
@@ -835,7 +845,11 @@ static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *par
}
response->code = param->code;
response->expiration = param->expiration;
- /* Transfer client_state reference to response. */
+ /*
+ * Transfer client_state reference to response so the
+ * nominal path will not dec the client_state ref in this
+ * pjproject callback thread.
+ */
response->client_state = client_state;
ast_debug(1, "Received REGISTER response %d(%.*s)\n",
@@ -854,6 +868,11 @@ static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *par
pjsip_rx_data_clone(param->rdata, 0, &response->rdata);
}
+ /*
+ * Transfer response reference to serializer task so the
+ * nominal path will not dec the response ref in this
+ * pjproject callback thread.
+ */
if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) {
ast_log(LOG_WARNING, "Failed to pass incoming registration response to threadpool\n");
ao2_cleanup(response);
@@ -905,7 +924,7 @@ static struct sip_outbound_registration_state *sip_outbound_registration_state_a
return NULL;
}
- state->client_state->serializer = ast_sip_create_serializer();
+ state->client_state->serializer = ast_sip_create_serializer_group(shutdown_group);
if (!state->client_state->serializer) {
ao2_cleanup(state);
return NULL;
@@ -1235,7 +1254,8 @@ static int sip_outbound_registration_apply(const struct ast_sorcery *sorcery, vo
return -1;
}
- if (ast_sip_push_task_synchronous(NULL, sip_outbound_registration_regc_alloc, new_state)) {
+ if (ast_sip_push_task_synchronous(new_state->client_state->serializer,
+ sip_outbound_registration_regc_alloc, new_state)) {
return -1;
}
@@ -1806,6 +1826,8 @@ static const struct ast_sorcery_instance_observer observer_callbacks_registratio
static int unload_module(void)
{
+ int remaining;
+
ast_manager_unregister("PJSIPShowRegistrationsOutbound");
ast_manager_unregister("PJSIPUnregister");
ast_manager_unregister("PJSIPRegister");
@@ -1823,6 +1845,25 @@ static int unload_module(void)
ao2_global_obj_release(current_states);
+ /* Wait for registration serializers to get destroyed. */
+ ast_debug(2, "Waiting for registration transactions to complete for unload.\n");
+ remaining = ast_serializer_shutdown_group_join(shutdown_group, MAX_UNLOAD_TIMEOUT_TIME);
+ if (remaining) {
+ /*
+ * NOTE: We probably have a sip_outbound_registration_client_state
+ * ref leak if the remaining count cannot reach zero after a few
+ * minutes of trying to unload.
+ */
+ ast_log(LOG_WARNING, "Unload incomplete. Could not stop %d outbound registrations. Try again later.\n",
+ remaining);
+ return -1;
+ }
+
+ ast_debug(2, "Successful shutdown.\n");
+
+ ao2_cleanup(shutdown_group);
+ shutdown_group = NULL;
+
return 0;
}
@@ -1832,6 +1873,11 @@ static int load_module(void)
CHECK_PJSIP_MODULE_LOADED();
+ shutdown_group = ast_serializer_shutdown_group_alloc();
+ if (!shutdown_group) {
+ return AST_MODULE_LOAD_FAILURE;
+ }
+
/* Create outbound registration states container. */
new_states = ao2_container_alloc(DEFAULT_STATE_BUCKETS,
registration_state_hash, registration_state_cmp);
diff --git a/res/res_pjsip_pidf_body_generator.c b/res/res_pjsip_pidf_body_generator.c
index ef0cce599..d3be8c131 100644
--- a/res/res_pjsip_pidf_body_generator.c
+++ b/res/res_pjsip_pidf_body_generator.c
@@ -84,19 +84,18 @@ static int pidf_generate_body_content(void *body, void *data)
static void pidf_to_string(void *body, struct ast_str **str)
{
- int size;
- int growths = 0;
pjpidf_pres *pres = body;
+ int growths = 0;
+ int size;
do {
size = pjpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
- if (size == AST_PJSIP_XML_PROLOG_LEN) {
+ if (size <= AST_PJSIP_XML_PROLOG_LEN) {
ast_str_make_space(str, ast_str_size(*str) * 2);
++growths;
}
- } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
-
- if (size == AST_PJSIP_XML_PROLOG_LEN) {
+ } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
+ if (size <= AST_PJSIP_XML_PROLOG_LEN) {
ast_log(LOG_WARNING, "PIDF body text too large\n");
return;
}
diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c
index c00bc76ee..650f5c5c8 100644
--- a/res/res_pjsip_pubsub.c
+++ b/res/res_pjsip_pubsub.c
@@ -1769,7 +1769,7 @@ static int rlmi_print_body(struct pjsip_msg_body *msg_body, char *buf, pj_size_t
pj_xml_node *rlmi = msg_body->data;
num_printed = pj_xml_print(rlmi, buf, size, PJ_TRUE);
- if (num_printed == AST_PJSIP_XML_PROLOG_LEN) {
+ if (num_printed <= AST_PJSIP_XML_PROLOG_LEN) {
return -1;
}
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index a8f8cba85..8c04a7cfb 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -1039,7 +1039,10 @@ void ast_sip_session_resume_reinvite(struct ast_sip_session *session)
return;
}
- pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(), session->deferred_reinvite, NULL, NULL);
+ if (session->channel) {
+ pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(),
+ session->deferred_reinvite, NULL, NULL);
+ }
pjsip_rx_data_free_cloned(session->deferred_reinvite);
session->deferred_reinvite = NULL;
}
diff --git a/res/res_pjsip_t38.c b/res/res_pjsip_t38.c
index 06a73cc11..5a2357dfc 100644
--- a/res/res_pjsip_t38.c
+++ b/res/res_pjsip_t38.c
@@ -135,10 +135,13 @@ static void t38_change_state(struct ast_sip_session *session, struct ast_sip_ses
}
session->t38state = new_state;
- ast_debug(2, "T.38 state changed to '%u' from '%u' on channel '%s'\n", new_state, old_state, ast_channel_name(session->channel));
+ ast_debug(2, "T.38 state changed to '%u' from '%u' on channel '%s'\n",
+ new_state, old_state,
+ session->channel ? ast_channel_name(session->channel) : "<gone>");
if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &state->timer)) {
- ast_debug(2, "Automatic T.38 rejection on channel '%s' terminated\n", ast_channel_name(session->channel));
+ ast_debug(2, "Automatic T.38 rejection on channel '%s' terminated\n",
+ session->channel ? ast_channel_name(session->channel) : "<gone>");
ao2_ref(session, -1);
}
@@ -198,7 +201,8 @@ static int t38_automatic_reject(void *obj)
return 0;
}
- ast_debug(2, "Automatically rejecting T.38 request on channel '%s'\n", ast_channel_name(session->channel));
+ ast_debug(2, "Automatically rejecting T.38 request on channel '%s'\n",
+ session->channel ? ast_channel_name(session->channel) : "<gone>");
t38_change_state(session, session_media, datastore->data, T38_REJECTED);
ast_sip_session_resume_reinvite(session);
@@ -227,9 +231,9 @@ static struct t38_state *t38_state_get_or_alloc(struct ast_sip_session *session)
return datastore->data;
}
- if (!(datastore = ast_sip_session_alloc_datastore(&t38_datastore, "t38")) ||
- !(datastore->data = ast_calloc(1, sizeof(struct t38_state))) ||
- ast_sip_session_add_datastore(session, datastore)) {
+ if (!(datastore = ast_sip_session_alloc_datastore(&t38_datastore, "t38"))
+ || !(datastore->data = ast_calloc(1, sizeof(struct t38_state)))
+ || ast_sip_session_add_datastore(session, datastore)) {
return NULL;
}
@@ -324,9 +328,13 @@ static int t38_interpret_parameters(void *obj)
case AST_T38_REQUEST_NEGOTIATE: /* Request T38 */
/* Negotiation can not take place without a valid max_ifp value. */
if (!parameters->max_ifp) {
- t38_change_state(data->session, session_media, state, T38_REJECTED);
if (data->session->t38state == T38_PEER_REINVITE) {
+ t38_change_state(data->session, session_media, state, T38_REJECTED);
ast_sip_session_resume_reinvite(data->session);
+ } else if (data->session->t38state == T38_ENABLED) {
+ t38_change_state(data->session, session_media, state, T38_DISABLED);
+ ast_sip_session_refresh(data->session, NULL, NULL, NULL,
+ AST_SIP_SESSION_REFRESH_METHOD_INVITE, 1);
}
break;
} else if (data->session->t38state == T38_PEER_REINVITE) {
diff --git a/res/res_pjsip_xpidf_body_generator.c b/res/res_pjsip_xpidf_body_generator.c
index 43cb1e78b..298235cbc 100644
--- a/res/res_pjsip_xpidf_body_generator.c
+++ b/res/res_pjsip_xpidf_body_generator.c
@@ -106,14 +106,13 @@ static void xpidf_to_string(void *body, struct ast_str **str)
int size;
do {
- size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str));
- if (size == AST_PJSIP_XML_PROLOG_LEN) {
+ size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
+ if (size <= AST_PJSIP_XML_PROLOG_LEN) {
ast_str_make_space(str, ast_str_size(*str) * 2);
++growths;
}
- } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
-
- if (size == AST_PJSIP_XML_PROLOG_LEN) {
+ } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
+ if (size <= AST_PJSIP_XML_PROLOG_LEN) {
ast_log(LOG_WARNING, "XPIDF body text too large\n");
return;
}
diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index b78f6e24e..adce9e7ed 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -204,11 +204,13 @@ struct rtp_learning_info {
#ifdef HAVE_OPENSSL_SRTP
struct dtls_details {
+ ast_mutex_t lock; /*!< Lock for timeout timer synchronization */
SSL *ssl; /*!< SSL session */
BIO *read_bio; /*!< Memory buffer for reading */
BIO *write_bio; /*!< Memory buffer for writing */
enum ast_rtp_dtls_setup dtls_setup; /*!< Current setup state */
enum ast_rtp_dtls_connection connection; /*!< Whether this is a new or existing connection */
+ int timeout_timer; /*!< Scheduler id for timeout timer */
};
#endif
@@ -317,7 +319,6 @@ struct ast_rtp {
#ifdef HAVE_OPENSSL_SRTP
SSL_CTX *ssl_ctx; /*!< SSL context */
- ast_mutex_t dtls_timer_lock; /*!< Lock for synchronization purposes */
enum ast_rtp_dtls_verify dtls_verify; /*!< What to verify */
enum ast_srtp_suite suite; /*!< SRTP crypto suite */
enum ast_rtp_dtls_hash local_hash; /*!< Local hash used for the fingerprint */
@@ -326,7 +327,6 @@ struct ast_rtp {
unsigned char remote_fingerprint[EVP_MAX_MD_SIZE]; /*!< Fingerprint of the peer certificate */
unsigned int rekey; /*!< Interval at which to renegotiate and rekey */
int rekeyid; /*!< Scheduled item id for rekeying */
- int dtlstimerid; /*!< Scheduled item id for DTLS retransmission for RTP */
struct dtls_details dtls; /*!< DTLS state information */
#endif
};
@@ -444,6 +444,8 @@ static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level);
#ifdef HAVE_OPENSSL_SRTP
static int ast_rtp_activate(struct ast_rtp_instance *instance);
static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
+static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
+static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp);
#endif
static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *ice, int use_srtp);
@@ -1229,6 +1231,8 @@ static int dtls_details_initialize(struct dtls_details *dtls, SSL_CTX *ssl_ctx,
}
dtls->connection = AST_RTP_DTLS_CONNECTION_NEW;
+ ast_mutex_init(&dtls->lock);
+
return 0;
error:
@@ -1397,6 +1401,8 @@ static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+ dtls_srtp_stop_timeout_timer(instance, rtp, 0);
+
if (rtp->ssl_ctx) {
SSL_CTX_free(rtp->ssl_ctx);
rtp->ssl_ctx = NULL;
@@ -1405,11 +1411,17 @@ static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance)
if (rtp->dtls.ssl) {
SSL_free(rtp->dtls.ssl);
rtp->dtls.ssl = NULL;
+ ast_mutex_destroy(&rtp->dtls.lock);
}
- if (rtp->rtcp && rtp->rtcp->dtls.ssl) {
- SSL_free(rtp->rtcp->dtls.ssl);
- rtp->rtcp->dtls.ssl = NULL;
+ if (rtp->rtcp) {
+ dtls_srtp_stop_timeout_timer(instance, rtp, 1);
+
+ if (rtp->rtcp->dtls.ssl) {
+ SSL_free(rtp->rtcp->dtls.ssl);
+ rtp->rtcp->dtls.ssl = NULL;
+ ast_mutex_destroy(&rtp->rtcp->dtls.lock);
+ }
}
}
@@ -1586,21 +1598,25 @@ static void dtls_perform_handshake(struct ast_rtp_instance *instance, struct dtl
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
- if (!dtls->ssl) {
+ /* If we are not acting as a client connecting to the remote side then
+ * don't start the handshake as it will accomplish nothing and would conflict
+ * with the handshake we receive from the remote side.
+ */
+ if (!dtls->ssl || (dtls->dtls_setup != AST_RTP_DTLS_SETUP_ACTIVE)) {
return;
}
- if (SSL_is_init_finished(dtls->ssl)) {
- SSL_clear(dtls->ssl);
- if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
- SSL_set_accept_state(dtls->ssl);
- } else {
- SSL_set_connect_state(dtls->ssl);
- }
- dtls->connection = AST_RTP_DTLS_CONNECTION_NEW;
- }
SSL_do_handshake(dtls->ssl);
+
+ /* Since the handshake is started in a thread outside of the channel thread it's possible
+ * for the response to be handled in the channel thread before we start the timeout timer.
+ * To ensure this doesn't actually happen we hold the DTLS lock. The channel thread will
+ * block until we're done at which point the timeout timer will be immediately stopped.
+ */
+ ast_mutex_lock(&dtls->lock);
dtls_srtp_check_pending(instance, rtp, rtcp);
+ dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
+ ast_mutex_unlock(&dtls->lock);
}
#endif
@@ -1754,48 +1770,83 @@ static inline int rtcp_debug_test_addr(struct ast_sockaddr *addr)
}
#ifdef HAVE_OPENSSL_SRTP
-
-static int dtls_srtp_handle_timeout(const void *data)
+static int dtls_srtp_handle_timeout(struct ast_rtp_instance *instance, int rtcp)
{
- struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+ struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
+ struct timeval dtls_timeout;
+
+ DTLSv1_handle_timeout(dtls->ssl);
+ dtls_srtp_check_pending(instance, rtp, rtcp);
- if (!rtp)
- {
+ /* If a timeout can't be retrieved then this recurring scheduled item must stop */
+ if (!DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
+ dtls->timeout_timer = -1;
return 0;
}
- ast_mutex_lock(&rtp->dtls_timer_lock);
- if (rtp->dtlstimerid == -1)
- {
- ast_mutex_unlock(&rtp->dtls_timer_lock);
+ return dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
+}
+
+static int dtls_srtp_handle_rtp_timeout(const void *data)
+{
+ struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
+ int reschedule;
+
+ reschedule = dtls_srtp_handle_timeout(instance, 0);
+
+ if (!reschedule) {
ao2_ref(instance, -1);
- return 0;
}
- rtp->dtlstimerid = -1;
- ast_mutex_unlock(&rtp->dtls_timer_lock);
+ return reschedule;
+}
+
+static int dtls_srtp_handle_rtcp_timeout(const void *data)
+{
+ struct ast_rtp_instance *instance = (struct ast_rtp_instance *)data;
+ int reschedule;
+
+ reschedule = dtls_srtp_handle_timeout(instance, 1);
- if (rtp->dtls.ssl && !SSL_is_init_finished(rtp->dtls.ssl)) {
- DTLSv1_handle_timeout(rtp->dtls.ssl);
+ if (!reschedule) {
+ ao2_ref(instance, -1);
}
- dtls_srtp_check_pending(instance, rtp, 0);
- if (rtp->rtcp && rtp->rtcp->dtls.ssl && !SSL_is_init_finished(rtp->rtcp->dtls.ssl)) {
- DTLSv1_handle_timeout(rtp->rtcp->dtls.ssl);
+ return reschedule;
+}
+
+static void dtls_srtp_start_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
+{
+ struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
+ struct timeval dtls_timeout;
+
+ if (DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
+ int timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
+
+ ast_assert(dtls->timeout_timer == -1);
+
+ ao2_ref(instance, +1);
+ if ((dtls->timeout_timer = ast_sched_add(rtp->sched, timeout,
+ !rtcp ? dtls_srtp_handle_rtp_timeout : dtls_srtp_handle_rtcp_timeout, instance)) < 0) {
+ ao2_ref(instance, -1);
+ ast_log(LOG_WARNING, "Scheduling '%s' DTLS retransmission for RTP instance [%p] failed.\n",
+ !rtcp ? "RTP" : "RTCP", instance);
+ }
}
- dtls_srtp_check_pending(instance, rtp, 1);
+}
- ao2_ref(instance, -1);
+static void dtls_srtp_stop_timeout_timer(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
+{
+ struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
- return 0;
+ AST_SCHED_DEL_UNREF(rtp->sched, dtls->timeout_timer, ao2_ref(instance, -1));
}
static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct ast_rtp *rtp, int rtcp)
{
struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
size_t pending;
- struct timeval dtls_timeout; /* timeout on DTLS */
if (!dtls->ssl || !dtls->write_bio) {
return;
@@ -1821,24 +1872,6 @@ static void dtls_srtp_check_pending(struct ast_rtp_instance *instance, struct as
}
out = BIO_read(dtls->write_bio, outgoing, sizeof(outgoing));
-
- /* Stop existing DTLS timer if running */
- ast_mutex_lock(&rtp->dtls_timer_lock);
- if (rtp->dtlstimerid > -1) {
- AST_SCHED_DEL_UNREF(rtp->sched, rtp->dtlstimerid, ao2_ref(instance, -1));
- rtp->dtlstimerid = -1;
- }
-
- if (DTLSv1_get_timeout(dtls->ssl, &dtls_timeout)) {
- int timeout = dtls_timeout.tv_sec * 1000 + dtls_timeout.tv_usec / 1000;
- ao2_ref(instance, +1);
- if ((rtp->dtlstimerid = ast_sched_add(rtp->sched, timeout, dtls_srtp_handle_timeout, instance)) < 0) {
- ao2_ref(instance, -1);
- ast_log(LOG_WARNING, "scheduling DTLS retransmission for RTP instance [%p] failed.\n", instance);
- }
- }
- ast_mutex_unlock(&rtp->dtls_timer_lock);
-
__rtp_sendto(instance, outgoing, out, 0, &remote_address, rtcp, &ice, 0);
}
}
@@ -2014,8 +2047,6 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s
}
#ifdef HAVE_OPENSSL_SRTP
- dtls_srtp_check_pending(instance, rtp, rtcp);
-
/* If this is an SSL packet pass it to OpenSSL for processing. RFC section for first byte value:
* https://tools.ietf.org/html/rfc5764#section-5.1.2 */
if ((*in >= 20) && (*in <= 63)) {
@@ -2029,6 +2060,15 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s
return -1;
}
+ /* This mutex is locked so that this thread blocks until the dtls_perform_handshake function
+ * completes.
+ */
+ ast_mutex_lock(&dtls->lock);
+ ast_mutex_unlock(&dtls->lock);
+
+ /* Before we feed data into OpenSSL ensure that the timeout timer is either stopped or completed */
+ dtls_srtp_stop_timeout_timer(instance, rtp, rtcp);
+
/* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
dtls->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
@@ -2057,6 +2097,9 @@ static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t s
/* Use the keying material to set up key/salt information */
res = dtls_srtp_setup(rtp, srtp, instance);
}
+ } else {
+ /* Since we've sent additional traffic start the timeout timer for retransmission */
+ dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
}
return res;
@@ -2479,7 +2522,7 @@ static int ast_rtp_new(struct ast_rtp_instance *instance,
#ifdef HAVE_OPENSSL_SRTP
rtp->rekeyid = -1;
- rtp->dtlstimerid = -1;
+ rtp->dtls.timeout_timer = -1;
#endif
rtp->f.subclass.format = ao2_bump(ast_format_none);
@@ -2497,6 +2540,10 @@ static int ast_rtp_destroy(struct ast_rtp_instance *instance)
struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
#endif
+#ifdef HAVE_OPENSSL_SRTP
+ ast_rtp_dtls_stop(instance);
+#endif
+
/* Destroy the smoother that was smoothing out audio if present */
if (rtp->smoother) {
ast_smoother_free(rtp->smoother);
@@ -2515,11 +2562,6 @@ static int ast_rtp_destroy(struct ast_rtp_instance *instance)
* RTP instance while it's active.
*/
close(rtp->rtcp->s);
-#ifdef HAVE_OPENSSL_SRTP
- if (rtp->rtcp->dtls.ssl) {
- SSL_free(rtp->rtcp->dtls.ssl);
- }
-#endif
ast_free(rtp->rtcp);
}
@@ -2571,18 +2613,6 @@ static int ast_rtp_destroy(struct ast_rtp_instance *instance)
}
#endif
-#ifdef HAVE_OPENSSL_SRTP
- /* Destroy the SSL context if present */
- if (rtp->ssl_ctx) {
- SSL_CTX_free(rtp->ssl_ctx);
- }
-
- /* Destroy the SSL session if present */
- if (rtp->dtls.ssl) {
- SSL_free(rtp->dtls.ssl);
- }
-#endif
-
ao2_cleanup(rtp->lasttxformat);
ao2_cleanup(rtp->lastrxformat);
ao2_cleanup(rtp->f.subclass.format);
@@ -4693,6 +4723,7 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_pro
#endif
#ifdef HAVE_OPENSSL_SRTP
+ rtp->rtcp->dtls.timeout_timer = -1;
dtls_setup_rtcp(instance);
#endif
@@ -4909,9 +4940,11 @@ static void ast_rtp_stop(struct ast_rtp_instance *instance)
#ifdef HAVE_OPENSSL_SRTP
AST_SCHED_DEL_UNREF(rtp->sched, rtp->rekeyid, ao2_ref(instance, -1));
- ast_mutex_lock(&rtp->dtls_timer_lock);
- AST_SCHED_DEL_UNREF(rtp->sched, rtp->dtlstimerid, ao2_ref(instance, -1));
- ast_mutex_unlock(&rtp->dtls_timer_lock);
+
+ dtls_srtp_stop_timeout_timer(instance, rtp, 0);
+ if (rtp->rtcp) {
+ dtls_srtp_stop_timeout_timer(instance, rtp, 1);
+ }
#endif
if (rtp->rtcp && rtp->rtcp->schedid > 0) {
@@ -4993,10 +5026,31 @@ static int ast_rtp_sendcng(struct ast_rtp_instance *instance, int level)
}
#ifdef HAVE_OPENSSL_SRTP
+static void dtls_perform_setup(struct dtls_details *dtls)
+{
+ if (!dtls->ssl || !SSL_is_init_finished(dtls->ssl)) {
+ return;
+ }
+
+ SSL_clear(dtls->ssl);
+ if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_PASSIVE) {
+ SSL_set_accept_state(dtls->ssl);
+ } else {
+ SSL_set_connect_state(dtls->ssl);
+ }
+ dtls->connection = AST_RTP_DTLS_CONNECTION_NEW;
+}
+
static int ast_rtp_activate(struct ast_rtp_instance *instance)
{
struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+ dtls_perform_setup(&rtp->dtls);
+
+ if (rtp->rtcp) {
+ dtls_perform_setup(&rtp->rtcp->dtls);
+ }
+
/* If ICE negotiation is enabled the DTLS Handshake will be performed upon completion of it */
#ifdef HAVE_PJPROJECT
if (rtp->ice) {
diff --git a/res/res_sorcery_memory_cache.c b/res/res_sorcery_memory_cache.c
new file mode 100644
index 000000000..f0fc05537
--- /dev/null
+++ b/res/res_sorcery_memory_cache.c
@@ -0,0 +1,2572 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2015, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@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
+ *
+ * \brief Sorcery Memory Cache Object Wizard
+ *
+ * \author Joshua Colp <jcolp@digium.com>
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/sched.h"
+#include "asterisk/test.h"
+#include "asterisk/heap.h"
+#include "asterisk/cli.h"
+#include "asterisk/manager.h"
+
+/*** DOCUMENTATION
+ <manager name="SorceryMemoryCacheExpireObject" language="en_US">
+ <synopsis>
+ Expire (remove) an object from a sorcery memory cache.
+ </synopsis>
+ <syntax>
+ <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+ <parameter name="Cache" required="true">
+ <para>The name of the cache to expire the object from.</para>
+ </parameter>
+ <parameter name="Object" required="true">
+ <para>The name of the object to expire.</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Expires (removes) an object from a sorcery memory cache.</para>
+ </description>
+ </manager>
+ <manager name="SorceryMemoryCacheExpire" language="en_US">
+ <synopsis>
+ Expire (remove) ALL objects from a sorcery memory cache.
+ </synopsis>
+ <syntax>
+ <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+ <parameter name="Cache" required="true">
+ <para>The name of the cache to expire all objects from.</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Expires (removes) ALL objects from a sorcery memory cache.</para>
+ </description>
+ </manager>
+ <manager name="SorceryMemoryCacheStaleObject" language="en_US">
+ <synopsis>
+ Mark an object in a sorcery memory cache as stale.
+ </synopsis>
+ <syntax>
+ <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+ <parameter name="Cache" required="true">
+ <para>The name of the cache to mark the object as stale in.</para>
+ </parameter>
+ <parameter name="Object" required="true">
+ <para>The name of the object to mark as stale.</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Marks an object as stale within a sorcery memory cache.</para>
+ </description>
+ </manager>
+ <manager name="SorceryMemoryCacheStale" language="en_US">
+ <synopsis>
+ Marks ALL objects in a sorcery memory cache as stale.
+ </synopsis>
+ <syntax>
+ <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+ <parameter name="Cache" required="true">
+ <para>The name of the cache to mark all object as stale in.</para>
+ </parameter>
+ </syntax>
+ <description>
+ <para>Marks ALL objects in a sorcery memory cache as stale.</para>
+ </description>
+ </manager>
+ ***/
+
+/*! \brief Structure for storing a memory cache */
+struct sorcery_memory_cache {
+ /*! \brief The name of the memory cache */
+ char *name;
+ /*! \brief Objects in the cache */
+ struct ao2_container *objects;
+ /*! \brief The maximum number of objects permitted in the cache, 0 if no limit */
+ unsigned int maximum_objects;
+ /*! \brief The maximum time (in seconds) an object will stay in the cache, 0 if no limit */
+ unsigned int object_lifetime_maximum;
+ /*! \brief The amount of time (in seconds) before an object is marked as stale, 0 if disabled */
+ unsigned int object_lifetime_stale;
+ /** \brief Whether all objects are expired when the object type is reloaded, 0 if disabled */
+ unsigned int expire_on_reload;
+ /*! \brief Heap of cached objects. Oldest object is at the top. */
+ struct ast_heap *object_heap;
+ /*! \brief Scheduler item for expiring oldest object. */
+ int expire_id;
+#ifdef TEST_FRAMEWORK
+ /*! \brief Variable used to indicate we should notify a test when we reach empty */
+ unsigned int cache_notify;
+ /*! \brief Mutex lock used for signaling when the cache has reached empty */
+ ast_mutex_t lock;
+ /*! \brief Condition used for signaling when the cache has reached empty */
+ ast_cond_t cond;
+ /*! \brief Variable that is set when the cache has reached empty */
+ unsigned int cache_completed;
+#endif
+};
+
+/*! \brief Structure for stored a cached object */
+struct sorcery_memory_cached_object {
+ /*! \brief The cached object */
+ void *object;
+ /*! \brief The time at which the object was created */
+ struct timeval created;
+ /*! \brief index required by heap */
+ ssize_t __heap_index;
+ /*! \brief scheduler id of stale update task */
+ int stale_update_sched_id;
+};
+
+static void *sorcery_memory_cache_open(const char *data);
+static int sorcery_memory_cache_create(const struct ast_sorcery *sorcery, void *data, void *object);
+static void sorcery_memory_cache_load(void *data, const struct ast_sorcery *sorcery, const char *type);
+static void sorcery_memory_cache_reload(void *data, const struct ast_sorcery *sorcery, const char *type);
+static void *sorcery_memory_cache_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type,
+ const char *id);
+static int sorcery_memory_cache_delete(const struct ast_sorcery *sorcery, void *data, void *object);
+static void sorcery_memory_cache_close(void *data);
+
+static struct ast_sorcery_wizard memory_cache_object_wizard = {
+ .name = "memory_cache",
+ .open = sorcery_memory_cache_open,
+ .create = sorcery_memory_cache_create,
+ .update = sorcery_memory_cache_create,
+ .delete = sorcery_memory_cache_delete,
+ .load = sorcery_memory_cache_load,
+ .reload = sorcery_memory_cache_reload,
+ .retrieve_id = sorcery_memory_cache_retrieve_id,
+ .close = sorcery_memory_cache_close,
+};
+
+/*! \brief The bucket size for the container of caches */
+#define CACHES_CONTAINER_BUCKET_SIZE 53
+
+/*! \brief The default bucket size for the container of objects in the cache */
+#define CACHE_CONTAINER_BUCKET_SIZE 53
+
+/*! \brief Height of heap for cache object heap. Allows 31 initial objects */
+#define CACHE_HEAP_INIT_HEIGHT 5
+
+/*! \brief Container of created caches */
+static struct ao2_container *caches;
+
+/*! \brief Scheduler for cache management */
+static struct ast_sched_context *sched;
+
+#define STALE_UPDATE_THREAD_ID 0x5EED1E55
+AST_THREADSTORAGE(stale_update_id_storage);
+
+static int is_stale_update(void)
+{
+ uint32_t *stale_update_thread_id;
+
+ stale_update_thread_id = ast_threadstorage_get(&stale_update_id_storage,
+ sizeof(*stale_update_thread_id));
+ if (!stale_update_thread_id) {
+ return 0;
+ }
+
+ return *stale_update_thread_id == STALE_UPDATE_THREAD_ID;
+}
+
+static void start_stale_update(void)
+{
+ uint32_t *stale_update_thread_id;
+
+ stale_update_thread_id = ast_threadstorage_get(&stale_update_id_storage,
+ sizeof(*stale_update_thread_id));
+ if (!stale_update_thread_id) {
+ ast_log(LOG_ERROR, "Could not set stale update ID for sorcery memory cache thread\n");
+ return;
+ }
+
+ *stale_update_thread_id = STALE_UPDATE_THREAD_ID;
+}
+
+static void end_stale_update(void)
+{
+ uint32_t *stale_update_thread_id;
+
+ stale_update_thread_id = ast_threadstorage_get(&stale_update_id_storage,
+ sizeof(*stale_update_thread_id));
+ if (!stale_update_thread_id) {
+ ast_log(LOG_ERROR, "Could not set stale update ID for sorcery memory cache thread\n");
+ return;
+ }
+
+ *stale_update_thread_id = 0;
+}
+
+/*!
+ * \internal
+ * \brief Hashing function for the container holding caches
+ *
+ * \param obj A sorcery memory cache or name of one
+ * \param flags Hashing flags
+ *
+ * \return The hash of the memory cache name
+ */
+static int sorcery_memory_cache_hash(const void *obj, int flags)
+{
+ const struct sorcery_memory_cache *cache = obj;
+ const char *name = obj;
+ int hash;
+
+ switch (flags & (OBJ_SEARCH_OBJECT | OBJ_SEARCH_KEY | OBJ_SEARCH_PARTIAL_KEY)) {
+ default:
+ case OBJ_SEARCH_OBJECT:
+ name = cache->name;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ hash = ast_str_hash(name);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ /* Should never happen in hash callback. */
+ ast_assert(0);
+ hash = 0;
+ break;
+ }
+ return hash;
+}
+
+/*!
+ * \internal
+ * \brief Comparison function for the container holding caches
+ *
+ * \param obj A sorcery memory cache
+ * \param arg A sorcery memory cache, or name of one
+ * \param flags Comparison flags
+ *
+ * \retval CMP_MATCH if the name is the same
+ * \retval 0 if the name does not match
+ */
+static int sorcery_memory_cache_cmp(void *obj, void *arg, int flags)
+{
+ const struct sorcery_memory_cache *left = obj;
+ const struct sorcery_memory_cache *right = arg;
+ const char *right_name = arg;
+ int cmp;
+
+ switch (flags & (OBJ_SEARCH_OBJECT | OBJ_SEARCH_KEY | OBJ_SEARCH_PARTIAL_KEY)) {
+ default:
+ case OBJ_SEARCH_OBJECT:
+ right_name = right->name;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(left->name, right_name);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(left->name, right_name, strlen(right_name));
+ break;
+ }
+ return cmp ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Hashing function for the container holding cached objects
+ *
+ * \param obj A cached object or id of one
+ * \param flags Hashing flags
+ *
+ * \return The hash of the cached object id
+ */
+static int sorcery_memory_cached_object_hash(const void *obj, int flags)
+{
+ const struct sorcery_memory_cached_object *cached = obj;
+ const char *name = obj;
+ int hash;
+
+ switch (flags & (OBJ_SEARCH_OBJECT | OBJ_SEARCH_KEY | OBJ_SEARCH_PARTIAL_KEY)) {
+ default:
+ case OBJ_SEARCH_OBJECT:
+ name = ast_sorcery_object_get_id(cached->object);
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ hash = ast_str_hash(name);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ /* Should never happen in hash callback. */
+ ast_assert(0);
+ hash = 0;
+ break;
+ }
+ return hash;
+}
+
+/*!
+ * \internal
+ * \brief Comparison function for the container holding cached objects
+ *
+ * \param obj A cached object
+ * \param arg A cached object, or id of one
+ * \param flags Comparison flags
+ *
+ * \retval CMP_MATCH if the id is the same
+ * \retval 0 if the id does not match
+ */
+static int sorcery_memory_cached_object_cmp(void *obj, void *arg, int flags)
+{
+ struct sorcery_memory_cached_object *left = obj;
+ struct sorcery_memory_cached_object *right = arg;
+ const char *right_name = arg;
+ int cmp;
+
+ switch (flags & (OBJ_SEARCH_OBJECT | OBJ_SEARCH_KEY | OBJ_SEARCH_PARTIAL_KEY)) {
+ default:
+ case OBJ_SEARCH_OBJECT:
+ right_name = ast_sorcery_object_get_id(right->object);
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(ast_sorcery_object_get_id(left->object), right_name);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(ast_sorcery_object_get_id(left->object), right_name, strlen(right_name));
+ break;
+ }
+ return cmp ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Destructor function for a sorcery memory cache
+ *
+ * \param obj A sorcery memory cache
+ */
+static void sorcery_memory_cache_destructor(void *obj)
+{
+ struct sorcery_memory_cache *cache = obj;
+
+ ast_free(cache->name);
+ ao2_cleanup(cache->objects);
+ if (cache->object_heap) {
+ ast_heap_destroy(cache->object_heap);
+ }
+}
+
+/*!
+ * \internal
+ * \brief Destructor function for sorcery memory cached objects
+ *
+ * \param obj A sorcery memory cached object
+ */
+static void sorcery_memory_cached_object_destructor(void *obj)
+{
+ struct sorcery_memory_cached_object *cached = obj;
+
+ ao2_cleanup(cached->object);
+}
+
+static int schedule_cache_expiration(struct sorcery_memory_cache *cache);
+
+/*!
+ * \internal
+ * \brief Remove an object from the cache.
+ *
+ * This removes the item from both the hashtable and the heap.
+ *
+ * \pre cache->objects is write-locked
+ *
+ * \param cache The cache from which the object is being removed.
+ * \param id The sorcery object id of the object to remove.
+ * \param reschedule Reschedule cache expiration if this was the oldest object.
+ *
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+static int remove_from_cache(struct sorcery_memory_cache *cache, const char *id, int reschedule)
+{
+ struct sorcery_memory_cached_object *hash_object;
+ struct sorcery_memory_cached_object *oldest_object;
+ struct sorcery_memory_cached_object *heap_object;
+
+ hash_object = ao2_find(cache->objects, id,
+ OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NOLOCK);
+ if (!hash_object) {
+ return -1;
+ }
+
+ ast_assert(!strcmp(ast_sorcery_object_get_id(hash_object->object), id));
+
+ oldest_object = ast_heap_peek(cache->object_heap, 1);
+ heap_object = ast_heap_remove(cache->object_heap, hash_object);
+
+ ast_assert(heap_object == hash_object);
+
+ ao2_ref(hash_object, -1);
+
+ if (reschedule && (oldest_object == heap_object)) {
+ schedule_cache_expiration(cache);
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Scheduler callback invoked to expire old objects
+ *
+ * \param data The opaque callback data (in our case, the memory cache)
+ */
+static int expire_objects_from_cache(const void *data)
+{
+ struct sorcery_memory_cache *cache = (struct sorcery_memory_cache *)data;
+ struct sorcery_memory_cached_object *cached;
+
+ ao2_wrlock(cache->objects);
+
+ cache->expire_id = -1;
+
+ /* This is an optimization for objects which have been cached close to eachother */
+ while ((cached = ast_heap_peek(cache->object_heap, 1))) {
+ int expiration;
+
+ expiration = ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(cache->object_lifetime_maximum, 1)), ast_tvnow());
+
+ /* If the current oldest object has not yet expired stop and reschedule for it */
+ if (expiration > 0) {
+ break;
+ }
+
+ remove_from_cache(cache, ast_sorcery_object_get_id(cached->object), 0);
+ }
+
+ schedule_cache_expiration(cache);
+
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Remove all objects from the cache.
+ *
+ * This removes ALL objects from both the hash table and heap.
+ *
+ * \pre cache->objects is write-locked
+ *
+ * \param cache The cache to empty.
+ */
+static void remove_all_from_cache(struct sorcery_memory_cache *cache)
+{
+ while (ast_heap_pop(cache->object_heap));
+
+ ao2_callback(cache->objects, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE,
+ NULL, NULL);
+
+ AST_SCHED_DEL_UNREF(sched, cache->expire_id, ao2_ref(cache, -1));
+}
+
+/*!
+ * \internal
+ * \brief AO2 callback function for making an object stale immediately
+ *
+ * This changes the creation time of an object so it appears as though it is stale immediately.
+ *
+ * \param obj The cached object
+ * \param arg The cache itself
+ * \param flags Unused flags
+ */
+static int object_stale_callback(void *obj, void *arg, int flags)
+{
+ struct sorcery_memory_cached_object *cached = obj;
+ struct sorcery_memory_cache *cache = arg;
+
+ /* Since our granularity is seconds it's possible for something to retrieve us within a window
+ * where we wouldn't be treated as stale. To ensure that doesn't happen we use the configured stale
+ * time plus a second.
+ */
+ cached->created = ast_tvsub(cached->created, ast_samp2tv(cache->object_lifetime_stale + 1, 1));
+
+ return CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief Mark an object as stale explicitly.
+ *
+ * This changes the creation time of an object so it appears as though it is stale immediately.
+ *
+ * \pre cache->objects is read-locked
+ *
+ * \param cache The cache the object is in
+ * \param id The unique identifier of the object
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+static int mark_object_as_stale_in_cache(struct sorcery_memory_cache *cache, const char *id)
+{
+ struct sorcery_memory_cached_object *cached;
+
+ cached = ao2_find(cache->objects, id, OBJ_SEARCH_KEY | OBJ_NOLOCK);
+ if (!cached) {
+ return -1;
+ }
+
+ ast_assert(!strcmp(ast_sorcery_object_get_id(cached->object), id));
+
+ object_stale_callback(cached, cache, 0);
+ ao2_ref(cached, -1);
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Mark all objects as stale within a cache.
+ *
+ * This changes the creation time of ALL objects so they appear as though they are stale.
+ *
+ * \pre cache->objects is read-locked
+ *
+ * \param cache
+ */
+static void mark_all_as_stale_in_cache(struct sorcery_memory_cache *cache)
+{
+ ao2_callback(cache->objects, OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, object_stale_callback, cache);
+}
+
+/*!
+ * \internal
+ * \brief Schedule a callback for cached object expiration.
+ *
+ * \pre cache->objects is write-locked
+ *
+ * \param cache The cache that is having its callback scheduled.
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+static int schedule_cache_expiration(struct sorcery_memory_cache *cache)
+{
+ struct sorcery_memory_cached_object *cached;
+ int expiration = 0;
+
+ if (!cache->object_lifetime_maximum) {
+ return 0;
+ }
+
+ if (cache->expire_id != -1) {
+ /* If we can't unschedule this expiration then it is currently attempting to run,
+ * so let it run - it just means that it'll be the one scheduling instead of us.
+ */
+ if (ast_sched_del(sched, cache->expire_id)) {
+ return 0;
+ }
+
+ /* Since it successfully cancelled we need to drop the ref to the cache it had */
+ ao2_ref(cache, -1);
+ cache->expire_id = -1;
+ }
+
+ cached = ast_heap_peek(cache->object_heap, 1);
+ if (!cached) {
+#ifdef TEST_FRAMEWORK
+ ast_mutex_lock(&cache->lock);
+ cache->cache_completed = 1;
+ ast_cond_signal(&cache->cond);
+ ast_mutex_unlock(&cache->lock);
+#endif
+ return 0;
+ }
+
+ expiration = MAX(ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(cache->object_lifetime_maximum, 1)), ast_tvnow()),
+ 1);
+
+ cache->expire_id = ast_sched_add(sched, expiration, expire_objects_from_cache, ao2_bump(cache));
+ if (cache->expire_id < 0) {
+ ao2_ref(cache, -1);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Remove the oldest item from the cache.
+ *
+ * \pre cache->objects is write-locked
+ *
+ * \param cache The cache from which to remove the oldest object
+ *
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+static int remove_oldest_from_cache(struct sorcery_memory_cache *cache)
+{
+ struct sorcery_memory_cached_object *heap_old_object;
+ struct sorcery_memory_cached_object *hash_old_object;
+
+ heap_old_object = ast_heap_pop(cache->object_heap);
+ if (!heap_old_object) {
+ return -1;
+ }
+ hash_old_object = ao2_find(cache->objects, heap_old_object,
+ OBJ_SEARCH_OBJECT | OBJ_UNLINK | OBJ_NOLOCK);
+
+ ast_assert(heap_old_object == hash_old_object);
+
+ ao2_ref(hash_old_object, -1);
+
+ schedule_cache_expiration(cache);
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Add a new object to the cache.
+ *
+ * \pre cache->objects is write-locked
+ *
+ * \param cache The cache in which to add the new object
+ * \param cached_object The object to add to the cache
+ *
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+static int add_to_cache(struct sorcery_memory_cache *cache,
+ struct sorcery_memory_cached_object *cached_object)
+{
+ if (!ao2_link_flags(cache->objects, cached_object, OBJ_NOLOCK)) {
+ return -1;
+ }
+
+ if (ast_heap_push(cache->object_heap, cached_object)) {
+ ao2_find(cache->objects, cached_object,
+ OBJ_SEARCH_OBJECT | OBJ_UNLINK | OBJ_NODATA | OBJ_NOLOCK);
+ return -1;
+ }
+
+ if (cache->expire_id == -1) {
+ schedule_cache_expiration(cache);
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Callback function to cache an object in a memory cache
+ *
+ * \param sorcery The sorcery instance
+ * \param data The sorcery memory cache
+ * \param object The object to cache
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+static int sorcery_memory_cache_create(const struct ast_sorcery *sorcery, void *data, void *object)
+{
+ struct sorcery_memory_cache *cache = data;
+ struct sorcery_memory_cached_object *cached;
+
+ cached = ao2_alloc(sizeof(*cached), sorcery_memory_cached_object_destructor);
+ if (!cached) {
+ return -1;
+ }
+ cached->object = ao2_bump(object);
+ cached->created = ast_tvnow();
+ cached->stale_update_sched_id = -1;
+
+ /* As there is no guarantee that this won't be called by multiple threads wanting to cache
+ * the same object we remove any old ones, which turns this into a create/update function
+ * in reality. As well since there's no guarantee that the object in the cache is the same
+ * one here we remove any old objects using the object identifier.
+ */
+
+ ao2_wrlock(cache->objects);
+ remove_from_cache(cache, ast_sorcery_object_get_id(object), 1);
+ if (cache->maximum_objects && ao2_container_count(cache->objects) >= cache->maximum_objects) {
+ if (remove_oldest_from_cache(cache)) {
+ ast_log(LOG_ERROR, "Unable to make room in cache for sorcery object '%s'.\n",
+ ast_sorcery_object_get_id(object));
+ ao2_ref(cached, -1);
+ ao2_unlock(cache->objects);
+ return -1;
+ }
+ ast_assert(ao2_container_count(cache->objects) != cache->maximum_objects);
+ }
+ if (add_to_cache(cache, cached)) {
+ ast_log(LOG_ERROR, "Unable to add object '%s' to the cache\n",
+ ast_sorcery_object_get_id(object));
+ ao2_ref(cached, -1);
+ ao2_unlock(cache->objects);
+ return -1;
+ }
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cached, -1);
+ return 0;
+}
+
+struct stale_update_task_data {
+ struct ast_sorcery *sorcery;
+ struct sorcery_memory_cache *cache;
+ void *object;
+};
+
+static void stale_update_task_data_destructor(void *obj)
+{
+ struct stale_update_task_data *task_data = obj;
+
+ ao2_cleanup(task_data->cache);
+ ao2_cleanup(task_data->object);
+ ast_sorcery_unref(task_data->sorcery);
+}
+
+static struct stale_update_task_data *stale_update_task_data_alloc(struct ast_sorcery *sorcery,
+ struct sorcery_memory_cache *cache, const char *type, void *object)
+{
+ struct stale_update_task_data *task_data;
+
+ task_data = ao2_alloc_options(sizeof(*task_data), stale_update_task_data_destructor,
+ AO2_ALLOC_OPT_LOCK_NOLOCK);
+ if (!task_data) {
+ return NULL;
+ }
+
+ task_data->sorcery = ao2_bump(sorcery);
+ task_data->cache = ao2_bump(cache);
+ task_data->object = ao2_bump(object);
+
+ return task_data;
+}
+
+static int stale_item_update(const void *data)
+{
+ struct stale_update_task_data *task_data = (struct stale_update_task_data *) data;
+ void *object;
+
+ start_stale_update();
+
+ object = ast_sorcery_retrieve_by_id(task_data->sorcery,
+ ast_sorcery_object_get_type(task_data->object),
+ ast_sorcery_object_get_id(task_data->object));
+ if (!object) {
+ ast_debug(1, "Backend no longer has object type '%s' ID '%s'. Removing from cache\n",
+ ast_sorcery_object_get_type(task_data->object),
+ ast_sorcery_object_get_id(task_data->object));
+ sorcery_memory_cache_delete(task_data->sorcery, task_data->cache,
+ task_data->object);
+ } else {
+ ast_debug(1, "Refreshing stale cache object type '%s' ID '%s'\n",
+ ast_sorcery_object_get_type(task_data->object),
+ ast_sorcery_object_get_id(task_data->object));
+ sorcery_memory_cache_create(task_data->sorcery, task_data->cache,
+ object);
+ }
+
+ ast_test_suite_event_notify("SORCERY_MEMORY_CACHE_REFRESHED", "Cache: %s\r\nType: %s\r\nName: %s\r\n",
+ task_data->cache->name, ast_sorcery_object_get_type(task_data->object),
+ ast_sorcery_object_get_id(task_data->object));
+
+ ao2_ref(task_data, -1);
+ end_stale_update();
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief Callback function to retrieve an object from a memory cache
+ *
+ * \param sorcery The sorcery instance
+ * \param data The sorcery memory cache
+ * \param type The type of the object to retrieve
+ * \param id The id of the object to retrieve
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+static void *sorcery_memory_cache_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
+{
+ struct sorcery_memory_cache *cache = data;
+ struct sorcery_memory_cached_object *cached;
+ void *object;
+
+ if (is_stale_update()) {
+ return NULL;
+ }
+
+ cached = ao2_find(cache->objects, id, OBJ_SEARCH_KEY);
+ if (!cached) {
+ return NULL;
+ }
+
+ ast_assert(!strcmp(ast_sorcery_object_get_id(cached->object), id));
+
+ if (cache->object_lifetime_stale) {
+ struct timeval elapsed;
+
+ elapsed = ast_tvsub(ast_tvnow(), cached->created);
+ if (elapsed.tv_sec > cache->object_lifetime_stale) {
+ ao2_lock(cached);
+ if (cached->stale_update_sched_id == -1) {
+ struct stale_update_task_data *task_data;
+
+ task_data = stale_update_task_data_alloc((struct ast_sorcery *)sorcery, cache,
+ type, cached->object);
+ if (task_data) {
+ ast_debug(1, "Cached sorcery object type '%s' ID '%s' is stale. Refreshing\n",
+ type, id);
+ cached->stale_update_sched_id = ast_sched_add(sched, 1, stale_item_update, task_data);
+ } else {
+ ast_log(LOG_ERROR, "Unable to update stale cached object type '%s', ID '%s'.\n",
+ type, id);
+ }
+ }
+ ao2_unlock(cached);
+ }
+ }
+
+ object = ao2_bump(cached->object);
+ ao2_ref(cached, -1);
+
+ return object;
+}
+
+/*!
+ * \internal
+ * \brief Callback function to finish configuring the memory cache
+ *
+ * \param data The sorcery memory cache
+ * \param sorcery The sorcery instance
+ * \param type The type of object being loaded
+ */
+static void sorcery_memory_cache_load(void *data, const struct ast_sorcery *sorcery, const char *type)
+{
+ struct sorcery_memory_cache *cache = data;
+
+ /* If no name was explicitly specified generate one given the sorcery instance and object type */
+ if (ast_strlen_zero(cache->name)) {
+ ast_asprintf(&cache->name, "%s/%s", ast_sorcery_get_module(sorcery), type);
+ }
+
+ ao2_link(caches, cache);
+ ast_debug(1, "Memory cache '%s' associated with sorcery instance '%p' of module '%s' with object type '%s'\n",
+ cache->name, sorcery, ast_sorcery_get_module(sorcery), type);
+}
+
+/*!
+ * \internal
+ * \brief Callback function to expire objects from the memory cache on reload (if configured)
+ *
+ * \param data The sorcery memory cache
+ * \param sorcery The sorcery instance
+ * \param type The type of object being reloaded
+ */
+static void sorcery_memory_cache_reload(void *data, const struct ast_sorcery *sorcery, const char *type)
+{
+ struct sorcery_memory_cache *cache = data;
+
+ if (!cache->expire_on_reload) {
+ return;
+ }
+
+ ao2_wrlock(cache->objects);
+ remove_all_from_cache(cache);
+ ao2_unlock(cache->objects);
+}
+
+/*!
+ * \internal
+ * \brief Function used to take an unsigned integer based configuration option and parse it
+ *
+ * \param value The string value of the configuration option
+ * \param result The unsigned integer to place the result in
+ *
+ * \retval 0 failure
+ * \retval 1 success
+ */
+static int configuration_parse_unsigned_integer(const char *value, unsigned int *result)
+{
+ if (ast_strlen_zero(value) || !strncmp(value, "-", 1)) {
+ return 0;
+ }
+
+ return sscanf(value, "%30u", result);
+}
+
+static int age_cmp(void *a, void *b)
+{
+ return ast_tvcmp(((struct sorcery_memory_cached_object *) b)->created,
+ ((struct sorcery_memory_cached_object *) a)->created);
+}
+
+/*!
+ * \internal
+ * \brief Callback function to create a new sorcery memory cache using provided configuration
+ *
+ * \param data A stringified configuration for the memory cache
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+static void *sorcery_memory_cache_open(const char *data)
+{
+ char *options = ast_strdup(data), *option;
+ RAII_VAR(struct sorcery_memory_cache *, cache, NULL, ao2_cleanup);
+
+ cache = ao2_alloc_options(sizeof(*cache), sorcery_memory_cache_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
+ if (!cache) {
+ return NULL;
+ }
+
+ cache->expire_id = -1;
+
+ /* If no configuration options have been provided this memory cache will operate in a default
+ * configuration.
+ */
+ while (!ast_strlen_zero(options) && (option = strsep(&options, ","))) {
+ char *name = strsep(&option, "="), *value = option;
+
+ if (!strcasecmp(name, "name")) {
+ if (ast_strlen_zero(value)) {
+ ast_log(LOG_ERROR, "A name must be specified for the memory cache\n");
+ return NULL;
+ }
+ ast_free(cache->name);
+ cache->name = ast_strdup(value);
+ } else if (!strcasecmp(name, "maximum_objects")) {
+ if (configuration_parse_unsigned_integer(value, &cache->maximum_objects) != 1) {
+ ast_log(LOG_ERROR, "Unsupported maximum objects value of '%s' used for memory cache\n",
+ value);
+ return NULL;
+ }
+ } else if (!strcasecmp(name, "object_lifetime_maximum")) {
+ if (configuration_parse_unsigned_integer(value, &cache->object_lifetime_maximum) != 1) {
+ ast_log(LOG_ERROR, "Unsupported object maximum lifetime value of '%s' used for memory cache\n",
+ value);
+ return NULL;
+ }
+ } else if (!strcasecmp(name, "object_lifetime_stale")) {
+ if (configuration_parse_unsigned_integer(value, &cache->object_lifetime_stale) != 1) {
+ ast_log(LOG_ERROR, "Unsupported object stale lifetime value of '%s' used for memory cache\n",
+ value);
+ return NULL;
+ }
+ } else if (!strcasecmp(name, "expire_on_reload")) {
+ cache->expire_on_reload = ast_true(value);
+ } else {
+ ast_log(LOG_ERROR, "Unsupported option '%s' used for memory cache\n", name);
+ return NULL;
+ }
+ }
+
+ cache->objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK,
+ cache->maximum_objects ? cache->maximum_objects : CACHE_CONTAINER_BUCKET_SIZE,
+ sorcery_memory_cached_object_hash, sorcery_memory_cached_object_cmp);
+ if (!cache->objects) {
+ ast_log(LOG_ERROR, "Could not create a container to hold cached objects for memory cache\n");
+ return NULL;
+ }
+
+ cache->object_heap = ast_heap_create(CACHE_HEAP_INIT_HEIGHT, age_cmp,
+ offsetof(struct sorcery_memory_cached_object, __heap_index));
+ if (!cache->object_heap) {
+ ast_log(LOG_ERROR, "Could not create heap to hold cached objects\n");
+ return NULL;
+ }
+
+ /* The memory cache is not linked to the caches container until the load callback is invoked.
+ * Linking occurs there so an intelligent cache name can be constructed using the module of
+ * the sorcery instance and the specific object type if no cache name was specified as part
+ * of the configuration.
+ */
+
+ /* This is done as RAII_VAR will drop the reference */
+ return ao2_bump(cache);
+}
+
+/*!
+ * \internal
+ * \brief Callback function to delete an object from a memory cache
+ *
+ * \param sorcery The sorcery instance
+ * \param data The sorcery memory cache
+ * \param object The object to cache
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+static int sorcery_memory_cache_delete(const struct ast_sorcery *sorcery, void *data, void *object)
+{
+ struct sorcery_memory_cache *cache = data;
+ int res;
+
+ ao2_wrlock(cache->objects);
+ res = remove_from_cache(cache, ast_sorcery_object_get_id(object), 1);
+ ao2_unlock(cache->objects);
+
+ if (res) {
+ ast_log(LOG_ERROR, "Unable to delete object '%s' from sorcery cache\n", ast_sorcery_object_get_id(object));
+ }
+
+ return res;
+}
+
+/*!
+ * \internal
+ * \brief Callback function to terminate a memory cache
+ *
+ * \param data The sorcery memory cache
+ */
+static void sorcery_memory_cache_close(void *data)
+{
+ struct sorcery_memory_cache *cache = data;
+
+ /* This can occur if a cache is created but never loaded */
+ if (!ast_strlen_zero(cache->name)) {
+ ao2_unlink(caches, cache);
+ }
+
+ if (cache->object_lifetime_maximum) {
+ /* If object lifetime support is enabled we need to explicitly drop all cached objects here
+ * and stop the scheduled task. Failure to do so could potentially keep the cache around for
+ * a prolonged period of time.
+ */
+ ao2_wrlock(cache->objects);
+ ao2_callback(cache->objects, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE,
+ NULL, NULL);
+ AST_SCHED_DEL_UNREF(sched, cache->expire_id, ao2_ref(cache, -1));
+ ao2_unlock(cache->objects);
+ }
+
+ ao2_ref(cache, -1);
+}
+
+/*!
+ * \internal
+ * \brief CLI tab completion for cache names
+ */
+static char *sorcery_memory_cache_complete_name(const char *word, int state)
+{
+ struct sorcery_memory_cache *cache;
+ struct ao2_iterator it_caches;
+ int wordlen = strlen(word);
+ int which = 0;
+ char *result = NULL;
+
+ it_caches = ao2_iterator_init(caches, 0);
+ while ((cache = ao2_iterator_next(&it_caches))) {
+ if (!strncasecmp(word, cache->name, wordlen)
+ && ++which > state) {
+ result = ast_strdup(cache->name);
+ }
+ ao2_ref(cache, -1);
+ if (result) {
+ break;
+ }
+ }
+ ao2_iterator_destroy(&it_caches);
+ return result;
+}
+
+/*!
+ * \internal
+ * \brief CLI command implementation for 'sorcery memory cache show'
+ */
+static char *sorcery_memory_cache_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct sorcery_memory_cache *cache;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "sorcery memory cache show";
+ e->usage =
+ "Usage: sorcery memory cache show <name>\n"
+ " Show sorcery memory cache configuration and statistics.\n";
+ return NULL;
+ case CLI_GENERATE:
+ if (a->pos == 4) {
+ return sorcery_memory_cache_complete_name(a->word, a->n);
+ } else {
+ return NULL;
+ }
+ }
+
+ if (a->argc != 5) {
+ return CLI_SHOWUSAGE;
+ }
+
+ cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY);
+ if (!cache) {
+ ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]);
+ return CLI_FAILURE;
+ }
+
+ ast_cli(a->fd, "Sorcery memory cache: %s\n", cache->name);
+ ast_cli(a->fd, "Number of objects within cache: %d\n", ao2_container_count(cache->objects));
+ if (cache->maximum_objects) {
+ ast_cli(a->fd, "Maximum allowed objects: %d\n", cache->maximum_objects);
+ } else {
+ ast_cli(a->fd, "There is no limit on the maximum number of objects in the cache\n");
+ }
+ if (cache->object_lifetime_maximum) {
+ ast_cli(a->fd, "Number of seconds before object expires: %d\n", cache->object_lifetime_maximum);
+ } else {
+ ast_cli(a->fd, "Object expiration is not enabled - cached objects will not expire\n");
+ }
+ if (cache->object_lifetime_stale) {
+ ast_cli(a->fd, "Number of seconds before object becomes stale: %d\n", cache->object_lifetime_stale);
+ } else {
+ ast_cli(a->fd, "Object staleness is not enabled - cached objects will not go stale\n");
+ }
+ ast_cli(a->fd, "Expire all objects on reload: %s\n", AST_CLI_ONOFF(cache->expire_on_reload));
+
+ ao2_ref(cache, -1);
+
+ return CLI_SUCCESS;
+}
+
+/*! \brief Structure used to pass data for printing cached object information */
+struct print_object_details {
+ /*! \brief The sorcery memory cache */
+ struct sorcery_memory_cache *cache;
+ /*! \brief The CLI arguments */
+ struct ast_cli_args *a;
+};
+
+/*!
+ * \internal
+ * \brief Callback function for displaying object within the cache
+ */
+static int sorcery_memory_cache_print_object(void *obj, void *arg, int flags)
+{
+#define FORMAT "%-25.25s %-15u %-15u \n"
+ struct sorcery_memory_cached_object *cached = obj;
+ struct print_object_details *details = arg;
+ int seconds_until_expire = 0, seconds_until_stale = 0;
+
+ if (details->cache->object_lifetime_maximum) {
+ seconds_until_expire = ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(details->cache->object_lifetime_maximum, 1)), ast_tvnow()) / 1000;
+ }
+ if (details->cache->object_lifetime_stale) {
+ seconds_until_stale = ast_tvdiff_ms(ast_tvadd(cached->created, ast_samp2tv(details->cache->object_lifetime_stale, 1)), ast_tvnow()) / 1000;
+ }
+
+ ast_cli(details->a->fd, FORMAT, ast_sorcery_object_get_id(cached->object), MAX(seconds_until_stale, 0), MAX(seconds_until_expire, 0));
+
+ return CMP_MATCH;
+#undef FORMAT
+}
+
+/*!
+ * \internal
+ * \brief CLI command implementation for 'sorcery memory cache dump'
+ */
+static char *sorcery_memory_cache_dump(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+#define FORMAT "%-25.25s %-15.15s %-15.15s \n"
+ struct sorcery_memory_cache *cache;
+ struct print_object_details details;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "sorcery memory cache dump";
+ e->usage =
+ "Usage: sorcery memory cache dump <name>\n"
+ " Dump a list of the objects within the cache, listed by object identifier.\n";
+ return NULL;
+ case CLI_GENERATE:
+ if (a->pos == 4) {
+ return sorcery_memory_cache_complete_name(a->word, a->n);
+ } else {
+ return NULL;
+ }
+ }
+
+ if (a->argc != 5) {
+ return CLI_SHOWUSAGE;
+ }
+
+ cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY);
+ if (!cache) {
+ ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]);
+ return CLI_FAILURE;
+ }
+
+ details.cache = cache;
+ details.a = a;
+
+ ast_cli(a->fd, "Dumping sorcery memory cache '%s':\n", cache->name);
+ if (!cache->object_lifetime_stale) {
+ ast_cli(a->fd, " * Staleness is not enabled - objects will not go stale\n");
+ }
+ if (!cache->object_lifetime_maximum) {
+ ast_cli(a->fd, " * Object lifetime is not enabled - objects will not expire\n");
+ }
+ ast_cli(a->fd, FORMAT, "Object Name", "Stale In", "Expires In");
+ ast_cli(a->fd, FORMAT, "-------------------------", "---------------", "---------------");
+ ao2_callback(cache->objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_memory_cache_print_object, &details);
+ ast_cli(a->fd, FORMAT, "-------------------------", "---------------", "---------------");
+ ast_cli(a->fd, "Total number of objects cached: %d\n", ao2_container_count(cache->objects));
+
+ ao2_ref(cache, -1);
+
+ return CLI_SUCCESS;
+#undef FORMAT
+}
+
+/*!
+ * \internal
+ * \brief CLI tab completion for cached object names
+ */
+static char *sorcery_memory_cache_complete_object_name(const char *cache_name, const char *word, int state)
+{
+ struct sorcery_memory_cache *cache;
+ struct sorcery_memory_cached_object *cached;
+ struct ao2_iterator it_cached;
+ int wordlen = strlen(word);
+ int which = 0;
+ char *result = NULL;
+
+ cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY);
+ if (!cache) {
+ return NULL;
+ }
+
+ it_cached = ao2_iterator_init(cache->objects, 0);
+ while ((cached = ao2_iterator_next(&it_cached))) {
+ if (!strncasecmp(word, ast_sorcery_object_get_id(cached->object), wordlen)
+ && ++which > state) {
+ result = ast_strdup(ast_sorcery_object_get_id(cached->object));
+ }
+ ao2_ref(cached, -1);
+ if (result) {
+ break;
+ }
+ }
+ ao2_iterator_destroy(&it_cached);
+
+ ao2_ref(cache, -1);
+
+ return result;
+}
+
+/*!
+ * \internal
+ * \brief CLI command implementation for 'sorcery memory cache expire'
+ */
+static char *sorcery_memory_cache_expire(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct sorcery_memory_cache *cache;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "sorcery memory cache expire";
+ e->usage =
+ "Usage: sorcery memory cache expire <cache name> [object name]\n"
+ " Expire a specific object or ALL objects within a sorcery memory cache.\n";
+ return NULL;
+ case CLI_GENERATE:
+ if (a->pos == 4) {
+ return sorcery_memory_cache_complete_name(a->word, a->n);
+ } else if (a->pos == 5) {
+ return sorcery_memory_cache_complete_object_name(a->argv[4], a->word, a->n);
+ } else {
+ return NULL;
+ }
+ }
+
+ if (a->argc > 6) {
+ return CLI_SHOWUSAGE;
+ }
+
+ cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY);
+ if (!cache) {
+ ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]);
+ return CLI_FAILURE;
+ }
+
+ ao2_wrlock(cache->objects);
+ if (a->argc == 5) {
+ remove_all_from_cache(cache);
+ ast_cli(a->fd, "All objects have been removed from cache '%s'\n", a->argv[4]);
+ } else {
+ if (!remove_from_cache(cache, a->argv[5], 1)) {
+ ast_cli(a->fd, "Successfully expired object '%s' from cache '%s'\n", a->argv[5], a->argv[4]);
+ } else {
+ ast_cli(a->fd, "Object '%s' was not expired from cache '%s' as it was not found\n", a->argv[5],
+ a->argv[4]);
+ }
+ }
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ return CLI_SUCCESS;
+}
+
+/*!
+ * \internal
+ * \brief CLI command implementation for 'sorcery memory cache stale'
+ */
+static char *sorcery_memory_cache_stale(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct sorcery_memory_cache *cache;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "sorcery memory cache stale";
+ e->usage =
+ "Usage: sorcery memory cache stale <cache name> [object name]\n"
+ " Mark a specific object or ALL objects as stale in a sorcery memory cache.\n";
+ return NULL;
+ case CLI_GENERATE:
+ if (a->pos == 4) {
+ return sorcery_memory_cache_complete_name(a->word, a->n);
+ } else if (a->pos == 5) {
+ return sorcery_memory_cache_complete_object_name(a->argv[4], a->word, a->n);
+ } else {
+ return NULL;
+ }
+ }
+
+ if (a->argc > 6) {
+ return CLI_SHOWUSAGE;
+ }
+
+ cache = ao2_find(caches, a->argv[4], OBJ_SEARCH_KEY);
+ if (!cache) {
+ ast_cli(a->fd, "Specified sorcery memory cache '%s' does not exist\n", a->argv[4]);
+ return CLI_FAILURE;
+ }
+
+ if (!cache->object_lifetime_stale) {
+ ast_cli(a->fd, "Specified sorcery memory cache '%s' does not have staleness enabled\n", a->argv[4]);
+ ao2_ref(cache, -1);
+ return CLI_FAILURE;
+ }
+
+ ao2_rdlock(cache->objects);
+ if (a->argc == 5) {
+ mark_all_as_stale_in_cache(cache);
+ ast_cli(a->fd, "Marked all objects in sorcery memory cache '%s' as stale\n", a->argv[4]);
+ } else {
+ if (!mark_object_as_stale_in_cache(cache, a->argv[5])) {
+ ast_cli(a->fd, "Successfully marked object '%s' in memory cache '%s' as stale\n",
+ a->argv[5], a->argv[4]);
+ } else {
+ ast_cli(a->fd, "Object '%s' in sorcery memory cache '%s' could not be marked as stale as it was not found\n",
+ a->argv[5], a->argv[4]);
+ }
+ }
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_memory_cache[] = {
+ AST_CLI_DEFINE(sorcery_memory_cache_show, "Show sorcery memory cache information"),
+ AST_CLI_DEFINE(sorcery_memory_cache_dump, "Dump all objects within a sorcery memory cache"),
+ AST_CLI_DEFINE(sorcery_memory_cache_expire, "Expire a specific object or ALL objects within a sorcery memory cache"),
+ AST_CLI_DEFINE(sorcery_memory_cache_stale, "Mark a specific object or ALL objects as stale within a sorcery memory cache"),
+};
+
+/*!
+ * \internal
+ * \brief AMI command implementation for 'SorceryMemoryCacheExpireObject'
+ */
+static int sorcery_memory_cache_ami_expire_object(struct mansession *s, const struct message *m)
+{
+ const char *cache_name = astman_get_header(m, "Cache");
+ const char *object_name = astman_get_header(m, "Object");
+ struct sorcery_memory_cache *cache;
+ int res;
+
+ if (ast_strlen_zero(cache_name)) {
+ astman_send_error(s, m, "SorceryMemoryCacheExpireObject requires that a cache name be provided.\n");
+ return 0;
+ } else if (ast_strlen_zero(object_name)) {
+ astman_send_error(s, m, "SorceryMemoryCacheExpireObject requires that an object name be provided\n");
+ return 0;
+ }
+
+ cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY);
+ if (!cache) {
+ astman_send_error(s, m, "The provided cache does not exist\n");
+ return 0;
+ }
+
+ ao2_wrlock(cache->objects);
+ res = remove_from_cache(cache, object_name, 1);
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ if (!res) {
+ astman_send_ack(s, m, "The provided object was expired from the cache\n");
+ } else {
+ astman_send_error(s, m, "The provided object could not be expired from the cache\n");
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief AMI command implementation for 'SorceryMemoryCacheExpire'
+ */
+static int sorcery_memory_cache_ami_expire(struct mansession *s, const struct message *m)
+{
+ const char *cache_name = astman_get_header(m, "Cache");
+ struct sorcery_memory_cache *cache;
+
+ if (ast_strlen_zero(cache_name)) {
+ astman_send_error(s, m, "SorceryMemoryCacheExpire requires that a cache name be provided.\n");
+ return 0;
+ }
+
+ cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY);
+ if (!cache) {
+ astman_send_error(s, m, "The provided cache does not exist\n");
+ return 0;
+ }
+
+ ao2_wrlock(cache->objects);
+ remove_all_from_cache(cache);
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ astman_send_ack(s, m, "All objects were expired from the cache\n");
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief AMI command implementation for 'SorceryMemoryCacheStaleObject'
+ */
+static int sorcery_memory_cache_ami_stale_object(struct mansession *s, const struct message *m)
+{
+ const char *cache_name = astman_get_header(m, "Cache");
+ const char *object_name = astman_get_header(m, "Object");
+ struct sorcery_memory_cache *cache;
+ int res;
+
+ if (ast_strlen_zero(cache_name)) {
+ astman_send_error(s, m, "SorceryMemoryCacheStaleObject requires that a cache name be provided.\n");
+ return 0;
+ } else if (ast_strlen_zero(object_name)) {
+ astman_send_error(s, m, "SorceryMemoryCacheStaleObject requires that an object name be provided\n");
+ return 0;
+ }
+
+ cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY);
+ if (!cache) {
+ astman_send_error(s, m, "The provided cache does not exist\n");
+ return 0;
+ }
+
+ ao2_rdlock(cache->objects);
+ res = mark_object_as_stale_in_cache(cache, object_name);
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ if (!res) {
+ astman_send_ack(s, m, "The provided object was marked as stale in the cache\n");
+ } else {
+ astman_send_error(s, m, "The provided object could not be marked as stale in the cache\n");
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief AMI command implementation for 'SorceryMemoryCacheStale'
+ */
+static int sorcery_memory_cache_ami_stale(struct mansession *s, const struct message *m)
+{
+ const char *cache_name = astman_get_header(m, "Cache");
+ struct sorcery_memory_cache *cache;
+
+ if (ast_strlen_zero(cache_name)) {
+ astman_send_error(s, m, "SorceryMemoryCacheStale requires that a cache name be provided.\n");
+ return 0;
+ }
+
+ cache = ao2_find(caches, cache_name, OBJ_SEARCH_KEY);
+ if (!cache) {
+ astman_send_error(s, m, "The provided cache does not exist\n");
+ return 0;
+ }
+
+ ao2_rdlock(cache->objects);
+ mark_all_as_stale_in_cache(cache);
+ ao2_unlock(cache->objects);
+
+ ao2_ref(cache, -1);
+
+ astman_send_ack(s, m, "All objects were marked as stale in the cache\n");
+
+ return 0;
+}
+
+#ifdef TEST_FRAMEWORK
+
+/*! \brief Dummy sorcery object */
+struct test_sorcery_object {
+ SORCERY_OBJECT(details);
+};
+
+/*!
+ * \internal
+ * \brief Allocator for test object
+ *
+ * \param id The identifier for the object
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+static void *test_sorcery_object_alloc(const char *id)
+{
+ return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), NULL);
+}
+
+/*!
+ * \internal
+ * \brief Allocator for test sorcery instance
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+static struct ast_sorcery *alloc_and_initialize_sorcery(void)
+{
+ struct ast_sorcery *sorcery;
+
+ if (!(sorcery = ast_sorcery_open())) {
+ return NULL;
+ }
+
+ if ((ast_sorcery_apply_default(sorcery, "test", "memory", NULL) != AST_SORCERY_APPLY_SUCCESS) ||
+ ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
+ ast_sorcery_unref(sorcery);
+ return NULL;
+ }
+
+ return sorcery;
+}
+
+AST_TEST_DEFINE(open_with_valid_options)
+{
+ int res = AST_TEST_PASS;
+ struct sorcery_memory_cache *cache;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "open_with_valid_options";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Attempt to create sorcery memory caches using valid options";
+ info->description = "This test performs the following:\n"
+ "\t* Creates a memory cache with default configuration\n"
+ "\t* Creates a memory cache with a maximum object count of 10 and verifies it\n"
+ "\t* Creates a memory cache with a maximum object lifetime of 60 and verifies it\n"
+ "\t* Creates a memory cache with a stale object lifetime of 90 and verifies it\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache using default configuration\n");
+ res = AST_TEST_FAIL;
+ } else {
+ sorcery_memory_cache_close(cache);
+ }
+
+ cache = sorcery_memory_cache_open("maximum_objects=10");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache with a maximum object count of 10\n");
+ res = AST_TEST_FAIL;
+ } else {
+ if (cache->maximum_objects != 10) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a maximum object count of 10 but it has '%u'\n",
+ cache->maximum_objects);
+ }
+ sorcery_memory_cache_close(cache);
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_maximum=60");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache with a maximum object lifetime of 60\n");
+ res = AST_TEST_FAIL;
+ } else {
+ if (cache->object_lifetime_maximum != 60) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a maximum object lifetime of 60 but it has '%u'\n",
+ cache->object_lifetime_maximum);
+ }
+ sorcery_memory_cache_close(cache);
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_stale=90");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache with a stale object lifetime of 90\n");
+ res = AST_TEST_FAIL;
+ } else {
+ if (cache->object_lifetime_stale != 90) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a stale object lifetime of 90 but it has '%u'\n",
+ cache->object_lifetime_stale);
+ }
+ sorcery_memory_cache_close(cache);
+ }
+
+
+ return res;
+}
+
+AST_TEST_DEFINE(open_with_invalid_options)
+{
+ int res = AST_TEST_PASS;
+ struct sorcery_memory_cache *cache;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "open_with_invalid_options";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Attempt to create sorcery memory caches using invalid options";
+ info->description = "This test attempts to perform the following:\n"
+ "\t* Create a memory cache with an empty name\n"
+ "\t* Create a memory cache with a maximum object count of -1\n"
+ "\t* Create a memory cache with a maximum object count of toast\n"
+ "\t* Create a memory cache with a maximum object lifetime of -1\n"
+ "\t* Create a memory cache with a maximum object lifetime of toast\n"
+ "\t* Create a memory cache with a stale object lifetime of -1\n"
+ "\t* Create a memory cache with a stale object lifetime of toast\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("name=");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with an empty name\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("maximum_objects=-1");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a maximum object count of -1\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("maximum_objects=toast");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a maximum object count of toast\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_maximum=-1");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with an object lifetime maximum of -1\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_maximum=toast");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with an object lifetime maximum of toast\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_stale=-1");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a stale object lifetime of -1\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_stale=toast");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with a stale object lifetime of toast\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ cache = sorcery_memory_cache_open("tacos");
+ if (cache) {
+ ast_test_status_update(test, "Created a sorcery memory cache with an invalid configuration option 'tacos'\n");
+ sorcery_memory_cache_close(cache);
+ res = AST_TEST_FAIL;
+ }
+
+ return res;
+}
+
+AST_TEST_DEFINE(create_and_retrieve)
+{
+ int res = AST_TEST_FAIL;
+ struct ast_sorcery *sorcery = NULL;
+ struct sorcery_memory_cache *cache = NULL;
+ RAII_VAR(void *, object, NULL, ao2_cleanup);
+ RAII_VAR(void *, cached_object, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "create";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Attempt to create an object in the cache";
+ info->description = "This test performs the following:\n"
+ "\t* Creates a memory cache with default options\n"
+ "\t* Creates a sorcery instance with a test object\n"
+ "\t* Creates a test object with an id of test\n"
+ "\t* Pushes the test object into the memory cache\n"
+ "\t* Confirms that the test object is in the cache\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n");
+ goto cleanup;
+ }
+
+ if (ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Memory cache contains cached objects before we added one\n");
+ goto cleanup;
+ }
+
+ sorcery = alloc_and_initialize_sorcery();
+ if (!sorcery) {
+ ast_test_status_update(test, "Failed to create a test sorcery instance\n");
+ goto cleanup;
+ }
+
+ object = ast_sorcery_alloc(sorcery, "test", "test");
+ if (!object) {
+ ast_test_status_update(test, "Failed to allocate a test object\n");
+ goto cleanup;
+ }
+
+ sorcery_memory_cache_create(sorcery, cache, object);
+
+ if (!ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Added test object to memory cache but cache remains empty\n");
+ goto cleanup;
+ }
+
+ cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test");
+ if (!cached_object) {
+ ast_test_status_update(test, "Object placed into memory cache could not be retrieved\n");
+ goto cleanup;
+ }
+
+ if (cached_object != object) {
+ ast_test_status_update(test, "Object retrieved from memory cached is not the one we cached\n");
+ goto cleanup;
+ }
+
+ res = AST_TEST_PASS;
+
+cleanup:
+ if (cache) {
+ sorcery_memory_cache_close(cache);
+ }
+ if (sorcery) {
+ ast_sorcery_unref(sorcery);
+ }
+
+ return res;
+}
+
+AST_TEST_DEFINE(update)
+{
+ int res = AST_TEST_FAIL;
+ struct ast_sorcery *sorcery = NULL;
+ struct sorcery_memory_cache *cache = NULL;
+ RAII_VAR(void *, original_object, NULL, ao2_cleanup);
+ RAII_VAR(void *, updated_object, NULL, ao2_cleanup);
+ RAII_VAR(void *, cached_object, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "create";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Attempt to create and then update an object in the cache";
+ info->description = "This test performs the following:\n"
+ "\t* Creates a memory cache with default options\n"
+ "\t* Creates a sorcery instance with a test object\n"
+ "\t* Creates a test object with an id of test\n"
+ "\t* Pushes the test object into the memory cache\n"
+ "\t* Confirms that the test object is in the cache\n"
+ "\t* Creates a new test object with the same id of test\n"
+ "\t* Pushes the new test object into the memory cache\n"
+ "\t* Confirms that the new test object has replaced the old one\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n");
+ goto cleanup;
+ }
+
+ if (ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Memory cache contains cached objects before we added one\n");
+ goto cleanup;
+ }
+
+ sorcery = alloc_and_initialize_sorcery();
+ if (!sorcery) {
+ ast_test_status_update(test, "Failed to create a test sorcery instance\n");
+ goto cleanup;
+ }
+
+ original_object = ast_sorcery_alloc(sorcery, "test", "test");
+ if (!original_object) {
+ ast_test_status_update(test, "Failed to allocate a test object\n");
+ goto cleanup;
+ }
+
+ sorcery_memory_cache_create(sorcery, cache, original_object);
+
+ updated_object = ast_sorcery_alloc(sorcery, "test", "test");
+ if (!updated_object) {
+ ast_test_status_update(test, "Failed to allocate an updated test object\n");
+ goto cleanup;
+ }
+
+ sorcery_memory_cache_create(sorcery, cache, updated_object);
+
+ if (ao2_container_count(cache->objects) != 1) {
+ ast_test_status_update(test, "Added updated test object to memory cache but cache now contains %d objects instead of 1\n",
+ ao2_container_count(cache->objects));
+ goto cleanup;
+ }
+
+ cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test");
+ if (!cached_object) {
+ ast_test_status_update(test, "Updated object placed into memory cache could not be retrieved\n");
+ goto cleanup;
+ }
+
+ if (cached_object == original_object) {
+ ast_test_status_update(test, "Updated object placed into memory cache but old one is being retrieved\n");
+ goto cleanup;
+ } else if (cached_object != updated_object) {
+ ast_test_status_update(test, "Updated object placed into memory cache but different one is being retrieved\n");
+ goto cleanup;
+ }
+
+ res = AST_TEST_PASS;
+
+cleanup:
+ if (cache) {
+ sorcery_memory_cache_close(cache);
+ }
+ if (sorcery) {
+ ast_sorcery_unref(sorcery);
+ }
+
+ return res;
+}
+
+AST_TEST_DEFINE(delete)
+{
+ int res = AST_TEST_FAIL;
+ struct ast_sorcery *sorcery = NULL;
+ struct sorcery_memory_cache *cache = NULL;
+ RAII_VAR(void *, object, NULL, ao2_cleanup);
+ RAII_VAR(void *, cached_object, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "delete";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Attempt to create and then delete an object in the cache";
+ info->description = "This test performs the following:\n"
+ "\t* Creates a memory cache with default options\n"
+ "\t* Creates a sorcery instance with a test object\n"
+ "\t* Creates a test object with an id of test\n"
+ "\t* Pushes the test object into the memory cache\n"
+ "\t* Confirms that the test object is in the cache\n"
+ "\t* Deletes the test object from the cache\n"
+ "\t* Confirms that the test object is no longer in the cache\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n");
+ goto cleanup;
+ }
+
+ if (ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Memory cache contains cached objects before we added one\n");
+ goto cleanup;
+ }
+
+ sorcery = alloc_and_initialize_sorcery();
+ if (!sorcery) {
+ ast_test_status_update(test, "Failed to create a test sorcery instance\n");
+ goto cleanup;
+ }
+
+ object = ast_sorcery_alloc(sorcery, "test", "test");
+ if (!object) {
+ ast_test_status_update(test, "Failed to allocate a test object\n");
+ goto cleanup;
+ }
+
+ sorcery_memory_cache_create(sorcery, cache, object);
+
+ if (!ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Added test object to memory cache but cache contains no objects\n");
+ goto cleanup;
+ }
+
+ cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test");
+ if (!cached_object) {
+ ast_test_status_update(test, "Test object placed into memory cache could not be retrieved\n");
+ goto cleanup;
+ }
+
+ ao2_ref(cached_object, -1);
+ cached_object = NULL;
+
+ sorcery_memory_cache_delete(sorcery, cache, object);
+
+ cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", "test");
+ if (cached_object) {
+ ast_test_status_update(test, "Test object deleted from memory cache can still be retrieved\n");
+ goto cleanup;
+ }
+
+ res = AST_TEST_PASS;
+
+cleanup:
+ if (cache) {
+ sorcery_memory_cache_close(cache);
+ }
+ if (sorcery) {
+ ast_sorcery_unref(sorcery);
+ }
+
+ return res;
+}
+
+static int check_cache_content(struct ast_test *test, struct ast_sorcery *sorcery, struct sorcery_memory_cache *cache,
+ const char **in_cache, size_t num_in_cache, const char **not_in_cache, size_t num_not_in_cache)
+{
+ int i;
+ int res = 0;
+ RAII_VAR(void *, cached_object, NULL, ao2_cleanup);
+
+ for (i = 0; i < num_in_cache; ++i) {
+ cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", in_cache[i]);
+ if (!cached_object) {
+ ast_test_status_update(test, "Failed to retrieve '%s' object from the cache\n",
+ in_cache[i]);
+ res = -1;
+ }
+ ao2_ref(cached_object, -1);
+ }
+
+ for (i = 0; i < num_not_in_cache; ++i) {
+ cached_object = sorcery_memory_cache_retrieve_id(sorcery, cache, "test", not_in_cache[i]);
+ if (cached_object) {
+ ast_test_status_update(test, "Retrieved '%s' object from the cache unexpectedly\n",
+ not_in_cache[i]);
+ ao2_ref(cached_object, -1);
+ res = -1;
+ }
+ }
+
+ return res;
+}
+
+AST_TEST_DEFINE(maximum_objects)
+{
+ int res = AST_TEST_FAIL;
+ struct ast_sorcery *sorcery = NULL;
+ struct sorcery_memory_cache *cache = NULL;
+ RAII_VAR(void *, alice, NULL, ao2_cleanup);
+ RAII_VAR(void *, bob, NULL, ao2_cleanup);
+ RAII_VAR(void *, charlie, NULL, ao2_cleanup);
+ RAII_VAR(void *, cached_object, NULL, ao2_cleanup);
+ const char *in_cache[2];
+ const char *not_in_cache[2];
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "maximum_objects";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Ensure that the 'maximum_objects' option works as expected";
+ info->description = "This test performs the following:\n"
+ "\t* Creates a memory cache with maximum_objects=2\n"
+ "\t* Creates a sorcery instance\n"
+ "\t* Creates a three test objects: alice, bob, charlie, and david\n"
+ "\t* Pushes alice and bob into the memory cache\n"
+ "\t* Confirms that alice and bob are in the memory cache\n"
+ "\t* Pushes charlie into the memory cache\n"
+ "\t* Confirms that bob and charlie are in the memory cache\n"
+ "\t* Deletes charlie from the memory cache\n"
+ "\t* Confirms that only bob is in the memory cache\n"
+ "\t* Pushes alice into the memory cache\n"
+ "\t* Confirms that bob and alice are in the memory cache\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("maximum_objects=2");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache with maximum_objects=2\n");
+ goto cleanup;
+ }
+
+ if (ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Memory cache contains cached objects before we added one\n");
+ goto cleanup;
+ }
+
+ sorcery = alloc_and_initialize_sorcery();
+ if (!sorcery) {
+ ast_test_status_update(test, "Failed to create a test sorcery instance\n");
+ goto cleanup;
+ }
+
+ alice = ast_sorcery_alloc(sorcery, "test", "alice");
+ bob = ast_sorcery_alloc(sorcery, "test", "bob");
+ charlie = ast_sorcery_alloc(sorcery, "test", "charlie");
+
+ if (!alice || !bob || !charlie) {
+ ast_test_status_update(test, "Failed to allocate sorcery object(s)\n");
+ goto cleanup;
+ }
+
+ sorcery_memory_cache_create(sorcery, cache, alice);
+ in_cache[0] = "alice";
+ in_cache[1] = NULL;
+ not_in_cache[0] = "bob";
+ not_in_cache[1] = "charlie";
+ if (check_cache_content(test, sorcery, cache, in_cache, 1, not_in_cache, 2)) {
+ goto cleanup;
+ }
+
+ /* Delays are added to ensure that we are not adding cache entries within the
+ * same microsecond
+ */
+ usleep(1000);
+
+ sorcery_memory_cache_create(sorcery, cache, bob);
+ in_cache[0] = "alice";
+ in_cache[1] = "bob";
+ not_in_cache[0] = "charlie";
+ not_in_cache[1] = NULL;
+ if (check_cache_content(test, sorcery, cache, in_cache, 2, not_in_cache, 1)) {
+ goto cleanup;
+ }
+
+ usleep(1000);
+
+ sorcery_memory_cache_create(sorcery, cache, charlie);
+ in_cache[0] = "bob";
+ in_cache[1] = "charlie";
+ not_in_cache[0] = "alice";
+ not_in_cache[1] = NULL;
+ if (check_cache_content(test, sorcery, cache, in_cache, 2, not_in_cache, 1)) {
+ goto cleanup;
+ }
+ usleep(1000);
+
+ sorcery_memory_cache_delete(sorcery, cache, charlie);
+ in_cache[0] = "bob";
+ in_cache[1] = NULL;
+ not_in_cache[0] = "alice";
+ not_in_cache[1] = "charlie";
+ if (check_cache_content(test, sorcery, cache, in_cache, 1, not_in_cache, 2)) {
+ goto cleanup;
+ }
+ usleep(1000);
+
+ sorcery_memory_cache_create(sorcery, cache, alice);
+ in_cache[0] = "bob";
+ in_cache[1] = "alice";
+ not_in_cache[0] = "charlie";
+ not_in_cache[1] = NULL;
+ if (check_cache_content(test, sorcery, cache, in_cache, 2, not_in_cache, 1)) {
+ goto cleanup;
+ }
+
+ res = AST_TEST_PASS;
+
+cleanup:
+ if (cache) {
+ sorcery_memory_cache_close(cache);
+ }
+ if (sorcery) {
+ ast_sorcery_unref(sorcery);
+ }
+
+ return res;
+}
+
+AST_TEST_DEFINE(expiration)
+{
+ int res = AST_TEST_FAIL;
+ struct ast_sorcery *sorcery = NULL;
+ struct sorcery_memory_cache *cache = NULL;
+ int i;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "expiration";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Add objects to a cache configured with maximum lifetime, confirm they are removed";
+ info->description = "This test performs the following:\n"
+ "\t* Creates a memory cache with a maximum object lifetime of 5 seconds\n"
+ "\t* Pushes 10 objects into the memory cache\n"
+ "\t* Waits (up to) 10 seconds for expiration to occur\n"
+ "\t* Confirms that the objects have been removed from the cache\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ cache = sorcery_memory_cache_open("object_lifetime_maximum=5");
+ if (!cache) {
+ ast_test_status_update(test, "Failed to create a sorcery memory cache using default options\n");
+ goto cleanup;
+ }
+
+ sorcery = alloc_and_initialize_sorcery();
+ if (!sorcery) {
+ ast_test_status_update(test, "Failed to create a test sorcery instance\n");
+ goto cleanup;
+ }
+
+ cache->cache_notify = 1;
+ ast_mutex_init(&cache->lock);
+ ast_cond_init(&cache->cond, NULL);
+
+ for (i = 0; i < 5; ++i) {
+ char uuid[AST_UUID_STR_LEN];
+ void *object;
+
+ object = ast_sorcery_alloc(sorcery, "test", ast_uuid_generate_str(uuid, sizeof(uuid)));
+ if (!object) {
+ ast_test_status_update(test, "Failed to allocate test object for expiration\n");
+ goto cleanup;
+ }
+
+ sorcery_memory_cache_create(sorcery, cache, object);
+
+ ao2_ref(object, -1);
+ }
+
+ ast_mutex_lock(&cache->lock);
+ while (!cache->cache_completed) {
+ struct timeval start = ast_tvnow();
+ struct timespec end = {
+ .tv_sec = start.tv_sec + 10,
+ .tv_nsec = start.tv_usec * 1000,
+ };
+
+ if (ast_cond_timedwait(&cache->cond, &cache->lock, &end) == ETIMEDOUT) {
+ break;
+ }
+ }
+ ast_mutex_unlock(&cache->lock);
+
+ if (ao2_container_count(cache->objects)) {
+ ast_test_status_update(test, "Objects placed into the memory cache did not expire and get removed\n");
+ goto cleanup;
+ }
+
+ res = AST_TEST_PASS;
+
+cleanup:
+ if (cache) {
+ if (cache->cache_notify) {
+ ast_cond_destroy(&cache->cond);
+ ast_mutex_destroy(&cache->lock);
+ }
+ sorcery_memory_cache_close(cache);
+ }
+ if (sorcery) {
+ ast_sorcery_unref(sorcery);
+ }
+
+ return res;
+}
+
+/*!
+ * \brief Backend data that the mock sorcery wizard uses to create objects
+ */
+static struct backend_data {
+ /*! An arbitrary data field */
+ int salt;
+ /*! Another arbitrary data field */
+ int pepper;
+ /*! Indicates whether the backend has data */
+ int exists;
+} *real_backend_data;
+
+/*!
+ * \brief Sorcery object created based on backend data
+ */
+struct test_data {
+ SORCERY_OBJECT(details);
+ /*! Mirrors the backend data's salt field */
+ int salt;
+ /*! Mirrors the backend data's pepper field */
+ int pepper;
+};
+
+/*!
+ * \brief Allocation callback for test_data sorcery object
+ */
+static void *test_data_alloc(const char *id) {
+ return ast_sorcery_generic_alloc(sizeof(struct test_data), NULL);
+}
+
+/*!
+ * \brief Callback for retrieving sorcery object by ID
+ *
+ * The mock wizard uses the \ref real_backend_data in order to construct
+ * objects. If the backend data is "nonexisent" then no object is returned.
+ * Otherwise, an object is created that has the backend data's salt and
+ * pepper values copied.
+ *
+ * \param sorcery The sorcery instance
+ * \param data Unused
+ * \param type The object type. Will always be "test".
+ * \param id The object id. Will always be "test".
+ *
+ * \retval NULL Backend data does not exist
+ * \retval non-NULL An object representing the backend data
+ */
+static void *mock_retrieve_id(const struct ast_sorcery *sorcery, void *data,
+ const char *type, const char *id)
+{
+ struct test_data *b_data;
+
+ if (!real_backend_data->exists) {
+ return NULL;
+ }
+
+ b_data = ast_sorcery_alloc(sorcery, type, id);
+ if (!b_data) {
+ return NULL;
+ }
+
+ b_data->salt = real_backend_data->salt;
+ b_data->pepper = real_backend_data->pepper;
+ return b_data;
+}
+
+/*!
+ * \brief A mock sorcery wizard used for the stale test
+ */
+static struct ast_sorcery_wizard mock_wizard = {
+ .name = "mock",
+ .retrieve_id = mock_retrieve_id,
+};
+
+/*!
+ * \brief Wait for the cache to be updated after a stale object is retrieved.
+ *
+ * Since the cache does not know what type of objects it is dealing with, and
+ * since we do not have the internals of the cache, the only way to make this
+ * determination is to continuously retrieve an object from the cache until
+ * we retrieve a different object than we had previously retrieved.
+ *
+ * \param sorcery The sorcery instance
+ * \param previous_object The object we had previously retrieved from the cache
+ * \param[out] new_object The new object we retrieve from the cache
+ *
+ * \retval 0 Successfully retrieved a new object from the cache
+ * \retval non-zero Failed to retrieve a new object from the cache
+ */
+static int wait_for_cache_update(const struct ast_sorcery *sorcery,
+ void *previous_object, struct test_data **new_object)
+{
+ struct timeval start = ast_tvnow();
+
+ while (ast_remaining_ms(start, 5000) > 0) {
+ void *object;
+
+ object = ast_sorcery_retrieve_by_id(sorcery, "test", "test");
+ if (object != previous_object) {
+ *new_object = object;
+ return 0;
+ }
+ ao2_cleanup(object);
+ }
+
+ return -1;
+}
+
+AST_TEST_DEFINE(stale)
+{
+ int res = AST_TEST_FAIL;
+ struct ast_sorcery *sorcery = NULL;
+ struct test_data *backend_object;
+ struct backend_data iterations[] = {
+ { .salt = 1, .pepper = 2, .exists = 1 },
+ { .salt = 568729, .pepper = -234123, .exists = 1 },
+ { .salt = 0, .pepper = 0, .exists = 0 },
+ };
+ struct backend_data initial = {
+ .salt = 0,
+ .pepper = 0,
+ .exists = 1,
+ };
+ int i;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "stale";
+ info->category = "/res/res_sorcery_memory_cache/";
+ info->summary = "Ensure that stale objects are replaced with updated objects";
+ info->description = "This test performs the following:\n"
+ "\t* Create a sorcery instance with two wizards"
+ "\t\t* The first is a memory cache that marks items stale after 3 seconds\n"
+ "\t\t* The second is a mock of a back-end\n"
+ "\t* Pre-populates the cache by retrieving some initial data from the backend.\n"
+ "\t* Performs iterations of the following:\n"
+ "\t\t* Update backend data with new values\n"
+ "\t\t* Retrieve item from the cache\n"
+ "\t\t* Ensure the retrieved item does not have the new backend values\n"
+ "\t\t* Wait for cached object to become stale\n"
+ "\t\t* Retrieve the stale cached object\n"
+ "\t\t* Ensure that the stale object retrieved is the same as the fresh one from earlier\n"
+ "\t\t* Wait for the cache to update with new data\n"
+ "\t\t* Ensure that new data in the cache matches backend data\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_sorcery_wizard_register(&mock_wizard);
+
+ sorcery = ast_sorcery_open();
+ if (!sorcery) {
+ ast_test_status_update(test, "Failed to create sorcery instance\n");
+ goto cleanup;
+ }
+
+ ast_sorcery_apply_wizard_mapping(sorcery, "test", "memory_cache",
+ "object_lifetime_stale=3", 1);
+ ast_sorcery_apply_wizard_mapping(sorcery, "test", "mock", NULL, 0);
+ ast_sorcery_internal_object_register(sorcery, "test", test_data_alloc, NULL, NULL);
+
+ /* Prepopulate the cache */
+ real_backend_data = &initial;
+
+ backend_object = ast_sorcery_retrieve_by_id(sorcery, "test", "test");
+ if (!backend_object) {
+ ast_test_status_update(test, "Unable to retrieve backend data and populate the cache\n");
+ goto cleanup;
+ }
+ ao2_ref(backend_object, -1);
+
+ for (i = 0; i < ARRAY_LEN(iterations); ++i) {
+ RAII_VAR(struct test_data *, cache_fresh, NULL, ao2_cleanup);
+ RAII_VAR(struct test_data *, cache_stale, NULL, ao2_cleanup);
+ RAII_VAR(struct test_data *, cache_new, NULL, ao2_cleanup);
+
+ real_backend_data = &iterations[i];
+
+ ast_test_status_update(test, "Begininning iteration %d\n", i);
+
+ cache_fresh = ast_sorcery_retrieve_by_id(sorcery, "test", "test");
+ if (!cache_fresh) {
+ ast_test_status_update(test, "Unable to retrieve fresh cached object\n");
+ goto cleanup;
+ }
+
+ if (cache_fresh->salt == iterations[i].salt || cache_fresh->pepper == iterations[i].pepper) {
+ ast_test_status_update(test, "Fresh cached object has unexpected values. Did we hit the backend?\n");
+ goto cleanup;
+ }
+
+ sleep(5);
+
+ cache_stale = ast_sorcery_retrieve_by_id(sorcery, "test", "test");
+ if (!cache_stale) {
+ ast_test_status_update(test, "Unable to retrieve stale cached object\n");
+ goto cleanup;
+ }
+
+ if (cache_stale != cache_fresh) {
+ ast_test_status_update(test, "Stale cache hit retrieved different object than fresh cache hit\n");
+ goto cleanup;
+ }
+
+ if (wait_for_cache_update(sorcery, cache_stale, &cache_new)) {
+ ast_test_status_update(test, "Cache was not updated\n");
+ goto cleanup;
+ }
+
+ if (iterations[i].exists) {
+ if (!cache_new) {
+ ast_test_status_update(test, "Failed to retrieve item from cache when there should be one present\n");
+ goto cleanup;
+ } else if (cache_new->salt != iterations[i].salt ||
+ cache_new->pepper != iterations[i].pepper) {
+ ast_test_status_update(test, "New cached item has unexpected values\n");
+ goto cleanup;
+ }
+ } else if (cache_new) {
+ ast_test_status_update(test, "Retrieved a cached item when there should not have been one present\n");
+ goto cleanup;
+ }
+ }
+
+ res = AST_TEST_PASS;
+
+cleanup:
+ if (sorcery) {
+ ast_sorcery_unref(sorcery);
+ }
+ ast_sorcery_wizard_unregister(&mock_wizard);
+ return res;
+}
+
+#endif
+
+static int unload_module(void)
+{
+ if (sched) {
+ ast_sched_context_destroy(sched);
+ sched = NULL;
+ }
+
+ ao2_cleanup(caches);
+
+ ast_sorcery_wizard_unregister(&memory_cache_object_wizard);
+
+ ast_cli_unregister_multiple(cli_memory_cache, ARRAY_LEN(cli_memory_cache));
+
+ ast_manager_unregister("SorceryMemoryCacheExpireObject");
+ ast_manager_unregister("SorceryMemoryCacheExpire");
+ ast_manager_unregister("SorceryMemoryCacheStaleObject");
+ ast_manager_unregister("SorceryMemoryCacheStale");
+
+ AST_TEST_UNREGISTER(open_with_valid_options);
+ AST_TEST_UNREGISTER(open_with_invalid_options);
+ AST_TEST_UNREGISTER(create_and_retrieve);
+ AST_TEST_UNREGISTER(update);
+ AST_TEST_UNREGISTER(delete);
+ AST_TEST_UNREGISTER(maximum_objects);
+ AST_TEST_UNREGISTER(expiration);
+ AST_TEST_UNREGISTER(stale);
+
+ return 0;
+}
+
+static int load_module(void)
+{
+ int res;
+
+ sched = ast_sched_context_create();
+ if (!sched) {
+ ast_log(LOG_ERROR, "Failed to create scheduler for cache management\n");
+ unload_module();
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ if (ast_sched_start_thread(sched)) {
+ ast_log(LOG_ERROR, "Failed to create scheduler thread for cache management\n");
+ unload_module();
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ caches = ao2_container_alloc(CACHES_CONTAINER_BUCKET_SIZE, sorcery_memory_cache_hash,
+ sorcery_memory_cache_cmp);
+ if (!caches) {
+ ast_log(LOG_ERROR, "Failed to create container for configured caches\n");
+ unload_module();
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ if (ast_sorcery_wizard_register(&memory_cache_object_wizard)) {
+ unload_module();
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ res = ast_cli_register_multiple(cli_memory_cache, ARRAY_LEN(cli_memory_cache));
+ res |= ast_manager_register_xml("SorceryMemoryCacheExpireObject", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_expire_object);
+ res |= ast_manager_register_xml("SorceryMemoryCacheExpire", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_expire);
+ res |= ast_manager_register_xml("SorceryMemoryCacheStaleObject", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_stale_object);
+ res |= ast_manager_register_xml("SorceryMemoryCacheStale", EVENT_FLAG_SYSTEM, sorcery_memory_cache_ami_stale);
+
+ if (res) {
+ unload_module();
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ /* This causes the stale unit test to execute last, so if a sorcery instance persists
+ * longer than expected subsequent unit tests don't fail when setting it up.
+ */
+ AST_TEST_REGISTER(stale);
+ AST_TEST_REGISTER(open_with_valid_options);
+ AST_TEST_REGISTER(open_with_invalid_options);
+ AST_TEST_REGISTER(create_and_retrieve);
+ AST_TEST_REGISTER(update);
+ AST_TEST_REGISTER(delete);
+ AST_TEST_REGISTER(maximum_objects);
+ AST_TEST_REGISTER(expiration);
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Memory Cache Object Wizard",
+ .support_level = AST_MODULE_SUPPORT_CORE,
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_REALTIME_DRIVER,
+);
diff --git a/res/res_sorcery_realtime.c b/res/res_sorcery_realtime.c
index fcdc2a971..fc22170a5 100644
--- a/res/res_sorcery_realtime.c
+++ b/res/res_sorcery_realtime.c
@@ -142,6 +142,8 @@ static struct ast_variable *sorcery_realtime_filter_objectset(struct ast_variabl
}
}
+ ao2_ref(object_type, -1);
+
return objectset;
}
diff --git a/res/res_timing_kqueue.c b/res/res_timing_kqueue.c
index 17f98360e..d46f7f3d6 100644
--- a/res/res_timing_kqueue.c
+++ b/res/res_timing_kqueue.c
@@ -159,7 +159,9 @@ static void timer_destroy(void *obj)
struct kqueue_timer *timer = obj;
ast_debug(5, "[%d]: Timer Destroy\n", timer->handle);
kqueue_timer_fini_continuous_event(timer);
- close(timer->handle);
+ if (timer->handle > -1) {
+ close(timer->handle);
+ }
}
static void *kqueue_timer_open(void)
diff --git a/res/res_timing_timerfd.c b/res/res_timing_timerfd.c
index 6d5400bc3..43deb6ce7 100644
--- a/res/res_timing_timerfd.c
+++ b/res/res_timing_timerfd.c
@@ -76,8 +76,9 @@ struct timerfd_timer {
static void timer_destroy(void *obj)
{
struct timerfd_timer *timer = obj;
-
- close(timer->fd);
+ if (timer->fd > -1) {
+ close(timer->fd);
+ }
}
static void *timerfd_timer_open(void)
diff --git a/tests/test_sorcery_memory_cache_thrash.c b/tests/test_sorcery_memory_cache_thrash.c
new file mode 100644
index 000000000..8bf63a62c
--- /dev/null
+++ b/tests/test_sorcery_memory_cache_thrash.c
@@ -0,0 +1,618 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2015, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@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
+ * \brief Sorcery Unit Tests
+ *
+ * \author Joshua Colp <jcolp@digium.com>
+ *
+ */
+
+/*** MODULEINFO
+ <depend>TEST_FRAMEWORK</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/test.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/logger.h"
+#include "asterisk/vector.h"
+#include "asterisk/cli.h"
+
+/*! \brief The default amount of time (in seconds) that thrash unit tests execute for */
+#define TEST_THRASH_TIME 3
+
+/*! \brief The number of threads to use for retrieving for applicable tests */
+#define TEST_THRASH_RETRIEVERS 25
+
+/*! \brief The number of threads to use for updating for applicable tests*/
+#define TEST_THRASH_UPDATERS 25
+
+/*! \brief Structure for a memory cache thras thread */
+struct sorcery_memory_cache_thrash_thread {
+ /*! \brief The thread thrashing the cache */
+ pthread_t thread;
+ /*! \brief Sorcery instance being tested */
+ struct ast_sorcery *sorcery;
+ /*! \brief The number of unique objects we should restrict ourself to */
+ unsigned int unique_objects;
+ /*! \brief Set when the thread should stop */
+ unsigned int stop;
+ /*! \brief Average time spent executing sorcery operation in this thread */
+ unsigned int average_execution_time;
+};
+
+/*! \brief Structure for memory cache thrasing */
+struct sorcery_memory_cache_thrash {
+ /*! \brief The sorcery instance being tested */
+ struct ast_sorcery *sorcery;
+ /*! \brief The number of threads which are updating */
+ unsigned int update_threads;
+ /*! \brief The average execution time of sorcery update operations */
+ unsigned int average_update_execution_time;
+ /*! \brief The number of threads which are retrieving */
+ unsigned int retrieve_threads;
+ /*! \brief The average execution time of sorcery retrieve operations */
+ unsigned int average_retrieve_execution_time;
+ /*! \brief Threads which are updating or reading from the cache */
+ AST_VECTOR(, struct sorcery_memory_cache_thrash_thread *) threads;
+};
+
+/*!
+ * \brief Sorcery object created based on backend data
+ */
+struct test_data {
+ SORCERY_OBJECT(details);
+};
+
+/*!
+ * \brief Allocation callback for test_data sorcery object
+ */
+static void *test_data_alloc(const char *id)
+{
+ return ast_sorcery_generic_alloc(sizeof(struct test_data), NULL);
+}
+
+/*!
+ * \brief Callback for retrieving sorcery object by ID
+ *
+ * \param sorcery The sorcery instance
+ * \param data Unused
+ * \param type The object type. Will always be "test".
+ * \param id The object id. Will always be "test".
+ *
+ * \retval NULL Backend data successfully allocated
+ * \retval non-NULL Backend data could not be successfully allocated
+ */
+static void *mock_retrieve_id(const struct ast_sorcery *sorcery, void *data,
+ const char *type, const char *id)
+{
+ return ast_sorcery_alloc(sorcery, type, id);
+}
+
+/*!
+ * \brief Callback for updating a sorcery object
+ *
+ * \param sorcery The sorcery instance
+ * \param data Unused
+ * \param object The object to update.
+ *
+ */
+static int mock_update(const struct ast_sorcery *sorcery, void *data,
+ void *object)
+{
+ return 0;
+}
+
+/*!
+ * \brief A mock sorcery wizard used for the stale test
+ */
+static struct ast_sorcery_wizard mock_wizard = {
+ .name = "mock",
+ .retrieve_id = mock_retrieve_id,
+ .update = mock_update,
+};
+
+/*!
+ * \internal
+ * \brief Destructor for sorcery memory cache thrasher
+ *
+ * \param obj The sorcery memory cache thrash structure
+ */
+static void sorcery_memory_cache_thrash_destroy(void *obj)
+{
+ struct sorcery_memory_cache_thrash *thrash = obj;
+ int idx;
+
+ if (thrash->sorcery) {
+ ast_sorcery_unref(thrash->sorcery);
+ }
+
+ for (idx = 0; idx < AST_VECTOR_SIZE(&thrash->threads); ++idx) {
+ struct sorcery_memory_cache_thrash_thread *thread;
+
+ thread = AST_VECTOR_GET(&thrash->threads, idx);
+ ast_free(thread);
+ }
+ AST_VECTOR_FREE(&thrash->threads);
+
+ ast_sorcery_wizard_unregister(&mock_wizard);
+}
+
+/*!
+ * \internal
+ * \brief Set up thrasing against a memory cache on a sorcery instance
+ *
+ * \param cache_configuration The sorcery memory cache configuration to use
+ * \param update_threads The number of threads which should be constantly updating sorcery
+ * \param retrieve_threads The number of threads which should be constantly retrieving from sorcery
+ * \param unique_objects The number of unique objects that can exist
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+static struct sorcery_memory_cache_thrash *sorcery_memory_cache_thrash_create(const char *cache_configuration,
+ unsigned int update_threads, unsigned int retrieve_threads, unsigned int unique_objects)
+{
+ struct sorcery_memory_cache_thrash *thrash;
+ struct sorcery_memory_cache_thrash_thread *thread;
+ unsigned int total_threads = update_threads + retrieve_threads;
+
+ thrash = ao2_alloc_options(sizeof(*thrash), sorcery_memory_cache_thrash_destroy,
+ AO2_ALLOC_OPT_LOCK_NOLOCK);
+ if (!thrash) {
+ return NULL;
+ }
+
+ thrash->update_threads = update_threads;
+ thrash->retrieve_threads = retrieve_threads;
+
+ ast_sorcery_wizard_register(&mock_wizard);
+
+ thrash->sorcery = ast_sorcery_open();
+ if (!thrash->sorcery) {
+ ao2_ref(thrash, -1);
+ return NULL;
+ }
+
+ ast_sorcery_apply_wizard_mapping(thrash->sorcery, "test", "memory_cache",
+ !strcmp(cache_configuration, "default") ? "" : cache_configuration, 1);
+ ast_sorcery_apply_wizard_mapping(thrash->sorcery, "test", "mock", NULL, 0);
+ ast_sorcery_internal_object_register(thrash->sorcery, "test", test_data_alloc, NULL, NULL);
+
+ if (AST_VECTOR_INIT(&thrash->threads, update_threads + retrieve_threads)) {
+ ao2_ref(thrash, -1);
+ return NULL;
+ }
+
+ while (AST_VECTOR_SIZE(&thrash->threads) != total_threads) {
+ thread = ast_calloc(1, sizeof(*thread));
+
+ if (!thread) {
+ ao2_ref(thrash, -1);
+ return NULL;
+ }
+
+ thread->thread = AST_PTHREADT_NULL;
+ thread->unique_objects = unique_objects;
+
+ /* This purposely holds no ref as the main thrash structure does */
+ thread->sorcery = thrash->sorcery;
+
+ AST_VECTOR_APPEND(&thrash->threads, thread);
+ }
+
+ return thrash;
+}
+
+/*!
+ * \internal
+ * \brief Thrashing cache update thread
+ *
+ * \param data The sorcery memory cache thrash thread
+ */
+static void *sorcery_memory_cache_thrash_update(void *data)
+{
+ struct sorcery_memory_cache_thrash_thread *thread = data;
+ struct timeval start;
+ unsigned int object_id;
+ char object_id_str[AST_UUID_STR_LEN];
+ void *object;
+
+ while (!thread->stop) {
+ object_id = ast_random() % thread->unique_objects;
+ snprintf(object_id_str, sizeof(object_id_str), "%u", object_id);
+
+ object = ast_sorcery_alloc(thread->sorcery, "test", object_id_str);
+ ast_assert(object != NULL);
+
+ start = ast_tvnow();
+ ast_sorcery_update(thread->sorcery, object);
+ thread->average_execution_time = (thread->average_execution_time + ast_tvdiff_ms(ast_tvnow(), start)) / 2;
+ ao2_ref(object, -1);
+ }
+
+ return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Thrashing cache retrieve thread
+ *
+ * \param data The sorcery memory cache thrash thread
+ */
+static void *sorcery_memory_cache_thrash_retrieve(void *data)
+{
+ struct sorcery_memory_cache_thrash_thread *thread = data;
+ struct timeval start;
+ unsigned int object_id;
+ char object_id_str[AST_UUID_STR_LEN];
+ void *object;
+
+ while (!thread->stop) {
+ object_id = ast_random() % thread->unique_objects;
+ snprintf(object_id_str, sizeof(object_id_str), "%u", object_id);
+
+ start = ast_tvnow();
+ object = ast_sorcery_retrieve_by_id(thread->sorcery, "test", object_id_str);
+ thread->average_execution_time = (thread->average_execution_time + ast_tvdiff_ms(ast_tvnow(), start)) / 2;
+ ast_assert(object != NULL);
+
+ ao2_ref(object, -1);
+ }
+
+ return NULL;
+}
+
+/*!
+ * \internal
+ * \brief Stop thrashing against a sorcery memory cache
+ *
+ * \param thrash The sorcery memory cache thrash structure
+ */
+static void sorcery_memory_cache_thrash_stop(struct sorcery_memory_cache_thrash *thrash)
+{
+ int idx;
+
+ for (idx = 0; idx < AST_VECTOR_SIZE(&thrash->threads); ++idx) {
+ struct sorcery_memory_cache_thrash_thread *thread;
+
+ thread = AST_VECTOR_GET(&thrash->threads, idx);
+ if (thread->thread == AST_PTHREADT_NULL) {
+ continue;
+ }
+
+ thread->stop = 1;
+
+ pthread_join(thread->thread, NULL);
+
+ if (idx < thrash->update_threads) {
+ thrash->average_update_execution_time += thread->average_execution_time;
+ } else {
+ thrash->average_retrieve_execution_time += thread->average_execution_time;
+ }
+ }
+
+ if (thrash->update_threads) {
+ thrash->average_update_execution_time /= thrash->update_threads;
+ }
+ if (thrash->retrieve_threads) {
+ thrash->average_retrieve_execution_time /= thrash->retrieve_threads;
+ }
+}
+
+/*!
+ * \internal
+ * \brief Start thrashing against a sorcery memory cache
+ *
+ * \param thrash The sorcery memory cache thrash structure
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+static int sorcery_memory_cache_thrash_start(struct sorcery_memory_cache_thrash *thrash)
+{
+ int idx;
+
+ for (idx = 0; idx < AST_VECTOR_SIZE(&thrash->threads); ++idx) {
+ struct sorcery_memory_cache_thrash_thread *thread;
+
+ thread = AST_VECTOR_GET(&thrash->threads, idx);
+
+ if (ast_pthread_create(&thread->thread, NULL, idx < thrash->update_threads ?
+ sorcery_memory_cache_thrash_update : sorcery_memory_cache_thrash_retrieve, thread)) {
+ sorcery_memory_cache_thrash_stop(thrash);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ * \internal
+ * \brief CLI command implementation for 'sorcery memory cache thrash'
+ */
+static char *sorcery_memory_cache_cli_thrash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ struct sorcery_memory_cache_thrash *thrash;
+ unsigned int thrash_time, unique_objects, retrieve_threads, update_threads;
+
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "sorcery memory cache thrash";
+ e->usage =
+ "Usage: sorcery memory cache thrash <cache configuration> <amount of time to thrash the cache> <number of unique objects> <number of retrieve threads> <number of update threads>\n"
+ " Create a sorcery instance with a memory cache using the provided configuration and thrash it.\n";
+ return NULL;
+ case CLI_GENERATE:
+ return NULL;
+ }
+
+ if (a->argc != 9) {
+ return CLI_SHOWUSAGE;
+ }
+
+ if (sscanf(a->argv[5], "%30u", &thrash_time) != 1) {
+ ast_cli(a->fd, "An invalid value of '%s' has been provided for the thrashing time\n", a->argv[5]);
+ return CLI_FAILURE;
+ } else if (sscanf(a->argv[6], "%30u", &unique_objects) != 1) {
+ ast_cli(a->fd, "An invalid value of '%s' has been provided for number of unique objects\n", a->argv[6]);
+ return CLI_FAILURE;
+ } else if (sscanf(a->argv[7], "%30u", &retrieve_threads) != 1) {
+ ast_cli(a->fd, "An invalid value of '%s' has been provided for the number of retrieve threads\n", a->argv[7]);
+ return CLI_FAILURE;
+ } else if (sscanf(a->argv[8], "%30u", &update_threads) != 1) {
+ ast_cli(a->fd, "An invalid value of '%s' has been provided for the number of update threads\n", a->argv[8]);
+ return CLI_FAILURE;
+ }
+
+ thrash = sorcery_memory_cache_thrash_create(a->argv[4], update_threads, retrieve_threads, unique_objects);
+ if (!thrash) {
+ ast_cli(a->fd, "Could not create a sorcery memory cache thrash test using the provided arguments\n");
+ return CLI_FAILURE;
+ }
+
+ ast_cli(a->fd, "Starting cache thrash test.\n");
+ ast_cli(a->fd, "Memory cache configuration: %s\n", a->argv[4]);
+ ast_cli(a->fd, "Amount of time to perform test: %u seconds\n", thrash_time);
+ ast_cli(a->fd, "Number of unique objects: %u\n", unique_objects);
+ ast_cli(a->fd, "Number of retrieve threads: %u\n", retrieve_threads);
+ ast_cli(a->fd, "Number of update threads: %u\n", update_threads);
+
+ sorcery_memory_cache_thrash_start(thrash);
+ while ((thrash_time = sleep(thrash_time)));
+ sorcery_memory_cache_thrash_stop(thrash);
+
+ ast_cli(a->fd, "Stopped cache thrash test\n");
+
+ ast_cli(a->fd, "Average retrieve execution time (in milliseconds): %u\n", thrash->average_retrieve_execution_time);
+ ast_cli(a->fd, "Average update execution time (in milliseconds): %u\n", thrash->average_update_execution_time);
+
+ ao2_ref(thrash, -1);
+
+ return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_memory_cache_thrash[] = {
+ AST_CLI_DEFINE(sorcery_memory_cache_cli_thrash, "Thrash a sorcery memory cache"),
+};
+
+/*!
+ * \internal
+ * \brief Perform a thrash test against a cache
+ *
+ * \param test The unit test being run
+ * \param cache_configuration The underlying cache configuration
+ * \param thrash_time How long (in seconds) to thrash the cache for
+ * \param unique_objects The number of unique objects
+ * \param retrieve_threads The number of threads constantly doing a retrieve
+ * \param update_threads The number of threads constantly doing an update
+ *
+ * \retval AST_TEST_PASS success
+ * \retval AST_TEST_FAIL failure
+ */
+static enum ast_test_result_state nominal_thrash(struct ast_test *test, const char *cache_configuration,
+ unsigned int thrash_time, unsigned int unique_objects, unsigned int retrieve_threads,
+ unsigned int update_threads)
+{
+ struct sorcery_memory_cache_thrash *thrash;
+
+ thrash = sorcery_memory_cache_thrash_create(cache_configuration, update_threads, retrieve_threads, unique_objects);
+ if (!thrash) {
+ return AST_TEST_FAIL;
+ }
+
+ sorcery_memory_cache_thrash_start(thrash);
+ while ((thrash_time = sleep(thrash_time)));
+ sorcery_memory_cache_thrash_stop(thrash);
+
+ ao2_ref(thrash, -1);
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(low_unique_object_count_immediately_stale)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "low_unique_object_count_immediately_stale";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with low number of unique objects that are immediately stale";
+ info->description = "This test creates a cache with objects that are stale\n"
+ "after 1 second. It also creates 25 threads which are constantly attempting\n"
+ "to retrieve the objects. This test confirms that the background refreshes\n"
+ "being done as a result of going stale do not conflict or cause problems with\n"
+ "the large number of retrieve threads.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "object_lifetime_stale=1", TEST_THRASH_TIME, 10, TEST_THRASH_RETRIEVERS, 0);
+}
+
+AST_TEST_DEFINE(low_unique_object_count_immediately_expire)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "low_unique_object_count_immediately_expire";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with low number of unique objects that are immediately expired";
+ info->description = "This test creates a cache with objects that are expired\n"
+ "after 1 second. It also creates 25 threads which are constantly attempting\n"
+ "to retrieve the objects. This test confirms that the expiration process does\n"
+ "not cause a problem as the retrieve threads execute.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "object_lifetime_maximum=1", TEST_THRASH_TIME, 10, TEST_THRASH_RETRIEVERS, 0);
+}
+
+AST_TEST_DEFINE(low_unique_object_count_high_concurrent_updates)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "low_unique_object_count_high_concurrent_updates";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with low number of unique objects that are updated frequently";
+ info->description = "This test creates a cache with objects that are being constantly\n"
+ "updated and retrieved at the same time. This will create contention between all\n"
+ "of the threads as the write lock is held for the updates. This test confirms that\n"
+ "no problems occur in this situation.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "default", TEST_THRASH_TIME, 10, TEST_THRASH_RETRIEVERS, TEST_THRASH_UPDATERS);
+}
+
+AST_TEST_DEFINE(unique_objects_exceeding_maximum)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "unique_objects_exceeding_maximum";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with a fixed maximum object count";
+ info->description = "This test creates a cache with a maximum number of objects\n"
+ "allowed in it. The maximum number of unique objects, however, far exceeds the\n"
+ "the maximum number allowed in the cache. This test confirms that the cache does\n"
+ "not exceed the maximum and that the removal of older objects does not cause\n"
+ "a problem.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "maximum_objects=10", TEST_THRASH_TIME, 100, TEST_THRASH_RETRIEVERS, 0);
+}
+
+AST_TEST_DEFINE(unique_objects_exceeding_maximum_with_expire_and_stale)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "unique_objects_exceeding_maximum_with_expire_and_stale";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with a fixed maximum object count with objects that expire and go stale";
+ info->description = "This test creates a cache with a maximum number of objects\n"
+ "allowed in it with objects that also go stale after a period of time and expire.\n"
+ "A number of threads are created that constantly retrieve from the cache, causing\n"
+ "both stale refresh and expiration to occur. This test confirms that the combination\n"
+ "of these do not present a problem.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "maximum_objects=10,object_lifetime_maximum=2,object_lifetime_stale=1",
+ TEST_THRASH_TIME * 2, 100, TEST_THRASH_RETRIEVERS, 0);
+}
+
+AST_TEST_DEFINE(conflicting_expire_and_stale)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "conflicting_expire_and_stale";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with a large number of objects that expire and go stale";
+ info->description = "This test creates a cache with a large number of objects that expire\n"
+ "and go stale. As there is such a large number this ensures that both operations occur.\n"
+ "This test confirms that stale refreshing and expiration do not conflict.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "object_lifetime_maximum=2,object_lifetime_stale=1", TEST_THRASH_TIME * 2, 5000,
+ TEST_THRASH_RETRIEVERS, 0);
+}
+
+AST_TEST_DEFINE(high_object_count_without_expiration)
+{
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "high_object_count_without_expiration";
+ info->category = "/res/res_sorcery_memory_cache/thrash/";
+ info->summary = "Thrash a cache with a large number of objects";
+ info->description = "This test creates a cache with a large number of objects that persist.\n"
+ "A large number of threads are created which constantly retrieve from the cache.\n"
+ "This test confirms that the large number of retrieves do not cause a problem.\n";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ return nominal_thrash(test, "default", TEST_THRASH_TIME, 5000, TEST_THRASH_RETRIEVERS, 0);
+}
+
+static int unload_module(void)
+{
+ ast_cli_unregister_multiple(cli_memory_cache_thrash, ARRAY_LEN(cli_memory_cache_thrash));
+ AST_TEST_UNREGISTER(low_unique_object_count_immediately_stale);
+ AST_TEST_UNREGISTER(low_unique_object_count_immediately_expire);
+ AST_TEST_UNREGISTER(low_unique_object_count_high_concurrent_updates);
+ AST_TEST_UNREGISTER(unique_objects_exceeding_maximum);
+ AST_TEST_UNREGISTER(unique_objects_exceeding_maximum_with_expire_and_stale);
+ AST_TEST_UNREGISTER(conflicting_expire_and_stale);
+ AST_TEST_UNREGISTER(high_object_count_without_expiration);
+
+ return 0;
+}
+
+static int load_module(void)
+{
+ ast_cli_register_multiple(cli_memory_cache_thrash, ARRAY_LEN(cli_memory_cache_thrash));
+ AST_TEST_REGISTER(low_unique_object_count_immediately_stale);
+ AST_TEST_REGISTER(low_unique_object_count_immediately_expire);
+ AST_TEST_REGISTER(low_unique_object_count_high_concurrent_updates);
+ AST_TEST_REGISTER(unique_objects_exceeding_maximum);
+ AST_TEST_REGISTER(unique_objects_exceeding_maximum_with_expire_and_stale);
+ AST_TEST_REGISTER(conflicting_expire_and_stale);
+ AST_TEST_REGISTER(high_object_count_without_expiration);
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sorcery Cache Thrasing test module");