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