public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 02/12] dmesg: move get_boot_time() to lib/timeutils
Date: Sun, 27 Apr 2014 21:05:28 +0100	[thread overview]
Message-ID: <1398629138-31718-3-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1398629138-31718-1-git-send-email-kerolasa@iki.fi>

In future the last(1) will use get_boot_time() as well.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 include/timeutils.h |  1 +
 lib/timeutils.c     | 29 +++++++++++++++++++++++++++++
 sys-utils/dmesg.c   | 27 ---------------------------
 3 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/include/timeutils.h b/include/timeutils.h
index bcae613..b9ba0f7 100644
--- a/include/timeutils.h
+++ b/include/timeutils.h
@@ -51,5 +51,6 @@ typedef uint64_t nsec_t;
 #define FORMAT_TIMESPAN_MAX 64
 
 int parse_timestamp(const char *t, usec_t *usec);
+int get_boot_time(struct timeval *boot_time);
 
 #endif /* UTIL_LINUX_TIME_UTIL_H */
diff --git a/lib/timeutils.c b/lib/timeutils.c
index 7fe6218..c1b632e 100644
--- a/lib/timeutils.c
+++ b/lib/timeutils.c
@@ -21,9 +21,12 @@
 #include <assert.h>
 #include <ctype.h>
 #include <string.h>
+#include <sys/sysinfo.h>
+#include <sys/time.h>
 #include <time.h>
 
 #include "c.h"
+#include "nls.h"
 #include "strutils.h"
 #include "timeutils.h"
 
@@ -336,3 +339,29 @@ int parse_timestamp(const char *t, usec_t *usec)
 
 	return 0;
 }
+
+int get_boot_time(struct timeval *boot_time)
+{
+	struct timespec hires_uptime;
+	struct timeval lores_uptime, now;
+	struct sysinfo info;
+
+	if (gettimeofday(&now, NULL) != 0) {
+		warn(_("gettimeofday failed"));
+		return -errno;
+	}
+#ifdef CLOCK_BOOTTIME
+	if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) {
+		TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
+		timersub(&now, &lores_uptime, boot_time);
+		return 0;
+	}
+#endif
+	/* fallback */
+	if (sysinfo(&info) != 0)
+		warn(_("sysinfo failed"));
+
+	boot_time->tv_sec = now.tv_sec - info.uptime;
+	boot_time->tv_usec = 0;
+	return 0;
+}
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index df68c84..196cc61 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -472,33 +472,6 @@ static int get_syslog_buffer_size(void)
 	return n > 0 ? n : 0;
 }
 
-static int get_boot_time(struct timeval *boot_time)
-{
-	struct timespec hires_uptime;
-	struct timeval lores_uptime, now;
-	struct sysinfo info;
-
-	if (gettimeofday(&now, NULL) != 0) {
-		warn(_("gettimeofday failed"));
-		return -errno;
-	}
-
-#ifdef CLOCK_BOOTTIME
-	if (clock_gettime(CLOCK_BOOTTIME, &hires_uptime) == 0) {
-		TIMESPEC_TO_TIMEVAL(&lores_uptime, &hires_uptime);
-		timersub(&now, &lores_uptime, boot_time);
-		return 0;
-	}
-#endif
-	/* fallback */
-	if (sysinfo(&info) != 0)
-		warn(_("sysinfo failed"));
-
-	boot_time->tv_sec = now.tv_sec - info.uptime;
-	boot_time->tv_usec = 0;
-	return 0;
-}
-
 /*
  * Reads messages from regular file by mmap
  */
-- 
1.9.2


  parent reply	other threads:[~2014-04-27 20:05 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-27 20:05 [PATCH 00/12] pull: mostly swap command clarifications Sami Kerola
2014-04-27 20:05 ` [PATCH 01/12] include/xalloc: ensure arithmetics overflow cannot happen Sami Kerola
2014-04-27 20:45   ` Bernhard Voelker
2014-04-28  6:52   ` Karel Zak
2014-04-28  8:42     ` Sami Kerola
2014-04-27 20:05 ` Sami Kerola [this message]
2014-05-06 10:00   ` [PATCH 02/12] dmesg: move get_boot_time() to lib/timeutils Ruediger Meier
2014-05-06 11:20     ` Karel Zak
2014-05-06 13:42       ` Ruediger Meier
2014-05-06 14:40         ` Karel Zak
2014-05-06 16:03           ` Ruediger Meier
2014-05-07  9:52             ` Karel Zak
2014-05-07 14:04               ` Ruediger Meier
2014-04-27 20:05 ` [PATCH 03/12] last: fix is_phantom() detection Sami Kerola
2014-04-27 20:05 ` [PATCH 04/12] include/c.h: add macro to print definitions as string Sami Kerola
2014-04-27 20:05 ` [PATCH 05/12] mkswap, swaplabel: move version number to header Sami Kerola
2014-04-27 20:05 ` [PATCH 06/12] mkswap: remove legacy swap structure Sami Kerola
2014-04-27 20:05 ` [PATCH 07/12] include/swapheader.h: ensure type sizes Sami Kerola
2014-04-27 20:05 ` [PATCH 08/12] swapon: swaps with legacy version label are not supported Sami Kerola
2014-04-28  8:37   ` Benno Schulenberg
2014-04-28  8:44     ` Sami Kerola
2014-04-28  9:00       ` Karel Zak
2014-04-27 20:05 ` [PATCH 09/12] swapon, swapheader, mkswap: move swap signature to header Sami Kerola
2014-04-27 20:05 ` [PATCH 10/12] libsmartcols: remove ununsed assignment Sami Kerola
2014-04-28  9:02   ` Karel Zak
2014-04-27 20:05 ` [PATCH 11/12] lib/timeutils: fix memory leak Sami Kerola
2014-04-27 20:05 ` [PATCH 12/12] lib/pager: use names when referring to standard file descriptors Sami Kerola
2014-04-29 21:05 ` [PATCH 00/12] pull: mostly swap command clarifications Sami Kerola
2014-04-29 21:46   ` Bernhard Voelker
2014-05-06  8:36 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1398629138-31718-3-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox