Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systemd timers treat all timezones as UTC #105049

Closed
ncfavier opened this issue Nov 26, 2020 · 11 comments · Fixed by #150922
Closed

systemd timers treat all timezones as UTC #105049

ncfavier opened this issue Nov 26, 2020 · 11 comments · Fixed by #150922
Labels
0.kind: bug Something is broken 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS

Comments

@ncfavier
Copy link
Member

Describe the bug

The OnCalendar field of systemd timer units is treated as UTC, even if a different timezone is specified.

To Reproduce

  1. Declare a trivial service and make it start at midnight on some non-UTC timezone:
{                                                                                            
  systemd.services.foo = {                                                                   
    script = "true";                                                                         
    startAt = "00:00:00 Pacific/Auckland";                                                      
  };                                                                                         
}
  1. Run systemctl status foo.timer and observe that the Trigger field says midnight UTC (in my case, Fri 2020-11-27 01:00:00 CET).

Expected behavior

In this case, the Trigger field should say Fri 2020-11-27 12:00:00 CET (or Fri 2020-11-27 11:00:00 UTC).

Additional context

  • I have tested this with two different non-UTC timezones.
  • systemd-analyze calendar "00:00:00 Pacific/Auckland" on the same machine gives the correct result, so this is linked to timer units.
  • I am using the systemd package from the NixOS 20.09 release, version 246.
  • I could not reproduce the issue on an Arch Linux server (systemd 246.4-1-arch), so it's (probably) a NixOS problem.

Notify maintainers

@edolstra @andir @flokli @kloenk

Metadata

 - system: `"x86_64-linux"`
 - host os: `Linux 5.4.64, NixOS, 20.09.20201125.7a7bae5 (Nightingale)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.4pre20201118_79aa7d9`
 - channels(root): `"nixos-20.09.1721.896270d629e"`
 - channels(n): `"home-manager"`
 - nixpkgs: `/nix/store/c0ri3lxx3ymf3spqlhz7dkycmf8fwd3w-source`
@ncfavier ncfavier added the 0.kind: bug Something is broken label Nov 26, 2020
@flokli
Copy link
Contributor

flokli commented Nov 26, 2020

Maybe our patch in 0009-Change-usr-share-zoneinfo-to-etc-zoneinfo.patch (from #80038 (comment)) isn't sufficient and this needs to be patched in another location, too?

@ncfavier
Copy link
Member Author

Note that if I specify a timezone that doesn't exist, I get an error on activation. So presumably it can find the timezone info, it just doesn't respect it.

@flokli
Copy link
Contributor

flokli commented Nov 26, 2020

Can you reproduce on non-NixOS?

@ncfavier
Copy link
Member Author

Nope, see the fourth bullet point under Additional context.

@veprbl veprbl added the 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS label Nov 27, 2020
@flokli
Copy link
Contributor

flokli commented Dec 20, 2020

I was able to reproduce this on NixOS, and verified it works correctly on Archlinux.

I still don't understand why it behaves the way it does (it seems we did properly patch all occurences this might touch).

This might require some deeper digging into the codebase, and adding some logs along the way to see what's going on.

@stale
Copy link

stale bot commented Jun 18, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 18, 2021
@flokli
Copy link
Contributor

flokli commented Jun 18, 2021

Not stale, but help welcome ;-)

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 18, 2021
@teh
Copy link
Contributor

teh commented Oct 1, 2021

I haven't had time to test this but a cursory reading of the systemd code shows that it uses tzset():

https://github.com/systemd/systemd/blob/main/src/shared/calendarspec.c#L1411

tzset uses TZDIR. On a normal nixos system I get this:

$ export | rg TZ
declare -x TZDIR="/etc/zoneinfo

but pid 1 doesn't set TZDIR:

$ cat /proc/1/environ | tr '\00' '\n'
LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive
PATH=/run/current-system/systemd/lib/systemd:/nix/store/5kp6328y6wgws17anpl2wvc8w6yny0wc-nfs-utils-2.5.1/bin:/nix/store/nva6mlbsb1gv6q60xblk4dbspyg2l51j-e2fsprogs-1.45.6-bin/bin:/nix/store/gf088si5zlfryb0pmw5mx8x6ymf16lq2-btrfs-progs-5.9/bin:/nix/store/9kfd3p0s56bzqwzcapzm5wqdyqy17v72-dosfstools-4.1/bin
PWD=/
HOME=/root
SHLVL=0

@teh
Copy link
Contributor

teh commented Oct 3, 2021

I confirmed TZDIR is the issue by applying the following patch. I think my fix is wrong but I couldn't figure out where in systemd pid 1 I should set env vars so I did id in a function that I know will be called.

diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c
index bf24d8d5bb..2ae788f21e 100644
--- a/src/shared/calendarspec.c
+++ b/src/shared/calendarspec.c
@@ -901,6 +901,8 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
                 const char *e = NULL;
                 int j;

+		setenv("TZDIR", "/etc/zoneinfo", 1);
+
                 tzset();

                 /* Check if the local timezone was specified? */

without patch:

NEXT                        LEFT       LAST PASSED UNIT                         ACTIVATES
Sun 2021-10-03 09:18:23 UTC 14min left n/a  n/a    systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2021-10-04 09:00:00 UTC 23h left   n/a  n/a    oncalendar-test.timer        oncalendar-test.service

with patch:

NEXT                        LEFT          LAST PASSED UNIT                         ACTIVATES
Sun 2021-10-03 09:18:48 UTC 14min left    n/a  n/a    systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Sun 2021-10-03 13:00:00 UTC 3h 56min left n/a  n/a    oncalendar-test.timer        oncalendar-test.service

unit used:

    systemd.services.oncalendar-test = {
      description = "xx";
      serviceConfig.Type = "oneshot";
      wantedBy = [ "default.target" ];
      startAt = "09:00 America/New_York";
      script = "touch $HOME/xx";
    };```

@ncfavier
Copy link
Member Author

ncfavier commented Oct 3, 2021

I wonder if this is related to #65415 and #89487

@flokli
Copy link
Contributor

flokli commented Oct 3, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants