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 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
33 struct ubus_request_data *req, const char *method,
34 struct blob_attr *msg)
39 struct utsname utsname;
44 if (uname(&utsname) >= 0)
46 blobmsg_add_string(&b, "kernel", utsname.release);
47 blobmsg_add_string(&b, "hostname", utsname.nodename);
50 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
52 while(fgets(line, sizeof(line), f))
54 key = strtok(line, "\t:");
55 val = strtok(NULL, "\t\n");
60 if (!strcasecmp(key, "system type") ||
61 !strcasecmp(key, "processor") ||
62 !strcasecmp(key, "model name"))
64 blobmsg_add_string(&b, "system", val + 2);
72 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
74 if (fgets(line, sizeof(line), f))
76 val = strtok(line, "\t\n");
79 blobmsg_add_string(&b, "model", val);
84 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
86 while(fgets(line, sizeof(line), f))
88 key = strtok(line, "\t:");
89 val = strtok(NULL, "\t\n");
94 if (!strcasecmp(key, "machine") ||
95 !strcasecmp(key, "hardware"))
97 blobmsg_add_string(&b, "model", val + 2);
105 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
107 c = blobmsg_open_table(&b, "release");
109 while (fgets(line, sizeof(line), f))
111 key = strtok(line, "=\"");
112 val = strtok(NULL, "\"\n");
117 if (!strcasecmp(key, "DISTRIB_ID"))
118 blobmsg_add_string(&b, "distribution", val);
119 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
120 blobmsg_add_string(&b, "version", val);
121 else if (!strcasecmp(key, "DISTRIB_REVISION"))
122 blobmsg_add_string(&b, "revision", val);
123 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
124 blobmsg_add_string(&b, "codename", val);
125 else if (!strcasecmp(key, "DISTRIB_TARGET"))
126 blobmsg_add_string(&b, "target", val);
127 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
128 blobmsg_add_string(&b, "description", val);
131 blobmsg_close_array(&b, c);
136 ubus_send_reply(ctx, req, b.head);
138 return UBUS_STATUS_OK;
141 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
142 struct ubus_request_data *req, const char *method,
143 struct blob_attr *msg)
152 if (!(tm = localtime(&now)))
153 return UBUS_STATUS_UNKNOWN_ERROR;
156 return UBUS_STATUS_UNKNOWN_ERROR;
158 blob_buf_init(&b, 0);
160 blobmsg_add_u32(&b, "uptime", info.uptime);
161 blobmsg_add_u32(&b, "localtime", mktime(tm));
163 c = blobmsg_open_array(&b, "load");
164 blobmsg_add_u32(&b, NULL, info.loads[0]);
165 blobmsg_add_u32(&b, NULL, info.loads[1]);
166 blobmsg_add_u32(&b, NULL, info.loads[2]);
167 blobmsg_close_array(&b, c);
169 c = blobmsg_open_table(&b, "memory");
170 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalram);
171 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeram);
172 blobmsg_add_u32(&b, "shared", info.mem_unit * info.sharedram);
173 blobmsg_add_u32(&b, "buffered", info.mem_unit * info.bufferram);
174 blobmsg_close_table(&b, c);
176 c = blobmsg_open_table(&b, "swap");
177 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalswap);
178 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeswap);
179 blobmsg_close_table(&b, c);
181 ubus_send_reply(ctx, req, b.head);
183 return UBUS_STATUS_OK;
186 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
187 struct ubus_request_data *req, const char *method,
188 struct blob_attr *msg)
190 procd_reconnect_ubus(0);
204 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
205 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
206 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
207 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
210 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
211 struct ubus_request_data *req, const char *method,
212 struct blob_attr *msg)
214 struct blob_attr *tb[__WDT_MAX];
218 return UBUS_STATUS_INVALID_ARGUMENT;
220 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
221 if (tb[WDT_FREQUENCY]) {
222 unsigned int timeout = watchdog_timeout(0);
223 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
226 if (freq > timeout / 2)
228 watchdog_frequency(freq);
232 if (tb[WDT_TIMEOUT]) {
233 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
234 unsigned int frequency = watchdog_frequency(0);
236 if (timeout <= frequency)
237 timeout = frequency * 2;
238 watchdog_timeout(timeout);
242 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
244 if (watchdog_fd() < 0)
246 else if (watchdog_get_stopped())
251 blob_buf_init(&b, 0);
252 blobmsg_add_string(&b, "status", status);
253 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
254 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
255 ubus_send_reply(ctx, req, b.head);
260 static const struct ubus_method system_methods[] = {
261 UBUS_METHOD_NOARG("board", system_board),
262 UBUS_METHOD_NOARG("info", system_info),
263 UBUS_METHOD_NOARG("upgrade", system_upgrade),
264 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
267 static struct ubus_object_type system_object_type =
268 UBUS_OBJECT_TYPE("system", system_methods);
270 static struct ubus_object system_object = {
272 .type = &system_object_type,
273 .methods = system_methods,
274 .n_methods = ARRAY_SIZE(system_methods),
277 void ubus_init_system(struct ubus_context *ctx)
281 ret = ubus_add_object(ctx, &system_object);
283 ERROR("Failed to add object: %s\n", ubus_strerror(ret));