system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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/utsname.h>
  15. #ifdef linux
  16. #include <sys/sysinfo.h>
  17. #endif
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include <sys/reboot.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <signal.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <json-c/json_tokener.h>
  27. #include <libubox/blobmsg_json.h>
  28. #include <libubox/uloop.h>
  29. #include "procd.h"
  30. #include "sysupgrade.h"
  31. #include "watchdog.h"
  32. static struct blob_buf b;
  33. static int notify;
  34. static struct ubus_context *_ctx;
  35. static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
  36. struct ubus_request_data *req, const char *method,
  37. struct blob_attr *msg)
  38. {
  39. void *c;
  40. char line[256];
  41. char *key, *val, *next;
  42. struct utsname utsname;
  43. FILE *f;
  44. blob_buf_init(&b, 0);
  45. if (uname(&utsname) >= 0)
  46. {
  47. blobmsg_add_string(&b, "kernel", utsname.release);
  48. blobmsg_add_string(&b, "hostname", utsname.nodename);
  49. }
  50. if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
  51. {
  52. while(fgets(line, sizeof(line), f))
  53. {
  54. key = strtok(line, "\t:");
  55. val = strtok(NULL, "\t\n");
  56. if (!key || !val)
  57. continue;
  58. if (!strcasecmp(key, "system type") ||
  59. !strcasecmp(key, "processor") ||
  60. !strcasecmp(key, "cpu") ||
  61. !strcasecmp(key, "model name"))
  62. {
  63. strtoul(val + 2, &key, 0);
  64. if (key == (val + 2) || *key != 0)
  65. {
  66. blobmsg_add_string(&b, "system", val + 2);
  67. break;
  68. }
  69. }
  70. }
  71. fclose(f);
  72. }
  73. if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
  74. (f = fopen("/proc/device-tree/model", "r")) != NULL)
  75. {
  76. if (fgets(line, sizeof(line), f))
  77. {
  78. val = strtok(line, "\t\n");
  79. if (val)
  80. blobmsg_add_string(&b, "model", val);
  81. }
  82. fclose(f);
  83. }
  84. else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
  85. {
  86. while(fgets(line, sizeof(line), f))
  87. {
  88. key = strtok(line, "\t:");
  89. val = strtok(NULL, "\t\n");
  90. if (!key || !val)
  91. continue;
  92. if (!strcasecmp(key, "machine") ||
  93. !strcasecmp(key, "hardware"))
  94. {
  95. blobmsg_add_string(&b, "model", val + 2);
  96. break;
  97. }
  98. }
  99. fclose(f);
  100. }
  101. if ((f = fopen("/tmp/sysinfo/board_name", "r")) != NULL)
  102. {
  103. if (fgets(line, sizeof(line), f))
  104. {
  105. val = strtok(line, "\t\n");
  106. if (val)
  107. blobmsg_add_string(&b, "board_name", val);
  108. }
  109. fclose(f);
  110. }
  111. else if ((f = fopen("/proc/device-tree/compatible", "r")) != NULL)
  112. {
  113. if (fgets(line, sizeof(line), f))
  114. {
  115. val = strtok(line, "\t\n");
  116. if (val)
  117. {
  118. next = val;
  119. while ((next = strchr(next, ',')) != NULL)
  120. {
  121. *next = '-';
  122. next++;
  123. }
  124. blobmsg_add_string(&b, "board_name", val);
  125. }
  126. }
  127. fclose(f);
  128. }
  129. if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
  130. {
  131. c = blobmsg_open_table(&b, "release");
  132. while (fgets(line, sizeof(line), f))
  133. {
  134. char *dest;
  135. char ch;
  136. key = line;
  137. val = strchr(line, '=');
  138. if (!val)
  139. continue;
  140. *(val++) = 0;
  141. if (!strcasecmp(key, "DISTRIB_ID"))
  142. key = "distribution";
  143. else if (!strcasecmp(key, "DISTRIB_RELEASE"))
  144. key = "version";
  145. else if (!strcasecmp(key, "DISTRIB_REVISION"))
  146. key = "revision";
  147. else if (!strcasecmp(key, "DISTRIB_CODENAME"))
  148. key = "codename";
  149. else if (!strcasecmp(key, "DISTRIB_TARGET"))
  150. key = "target";
  151. else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
  152. key = "description";
  153. else
  154. continue;
  155. dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
  156. if (!dest) {
  157. ERROR("Failed to allocate blob.\n");
  158. continue;
  159. }
  160. while (val && (ch = *(val++)) != 0) {
  161. switch (ch) {
  162. case '\'':
  163. case '"':
  164. next = strchr(val, ch);
  165. if (next)
  166. *next = 0;
  167. strcpy(dest, val);
  168. if (next)
  169. val = next + 1;
  170. dest += strlen(dest);
  171. break;
  172. case '\\':
  173. *(dest++) = *(val++);
  174. break;
  175. }
  176. }
  177. blobmsg_add_string_buffer(&b);
  178. }
  179. blobmsg_close_array(&b, c);
  180. fclose(f);
  181. }
  182. ubus_send_reply(ctx, req, b.head);
  183. return UBUS_STATUS_OK;
  184. }
  185. static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
  186. struct ubus_request_data *req, const char *method,
  187. struct blob_attr *msg)
  188. {
  189. time_t now;
  190. struct tm *tm;
  191. #ifdef linux
  192. struct sysinfo info;
  193. void *c;
  194. char line[256];
  195. char *key, *val;
  196. unsigned long long available, cached;
  197. FILE *f;
  198. if (sysinfo(&info))
  199. return UBUS_STATUS_UNKNOWN_ERROR;
  200. if ((f = fopen("/proc/meminfo", "r")) == NULL)
  201. return UBUS_STATUS_UNKNOWN_ERROR;
  202. /* if linux < 3.14 MemAvailable is not in meminfo */
  203. available = 0;
  204. cached = 0;
  205. while (fgets(line, sizeof(line), f))
  206. {
  207. key = strtok(line, " :");
  208. val = strtok(NULL, " ");
  209. if (!key || !val)
  210. continue;
  211. if (!strcasecmp(key, "MemAvailable"))
  212. available = 1024 * atoll(val);
  213. else if (!strcasecmp(key, "Cached"))
  214. cached = 1024 * atoll(val);
  215. }
  216. fclose(f);
  217. #endif
  218. now = time(NULL);
  219. if (!(tm = localtime(&now)))
  220. return UBUS_STATUS_UNKNOWN_ERROR;
  221. blob_buf_init(&b, 0);
  222. blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff);
  223. #ifdef linux
  224. blobmsg_add_u32(&b, "uptime", info.uptime);
  225. c = blobmsg_open_array(&b, "load");
  226. blobmsg_add_u32(&b, NULL, info.loads[0]);
  227. blobmsg_add_u32(&b, NULL, info.loads[1]);
  228. blobmsg_add_u32(&b, NULL, info.loads[2]);
  229. blobmsg_close_array(&b, c);
  230. c = blobmsg_open_table(&b, "memory");
  231. blobmsg_add_u64(&b, "total",
  232. (uint64_t)info.mem_unit * (uint64_t)info.totalram);
  233. blobmsg_add_u64(&b, "free",
  234. (uint64_t)info.mem_unit * (uint64_t)info.freeram);
  235. blobmsg_add_u64(&b, "shared",
  236. (uint64_t)info.mem_unit * (uint64_t)info.sharedram);
  237. blobmsg_add_u64(&b, "buffered",
  238. (uint64_t)info.mem_unit * (uint64_t)info.bufferram);
  239. blobmsg_add_u64(&b, "available", available);
  240. blobmsg_add_u64(&b, "cached", cached);
  241. blobmsg_close_table(&b, c);
  242. c = blobmsg_open_table(&b, "swap");
  243. blobmsg_add_u64(&b, "total",
  244. (uint64_t)info.mem_unit * (uint64_t)info.totalswap);
  245. blobmsg_add_u64(&b, "free",
  246. (uint64_t)info.mem_unit * (uint64_t)info.freeswap);
  247. blobmsg_close_table(&b, c);
  248. #endif
  249. ubus_send_reply(ctx, req, b.head);
  250. return UBUS_STATUS_OK;
  251. }
  252. static int system_reboot(struct ubus_context *ctx, struct ubus_object *obj,
  253. struct ubus_request_data *req, const char *method,
  254. struct blob_attr *msg)
  255. {
  256. procd_shutdown(RB_AUTOBOOT);
  257. return 0;
  258. }
  259. enum {
  260. WDT_FREQUENCY,
  261. WDT_TIMEOUT,
  262. WDT_MAGICCLOSE,
  263. WDT_STOP,
  264. __WDT_MAX
  265. };
  266. static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
  267. [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
  268. [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
  269. [WDT_MAGICCLOSE] = { .name = "magicclose", .type = BLOBMSG_TYPE_BOOL },
  270. [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
  271. };
  272. static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
  273. struct ubus_request_data *req, const char *method,
  274. struct blob_attr *msg)
  275. {
  276. struct blob_attr *tb[__WDT_MAX];
  277. const char *status;
  278. if (!msg)
  279. return UBUS_STATUS_INVALID_ARGUMENT;
  280. blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
  281. if (tb[WDT_FREQUENCY]) {
  282. unsigned int timeout = tb[WDT_TIMEOUT] ? blobmsg_get_u32(tb[WDT_TIMEOUT]) :
  283. watchdog_timeout(0);
  284. unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
  285. if (freq) {
  286. if (freq > timeout / 2)
  287. freq = timeout / 2;
  288. watchdog_frequency(freq);
  289. }
  290. }
  291. if (tb[WDT_TIMEOUT]) {
  292. unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
  293. unsigned int frequency = watchdog_frequency(0);
  294. if (timeout <= frequency)
  295. timeout = frequency * 2;
  296. watchdog_timeout(timeout);
  297. }
  298. if (tb[WDT_MAGICCLOSE])
  299. watchdog_set_magicclose(blobmsg_get_bool(tb[WDT_MAGICCLOSE]));
  300. if (tb[WDT_STOP])
  301. watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
  302. if (watchdog_fd() == NULL)
  303. status = "offline";
  304. else if (watchdog_get_stopped())
  305. status = "stopped";
  306. else
  307. status = "running";
  308. blob_buf_init(&b, 0);
  309. blobmsg_add_string(&b, "status", status);
  310. blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
  311. blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
  312. blobmsg_add_u8(&b, "magicclose", watchdog_get_magicclose());
  313. ubus_send_reply(ctx, req, b.head);
  314. return 0;
  315. }
  316. enum {
  317. SIGNAL_PID,
  318. SIGNAL_NUM,
  319. __SIGNAL_MAX
  320. };
  321. static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
  322. [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
  323. [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
  324. };
  325. static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
  326. struct ubus_request_data *req, const char *method,
  327. struct blob_attr *msg)
  328. {
  329. struct blob_attr *tb[__SIGNAL_MAX];
  330. if (!msg)
  331. return UBUS_STATUS_INVALID_ARGUMENT;
  332. blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
  333. if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
  334. return UBUS_STATUS_INVALID_ARGUMENT;
  335. kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
  336. return 0;
  337. }
  338. /**
  339. * validate_firmware_image_call - perform validation & store result in global b
  340. *
  341. * @file: firmware image path
  342. */
  343. static int validate_firmware_image_call(const char *file)
  344. {
  345. const char *path = "/usr/libexec/validate_firmware_image";
  346. json_object *jsobj = NULL;
  347. json_tokener *tok;
  348. char buf[64];
  349. ssize_t len;
  350. int fds[2];
  351. int err;
  352. int fd;
  353. if (pipe(fds))
  354. return -errno;
  355. switch (fork()) {
  356. case -1:
  357. close(fds[0]);
  358. close(fds[1]);
  359. return -errno;
  360. case 0:
  361. /* Set stdin & stderr to /dev/null */
  362. fd = open("/dev/null", O_RDWR);
  363. if (fd >= 0) {
  364. dup2(fd, 0);
  365. dup2(fd, 2);
  366. close(fd);
  367. }
  368. /* Set stdout to the shared pipe */
  369. dup2(fds[1], 1);
  370. close(fds[0]);
  371. close(fds[1]);
  372. execl(path, path, file, NULL);
  373. exit(errno);
  374. }
  375. /* Parent process */
  376. close(fds[1]);
  377. tok = json_tokener_new();
  378. if (!tok) {
  379. close(fds[0]);
  380. return -ENOMEM;
  381. }
  382. blob_buf_init(&b, 0);
  383. while ((len = read(fds[0], buf, sizeof(buf)))) {
  384. jsobj = json_tokener_parse_ex(tok, buf, len);
  385. if (json_tokener_get_error(tok) == json_tokener_success)
  386. break;
  387. else if (json_tokener_get_error(tok) == json_tokener_continue)
  388. continue;
  389. else
  390. fprintf(stderr, "Failed to parse JSON: %d\n",
  391. json_tokener_get_error(tok));
  392. }
  393. close(fds[0]);
  394. err = -ENOENT;
  395. if (jsobj) {
  396. if (json_object_get_type(jsobj) == json_type_object) {
  397. blobmsg_add_object(&b, jsobj);
  398. err = 0;
  399. }
  400. json_object_put(jsobj);
  401. }
  402. json_tokener_free(tok);
  403. return err;
  404. }
  405. enum {
  406. VALIDATE_FIRMWARE_IMAGE_PATH,
  407. __VALIDATE_FIRMWARE_IMAGE_MAX,
  408. };
  409. static const struct blobmsg_policy validate_firmware_image_policy[__VALIDATE_FIRMWARE_IMAGE_MAX] = {
  410. [VALIDATE_FIRMWARE_IMAGE_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
  411. };
  412. static int validate_firmware_image(struct ubus_context *ctx,
  413. struct ubus_object *obj,
  414. struct ubus_request_data *req,
  415. const char *method, struct blob_attr *msg)
  416. {
  417. struct blob_attr *tb[__VALIDATE_FIRMWARE_IMAGE_MAX];
  418. if (!msg)
  419. return UBUS_STATUS_INVALID_ARGUMENT;
  420. blobmsg_parse(validate_firmware_image_policy, __VALIDATE_FIRMWARE_IMAGE_MAX, tb, blob_data(msg), blob_len(msg));
  421. if (!tb[VALIDATE_FIRMWARE_IMAGE_PATH])
  422. return UBUS_STATUS_INVALID_ARGUMENT;
  423. if (validate_firmware_image_call(blobmsg_get_string(tb[VALIDATE_FIRMWARE_IMAGE_PATH])))
  424. return UBUS_STATUS_UNKNOWN_ERROR;
  425. ubus_send_reply(ctx, req, b.head);
  426. return UBUS_STATUS_OK;
  427. }
  428. enum {
  429. SYSUPGRADE_PATH,
  430. SYSUPGRADE_FORCE,
  431. SYSUPGRADE_BACKUP,
  432. SYSUPGRADE_PREFIX,
  433. SYSUPGRADE_COMMAND,
  434. SYSUPGRADE_OPTIONS,
  435. __SYSUPGRADE_MAX
  436. };
  437. static const struct blobmsg_policy sysupgrade_policy[__SYSUPGRADE_MAX] = {
  438. [SYSUPGRADE_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
  439. [SYSUPGRADE_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_BOOL },
  440. [SYSUPGRADE_BACKUP] = { .name = "backup", .type = BLOBMSG_TYPE_STRING },
  441. [SYSUPGRADE_PREFIX] = { .name = "prefix", .type = BLOBMSG_TYPE_STRING },
  442. [SYSUPGRADE_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
  443. [SYSUPGRADE_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_TABLE },
  444. };
  445. static void sysupgrade_error(struct ubus_context *ctx,
  446. struct ubus_request_data *req,
  447. const char *message)
  448. {
  449. void *c;
  450. blob_buf_init(&b, 0);
  451. c = blobmsg_open_table(&b, "error");
  452. blobmsg_add_string(&b, "message", message);
  453. blobmsg_close_table(&b, c);
  454. ubus_send_reply(ctx, req, b.head);
  455. }
  456. static int sysupgrade(struct ubus_context *ctx, struct ubus_object *obj,
  457. struct ubus_request_data *req, const char *method,
  458. struct blob_attr *msg)
  459. {
  460. enum {
  461. VALIDATION_VALID,
  462. VALIDATION_FORCEABLE,
  463. VALIDATION_ALLOW_BACKUP,
  464. __VALIDATION_MAX
  465. };
  466. static const struct blobmsg_policy validation_policy[__VALIDATION_MAX] = {
  467. [VALIDATION_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_BOOL },
  468. [VALIDATION_FORCEABLE] = { .name = "forceable", .type = BLOBMSG_TYPE_BOOL },
  469. [VALIDATION_ALLOW_BACKUP] = { .name = "allow_backup", .type = BLOBMSG_TYPE_BOOL },
  470. };
  471. struct blob_attr *validation[__VALIDATION_MAX];
  472. struct blob_attr *tb[__SYSUPGRADE_MAX];
  473. bool valid, forceable, allow_backup;
  474. if (!msg)
  475. return UBUS_STATUS_INVALID_ARGUMENT;
  476. blobmsg_parse(sysupgrade_policy, __SYSUPGRADE_MAX, tb, blob_data(msg), blob_len(msg));
  477. if (!tb[SYSUPGRADE_PATH] || !tb[SYSUPGRADE_PREFIX])
  478. return UBUS_STATUS_INVALID_ARGUMENT;
  479. if (validate_firmware_image_call(blobmsg_get_string(tb[SYSUPGRADE_PATH]))) {
  480. sysupgrade_error(ctx, req, "Firmware image couldn't be validated");
  481. return UBUS_STATUS_UNKNOWN_ERROR;
  482. }
  483. blobmsg_parse(validation_policy, __VALIDATION_MAX, validation, blob_data(b.head), blob_len(b.head));
  484. valid = validation[VALIDATION_VALID] && blobmsg_get_bool(validation[VALIDATION_VALID]);
  485. forceable = validation[VALIDATION_FORCEABLE] && blobmsg_get_bool(validation[VALIDATION_FORCEABLE]);
  486. allow_backup = validation[VALIDATION_ALLOW_BACKUP] && blobmsg_get_bool(validation[VALIDATION_ALLOW_BACKUP]);
  487. if (!valid) {
  488. if (!forceable) {
  489. sysupgrade_error(ctx, req, "Firmware image is broken and cannot be installed");
  490. return UBUS_STATUS_NOT_SUPPORTED;
  491. } else if (!tb[SYSUPGRADE_FORCE] || !blobmsg_get_bool(tb[SYSUPGRADE_FORCE])) {
  492. sysupgrade_error(ctx, req, "Firmware image is invalid");
  493. return UBUS_STATUS_NOT_SUPPORTED;
  494. }
  495. } else if (!allow_backup && tb[SYSUPGRADE_BACKUP]) {
  496. sysupgrade_error(ctx, req, "Firmware image doesn't allow preserving a backup");
  497. return UBUS_STATUS_NOT_SUPPORTED;
  498. }
  499. sysupgrade_exec_upgraded(blobmsg_get_string(tb[SYSUPGRADE_PREFIX]),
  500. blobmsg_get_string(tb[SYSUPGRADE_PATH]),
  501. tb[SYSUPGRADE_BACKUP] ? blobmsg_get_string(tb[SYSUPGRADE_BACKUP]) : NULL,
  502. tb[SYSUPGRADE_COMMAND] ? blobmsg_get_string(tb[SYSUPGRADE_COMMAND]) : NULL,
  503. tb[SYSUPGRADE_OPTIONS]);
  504. /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
  505. return UBUS_STATUS_UNKNOWN_ERROR;
  506. }
  507. static void
  508. procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
  509. {
  510. notify = obj->has_subscribers;
  511. }
  512. static const struct ubus_method system_methods[] = {
  513. UBUS_METHOD_NOARG("board", system_board),
  514. UBUS_METHOD_NOARG("info", system_info),
  515. UBUS_METHOD_NOARG("reboot", system_reboot),
  516. UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
  517. UBUS_METHOD("signal", proc_signal, signal_policy),
  518. UBUS_METHOD("validate_firmware_image", validate_firmware_image, validate_firmware_image_policy),
  519. UBUS_METHOD("sysupgrade", sysupgrade, sysupgrade_policy),
  520. };
  521. static struct ubus_object_type system_object_type =
  522. UBUS_OBJECT_TYPE("system", system_methods);
  523. static struct ubus_object system_object = {
  524. .name = "system",
  525. .type = &system_object_type,
  526. .methods = system_methods,
  527. .n_methods = ARRAY_SIZE(system_methods),
  528. .subscribe_cb = procd_subscribe_cb,
  529. };
  530. void
  531. procd_bcast_event(char *event, struct blob_attr *msg)
  532. {
  533. int ret;
  534. if (!notify)
  535. return;
  536. ret = ubus_notify(_ctx, &system_object, event, msg, -1);
  537. if (ret)
  538. fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
  539. }
  540. void ubus_init_system(struct ubus_context *ctx)
  541. {
  542. int ret;
  543. _ctx = ctx;
  544. ret = ubus_add_object(ctx, &system_object);
  545. if (ret)
  546. ERROR("Failed to add object: %s\n", ubus_strerror(ret));
  547. }