[PATCH] hwclock: always reads hardware clock

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It's a pity that hwclock first tries to read the clock when running

	hwclock --systohc --noadjfile --utc

and exits as this fails.  I cannot see a reason to read first in that
case.

Old version:

	# hwclock --systohc --noadjfile --utc --debug

	hwclock from util-linux-ng 2.14
	Using /dev interface to clock.
	Assuming hardware clock is kept in UTC time.
	Waiting for clock tick...
	/dev/rtc does not have interrupt functions. Waiting in loop for time
	from /dev/rtc to change
	...got clock tick
	Time read from Hardware Clock: 2008/06/17 11:18:24
	Hw clock time : 2008/06/17 11:18:24 = 1213701504 seconds since 1969
	Time elapsed since reference time has been 0.904855 seconds.
	Delaying further to reach the next full second.
	Setting Hardware Clock to 11:18:24 = 1213701504 seconds since 1969
	ioctl(RTC_SET_TIME) was successful.

New version:

	# hwclock --systohc --noadjfile --utc --debug

	hwclock from util-linux-ng 2.14
	Using /dev interface to clock.
	Assuming hardware clock is kept in UTC time.
	Time elapsed since reference time has been 0.572151 seconds.
	Delaying further to reach the next full second.
	Setting Hardware Clock to 11:18:52 = 1213701532 seconds since 1969
	ioctl(RTC_SET_TIME) was successful.

Addresses-Debian-Bug: #478663
Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>
---
 hwclock/hwclock.c |   26 +++++++++++++++++---------
 hwclock/rtc.c     |    2 +-
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/hwclock/hwclock.c b/hwclock/hwclock.c
index 6660a09..1fb0058 100644
--- a/hwclock/hwclock.c
+++ b/hwclock/hwclock.c
@@ -425,7 +425,7 @@ mktime_tz(struct tm tm, const bool universal,
 }
 
 
-static void
+static int
 read_hardware_clock(const bool universal, bool *valid_p, time_t *systime_p){
 /*----------------------------------------------------------------------------
   Read the hardware clock and return the current time via <tm> argument.
@@ -436,6 +436,8 @@ read_hardware_clock(const bool universal, bool *valid_p, time_t *systime_p){
   int err;
 
   err = ur->read_hardware_clock(&tm);
+  if (err)
+	  return err;
 
   if (badyear)
     read_date_from_file(&tm);
@@ -445,6 +447,8 @@ read_hardware_clock(const bool universal, bool *valid_p, time_t *systime_p){
 	    tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
             tm.tm_hour, tm.tm_min, tm.tm_sec);
   mktime_tz(tm, universal, valid_p, systime_p);
+
+  return 0;
 }
 
 
@@ -1073,27 +1077,31 @@ manipulate_clock(const bool show, const bool adjust, const bool noadjfile,
 	adjtime.dirty = TRUE;
       }
 
-      rc = synchronize_to_clock_tick();  /* this takes up to 1 second */
-      if (rc)
-	      return rc;
-
       {
         struct timeval read_time;
           /* The time at which we read the Hardware Clock */
 
-        bool hclock_valid;
+        bool hclock_valid = FALSE;
           /* The Hardware Clock gives us a valid time, or at least something
              close enough to fool mktime().
              */
 
-        time_t hclocktime;
+        time_t hclocktime = 0;
           /* The time the hardware clock had just after we
              synchronized to its next clock tick when we started up.
              Defined only if hclock_valid is true.
              */
 
-        gettimeofday(&read_time, NULL);
-        read_hardware_clock(universal, &hclock_valid, &hclocktime);
+       if (show || adjust || hctosys || !noadjfile) {
+          /* data from HW-clock are required */
+          rc = synchronize_to_clock_tick();
+          if (rc)
+             return EX_IOERR;
+          gettimeofday(&read_time, NULL);
+          rc = read_hardware_clock(universal, &hclock_valid, &hclocktime);
+          if (rc)
+             return EX_IOERR;
+	}
 
         if (show) {
           display_time(hclock_valid, hclocktime,
diff --git a/hwclock/rtc.c b/hwclock/rtc.c
index 46a5b52..2acde81 100644
--- a/hwclock/rtc.c
+++ b/hwclock/rtc.c
@@ -163,7 +163,7 @@ do_rtc_read_ioctl(int rtc_fd, struct tm *tm) {
 		perror(ioctlname);
 		fprintf(stderr, _("ioctl() to %s to read the time failed.\n"),
 			rtc_dev_name);
-		hwclock_exit(EX_IOERR);
+		return -1;
 	}
 
 	tm->tm_isdst = -1;          /* don't know whether it's dst */
-- 
1.5.4.1

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux