instance.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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/resource.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <net/if.h>
  18. #include <unistd.h>
  19. #include <stdint.h>
  20. #include <fcntl.h>
  21. #include <pwd.h>
  22. #include <libgen.h>
  23. #include <libubox/md5.h>
  24. #include "../procd.h"
  25. #include "service.h"
  26. #include "instance.h"
  27. enum {
  28. INSTANCE_ATTR_COMMAND,
  29. INSTANCE_ATTR_ENV,
  30. INSTANCE_ATTR_DATA,
  31. INSTANCE_ATTR_NETDEV,
  32. INSTANCE_ATTR_FILE,
  33. INSTANCE_ATTR_TRIGGER,
  34. INSTANCE_ATTR_RESPAWN,
  35. INSTANCE_ATTR_NICE,
  36. INSTANCE_ATTR_LIMITS,
  37. INSTANCE_ATTR_WATCH,
  38. INSTANCE_ATTR_ERROR,
  39. INSTANCE_ATTR_USER,
  40. INSTANCE_ATTR_STDOUT,
  41. INSTANCE_ATTR_STDERR,
  42. __INSTANCE_ATTR_MAX
  43. };
  44. static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
  45. [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
  46. [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
  47. [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
  48. [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
  49. [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
  50. [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
  51. [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
  52. [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
  53. [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
  54. [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
  55. [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
  56. [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
  57. [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
  58. [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
  59. };
  60. struct instance_netdev {
  61. struct blobmsg_list_node node;
  62. int ifindex;
  63. };
  64. struct instance_file {
  65. struct blobmsg_list_node node;
  66. uint32_t md5[4];
  67. };
  68. struct rlimit_name {
  69. const char *name;
  70. int resource;
  71. };
  72. static const struct rlimit_name rlimit_names[] = {
  73. { "as", RLIMIT_AS },
  74. { "core", RLIMIT_CORE },
  75. { "cpu", RLIMIT_CPU },
  76. { "data", RLIMIT_DATA },
  77. { "fsize", RLIMIT_FSIZE },
  78. { "memlock", RLIMIT_MEMLOCK },
  79. { "msgqueue", RLIMIT_MSGQUEUE },
  80. { "nice", RLIMIT_NICE },
  81. { "nofile", RLIMIT_NOFILE },
  82. { "nproc", RLIMIT_NPROC },
  83. { "rss", RLIMIT_RSS },
  84. { "rtprio", RLIMIT_RTPRIO },
  85. { "sigpending", RLIMIT_SIGPENDING },
  86. { "stack", RLIMIT_STACK },
  87. { NULL, 0 }
  88. };
  89. static void closefd(int fd)
  90. {
  91. if (fd > STDERR_FILENO)
  92. close(fd);
  93. }
  94. static void
  95. instance_limits(const char *limit, const char *value)
  96. {
  97. int i;
  98. struct rlimit rlim;
  99. unsigned long cur, max;
  100. for (i = 0; rlimit_names[i].name != NULL; i++) {
  101. if (strcmp(rlimit_names[i].name, limit))
  102. continue;
  103. if (!strcmp(value, "unlimited")) {
  104. rlim.rlim_cur = RLIM_INFINITY;
  105. rlim.rlim_max = RLIM_INFINITY;
  106. } else {
  107. if (getrlimit(rlimit_names[i].resource, &rlim))
  108. return;
  109. cur = rlim.rlim_cur;
  110. max = rlim.rlim_max;
  111. if (sscanf(value, "%lu %lu", &cur, &max) < 1)
  112. return;
  113. rlim.rlim_cur = cur;
  114. rlim.rlim_max = max;
  115. }
  116. setrlimit(rlimit_names[i].resource, &rlim);
  117. return;
  118. }
  119. }
  120. static void
  121. instance_run(struct service_instance *in, int stdout, int stderr)
  122. {
  123. struct blobmsg_list_node *var;
  124. struct blob_attr *cur;
  125. char **argv;
  126. int argc = 1; /* NULL terminated */
  127. int rem, stdin;
  128. if (in->nice)
  129. setpriority(PRIO_PROCESS, 0, in->nice);
  130. blobmsg_for_each_attr(cur, in->command, rem)
  131. argc++;
  132. blobmsg_list_for_each(&in->env, var)
  133. setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
  134. blobmsg_list_for_each(&in->limits, var)
  135. instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
  136. argv = alloca(sizeof(char *) * argc);
  137. argc = 0;
  138. blobmsg_for_each_attr(cur, in->command, rem)
  139. argv[argc++] = blobmsg_data(cur);
  140. argv[argc] = NULL;
  141. stdin = open("/dev/null", O_RDONLY);
  142. if (stdout == -1)
  143. stdout = open("/dev/null", O_WRONLY);
  144. if (stderr == -1)
  145. stderr = open("/dev/null", O_WRONLY);
  146. if (stdin > -1) {
  147. dup2(stdin, STDIN_FILENO);
  148. closefd(stdin);
  149. }
  150. if (stdout > -1) {
  151. dup2(stdout, STDOUT_FILENO);
  152. closefd(stdout);
  153. }
  154. if (stderr > -1) {
  155. dup2(stderr, STDERR_FILENO);
  156. closefd(stderr);
  157. }
  158. if (in->uid || in->gid) {
  159. setuid(in->uid);
  160. setgid(in->gid);
  161. }
  162. execvp(argv[0], argv);
  163. exit(127);
  164. }
  165. void
  166. instance_start(struct service_instance *in)
  167. {
  168. int pid;
  169. int opipe[2] = { -1, -1 };
  170. int epipe[2] = { -1, -1 };
  171. if (!avl_is_empty(&in->errors.avl)) {
  172. LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
  173. return;
  174. }
  175. if (in->proc.pending)
  176. return;
  177. if (in->stdout.fd.fd > -2) {
  178. if (pipe(opipe)) {
  179. ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
  180. opipe[0] = opipe[1] = -1;
  181. }
  182. }
  183. if (in->stderr.fd.fd > -2) {
  184. if (pipe(epipe)) {
  185. ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
  186. epipe[0] = epipe[1] = -1;
  187. }
  188. }
  189. in->restart = false;
  190. in->halt = !in->respawn;
  191. if (!in->valid)
  192. return;
  193. pid = fork();
  194. if (pid < 0)
  195. return;
  196. if (!pid) {
  197. uloop_done();
  198. closefd(opipe[0]);
  199. closefd(epipe[0]);
  200. instance_run(in, opipe[1], epipe[1]);
  201. return;
  202. }
  203. DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
  204. in->proc.pid = pid;
  205. clock_gettime(CLOCK_MONOTONIC, &in->start);
  206. uloop_process_add(&in->proc);
  207. if (opipe[0] > -1) {
  208. ustream_fd_init(&in->stdout, opipe[0]);
  209. closefd(opipe[1]);
  210. }
  211. if (epipe[0] > -1) {
  212. ustream_fd_init(&in->stderr, epipe[0]);
  213. closefd(epipe[1]);
  214. }
  215. service_event("instance.start", in->srv->name, in->name);
  216. }
  217. static void
  218. instance_stdio(struct ustream *s, int prio, struct service_instance *in)
  219. {
  220. char *newline, *str, *arg0, ident[32];
  221. int len;
  222. do {
  223. str = ustream_get_read_buf(s, NULL);
  224. if (!str)
  225. break;
  226. newline = strchr(str, '\n');
  227. if (!newline)
  228. break;
  229. *newline = 0;
  230. len = newline + 1 - str;
  231. arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
  232. snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
  233. ulog_open(ULOG_STDIO|ULOG_SYSLOG, LOG_DAEMON, ident);
  234. ulog(prio, "%s\n", str);
  235. ulog_open(ULOG_STDIO|ULOG_SYSLOG, LOG_DAEMON, "procd");
  236. ustream_consume(s, len);
  237. } while (1);
  238. }
  239. static void
  240. instance_stdout(struct ustream *s, int bytes)
  241. {
  242. instance_stdio(s, LOG_INFO,
  243. container_of(s, struct service_instance, stdout.stream));
  244. }
  245. static void
  246. instance_stderr(struct ustream *s, int bytes)
  247. {
  248. instance_stdio(s, LOG_ERR,
  249. container_of(s, struct service_instance, stderr.stream));
  250. }
  251. static void
  252. instance_timeout(struct uloop_timeout *t)
  253. {
  254. struct service_instance *in;
  255. in = container_of(t, struct service_instance, timeout);
  256. if (!in->halt && (in->restart || in->respawn))
  257. instance_start(in);
  258. }
  259. static void
  260. instance_exit(struct uloop_process *p, int ret)
  261. {
  262. struct service_instance *in;
  263. struct timespec tp;
  264. long runtime;
  265. in = container_of(p, struct service_instance, proc);
  266. clock_gettime(CLOCK_MONOTONIC, &tp);
  267. runtime = tp.tv_sec - in->start.tv_sec;
  268. DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
  269. if (upgrade_running)
  270. return;
  271. uloop_timeout_cancel(&in->timeout);
  272. if (in->halt) {
  273. /* no action */
  274. } else if (in->restart) {
  275. instance_start(in);
  276. } else if (in->respawn) {
  277. if (runtime < in->respawn_threshold)
  278. in->respawn_count++;
  279. else
  280. in->respawn_count = 0;
  281. if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
  282. LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
  283. in->srv->name, in->name, in->respawn_count, runtime);
  284. in->restart = in->respawn = 0;
  285. in->halt = 1;
  286. } else {
  287. uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
  288. }
  289. }
  290. service_event("instance.stop", in->srv->name, in->name);
  291. }
  292. void
  293. instance_stop(struct service_instance *in)
  294. {
  295. if (!in->proc.pending)
  296. return;
  297. in->halt = true;
  298. in->restart = in->respawn = false;
  299. kill(in->proc.pid, SIGTERM);
  300. }
  301. static void
  302. instance_restart(struct service_instance *in)
  303. {
  304. if (!in->proc.pending)
  305. return;
  306. in->halt = false;
  307. in->restart = true;
  308. kill(in->proc.pid, SIGTERM);
  309. }
  310. static bool
  311. instance_config_changed(struct service_instance *in, struct service_instance *in_new)
  312. {
  313. if (!in->valid)
  314. return true;
  315. if (!blob_attr_equal(in->command, in_new->command))
  316. return true;
  317. if (!blobmsg_list_equal(&in->env, &in_new->env))
  318. return true;
  319. if (!blobmsg_list_equal(&in->data, &in_new->data))
  320. return true;
  321. if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
  322. return true;
  323. if (!blobmsg_list_equal(&in->file, &in_new->file))
  324. return true;
  325. if (in->nice != in_new->nice)
  326. return true;
  327. if (in->uid != in_new->uid)
  328. return true;
  329. if (in->gid != in_new->gid)
  330. return true;
  331. if (!blobmsg_list_equal(&in->limits, &in_new->limits))
  332. return true;
  333. if (!blobmsg_list_equal(&in->errors, &in_new->errors))
  334. return true;
  335. return false;
  336. }
  337. static bool
  338. instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
  339. {
  340. struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
  341. struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
  342. return n1->ifindex == n2->ifindex;
  343. }
  344. static void
  345. instance_netdev_update(struct blobmsg_list_node *l)
  346. {
  347. struct instance_netdev *n = container_of(l, struct instance_netdev, node);
  348. n->ifindex = if_nametoindex(n->node.avl.key);
  349. }
  350. static bool
  351. instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
  352. {
  353. struct instance_file *f1 = container_of(l1, struct instance_file, node);
  354. struct instance_file *f2 = container_of(l2, struct instance_file, node);
  355. return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
  356. }
  357. static void
  358. instance_file_update(struct blobmsg_list_node *l)
  359. {
  360. struct instance_file *f = container_of(l, struct instance_file, node);
  361. md5_ctx_t md5;
  362. char buf[256];
  363. int len, fd;
  364. memset(f->md5, 0, sizeof(f->md5));
  365. fd = open(l->avl.key, O_RDONLY);
  366. if (fd < 0)
  367. return;
  368. md5_begin(&md5);
  369. do {
  370. len = read(fd, buf, sizeof(buf));
  371. if (len < 0) {
  372. if (errno == EINTR)
  373. continue;
  374. break;
  375. }
  376. if (!len)
  377. break;
  378. md5_hash(buf, len, &md5);
  379. } while(1);
  380. md5_end(f->md5, &md5);
  381. close(fd);
  382. }
  383. static void
  384. instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
  385. {
  386. if (!cur)
  387. return;
  388. blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
  389. }
  390. static bool
  391. instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
  392. {
  393. struct blobmsg_list_node *node;
  394. if (!cur)
  395. return true;
  396. if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
  397. return false;
  398. blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
  399. if (cb) {
  400. blobmsg_list_for_each(l, node)
  401. cb(node);
  402. }
  403. return true;
  404. }
  405. static bool
  406. instance_config_parse(struct service_instance *in)
  407. {
  408. struct blob_attr *tb[__INSTANCE_ATTR_MAX];
  409. struct blob_attr *cur, *cur2;
  410. int argc = 0;
  411. int rem;
  412. blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
  413. blobmsg_data(in->config), blobmsg_data_len(in->config));
  414. cur = tb[INSTANCE_ATTR_COMMAND];
  415. if (!cur)
  416. return false;
  417. if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
  418. return false;
  419. blobmsg_for_each_attr(cur2, cur, rem) {
  420. argc++;
  421. break;
  422. }
  423. if (!argc)
  424. return false;
  425. in->command = cur;
  426. if (tb[INSTANCE_ATTR_RESPAWN]) {
  427. int i = 0;
  428. uint32_t vals[3] = { 3600, 5, 5};
  429. blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
  430. if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
  431. continue;
  432. vals[i] = atoi(blobmsg_get_string(cur2));
  433. i++;
  434. }
  435. in->respawn = true;
  436. in->respawn_count = 0;
  437. in->respawn_threshold = vals[0];
  438. in->respawn_timeout = vals[1];
  439. in->respawn_retry = vals[2];
  440. }
  441. if (tb[INSTANCE_ATTR_TRIGGER]) {
  442. in->trigger = tb[INSTANCE_ATTR_TRIGGER];
  443. trigger_add(in->trigger, in);
  444. }
  445. if (tb[INSTANCE_ATTR_WATCH]) {
  446. blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
  447. if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
  448. continue;
  449. DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
  450. watch_add(blobmsg_get_string(cur2), in);
  451. }
  452. }
  453. if ((cur = tb[INSTANCE_ATTR_NICE])) {
  454. in->nice = (int8_t) blobmsg_get_u32(cur);
  455. if (in->nice < -20 || in->nice > 20)
  456. return false;
  457. }
  458. if (tb[INSTANCE_ATTR_USER]) {
  459. struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
  460. if (p) {
  461. in->uid = p->pw_uid;
  462. in->gid = p->pw_gid;
  463. }
  464. }
  465. if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
  466. in->stdout.fd.fd = -1;
  467. if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
  468. in->stderr.fd.fd = -1;
  469. instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
  470. if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
  471. return false;
  472. if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
  473. return false;
  474. if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
  475. return false;
  476. if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
  477. return false;
  478. if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
  479. return false;
  480. return true;
  481. }
  482. static void
  483. instance_config_cleanup(struct service_instance *in)
  484. {
  485. blobmsg_list_free(&in->env);
  486. blobmsg_list_free(&in->data);
  487. blobmsg_list_free(&in->netdev);
  488. blobmsg_list_free(&in->file);
  489. blobmsg_list_free(&in->limits);
  490. blobmsg_list_free(&in->errors);
  491. }
  492. static void
  493. instance_config_move(struct service_instance *in, struct service_instance *in_src)
  494. {
  495. instance_config_cleanup(in);
  496. blobmsg_list_move(&in->env, &in_src->env);
  497. blobmsg_list_move(&in->data, &in_src->data);
  498. blobmsg_list_move(&in->netdev, &in_src->netdev);
  499. blobmsg_list_move(&in->file, &in_src->file);
  500. blobmsg_list_move(&in->limits, &in_src->limits);
  501. blobmsg_list_move(&in->errors, &in_src->errors);
  502. in->trigger = in_src->trigger;
  503. in->command = in_src->command;
  504. in->name = in_src->name;
  505. in->node.avl.key = in_src->node.avl.key;
  506. free(in->config);
  507. in->config = in_src->config;
  508. in_src->config = NULL;
  509. }
  510. bool
  511. instance_update(struct service_instance *in, struct service_instance *in_new)
  512. {
  513. bool changed = instance_config_changed(in, in_new);
  514. bool running = in->proc.pending;
  515. if (!changed && running)
  516. return false;
  517. if (!running) {
  518. if (changed)
  519. instance_config_move(in, in_new);
  520. instance_start(in);
  521. } else {
  522. instance_restart(in);
  523. instance_config_move(in, in_new);
  524. /* restart happens in the child callback handler */
  525. }
  526. return true;
  527. }
  528. void
  529. instance_free(struct service_instance *in)
  530. {
  531. if (in->stdout.fd.fd > -1) {
  532. ustream_free(&in->stdout.stream);
  533. close(in->stdout.fd.fd);
  534. }
  535. if (in->stderr.fd.fd > -1) {
  536. ustream_free(&in->stderr.stream);
  537. close(in->stderr.fd.fd);
  538. }
  539. uloop_process_delete(&in->proc);
  540. uloop_timeout_cancel(&in->timeout);
  541. trigger_del(in);
  542. watch_del(in);
  543. instance_config_cleanup(in);
  544. free(in->config);
  545. free(in);
  546. }
  547. void
  548. instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
  549. {
  550. config = blob_memdup(config);
  551. in->srv = s;
  552. in->name = blobmsg_name(config);
  553. in->config = config;
  554. in->timeout.cb = instance_timeout;
  555. in->proc.cb = instance_exit;
  556. in->stdout.fd.fd = -2;
  557. in->stdout.stream.string_data = true;
  558. in->stdout.stream.notify_read = instance_stdout;
  559. in->stderr.fd.fd = -2;
  560. in->stderr.stream.string_data = true;
  561. in->stderr.stream.notify_read = instance_stderr;
  562. blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
  563. blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
  564. blobmsg_list_simple_init(&in->env);
  565. blobmsg_list_simple_init(&in->data);
  566. blobmsg_list_simple_init(&in->limits);
  567. blobmsg_list_simple_init(&in->errors);
  568. in->valid = instance_config_parse(in);
  569. }
  570. void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
  571. {
  572. void *i;
  573. i = blobmsg_open_table(b, in->name);
  574. blobmsg_add_u8(b, "running", in->proc.pending);
  575. if (in->proc.pending)
  576. blobmsg_add_u32(b, "pid", in->proc.pid);
  577. blobmsg_add_blob(b, in->command);
  578. if (!avl_is_empty(&in->errors.avl)) {
  579. struct blobmsg_list_node *var;
  580. void *e = blobmsg_open_array(b, "errors");
  581. blobmsg_list_for_each(&in->errors, var)
  582. blobmsg_add_string(b, NULL, blobmsg_data(var->data));
  583. blobmsg_close_table(b, e);
  584. }
  585. if (!avl_is_empty(&in->env.avl)) {
  586. struct blobmsg_list_node *var;
  587. void *e = blobmsg_open_table(b, "env");
  588. blobmsg_list_for_each(&in->env, var)
  589. blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
  590. blobmsg_close_table(b, e);
  591. }
  592. if (!avl_is_empty(&in->data.avl)) {
  593. struct blobmsg_list_node *var;
  594. void *e = blobmsg_open_table(b, "data");
  595. blobmsg_list_for_each(&in->data, var)
  596. blobmsg_add_blob(b, var->data);
  597. blobmsg_close_table(b, e);
  598. }
  599. if (!avl_is_empty(&in->limits.avl)) {
  600. struct blobmsg_list_node *var;
  601. void *e = blobmsg_open_table(b, "limits");
  602. blobmsg_list_for_each(&in->limits, var)
  603. blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
  604. blobmsg_close_table(b, e);
  605. }
  606. if (in->respawn) {
  607. void *r = blobmsg_open_table(b, "respawn");
  608. blobmsg_add_u32(b, "timeout", in->respawn_timeout);
  609. blobmsg_add_u32(b, "threshold", in->respawn_threshold);
  610. blobmsg_add_u32(b, "retry", in->respawn_retry);
  611. blobmsg_close_table(b, r);
  612. }
  613. if (verbose && in->trigger)
  614. blobmsg_add_blob(b, in->trigger);
  615. blobmsg_close_table(b, i);
  616. }