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; }
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_parse_environment(); log_open(); if (argc != 1) { log_error("This program takes no arguments."); r = -EINVAL; goto finish; } umask(0022); r = mac_selinux_init(NULL); if (r < 0) { log_error_errno(r, "SELinux setup failed: %m"); 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; } /* Always create the directory where resolv.conf will live */ r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid); if (r < 0) { log_error_errno(r, "Could not create runtime directory: %m"); goto finish; } r = drop_privileges(uid, gid, 0); if (r < 0) goto finish; assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, -1) >= 0); r = manager_new(&m); if (r < 0) { log_error_errno(r, "Could not create manager: %m"); goto finish; } r = manager_parse_config_file(m); if (r < 0) log_warning_errno(r, "Failed to parse configuration file: %m"); r = manager_start(m); if (r < 0) { log_error_errno(r, "Failed to start manager: %m"); goto finish; } /* Write finish default resolv.conf to avoid a dangling * symlink */ r = manager_write_resolv_conf(m); if (r < 0) log_warning_errno(r, "Could not create resolv.conf: %m"); sd_notify(false, "READY=1\n" "STATUS=Processing requests..."); r = sd_event_loop(m->event); if (r < 0) { log_error_errno(r, "Event loop failed: %m"); goto finish; } 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; }