2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <libubox/blobmsg_json.h>
16 #include <libubox/avl-cmp.h>
21 struct avl_tree services
;
22 static struct blob_buf b
;
25 service_instance_add(struct service
*s
, struct blob_attr
*attr
)
27 struct service_instance
*in
;
29 if (blobmsg_type(attr
) != BLOBMSG_TYPE_TABLE
)
32 in
= calloc(1, sizeof(*in
));
36 instance_init(in
, s
, attr
);
37 vlist_add(&s
->instances
, &in
->node
, (void *) in
->name
);
41 service_instance_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
42 struct vlist_node
*node_old
)
44 struct service_instance
*in_o
= NULL
, *in_n
= NULL
;
47 in_o
= container_of(node_old
, struct service_instance
, node
);
50 in_n
= container_of(node_new
, struct service_instance
, node
);
53 DEBUG(1, "Update instance %s::%s\n", in_o
->srv
->name
, in_o
->name
);
54 instance_update(in_o
, in_n
);
57 DEBUG(1, "Free instance %s::%s\n", in_o
->srv
->name
, in_o
->name
);
61 DEBUG(1, "Create instance %s::%s\n", in_n
->srv
->name
, in_n
->name
);
66 static struct service
*
67 service_alloc(const char *name
)
72 s
= calloc_a(sizeof(*s
), &new_name
, strlen(name
) + 1);
73 strcpy(new_name
, name
);
75 vlist_init(&s
->instances
, avl_strcmp
, service_instance_update
);
76 s
->instances
.keep_old
= true;
86 SERVICE_SET_INSTANCES
,
91 static const struct blobmsg_policy service_set_attrs
[__SERVICE_SET_MAX
] = {
92 [SERVICE_SET_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
93 [SERVICE_SET_SCRIPT
] = { "script", BLOBMSG_TYPE_STRING
},
94 [SERVICE_SET_INSTANCES
] = { "instances", BLOBMSG_TYPE_TABLE
},
95 [SERVICE_SET_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
99 service_update(struct service
*s
, struct blob_attr
*config
, struct blob_attr
**tb
, bool add
)
101 struct blob_attr
*cur
;
110 if (tb
[SERVICE_SET_TRIGGER
] && blobmsg_data_len(tb
[SERVICE_SET_TRIGGER
])) {
111 s
->trigger
= malloc(blob_len(tb
[SERVICE_SET_TRIGGER
]));
114 memcpy(s
->trigger
, tb
[SERVICE_SET_TRIGGER
], blob_len(tb
[SERVICE_SET_TRIGGER
]));
115 trigger_add(s
->trigger
, s
);
118 if (tb
[SERVICE_SET_INSTANCES
]) {
120 vlist_update(&s
->instances
);
121 blobmsg_for_each_attr(cur
, tb
[SERVICE_SET_INSTANCES
], rem
) {
122 service_instance_add(s
, cur
);
125 vlist_flush(&s
->instances
);
132 service_delete(struct service
*s
)
134 vlist_flush_all(&s
->instances
);
135 avl_delete(&services
, &s
->avl
);
148 static const struct blobmsg_policy service_attrs
[__SERVICE_ATTR_MAX
] = {
149 [SERVICE_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
153 SERVICE_DEL_ATTR_NAME
,
154 SERVICE_DEL_ATTR_INSTANCE
,
155 __SERVICE_DEL_ATTR_MAX
,
158 static const struct blobmsg_policy service_del_attrs
[__SERVICE_DEL_ATTR_MAX
] = {
159 [SERVICE_DEL_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
160 [SERVICE_DEL_ATTR_INSTANCE
] = { "instance", BLOBMSG_TYPE_STRING
},
164 SERVICE_LIST_ATTR_VERBOSE
,
165 __SERVICE_LIST_ATTR_MAX
,
168 static const struct blobmsg_policy service_list_attrs
[__SERVICE_LIST_ATTR_MAX
] = {
169 [SERVICE_LIST_ATTR_VERBOSE
] = { "verbose", BLOBMSG_TYPE_BOOL
},
178 static const struct blobmsg_policy event_policy
[__EVENT_MAX
] = {
179 [EVENT_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
180 [EVENT_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
184 service_handle_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
185 struct ubus_request_data
*req
, const char *method
,
186 struct blob_attr
*msg
)
188 struct blob_attr
*tb
[__SERVICE_SET_MAX
], *cur
;
189 struct service
*s
= NULL
;
191 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
192 bool add
= !strcmp(method
, "add");
194 blobmsg_parse(service_set_attrs
, __SERVICE_SET_MAX
, tb
, blob_data(msg
), blob_len(msg
));
195 cur
= tb
[SERVICE_ATTR_NAME
];
199 name
= blobmsg_data(cur
);
201 s
= avl_find_element(&services
, name
, s
, avl
);
203 DEBUG(1, "Update service %s\n", name
);
204 return service_update(s
, msg
, tb
, add
);
207 DEBUG(1, "Create service %s\n", name
);
208 s
= service_alloc(name
);
210 return UBUS_STATUS_UNKNOWN_ERROR
;
212 ret
= service_update(s
, msg
, tb
, add
);
216 avl_insert(&services
, &s
->avl
);
226 service_dump(struct service
*s
, int verbose
)
228 struct service_instance
*in
;
231 c
= blobmsg_open_table(&b
, s
->name
);
233 if (avl_is_empty(&s
->instances
.avl
)) {
234 blobmsg_close_table(&b
, c
);
238 i
= blobmsg_open_table(&b
, "instances");
239 vlist_for_each_element(&s
->instances
, in
, node
)
240 instance_dump(&b
, in
, verbose
);
241 blobmsg_close_table(&b
, i
);
242 if (verbose
&& s
->trigger
)
243 blobmsg_add_blob(&b
, s
->trigger
);
244 blobmsg_close_table(&b
, c
);
248 service_handle_list(struct ubus_context
*ctx
, struct ubus_object
*obj
,
249 struct ubus_request_data
*req
, const char *method
,
250 struct blob_attr
*msg
)
252 struct blob_attr
*tb
[__SERVICE_LIST_ATTR_MAX
];
256 blobmsg_parse(service_list_attrs
, __SERVICE_LIST_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
258 if (tb
[SERVICE_LIST_ATTR_VERBOSE
] && blobmsg_get_bool(tb
[SERVICE_LIST_ATTR_VERBOSE
]))
261 blob_buf_init(&b
, 0);
262 avl_for_each_element(&services
, s
, avl
)
263 service_dump(s
, verbose
);
265 ubus_send_reply(ctx
, req
, b
.head
);
271 service_handle_delete(struct ubus_context
*ctx
, struct ubus_object
*obj
,
272 struct ubus_request_data
*req
, const char *method
,
273 struct blob_attr
*msg
)
275 struct blob_attr
*tb
[__SERVICE_DEL_ATTR_MAX
], *cur
;
277 struct service_instance
*in
;
279 blobmsg_parse(service_del_attrs
, __SERVICE_DEL_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
281 cur
= tb
[SERVICE_DEL_ATTR_NAME
];
283 return UBUS_STATUS_NOT_FOUND
;
285 s
= avl_find_element(&services
, blobmsg_data(cur
), s
, avl
);
287 return UBUS_STATUS_NOT_FOUND
;
289 cur
= tb
[SERVICE_DEL_ATTR_INSTANCE
];
295 in
= vlist_find(&s
->instances
, blobmsg_data(cur
), in
, node
);
297 ERROR("instance %s not found\n", (char *) blobmsg_data(cur
));
298 return UBUS_STATUS_NOT_FOUND
;
301 vlist_delete(&s
->instances
, &in
->node
);
307 service_handle_update(struct ubus_context
*ctx
, struct ubus_object
*obj
,
308 struct ubus_request_data
*req
, const char *method
,
309 struct blob_attr
*msg
)
311 struct blob_attr
*tb
[__SERVICE_ATTR_MAX
], *cur
;
314 blobmsg_parse(service_attrs
, __SERVICE_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
316 cur
= tb
[SERVICE_ATTR_NAME
];
318 return UBUS_STATUS_INVALID_ARGUMENT
;
320 s
= avl_find_element(&services
, blobmsg_data(cur
), s
, avl
);
322 return UBUS_STATUS_NOT_FOUND
;
324 if (!strcmp(method
, "update_start"))
325 vlist_update(&s
->instances
);
327 vlist_flush(&s
->instances
);
333 service_handle_event(struct ubus_context
*ctx
, struct ubus_object
*obj
,
334 struct ubus_request_data
*req
, const char *method
,
335 struct blob_attr
*msg
)
337 struct blob_attr
*tb
[__EVENT_MAX
];
340 return UBUS_STATUS_INVALID_ARGUMENT
;
342 blobmsg_parse(event_policy
, __EVENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
343 if (!tb
[EVENT_TYPE
] || !tb
[EVENT_DATA
])
344 return UBUS_STATUS_INVALID_ARGUMENT
;
346 trigger_event(blobmsg_get_string(tb
[EVENT_TYPE
]), tb
[EVENT_DATA
]);
351 static struct ubus_method main_object_methods
[] = {
352 UBUS_METHOD("set", service_handle_set
, service_set_attrs
),
353 UBUS_METHOD("add", service_handle_set
, service_set_attrs
),
354 UBUS_METHOD("list", service_handle_list
, service_attrs
),
355 UBUS_METHOD("delete", service_handle_delete
, service_del_attrs
),
356 UBUS_METHOD("update_start", service_handle_update
, service_attrs
),
357 UBUS_METHOD("update_complete", service_handle_update
, service_attrs
),
358 UBUS_METHOD("event", service_handle_event
, event_policy
),
361 static struct ubus_object_type main_object_type
=
362 UBUS_OBJECT_TYPE("service", main_object_methods
);
364 static struct ubus_object main_object
= {
366 .type
= &main_object_type
,
367 .methods
= main_object_methods
,
368 .n_methods
= ARRAY_SIZE(main_object_methods
),
371 void ubus_init_service(struct ubus_context
*ctx
)
373 avl_init(&services
, avl_strcmp
, false, NULL
);
374 ubus_add_object(ctx
, &main_object
);