Detect if the clock seems to be going backwards
authorSteve McIntyre <steve@einval.com>
Mon, 5 Mar 2012 16:28:18 +0000 (16:28 +0000)
committerSteve McIntyre <steve@einval.com>
Mon, 5 Mar 2012 16:28:18 +0000 (16:28 +0000)
Check if we're trying to set the clock backwards. If so, bail unless
we're explicitly told to in a config file. Thanks to Barak
A. Pearlmutter for the idea and (some of) the code. Closes: #661146.

debian/changelog
debian/fake-hwclock.init
debian/fake-hwclock.install
etc/default/fake-hwclock [new file with mode: 0644]
fake-hwclock

index 61255cc..86d9795 100644 (file)
@@ -4,6 +4,9 @@ fake-hwclock (0.4) UNRELEASE; urgency=low
   * Add vcs fields to debian/control.
   * Fix debian/fake-hwclock.install to match the move to /sbin; move to
     using dh_install.
+  * Check if we're trying to set the clock backwards. If so, bail unless
+    we're explicitly told to in a config file. Thanks to Barak
+    A. Pearlmutter for the idea and (some of) the code. Closes: #661146.
 
  -- Steve McIntyre <93sam@debian.org>  Wed, 22 Feb 2012 10:20:26 +0000
 
index 36d0bc1..e8cf653 100644 (file)
 
 set -e
 
+PARAM=/etc/default/fake-hwclock
+if [ -f $PARAM ]; then
+    . "$PARAM"
+fi
+
 case "${1:-}" in
   stop|reload|restart|force-reload)
         echo "Stopping fake hwclock: saving system time."
@@ -21,7 +26,7 @@ case "${1:-}" in
 
   start)
         echo "Starting fake hwclock: loading system time."
-        fake-hwclock load;;
+        fake-hwclock load $FORCE ;;
 
   *)
         echo "Usage: ${0:-} {start|stop|status|restart|reload|force-reload}" >&2
index 2745373..089e8aa 100644 (file)
@@ -1 +1,3 @@
 fake-hwclock sbin
+etc/default/fake-hwclock etc/default
+
diff --git a/etc/default/fake-hwclock b/etc/default/fake-hwclock
new file mode 100644 (file)
index 0000000..fd66761
--- /dev/null
@@ -0,0 +1,2 @@
+# Uncomment to set clock even if saved value appears to be in the past
+#FORCE=force
index f36a615..e9ba7f1 100755 (executable)
@@ -19,16 +19,33 @@ if [ "$COMMAND"x = ""x ] ; then
     COMMAND="save"
 fi
 
+FORCE=false
+if [ "$2"x = "force"x ] ; then
+    FORCE=true
+fi
+
 case $COMMAND in
     save)
-        date -u '+%Y-%m-%d %H:%M:%S' > $FILE;;
+        date -u '+%Y-%m-%d %H:%M:%S' > $FILE
+        ;;
     load)
         if [ -e $FILE ] ; then
-            date -u -s "`cat $FILE`"
+            SAVED="$(cat $FILE)"
+            SAVED_SEC=$(date -u -d "$SAVED" '+%s')
+            NOW_SEC=$(date -u '+%s')
+            if $FORCE || [ $NOW_SEC -le $SAVED_SEC ] ; then
+                date -u -s "$SAVED"
+            else
+                echo "Current system time: $(date -u '+%Y-%m-%d %H:%M:%S')"
+                echo "fake-hwclock saved clock information is in the past: $SAVED"
+                echo "To set system time to this saved clock anyway, use \"force\""
+            fi      
         else
             echo "Unable to read saved clock information: $FILE does not exist"
-        fi;;
+        fi
+        ;;
     *)
         echo $0: Unknown command $COMMAND
-        exit 1;;
+        exit 1
+        ;;
 esac