service.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. AVL_TREE(services, avl_strcmp, false, NULL);
  21. static struct blob_buf b;
  22. static struct ubus_context *ctx;
  23. static void
  24. service_instance_add(struct service *s, struct blob_attr *attr)
  25. {
  26. struct service_instance *in;
  27. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  28. return;
  29. in = calloc(1, sizeof(*in));
  30. if (!in)
  31. return;
  32. instance_init(in, s, attr);
  33. vlist_add(&s->instances, &in->node, (void *) in->name);
  34. }
  35. static void
  36. service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
  37. struct vlist_node *node_old)
  38. {
  39. struct service_instance *in_o = NULL, *in_n = NULL;
  40. if (node_old)
  41. in_o = container_of(node_old, struct service_instance, node);
  42. if (node_new)
  43. in_n = container_of(node_new, struct service_instance, node);
  44. if (in_o && in_n) {
  45. DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
  46. instance_update(in_o, in_n);
  47. instance_free(in_n);
  48. } else if (in_o) {
  49. DEBUG(2, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
  50. instance_stop(in_o);
  51. instance_free(in_o);
  52. } else if (in_n) {
  53. DEBUG(2, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
  54. instance_start(in_n);
  55. }
  56. blob_buf_init(&b, 0);
  57. trigger_event("instance.update", b.head);
  58. }
  59. static struct service *
  60. service_alloc(const char *name)
  61. {
  62. struct service *s;
  63. char *new_name;
  64. s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
  65. strcpy(new_name, name);
  66. vlist_init(&s->instances, avl_strcmp, service_instance_update);
  67. s->instances.keep_old = true;
  68. s->name = new_name;
  69. s->avl.key = s->name;
  70. INIT_LIST_HEAD(&s->validators);
  71. return s;
  72. }
  73. enum {
  74. SERVICE_SET_NAME,
  75. SERVICE_SET_SCRIPT,
  76. SERVICE_SET_INSTANCES,
  77. SERVICE_SET_TRIGGER,
  78. SERVICE_SET_VALIDATE,
  79. __SERVICE_SET_MAX
  80. };
  81. static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
  82. [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
  83. [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
  84. [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
  85. [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
  86. [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
  87. };
  88. static int
  89. service_update(struct service *s, struct blob_attr **tb, bool add)
  90. {
  91. struct blob_attr *cur;
  92. int rem;
  93. if (s->trigger) {
  94. trigger_del(s);
  95. free(s->trigger);
  96. s->trigger = NULL;
  97. }
  98. service_validate_del(s);
  99. if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
  100. s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
  101. if (!s->trigger)
  102. return -1;
  103. trigger_add(s->trigger, s);
  104. }
  105. if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
  106. blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
  107. service_validate_add(s, cur);
  108. }
  109. if (tb[SERVICE_SET_INSTANCES]) {
  110. if (!add)
  111. vlist_update(&s->instances);
  112. blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
  113. service_instance_add(s, cur);
  114. }
  115. if (!add)
  116. vlist_flush(&s->instances);
  117. }
  118. rc(s->name, "running");
  119. return 0;
  120. }
  121. static void
  122. service_delete(struct service *s)
  123. {
  124. service_event("service.stop", s->name, NULL);
  125. vlist_flush_all(&s->instances);
  126. avl_delete(&services, &s->avl);
  127. trigger_del(s);
  128. free(s->trigger);
  129. free(s);
  130. service_validate_del(s);
  131. }
  132. enum {
  133. SERVICE_ATTR_NAME,
  134. __SERVICE_ATTR_MAX,
  135. };
  136. static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
  137. [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  138. };
  139. enum {
  140. SERVICE_DEL_ATTR_NAME,
  141. SERVICE_DEL_ATTR_INSTANCE,
  142. __SERVICE_DEL_ATTR_MAX,
  143. };
  144. static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
  145. [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  146. [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  147. };
  148. enum {
  149. SERVICE_LIST_ATTR_NAME,
  150. SERVICE_LIST_ATTR_VERBOSE,
  151. __SERVICE_LIST_ATTR_MAX,
  152. };
  153. static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
  154. [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  155. [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
  156. };
  157. enum {
  158. EVENT_TYPE,
  159. EVENT_DATA,
  160. __EVENT_MAX
  161. };
  162. static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
  163. [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  164. [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
  165. };
  166. enum {
  167. VALIDATE_PACKAGE,
  168. VALIDATE_TYPE,
  169. VALIDATE_SERVICE,
  170. __VALIDATE_MAX
  171. };
  172. static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
  173. [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
  174. [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  175. [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
  176. };
  177. enum {
  178. DATA_NAME,
  179. DATA_INSTANCE,
  180. DATA_TYPE,
  181. __DATA_MAX
  182. };
  183. static const struct blobmsg_policy get_data_policy[] = {
  184. [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
  185. [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  186. [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
  187. };
  188. static int
  189. service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
  190. struct ubus_request_data *req, const char *method,
  191. struct blob_attr *msg)
  192. {
  193. struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
  194. struct service *s = NULL;
  195. const char *name;
  196. bool add = !strcmp(method, "add");
  197. int ret;
  198. blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
  199. cur = tb[SERVICE_ATTR_NAME];
  200. if (!cur)
  201. return UBUS_STATUS_INVALID_ARGUMENT;
  202. name = blobmsg_data(cur);
  203. s = avl_find_element(&services, name, s, avl);
  204. if (s) {
  205. DEBUG(2, "Update service %s\n", name);
  206. return service_update(s, tb, add);
  207. }
  208. DEBUG(2, "Create service %s\n", name);
  209. s = service_alloc(name);
  210. if (!s)
  211. return UBUS_STATUS_UNKNOWN_ERROR;
  212. ret = service_update(s, tb, add);
  213. if (ret)
  214. return ret;
  215. avl_insert(&services, &s->avl);
  216. service_event("service.start", s->name, NULL);
  217. return 0;
  218. }
  219. static void
  220. service_dump(struct service *s, bool verbose)
  221. {
  222. struct service_instance *in;
  223. void *c, *i;
  224. c = blobmsg_open_table(&b, s->name);
  225. if (!avl_is_empty(&s->instances.avl)) {
  226. i = blobmsg_open_table(&b, "instances");
  227. vlist_for_each_element(&s->instances, in, node)
  228. instance_dump(&b, in, verbose);
  229. blobmsg_close_table(&b, i);
  230. }
  231. if (verbose && s->trigger)
  232. blobmsg_add_blob(&b, s->trigger);
  233. if (verbose && !list_empty(&s->validators))
  234. service_validate_dump(&b, s);
  235. blobmsg_close_table(&b, c);
  236. }
  237. static int
  238. service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
  239. struct ubus_request_data *req, const char *method,
  240. struct blob_attr *msg)
  241. {
  242. struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
  243. struct service *s;
  244. const char *name = NULL;
  245. bool verbose = false;
  246. blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  247. if (tb[SERVICE_LIST_ATTR_VERBOSE])
  248. verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
  249. if (tb[SERVICE_LIST_ATTR_NAME])
  250. name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
  251. blob_buf_init(&b, 0);
  252. avl_for_each_element(&services, s, avl) {
  253. if (name && strcmp(s->name, name) != 0)
  254. continue;
  255. service_dump(s, verbose);
  256. }
  257. ubus_send_reply(ctx, req, b.head);
  258. return 0;
  259. }
  260. static int
  261. service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
  262. struct ubus_request_data *req, const char *method,
  263. struct blob_attr *msg)
  264. {
  265. struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
  266. struct service *s;
  267. struct service_instance *in;
  268. blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  269. cur = tb[SERVICE_DEL_ATTR_NAME];
  270. if (!cur)
  271. return UBUS_STATUS_NOT_FOUND;
  272. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  273. if (!s)
  274. return UBUS_STATUS_NOT_FOUND;
  275. cur = tb[SERVICE_DEL_ATTR_INSTANCE];
  276. if (!cur) {
  277. service_delete(s);
  278. return 0;
  279. }
  280. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  281. if (!in) {
  282. ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
  283. return UBUS_STATUS_NOT_FOUND;
  284. }
  285. vlist_delete(&s->instances, &in->node);
  286. return 0;
  287. }
  288. static int
  289. service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
  290. struct ubus_request_data *req, const char *method,
  291. struct blob_attr *msg)
  292. {
  293. struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
  294. struct service *s;
  295. blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
  296. cur = tb[SERVICE_ATTR_NAME];
  297. if (!cur)
  298. return UBUS_STATUS_INVALID_ARGUMENT;
  299. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  300. if (!s)
  301. return UBUS_STATUS_NOT_FOUND;
  302. if (!strcmp(method, "update_start"))
  303. vlist_update(&s->instances);
  304. else
  305. vlist_flush(&s->instances);
  306. return 0;
  307. }
  308. static int
  309. service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
  310. struct ubus_request_data *req, const char *method,
  311. struct blob_attr *msg)
  312. {
  313. struct blob_attr *tb[__EVENT_MAX];
  314. if (!msg)
  315. return UBUS_STATUS_INVALID_ARGUMENT;
  316. blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
  317. if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
  318. return UBUS_STATUS_INVALID_ARGUMENT;
  319. trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
  320. return 0;
  321. }
  322. static int
  323. service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
  324. struct ubus_request_data *req, const char *method,
  325. struct blob_attr *msg)
  326. {
  327. struct blob_attr *tb[__VALIDATE_MAX];
  328. char *p = NULL, *t = NULL;
  329. if (!msg)
  330. return UBUS_STATUS_INVALID_ARGUMENT;
  331. blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
  332. if (tb[VALIDATE_SERVICE]) {
  333. return 0;
  334. }
  335. if (tb[VALIDATE_PACKAGE])
  336. p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
  337. if (tb[VALIDATE_TYPE])
  338. t = blobmsg_get_string(tb[VALIDATE_TYPE]);
  339. blob_buf_init(&b, 0);
  340. service_validate_dump_all(&b, p, t);
  341. ubus_send_reply(ctx, req, b.head);
  342. return 0;
  343. }
  344. static int
  345. service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
  346. struct ubus_request_data *req, const char *method,
  347. struct blob_attr *msg)
  348. {
  349. struct service_instance *in;
  350. struct service *s;
  351. struct blob_attr *tb[__DATA_MAX];
  352. const char *name = NULL;
  353. const char *instance = NULL;
  354. const char *type = NULL;
  355. blobmsg_parse(get_data_policy, __DATA_MAX, tb, blob_data(msg), blob_len(msg));
  356. if (tb[DATA_NAME])
  357. name = blobmsg_data(tb[DATA_NAME]);
  358. if (tb[DATA_INSTANCE])
  359. instance = blobmsg_data(tb[DATA_INSTANCE]);
  360. if (tb[DATA_TYPE])
  361. type = blobmsg_data(tb[DATA_TYPE]);
  362. blob_buf_init(&b, 0);
  363. avl_for_each_element(&services, s, avl) {
  364. void *cs = NULL;
  365. if (name && strcmp(name, s->name))
  366. continue;
  367. vlist_for_each_element(&s->instances, in, node) {
  368. struct blobmsg_list_node *var;
  369. void *ci = NULL;
  370. if (instance && strcmp(instance, in->name))
  371. continue;
  372. blobmsg_list_for_each(&in->data, var) {
  373. if (type &&
  374. strcmp(blobmsg_name(var->data), type))
  375. continue;
  376. if (!cs)
  377. cs = blobmsg_open_table(&b, s->name);
  378. if (!ci)
  379. ci = blobmsg_open_table(&b, in->name);
  380. blobmsg_add_blob(&b, var->data);
  381. }
  382. if (ci)
  383. blobmsg_close_table(&b, ci);
  384. }
  385. if (cs)
  386. blobmsg_close_table(&b, cs);
  387. }
  388. ubus_send_reply(ctx, req, b.head);
  389. return 0;
  390. }
  391. static struct ubus_method main_object_methods[] = {
  392. UBUS_METHOD("set", service_handle_set, service_set_attrs),
  393. UBUS_METHOD("add", service_handle_set, service_set_attrs),
  394. UBUS_METHOD("list", service_handle_list, service_list_attrs),
  395. UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
  396. UBUS_METHOD("update_start", service_handle_update, service_attrs),
  397. UBUS_METHOD("update_complete", service_handle_update, service_attrs),
  398. UBUS_METHOD("event", service_handle_event, event_policy),
  399. UBUS_METHOD("validate", service_handle_validate, validate_policy),
  400. UBUS_METHOD("get_data", service_get_data, get_data_policy),
  401. };
  402. static struct ubus_object_type main_object_type =
  403. UBUS_OBJECT_TYPE("service", main_object_methods);
  404. static struct ubus_object main_object = {
  405. .name = "service",
  406. .type = &main_object_type,
  407. .methods = main_object_methods,
  408. .n_methods = ARRAY_SIZE(main_object_methods),
  409. };
  410. int
  411. service_start_early(char *name, char *cmdline)
  412. {
  413. void *instances, *instance, *command, *respawn;
  414. char *t;
  415. blob_buf_init(&b, 0);
  416. blobmsg_add_string(&b, "name", name);
  417. instances = blobmsg_open_table(&b, "instances");
  418. instance = blobmsg_open_table(&b, "instance1");
  419. command = blobmsg_open_array(&b, "command");
  420. t = strtok(cmdline, " ");
  421. while (t) {
  422. blobmsg_add_string(&b, NULL, t);
  423. t = strtok(NULL, " ");
  424. }
  425. blobmsg_close_array(&b, command);
  426. respawn = blobmsg_open_array(&b, "respawn");
  427. blobmsg_add_string(&b, NULL, "3600");
  428. blobmsg_add_string(&b, NULL, "1");
  429. blobmsg_add_string(&b, NULL, "0");
  430. blobmsg_close_array(&b, respawn);
  431. blobmsg_close_table(&b, instance);
  432. blobmsg_close_table(&b, instances);
  433. return service_handle_set(NULL, NULL, NULL, "add", b.head);
  434. }
  435. void service_event(const char *type, const char *service, const char *instance)
  436. {
  437. if (!ctx)
  438. return;
  439. blob_buf_init(&b, 0);
  440. blobmsg_add_string(&b, "service", service);
  441. if (instance)
  442. blobmsg_add_string(&b, "instance", instance);
  443. ubus_notify(ctx, &main_object, type, b.head, -1);
  444. }
  445. void ubus_init_service(struct ubus_context *_ctx)
  446. {
  447. ctx = _ctx;
  448. ubus_add_object(ctx, &main_object);
  449. }