[klibc] [PATCH 2/6] ipconfig: Write $UPTIME as uptime to /tmp/net-$DEVICE.conf

KUMAAN 9maaan at gmail.com
Mon Aug 22 22:53:55 PDT 2011


This patch stores uptime value of sysinfo when ipconfig configured $DEVICE,
and writes $UPTIME as the value to /tmp/net-$DEVICE.conf file.

For example, $UPTIME is equal to '1' which means one second.

Later, other scripts which source the file can calculate the time
when ipconfig configured a $DEVICE . Since the clock of uptime is not
adjusted until reboot, it is useful, while system clock may be
adjusted by some programs like ntpdate or hwclock before other scripts
source the file.

Signed-off-by: KUMAAN <9maaan at gmail.com>
---
 usr/kinit/ipconfig/main.c   |   12 ++++++++++++
 usr/kinit/ipconfig/netdev.h |    1 +
 2 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/usr/kinit/ipconfig/main.c b/usr/kinit/ipconfig/main.c
index 5945b29..74bf1e5 100644
--- a/usr/kinit/ipconfig/main.c
+++ b/usr/kinit/ipconfig/main.c
@@ -8,6 +8,7 @@
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/time.h>
+#include <sys/sysinfo.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <unistd.h>		/* for getopts */
@@ -141,6 +142,11 @@ static void dump_device_config(struct netdev *dev)
 {
 	char fn[40];
 	FILE *f;
+	/*
+	 * char UINT64_MAX[] = "18446744073709551615";
+	 * sizeof(UINT64_MAX)==21
+	 */
+	char buf21[21];
 
 	snprintf(fn, sizeof(fn), "/tmp/net-%s.conf", dev->name);
 	f = fopen(fn, "w");
@@ -166,6 +172,8 @@ static void dump_device_config(struct netdev *dev)
 				my_inet_ntoa(dev->ip_server));
 		write_option(f, "ROOTPATH", dev->bootpath);
 		write_option(f, "filename", dev->filename);
+		sprintf(buf21, "%ld", (long)dev->uptime);
+		write_option(f, "UPTIME", buf21);
 		fclose(f);
 	}
 }
@@ -199,6 +207,10 @@ static void postprocess_device(struct netdev *dev)
 
 static void complete_device(struct netdev *dev)
 {
+	struct sysinfo info;
+	if (!sysinfo(&info))
+		dev->uptime = info.uptime;
+
 	postprocess_device(dev);
 	configure_device(dev);
 	dump_device_config(dev);
diff --git a/usr/kinit/ipconfig/netdev.h b/usr/kinit/ipconfig/netdev.h
index eaf04dc..27b7b78 100644
--- a/usr/kinit/ipconfig/netdev.h
+++ b/usr/kinit/ipconfig/netdev.h
@@ -42,6 +42,7 @@ struct netdev {
 	char nisdomainname[SYS_NMLN];	/* nis domain name      */
 	char bootpath[BPLEN];	/* boot path            */
 	char filename[FNLEN];   /* filename             */
+	long uptime;		/* when complete configuration */
 	struct netdev *next;	/* next configured i/f  */
 };
 
-- 
1.7.2.5




More information about the klibc mailing list