Merge "asterisk.c: When astcanary dies on linux, reset priority on all threads."
[asterisk/asterisk.git] / main / sorcery.c
index 790e782..9f8c35c 100644 (file)
@@ -293,6 +293,12 @@ static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
        return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
 }
 
+static int yesno_handler_fn(const void *obj, const intptr_t *args, char **buf)
+{
+       unsigned int *field = (unsigned int *)(obj + args[0]);
+       return !(*buf = ast_strdup(*field ? "yes" : "no")) ? -1 : 0;
+}
+
 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
 {
        struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
@@ -307,7 +313,7 @@ static int chararray_handler_fn(const void *obj, const intptr_t *args, char **bu
 
 static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
 {
-       struct ast_str *codec_buf = ast_str_alloca(64);
+       struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
        struct ast_format_cap **cap = (struct ast_format_cap **)(obj + args[0]);
        return !(*buf = ast_strdup(ast_format_cap_get_names(*cap, &codec_buf)));
 }
@@ -316,6 +322,7 @@ static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type
 {
        switch(type) {
        case OPT_BOOL_T: return bool_handler_fn;
+       case OPT_YESNO_T: return yesno_handler_fn;
        case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
        case OPT_CODEC_T: return codec_handler_fn;
        case OPT_DOUBLE_T: return double_handler_fn;
@@ -501,13 +508,15 @@ int ast_sorcery_init(void)
        };
        ast_assert(wizards == NULL);
 
-       if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
-               threadpool = NULL;
+       threadpool = ast_threadpool_create("Sorcery", NULL, &options);
+       if (!threadpool) {
                return -1;
        }
 
-       if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
-               ast_threadpool_shutdown(threadpool);
+       wizards = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, WIZARD_BUCKETS,
+               sorcery_wizard_hash, NULL, sorcery_wizard_cmp);
+       if (!wizards) {
+               sorcery_cleanup();
                return -1;
        }
 
@@ -517,8 +526,8 @@ int ast_sorcery_init(void)
                return -1;
        }
 
-       instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
-               sorcery_instance_hash, sorcery_instance_cmp);
+       instances = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, INSTANCE_BUCKETS,
+               sorcery_instance_hash, NULL, sorcery_instance_cmp);
        if (!instances) {
                sorcery_cleanup();
                return -1;
@@ -599,7 +608,7 @@ static int sorcery_generic_observer_remove(void *obj, void *arg, int flags)
 {
        const struct sorcery_global_observer *observer = obj;
 
-       return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
+       return (observer->callbacks == arg) ? CMP_MATCH : 0;
 }
 
 int ast_sorcery_global_observer_add(const struct ast_sorcery_global_observer *callbacks)
@@ -823,7 +832,7 @@ static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *typ
 {
 #define INITIAL_WIZARD_VECTOR_SIZE 5
        struct ast_sorcery_object_type *object_type;
-       char uuid[AST_UUID_STR_LEN];
+       char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
 
        if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
                return NULL;
@@ -835,33 +844,38 @@ static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *typ
                return NULL;
        }
 
-       if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, OBJECT_FIELD_BUCKETS,
-                                       object_type_field_hash, object_type_field_cmp))) {
+       object_type->fields = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
+               OBJECT_FIELD_BUCKETS, object_type_field_hash, NULL, object_type_field_cmp);
+       if (!object_type->fields) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
+       object_type->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0,
+               NULL, NULL);
+       if (!object_type->observers) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
+       object_type->info = ast_calloc(1,
+               sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0]));
+       if (!object_type->info) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
+       object_type->file = ast_calloc(1,
+               sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0]));
+       if (!object_type->file) {
                ao2_ref(object_type, -1);
                return NULL;
        }
 
-       if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
-               ao2_ref(object_type, -1);
-               return NULL;
-       }
+       /* Create name with seq number appended. */
+       ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "sorcery/%s", type);
 
-       if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
+       if (!(object_type->serializer = ast_threadpool_serializer(tps_name, threadpool))) {
                ao2_ref(object_type, -1);
                return NULL;
        }
@@ -1166,6 +1180,20 @@ int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type,
        return 0;
 }
 
+int ast_sorcery_object_set_congestion_levels(struct ast_sorcery *sorcery, const char *type, long low_water, long high_water)
+{
+       struct ast_sorcery_object_type *object_type;
+       int res = -1;
+
+       object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
+       if (object_type) {
+               res = ast_taskprocessor_alert_set_levels(object_type->serializer,
+                       low_water, high_water);
+               ao2_ref(object_type, -1);
+       }
+       return res;
+}
+
 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
 {
        RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
@@ -1324,8 +1352,10 @@ static void sorcery_observer_invocation_destroy(void *obj)
 /*! \brief Allocator function for observer invocation */
 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
 {
-       struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
+       struct sorcery_observer_invocation *invocation;
 
+       invocation = ao2_alloc_options(sizeof(*invocation),
+               sorcery_observer_invocation_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
        if (!invocation) {
                return NULL;
        }
@@ -1600,10 +1630,13 @@ struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sor
                        char *buf = NULL;
                        struct ast_json *value = NULL;
 
-                       if ((res = object_field->handler(object, object_field->args, &buf))
+                       if (object_field->handler(object, object_field->args, &buf)
                                || !(value = ast_json_string_create(buf))
                                || ast_json_object_set(json, object_field->name, value)) {
-                               res = -1;
+                               ast_free(buf);
+                               ast_debug(5, "Skipping field '%s' for object type '%s'\n",
+                                       object_field->name, object_type->name);
+                               continue;
                        }
 
                        ast_free(buf);
@@ -1703,9 +1736,26 @@ static void sorcery_object_destructor(void *object)
        ast_free(details->object->id);
 }
 
+void *ast_sorcery_lockable_alloc(size_t size, ao2_destructor_fn destructor, void *lockobj)
+{
+       void *object = ao2_alloc_with_lockobj(size + sizeof(struct ast_sorcery_object),
+               sorcery_object_destructor, lockobj, "");
+       struct ast_sorcery_object_details *details = object;
+
+       if (!object) {
+               return NULL;
+       }
+
+       details->object = object + size;
+       details->object->destructor = destructor;
+
+       return object;
+}
+
 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
 {
-       void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
+       void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object),
+               sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
        struct ast_sorcery_object_details *details = object;
 
        if (!object) {
@@ -1736,6 +1786,10 @@ void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, con
        } else {
                details->object->id = ast_strdup(id);
        }
+       if (!details->object->id) {
+               ao2_ref(details, -1);
+               return NULL;
+       }
 
        details->object->created = ast_tvnow();
        ast_copy_string(details->object->type, type, sizeof(details->object->type));
@@ -1894,7 +1948,7 @@ void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const ch
                        }
                }
 
-               if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
+               if (((flags & AST_RETRIEVE_FLAG_MULTIPLE) && (!ao2_container_count(object) || !wizard->caching)) || !object) {
                        continue;
                }
 
@@ -1932,6 +1986,10 @@ struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *so
                }
 
                wizard->wizard->callbacks.retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
+
+               if (wizard->caching && ao2_container_count(objects)) {
+                       break;
+               }
        }
        AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
@@ -1939,16 +1997,18 @@ struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *so
 }
 
 /*! \brief Internal function which returns if the wizard has created the object */
-static int sorcery_wizard_create(void *obj, void *arg, int flags)
+static int sorcery_wizard_create(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
 {
-       const struct ast_sorcery_object_wizard *object_wizard = obj;
-       const struct sorcery_details *details = arg;
-
        if (!object_wizard->wizard->callbacks.create) {
                ast_debug(5, "Sorcery wizard '%s' does not support creation\n", object_wizard->wizard->callbacks.name);
                return 0;
        }
-       return (!object_wizard->caching && !object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
+
+       if (object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) {
+               return 0;
+       }
+
+       return CMP_MATCH;
 }
 
 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
@@ -1993,17 +2053,32 @@ int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
        AST_VECTOR_RW_RDLOCK(&object_type->wizards);
        for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
                found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
-               if (sorcery_wizard_create(found_wizard, &sdetails, 0) == (CMP_MATCH | CMP_STOP)) {
+               if (!found_wizard->caching
+                       && sorcery_wizard_create(found_wizard, &sdetails) == CMP_MATCH) {
                        object_wizard = found_wizard;
-                       if(ao2_container_count(object_type->observers)) {
-                               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+               }
+       }
 
-                               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
-                                       ao2_cleanup(invocation);
-                               }
+       if (object_wizard) {
+               for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+                       found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+                       if (found_wizard->caching) {
+                               sorcery_wizard_create(found_wizard, &sdetails);
+                       }
+               }
+
+               if (ao2_container_count(object_type->observers)) {
+                       struct sorcery_observer_invocation *invocation;
+
+                       invocation = sorcery_observer_invocation_alloc(object_type, object);
+                       if (invocation
+                               && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
                        }
                }
        }
+
        AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
        return object_wizard ? 0 : -1;
@@ -2033,18 +2108,18 @@ static int sorcery_observers_notify_update(void *data)
 }
 
 /*! \brief Internal function which returns if a wizard has updated the object */
-static int sorcery_wizard_update(void *obj, void *arg, int flags)
+static int sorcery_wizard_update(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
 {
-       const struct ast_sorcery_object_wizard *object_wizard = obj;
-       const struct sorcery_details *details = arg;
-
        if (!object_wizard->wizard->callbacks.update) {
                ast_debug(5, "Sorcery wizard '%s' does not support updating\n", object_wizard->wizard->callbacks.name);
                return 0;
        }
 
-       return (!object_wizard->wizard->callbacks.update(details->sorcery, object_wizard->data, details->obj) &&
-               !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
+       if (object_wizard->wizard->callbacks.update(details->sorcery, object_wizard->data, details->obj)) {
+               return 0;
+       }
+
+       return CMP_MATCH;
 }
 
 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
@@ -2066,17 +2141,32 @@ int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
        AST_VECTOR_RW_RDLOCK(&object_type->wizards);
        for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
                found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
-               if (sorcery_wizard_update(found_wizard, &sdetails, 0) == (CMP_MATCH | CMP_STOP)) {
+               if (!found_wizard->caching
+                       && sorcery_wizard_update(found_wizard, &sdetails) == CMP_MATCH) {
                        object_wizard = found_wizard;
-                       if (ao2_container_count(object_type->observers)) {
-                               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+               }
+       }
 
-                               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
-                                       ao2_cleanup(invocation);
-                               }
+       if (object_wizard) {
+               for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+                       found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+                       if (found_wizard->caching) {
+                               sorcery_wizard_update(found_wizard, &sdetails);
+                       }
+               }
+
+               if (ao2_container_count(object_type->observers)) {
+                       struct sorcery_observer_invocation *invocation;
+
+                       invocation = sorcery_observer_invocation_alloc(object_type, object);
+                       if (invocation
+                               && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
                        }
                }
        }
+
        AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
        return object_wizard ? 0 : -1;
@@ -2106,18 +2196,18 @@ static int sorcery_observers_notify_delete(void *data)
 }
 
 /*! \brief Internal function which returns if a wizard has deleted the object */
-static int sorcery_wizard_delete(void *obj, void *arg, int flags)
+static int sorcery_wizard_delete(const struct ast_sorcery_object_wizard *object_wizard, const struct sorcery_details *details)
 {
-       const struct ast_sorcery_object_wizard *object_wizard = obj;
-       const struct sorcery_details *details = arg;
-
        if (!object_wizard->wizard->callbacks.delete) {
                ast_debug(5, "Sorcery wizard '%s' does not support deletion\n", object_wizard->wizard->callbacks.name);
                return 0;
        }
 
-       return (!object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj) &&
-               !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
+       if (object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj)) {
+               return 0;
+       }
+
+       return CMP_MATCH;
 }
 
 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
@@ -2139,17 +2229,32 @@ int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
        AST_VECTOR_RW_RDLOCK(&object_type->wizards);
        for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
                found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
-               if (sorcery_wizard_delete(found_wizard, &sdetails, 0) == (CMP_MATCH | CMP_STOP)) {
+               if (!found_wizard->caching
+                       && sorcery_wizard_delete(found_wizard, &sdetails) == CMP_MATCH) {
                        object_wizard = found_wizard;
-                       if (ao2_container_count(object_type->observers)) {
-                               struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
+               }
+       }
 
-                               if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
-                                       ao2_cleanup(invocation);
-                               }
+       if (object_wizard) {
+               for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
+                       found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
+                       if (found_wizard->caching) {
+                               sorcery_wizard_delete(found_wizard, &sdetails);
+                       }
+               }
+
+               if (ao2_container_count(object_type->observers)) {
+                       struct sorcery_observer_invocation *invocation;
+
+                       invocation = sorcery_observer_invocation_alloc(object_type, object);
+                       if (invocation
+                               && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete,
+                                       invocation)) {
+                               ao2_cleanup(invocation);
                        }
                }
        }
+
        AST_VECTOR_RW_UNLOCK(&object_type->wizards);
 
        return object_wizard ? 0 : -1;
@@ -2285,7 +2390,7 @@ static int sorcery_observer_remove(void *obj, void *arg, int flags)
 {
        const struct ast_sorcery_object_type_observer *observer = obj;
 
-       return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
+       return (observer->callbacks == arg) ? CMP_MATCH : 0;
 }
 
 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
@@ -2307,18 +2412,20 @@ void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *
 
 int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
 {
+       const void *object_left = obj;
+       const void *object_right = arg;
        const char *right_key = arg;
        int cmp;
 
        switch (flags & OBJ_SEARCH_MASK) {
        case OBJ_SEARCH_OBJECT:
-               right_key = ast_sorcery_object_get_id(arg);
+               right_key = ast_sorcery_object_get_id(object_right);
                /* Fall through */
        case OBJ_SEARCH_KEY:
-               cmp = strcmp(ast_sorcery_object_get_id(obj), right_key);
+               cmp = strcmp(ast_sorcery_object_get_id(object_left), right_key);
                break;
        case OBJ_SEARCH_PARTIAL_KEY:
-               cmp = strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key));
+               cmp = strncmp(ast_sorcery_object_get_id(object_left), right_key, strlen(right_key));
                break;
        default:
                cmp = 0;
@@ -2329,37 +2436,32 @@ int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
 
 int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
 {
-       const char *right_key = arg;
-       int cmp = 0;
+       int cmp;
+
+       cmp = ast_sorcery_object_id_sort(obj, arg, flags);
+       if (cmp) {
+               return 0;
+       }
+       return CMP_MATCH;
+}
+
+int ast_sorcery_object_id_hash(const void *obj, int flags)
+{
+       const char *key;
 
        switch (flags & OBJ_SEARCH_MASK) {
-       case OBJ_SEARCH_OBJECT:
-               right_key = ast_sorcery_object_get_id(arg);
-               /* Fall through */
        case OBJ_SEARCH_KEY:
-               if (strcmp(ast_sorcery_object_get_id(obj), right_key) == 0) {
-                       cmp = CMP_MATCH | CMP_STOP;
-               }
+               key = obj;
                break;
-       case OBJ_SEARCH_PARTIAL_KEY:
-               if (strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key)) == 0) {
-                       cmp = CMP_MATCH;
-               }
+       case OBJ_SEARCH_OBJECT:
+               key = ast_sorcery_object_get_id(obj);
                break;
        default:
-               cmp = 0;
-               break;
-       }
-       return cmp;
-}
-
-int ast_sorcery_object_id_hash(const void *obj, int flags) {
-       if (flags & OBJ_SEARCH_OBJECT) {
-               return ast_str_hash(ast_sorcery_object_get_id(obj));
-       } else if (flags & OBJ_SEARCH_KEY) {
-               return ast_str_hash(obj);
+               /* Hash can only work on something with a full key. */
+               ast_assert(0);
+               return 0;
        }
-       return -1;
+       return ast_str_hash(key);
 }
 
 struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
@@ -2376,7 +2478,7 @@ static int is_registered_cb(void *obj, void *arg, int flags)
 
        if (object_field->name_regex
                && !regexec(object_field->name_regex, name, 0, NULL, 0)) {
-               rc = CMP_MATCH | CMP_STOP;
+               rc = CMP_MATCH;
        }
 
        return rc;