instance.c 22 KB

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