static void test_clock_is_localtime(void) { char adjtime[] = "/tmp/test-adjtime.XXXXXX"; int fd = -1; _cleanup_fclose_ FILE* f = NULL; static const struct scenario { const char* contents; int expected_result; } scenarios[] = { /* adjtime configures UTC */ {"0.0 0 0\n0\nUTC\n", 0}, /* adjtime configures local time */ {"0.0 0 0\n0\nLOCAL\n", 1}, /* no final EOL */ {"0.0 0 0\n0\nUTC", 0}, {"0.0 0 0\n0\nLOCAL", 1}, /* empty value -> defaults to UTC */ {"0.0 0 0\n0\n", 0}, /* unknown value -> defaults to UTC */ {"0.0 0 0\n0\nFOO\n", 0}, /* no third line */ {"0.0 0 0", 0}, {"0.0 0 0\n", 0}, {"0.0 0 0\n0", 0}, }; /* without an adjtime file we default to UTC */ assert_se(clock_is_localtime("/nonexisting/adjtime") == 0); fd = mkostemp_safe(adjtime); assert_se(fd >= 0); log_info("adjtime test file: %s", adjtime); f = fdopen(fd, "w"); assert_se(f); for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) { log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result); log_info("%s", scenarios[i].contents); rewind(f); ftruncate(fd, 0); assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0); assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result); } unlink(adjtime); }
/* Test with the real /etc/adjtime */ static void test_clock_is_localtime_system(void) { int r; r = clock_is_localtime(NULL); if (access("/etc/adjtime", F_OK) == 0) { log_info("/etc/adjtime exists, clock_is_localtime() == %i", r); /* if /etc/adjtime exists we expect some answer, no error or * crash */ assert_se(IN_SET(r, 0, 1)); } else /* default is UTC if there is no /etc/adjtime */ assert_se(r == 0); }
int main(int argc, char *argv[]) { _cleanup_(manager_freep) Manager *m = NULL; const char *user = "******"; uid_t uid; gid_t gid; int r; log_set_target(LOG_TARGET_AUTO); log_set_facility(LOG_CRON); log_parse_environment(); log_open(); umask(0022); if (argc != 1) { log_error("This program does not take arguments."); r = -EINVAL; goto finish; } r = get_user_creds(&user, &uid, &gid, NULL, NULL); if (r < 0) { log_error_errno(r, "Cannot resolve user name %s: %m", user); goto finish; } r = load_clock_timestamp(uid, gid); if (r < 0) goto finish; r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME)); if (r < 0) goto finish; /* We need one process for ourselves, plus one thread for the asynchronous resolver */ if (setrlimit(RLIMIT_NPROC, &RLIMIT_MAKE_CONST(2)) < 0) log_warning_errno(errno, "Failed to lower RLIMIT_NPROC to 2: %m"); assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); r = manager_new(&m); if (r < 0) { log_error_errno(r, "Failed to allocate manager: %m"); goto finish; } if (clock_is_localtime() > 0) { log_info("The system is configured to read the RTC time in the local time zone. " "This mode can not be fully supported. All system time to RTC updates are disabled."); m->rtc_local_time = true; } r = manager_parse_config_file(m); if (r < 0) log_warning_errno(r, "Failed to parse configuration file: %m"); log_debug("systemd-timesyncd running as pid %lu", (unsigned long) getpid()); sd_notify(false, "READY=1\n" "STATUS=Daemon is running"); if (network_is_online()) { r = manager_connect(m); if (r < 0) goto finish; } r = sd_event_loop(m->event); if (r < 0) { log_error_errno(r, "Failed to run event loop: %m"); goto finish; } /* if we got an authoritative time, store it in the file system */ if (m->sync) touch("/var/lib/systemd/clock"); sd_event_get_exit_code(m->event, &r); finish: sd_notify(false, "STOPPING=1\n" "STATUS=Shutting down..."); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
{"0.0 0 0\n0\nLOCAL\n", 1}, /* no final EOL */ {"0.0 0 0\n0\nUTC", 0}, {"0.0 0 0\n0\nLOCAL", 1}, /* empty value -> defaults to UTC */ {"0.0 0 0\n0\n", 0}, /* unknown value -> defaults to UTC */ {"0.0 0 0\n0\nFOO\n", 0}, /* no third line */ {"0.0 0 0", 0}, {"0.0 0 0\n", 0}, {"0.0 0 0\n0", 0}, }; /* without an adjtime file we default to UTC */ assert_se(clock_is_localtime("/nonexisting/adjtime") == 0); assert_se(fmkostemp_safe(adjtime, "w", &f) == 0); log_info("adjtime test file: %s", adjtime); for (size_t i = 0; i < ELEMENTSOF(scenarios); ++i) { log_info("scenario #%zu:, expected result %i", i, scenarios[i].expected_result); log_info("%s", scenarios[i].contents); rewind(f); ftruncate(fileno(f), 0); assert_se(write_string_stream(f, scenarios[i].contents, WRITE_STRING_FILE_AVOID_NEWLINE) == 0); assert_se(clock_is_localtime(adjtime) == scenarios[i].expected_result); } } /* Test with the real /etc/adjtime */