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>
24 #include <libubox/uloop.h>
30 static struct blob_buf b;
32 int upgrade_running = 0;
34 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
35 struct ubus_request_data *req, const char *method,
36 struct blob_attr *msg)
41 struct utsname utsname;
46 if (uname(&utsname) >= 0)
48 blobmsg_add_string(&b, "kernel", utsname.release);
49 blobmsg_add_string(&b, "hostname", utsname.nodename);
52 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
54 while(fgets(line, sizeof(line), f))
56 key = strtok(line, "\t:");
57 val = strtok(NULL, "\t\n");
62 if (!strcasecmp(key, "system type") ||
63 !strcasecmp(key, "processor") ||
64 !strcasecmp(key, "model name"))
66 blobmsg_add_string(&b, "system", val + 2);
74 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
76 if (fgets(line, sizeof(line), f))
78 val = strtok(line, "\t\n");
81 blobmsg_add_string(&b, "model", val);
86 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
88 while(fgets(line, sizeof(line), f))
90 key = strtok(line, "\t:");
91 val = strtok(NULL, "\t\n");
96 if (!strcasecmp(key, "machine") ||
97 !strcasecmp(key, "hardware"))
99 blobmsg_add_string(&b, "model", val + 2);
107 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
109 c = blobmsg_open_table(&b, "release");
111 while (fgets(line, sizeof(line), f))
113 key = strtok(line, "=\"");
114 val = strtok(NULL, "\"\n");
119 if (!strcasecmp(key, "DISTRIB_ID"))
120 blobmsg_add_string(&b, "distribution", val);
121 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
122 blobmsg_add_string(&b, "version", val);
123 else if (!strcasecmp(key, "DISTRIB_REVISION"))
124 blobmsg_add_string(&b, "revision", val);
125 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
126 blobmsg_add_string(&b, "codename", val);
127 else if (!strcasecmp(key, "DISTRIB_TARGET"))
128 blobmsg_add_string(&b, "target", val);
129 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
130 blobmsg_add_string(&b, "description", val);
133 blobmsg_close_array(&b, c);
138 ubus_send_reply(ctx, req, b.head);
140 return UBUS_STATUS_OK;
143 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
144 struct ubus_request_data *req, const char *method,
145 struct blob_attr *msg)
154 if (!(tm = localtime(&now)))
155 return UBUS_STATUS_UNKNOWN_ERROR;
158 return UBUS_STATUS_UNKNOWN_ERROR;
160 blob_buf_init(&b, 0);
162 blobmsg_add_u32(&b, "uptime", info.uptime);
163 blobmsg_add_u32(&b, "localtime", mktime(tm));
165 c = blobmsg_open_array(&b, "load");
166 blobmsg_add_u32(&b, NULL, info.loads[0]);
167 blobmsg_add_u32(&b, NULL, info.loads[1]);
168 blobmsg_add_u32(&b, NULL, info.loads[2]);
169 blobmsg_close_array(&b, c);
171 c = blobmsg_open_table(&b, "memory");
172 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalram);
173 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeram);
174 blobmsg_add_u32(&b, "shared", info.mem_unit * info.sharedram);
175 blobmsg_add_u32(&b, "buffered", info.mem_unit * info.bufferram);
176 blobmsg_close_table(&b, c);
178 c = blobmsg_open_table(&b, "swap");
179 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalswap);
180 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeswap);
181 blobmsg_close_table(&b, c);
183 ubus_send_reply(ctx, req, b.head);
185 return UBUS_STATUS_OK;
188 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
189 struct ubus_request_data *req, const char *method,
190 struct blob_attr *msg)
192 procd_reconnect_ubus(0);
208 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
209 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
210 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
211 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
214 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
215 struct ubus_request_data *req, const char *method,
216 struct blob_attr *msg)
218 struct blob_attr *tb[__WDT_MAX];
222 return UBUS_STATUS_INVALID_ARGUMENT;
224 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
225 if (tb[WDT_FREQUENCY]) {
226 unsigned int timeout = watchdog_timeout(0);
227 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
230 if (freq > timeout / 2)
232 watchdog_frequency(freq);
236 if (tb[WDT_TIMEOUT]) {
237 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
238 unsigned int frequency = watchdog_frequency(0);
240 if (timeout <= frequency)
241 timeout = frequency * 2;
242 watchdog_timeout(timeout);
246 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
248 if (watchdog_fd() < 0)
250 else if (watchdog_get_stopped())
255 blob_buf_init(&b, 0);
256 blobmsg_add_string(&b, "status", status);
257 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
258 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
259 ubus_send_reply(ctx, req, b.head);
270 static const struct blobmsg_policy signal_policy[__WDT_MAX] = {
271 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
272 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
275 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
276 struct ubus_request_data *req, const char *method,
277 struct blob_attr *msg)
279 struct blob_attr *tb[__SIGNAL_MAX];
282 return UBUS_STATUS_INVALID_ARGUMENT;
284 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
285 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
286 return UBUS_STATUS_INVALID_ARGUMENT;
288 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
293 static const struct ubus_method system_methods[] = {
294 UBUS_METHOD_NOARG("board", system_board),
295 UBUS_METHOD_NOARG("info", system_info),
296 UBUS_METHOD_NOARG("upgrade", system_upgrade),
297 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
298 UBUS_METHOD("signal", proc_signal, signal_policy),
301 static struct ubus_object_type system_object_type =
302 UBUS_OBJECT_TYPE("system", system_methods);
304 static struct ubus_object system_object = {
306 .type = &system_object_type,
307 .methods = system_methods,
308 .n_methods = ARRAY_SIZE(system_methods),
311 void ubus_init_system(struct ubus_context *ctx)
315 ret = ubus_add_object(ctx, &system_object);
317 ERROR("Failed to add object: %s\n", ubus_strerror(ret));