| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "common.h" |
| 4 | |
| 5 | bool netdata_ready = false; |
| 6 | |
| 7 | // ============================================================================ |
| 8 | // system timezone - thread-safe access |
| 9 | |
| 10 | static struct { |
| 11 | SPINLOCK spinlock; |
| 12 | char *timezone; |
| 13 | char *abbrev_timezone; |
| 14 | int32_t utc_offset; |
| 15 | } system_tz = { |
| 16 | .spinlock = SPINLOCK_INITIALIZER, |
| 17 | .timezone = NULL, |
| 18 | .abbrev_timezone = NULL, |
| 19 | .utc_offset = 0, |
| 20 | }; |
| 21 | |
| 22 | void system_tz_set(const char *timezone, const char *abbrev_timezone, int32_t utc_offset) { |
| 23 | // Own copies of both strings |
| 24 | char *new_tz = strdupz(timezone ? timezone : "unknown"); |
| 25 | char *new_abbrev = strdupz(abbrev_timezone ? abbrev_timezone : "UTC"); |
| 26 | |
| 27 | spinlock_lock(&system_tz.spinlock); |
| 28 | // All readers use system_tz_get() which holds this same spinlock and copies, |
| 29 | // so no reader can be using these pointers after we release the lock. |
| 30 | char *old_tz = system_tz.timezone; |
| 31 | char *old_abbrev = system_tz.abbrev_timezone; |
| 32 | system_tz.timezone = new_tz; |
| 33 | system_tz.abbrev_timezone = new_abbrev; |
| 34 | system_tz.utc_offset = utc_offset; |
| 35 | spinlock_unlock(&system_tz.spinlock); |
| 36 | |
| 37 | freez(old_tz); |
| 38 | freez(old_abbrev); |
| 39 | } |
| 40 | |
| 41 | SYSTEM_TZ system_tz_get(void) { |
| 42 | SYSTEM_TZ tz; |
| 43 | spinlock_lock(&system_tz.spinlock); |
| 44 | tz.timezone = strdupz(system_tz.timezone ? system_tz.timezone : "unknown"); |
| 45 | tz.abbrev_timezone = strdupz(system_tz.abbrev_timezone ? system_tz.abbrev_timezone : "UTC"); |
| 46 | tz.utc_offset = system_tz.utc_offset; |
| 47 | spinlock_unlock(&system_tz.spinlock); |
| 48 | return tz; |
| 49 | } |
| 50 | |
| 51 | void system_tz_free(SYSTEM_TZ *tz) { |
| 52 | freez(tz->timezone); |
| 53 | freez(tz->abbrev_timezone); |
| 54 | tz->timezone = NULL; |
| 55 | tz->abbrev_timezone = NULL; |
| 56 | tz->utc_offset = 0; |
| 57 | } |