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