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 int upgrade_running = 0;
39 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
40 struct ubus_request_data *req, const char *method,
41 struct blob_attr *msg)
45 char *key, *val, *next;
46 struct utsname utsname;
51 if (uname(&utsname) >= 0)
53 blobmsg_add_string(&b, "kernel", utsname.release);
54 blobmsg_add_string(&b, "hostname", utsname.nodename);
57 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
59 while(fgets(line, sizeof(line), f))
61 key = strtok(line, "\t:");
62 val = strtok(NULL, "\t\n");
67 if (!strcasecmp(key, "system type") ||
68 !strcasecmp(key, "processor") ||
69 !strcasecmp(key, "model name"))
71 strtoul(val + 2, &key, 0);
73 if (key == (val + 2) || *key != 0)
75 blobmsg_add_string(&b, "system", val + 2);
84 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
85 (f = fopen("/proc/device-tree/model", "r")) != NULL)
87 if (fgets(line, sizeof(line), f))
89 val = strtok(line, "\t\n");
92 blobmsg_add_string(&b, "model", val);
97 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
99 while(fgets(line, sizeof(line), f))
101 key = strtok(line, "\t:");
102 val = strtok(NULL, "\t\n");
107 if (!strcasecmp(key, "machine") ||
108 !strcasecmp(key, "hardware"))
110 blobmsg_add_string(&b, "model", val + 2);
118 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
120 c = blobmsg_open_table(&b, "release");
122 while (fgets(line, sizeof(line), f))
128 val = strchr(line, '=');
134 if (!strcasecmp(key, "DISTRIB_ID"))
135 key = "distribution";
136 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
138 else if (!strcasecmp(key, "DISTRIB_REVISION"))
140 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
142 else if (!strcasecmp(key, "DISTRIB_TARGET"))
144 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
149 dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
151 ERROR("Failed to allocate blob.\n");
155 while (val && (ch = *(val++)) != 0) {
159 next = strchr(val, ch);
168 dest += strlen(dest);
171 *(dest++) = *(val++);
175 blobmsg_add_string_buffer(&b);
178 blobmsg_close_array(&b, c);
183 ubus_send_reply(ctx, req, b.head);
185 return UBUS_STATUS_OK;
188 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
189 struct ubus_request_data *req, const char *method,
190 struct blob_attr *msg)
199 return UBUS_STATUS_UNKNOWN_ERROR;
204 if (!(tm = localtime(&now)))
205 return UBUS_STATUS_UNKNOWN_ERROR;
207 blob_buf_init(&b, 0);
209 blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff);
212 blobmsg_add_u32(&b, "uptime", info.uptime);
214 c = blobmsg_open_array(&b, "load");
215 blobmsg_add_u32(&b, NULL, info.loads[0]);
216 blobmsg_add_u32(&b, NULL, info.loads[1]);
217 blobmsg_add_u32(&b, NULL, info.loads[2]);
218 blobmsg_close_array(&b, c);
220 c = blobmsg_open_table(&b, "memory");
221 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram);
222 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram);
223 blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram);
224 blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram);
225 blobmsg_close_table(&b, c);
227 c = blobmsg_open_table(&b, "swap");
228 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap);
229 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap);
230 blobmsg_close_table(&b, c);
233 ubus_send_reply(ctx, req, b.head);
235 return UBUS_STATUS_OK;
238 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
239 struct ubus_request_data *req, const char *method,
240 struct blob_attr *msg)
246 static int system_reboot(struct ubus_context *ctx, struct ubus_object *obj,
247 struct ubus_request_data *req, const char *method,
248 struct blob_attr *msg)
250 procd_shutdown(RB_AUTOBOOT);
261 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
262 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
263 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
264 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
267 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
268 struct ubus_request_data *req, const char *method,
269 struct blob_attr *msg)
271 struct blob_attr *tb[__WDT_MAX];
275 return UBUS_STATUS_INVALID_ARGUMENT;
277 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
278 if (tb[WDT_FREQUENCY]) {
279 unsigned int timeout = watchdog_timeout(0);
280 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
283 if (freq > timeout / 2)
285 watchdog_frequency(freq);
289 if (tb[WDT_TIMEOUT]) {
290 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
291 unsigned int frequency = watchdog_frequency(0);
293 if (timeout <= frequency)
294 timeout = frequency * 2;
295 watchdog_timeout(timeout);
299 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
301 if (watchdog_fd() == NULL)
303 else if (watchdog_get_stopped())
308 blob_buf_init(&b, 0);
309 blobmsg_add_string(&b, "status", status);
310 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
311 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
312 ubus_send_reply(ctx, req, b.head);
323 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
324 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
325 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
328 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
329 struct ubus_request_data *req, const char *method,
330 struct blob_attr *msg)
332 struct blob_attr *tb[__SIGNAL_MAX];
335 return UBUS_STATUS_INVALID_ARGUMENT;
337 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
338 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
339 return UBUS_STATUS_INVALID_ARGUMENT;
341 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
351 static const struct blobmsg_policy nand_policy[__NAND_MAX] = {
352 [NAND_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
356 procd_spawn_upgraded(char *path)
358 char *wdt_fd = watchdog_fd();
359 char *argv[] = { "/tmp/upgraded", NULL, NULL};
363 DEBUG(2, "Exec to upgraded now\n");
365 watchdog_no_cloexec();
366 setenv("WDTFD", wdt_fd, 1);
368 execvp(argv[0], argv);
371 static int nand_set(struct ubus_context *ctx, struct ubus_object *obj,
372 struct ubus_request_data *req, const char *method,
373 struct blob_attr *msg)
375 struct blob_attr *tb[__NAND_MAX];
378 return UBUS_STATUS_INVALID_ARGUMENT;
380 blobmsg_parse(nand_policy, __NAND_MAX, tb, blob_data(msg), blob_len(msg));
382 return UBUS_STATUS_INVALID_ARGUMENT;
384 procd_spawn_upgraded(blobmsg_get_string(tb[NAND_PATH]));
385 fprintf(stderr, "Yikees, something went wrong. no /sbin/upgraded ?\n");
390 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
392 notify = obj->has_subscribers;
396 static const struct ubus_method system_methods[] = {
397 UBUS_METHOD_NOARG("board", system_board),
398 UBUS_METHOD_NOARG("info", system_info),
399 UBUS_METHOD_NOARG("upgrade", system_upgrade),
400 UBUS_METHOD_NOARG("reboot", system_reboot),
401 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
402 UBUS_METHOD("signal", proc_signal, signal_policy),
404 /* must remain at the end as it ia not always loaded */
405 UBUS_METHOD("nandupgrade", nand_set, nand_policy),
408 static struct ubus_object_type system_object_type =
409 UBUS_OBJECT_TYPE("system", system_methods);
411 static struct ubus_object system_object = {
413 .type = &system_object_type,
414 .methods = system_methods,
415 .n_methods = ARRAY_SIZE(system_methods),
416 .subscribe_cb = procd_subscribe_cb,
420 procd_bcast_event(char *event, struct blob_attr *msg)
427 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
429 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
432 void ubus_init_system(struct ubus_context *ctx)
437 if (stat("/sbin/upgraded", &s))
438 system_object.n_methods -= 1;
441 ret = ubus_add_object(ctx, &system_object);
443 ERROR("Failed to add object: %s\n", ubus_strerror(ret));