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 <libubox/uloop.h>
33 static struct blob_buf b;
35 static struct ubus_context *_ctx;
37 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
38 struct ubus_request_data *req, const char *method,
39 struct blob_attr *msg)
43 char *key, *val, *next;
44 struct utsname utsname;
49 if (uname(&utsname) >= 0)
51 blobmsg_add_string(&b, "kernel", utsname.release);
52 blobmsg_add_string(&b, "hostname", utsname.nodename);
55 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
57 while(fgets(line, sizeof(line), f))
59 key = strtok(line, "\t:");
60 val = strtok(NULL, "\t\n");
65 if (!strcasecmp(key, "system type") ||
66 !strcasecmp(key, "processor") ||
67 !strcasecmp(key, "model name"))
69 strtoul(val + 2, &key, 0);
71 if (key == (val + 2) || *key != 0)
73 blobmsg_add_string(&b, "system", val + 2);
82 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
83 (f = fopen("/proc/device-tree/model", "r")) != NULL)
85 if (fgets(line, sizeof(line), f))
87 val = strtok(line, "\t\n");
90 blobmsg_add_string(&b, "model", val);
95 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
97 while(fgets(line, sizeof(line), f))
99 key = strtok(line, "\t:");
100 val = strtok(NULL, "\t\n");
105 if (!strcasecmp(key, "machine") ||
106 !strcasecmp(key, "hardware"))
108 blobmsg_add_string(&b, "model", val + 2);
116 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
118 c = blobmsg_open_table(&b, "release");
120 while (fgets(line, sizeof(line), f))
126 val = strchr(line, '=');
132 if (!strcasecmp(key, "DISTRIB_ID"))
133 key = "distribution";
134 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
136 else if (!strcasecmp(key, "DISTRIB_REVISION"))
138 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
140 else if (!strcasecmp(key, "DISTRIB_TARGET"))
142 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
147 dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
149 ERROR("Failed to allocate blob.\n");
153 while (val && (ch = *(val++)) != 0) {
157 next = strchr(val, ch);
166 dest += strlen(dest);
169 *(dest++) = *(val++);
173 blobmsg_add_string_buffer(&b);
176 blobmsg_close_array(&b, c);
181 ubus_send_reply(ctx, req, b.head);
183 return UBUS_STATUS_OK;
186 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
187 struct ubus_request_data *req, const char *method,
188 struct blob_attr *msg)
197 return UBUS_STATUS_UNKNOWN_ERROR;
202 if (!(tm = localtime(&now)))
203 return UBUS_STATUS_UNKNOWN_ERROR;
205 blob_buf_init(&b, 0);
207 blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff);
210 blobmsg_add_u32(&b, "uptime", info.uptime);
212 c = blobmsg_open_array(&b, "load");
213 blobmsg_add_u32(&b, NULL, info.loads[0]);
214 blobmsg_add_u32(&b, NULL, info.loads[1]);
215 blobmsg_add_u32(&b, NULL, info.loads[2]);
216 blobmsg_close_array(&b, c);
218 c = blobmsg_open_table(&b, "memory");
219 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram);
220 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram);
221 blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram);
222 blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram);
223 blobmsg_close_table(&b, c);
225 c = blobmsg_open_table(&b, "swap");
226 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap);
227 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap);
228 blobmsg_close_table(&b, c);
231 ubus_send_reply(ctx, req, b.head);
233 return UBUS_STATUS_OK;
236 static int system_reboot(struct ubus_context *ctx, struct ubus_object *obj,
237 struct ubus_request_data *req, const char *method,
238 struct blob_attr *msg)
240 procd_shutdown(RB_AUTOBOOT);
251 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
252 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
253 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
254 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
257 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
258 struct ubus_request_data *req, const char *method,
259 struct blob_attr *msg)
261 struct blob_attr *tb[__WDT_MAX];
265 return UBUS_STATUS_INVALID_ARGUMENT;
267 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
268 if (tb[WDT_FREQUENCY]) {
269 unsigned int timeout = watchdog_timeout(0);
270 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
273 if (freq > timeout / 2)
275 watchdog_frequency(freq);
279 if (tb[WDT_TIMEOUT]) {
280 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
281 unsigned int frequency = watchdog_frequency(0);
283 if (timeout <= frequency)
284 timeout = frequency * 2;
285 watchdog_timeout(timeout);
289 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
291 if (watchdog_fd() == NULL)
293 else if (watchdog_get_stopped())
298 blob_buf_init(&b, 0);
299 blobmsg_add_string(&b, "status", status);
300 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
301 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
302 ubus_send_reply(ctx, req, b.head);
313 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
314 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
315 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
318 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
319 struct ubus_request_data *req, const char *method,
320 struct blob_attr *msg)
322 struct blob_attr *tb[__SIGNAL_MAX];
325 return UBUS_STATUS_INVALID_ARGUMENT;
327 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
328 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
329 return UBUS_STATUS_INVALID_ARGUMENT;
331 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
343 static const struct blobmsg_policy sysupgrade_policy[__SYSUPGRADE_MAX] = {
344 [SYSUPGRADE_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
345 [SYSUPGRADE_PREFIX] = { .name = "prefix", .type = BLOBMSG_TYPE_STRING },
346 [SYSUPGRADE_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
350 procd_exec_upgraded(const char *prefix, char *path, char *command)
352 char *wdt_fd = watchdog_fd();
353 char *argv[] = { "/sbin/upgraded", NULL, NULL, NULL};
355 if (chroot(prefix)) {
356 fprintf(stderr, "Failed to chroot for upgraded exec.\n");
363 DEBUG(2, "Exec to upgraded now\n");
365 watchdog_set_cloexec(false);
366 setenv("WDTFD", wdt_fd, 1);
368 execvp(argv[0], argv);
370 /* Cleanup on failure */
371 fprintf(stderr, "Failed to exec upgraded.\n");
373 watchdog_set_cloexec(true);
377 static int sysupgrade(struct ubus_context *ctx, struct ubus_object *obj,
378 struct ubus_request_data *req, const char *method,
379 struct blob_attr *msg)
381 struct blob_attr *tb[__SYSUPGRADE_MAX];
384 return UBUS_STATUS_INVALID_ARGUMENT;
386 blobmsg_parse(sysupgrade_policy, __SYSUPGRADE_MAX, tb, blob_data(msg), blob_len(msg));
387 if (!tb[SYSUPGRADE_PATH] || !tb[SYSUPGRADE_PREFIX])
388 return UBUS_STATUS_INVALID_ARGUMENT;
390 procd_exec_upgraded(blobmsg_get_string(tb[SYSUPGRADE_PREFIX]),
391 blobmsg_get_string(tb[SYSUPGRADE_PATH]),
392 tb[SYSUPGRADE_COMMAND] ? blobmsg_get_string(tb[SYSUPGRADE_COMMAND]) : NULL);
397 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
399 notify = obj->has_subscribers;
403 static const struct ubus_method system_methods[] = {
404 UBUS_METHOD_NOARG("board", system_board),
405 UBUS_METHOD_NOARG("info", system_info),
406 UBUS_METHOD_NOARG("reboot", system_reboot),
407 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
408 UBUS_METHOD("signal", proc_signal, signal_policy),
409 UBUS_METHOD("sysupgrade", sysupgrade, sysupgrade_policy),
412 static struct ubus_object_type system_object_type =
413 UBUS_OBJECT_TYPE("system", system_methods);
415 static struct ubus_object system_object = {
417 .type = &system_object_type,
418 .methods = system_methods,
419 .n_methods = ARRAY_SIZE(system_methods),
420 .subscribe_cb = procd_subscribe_cb,
424 procd_bcast_event(char *event, struct blob_attr *msg)
431 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
433 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
436 void ubus_init_system(struct ubus_context *ctx)
441 ret = ubus_add_object(ctx, &system_object);
443 ERROR("Failed to add object: %s\n", ubus_strerror(ret));