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>
16 #include <sys/sysinfo.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
25 #include <libubox/uloop.h>
30 static struct blob_buf b;
32 static struct ubus_context *_ctx;
34 int upgrade_running = 0;
36 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
37 struct ubus_request_data *req, const char *method,
38 struct blob_attr *msg)
42 char *key, *val, *next;
43 struct utsname utsname;
48 if (uname(&utsname) >= 0)
50 blobmsg_add_string(&b, "kernel", utsname.release);
51 blobmsg_add_string(&b, "hostname", utsname.nodename);
54 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
56 while(fgets(line, sizeof(line), f))
58 key = strtok(line, "\t:");
59 val = strtok(NULL, "\t\n");
64 if (!strcasecmp(key, "system type") ||
65 !strcasecmp(key, "processor") ||
66 !strcasecmp(key, "model name"))
68 strtoul(val + 2, &key, 0);
70 if (key == (val + 2) || *key != 0)
72 blobmsg_add_string(&b, "system", val + 2);
81 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
82 (f = fopen("/proc/device-tree/model", "r")) != NULL)
84 if (fgets(line, sizeof(line), f))
86 val = strtok(line, "\t\n");
89 blobmsg_add_string(&b, "model", val);
94 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
96 while(fgets(line, sizeof(line), f))
98 key = strtok(line, "\t:");
99 val = strtok(NULL, "\t\n");
104 if (!strcasecmp(key, "machine") ||
105 !strcasecmp(key, "hardware"))
107 blobmsg_add_string(&b, "model", val + 2);
115 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
117 c = blobmsg_open_table(&b, "release");
119 while (fgets(line, sizeof(line), f))
125 val = strchr(line, '=');
131 if (!strcasecmp(key, "DISTRIB_ID"))
132 key = "distribution";
133 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
135 else if (!strcasecmp(key, "DISTRIB_REVISION"))
137 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
139 else if (!strcasecmp(key, "DISTRIB_TARGET"))
141 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
146 dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
147 while (val && (ch = *(val++)) != 0) {
151 next = strchr(val, ch);
160 dest += strlen(dest);
163 *(dest++) = *(val++);
167 blobmsg_add_string_buffer(&b);
170 blobmsg_close_array(&b, c);
175 ubus_send_reply(ctx, req, b.head);
177 return UBUS_STATUS_OK;
180 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
181 struct ubus_request_data *req, const char *method,
182 struct blob_attr *msg)
191 if (!(tm = localtime(&now)))
192 return UBUS_STATUS_UNKNOWN_ERROR;
195 return UBUS_STATUS_UNKNOWN_ERROR;
197 blob_buf_init(&b, 0);
199 blobmsg_add_u32(&b, "uptime", info.uptime);
200 blobmsg_add_u32(&b, "localtime", mktime(tm));
202 c = blobmsg_open_array(&b, "load");
203 blobmsg_add_u32(&b, NULL, info.loads[0]);
204 blobmsg_add_u32(&b, NULL, info.loads[1]);
205 blobmsg_add_u32(&b, NULL, info.loads[2]);
206 blobmsg_close_array(&b, c);
208 c = blobmsg_open_table(&b, "memory");
209 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram);
210 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram);
211 blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram);
212 blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram);
213 blobmsg_close_table(&b, c);
215 c = blobmsg_open_table(&b, "swap");
216 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap);
217 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap);
218 blobmsg_close_table(&b, c);
220 ubus_send_reply(ctx, req, b.head);
222 return UBUS_STATUS_OK;
225 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
226 struct ubus_request_data *req, const char *method,
227 struct blob_attr *msg)
240 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
241 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
242 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
243 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
246 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
247 struct ubus_request_data *req, const char *method,
248 struct blob_attr *msg)
250 struct blob_attr *tb[__WDT_MAX];
254 return UBUS_STATUS_INVALID_ARGUMENT;
256 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
257 if (tb[WDT_FREQUENCY]) {
258 unsigned int timeout = watchdog_timeout(0);
259 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
262 if (freq > timeout / 2)
264 watchdog_frequency(freq);
268 if (tb[WDT_TIMEOUT]) {
269 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
270 unsigned int frequency = watchdog_frequency(0);
272 if (timeout <= frequency)
273 timeout = frequency * 2;
274 watchdog_timeout(timeout);
278 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
280 if (watchdog_fd() < 0)
282 else if (watchdog_get_stopped())
287 blob_buf_init(&b, 0);
288 blobmsg_add_string(&b, "status", status);
289 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
290 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
291 ubus_send_reply(ctx, req, b.head);
302 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
303 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
304 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
307 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
308 struct ubus_request_data *req, const char *method,
309 struct blob_attr *msg)
311 struct blob_attr *tb[__SIGNAL_MAX];
314 return UBUS_STATUS_INVALID_ARGUMENT;
316 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
317 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
318 return UBUS_STATUS_INVALID_ARGUMENT;
320 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
330 static const struct blobmsg_policy nand_policy[__NAND_MAX] = {
331 [NAND_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
335 procd_spawn_upgraded(char *path)
337 char *wdt_fd = watchdog_fd();
338 char *argv[] = { "/tmp/upgraded", NULL, NULL};
342 DEBUG(2, "Exec to upgraded now\n");
344 watchdog_no_cloexec();
345 setenv("WDTFD", wdt_fd, 1);
347 execvp(argv[0], argv);
350 static int nand_set(struct ubus_context *ctx, struct ubus_object *obj,
351 struct ubus_request_data *req, const char *method,
352 struct blob_attr *msg)
354 struct blob_attr *tb[__NAND_MAX];
357 return UBUS_STATUS_INVALID_ARGUMENT;
359 blobmsg_parse(nand_policy, __NAND_MAX, tb, blob_data(msg), blob_len(msg));
361 return UBUS_STATUS_INVALID_ARGUMENT;
363 procd_spawn_upgraded(blobmsg_get_string(tb[NAND_PATH]));
364 fprintf(stderr, "Yikees, something went wrong. no /sbin/upgraded ?\n");
369 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
371 notify = obj->has_subscribers;
375 static const struct ubus_method system_methods[] = {
376 UBUS_METHOD_NOARG("board", system_board),
377 UBUS_METHOD_NOARG("info", system_info),
378 UBUS_METHOD_NOARG("upgrade", system_upgrade),
379 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
380 UBUS_METHOD("signal", proc_signal, signal_policy),
382 /* must remain at the end as it ia not always loaded */
383 UBUS_METHOD("nandupgrade", nand_set, nand_policy),
386 static struct ubus_object_type system_object_type =
387 UBUS_OBJECT_TYPE("system", system_methods);
389 static struct ubus_object system_object = {
391 .type = &system_object_type,
392 .methods = system_methods,
393 .n_methods = ARRAY_SIZE(system_methods),
394 .subscribe_cb = procd_subscribe_cb,
398 procd_bcast_event(char *event, struct blob_attr *msg)
405 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
407 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
410 void ubus_init_system(struct ubus_context *ctx)
415 if (stat("/sbin/upgraded", &s))
416 system_object.n_methods -= 1;
419 ret = ubus_add_object(ctx, &system_object);
421 ERROR("Failed to add object: %s\n", ubus_strerror(ret));