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

Bug#1051268: apt.systemd.daily: Time stamp handling is inaccurate



Package: apt
Version: 2.6.1
Severity: normal
Tags: patch
X-Debbugs-Cc: martin.lottermoser@htp-tel.de

Dear Maintainers,

the initial comments in apt.systemd.daily state:

  All of the n-days interval options also accept the suffixes
  s for seconds, m for minutes, h for hours, d for days or
  the "always" value to do the action for every job run,
  which can be used with systemd OnCalendar overrides to
  define custom schedules for the apt update/upgrade jobs.

This strongly suggests that a granularity below one day is possible.
That, however, is false because the check_stamp() function in that file
converts the modification date of the time stamp file as well as the
current time by first moving each back to the next 00:00 hours in the
local timezone before comparing the two. That gives an effective
granularity of one day; in particular, the resulting difference
(calculated by the script in seconds!) may be incorrect by up to
23 hours, 59 minutes and 59 seconds in either direction, an interval
of almost two days.

No motivation is given for this shift to midnight and in addition
comments in the file state that the calculation might fail sometimes in
certain timezones.

None of this seems necessary and the shifts should therefore be eliminated.
A proposed patch is attached which also removes two unnecessary "date"
calls used for recomputing the already-set variable "now".

-- System Information:
Debian Release: 12.1
  APT prefers stable-security
  APT policy: (500, 'stable-security'), (500, 'stable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.1.0-11-amd64 (SMP w/4 CPU threads; PREEMPT)
Locale: LANG=de_DE.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages apt depends on:
ii  adduser                 3.134
ii  debian-archive-keyring  2023.3
ii  gpgv                    2.2.40-1.1
ii  libapt-pkg6.0           2.6.1
ii  libc6                   2.36-9+deb12u1
ii  libgcc-s1               12.2.0-14
ii  libgnutls30             3.7.9-2
ii  libseccomp2             2.5.4-1+b3
ii  libstdc++6              12.2.0-14
ii  libsystemd0             252.12-1~deb12u1

Versions of packages apt recommends:
ii  ca-certificates  20230311

Versions of packages apt suggests:
pn  apt-doc         <none>
ii  aptitude        0.8.13-5
ii  dpkg-dev        1.21.22
ii  gnupg           2.2.40-1.1
ii  gnupg1          1.4.23-1.1+b1
ii  powermgmt-base  1.37
ii  synaptic        0.91.3

-- no debconf information

-- debsums errors found:
debsums: changed file /usr/lib/apt/apt.systemd.daily (from apt package)
[Changes as proposed in this report.]

Regards,
Martin Lottermoser
-- 
Martin Lottermoser             martin.lottermoser@htp-tel.de
Greifswaldstrasse 28
38124 Braunschweig             http://home.htp-tel.de/lottermose2
Germany                        Telephone: +49 531 6802747
Patch proposed by Martin Lottermoser for apt.systemd.daily in apt 2.6.1.

--- apt.systemd.daily.orig	2023-05-25 16:11:37.000000000 +0200
+++ apt.systemd.daily	2023-08-28 08:29:06.874803852 +0200
@@ -102,25 +102,14 @@
         return 0
     fi
 
-    # compare midnight today to midnight the day the stamp was updated
     stamp_file="$stamp"
-    stamp=$(date --date="$(date -r "$stamp_file" --iso-8601)" +%s 2>/dev/null)
+    stamp=$(date -r "$stamp_file" +%s 2>/dev/null)
     if [ "$?" != "0" ]; then
-        # Due to some timezones returning 'invalid date' for midnight on
-        # certain dates (e.g. America/Sao_Paulo), if date returns with error
-        # remove the stamp file and return 0. See coreutils bug:
-        # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
         rm -f "$stamp_file"
         return 0
     fi
 
-    now=$(date --date="$(date --iso-8601)" +%s 2>/dev/null)
-    if [ "$?" != "0" ]; then
-        # As above, due to some timezones returning 'invalid date' for midnight
-        # on certain dates (e.g. America/Sao_Paulo), if date returns with error
-        # return 0.
-        return 0
-    fi
+    # ${now} has already been set in the main section.
 
     delta=$((now-stamp))
 
@@ -200,8 +189,7 @@
 	# maxSize is in MB
 	MaxSize=$((MaxSize*1024))
 
-	#get current time
-	now=$(date --date="$(date --iso-8601)" +%s)
+	# MinAge is in days
 	MinAge=$((MinAge*24*60*60))
 
 	# reverse-sort by mtime
@@ -219,6 +207,7 @@
 		# check both ctime and mtime 
 		mtime=$(stat -c %Y "$file")
 		ctime=$(stat -c %Z "$file")
+		# ${now} has already been set in the main section.
 		if [ "$mtime" -gt "$ctime" ]; then
 		    delta=$((now-mtime))
 		else
@@ -388,6 +377,10 @@
 
 # Global current time in seconds since 1970-01-01 00:00:00 UTC
 now=$(date +%s)
+if [ $? -ne 0 ]; then
+    echo "E: Cannot determine current time" >&2
+    exit 1
+fi
 
 # Support old Archive for compatibility.
 # Document only Periodic for all controlling parameters of this script.

Attachment: signature.asc
Description: PGP signature


Reply to: