debloat and reorganize code
[project/procd.git] / service / validate.c
1 /*
2 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <libubox/blobmsg_json.h>
15 #include <libubox/avl-cmp.h>
16
17 #include "../procd.h"
18
19 #include "service.h"
20
21 enum {
22 SERVICE_VAL_PACKAGE,
23 SERVICE_VAL_TYPE,
24 SERVICE_VAL_DATA,
25 __SERVICE_VAL_MAX
26 };
27
28 static const struct blobmsg_policy service_validate_attrs[__SERVICE_VAL_MAX] = {
29 [SERVICE_VAL_PACKAGE] = { "package", BLOBMSG_TYPE_STRING },
30 [SERVICE_VAL_TYPE] = { "type", BLOBMSG_TYPE_STRING },
31 [SERVICE_VAL_DATA] = { "data", BLOBMSG_TYPE_TABLE },
32 };
33
34 static struct avl_tree validators;
35
36 void
37 service_validate_dump_all(struct blob_buf *b, char *p, char *s)
38 {
39 struct json_object *r = json_object_new_object();
40 struct validate *v;
41
42 if (!r)
43 return;
44
45 avl_for_each_element(&validators, v, avl) {
46 struct json_object *o, *t;
47 struct vrule *vr;
48
49 if (p && strcmp(p, v->package))
50 continue;
51
52 if (s && strcmp(s, v->type))
53 continue;
54
55 o = json_object_object_get(r, v->package);
56 if (!o) {
57 o = json_object_new_object();
58 json_object_object_add(r, v->package, o);
59 }
60 t = json_object_object_get(o, v->type);
61 if (!t) {
62 t = json_object_new_object();
63 json_object_object_add(o, v->type, t);
64 }
65 avl_for_each_element(&v->rules, vr, avl)
66 json_object_object_add(t, vr->option, json_object_new_string(vr->rule));
67 }
68 blobmsg_add_object(b, r);
69 }
70
71 void
72 service_validate_dump(struct blob_buf *b, struct service *s)
73 {
74 struct validate *v;
75 void *i = blobmsg_open_array(b, "validate");
76
77 list_for_each_entry(v, &s->validators, list) {
78 struct vrule *vr;
79 void *k, *j = blobmsg_open_table(b, "validate");
80
81 blobmsg_add_string(b, "package", v->package);
82 blobmsg_add_string(b, "type", v->type);
83 k = blobmsg_open_table(b, "rules");
84 avl_for_each_element(&v->rules, vr, avl)
85 blobmsg_add_string(b, vr->option, vr->rule);
86 blobmsg_close_table(b, k);
87 blobmsg_close_table(b, j);
88 }
89 blobmsg_close_array(b, i);
90 }
91
92 void
93 service_validate_del(struct service *s)
94 {
95 struct validate *v, *n;
96
97 if (list_empty(&s->validators))
98 return;
99
100 list_for_each_entry_safe(v, n, &s->validators, list) {
101 struct vrule *vr, *a;
102
103 avl_for_each_element_safe(&v->rules, vr, avl, a) {
104 avl_delete(&v->rules, &vr->avl);
105 free(vr);
106 }
107 avl_delete(&validators, &v->avl);
108 list_del(&v->list);
109 free(v);
110 }
111 }
112
113 void
114 service_validate_add(struct service *s, struct blob_attr *msg)
115 {
116 struct blob_attr *tb[__SERVICE_VAL_MAX];
117 struct validate *v;
118 char *type, *package;
119 struct blob_attr *cur;
120 int rem;
121
122 blobmsg_parse(service_validate_attrs, __SERVICE_VAL_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
123 if (!tb[SERVICE_VAL_PACKAGE] || !tb[SERVICE_VAL_TYPE] || !tb[SERVICE_VAL_DATA])
124 return;
125
126 v = calloc_a(sizeof(*v), &package, blobmsg_data_len(tb[SERVICE_VAL_PACKAGE]) + 1,
127 &type, blobmsg_data_len(tb[SERVICE_VAL_TYPE]) + 1);
128 if (!v)
129 return;
130
131 v->type = type;
132 v->avl.key = v->package = package;
133 strcpy(v->package, blobmsg_get_string(tb[SERVICE_VAL_PACKAGE]));
134 strcpy(v->type, blobmsg_get_string(tb[SERVICE_VAL_TYPE]));
135
136 list_add(&v->list, &s->validators);
137 if (avl_insert(&validators, &v->avl)) {
138 free(v);
139 return;
140 }
141 avl_init(&v->rules, avl_strcmp, false, NULL);
142
143 blobmsg_for_each_attr(cur, tb[SERVICE_VAL_DATA], rem) {
144 char *option;
145 char *rule;
146 struct vrule *vr = calloc_a(sizeof(*vr), &option, strlen(blobmsg_name(cur)) + 1,
147 &rule, strlen(blobmsg_get_string(cur)) + 1);
148
149 vr->avl.key = vr->option = option;
150 vr->rule = rule;
151 strcpy(vr->option, blobmsg_name(cur));
152 strcpy(vr->rule, blobmsg_get_string(cur));
153 if (avl_insert(&v->rules, &vr->avl))
154 free(vr);
155 }
156 }
157
158 void
159 service_validate_init(void)
160 {
161 avl_init(&validators, avl_strcmp, true, NULL);
162 }