Commit fab31bbd authored by jan.koester's avatar jan.koester
Browse files

time: /etc/localtime als Fallback, geänderte Zeitzone ohne Neustart

Ohne /etc/time.yaml gilt /etc/localtime (TZif-Datei oder Symlink nach
zoneinfo). Bei nicht gesetztem TZ lädt localtime()/mktime() die Zone neu,
wenn sich /etc/time.yaml oder /etc/localtime geändert hat, so übernimmt
z. B. die Uhr des Desktops eine neue Zeitzone sofort. g_tz ist dafür
mit einer Spinlock geschützt; die alte Übergangstabelle wird erst beim
nächsten Neuladen freigegeben (tm_zone zeigt hinein). TZ=:Zone und
TZ=/pfad/zur/TZif-Datei gehen auch.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 88a12e70
Loading
Loading
Loading
Loading
+7 −1
Changes for include/time.h: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -49,9 +49,15 @@ struct itimerspec {
 *      both the Mm.w.d and Julian-day date forms) -- and a TZ value
 *      containing '/' (e.g. "Europe/Berlin") names a real zone loaded
 *      per point 3 below rather than being parsed as a POSIX rule.
 *      A leading ':' is skipped; ":/path" or "/path" is a TZif file.
 *   2. /etc/time.yaml: either a fixed name/offset (+ optional POSIX
 *      DST rule fields), or a `zone:` key naming a real zone per
 *      point 3.
 *      point 3 (FenrirOS: written by syslite, `syslitectl timezone`).
 *      Without /etc/time.yaml: /etc/localtime (a TZif file or a
 *      symlink into zoneinfo, as other systems have it).
 *      With TZ unset, a changed /etc/time.yaml or /etc/localtime is
 *      loaded again by the next localtime()/mktime(), so running
 *      programs follow a new time zone.
 *   3. A real IANA tzdata database: actual /usr/share/zoneinfo/<zone>
 *      TZif files (RFC 8536, v1/v2/v3) are parsed for their explicit
 *      transition table, with the v2/v3 POSIX-string footer used to
+95 −9
Changes for src/linux/time.cpp: 95 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -143,6 +143,30 @@ struct zone_transition {

zone_transition* g_transitions = nullptr;
int g_ntransitions = 0;
/* The table before the current one: tm_zone of a struct tm filled just
 * before a reload still points into it, so a table is freed only one
 * reload later. */
zone_transition* g_retired_transitions = nullptr;

void retire_transitions() {
    delete[] g_retired_transitions;
    g_retired_transitions = g_transitions;
    g_transitions = nullptr;
    g_ntransitions = 0;
}

/* Guards g_tz/g_transitions: the time zone is reloaded when the
 * configuration changes (see ensure_tz_ready()), maybe while another
 * thread is in localtime_r(). */
int g_tz_lock = 0;

struct tz_guard {
    tz_guard() {
        while (__atomic_exchange_n(&g_tz_lock, 1, __ATOMIC_ACQUIRE))
            while (__atomic_load_n(&g_tz_lock, __ATOMIC_RELAXED)) { }
    }
    ~tz_guard() { __atomic_store_n(&g_tz_lock, 0, __ATOMIC_RELEASE); }
};

const char* time_yaml_path() {
    const char* p = secure_getenv("SYSTEMPP_TIME_YAML");
@@ -154,6 +178,14 @@ const char* zoneinfo_dir() {
    return p ? p : "/usr/share/zoneinfo";
}

/* The zone of programs that don't read /etc/time.yaml (ICU, Java, ...):
 * a symlink into zoneinfo or a copied TZif file. Read here too when
 * there is no /etc/time.yaml. */
const char* localtime_path() {
    const char* p = secure_getenv("SYSTEMPP_LOCALTIME");
    return p ? p : "/etc/localtime";
}

/* Parses "[+-]hh[:mm[:ss]]" (POSIX's own west-positive sign
 * convention) into east-positive seconds. */
bool parse_offset(const char*& p, long& out) {
@@ -372,7 +404,7 @@ bool parse_tzif(const unsigned char* buf, size_t len) {
    const char* designations = (const char*)(buf + pos);
    pos += h.charcnt;

    delete[] g_transitions;
    retire_transitions();
    g_transitions = new zone_transition[h.timecnt];
    g_ntransitions = (int)h.timecnt;
    for (uint32_t i = 0; i < h.timecnt; i++) {
@@ -426,8 +458,12 @@ bool parse_tzif(const unsigned char* buf, size_t len) {
    return true;
}

/* A zone name below zoneinfo_dir(), or an absolute path to a TZif file */
bool load_zoneinfo(const char* zone_name) {
    char path[512];
    if (zone_name[0] == '/')
        snprintf(path, sizeof path, "%s", zone_name);
    else
        snprintf(path, sizeof path, "%s/%s", zoneinfo_dir(), zone_name);

    int fd = open(path, O_RDONLY, 0);
@@ -523,17 +559,53 @@ void load_from_yaml() {
/* The TZ value the current g_tz was loaded for (null: TZ unset) */
char* g_tz_env = nullptr;

/* What the configuration files looked like when g_tz was loaded from
 * them (TZ unset): a changed /etc/time.yaml or /etc/localtime (a new
 * time zone set while the program runs, e.g. the clock of the desktop)
 * is loaded again by the next localtime(). */
struct tz_stamp {
    dev_t dev; ino_t ino; long long mtime, ctime; off_t size;
    ino_t link_ino; long long link_ctime;
    bool yaml;
};
tz_stamp g_tz_stamp = {};

tz_stamp config_stamp() {
    tz_stamp s = {};
    struct stat st;
    if (stat(time_yaml_path(), &st) == 0) {
        s.yaml = true;
    } else if (lstat(localtime_path(), &st) == 0) {
        s.link_ino = st.st_ino;
        s.link_ctime = (long long)st.st_ctim.tv_sec * 1000000000LL + st.st_ctim.tv_nsec;
        if (stat(localtime_path(), &st) != 0) return s;
    } else {
        return s;
    }
    s.dev = st.st_dev;
    s.ino = st.st_ino;
    s.mtime = (long long)st.st_mtim.tv_sec * 1000000000LL + st.st_mtim.tv_nsec;
    s.ctime = (long long)st.st_ctim.tv_sec * 1000000000LL + st.st_ctim.tv_nsec;
    s.size = st.st_size;
    return s;
}

bool same_stamp(const tz_stamp& a, const tz_stamp& b) {
    return a.dev == b.dev && a.ino == b.ino && a.mtime == b.mtime && a.ctime == b.ctime &&
           a.size == b.size && a.link_ino == b.link_ino && a.link_ctime == b.link_ctime && a.yaml == b.yaml;
}

/* called with g_tz_lock held */
void do_tzset() {
    const char* env = getenv("TZ");
    free(g_tz_env);
    g_tz_env = env ? strdup(env) : nullptr;

    g_tz = TZ_STATE_DEFAULT;
    delete[] g_transitions;
    g_transitions = nullptr;
    g_ntransitions = 0;
    retire_transitions();

    const char* tz = getenv("TZ");
    if (tz && *tz == ':') tz++;  /* POSIX ":characters": here a zone or a file */
    if (tz && *tz) {
        /* A TZ value naming a real zone (e.g. "Europe/Berlin") always
         * contains '/' -- a plain POSIX rule string ("EST5EDT,...")
@@ -542,7 +614,11 @@ void do_tzset() {
        bool loaded = strchr(tz, '/') && load_zoneinfo(tz);
        if (!loaded && !parse_tz_env(tz, g_tz)) g_tz = TZ_STATE_DEFAULT;
    } else {
        g_tz_stamp = config_stamp();
        if (g_tz_stamp.yaml)
            load_from_yaml();
        else if (!load_zoneinfo(localtime_path()))
            g_tz = TZ_STATE_DEFAULT;
    }

    tzname[0] = g_tz.std_name;
@@ -554,10 +630,14 @@ void do_tzset() {
/* localtime(), mktime(), strftime(), ...: as if tzset() had been called
 * (POSIX), so a changed TZ takes effect -- reloaded only when its value
 * differs from the one g_tz was loaded for, like glibc. */
/* called with g_tz_lock held */
void ensure_tz_ready() {
    if (g_tz_ready) {
        const char* env = getenv("TZ");
        if (env ? (g_tz_env && strcmp(env, g_tz_env) == 0) : !g_tz_env) return;
        if (env ? (g_tz_env && strcmp(env, g_tz_env) == 0) : !g_tz_env) {
            if (env) return;
            if (same_stamp(config_stamp(), g_tz_stamp)) return;
        }
    }
    g_tz_ready = true;
    do_tzset();
@@ -829,9 +909,12 @@ extern "C" {
    }

    struct tm* localtime_r(const time_t* timer, struct tm* result) {
        ensure_tz_ready();
        long offset; const char* name; int isdst;
        {
            tz_guard lock;
            ensure_tz_ready();
            compute_offset(*timer, offset, name, isdst);
        }
        fill_tm_from_epoch(*timer + offset, result);
        result->tm_gmtoff = offset;
        result->tm_zone = name;
@@ -876,8 +959,6 @@ extern "C" {
         * every real implementation, this is inherently ambiguous for
         * the hour of wall-clock time that a DST transition skips or
         * repeats -- not something any mktime() fully resolves. */
        ensure_tz_ready();

        int y = tmv->tm_year + 1900;
        int m = tmv->tm_mon + 1;
        while (m > 12) { m -= 12; y++; }
@@ -886,9 +967,13 @@ extern "C" {
        long days = days_from_civil(y, (unsigned)m, (unsigned)tmv->tm_mday);
        time_t as_if_utc = (time_t)days * 86400 + tmv->tm_hour * 3600 + tmv->tm_min * 60 + tmv->tm_sec;

        time_t guess = as_if_utc - g_tz.std_offset;
        long offset; const char* name; int isdst;
        {
            tz_guard lock;
            ensure_tz_ready();
            time_t guess = as_if_utc - g_tz.std_offset;
            compute_offset(guess, offset, name, isdst);
        }
        time_t real_utc = as_if_utc - offset;

        localtime_r(&real_utc, tmv);
@@ -1278,6 +1363,7 @@ extern "C" {
    }

    void tzset(void) {
        tz_guard lock;
        do_tzset();
        g_tz_ready = true;
    }