瀏覽代碼

add interface uptime to the status info

Felix Fietkau 12 年之前
父節點
當前提交
34a1a4bad3
共有 6 個文件被更改,包括 39 次插入0 次删除
  1. 2 0
      interface.c
  2. 1 0
      interface.h
  3. 11 0
      system-dummy.c
  4. 15 0
      system-linux.c
  5. 3 0
      system.h
  6. 7 0
      ubus.c

+ 2 - 0
interface.c

@@ -9,6 +9,7 @@
 #include "proto.h"
 #include "ubus.h"
 #include "config.h"
+#include "system.h"
 
 struct vlist_tree interfaces;
 
@@ -238,6 +239,7 @@ interface_proto_cb(struct interface_proto_state *state, enum interface_proto_eve
 			return;
 
 		iface->state = IFS_UP;
+		iface->start_time = system_get_rtime();
 		interface_event(iface, IFEV_UP);
 		break;
 	case IFPEV_DOWN:

+ 1 - 0
interface.h

@@ -48,6 +48,7 @@ struct interface {
 	bool autostart;
 	bool config_autostart;
 
+	time_t start_time;
 	enum interface_state state;
 	enum interface_config_state config_state;
 

+ 11 - 0
system-dummy.c

@@ -1,3 +1,4 @@
+#include <sys/time.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -165,3 +166,13 @@ int system_del_route(struct device *dev, struct device_route *route)
 	D(SYSTEM, "route del %s%s%s\n", addr, gw, devstr);
 	return 0;
 }
+
+time_t system_get_rtime(void)
+{
+	struct timeval tv;
+
+	if (gettimeofday(&tv, NULL) == 0)
+		return tv.tv_sec;
+
+	return 0;
+}

+ 15 - 0
system-linux.c

@@ -1,6 +1,7 @@
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 
 #include <linux/rtnetlink.h>
 #include <linux/sockios.h>
@@ -395,3 +396,17 @@ int system_del_route(struct device *dev, struct device_route *route)
 {
 	return system_rt(dev, route, RTM_DELROUTE);
 }
+
+time_t system_get_rtime(void)
+{
+	struct timespec ts;
+	struct timeval tv;
+
+	if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
+		return ts.tv_sec;
+
+	if (gettimeofday(&tv, NULL) == 0)
+		return tv.tv_sec;
+
+	return 0;
+}

+ 3 - 0
system.h

@@ -1,6 +1,7 @@
 #ifndef __NETIFD_SYSTEM_H
 #define __NETIFD_SYSTEM_H
 
+#include <sys/time.h>
 #include <sys/socket.h>
 #include "device.h"
 #include "interface-ip.h"
@@ -43,4 +44,6 @@ int system_del_address(struct device *dev, struct device_addr *addr);
 int system_add_route(struct device *dev, struct device_route *route);
 int system_del_route(struct device *dev, struct device_route *route);
 
+time_t system_get_rtime(void);
+
 #endif

+ 7 - 0
ubus.c

@@ -4,6 +4,7 @@
 #include "interface.h"
 #include "proto.h"
 #include "ubus.h"
+#include "system.h"
 
 static struct ubus_context *ctx = NULL;
 static struct blob_buf b;
@@ -177,6 +178,12 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
 	blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
 	blobmsg_add_u8(&b, "available", iface->available);
 	blobmsg_add_u8(&b, "autostart", iface->autostart);
+
+	if (iface->state == IFS_UP) {
+		time_t cur = system_get_rtime();
+		blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
+	}
+
 	if (iface->main_dev.dev) {
 		struct device *dev = iface->main_dev.dev;
 		const char *field;