cosmetics: provide compatible system info on Aarch64
[project/procd.git] / system.c
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
15 #include <sys/utsname.h>
16 #ifdef linux
17 #include <sys/sysinfo.h>
18 #endif
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/reboot.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27
28 #include <json-c/json_tokener.h>
29 #include <libubox/blobmsg_json.h>
30 #include <libubox/uloop.h>
31
32 #include "procd.h"
33 #include "sysupgrade.h"
34 #include "watchdog.h"
35
36 static struct blob_buf b;
37 static int notify;
38 static struct ubus_context *_ctx;
39
40 enum vjson_state {
41 VJSON_ERROR,
42 VJSON_CONTINUE,
43 VJSON_SUCCESS,
44 };
45
46 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
47 struct ubus_request_data *req, const char *method,
48 struct blob_attr *msg)
49 {
50 void *c;
51 char line[256];
52 char *key, *val, *next;
53 struct utsname utsname;
54 FILE *f;
55
56 blob_buf_init(&b, 0);
57
58 if (uname(&utsname) >= 0)
59 {
60 blobmsg_add_string(&b, "kernel", utsname.release);
61 blobmsg_add_string(&b, "hostname", utsname.nodename);
62 }
63
64 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
65 {
66 while(fgets(line, sizeof(line), f))
67 {
68 key = strtok(line, "\t:");
69 val = strtok(NULL, "\t\n");
70
71 if (!key || !val)
72 continue;
73
74 #ifdef __aarch64__
75 if (!strcasecmp(key, "CPU revision")) {
76 snprintf(line, sizeof(line), "ARMv8 Processor rev %lu", strtoul(val + 2, NULL, 16));
77 blobmsg_add_string(&b, "system", line);
78 break;
79 }
80 #else
81 if (!strcasecmp(key, "system type") ||
82 !strcasecmp(key, "processor") ||
83 !strcasecmp(key, "cpu") ||
84 !strcasecmp(key, "model name"))
85 {
86 strtoul(val + 2, &key, 0);
87
88 if (key == (val + 2) || *key != 0)
89 {
90 blobmsg_add_string(&b, "system", val + 2);
91 break;
92 }
93 }
94 #endif
95 }
96
97 fclose(f);
98 }
99
100 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
101 (f = fopen("/proc/device-tree/model", "r")) != NULL)
102 {
103 if (fgets(line, sizeof(line), f))
104 {
105 val = strtok(line, "\t\n");
106
107 if (val)
108 blobmsg_add_string(&b, "model", val);
109 }
110
111 fclose(f);
112 }
113 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
114 {
115 while(fgets(line, sizeof(line), f))
116 {
117 key = strtok(line, "\t:");
118 val = strtok(NULL, "\t\n");
119
120 if (!key || !val)
121 continue;
122
123 if (!strcasecmp(key, "machine") ||
124 !strcasecmp(key, "hardware"))
125 {
126 blobmsg_add_string(&b, "model", val + 2);
127 break;
128 }
129 }
130
131 fclose(f);
132 }
133
134 if ((f = fopen("/tmp/sysinfo/board_name", "r")) != NULL)
135 {
136 if (fgets(line, sizeof(line), f))
137 {
138 val = strtok(line, "\t\n");
139
140 if (val)
141 blobmsg_add_string(&b, "board_name", val);
142 }
143
144 fclose(f);
145 }
146 else if ((f = fopen("/proc/device-tree/compatible", "r")) != NULL)
147 {
148 if (fgets(line, sizeof(line), f))
149 {
150 val = strtok(line, "\t\n");
151
152 if (val)
153 {
154 next = val;
155 while ((next = strchr(next, ',')) != NULL)
156 {
157 *next = '-';
158 next++;
159 }
160
161 blobmsg_add_string(&b, "board_name", val);
162 }
163 }
164
165 fclose(f);
166 }
167
168 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
169 {
170 c = blobmsg_open_table(&b, "release");
171
172 while (fgets(line, sizeof(line), f))
173 {
174 char *dest;
175 char ch;
176
177 key = line;
178 val = strchr(line, '=');
179 if (!val)
180 continue;
181
182 *(val++) = 0;
183
184 if (!strcasecmp(key, "DISTRIB_ID"))
185 key = "distribution";
186 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
187 key = "version";
188 else if (!strcasecmp(key, "DISTRIB_REVISION"))
189 key = "revision";
190 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
191 key = "codename";
192 else if (!strcasecmp(key, "DISTRIB_TARGET"))
193 key = "target";
194 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
195 key = "description";
196 else
197 continue;
198
199 dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
200 if (!dest) {
201 ERROR("Failed to allocate blob.\n");
202 continue;
203 }
204
205 while (val && (ch = *(val++)) != 0) {
206 switch (ch) {
207 case '\'':
208 case '"':
209 next = strchr(val, ch);
210 if (next)
211 *next = 0;
212
213 strcpy(dest, val);
214
215 if (next)
216 val = next + 1;
217
218 dest += strlen(dest);
219 break;
220 case '\\':
221 *(dest++) = *(val++);
222 break;
223 }
224 }
225 blobmsg_add_string_buffer(&b);
226 }
227
228 blobmsg_close_array(&b, c);
229
230 fclose(f);
231 }
232
233 ubus_send_reply(ctx, req, b.head);
234
235 return UBUS_STATUS_OK;
236 }
237
238 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
239 struct ubus_request_data *req, const char *method,
240 struct blob_attr *msg)
241 {
242 time_t now;
243 struct tm *tm;
244 #ifdef linux
245 struct sysinfo info;
246 void *c;
247 char line[256];
248 char *key, *val;
249 unsigned long long available, cached;
250 FILE *f;
251
252 if (sysinfo(&info))
253 return UBUS_STATUS_UNKNOWN_ERROR;
254
255 if ((f = fopen("/proc/meminfo", "r")) == NULL)
256 return UBUS_STATUS_UNKNOWN_ERROR;
257
258 /* if linux < 3.14 MemAvailable is not in meminfo */
259 available = 0;
260 cached = 0;
261
262 while (fgets(line, sizeof(line), f))
263 {
264 key = strtok(line, " :");
265 val = strtok(NULL, " ");
266
267 if (!key || !val)
268 continue;
269
270 if (!strcasecmp(key, "MemAvailable"))
271 available = 1024 * atoll(val);
272 else if (!strcasecmp(key, "Cached"))
273 cached = 1024 * atoll(val);
274 }
275
276 fclose(f);
277 #endif
278
279 now = time(NULL);
280
281 if (!(tm = localtime(&now)))
282 return UBUS_STATUS_UNKNOWN_ERROR;
283
284 blob_buf_init(&b, 0);
285
286 blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff);
287
288 #ifdef linux
289 blobmsg_add_u32(&b, "uptime", info.uptime);
290
291 c = blobmsg_open_array(&b, "load");
292 blobmsg_add_u32(&b, NULL, info.loads[0]);
293 blobmsg_add_u32(&b, NULL, info.loads[1]);
294 blobmsg_add_u32(&b, NULL, info.loads[2]);
295 blobmsg_close_array(&b, c);
296
297 c = blobmsg_open_table(&b, "memory");
298 blobmsg_add_u64(&b, "total",
299 (uint64_t)info.mem_unit * (uint64_t)info.totalram);
300 blobmsg_add_u64(&b, "free",
301 (uint64_t)info.mem_unit * (uint64_t)info.freeram);
302 blobmsg_add_u64(&b, "shared",
303 (uint64_t)info.mem_unit * (uint64_t)info.sharedram);
304 blobmsg_add_u64(&b, "buffered",
305 (uint64_t)info.mem_unit * (uint64_t)info.bufferram);
306 blobmsg_add_u64(&b, "available", available);
307 blobmsg_add_u64(&b, "cached", cached);
308 blobmsg_close_table(&b, c);
309
310 c = blobmsg_open_table(&b, "swap");
311 blobmsg_add_u64(&b, "total",
312 (uint64_t)info.mem_unit * (uint64_t)info.totalswap);
313 blobmsg_add_u64(&b, "free",
314 (uint64_t)info.mem_unit * (uint64_t)info.freeswap);
315 blobmsg_close_table(&b, c);
316 #endif
317
318 ubus_send_reply(ctx, req, b.head);
319
320 return UBUS_STATUS_OK;
321 }
322
323 static int system_reboot(struct ubus_context *ctx, struct ubus_object *obj,
324 struct ubus_request_data *req, const char *method,
325 struct blob_attr *msg)
326 {
327 procd_shutdown(RB_AUTOBOOT);
328 return 0;
329 }
330
331 enum {
332 WDT_FREQUENCY,
333 WDT_TIMEOUT,
334 WDT_MAGICCLOSE,
335 WDT_STOP,
336 __WDT_MAX
337 };
338
339 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
340 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
341 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
342 [WDT_MAGICCLOSE] = { .name = "magicclose", .type = BLOBMSG_TYPE_BOOL },
343 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
344 };
345
346 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
347 struct ubus_request_data *req, const char *method,
348 struct blob_attr *msg)
349 {
350 struct blob_attr *tb[__WDT_MAX];
351 const char *status;
352
353 if (!msg)
354 return UBUS_STATUS_INVALID_ARGUMENT;
355
356 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
357 if (tb[WDT_FREQUENCY]) {
358 unsigned int timeout = tb[WDT_TIMEOUT] ? blobmsg_get_u32(tb[WDT_TIMEOUT]) :
359 watchdog_timeout(0);
360 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
361
362 if (freq) {
363 if (freq > timeout / 2)
364 freq = timeout / 2;
365 watchdog_frequency(freq);
366 }
367 }
368
369 if (tb[WDT_TIMEOUT]) {
370 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
371 unsigned int frequency = watchdog_frequency(0);
372
373 if (timeout <= frequency)
374 timeout = frequency * 2;
375 watchdog_timeout(timeout);
376 }
377
378 if (tb[WDT_MAGICCLOSE])
379 watchdog_set_magicclose(blobmsg_get_bool(tb[WDT_MAGICCLOSE]));
380
381 if (tb[WDT_STOP])
382 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
383
384 if (watchdog_fd() == NULL)
385 status = "offline";
386 else if (watchdog_get_stopped())
387 status = "stopped";
388 else
389 status = "running";
390
391 blob_buf_init(&b, 0);
392 blobmsg_add_string(&b, "status", status);
393 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
394 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
395 blobmsg_add_u8(&b, "magicclose", watchdog_get_magicclose());
396 ubus_send_reply(ctx, req, b.head);
397
398 return 0;
399 }
400
401 enum {
402 SIGNAL_PID,
403 SIGNAL_NUM,
404 __SIGNAL_MAX
405 };
406
407 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
408 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
409 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
410 };
411
412 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
413 struct ubus_request_data *req, const char *method,
414 struct blob_attr *msg)
415 {
416 struct blob_attr *tb[__SIGNAL_MAX];
417
418 if (!msg)
419 return UBUS_STATUS_INVALID_ARGUMENT;
420
421 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
422 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
423 return UBUS_STATUS_INVALID_ARGUMENT;
424
425 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
426
427 return 0;
428 }
429
430 __attribute__((format (printf, 2, 3)))
431 static enum vjson_state vjson_error(char **b, const char *fmt, ...)
432 {
433 static char buf[256] = { 0 };
434 const char *pfx = "Firmware image couldn't be validated: ";
435 va_list va;
436 int r;
437
438 r = snprintf(buf, sizeof(buf), "%s", pfx);
439 if (r < 0) {
440 *b = "vjson_error() snprintf failed";
441 return VJSON_ERROR;
442 }
443
444 va_start(va, fmt);
445 r = vsnprintf(buf+r, sizeof(buf)-r, fmt, va);
446 if (r < 0) {
447 *b = "vjson_error() vsnprintf failed";
448 return VJSON_ERROR;
449 }
450 va_end(va);
451
452 *b = buf;
453 return VJSON_ERROR;
454 }
455
456 static enum vjson_state vjson_parse_token(json_tokener *tok, char *buf, ssize_t len, char **err)
457 {
458 json_object *jsobj = NULL;
459
460 jsobj = json_tokener_parse_ex(tok, buf, len);
461 if (json_tokener_get_error(tok) == json_tokener_continue)
462 return VJSON_CONTINUE;
463
464 if (json_tokener_get_error(tok) == json_tokener_success) {
465 if (json_object_get_type(jsobj) != json_type_object) {
466 json_object_put(jsobj);
467 return vjson_error(err, "result is not an JSON object");
468 }
469
470 blobmsg_add_object(&b, jsobj);
471 json_object_put(jsobj);
472 return VJSON_SUCCESS;
473 }
474
475 return vjson_error(err, "failed to parse JSON: %s (%d)",
476 json_tokener_error_desc(json_tokener_get_error(tok)),
477 json_tokener_get_error(tok));
478 }
479
480 static enum vjson_state vjson_parse(int fd, char **err)
481 {
482 enum vjson_state r = VJSON_ERROR;
483 size_t read_count = 0;
484 char buf[64] = { 0 };
485 json_tokener *tok;
486 ssize_t len;
487 int _errno;
488
489 tok = json_tokener_new();
490 if (!tok)
491 return vjson_error(err, "json_tokener_new() failed");
492
493 vjson_error(err, "incomplete JSON input");
494
495 while ((len = read(fd, buf, sizeof(buf)))) {
496 if (len < 0 && errno == EINTR)
497 continue;
498
499 if (len < 0) {
500 _errno = errno;
501 json_tokener_free(tok);
502 return vjson_error(err, "read() failed: %s (%d)",
503 strerror(_errno), _errno);
504 }
505
506 read_count += len;
507 r = vjson_parse_token(tok, buf, len, err);
508 if (r != VJSON_CONTINUE)
509 break;
510
511 memset(buf, 0, sizeof(buf));
512 }
513
514 if (read_count == 0)
515 vjson_error(err, "no JSON input");
516
517 json_tokener_free(tok);
518 return r;
519 }
520
521 /**
522 * validate_firmware_image_call - perform validation & store result in global b
523 *
524 * @file: firmware image path
525 */
526 static enum vjson_state validate_firmware_image_call(const char *file, char **err)
527 {
528 const char *path = "/usr/libexec/validate_firmware_image";
529 enum vjson_state ret = VJSON_ERROR;
530 int _errno;
531 int fds[2];
532 int fd;
533
534 blob_buf_init(&b, 0);
535 vjson_error(err, "unhandled error");
536
537 if (pipe(fds)) {
538 _errno = errno;
539 return vjson_error(err, "pipe() failed: %s (%d)",
540 strerror(_errno), _errno);
541 }
542
543 switch (fork()) {
544 case -1:
545 _errno = errno;
546
547 close(fds[0]);
548 close(fds[1]);
549
550 return vjson_error(err, "fork() failed: %s (%d)",
551 strerror(_errno), _errno);
552 case 0:
553 /* Set stdin & stderr to /dev/null */
554 fd = open("/dev/null", O_RDWR);
555 if (fd >= 0) {
556 dup2(fd, 0);
557 dup2(fd, 2);
558 close(fd);
559 }
560
561 /* Set stdout to the shared pipe */
562 dup2(fds[1], 1);
563 close(fds[0]);
564 close(fds[1]);
565
566 execl(path, path, file, NULL);
567 exit(errno);
568 }
569
570 /* Parent process */
571 close(fds[1]);
572
573 ret = vjson_parse(fds[0], err);
574 close(fds[0]);
575
576 return ret;
577 }
578
579 enum {
580 VALIDATE_FIRMWARE_IMAGE_PATH,
581 __VALIDATE_FIRMWARE_IMAGE_MAX,
582 };
583
584 static const struct blobmsg_policy validate_firmware_image_policy[__VALIDATE_FIRMWARE_IMAGE_MAX] = {
585 [VALIDATE_FIRMWARE_IMAGE_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
586 };
587
588 static int validate_firmware_image(struct ubus_context *ctx,
589 struct ubus_object *obj,
590 struct ubus_request_data *req,
591 const char *method, struct blob_attr *msg)
592 {
593 struct blob_attr *tb[__VALIDATE_FIRMWARE_IMAGE_MAX];
594 enum vjson_state ret = VJSON_ERROR;
595 char *err;
596
597 if (!msg)
598 return UBUS_STATUS_INVALID_ARGUMENT;
599
600 blobmsg_parse(validate_firmware_image_policy, __VALIDATE_FIRMWARE_IMAGE_MAX, tb, blob_data(msg), blob_len(msg));
601 if (!tb[VALIDATE_FIRMWARE_IMAGE_PATH])
602 return UBUS_STATUS_INVALID_ARGUMENT;
603
604 ret = validate_firmware_image_call(blobmsg_get_string(tb[VALIDATE_FIRMWARE_IMAGE_PATH]), &err);
605 if (ret != VJSON_SUCCESS)
606 return UBUS_STATUS_UNKNOWN_ERROR;
607
608 ubus_send_reply(ctx, req, b.head);
609
610 return UBUS_STATUS_OK;
611 }
612
613 enum {
614 SYSUPGRADE_PATH,
615 SYSUPGRADE_FORCE,
616 SYSUPGRADE_BACKUP,
617 SYSUPGRADE_PREFIX,
618 SYSUPGRADE_COMMAND,
619 SYSUPGRADE_OPTIONS,
620 __SYSUPGRADE_MAX
621 };
622
623 static const struct blobmsg_policy sysupgrade_policy[__SYSUPGRADE_MAX] = {
624 [SYSUPGRADE_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
625 [SYSUPGRADE_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_BOOL },
626 [SYSUPGRADE_BACKUP] = { .name = "backup", .type = BLOBMSG_TYPE_STRING },
627 [SYSUPGRADE_PREFIX] = { .name = "prefix", .type = BLOBMSG_TYPE_STRING },
628 [SYSUPGRADE_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
629 [SYSUPGRADE_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_TABLE },
630 };
631
632 static void sysupgrade_error(struct ubus_context *ctx,
633 struct ubus_request_data *req,
634 const char *message)
635 {
636 void *c;
637
638 blob_buf_init(&b, 0);
639
640 c = blobmsg_open_table(&b, "error");
641 blobmsg_add_string(&b, "message", message);
642 blobmsg_close_table(&b, c);
643
644 ubus_send_reply(ctx, req, b.head);
645 }
646
647 static int sysupgrade(struct ubus_context *ctx, struct ubus_object *obj,
648 struct ubus_request_data *req, const char *method,
649 struct blob_attr *msg)
650 {
651 enum {
652 VALIDATION_VALID,
653 VALIDATION_FORCEABLE,
654 VALIDATION_ALLOW_BACKUP,
655 __VALIDATION_MAX
656 };
657 static const struct blobmsg_policy validation_policy[__VALIDATION_MAX] = {
658 [VALIDATION_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_BOOL },
659 [VALIDATION_FORCEABLE] = { .name = "forceable", .type = BLOBMSG_TYPE_BOOL },
660 [VALIDATION_ALLOW_BACKUP] = { .name = "allow_backup", .type = BLOBMSG_TYPE_BOOL },
661 };
662 struct blob_attr *validation[__VALIDATION_MAX];
663 struct blob_attr *tb[__SYSUPGRADE_MAX];
664 bool valid, forceable, allow_backup;
665 enum vjson_state ret = VJSON_ERROR;
666 char *err;
667
668 if (!msg)
669 return UBUS_STATUS_INVALID_ARGUMENT;
670
671 blobmsg_parse(sysupgrade_policy, __SYSUPGRADE_MAX, tb, blob_data(msg), blob_len(msg));
672 if (!tb[SYSUPGRADE_PATH] || !tb[SYSUPGRADE_PREFIX])
673 return UBUS_STATUS_INVALID_ARGUMENT;
674
675 ret = validate_firmware_image_call(blobmsg_get_string(tb[SYSUPGRADE_PATH]), &err);
676 if (ret != VJSON_SUCCESS) {
677 sysupgrade_error(ctx, req, err);
678 return UBUS_STATUS_UNKNOWN_ERROR;
679 }
680
681 blobmsg_parse(validation_policy, __VALIDATION_MAX, validation, blob_data(b.head), blob_len(b.head));
682
683 if (!validation[VALIDATION_VALID] || !validation[VALIDATION_FORCEABLE] ||
684 !validation[VALIDATION_ALLOW_BACKUP]) {
685 sysupgrade_error(ctx, req, "Validation script provided invalid input");
686 return UBUS_STATUS_INVALID_ARGUMENT;
687 }
688
689 valid = validation[VALIDATION_VALID] && blobmsg_get_bool(validation[VALIDATION_VALID]);
690 forceable = validation[VALIDATION_FORCEABLE] && blobmsg_get_bool(validation[VALIDATION_FORCEABLE]);
691 allow_backup = validation[VALIDATION_ALLOW_BACKUP] && blobmsg_get_bool(validation[VALIDATION_ALLOW_BACKUP]);
692
693 if (!valid) {
694 if (!forceable) {
695 sysupgrade_error(ctx, req, "Firmware image is broken and cannot be installed");
696 return UBUS_STATUS_NOT_SUPPORTED;
697 } else if (!tb[SYSUPGRADE_FORCE] || !blobmsg_get_bool(tb[SYSUPGRADE_FORCE])) {
698 sysupgrade_error(ctx, req, "Firmware image is invalid");
699 return UBUS_STATUS_NOT_SUPPORTED;
700 }
701 } else if (!allow_backup && tb[SYSUPGRADE_BACKUP]) {
702 sysupgrade_error(ctx, req, "Firmware image doesn't allow preserving a backup");
703 return UBUS_STATUS_NOT_SUPPORTED;
704 }
705
706 sysupgrade_exec_upgraded(blobmsg_get_string(tb[SYSUPGRADE_PREFIX]),
707 blobmsg_get_string(tb[SYSUPGRADE_PATH]),
708 tb[SYSUPGRADE_BACKUP] ? blobmsg_get_string(tb[SYSUPGRADE_BACKUP]) : NULL,
709 tb[SYSUPGRADE_COMMAND] ? blobmsg_get_string(tb[SYSUPGRADE_COMMAND]) : NULL,
710 tb[SYSUPGRADE_OPTIONS]);
711
712 /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
713 return UBUS_STATUS_UNKNOWN_ERROR;
714 }
715
716 static void
717 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
718 {
719 notify = obj->has_subscribers;
720 }
721
722
723 static const struct ubus_method system_methods[] = {
724 UBUS_METHOD_NOARG("board", system_board),
725 UBUS_METHOD_NOARG("info", system_info),
726 UBUS_METHOD_NOARG("reboot", system_reboot),
727 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
728 UBUS_METHOD("signal", proc_signal, signal_policy),
729 UBUS_METHOD("validate_firmware_image", validate_firmware_image, validate_firmware_image_policy),
730 UBUS_METHOD("sysupgrade", sysupgrade, sysupgrade_policy),
731 };
732
733 static struct ubus_object_type system_object_type =
734 UBUS_OBJECT_TYPE("system", system_methods);
735
736 static struct ubus_object system_object = {
737 .name = "system",
738 .type = &system_object_type,
739 .methods = system_methods,
740 .n_methods = ARRAY_SIZE(system_methods),
741 .subscribe_cb = procd_subscribe_cb,
742 };
743
744 void
745 procd_bcast_event(char *event, struct blob_attr *msg)
746 {
747 int ret;
748
749 if (!notify)
750 return;
751
752 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
753 if (ret)
754 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
755 }
756
757 void ubus_init_system(struct ubus_context *ctx)
758 {
759 int ret;
760
761 _ctx = ctx;
762 ret = ubus_add_object(ctx, &system_object);
763 if (ret)
764 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
765 }