service: get rid of service_init and service_validate_init, use static avl tree initi...
[project/procd.git] / service / validate.c
index ca9bb39..cf04490 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <libubox/blobmsg_json.h>
 #include <libubox/avl-cmp.h>
 
 #include <libubox/blobmsg_json.h>
 #include <libubox/avl-cmp.h>
+#include <json-c/json.h>
 
 #include "../procd.h"
 
 
 #include "../procd.h"
 
@@ -31,7 +32,7 @@ static const struct blobmsg_policy service_validate_attrs[__SERVICE_VAL_MAX] = {
        [SERVICE_VAL_DATA] = { "data", BLOBMSG_TYPE_TABLE },
 };
 
        [SERVICE_VAL_DATA] = { "data", BLOBMSG_TYPE_TABLE },
 };
 
-static struct avl_tree validators;
+static AVL_TREE(validators, avl_strcmp, true, NULL);
 
 void
 service_validate_dump_all(struct blob_buf *b, char *p, char *s)
 
 void
 service_validate_dump_all(struct blob_buf *b, char *p, char *s)
@@ -52,12 +53,12 @@ service_validate_dump_all(struct blob_buf *b, char *p, char *s)
                if (s && strcmp(s, v->type))
                        continue;
 
                if (s && strcmp(s, v->type))
                        continue;
 
-               o = json_object_object_get(r, v->package);
+               json_object_object_get_ex(r, v->package, &o);
                if (!o) {
                        o = json_object_new_object();
                        json_object_object_add(r, v->package, o);
                }
                if (!o) {
                        o = json_object_new_object();
                        json_object_object_add(r, v->package, o);
                }
-               t = json_object_object_get(o, v->type);
+               json_object_object_get_ex(o, v->type, &t);
                if (!t) {
                        t = json_object_new_object();
                        json_object_object_add(o, v->type, t);
                if (!t) {
                        t = json_object_new_object();
                        json_object_object_add(o, v->type, t);
@@ -66,6 +67,7 @@ service_validate_dump_all(struct blob_buf *b, char *p, char *s)
                        json_object_object_add(t, vr->option, json_object_new_string(vr->rule));
        }
        blobmsg_add_object(b, r);
                        json_object_object_add(t, vr->option, json_object_new_string(vr->rule));
        }
        blobmsg_add_object(b, r);
+       json_object_put(r);
 }
 
 void
 }
 
 void
@@ -100,10 +102,9 @@ service_validate_del(struct service *s)
         list_for_each_entry_safe(v, n, &s->validators, list) {
                struct vrule *vr, *a;
 
         list_for_each_entry_safe(v, n, &s->validators, list) {
                struct vrule *vr, *a;
 
-               avl_for_each_element_safe(&v->rules, vr, avl, a) {
-                       avl_delete(&v->rules, &vr->avl);
+               avl_remove_all_elements(&v->rules, vr, avl, a)
                        free(vr);
                        free(vr);
-               }
+
                avl_delete(&validators, &v->avl);
                list_del(&v->list);
                free(v);
                avl_delete(&validators, &v->avl);
                list_del(&v->list);
                free(v);
@@ -154,9 +155,3 @@ service_validate_add(struct service *s, struct blob_attr *msg)
                        free(vr);
        }
 }
                        free(vr);
        }
 }
-
-void
-service_validate_init(void)
-{
-       avl_init(&validators, avl_strcmp, true, NULL);
-}