service.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 struct ubus_object main_object;
  24. static void
  25. service_instance_add(struct service *s, struct blob_attr *attr)
  26. {
  27. struct service_instance *in;
  28. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  29. return;
  30. in = calloc(1, sizeof(*in));
  31. if (!in)
  32. return;
  33. instance_init(in, s, attr);
  34. vlist_add(&s->instances, &in->node, (void *) in->name);
  35. }
  36. static void
  37. service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
  38. struct vlist_node *node_old)
  39. {
  40. struct service_instance *in_o = NULL, *in_n = NULL;
  41. if (node_old)
  42. in_o = container_of(node_old, struct service_instance, node);
  43. if (node_new)
  44. in_n = container_of(node_new, struct service_instance, node);
  45. if (in_o && in_n) {
  46. DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
  47. instance_update(in_o, in_n);
  48. instance_free(in_n);
  49. } else if (in_o) {
  50. DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
  51. instance_stop(in_o, true);
  52. } else if (in_n && in_n->srv->autostart) {
  53. DEBUG(2, "Start 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.no_delete = true;
  68. s->name = new_name;
  69. s->avl.key = s->name;
  70. INIT_LIST_HEAD(&s->validators);
  71. blobmsg_list_simple_init(&s->data_blob);
  72. return s;
  73. }
  74. enum {
  75. SERVICE_SET_NAME,
  76. SERVICE_SET_SCRIPT,
  77. SERVICE_SET_INSTANCES,
  78. SERVICE_SET_TRIGGER,
  79. SERVICE_SET_VALIDATE,
  80. SERVICE_SET_AUTOSTART,
  81. SERVICE_SET_DATA,
  82. __SERVICE_SET_MAX
  83. };
  84. static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
  85. [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
  86. [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
  87. [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
  88. [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
  89. [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
  90. [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
  91. [SERVICE_SET_DATA] = { "data", BLOBMSG_TYPE_TABLE },
  92. };
  93. static int
  94. service_update(struct service *s, struct blob_attr **tb, bool add)
  95. {
  96. struct blob_attr *cur;
  97. int rem;
  98. if (s->trigger) {
  99. trigger_del(s);
  100. free(s->trigger);
  101. s->trigger = NULL;
  102. }
  103. if (s->data) {
  104. blobmsg_list_free(&s->data_blob);
  105. free(s->data);
  106. s->data = NULL;
  107. }
  108. service_validate_del(s);
  109. if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
  110. s->autostart = false;
  111. else
  112. s->autostart = true;
  113. if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
  114. s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
  115. if (!s->trigger)
  116. return -1;
  117. trigger_add(s->trigger, s);
  118. }
  119. if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
  120. blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
  121. service_validate_add(s, cur);
  122. }
  123. if (tb[SERVICE_SET_INSTANCES]) {
  124. if (!add)
  125. vlist_update(&s->instances);
  126. blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
  127. service_instance_add(s, cur);
  128. }
  129. if (!add)
  130. vlist_flush(&s->instances);
  131. }
  132. if (tb[SERVICE_SET_DATA] && blobmsg_data_len(tb[SERVICE_SET_DATA])) {
  133. s->data = blob_memdup(tb[SERVICE_SET_DATA]);
  134. if (!s->data)
  135. return -1;
  136. blobmsg_list_fill(&s->data_blob, blobmsg_data(s->data),
  137. blobmsg_data_len(s->data), false);
  138. }
  139. s->deleted = false;
  140. rc(s->name, "running");
  141. return 0;
  142. }
  143. static void
  144. service_delete(struct service *s)
  145. {
  146. blobmsg_list_free(&s->data_blob);
  147. free(s->data);
  148. vlist_flush_all(&s->instances);
  149. s->deleted = true;
  150. service_stopped(s);
  151. }
  152. enum {
  153. SERVICE_ATTR_NAME,
  154. __SERVICE_ATTR_MAX,
  155. };
  156. static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
  157. [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  158. };
  159. enum {
  160. SERVICE_DEL_ATTR_NAME,
  161. SERVICE_DEL_ATTR_INSTANCE,
  162. __SERVICE_DEL_ATTR_MAX,
  163. };
  164. static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
  165. [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  166. [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  167. };
  168. enum {
  169. SERVICE_LIST_ATTR_NAME,
  170. SERVICE_LIST_ATTR_VERBOSE,
  171. __SERVICE_LIST_ATTR_MAX,
  172. };
  173. static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
  174. [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  175. [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
  176. };
  177. enum {
  178. SERVICE_SIGNAL_ATTR_NAME,
  179. SERVICE_SIGNAL_ATTR_INSTANCE,
  180. SERVICE_SIGNAL_ATTR_SIGNAL,
  181. __SERVICE_SIGNAL_ATTR_MAX,
  182. };
  183. static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
  184. [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  185. [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  186. [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
  187. };
  188. enum {
  189. SERVICE_STATE_ATTR_SPAWN,
  190. SERVICE_STATE_ATTR_NAME,
  191. __SERVICE_STATE_ATTR_MAX,
  192. };
  193. static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
  194. [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
  195. [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  196. };
  197. enum {
  198. EVENT_TYPE,
  199. EVENT_DATA,
  200. __EVENT_MAX
  201. };
  202. static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
  203. [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  204. [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
  205. };
  206. enum {
  207. VALIDATE_PACKAGE,
  208. VALIDATE_TYPE,
  209. VALIDATE_SERVICE,
  210. __VALIDATE_MAX
  211. };
  212. static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
  213. [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
  214. [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  215. [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
  216. };
  217. enum {
  218. DATA_NAME,
  219. DATA_INSTANCE,
  220. DATA_TYPE,
  221. __DATA_MAX
  222. };
  223. static const struct blobmsg_policy get_data_policy[] = {
  224. [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
  225. [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  226. [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
  227. };
  228. enum {
  229. SERVICE_CONSOLE_NAME,
  230. SERVICE_CONSOLE_INSTANCE,
  231. __SERVICE_CONSOLE_MAX,
  232. };
  233. static const struct blobmsg_policy service_console_policy[__SERVICE_CONSOLE_MAX] = {
  234. [SERVICE_CONSOLE_NAME] = { "name", BLOBMSG_TYPE_STRING },
  235. [SERVICE_CONSOLE_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  236. };
  237. static int
  238. service_handle_set(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_SET_MAX], *cur;
  243. struct service *s = NULL;
  244. const char *name;
  245. bool add = !strcmp(method, "add");
  246. int ret;
  247. blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  248. cur = tb[SERVICE_SET_NAME];
  249. if (!cur)
  250. return UBUS_STATUS_INVALID_ARGUMENT;
  251. name = blobmsg_data(cur);
  252. s = avl_find_element(&services, name, s, avl);
  253. if (s) {
  254. DEBUG(2, "Update service %s\n", name);
  255. return service_update(s, tb, add);
  256. }
  257. DEBUG(2, "Create service %s\n", name);
  258. s = service_alloc(name);
  259. if (!s)
  260. return UBUS_STATUS_UNKNOWN_ERROR;
  261. ret = service_update(s, tb, add);
  262. if (ret)
  263. return ret;
  264. avl_insert(&services, &s->avl);
  265. service_event("service.start", s->name, NULL);
  266. return 0;
  267. }
  268. static void
  269. service_dump(struct service *s, bool verbose)
  270. {
  271. struct service_instance *in;
  272. void *c, *i;
  273. c = blobmsg_open_table(&b, s->name);
  274. if (!s->autostart)
  275. blobmsg_add_u8(&b, "autostart", false);
  276. if (!avl_is_empty(&s->data_blob.avl)) {
  277. struct blobmsg_list_node *var;
  278. i = blobmsg_open_table(&b, "data");
  279. blobmsg_list_for_each(&s->data_blob, var)
  280. blobmsg_add_blob(&b, var->data);
  281. blobmsg_close_table(&b, i);
  282. }
  283. if (!avl_is_empty(&s->instances.avl)) {
  284. i = blobmsg_open_table(&b, "instances");
  285. vlist_for_each_element(&s->instances, in, node)
  286. instance_dump(&b, in, verbose);
  287. blobmsg_close_table(&b, i);
  288. }
  289. if (verbose && s->trigger)
  290. blobmsg_add_blob(&b, s->trigger);
  291. if (verbose && !list_empty(&s->validators))
  292. service_validate_dump(&b, s);
  293. blobmsg_close_table(&b, c);
  294. }
  295. static int
  296. service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
  297. struct ubus_request_data *req, const char *method,
  298. struct blob_attr *msg)
  299. {
  300. struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
  301. struct service *s;
  302. const char *name = NULL;
  303. bool verbose = false;
  304. blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  305. if (tb[SERVICE_LIST_ATTR_VERBOSE])
  306. verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
  307. if (tb[SERVICE_LIST_ATTR_NAME])
  308. name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
  309. blob_buf_init(&b, 0);
  310. avl_for_each_element(&services, s, avl) {
  311. if (name && strcmp(s->name, name) != 0)
  312. continue;
  313. service_dump(s, verbose);
  314. }
  315. ubus_send_reply(ctx, req, b.head);
  316. return 0;
  317. }
  318. static int
  319. service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
  320. struct ubus_request_data *req, const char *method,
  321. struct blob_attr *msg)
  322. {
  323. struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
  324. struct service *s;
  325. struct service_instance *in;
  326. blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  327. cur = tb[SERVICE_DEL_ATTR_NAME];
  328. if (!cur)
  329. return UBUS_STATUS_NOT_FOUND;
  330. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  331. if (!s)
  332. return UBUS_STATUS_NOT_FOUND;
  333. cur = tb[SERVICE_DEL_ATTR_INSTANCE];
  334. if (!cur) {
  335. service_delete(s);
  336. return 0;
  337. }
  338. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  339. if (!in) {
  340. ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
  341. return UBUS_STATUS_NOT_FOUND;
  342. }
  343. vlist_delete(&s->instances, &in->node);
  344. return 0;
  345. }
  346. static int
  347. service_handle_kill(struct service_instance *in, int sig)
  348. {
  349. if (kill(in->proc.pid, sig) == 0)
  350. return 0;
  351. switch (errno) {
  352. case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
  353. case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
  354. case ESRCH: return UBUS_STATUS_NOT_FOUND;
  355. }
  356. return UBUS_STATUS_UNKNOWN_ERROR;
  357. }
  358. static int
  359. service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
  360. struct ubus_request_data *req, const char *method,
  361. struct blob_attr *msg)
  362. {
  363. struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
  364. struct service *s;
  365. struct service_instance *in;
  366. int sig = SIGHUP;
  367. int rv = 0;
  368. blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  369. cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
  370. if (cur)
  371. sig = blobmsg_get_u32(cur);
  372. cur = tb[SERVICE_SIGNAL_ATTR_NAME];
  373. if (!cur)
  374. return UBUS_STATUS_NOT_FOUND;
  375. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  376. if (!s)
  377. return UBUS_STATUS_NOT_FOUND;
  378. cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
  379. if (!cur) {
  380. vlist_for_each_element(&s->instances, in, node)
  381. rv = service_handle_kill(in, sig);
  382. return rv;
  383. }
  384. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  385. if (!in) {
  386. ERROR("instance %s not found\n", blobmsg_get_string(cur));
  387. return UBUS_STATUS_NOT_FOUND;
  388. }
  389. return service_handle_kill(in, sig);
  390. }
  391. static int
  392. service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
  393. struct ubus_request_data *req, const char *method,
  394. struct blob_attr *msg)
  395. {
  396. struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
  397. struct service *s;
  398. struct service_instance *in;
  399. int spawn;
  400. blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  401. if (!tb[SERVICE_STATE_ATTR_SPAWN])
  402. return UBUS_STATUS_INVALID_ARGUMENT;
  403. if (!tb[SERVICE_STATE_ATTR_NAME])
  404. return UBUS_STATUS_NOT_FOUND;
  405. s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
  406. if (!s)
  407. return UBUS_STATUS_NOT_FOUND;
  408. spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
  409. vlist_for_each_element(&s->instances, in, node) {
  410. if (!!in->proc.pending == !!spawn)
  411. continue;
  412. else if (!in->proc.pending)
  413. instance_start(in);
  414. else
  415. instance_stop(in, false);
  416. }
  417. return UBUS_STATUS_OK;
  418. }
  419. static int
  420. service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
  421. struct ubus_request_data *req, const char *method,
  422. struct blob_attr *msg)
  423. {
  424. struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
  425. struct service *s;
  426. blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  427. cur = tb[SERVICE_SET_NAME];
  428. if (!cur)
  429. return UBUS_STATUS_INVALID_ARGUMENT;
  430. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  431. if (!s)
  432. return UBUS_STATUS_NOT_FOUND;
  433. if (!strcmp(method, "update_start"))
  434. vlist_update(&s->instances);
  435. else
  436. vlist_flush(&s->instances);
  437. return 0;
  438. }
  439. static void ubus_event_bcast(const char *type, const char *param1, const char *val1,
  440. const char *param2, const char *val2)
  441. {
  442. if (!ctx)
  443. return;
  444. blob_buf_init(&b, 0);
  445. if (param1 && val1)
  446. blobmsg_add_string(&b, param1, val1);
  447. if (param2 && val2)
  448. blobmsg_add_string(&b, param2, val2);
  449. ubus_notify(ctx, &main_object, type, b.head, -1);
  450. }
  451. static int
  452. service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
  453. struct ubus_request_data *req, const char *method,
  454. struct blob_attr *msg)
  455. {
  456. struct blob_attr *tb[__EVENT_MAX];
  457. const char *event;
  458. if (!msg)
  459. return UBUS_STATUS_INVALID_ARGUMENT;
  460. blobmsg_parse(event_policy, __EVENT_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  461. if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
  462. return UBUS_STATUS_INVALID_ARGUMENT;
  463. event = blobmsg_get_string(tb[EVENT_TYPE]);
  464. trigger_event(event, tb[EVENT_DATA]);
  465. if (!strcmp(event, "config.change")) {
  466. struct blob_attr *tb2[__VALIDATE_MAX];
  467. blobmsg_parse(validate_policy, __VALIDATE_MAX, tb2,
  468. blobmsg_data(tb[EVENT_DATA]), blobmsg_data_len(tb[EVENT_DATA]));
  469. if (tb2[VALIDATE_PACKAGE])
  470. ubus_event_bcast("config.change", "config",
  471. blobmsg_get_string(tb2[VALIDATE_PACKAGE]), NULL, NULL);
  472. }
  473. return 0;
  474. }
  475. static int
  476. service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
  477. struct ubus_request_data *req, const char *method,
  478. struct blob_attr *msg)
  479. {
  480. struct blob_attr *tb[__VALIDATE_MAX];
  481. char *p = NULL, *t = NULL;
  482. if (!msg)
  483. return UBUS_STATUS_INVALID_ARGUMENT;
  484. blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  485. if (tb[VALIDATE_SERVICE]) {
  486. return 0;
  487. }
  488. if (tb[VALIDATE_PACKAGE])
  489. p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
  490. if (tb[VALIDATE_TYPE])
  491. t = blobmsg_get_string(tb[VALIDATE_TYPE]);
  492. blob_buf_init(&b, 0);
  493. service_validate_dump_all(&b, p, t);
  494. ubus_send_reply(ctx, req, b.head);
  495. return 0;
  496. }
  497. static int
  498. service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
  499. struct ubus_request_data *req, const char *method,
  500. struct blob_attr *msg)
  501. {
  502. struct service_instance *in;
  503. struct service *s;
  504. struct blob_attr *tb[__DATA_MAX];
  505. const char *name = NULL;
  506. const char *instance = NULL;
  507. const char *type = NULL;
  508. blobmsg_parse(get_data_policy, __DATA_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  509. if (tb[DATA_NAME])
  510. name = blobmsg_data(tb[DATA_NAME]);
  511. if (tb[DATA_INSTANCE])
  512. instance = blobmsg_data(tb[DATA_INSTANCE]);
  513. if (tb[DATA_TYPE])
  514. type = blobmsg_data(tb[DATA_TYPE]);
  515. blob_buf_init(&b, 0);
  516. avl_for_each_element(&services, s, avl) {
  517. void *cs = NULL;
  518. void *ci = NULL;
  519. struct blobmsg_list_node *var;
  520. if (name && strcmp(name, s->name))
  521. continue;
  522. blobmsg_list_for_each(&s->data_blob, var) {
  523. if (type && strcmp(blobmsg_name(var->data), type))
  524. continue;
  525. if (!cs)
  526. cs = blobmsg_open_table(&b, s->name);
  527. blobmsg_add_blob(&b, var->data);
  528. }
  529. vlist_for_each_element(&s->instances, in, node) {
  530. ci = NULL;
  531. if (instance && strcmp(instance, in->name))
  532. continue;
  533. blobmsg_list_for_each(&in->data, var) {
  534. if (type &&
  535. strcmp(blobmsg_name(var->data), type))
  536. continue;
  537. if (!cs)
  538. cs = blobmsg_open_table(&b, s->name);
  539. if (!ci)
  540. ci = blobmsg_open_table(&b, in->name);
  541. blobmsg_add_blob(&b, var->data);
  542. }
  543. if (ci)
  544. blobmsg_close_table(&b, ci);
  545. }
  546. if (cs)
  547. blobmsg_close_table(&b, cs);
  548. }
  549. ubus_send_reply(ctx, req, b.head);
  550. return 0;
  551. }
  552. static int
  553. service_handle_console(struct ubus_context *ctx, struct ubus_object *obj,
  554. struct ubus_request_data *req, const char *method,
  555. struct blob_attr *msg)
  556. {
  557. bool attach = !strcmp(method, "console_attach");
  558. struct blob_attr *tb[__SERVICE_CONSOLE_MAX];
  559. struct service *s;
  560. struct service_instance *in;
  561. int console_fd = -1;
  562. console_fd = ubus_request_get_caller_fd(req);
  563. if (console_fd < 0)
  564. return UBUS_STATUS_INVALID_ARGUMENT;
  565. if (!msg)
  566. goto err_console_fd;
  567. blobmsg_parse(service_console_policy, __SERVICE_CONSOLE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  568. if (!tb[SERVICE_CONSOLE_NAME])
  569. goto err_console_fd;
  570. s = avl_find_element(&services, blobmsg_data(tb[SERVICE_CONSOLE_NAME]), s, avl);
  571. if (!s)
  572. goto err_console_fd;
  573. if (tb[SERVICE_CONSOLE_INSTANCE]) {
  574. in = vlist_find(&s->instances, blobmsg_data(tb[SERVICE_CONSOLE_INSTANCE]), in, node);
  575. } else {
  576. /* use first element in instances list */
  577. vlist_for_each_element(&s->instances, in, node)
  578. break;
  579. }
  580. if (!in)
  581. goto err_console_fd;
  582. if (attach) {
  583. if (in->console.fd.fd < 0) {
  584. close(console_fd);
  585. return UBUS_STATUS_NOT_SUPPORTED;
  586. }
  587. /* close and replace existing attached console */
  588. if (in->console_client.fd.fd > -1)
  589. close(in->console_client.fd.fd);
  590. ustream_fd_init(&in->console_client, console_fd);
  591. } else {
  592. ustream_fd_init(&in->console, console_fd);
  593. }
  594. return UBUS_STATUS_OK;
  595. err_console_fd:
  596. close(console_fd);
  597. return UBUS_STATUS_INVALID_ARGUMENT;
  598. }
  599. static struct ubus_method main_object_methods[] = {
  600. UBUS_METHOD("set", service_handle_set, service_set_attrs),
  601. UBUS_METHOD("add", service_handle_set, service_set_attrs),
  602. UBUS_METHOD("list", service_handle_list, service_list_attrs),
  603. UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
  604. UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
  605. UBUS_METHOD("update_start", service_handle_update, service_attrs),
  606. UBUS_METHOD("update_complete", service_handle_update, service_attrs),
  607. UBUS_METHOD("event", service_handle_event, event_policy),
  608. UBUS_METHOD("validate", service_handle_validate, validate_policy),
  609. UBUS_METHOD("get_data", service_get_data, get_data_policy),
  610. UBUS_METHOD("state", service_handle_state, service_state_attrs),
  611. UBUS_METHOD("console_set", service_handle_console, service_console_policy),
  612. UBUS_METHOD("console_attach", service_handle_console, service_console_policy),
  613. };
  614. static struct ubus_object_type main_object_type =
  615. UBUS_OBJECT_TYPE("service", main_object_methods);
  616. static struct ubus_object main_object = {
  617. .name = "service",
  618. .type = &main_object_type,
  619. .methods = main_object_methods,
  620. .n_methods = ARRAY_SIZE(main_object_methods),
  621. };
  622. int
  623. service_start_early(char *name, char *cmdline)
  624. {
  625. void *instances, *instance, *command, *respawn;
  626. char *t;
  627. blob_buf_init(&b, 0);
  628. blobmsg_add_string(&b, "name", name);
  629. instances = blobmsg_open_table(&b, "instances");
  630. instance = blobmsg_open_table(&b, "instance1");
  631. command = blobmsg_open_array(&b, "command");
  632. t = strtok(cmdline, " ");
  633. while (t) {
  634. blobmsg_add_string(&b, NULL, t);
  635. t = strtok(NULL, " ");
  636. }
  637. blobmsg_close_array(&b, command);
  638. respawn = blobmsg_open_array(&b, "respawn");
  639. blobmsg_add_string(&b, NULL, "3600");
  640. blobmsg_add_string(&b, NULL, "1");
  641. blobmsg_add_string(&b, NULL, "0");
  642. blobmsg_close_array(&b, respawn);
  643. blobmsg_close_table(&b, instance);
  644. blobmsg_close_table(&b, instances);
  645. return service_handle_set(NULL, NULL, NULL, "add", b.head);
  646. }
  647. void service_stopped(struct service *s)
  648. {
  649. if (s->deleted && avl_is_empty(&s->instances.avl)) {
  650. service_event("service.stop", s->name, NULL);
  651. avl_delete(&services, &s->avl);
  652. trigger_del(s);
  653. service_validate_del(s);
  654. free(s->trigger);
  655. free(s);
  656. }
  657. }
  658. void service_event(const char *type, const char *service, const char *instance)
  659. {
  660. ubus_event_bcast(type, "service", service, "instance", instance);
  661. }
  662. void ubus_init_service(struct ubus_context *_ctx)
  663. {
  664. ctx = _ctx;
  665. ubus_add_object(ctx, &main_object);
  666. }