1
0
mirror of https://github.com/systemd/systemd synced 2025-10-05 16:03:15 +02:00

Several more timezone related cleanups (#39160)

Continuation of #38876.
This commit is contained in:
Mike Yuan
2025-09-30 15:40:40 +02:00
committed by GitHub
5 changed files with 13 additions and 68 deletions

View File

@@ -1022,7 +1022,7 @@ int parse_timestamp(const char *t, usec_t *ret) {
if (timezone_is_valid(tz, LOG_DEBUG)) {
SAVE_TIMEZONE;
if (setenv("TZ", strjoina(":", tz), /* overwrite = */ true) < 0)
if (setenv("TZ", tz, /* overwrite = */ true) < 0)
return negative_errno();
return parse_timestamp_impl(t, max_len, /* utc = */ false, /* isdst = */ -1, /* gmtoff = */ 0, ret);

View File

@@ -1426,13 +1426,7 @@ static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, u
return 0;
}
typedef struct SpecNextResult {
usec_t next;
int return_value;
} SpecNextResult;
int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *ret_next) {
SpecNextResult *shared, tmp;
int r;
assert(spec);
@@ -1440,39 +1434,13 @@ int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *ret_n
if (isempty(spec->timezone))
return calendar_spec_next_usec_impl(spec, usec, ret_next);
shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (shared == MAP_FAILED)
return negative_errno();
SAVE_TIMEZONE;
r = safe_fork("(sd-calendar)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_WAIT, NULL);
if (r < 0) {
(void) munmap(shared, sizeof *shared);
r = RET_NERRNO(setenv("TZ", spec->timezone, /* overwrite = */ true));
if (r < 0)
return r;
}
if (r == 0) {
char *colon_tz;
/* tzset(3) says $TZ should be prefixed with ":" if we reference timezone files */
colon_tz = strjoina(":", spec->timezone);
tzset();
if (setenv("TZ", colon_tz, 1) != 0) {
shared->return_value = negative_errno();
_exit(EXIT_FAILURE);
}
tzset();
shared->return_value = calendar_spec_next_usec_impl(spec, usec, &shared->next);
_exit(EXIT_SUCCESS);
}
tmp = *shared;
if (munmap(shared, sizeof *shared) < 0)
return negative_errno();
if (tmp.return_value == 0 && ret_next)
*ret_next = tmp.next;
return tmp.return_value;
return calendar_spec_next_usec_impl(spec, usec, ret_next);
}

View File

@@ -44,12 +44,9 @@ static void _test_one(int line, const char *input, const char *output) {
static void _test_next(int line, const char *input, const char *new_tz, usec_t after, usec_t expect) {
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
usec_t u;
char *old_tz;
int r;
old_tz = getenv("TZ");
if (old_tz)
old_tz = strdupa_safe(old_tz);
SAVE_TIMEZONE;
if (!isempty(new_tz) && !strchr(new_tz, ','))
new_tz = strjoina(":", new_tz);
@@ -68,9 +65,6 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a
assert_se(r >= 0 && u == expect);
else
assert_se(r == -ENOENT);
assert_se(set_unset_env("TZ", old_tz, true) == 0);
tzset();
}
#define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect)

View File

@@ -13,13 +13,7 @@
#define TRIAL 100u
static void set_timezone(const char *tz) {
if (!tz)
ASSERT_OK_ERRNO(unsetenv("TZ"));
if (isempty(tz))
ASSERT_OK_ERRNO(setenv("TZ", tz, /* overwrite = */ true));
else
ASSERT_OK_ERRNO(setenv("TZ", strjoina(":", tz), /* overwrite = */ true));
ASSERT_OK(set_unset_env("TZ", tz, /* overwrite = */ true));
tzset();
log_info("TZ=%s, tzname[0]=%s, tzname[1]=%s", strna(getenv("TZ")), strempty(tzname[0]), strempty(tzname[1]));
}
@@ -421,7 +415,7 @@ static void test_format_timestamp_impl(usec_t x) {
* timezone may provide time shifted 1 hour from the original. See
* https://github.com/systemd/systemd/issues/28472 and https://github.com/systemd/systemd/pull/35471 */
bool ignore =
streq_ptr(getenv("TZ"), ":Africa/Windhoek") &&
streq_ptr(getenv("TZ"), "Africa/Windhoek") &&
(x_sec > y_sec ? x_sec - y_sec : y_sec - x_sec) == 3600;
log_full(ignore ? LOG_WARNING : LOG_ERR,
@@ -1113,14 +1107,14 @@ TEST(usec_shift_clock) {
TEST(in_utc_timezone) {
SAVE_TIMEZONE;
assert_se(setenv("TZ", ":UTC", 1) >= 0);
assert_se(setenv("TZ", "UTC", 1) >= 0);
assert_se(in_utc_timezone());
ASSERT_STREQ(tzname[0], "UTC");
ASSERT_STREQ(tzname[1], "UTC");
assert_se(timezone == 0);
assert_se(daylight == 0);
assert_se(setenv("TZ", ":Europe/Berlin", 1) >= 0);
assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0);
assert_se(!in_utc_timezone());
ASSERT_STREQ(tzname[0], "CET");
ASSERT_STREQ(tzname[1], "CEST");

View File

@@ -58,7 +58,6 @@ typedef struct StatusInfo {
static int print_status_info(const StatusInfo *i) {
_cleanup_(table_unrefp) Table *table = NULL;
const char *old_tz = NULL, *tz, *tz_colon;
char a[LINE_MAX];
TableCell *cell;
struct tm tm;
@@ -79,13 +78,10 @@ static int print_status_info(const StatusInfo *i) {
(void) table_set_ellipsize_percent(table, cell, 100);
/* Save the old $TZ */
tz = getenv("TZ");
if (tz)
old_tz = strdupa_safe(tz);
SAVE_TIMEZONE;
/* Set the new $TZ */
tz_colon = strjoina(":", isempty(i->timezone) ? "UTC" : i->timezone);
if (setenv("TZ", tz_colon, true) < 0)
if (setenv("TZ", isempty(i->timezone) ? "UTC" : i->timezone, /* overwrite = */ true) < 0)
log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
else
tzset();
@@ -158,13 +154,6 @@ static int print_status_info(const StatusInfo *i) {
if (r < 0)
return table_log_add_error(r);
/* Restore the $TZ */
r = set_unset_env("TZ", old_tz, true);
if (r < 0)
log_warning_errno(r, "Failed to set TZ environment variable, ignoring: %m");
else
tzset();
r = table_add_many(table,
TABLE_FIELD, "System clock synchronized",
TABLE_BOOLEAN, i->ntp_synced,