Add system namespace which offers various system information like software release...
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 30 May 2013 18:17:16 +0000 (20:17 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 30 May 2013 18:35:46 +0000 (20:35 +0200)
CMakeLists.txt
main.c
system.c [new file with mode: 0644]
system.h [new file with mode: 0644]

index 3c3470016c9498782d69f10795a1706d38dbe09e..6142e5cf01faba39210b3090da6d7593708df172 100644 (file)
@@ -10,7 +10,7 @@ IF(APPLE)
   LINK_DIRECTORIES(/opt/local/lib)
 ENDIF()
 
-ADD_EXECUTABLE(luci-rpcd main.c session.c file.c uci.c iwinfo.c)
+ADD_EXECUTABLE(luci-rpcd main.c session.c file.c uci.c iwinfo.c system.c)
 TARGET_LINK_LIBRARIES(luci-rpcd ubox ubus uci iwinfo)
 
 SET(CMAKE_INSTALL_PREFIX /usr)
diff --git a/main.c b/main.c
index ac5b8f817f8184381784dd09ac272777d3ac9c7c..71f5863afde3f9e9f9c9af2fb62ba387cc3f7008 100644 (file)
--- a/main.c
+++ b/main.c
@@ -27,6 +27,7 @@
 #include "file.h"
 #include "uci.h"
 #include "iwinfo.h"
+#include "system.h"
 
 static struct ubus_context *ctx;
 
@@ -64,6 +65,7 @@ int main(int argc, char **argv)
        rpc_file_api_init(ctx);
        rpc_uci_api_init(ctx);
        rpc_iwinfo_api_init(ctx);
+       rpc_system_api_init(ctx);
 
        uloop_run();
        ubus_free(ctx);
diff --git a/system.c b/system.c
new file mode 100644 (file)
index 0000000..2d28429
--- /dev/null
+++ b/system.c
@@ -0,0 +1,204 @@
+/*
+ * luci-rpcd - LuCI UBUS RPC server
+ *
+ *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <time.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <sys/sysinfo.h>
+#include <sys/utsname.h>
+
+#include "system.h"
+
+static struct blob_buf buf;
+
+static int
+rpc_system_board(struct ubus_context *ctx, struct ubus_object *obj,
+                 struct ubus_request_data *req, const char *method,
+                 struct blob_attr *msg)
+{
+       void *c;
+       char line[256];
+       char *key, *val;
+       struct utsname utsname;
+       FILE *f;
+
+       blob_buf_init(&buf, 0);
+
+       if (uname(&utsname) >= 0)
+       {
+               blobmsg_add_string(&buf, "kernel", utsname.release);
+               blobmsg_add_string(&buf, "hostname", utsname.nodename);
+       }
+
+       if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
+       {
+               while(fgets(line, sizeof(line), f))
+               {
+                       key = strtok(line, "\t:");
+                       val = strtok(NULL, "\t\n");
+
+                       if (!key || !val)
+                               continue;
+
+                       if (!strcasecmp(key, "system type") ||
+                           !strcasecmp(key, "processor") ||
+                           !strcasecmp(key, "model name"))
+                       {
+                               blobmsg_add_string(&buf, "system", val + 2);
+                               break;
+                       }
+               }
+
+               fclose(f);
+       }
+
+       if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
+       {
+               if (fgets(line, sizeof(line), f))
+               {
+                       val = strtok(line, "\t\n");
+
+                       if (val)
+                               blobmsg_add_string(&buf, "model", val);
+               }
+
+               fclose(f);
+       }
+       else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
+       {
+               while(fgets(line, sizeof(line), f))
+               {
+                       key = strtok(line, "\t:");
+                       val = strtok(NULL, "\t\n");
+
+                       if (!key || !val)
+                               continue;
+
+                       if (!strcasecmp(key, "machine") ||
+                           !strcasecmp(key, "hardware"))
+                       {
+                               blobmsg_add_string(&buf, "model", val + 2);
+                               break;
+                       }
+               }
+
+               fclose(f);
+       }
+
+       if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
+       {
+               c = blobmsg_open_table(&buf, "release");
+
+               while (fgets(line, sizeof(line), f))
+               {
+                       key = strtok(line, "=\"");
+                       val = strtok(NULL, "\"\n");
+
+                       if (!key || !val)
+                               continue;
+
+                       if (!strcasecmp(key, "DISTRIB_ID"))
+                               blobmsg_add_string(&buf, "distribution", val);
+                       else if (!strcasecmp(key, "DISTRIB_RELEASE"))
+                               blobmsg_add_string(&buf, "version", val);
+                       else if (!strcasecmp(key, "DISTRIB_REVISION"))
+                               blobmsg_add_string(&buf, "revision", val);
+                       else if (!strcasecmp(key, "DISTRIB_CODENAME"))
+                               blobmsg_add_string(&buf, "codename", val);
+                       else if (!strcasecmp(key, "DISTRIB_TARGET"))
+                               blobmsg_add_string(&buf, "target", val);
+                       else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
+                               blobmsg_add_string(&buf, "description", val);
+               }
+
+               blobmsg_close_array(&buf, c);
+
+               fclose(f);
+       }
+
+       ubus_send_reply(ctx, req, buf.head);
+
+       return UBUS_STATUS_OK;
+}
+
+static int
+rpc_system_info(struct ubus_context *ctx, struct ubus_object *obj,
+                struct ubus_request_data *req, const char *method,
+                struct blob_attr *msg)
+{
+       void *c;
+       time_t now;
+       struct tm *tm;
+       struct sysinfo info;
+
+       now = time(NULL);
+
+       if (!(tm = localtime(&now)))
+               return UBUS_STATUS_UNKNOWN_ERROR;
+
+       if (sysinfo(&info))
+               return UBUS_STATUS_UNKNOWN_ERROR;
+
+       blob_buf_init(&buf, 0);
+
+       blobmsg_add_u32(&buf, "uptime",    info.uptime);
+       blobmsg_add_u32(&buf, "localtime", mktime(tm));
+
+       c = blobmsg_open_array(&buf, "load");
+       blobmsg_add_u32(&buf, NULL, info.loads[0]);
+       blobmsg_add_u32(&buf, NULL, info.loads[1]);
+       blobmsg_add_u32(&buf, NULL, info.loads[2]);
+       blobmsg_close_array(&buf, c);
+
+       c = blobmsg_open_table(&buf, "memory");
+       blobmsg_add_u32(&buf, "total",    info.mem_unit * info.totalram);
+       blobmsg_add_u32(&buf, "free",     info.mem_unit * info.freeram);
+       blobmsg_add_u32(&buf, "shared",   info.mem_unit * info.sharedram);
+       blobmsg_add_u32(&buf, "buffered", info.mem_unit * info.bufferram);
+       blobmsg_close_table(&buf, c);
+
+       c = blobmsg_open_table(&buf, "swap");
+       blobmsg_add_u32(&buf, "total",    info.mem_unit * info.totalswap);
+       blobmsg_add_u32(&buf, "free",     info.mem_unit * info.freeswap);
+       blobmsg_close_table(&buf, c);
+
+       ubus_send_reply(ctx, req, buf.head);
+
+       return UBUS_STATUS_OK;
+}
+
+int rpc_system_api_init(struct ubus_context *ctx)
+{
+       static const struct ubus_method system_methods[] = {
+               UBUS_METHOD_NOARG("board", rpc_system_board),
+               UBUS_METHOD_NOARG("info",  rpc_system_info),
+       };
+
+       static struct ubus_object_type system_type =
+               UBUS_OBJECT_TYPE("luci-rpc-system", system_methods);
+
+       static struct ubus_object obj = {
+               .name = "system",
+               .type = &system_type,
+               .methods = system_methods,
+               .n_methods = ARRAY_SIZE(system_methods),
+       };
+
+       return ubus_add_object(ctx, &obj);
+}
diff --git a/system.h b/system.h
new file mode 100644 (file)
index 0000000..1771249
--- /dev/null
+++ b/system.h
@@ -0,0 +1,27 @@
+/*
+ * luci-rpcd - LuCI UBUS RPC server
+ *
+ *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __RPC_SYSTEM_H
+#define __RPC_SYSTEM_H
+
+#include <libubus.h>
+#include <libubox/blobmsg.h>
+
+int rpc_system_api_init(struct ubus_context *ctx);
+
+#endif