Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame^] | 1 | From ee85d3967ea09b215fcea5efdd90bbbf5e74a681 Mon Sep 17 00:00:00 2001 |
| 2 | From: Karel Zak <kzak@redhat.com> |
| 3 | Date: Wed, 19 Feb 2020 15:50:47 +0100 |
| 4 | Subject: [PATCH] hwclock: fix for glibc 2.31 settimeofday() |
| 5 | |
| 6 | glibc announce: |
| 7 | ... settimeofday can no longer be used to set the time and the offset |
| 8 | simultaneously. If both of its two arguments are non-null, the call |
| 9 | will fail (setting errno to EINVAL). |
| 10 | |
| 11 | It means we need to call settimeofday(NULL, tz) and settimeofday(tv, NULL). |
| 12 | |
| 13 | Unfortunately, settimeofday(NULL, tz) has very special warp-clock |
| 14 | semantic if used as the very first settimeofday() call. It means we |
| 15 | have to be sure that we do not touch warp-clock if we need only need |
| 16 | to modify system TZ. So, let's always call settimeofday(NULL, 0) |
| 17 | before settimeofday(NULL, tz) for UTC rtc mode when modify system TZ. |
| 18 | |
| 19 | Upstream-Status: Backport [https://github.com/karelzak/util-linux/commit/ee85d3967ea09b215fcea5efdd90bbbf5e74a681] |
| 20 | |
| 21 | CC: J William Piggott <elseifthen@gmx.com> |
| 22 | Signed-off-by: Karel Zak <kzak@redhat.com> |
| 23 | Addresses: https://github.com/karelzak/util-linux/issues/957 |
| 24 | Signed-off-by: Liwei Song <liwei.song@windriver.com> |
| 25 | --- |
| 26 | sys-utils/hwclock.c | 49 ++++++++++++++++++++++++++------------------- |
| 27 | 1 file changed, 28 insertions(+), 21 deletions(-) |
| 28 | |
| 29 | diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c |
| 30 | index e736da7179f8..16576bc186ff 100644 |
| 31 | --- a/sys-utils/hwclock.c |
| 32 | +++ b/sys-utils/hwclock.c |
| 33 | @@ -658,6 +658,9 @@ display_time(struct timeval hwctime) |
| 34 | * PCIL: persistent_clock_is_local, sets the "11 minute mode" timescale. |
| 35 | * firsttime: locks the warp_clock function (initialized to 1 at boot). |
| 36 | * |
| 37 | + * Note that very first settimeofday(NULL, tz) modifies warp-clock as well as |
| 38 | + * system TZ. |
| 39 | + * |
| 40 | * +---------------------------------------------------------------------------+ |
| 41 | * | op | RTC scale | settimeofday calls | |
| 42 | * |---------|-----------|-----------------------------------------------------| |
| 43 | @@ -675,41 +678,45 @@ set_system_clock(const struct hwclock_control *ctl, |
| 44 | struct tm broken; |
| 45 | int minuteswest; |
| 46 | int rc = 0; |
| 47 | - const struct timezone tz_utc = { 0 }; |
| 48 | |
| 49 | localtime_r(&newtime.tv_sec, &broken); |
| 50 | minuteswest = -get_gmtoff(&broken) / 60; |
| 51 | |
| 52 | if (ctl->verbose) { |
| 53 | - if (ctl->hctosys && !ctl->universal) |
| 54 | - printf(_("Calling settimeofday(NULL, %d) to set " |
| 55 | - "persistent_clock_is_local.\n"), minuteswest); |
| 56 | - if (ctl->systz && ctl->universal) |
| 57 | + if (ctl->universal) |
| 58 | puts(_("Calling settimeofday(NULL, 0) " |
| 59 | - "to lock the warp function.")); |
| 60 | + "to lock the warp function.")); |
| 61 | + else |
| 62 | + printf(_("Calling settimeofday(NULL, %d) to set " |
| 63 | + "persistent_clock_is_local and " |
| 64 | + "the kernel timezone.\n"), minuteswest); |
| 65 | + |
| 66 | + if (ctl->universal && minuteswest) |
| 67 | + printf(_("Calling settimeofday(NULL, %d) to set " |
| 68 | + "the kernel timezone.\n"), minuteswest); |
| 69 | + |
| 70 | if (ctl->hctosys) |
| 71 | - printf(_("Calling settimeofday(%ld.%06ld, %d)\n"), |
| 72 | - newtime.tv_sec, newtime.tv_usec, minuteswest); |
| 73 | - else { |
| 74 | - printf(_("Calling settimeofday(NULL, %d) "), minuteswest); |
| 75 | - if (ctl->universal) |
| 76 | - puts(_("to set the kernel timezone.")); |
| 77 | - else |
| 78 | - puts(_("to warp System time.")); |
| 79 | - } |
| 80 | + printf(_("Calling settimeofday(%ld.%06ld, 0) to set " |
| 81 | + "the kernel time.\n"), newtime.tv_sec, newtime.tv_usec); |
| 82 | } |
| 83 | |
| 84 | if (!ctl->testing) { |
| 85 | + const struct timezone tz_utc = { 0 }; |
| 86 | const struct timezone tz = { minuteswest }; |
| 87 | |
| 88 | - if (ctl->hctosys && !ctl->universal) /* set PCIL */ |
| 89 | + /* warp-clock */ |
| 90 | + if (ctl->universal) |
| 91 | + rc = settimeofday(NULL, &tz_utc); /* lock to UTC */ |
| 92 | + else |
| 93 | + rc = settimeofday(NULL, &tz); /* set PCIL and TZ */ |
| 94 | + |
| 95 | + /* set timezone */ |
| 96 | + if (!rc && ctl->universal && minuteswest) |
| 97 | rc = settimeofday(NULL, &tz); |
| 98 | - if (ctl->systz && ctl->universal) /* lock warp_clock */ |
| 99 | - rc = settimeofday(NULL, &tz_utc); |
| 100 | + |
| 101 | + /* set time */ |
| 102 | if (!rc && ctl->hctosys) |
| 103 | - rc = settimeofday(&newtime, &tz); |
| 104 | - else if (!rc) |
| 105 | - rc = settimeofday(NULL, &tz); |
| 106 | + rc = settimeofday(&newtime, NULL); |
| 107 | |
| 108 | if (rc) { |
| 109 | warn(_("settimeofday() failed")); |
| 110 | -- |
| 111 | 2.17.1 |
| 112 | |