int bus_event_loop_with_idle( sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata) { bool exiting = false; int r, code; assert(e); assert(bus); assert(name); for (;;) { bool idle; r = sd_event_get_state(e); if (r < 0) return r; if (r == SD_EVENT_FINISHED) break; if (check_idle) idle = check_idle(userdata); else idle = true; r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout); if (r < 0) return r; if (r == 0 && !exiting) { r = sd_bus_try_close(bus); if (r == -EBUSY) continue; /* Fallback for dbus1 connections: we * unregister the name and wait for the * response to come through for it */ if (r == -ENOTSUP) { /* Inform the service manager that we * are going down, so that it will * queue all further start requests, * instead of assuming we are already * running. */ sd_notify(false, "STOPPING=1"); r = bus_async_unregister_and_exit(e, bus, name); if (r < 0) return r; exiting = true; continue; } if (r < 0) return r; sd_event_exit(e, 0); break; } } r = sd_event_get_exit_code(e, &code); if (r < 0) return r; return code; }
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_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; }
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(); 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, false); if (r < 0) { log_error_errno(r, "Could not create runtime directory: %m"); goto finish; } /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all * privileges are already dropped. */ if (getuid() == 0) { /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */ r = drop_privileges(uid, gid, (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */ (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */ (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */); if (r < 0) goto finish; } assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0); r = manager_new(&m); if (r < 0) { log_error_errno(r, "Could not create manager: %m"); goto finish; } 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 */ (void) manager_write_resolv_conf(m); /* Let's drop the remaining caps now */ r = capability_bounding_set_drop(0, true); if (r < 0) { log_error_errno(r, "Failed to drop remaining caps: %m"); goto finish; } 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; }
static int evcat_sysview_fn(sysview_context *c, void *userdata, sysview_event *ev) { unsigned int flags, type; Evcat *e = userdata; sysview_device *d; const char *name; int r; switch (ev->type) { case SYSVIEW_EVENT_SESSION_FILTER: if (streq_ptr(e->session, ev->session_filter.id)) return 1; break; case SYSVIEW_EVENT_SESSION_ADD: assert(!e->idev_session); name = sysview_session_get_name(ev->session_add.session); flags = 0; if (e->managed) flags |= IDEV_SESSION_MANAGED; r = idev_session_new(&e->idev_session, e->idev, flags, name, evcat_idev_fn, e); if (r < 0) return log_error_errno(r, "Cannot create idev session: %m"); if (e->managed) { r = sysview_session_take_control(ev->session_add.session); if (r < 0) return log_error_errno(r, "Cannot request session control: %m"); } idev_session_enable(e->idev_session); break; case SYSVIEW_EVENT_SESSION_REMOVE: idev_session_disable(e->idev_session); e->idev_session = idev_session_free(e->idev_session); if (sd_event_get_exit_code(e->event, &r) == -ENODATA) sd_event_exit(e->event, 0); break; case SYSVIEW_EVENT_SESSION_ATTACH: d = ev->session_attach.device; type = sysview_device_get_type(d); if (type == SYSVIEW_DEVICE_EVDEV) { r = idev_session_add_evdev(e->idev_session, sysview_device_get_ud(d)); if (r < 0) return log_error_errno(r, "Cannot add evdev device to idev: %m"); } break; case SYSVIEW_EVENT_SESSION_DETACH: d = ev->session_detach.device; type = sysview_device_get_type(d); if (type == SYSVIEW_DEVICE_EVDEV) { r = idev_session_remove_evdev(e->idev_session, sysview_device_get_ud(d)); if (r < 0) return log_error_errno(r, "Cannot remove evdev device from idev: %m"); } break; case SYSVIEW_EVENT_SESSION_CONTROL: r = ev->session_control.error; if (r < 0) return log_error_errno(r, "Cannot acquire session control: %m"); r = ioctl(1, KDSKBMODE, K_UNICODE); if (r < 0) return log_error_errno(errno, "Cannot set K_UNICODE on stdout: %m"); r = ioctl(1, KDSETMODE, KD_TEXT); if (r < 0) return log_error_errno(errno, "Cannot set KD_TEXT on stdout: %m"); printf("\n"); break; } return 0; }