2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/utsname.h>
17 #include <sys/sysinfo.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/reboot.h>
28 #include <json-c/json_tokener.h>
29 #include <libubox/blobmsg_json.h>
30 #include <libubox/uloop.h>
33 #include "sysupgrade.h"
36 static struct blob_buf b
;
38 static struct ubus_context
*_ctx
;
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
)
52 char *key
, *val
, *next
;
53 struct utsname utsname
;
58 if (uname(&utsname
) >= 0)
60 blobmsg_add_string(&b
, "kernel", utsname
.release
);
61 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
64 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
66 while(fgets(line
, sizeof(line
), f
))
68 key
= strtok(line
, "\t:");
69 val
= strtok(NULL
, "\t\n");
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
);
81 if (!strcasecmp(key
, "system type") ||
82 !strcasecmp(key
, "processor") ||
83 !strcasecmp(key
, "cpu") ||
84 !strcasecmp(key
, "model name"))
86 strtoul(val
+ 2, &key
, 0);
88 if (key
== (val
+ 2) || *key
!= 0)
90 blobmsg_add_string(&b
, "system", val
+ 2);
100 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
||
101 (f
= fopen("/proc/device-tree/model", "r")) != NULL
)
103 if (fgets(line
, sizeof(line
), f
))
105 val
= strtok(line
, "\t\n");
108 blobmsg_add_string(&b
, "model", val
);
113 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
115 while(fgets(line
, sizeof(line
), f
))
117 key
= strtok(line
, "\t:");
118 val
= strtok(NULL
, "\t\n");
123 if (!strcasecmp(key
, "machine") ||
124 !strcasecmp(key
, "hardware"))
126 blobmsg_add_string(&b
, "model", val
+ 2);
134 if ((f
= fopen("/tmp/sysinfo/board_name", "r")) != NULL
)
136 if (fgets(line
, sizeof(line
), f
))
138 val
= strtok(line
, "\t\n");
141 blobmsg_add_string(&b
, "board_name", val
);
146 else if ((f
= fopen("/proc/device-tree/compatible", "r")) != NULL
)
148 if (fgets(line
, sizeof(line
), f
))
150 val
= strtok(line
, "\t\n");
155 while ((next
= strchr(next
, ',')) != NULL
)
161 blobmsg_add_string(&b
, "board_name", val
);
168 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
170 c
= blobmsg_open_table(&b
, "release");
172 while (fgets(line
, sizeof(line
), f
))
178 val
= strchr(line
, '=');
184 if (!strcasecmp(key
, "DISTRIB_ID"))
185 key
= "distribution";
186 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
188 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
190 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
192 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
194 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
199 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
201 ERROR("Failed to allocate blob.\n");
205 while (val
&& (ch
= *(val
++)) != 0) {
209 next
= strchr(val
, ch
);
218 dest
+= strlen(dest
);
221 *(dest
++) = *(val
++);
225 blobmsg_add_string_buffer(&b
);
228 blobmsg_close_array(&b
, c
);
233 ubus_send_reply(ctx
, req
, b
.head
);
235 return UBUS_STATUS_OK
;
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
)
249 unsigned long long available
, cached
;
253 return UBUS_STATUS_UNKNOWN_ERROR
;
255 if ((f
= fopen("/proc/meminfo", "r")) == NULL
)
256 return UBUS_STATUS_UNKNOWN_ERROR
;
258 /* if linux < 3.14 MemAvailable is not in meminfo */
262 while (fgets(line
, sizeof(line
), f
))
264 key
= strtok(line
, " :");
265 val
= strtok(NULL
, " ");
270 if (!strcasecmp(key
, "MemAvailable"))
271 available
= 1024 * atoll(val
);
272 else if (!strcasecmp(key
, "Cached"))
273 cached
= 1024 * atoll(val
);
281 if (!(tm
= localtime(&now
)))
282 return UBUS_STATUS_UNKNOWN_ERROR
;
284 blob_buf_init(&b
, 0);
286 blobmsg_add_u32(&b
, "localtime", now
+ tm
->tm_gmtoff
);
289 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
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
);
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
);
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
);
318 ubus_send_reply(ctx
, req
, b
.head
);
320 return UBUS_STATUS_OK
;
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
)
327 procd_shutdown(RB_AUTOBOOT
);
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
},
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
)
350 struct blob_attr
*tb
[__WDT_MAX
];
354 return UBUS_STATUS_INVALID_ARGUMENT
;
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
]) :
360 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
363 if (freq
> timeout
/ 2)
365 watchdog_frequency(freq
);
369 if (tb
[WDT_TIMEOUT
]) {
370 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
371 unsigned int frequency
= watchdog_frequency(0);
373 if (timeout
<= frequency
)
374 timeout
= frequency
* 2;
375 watchdog_timeout(timeout
);
378 if (tb
[WDT_MAGICCLOSE
])
379 watchdog_set_magicclose(blobmsg_get_bool(tb
[WDT_MAGICCLOSE
]));
382 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
384 if (watchdog_fd() == NULL
)
386 else if (watchdog_get_stopped())
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
);
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
},
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
)
416 struct blob_attr
*tb
[__SIGNAL_MAX
];
419 return UBUS_STATUS_INVALID_ARGUMENT
;
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
;
425 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
430 __attribute__((format (printf
, 2, 3)))
431 static enum vjson_state
vjson_error(char **b
, const char *fmt
, ...)
433 static char buf
[256] = { 0 };
434 const char *pfx
= "Firmware image couldn't be validated: ";
438 r
= snprintf(buf
, sizeof(buf
), "%s", pfx
);
440 *b
= "vjson_error() snprintf failed";
445 r
= vsnprintf(buf
+r
, sizeof(buf
)-r
, fmt
, va
);
447 *b
= "vjson_error() vsnprintf failed";
456 static enum vjson_state
vjson_parse_token(json_tokener
*tok
, char *buf
, ssize_t len
, char **err
)
458 json_object
*jsobj
= NULL
;
460 jsobj
= json_tokener_parse_ex(tok
, buf
, len
);
461 if (json_tokener_get_error(tok
) == json_tokener_continue
)
462 return VJSON_CONTINUE
;
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");
470 blobmsg_add_object(&b
, jsobj
);
471 json_object_put(jsobj
);
472 return VJSON_SUCCESS
;
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
));
480 static enum vjson_state
vjson_parse(int fd
, char **err
)
482 enum vjson_state r
= VJSON_ERROR
;
483 size_t read_count
= 0;
484 char buf
[64] = { 0 };
489 tok
= json_tokener_new();
491 return vjson_error(err
, "json_tokener_new() failed");
493 vjson_error(err
, "incomplete JSON input");
495 while ((len
= read(fd
, buf
, sizeof(buf
)))) {
496 if (len
< 0 && errno
== EINTR
)
501 json_tokener_free(tok
);
502 return vjson_error(err
, "read() failed: %s (%d)",
503 strerror(_errno
), _errno
);
507 r
= vjson_parse_token(tok
, buf
, len
, err
);
508 if (r
!= VJSON_CONTINUE
)
511 memset(buf
, 0, sizeof(buf
));
515 vjson_error(err
, "no JSON input");
517 json_tokener_free(tok
);
522 * validate_firmware_image_call - perform validation & store result in global b
524 * @file: firmware image path
526 static enum vjson_state
validate_firmware_image_call(const char *file
, char **err
)
528 const char *path
= "/usr/libexec/validate_firmware_image";
529 enum vjson_state ret
= VJSON_ERROR
;
534 blob_buf_init(&b
, 0);
535 vjson_error(err
, "unhandled error");
539 return vjson_error(err
, "pipe() failed: %s (%d)",
540 strerror(_errno
), _errno
);
550 return vjson_error(err
, "fork() failed: %s (%d)",
551 strerror(_errno
), _errno
);
553 /* Set stdin & stderr to /dev/null */
554 fd
= open("/dev/null", O_RDWR
);
561 /* Set stdout to the shared pipe */
566 execl(path
, path
, file
, NULL
);
573 ret
= vjson_parse(fds
[0], err
);
580 VALIDATE_FIRMWARE_IMAGE_PATH
,
581 __VALIDATE_FIRMWARE_IMAGE_MAX
,
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
},
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
)
593 struct blob_attr
*tb
[__VALIDATE_FIRMWARE_IMAGE_MAX
];
594 enum vjson_state ret
= VJSON_ERROR
;
598 return UBUS_STATUS_INVALID_ARGUMENT
;
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
;
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
;
608 ubus_send_reply(ctx
, req
, b
.head
);
610 return UBUS_STATUS_OK
;
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
},
632 static void sysupgrade_error(struct ubus_context
*ctx
,
633 struct ubus_request_data
*req
,
638 blob_buf_init(&b
, 0);
640 c
= blobmsg_open_table(&b
, "error");
641 blobmsg_add_string(&b
, "message", message
);
642 blobmsg_close_table(&b
, c
);
644 ubus_send_reply(ctx
, req
, b
.head
);
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
)
653 VALIDATION_FORCEABLE
,
654 VALIDATION_ALLOW_BACKUP
,
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
},
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
;
669 return UBUS_STATUS_INVALID_ARGUMENT
;
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
;
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
;
681 blobmsg_parse(validation_policy
, __VALIDATION_MAX
, validation
, blob_data(b
.head
), blob_len(b
.head
));
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
;
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
]);
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
;
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
;
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
]);
712 /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
713 return UBUS_STATUS_UNKNOWN_ERROR
;
717 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
719 notify
= obj
->has_subscribers
;
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
),
733 static struct ubus_object_type system_object_type
=
734 UBUS_OBJECT_TYPE("system", system_methods
);
736 static struct ubus_object system_object
= {
738 .type
= &system_object_type
,
739 .methods
= system_methods
,
740 .n_methods
= ARRAY_SIZE(system_methods
),
741 .subscribe_cb
= procd_subscribe_cb
,
745 procd_bcast_event(char *event
, struct blob_attr
*msg
)
752 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
754 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
757 void ubus_init_system(struct ubus_context
*ctx
)
762 ret
= ubus_add_object(ctx
, &system_object
);
764 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));