service.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <sys/utsname.h>
  17. #include <sys/types.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <sched.h>
  21. #include <libubox/blobmsg_json.h>
  22. #include <libubox/avl-cmp.h>
  23. #include "../procd.h"
  24. #include "service.h"
  25. #include "instance.h"
  26. #include "../rcS.h"
  27. AVL_TREE(services, avl_strcmp, false, NULL);
  28. AVL_TREE(containers, avl_strcmp, false, NULL);
  29. static struct blob_buf b;
  30. static struct ubus_context *ctx;
  31. static struct ubus_object main_object;
  32. static void
  33. service_instance_add(struct service *s, struct blob_attr *attr)
  34. {
  35. struct service_instance *in;
  36. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  37. return;
  38. in = calloc(1, sizeof(*in));
  39. if (!in)
  40. return;
  41. instance_init(in, s, attr);
  42. vlist_add(&s->instances, &in->node, (void *) in->name);
  43. }
  44. static void
  45. service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
  46. struct vlist_node *node_old)
  47. {
  48. struct service_instance *in_o = NULL, *in_n = NULL;
  49. if (node_old)
  50. in_o = container_of(node_old, struct service_instance, node);
  51. if (node_new)
  52. in_n = container_of(node_new, struct service_instance, node);
  53. if (in_o && in_n) {
  54. P_DEBUG(2, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
  55. instance_update(in_o, in_n);
  56. instance_free(in_n);
  57. } else if (in_o) {
  58. P_DEBUG(2, "Stop instance %s::%s\n", in_o->srv->name, in_o->name);
  59. instance_stop(in_o, true);
  60. } else if (in_n && in_n->srv->autostart) {
  61. P_DEBUG(2, "Start instance %s::%s\n", in_n->srv->name, in_n->name);
  62. instance_start(in_n);
  63. }
  64. blob_buf_init(&b, 0);
  65. trigger_event("instance.update", b.head);
  66. }
  67. static struct service *
  68. service_alloc(const char *name)
  69. {
  70. struct service *s;
  71. char *new_name;
  72. s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
  73. strcpy(new_name, name);
  74. vlist_init(&s->instances, avl_strcmp, service_instance_update);
  75. s->instances.no_delete = true;
  76. s->name = new_name;
  77. s->avl.key = s->name;
  78. INIT_LIST_HEAD(&s->validators);
  79. blobmsg_list_simple_init(&s->data_blob);
  80. return s;
  81. }
  82. enum {
  83. SERVICE_SET_NAME,
  84. SERVICE_SET_SCRIPT,
  85. SERVICE_SET_INSTANCES,
  86. SERVICE_SET_TRIGGER,
  87. SERVICE_SET_VALIDATE,
  88. SERVICE_SET_AUTOSTART,
  89. SERVICE_SET_DATA,
  90. __SERVICE_SET_MAX
  91. };
  92. static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
  93. [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
  94. [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
  95. [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
  96. [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
  97. [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
  98. [SERVICE_SET_AUTOSTART] = { "autostart", BLOBMSG_TYPE_BOOL },
  99. [SERVICE_SET_DATA] = { "data", BLOBMSG_TYPE_TABLE },
  100. };
  101. static int
  102. service_update(struct service *s, struct blob_attr **tb, bool add)
  103. {
  104. struct blob_attr *cur;
  105. int rem;
  106. if (s->trigger) {
  107. trigger_del(s);
  108. free(s->trigger);
  109. s->trigger = NULL;
  110. }
  111. if (s->data) {
  112. blobmsg_list_free(&s->data_blob);
  113. free(s->data);
  114. s->data = NULL;
  115. }
  116. service_validate_del(s);
  117. if (tb[SERVICE_SET_AUTOSTART] && !blobmsg_get_bool(tb[SERVICE_SET_AUTOSTART]))
  118. s->autostart = false;
  119. else
  120. s->autostart = true;
  121. if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
  122. s->trigger = blob_memdup(tb[SERVICE_SET_TRIGGER]);
  123. if (!s->trigger)
  124. return -1;
  125. trigger_add(s->trigger, s);
  126. }
  127. if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
  128. blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
  129. service_validate_add(s, cur);
  130. }
  131. if (tb[SERVICE_SET_INSTANCES]) {
  132. if (!add)
  133. vlist_update(&s->instances);
  134. blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
  135. service_instance_add(s, cur);
  136. }
  137. if (!add)
  138. vlist_flush(&s->instances);
  139. }
  140. if (tb[SERVICE_SET_DATA] && blobmsg_data_len(tb[SERVICE_SET_DATA])) {
  141. s->data = blob_memdup(tb[SERVICE_SET_DATA]);
  142. if (!s->data)
  143. return -1;
  144. blobmsg_list_fill(&s->data_blob, blobmsg_data(s->data),
  145. blobmsg_data_len(s->data), false);
  146. }
  147. s->deleted = false;
  148. rc(s->name, "running");
  149. return 0;
  150. }
  151. static void
  152. service_delete(struct service *s, bool container)
  153. {
  154. blobmsg_list_free(&s->data_blob);
  155. free(s->data);
  156. vlist_flush_all(&s->instances);
  157. s->deleted = true;
  158. service_stopped(s);
  159. }
  160. enum {
  161. SERVICE_ATTR_NAME,
  162. __SERVICE_ATTR_MAX,
  163. };
  164. static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
  165. [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  166. };
  167. enum {
  168. SERVICE_DEL_ATTR_NAME,
  169. SERVICE_DEL_ATTR_INSTANCE,
  170. __SERVICE_DEL_ATTR_MAX,
  171. };
  172. static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
  173. [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  174. [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  175. };
  176. enum {
  177. SERVICE_LIST_ATTR_NAME,
  178. SERVICE_LIST_ATTR_VERBOSE,
  179. __SERVICE_LIST_ATTR_MAX,
  180. };
  181. static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
  182. [SERVICE_LIST_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  183. [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
  184. };
  185. enum {
  186. SERVICE_SIGNAL_ATTR_NAME,
  187. SERVICE_SIGNAL_ATTR_INSTANCE,
  188. SERVICE_SIGNAL_ATTR_SIGNAL,
  189. __SERVICE_SIGNAL_ATTR_MAX,
  190. };
  191. static const struct blobmsg_policy service_signal_attrs[__SERVICE_SIGNAL_ATTR_MAX] = {
  192. [SERVICE_SIGNAL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  193. [SERVICE_SIGNAL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  194. [SERVICE_SIGNAL_ATTR_SIGNAL] = { "signal", BLOBMSG_TYPE_INT32 },
  195. };
  196. enum {
  197. SERVICE_STATE_ATTR_SPAWN,
  198. SERVICE_STATE_ATTR_NAME,
  199. __SERVICE_STATE_ATTR_MAX,
  200. };
  201. static const struct blobmsg_policy service_state_attrs[__SERVICE_STATE_ATTR_MAX] = {
  202. [SERVICE_STATE_ATTR_SPAWN] = { "spawn", BLOBMSG_TYPE_BOOL },
  203. [SERVICE_STATE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
  204. };
  205. enum {
  206. EVENT_TYPE,
  207. EVENT_DATA,
  208. __EVENT_MAX
  209. };
  210. static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
  211. [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  212. [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
  213. };
  214. enum {
  215. VALIDATE_PACKAGE,
  216. VALIDATE_TYPE,
  217. VALIDATE_SERVICE,
  218. __VALIDATE_MAX
  219. };
  220. static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
  221. [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
  222. [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  223. [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
  224. };
  225. enum {
  226. DATA_NAME,
  227. DATA_INSTANCE,
  228. DATA_TYPE,
  229. __DATA_MAX
  230. };
  231. static const struct blobmsg_policy get_data_policy[] = {
  232. [DATA_NAME] = { "name", BLOBMSG_TYPE_STRING },
  233. [DATA_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  234. [DATA_TYPE] = { "type", BLOBMSG_TYPE_STRING },
  235. };
  236. enum {
  237. CONTAINER_CONSOLE_NAME,
  238. CONTAINER_CONSOLE_INSTANCE,
  239. __CONTAINER_CONSOLE_MAX,
  240. };
  241. static const struct blobmsg_policy container_console_policy[__CONTAINER_CONSOLE_MAX] = {
  242. [CONTAINER_CONSOLE_NAME] = { "name", BLOBMSG_TYPE_STRING },
  243. [CONTAINER_CONSOLE_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  244. };
  245. static inline bool is_container_obj(struct ubus_object *obj)
  246. {
  247. return (obj && (strcmp(obj->name, "container") == 0));
  248. }
  249. static inline void put_namespace(struct blob_buf *b, char *name)
  250. {
  251. char nsfname[32];
  252. struct stat statbuf;
  253. snprintf(nsfname, sizeof(nsfname), "/proc/self/ns/%s", name);
  254. if (!stat(nsfname, &statbuf))
  255. blobmsg_add_string(b, NULL, name);
  256. }
  257. static void put_cgroups(struct blob_buf *b)
  258. {
  259. int fd, ret;
  260. static char buf[512] = "";
  261. char *t, *z;
  262. fd = open("/sys/fs/cgroup/cgroup.controllers", O_RDONLY);
  263. if (fd == -1)
  264. return;
  265. ret = read(fd, &buf, sizeof(buf));
  266. /* make sure buffer is NULL-terminated */
  267. buf[sizeof(buf)-1] = '\0';
  268. close(fd);
  269. if (ret < 2)
  270. return;
  271. t = buf;
  272. while(t) {
  273. z = t;
  274. /* replace space with \0 and direct next entry */
  275. t = strchr(z, ' ');
  276. if (t) {
  277. *(t++) = '\0';
  278. } else { /* replace trailing new-line with \0 */
  279. t = strchr(z, '\n');
  280. if (!t) /* shouldn't happen, but don't segfault if it does */
  281. break;
  282. *t = '\0';
  283. t = NULL;
  284. }
  285. blobmsg_add_string(b, NULL, z);
  286. }
  287. }
  288. static int
  289. container_handle_features(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 utsname utsbuf;
  294. struct stat statbuf;
  295. void *nsarray, *cgarray;
  296. if (stat("/sbin/ujail", &statbuf))
  297. return UBUS_STATUS_NOT_SUPPORTED;
  298. if (uname(&utsbuf) < 0)
  299. return UBUS_STATUS_UNKNOWN_ERROR;
  300. blob_buf_init(&b, 0);
  301. blobmsg_add_string(&b, "machine", utsbuf.machine);
  302. #ifdef SECCOMP_SUPPORT
  303. blobmsg_add_u8(&b, "seccomp", true);
  304. #else
  305. blobmsg_add_u8(&b, "seccomp", false);
  306. #endif
  307. cgarray = blobmsg_open_array(&b, "cgroup");
  308. put_cgroups(&b);
  309. blobmsg_close_array(&b, cgarray);
  310. nsarray = blobmsg_open_array(&b, "namespaces");
  311. put_namespace(&b, "cgroup");
  312. put_namespace(&b, "ipc");
  313. put_namespace(&b, "mnt");
  314. put_namespace(&b, "net");
  315. put_namespace(&b, "pid");
  316. #ifdef CLONE_NEWTIME
  317. put_namespace(&b, "time");
  318. #endif
  319. put_namespace(&b, "user");
  320. put_namespace(&b, "uts");
  321. blobmsg_close_array(&b, nsarray);
  322. ubus_send_reply(ctx, req, b.head);
  323. return UBUS_STATUS_OK;
  324. }
  325. static int
  326. service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
  327. struct ubus_request_data *req, const char *method,
  328. struct blob_attr *msg)
  329. {
  330. struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
  331. struct service *s = NULL;
  332. const char *name;
  333. bool container = is_container_obj(obj);
  334. bool add = !strcmp(method, "add");
  335. int ret;
  336. blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  337. cur = tb[SERVICE_SET_NAME];
  338. if (!cur)
  339. return UBUS_STATUS_INVALID_ARGUMENT;
  340. name = blobmsg_data(cur);
  341. if (container)
  342. s = avl_find_element(&containers, name, s, avl);
  343. else
  344. s = avl_find_element(&services, name, s, avl);
  345. if (s) {
  346. P_DEBUG(2, "Update service %s\n", name);
  347. return service_update(s, tb, add);
  348. }
  349. P_DEBUG(2, "Create service %s\n", name);
  350. s = service_alloc(name);
  351. if (!s)
  352. return UBUS_STATUS_UNKNOWN_ERROR;
  353. s->container = container;
  354. ret = service_update(s, tb, add);
  355. if (ret)
  356. return ret;
  357. if (container) {
  358. avl_insert(&containers, &s->avl);
  359. service_event("container.start", s->name, NULL);
  360. } else {
  361. avl_insert(&services, &s->avl);
  362. service_event("service.start", s->name, NULL);
  363. }
  364. return 0;
  365. }
  366. static void
  367. service_dump(struct service *s, bool verbose)
  368. {
  369. struct service_instance *in;
  370. void *c, *i;
  371. c = blobmsg_open_table(&b, s->name);
  372. if (!s->autostart)
  373. blobmsg_add_u8(&b, "autostart", false);
  374. if (!avl_is_empty(&s->data_blob.avl)) {
  375. struct blobmsg_list_node *var;
  376. i = blobmsg_open_table(&b, "data");
  377. blobmsg_list_for_each(&s->data_blob, var)
  378. blobmsg_add_blob(&b, var->data);
  379. blobmsg_close_table(&b, i);
  380. }
  381. if (!avl_is_empty(&s->instances.avl)) {
  382. i = blobmsg_open_table(&b, "instances");
  383. vlist_for_each_element(&s->instances, in, node)
  384. instance_dump(&b, in, verbose);
  385. blobmsg_close_table(&b, i);
  386. }
  387. if (verbose && s->trigger)
  388. blobmsg_add_blob(&b, s->trigger);
  389. if (verbose && !list_empty(&s->validators))
  390. service_validate_dump(&b, s);
  391. blobmsg_close_table(&b, c);
  392. }
  393. static int
  394. service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
  395. struct ubus_request_data *req, const char *method,
  396. struct blob_attr *msg)
  397. {
  398. struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
  399. struct service *s;
  400. const char *name = NULL;
  401. bool verbose = false;
  402. bool container = is_container_obj(obj);
  403. const struct avl_tree *tree = container?&containers:&services;
  404. blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  405. if (tb[SERVICE_LIST_ATTR_VERBOSE])
  406. verbose = blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]);
  407. if (tb[SERVICE_LIST_ATTR_NAME])
  408. name = blobmsg_get_string(tb[SERVICE_LIST_ATTR_NAME]);
  409. blob_buf_init(&b, 0);
  410. avl_for_each_element(tree, s, avl) {
  411. if (name && strcmp(s->name, name) != 0)
  412. continue;
  413. service_dump(s, verbose);
  414. }
  415. ubus_send_reply(ctx, req, b.head);
  416. return 0;
  417. }
  418. static int
  419. service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
  420. struct ubus_request_data *req, const char *method,
  421. struct blob_attr *msg)
  422. {
  423. struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
  424. struct service *s;
  425. struct service_instance *in;
  426. bool container = is_container_obj(obj);
  427. blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  428. cur = tb[SERVICE_DEL_ATTR_NAME];
  429. if (!cur)
  430. return UBUS_STATUS_NOT_FOUND;
  431. if (container)
  432. s = avl_find_element(&containers, blobmsg_data(cur), s, avl);
  433. else
  434. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  435. if (!s)
  436. return UBUS_STATUS_NOT_FOUND;
  437. cur = tb[SERVICE_DEL_ATTR_INSTANCE];
  438. if (!cur) {
  439. service_delete(s, container);
  440. return 0;
  441. }
  442. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  443. if (!in) {
  444. ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
  445. return UBUS_STATUS_NOT_FOUND;
  446. }
  447. vlist_delete(&s->instances, &in->node);
  448. return 0;
  449. }
  450. static int
  451. service_handle_kill(struct service_instance *in, int sig)
  452. {
  453. if (kill(in->proc.pid, sig) == 0)
  454. return 0;
  455. switch (errno) {
  456. case EINVAL: return UBUS_STATUS_INVALID_ARGUMENT;
  457. case EPERM: return UBUS_STATUS_PERMISSION_DENIED;
  458. case ESRCH: return UBUS_STATUS_NOT_FOUND;
  459. }
  460. return UBUS_STATUS_UNKNOWN_ERROR;
  461. }
  462. static int
  463. service_handle_signal(struct ubus_context *ctx, struct ubus_object *obj,
  464. struct ubus_request_data *req, const char *method,
  465. struct blob_attr *msg)
  466. {
  467. struct blob_attr *tb[__SERVICE_SIGNAL_ATTR_MAX], *cur;
  468. struct service *s;
  469. struct service_instance *in;
  470. bool container = is_container_obj(obj);
  471. int sig = SIGHUP;
  472. int rv = 0;
  473. blobmsg_parse(service_signal_attrs, __SERVICE_SIGNAL_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  474. cur = tb[SERVICE_SIGNAL_ATTR_SIGNAL];
  475. if (cur)
  476. sig = blobmsg_get_u32(cur);
  477. cur = tb[SERVICE_SIGNAL_ATTR_NAME];
  478. if (!cur)
  479. return UBUS_STATUS_NOT_FOUND;
  480. if (container)
  481. s = avl_find_element(&containers, blobmsg_data(cur), s, avl);
  482. else
  483. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  484. if (!s)
  485. return UBUS_STATUS_NOT_FOUND;
  486. cur = tb[SERVICE_SIGNAL_ATTR_INSTANCE];
  487. if (!cur) {
  488. vlist_for_each_element(&s->instances, in, node)
  489. rv = service_handle_kill(in, sig);
  490. return rv;
  491. }
  492. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  493. if (!in) {
  494. ERROR("instance %s not found\n", blobmsg_get_string(cur));
  495. return UBUS_STATUS_NOT_FOUND;
  496. }
  497. return service_handle_kill(in, sig);
  498. }
  499. static int
  500. service_handle_state(struct ubus_context *ctx, struct ubus_object *obj,
  501. struct ubus_request_data *req, const char *method,
  502. struct blob_attr *msg)
  503. {
  504. struct blob_attr *tb[__SERVICE_STATE_ATTR_MAX];
  505. struct service *s;
  506. struct service_instance *in;
  507. bool container = is_container_obj(obj);
  508. int spawn;
  509. blobmsg_parse(service_state_attrs, __SERVICE_STATE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  510. if (!tb[SERVICE_STATE_ATTR_SPAWN])
  511. return UBUS_STATUS_INVALID_ARGUMENT;
  512. if (!tb[SERVICE_STATE_ATTR_NAME])
  513. return UBUS_STATUS_NOT_FOUND;
  514. if (container)
  515. s = avl_find_element(&containers, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
  516. else
  517. s = avl_find_element(&services, blobmsg_data(tb[SERVICE_STATE_ATTR_NAME]), s, avl);
  518. if (!s)
  519. return UBUS_STATUS_NOT_FOUND;
  520. spawn = !!blobmsg_get_u8(tb[SERVICE_STATE_ATTR_SPAWN]);
  521. vlist_for_each_element(&s->instances, in, node) {
  522. if (!!in->proc.pending == !!spawn)
  523. continue;
  524. else if (!in->proc.pending)
  525. instance_start(in);
  526. else
  527. instance_stop(in, false);
  528. }
  529. return UBUS_STATUS_OK;
  530. }
  531. static void
  532. service_avl_stop_all(struct avl_tree *sctree, unsigned int *term_timeout)
  533. {
  534. struct service *s;
  535. avl_for_each_element(sctree, s, avl) {
  536. struct service_instance *in, *ptr;
  537. vlist_for_each_element_safe(&s->instances, in, node, ptr) {
  538. if (in->term_timeout > *term_timeout)
  539. *term_timeout = in->term_timeout;
  540. instance_stop(in, true);
  541. }
  542. }
  543. }
  544. void
  545. service_stop_all(void)
  546. {
  547. unsigned int term_timeout = 0;
  548. service_avl_stop_all(&containers, &term_timeout);
  549. service_avl_stop_all(&services, &term_timeout);
  550. procd_inittab_kill();
  551. sleep(term_timeout);
  552. }
  553. static int
  554. service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
  555. struct ubus_request_data *req, const char *method,
  556. struct blob_attr *msg)
  557. {
  558. struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
  559. struct service *s;
  560. bool container = is_container_obj(obj);
  561. blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  562. cur = tb[SERVICE_SET_NAME];
  563. if (!cur)
  564. return UBUS_STATUS_INVALID_ARGUMENT;
  565. if (container)
  566. s = avl_find_element(&containers, blobmsg_data(cur), s, avl);
  567. else
  568. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  569. if (!s)
  570. return UBUS_STATUS_NOT_FOUND;
  571. if (!strcmp(method, "update_start"))
  572. vlist_update(&s->instances);
  573. else
  574. vlist_flush(&s->instances);
  575. return 0;
  576. }
  577. static void ubus_event_bcast(const char *type, const char *param1, const char *val1,
  578. const char *param2, const char *val2)
  579. {
  580. if (!ctx)
  581. return;
  582. blob_buf_init(&b, 0);
  583. if (param1 && val1)
  584. blobmsg_add_string(&b, param1, val1);
  585. if (param2 && val2)
  586. blobmsg_add_string(&b, param2, val2);
  587. ubus_notify(ctx, &main_object, type, b.head, -1);
  588. }
  589. static int
  590. service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
  591. struct ubus_request_data *req, const char *method,
  592. struct blob_attr *msg)
  593. {
  594. struct blob_attr *tb[__EVENT_MAX];
  595. const char *event;
  596. if (!msg)
  597. return UBUS_STATUS_INVALID_ARGUMENT;
  598. blobmsg_parse(event_policy, __EVENT_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  599. if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
  600. return UBUS_STATUS_INVALID_ARGUMENT;
  601. event = blobmsg_get_string(tb[EVENT_TYPE]);
  602. trigger_event(event, tb[EVENT_DATA]);
  603. if (!strcmp(event, "config.change")) {
  604. struct blob_attr *tb2[__VALIDATE_MAX];
  605. blobmsg_parse(validate_policy, __VALIDATE_MAX, tb2,
  606. blobmsg_data(tb[EVENT_DATA]), blobmsg_data_len(tb[EVENT_DATA]));
  607. if (tb2[VALIDATE_PACKAGE])
  608. ubus_event_bcast("config.change", "config",
  609. blobmsg_get_string(tb2[VALIDATE_PACKAGE]), NULL, NULL);
  610. }
  611. return 0;
  612. }
  613. static int
  614. service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
  615. struct ubus_request_data *req, const char *method,
  616. struct blob_attr *msg)
  617. {
  618. struct blob_attr *tb[__VALIDATE_MAX];
  619. char *p = NULL, *t = NULL;
  620. if (!msg)
  621. return UBUS_STATUS_INVALID_ARGUMENT;
  622. blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  623. if (tb[VALIDATE_SERVICE]) {
  624. return 0;
  625. }
  626. if (tb[VALIDATE_PACKAGE])
  627. p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
  628. if (tb[VALIDATE_TYPE])
  629. t = blobmsg_get_string(tb[VALIDATE_TYPE]);
  630. blob_buf_init(&b, 0);
  631. service_validate_dump_all(&b, p, t);
  632. ubus_send_reply(ctx, req, b.head);
  633. return 0;
  634. }
  635. static int
  636. service_get_data(struct ubus_context *ctx, struct ubus_object *obj,
  637. struct ubus_request_data *req, const char *method,
  638. struct blob_attr *msg)
  639. {
  640. struct service_instance *in;
  641. struct service *s;
  642. struct blob_attr *tb[__DATA_MAX];
  643. const char *name = NULL;
  644. const char *instance = NULL;
  645. const char *type = NULL;
  646. blobmsg_parse(get_data_policy, __DATA_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  647. if (tb[DATA_NAME])
  648. name = blobmsg_data(tb[DATA_NAME]);
  649. if (tb[DATA_INSTANCE])
  650. instance = blobmsg_data(tb[DATA_INSTANCE]);
  651. if (tb[DATA_TYPE])
  652. type = blobmsg_data(tb[DATA_TYPE]);
  653. blob_buf_init(&b, 0);
  654. avl_for_each_element(&services, s, avl) {
  655. void *cs = NULL;
  656. void *ci = NULL;
  657. struct blobmsg_list_node *var;
  658. if (name && strcmp(name, s->name))
  659. continue;
  660. blobmsg_list_for_each(&s->data_blob, var) {
  661. if (type && strcmp(blobmsg_name(var->data), type))
  662. continue;
  663. if (!cs)
  664. cs = blobmsg_open_table(&b, s->name);
  665. blobmsg_add_blob(&b, var->data);
  666. }
  667. vlist_for_each_element(&s->instances, in, node) {
  668. ci = NULL;
  669. if (instance && strcmp(instance, in->name))
  670. continue;
  671. blobmsg_list_for_each(&in->data, var) {
  672. if (type &&
  673. strcmp(blobmsg_name(var->data), type))
  674. continue;
  675. if (!cs)
  676. cs = blobmsg_open_table(&b, s->name);
  677. if (!ci)
  678. ci = blobmsg_open_table(&b, in->name);
  679. blobmsg_add_blob(&b, var->data);
  680. }
  681. if (ci)
  682. blobmsg_close_table(&b, ci);
  683. }
  684. if (cs)
  685. blobmsg_close_table(&b, cs);
  686. }
  687. ubus_send_reply(ctx, req, b.head);
  688. return 0;
  689. }
  690. static int
  691. container_handle_console(struct ubus_context *ctx, struct ubus_object *obj,
  692. struct ubus_request_data *req, const char *method,
  693. struct blob_attr *msg)
  694. {
  695. bool attach = !strcmp(method, "console_attach");
  696. struct blob_attr *tb[__CONTAINER_CONSOLE_MAX];
  697. struct service *s;
  698. struct service_instance *in;
  699. int console_fd = -1;
  700. console_fd = ubus_request_get_caller_fd(req);
  701. if (console_fd < 0)
  702. return UBUS_STATUS_INVALID_ARGUMENT;
  703. if (!msg)
  704. goto err_console_fd;
  705. blobmsg_parse(container_console_policy, __CONTAINER_CONSOLE_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  706. if (!tb[CONTAINER_CONSOLE_NAME])
  707. goto err_console_fd;
  708. s = avl_find_element(&containers, blobmsg_data(tb[CONTAINER_CONSOLE_NAME]), s, avl);
  709. if (!s)
  710. goto err_console_fd;
  711. if (tb[CONTAINER_CONSOLE_INSTANCE]) {
  712. in = vlist_find(&s->instances, blobmsg_data(tb[CONTAINER_CONSOLE_INSTANCE]), in, node);
  713. } else {
  714. /* use first element in instances list */
  715. vlist_for_each_element(&s->instances, in, node)
  716. break;
  717. }
  718. if (!in)
  719. goto err_console_fd;
  720. if (attach) {
  721. if (in->console.fd.fd < 0) {
  722. close(console_fd);
  723. return UBUS_STATUS_NOT_SUPPORTED;
  724. }
  725. /* close and replace existing attached console */
  726. if (in->console_client.fd.fd > -1)
  727. close(in->console_client.fd.fd);
  728. ustream_fd_init(&in->console_client, console_fd);
  729. } else {
  730. ustream_fd_init(&in->console, console_fd);
  731. }
  732. return UBUS_STATUS_OK;
  733. err_console_fd:
  734. close(console_fd);
  735. return UBUS_STATUS_INVALID_ARGUMENT;
  736. }
  737. enum {
  738. SERVICE_WATCHDOG_MODE,
  739. SERVICE_WATCHDOG_TIMEOUT,
  740. SERVICE_WATCHDOG_NAME,
  741. SERVICE_WATCHDOG_INSTANCE,
  742. __SERVICE_WATCHDOG_MAX,
  743. };
  744. static const struct blobmsg_policy service_watchdog_policy[__SERVICE_WATCHDOG_MAX] = {
  745. [SERVICE_WATCHDOG_MODE] = { "mode", BLOBMSG_TYPE_INT32 },
  746. [SERVICE_WATCHDOG_NAME] = { "name", BLOBMSG_TYPE_STRING },
  747. [SERVICE_WATCHDOG_TIMEOUT] = { "timeout", BLOBMSG_TYPE_INT32 },
  748. [SERVICE_WATCHDOG_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
  749. };
  750. static int
  751. service_handle_watchdog(struct ubus_context *ctx, struct ubus_object *obj,
  752. struct ubus_request_data *req, const char *method,
  753. struct blob_attr *msg)
  754. {
  755. struct blob_attr *tb[__SERVICE_WATCHDOG_MAX] = {0};
  756. struct service *s;
  757. struct blob_attr *cur;
  758. struct service_instance *in;
  759. blobmsg_parse(service_watchdog_policy, __SERVICE_WATCHDOG_MAX, tb, blobmsg_data(msg), blobmsg_data_len(msg));
  760. cur = tb[SERVICE_WATCHDOG_NAME];
  761. if (!cur)
  762. return UBUS_STATUS_NOT_FOUND;
  763. s = avl_find_element(&services, blobmsg_data(cur), s, avl);
  764. if (!s)
  765. return UBUS_STATUS_NOT_FOUND;
  766. cur = tb[SERVICE_WATCHDOG_INSTANCE];
  767. if (!cur)
  768. return UBUS_STATUS_NOT_FOUND;
  769. in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
  770. if (!in) {
  771. ERROR("instance %s not found\n", blobmsg_get_string(cur));
  772. return UBUS_STATUS_NOT_FOUND;
  773. }
  774. if (tb[SERVICE_WATCHDOG_MODE])
  775. in->watchdog.mode = blobmsg_get_u32(tb[SERVICE_WATCHDOG_MODE]);
  776. if (tb[SERVICE_WATCHDOG_TIMEOUT])
  777. in->watchdog.freq = blobmsg_get_u32(tb[SERVICE_WATCHDOG_TIMEOUT]);
  778. if (in->watchdog.mode == INSTANCE_WATCHDOG_MODE_DISABLED)
  779. uloop_timeout_cancel(&in->watchdog.timeout);
  780. else
  781. uloop_timeout_set(&in->watchdog.timeout, in->watchdog.freq * 1000);
  782. blob_buf_init(&b, 0);
  783. blobmsg_add_string(&b, "name", blobmsg_get_string(tb[SERVICE_WATCHDOG_NAME]));
  784. blobmsg_add_string(&b, "instance", blobmsg_get_string(tb[SERVICE_WATCHDOG_INSTANCE]));
  785. blobmsg_add_u32(&b, "mode", in->watchdog.mode);
  786. blobmsg_add_u32(&b, "timeout", in->watchdog.freq);
  787. ubus_send_reply(ctx, req, b.head);
  788. return UBUS_STATUS_OK;
  789. }
  790. static struct ubus_method main_object_methods[] = {
  791. UBUS_METHOD("set", service_handle_set, service_set_attrs),
  792. UBUS_METHOD("add", service_handle_set, service_set_attrs),
  793. UBUS_METHOD("list", service_handle_list, service_list_attrs),
  794. UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
  795. UBUS_METHOD("signal", service_handle_signal, service_signal_attrs),
  796. UBUS_METHOD("update_start", service_handle_update, service_attrs),
  797. UBUS_METHOD("update_complete", service_handle_update, service_attrs),
  798. UBUS_METHOD("event", service_handle_event, event_policy),
  799. UBUS_METHOD("validate", service_handle_validate, validate_policy),
  800. UBUS_METHOD("get_data", service_get_data, get_data_policy),
  801. UBUS_METHOD("state", service_handle_state, service_state_attrs),
  802. UBUS_METHOD("watchdog", service_handle_watchdog, service_watchdog_policy),
  803. };
  804. static struct ubus_object_type main_object_type =
  805. UBUS_OBJECT_TYPE("service", main_object_methods);
  806. static struct ubus_object main_object = {
  807. .name = "service",
  808. .type = &main_object_type,
  809. .methods = main_object_methods,
  810. .n_methods = ARRAY_SIZE(main_object_methods),
  811. };
  812. int
  813. service_start_early(char *name, char *cmdline, char *user, char *group)
  814. {
  815. void *instances, *instance, *command, *respawn;
  816. char *t;
  817. blob_buf_init(&b, 0);
  818. blobmsg_add_string(&b, "name", name);
  819. instances = blobmsg_open_table(&b, "instances");
  820. instance = blobmsg_open_table(&b, "instance1");
  821. command = blobmsg_open_array(&b, "command");
  822. t = strtok(cmdline, " ");
  823. while (t) {
  824. blobmsg_add_string(&b, NULL, t);
  825. t = strtok(NULL, " ");
  826. }
  827. blobmsg_close_array(&b, command);
  828. respawn = blobmsg_open_array(&b, "respawn");
  829. blobmsg_add_string(&b, NULL, "3600");
  830. blobmsg_add_string(&b, NULL, "1");
  831. blobmsg_add_string(&b, NULL, "0");
  832. blobmsg_close_array(&b, respawn);
  833. if (user)
  834. blobmsg_add_string(&b, "user", user);
  835. if (group)
  836. blobmsg_add_string(&b, "group", group);
  837. blobmsg_close_table(&b, instance);
  838. blobmsg_close_table(&b, instances);
  839. return service_handle_set(NULL, NULL, NULL, "add", b.head);
  840. }
  841. void service_stopped(struct service *s)
  842. {
  843. if (s->deleted && avl_is_empty(&s->instances.avl)) {
  844. if (s->container) {
  845. service_event("container.stop", s->name, NULL);
  846. avl_delete(&containers, &s->avl);
  847. } else {
  848. service_event("service.stop", s->name, NULL);
  849. avl_delete(&services, &s->avl);
  850. }
  851. trigger_del(s);
  852. service_validate_del(s);
  853. free(s->trigger);
  854. free(s);
  855. }
  856. }
  857. void service_event(const char *type, const char *service, const char *instance)
  858. {
  859. ubus_event_bcast(type, "service", service, "instance", instance);
  860. }
  861. static struct ubus_method container_object_methods[] = {
  862. UBUS_METHOD("set", service_handle_set, service_set_attrs),
  863. UBUS_METHOD("add", service_handle_set, service_set_attrs),
  864. UBUS_METHOD("list", service_handle_list, service_list_attrs),
  865. UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
  866. UBUS_METHOD("state", service_handle_state, service_state_attrs),
  867. UBUS_METHOD_NOARG("get_features", container_handle_features),
  868. UBUS_METHOD("console_set", container_handle_console, container_console_policy),
  869. UBUS_METHOD("console_attach", container_handle_console, container_console_policy),
  870. };
  871. static struct ubus_object_type container_object_type =
  872. UBUS_OBJECT_TYPE("container", container_object_methods);
  873. static struct ubus_object container_object = {
  874. .name = "container",
  875. .type = &container_object_type,
  876. .methods = container_object_methods,
  877. .n_methods = ARRAY_SIZE(container_object_methods),
  878. };
  879. void ubus_init_service(struct ubus_context *_ctx)
  880. {
  881. struct stat statbuf;
  882. ctx = _ctx;
  883. ubus_add_object(ctx, &main_object);
  884. if (!stat("/sbin/ujail", &statbuf))
  885. ubus_add_object(ctx, &container_object);
  886. }