service.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  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
  8. *
  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.
  13. */
  14. #include <libubox/blobmsg_json.h>
  15. #include <libubox/avl-cmp.h>
  16. #include "procd.h"
  17. #include "service.h"
  18. #include "instance.h"
  19. #include "rcS.h"
  20. struct avl_tree services;
  21. static struct blob_buf b;
  22. static void
  23. service_instance_add(struct service *s, struct blob_attr *attr)
  24. {
  25. struct service_instance *in;
  26. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  27. return;
  28. in = calloc(1, sizeof(*in));
  29. if (!in)
  30. return;
  31. instance_init(in, s, attr);
  32. vlist_add(&s->instances, &in->node, (void *) in->name);
  33. }
  34. static void
  35. service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
  36. struct vlist_node *node_old)
  37. {
  38. struct service_instance *in_o = NULL, *in_n = NULL;
  39. if (node_old)
  40. in_o = container_of(node_old, struct service_instance, node);
  41. if (node_new)
  42. in_n = container_of(node_new, struct service_instance, node);
  43. if (in_o && in_n) {
  44. DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
  45. instance_update(in_o, in_n);
  46. instance_free(in_n);
  47. } else if (in_o) {
  48. DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
  49. instance_stop(in_o);
  50. instance_free(in_o);
  51. } else if (in_n) {
  52. DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
  53. instance_start(in_n);
  54. }
  55. }
  56. static struct service *
  57. service_alloc(const char *name)
  58. {
  59. struct service *s;
  60. char *new_name;
  61. s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
  62. strcpy(new_name, name);
  63. vlist_init(&s->instances, avl_strcmp, service_instance_update);
  64. s->instances.keep_old = true;
  65. s->name = new_name;
  66. s->avl.key = s->name;
  67. return s;
  68. }
  69. enum {
  70. SERVICE_SET_NAME,
  71. SERVICE_SET_SCRIPT,
  72. SERVICE_SET_INSTANCES,
  73. SERVICE_SET_TRIGGER,
  74. __SERVICE_SET_MAX
  75. };
  76. static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
  77. [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
  78. [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
  79. [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
  80. [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
  81. };
  82. static int
  83. service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
  84. {
  85. struct blob_attr *cur;
  86. int rem;
  87. if (s->trigger) {
  88. trigger_del(s);
  89. free(s->trigger);
  90. s->trigger = NULL;
  91. }
  92. if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
  93. s->trigger = malloc(blob_pad_len(tb[SERVICE_SET_TRIGGER]));
  94. if (!s->trigger)
  95. return -1;
  96. memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_pad_len(tb[SERVICE_SET_TRIGGER]));
  97. trigger_add(s->trigger, s);
  98. }
  99. if (tb[SERVICE_SET_INSTANCES]) {
  100. if (!add)
  101. vlist_update(&s->instances);
  102. blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
  103. service_instance_add(s, cur);
  104. }
  105. if (!add)
  106. vlist_flush(&s->instances);
  107. }
  108. rc(s->name, "running");
  109. return 0;
  110. }
  111. static void
  112. service_delete(struct service *s)
  113. {
  114. vlist_flush_all(&s->instances);
  115. avl_delete(&services, &s->avl);
  116. trigger_del(s);
  117. s->trigger = NULL;
  118. free(s->trigger);
  119. free(s);
  120. }
  121. enum {
  122. SERVICE_ATTR_NAME,
  123. __SERVICE_ATTR_MAX,
  124. };
  125. static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
  126. [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  127. };
  128. enum {
  129. SERVICE_DEL_ATTR_NAME,
  130. SERVICE_DEL_ATTR_INSTANCE,
  131. __SERVICE_DEL_ATTR_MAX,
  132. };
  133. static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
  134. [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  135. [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  136. };
  137. enum {
  138. SERVICE_LIST_ATTR_VERBOSE,
  139. __SERVICE_LIST_ATTR_MAX,
  140. };
  141. static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
  142. [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
  143. };
  144. enum {
  145. EVENT_TYPE,
  146. EVENT_DATA,
  147. __EVENT_MAX
  148. };
  149. static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
  150. [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  151. [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
  152. };
  153. static int
  154. service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
  155. struct ubus_request_data *req, const char *method,
  156. struct blob_attr *msg)
  157. {
  158. struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
  159. struct service *s = NULL;
  160. const char *name;
  161. int ret = UBUS_STATUS_INVALID_ARGUMENT;
  162. bool add = !strcmp(method, "add");
  163. blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
  164. cur = tb[SERVICE_ATTR_NAME];
  165. if (!cur)
  166. goto free;
  167. name = blobmsg_data(cur);
  168. s = avl_find_element(&services, name, s, avl);
  169. if (s) {
  170. DEBUG(1, "Update service %s\n", name);
  171. return service_update(s, msg, tb, add);
  172. }
  173. DEBUG(1, "Create service %s\n", name);
  174. s = service_alloc(name);
  175. if (!s)
  176. return UBUS_STATUS_UNKNOWN_ERROR;
  177. ret = service_update(s, msg, tb, add);
  178. if (ret)
  179. goto free;
  180. avl_insert(&services, &s->avl);
  181. return 0;
  182. free:
  183. free(msg);
  184. return ret;
  185. }
  186. static void
  187. service_dump(struct service *s, int verbose)
  188. {
  189. struct service_instance *in;
  190. void *c, *i;
  191. c = blobmsg_open_table(&b, s->name);
  192. if (avl_is_empty(&s->instances.avl)) {
  193. blobmsg_close_table(&b, c);
  194. return;
  195. }
  196. i = blobmsg_open_table(&b, "instances");
  197. vlist_for_each_element(&s->instances, in, node)
  198. instance_dump(&b, in, verbose);
  199. blobmsg_close_table(&b, i);
  200. if (verbose && s->trigger)
  201. blobmsg_add_blob(&b, s->trigger);
  202. blobmsg_close_table(&b, c);
  203. }
  204. static int
  205. service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
  206. struct ubus_request_data *req, const char *method,
  207. struct blob_attr *msg)
  208. {
  209. struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
  210. struct service *s;
  211. int verbose = 0;
  212. blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  213. if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
  214. verbose = 1;
  215. blob_buf_init(&b, 0);
  216. avl_for_each_element(&services, s, avl)
  217. service_dump(s, verbose);
  218. ubus_send_reply(ctx, req, b.head);
  219. return 0;
  220. }
  221. static int
  222. service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
  223. struct ubus_request_data *req, const char *method,
  224. struct blob_attr *msg)
  225. {
  226. struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
  227. struct service *s;
  228. struct service_instance *in;
  229. blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  230. cur = tb[SERVICE_DEL_ATTR_NAME];
  231. if (!cur)
  232. return UBUS_STATUS_NOT_FOUND;
  233. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  234. if (!s)
  235. return UBUS_STATUS_NOT_FOUND;
  236. cur = tb[SERVICE_DEL_ATTR_INSTANCE];
  237. if (!cur) {
  238. service_delete(s);
  239. return 0;
  240. }
  241. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  242. if (!in) {
  243. ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
  244. return UBUS_STATUS_NOT_FOUND;
  245. }
  246. vlist_delete(&s->instances, &in->node);
  247. return 0;
  248. }
  249. static int
  250. service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
  251. struct ubus_request_data *req, const char *method,
  252. struct blob_attr *msg)
  253. {
  254. struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
  255. struct service *s;
  256. blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  257. cur = tb[SERVICE_ATTR_NAME];
  258. if (!cur)
  259. return UBUS_STATUS_INVALID_ARGUMENT;
  260. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  261. if (!s)
  262. return UBUS_STATUS_NOT_FOUND;
  263. if (!strcmp(method, "update_start"))
  264. vlist_update(&s->instances);
  265. else
  266. vlist_flush(&s->instances);
  267. return 0;
  268. }
  269. static int
  270. service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
  271. struct ubus_request_data *req, const char *method,
  272. struct blob_attr *msg)
  273. {
  274. struct blob_attr *tb[__EVENT_MAX];
  275. if (!msg)
  276. return UBUS_STATUS_INVALID_ARGUMENT;
  277. blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
  278. if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
  279. return UBUS_STATUS_INVALID_ARGUMENT;
  280. trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
  281. return 0;
  282. }
  283. static struct ubus_method main_object_methods[] = {
  284. UBUS_METHOD("set", service_handle_set, service_set_attrs),
  285. UBUS_METHOD("add", service_handle_set, service_set_attrs),
  286. UBUS_METHOD("list", service_handle_list, service_attrs),
  287. UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
  288. UBUS_METHOD("update_start", service_handle_update, service_attrs),
  289. UBUS_METHOD("update_complete", service_handle_update, service_attrs),
  290. UBUS_METHOD("event", service_handle_event, event_policy),
  291. };
  292. static struct ubus_object_type main_object_type =
  293. UBUS_OBJECT_TYPE("service", main_object_methods);
  294. static struct ubus_object main_object = {
  295. .name = "service",
  296. .type = &main_object_type,
  297. .methods = main_object_methods,
  298. .n_methods = ARRAY_SIZE(main_object_methods),
  299. };
  300. void ubus_init_service(struct ubus_context *ctx)
  301. {
  302. avl_init(&services, avl_strcmp, false, NULL);
  303. ubus_add_object(ctx, &main_object);
  304. }