static int file_of_seat(const char *seat, char **_p) { char *p; int r; assert(_p); if (seat) { if (!filename_is_valid(seat)) return -EINVAL; p = strappend("/run/systemd/seats/", seat); } else { _cleanup_free_ char *buf = NULL; r = sd_session_get_seat(NULL, &buf); if (r < 0) return r; p = strappend("/run/systemd/seats/", buf); } if (!p) return -ENOMEM; *_p = TAKE_PTR(p); return 0; }
gboolean polkit_backend_session_monitor_is_session_local (PolkitBackendSessionMonitor *monitor, PolkitSubject *session) { char *seat; if (!sd_session_get_seat (polkit_unix_session_get_session_id (POLKIT_UNIX_SESSION (session)), &seat)) { free (seat); return TRUE; } return FALSE; }
static bool weston_launch_allowed(struct weston_launch *wl) { struct group *gr; gid_t *groups; int i; #ifdef HAVE_SYSTEMD_LOGIN char *session, *seat; int err; #endif if (getuid() == 0) return true; gr = getgrnam("weston-launch"); if (gr) { groups = read_groups(); if (groups) { for (i = 0; groups[i]; ++i) { if (groups[i] == gr->gr_gid) { free(groups); return true; } } free(groups); } } #ifdef HAVE_SYSTEMD_LOGIN err = sd_pid_get_session(getpid(), &session); if (err == 0 && session) { if (sd_session_is_active(session) && sd_session_get_seat(session, &seat) == 0) { free(seat); free(session); return true; } free(session); } #endif return false; }
static const gchar * get_seat (void) { static gsize once = 0; static char *seat = NULL; if (g_once_init_enter (&once)) { char *session = NULL; if (sd_pid_get_session (getpid (), &session) == 0) { sd_session_get_seat (session, &seat); free (session); /* we intentionally leak seat here... */ } g_once_init_leave (&once, (gsize) 1); } return seat; }
static void test_login(void) { int r, k; uid_t u, u2; char *seat, *type, *class, *display; char *session; char *state; char *session2; char *t; char **seats, **sessions, **machines; uid_t *uids; unsigned n; struct pollfd pollfd; sd_login_monitor *m; assert_se(sd_pid_get_session(0, &session) == 0); printf("session = %s\n", session); assert_se(sd_pid_get_owner_uid(0, &u2) == 0); printf("user = %lu\n", (unsigned long) u2); r = sd_uid_get_sessions(u2, false, &sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); assert_se(r == sd_uid_get_sessions(u2, false, NULL)); r = sd_uid_get_seats(u2, false, &seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("seats = %s\n", t); free(t); assert_se(r == sd_uid_get_seats(u2, false, NULL)); r = sd_session_is_active(session); assert_se(r >= 0); printf("active = %s\n", yes_no(r)); r = sd_session_get_state(session, &state); assert_se(r >= 0); printf("state = %s\n", state); free(state); assert_se(sd_session_get_uid(session, &u) >= 0); printf("uid = %lu\n", (unsigned long) u); assert_se(u == u2); assert_se(sd_session_get_type(session, &type) >= 0); printf("type = %s\n", type); free(type); assert_se(sd_session_get_class(session, &class) >= 0); printf("class = %s\n", class); free(class); assert_se(sd_session_get_display(session, &display) >= 0); printf("display = %s\n", display); free(display); assert_se(sd_session_get_seat(session, &seat) >= 0); printf("seat = %s\n", seat); r = sd_seat_can_multi_session(seat); assert_se(r >= 0); printf("can do multi session = %s\n", yes_no(r)); r = sd_seat_can_tty(seat); assert_se(r >= 0); printf("can do tty = %s\n", yes_no(r)); r = sd_seat_can_graphical(seat); assert_se(r >= 0); printf("can do graphical = %s\n", yes_no(r)); assert_se(sd_uid_get_state(u, &state) >= 0); printf("state = %s\n", state); assert_se(sd_uid_is_on_seat(u, 0, seat) > 0); k = sd_uid_is_on_seat(u, 1, seat); assert_se(k >= 0); assert_se(!!r == !!r); assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0); printf("session2 = %s\n", session2); printf("uid2 = %lu\n", (unsigned long) u2); r = sd_seat_get_sessions(seat, &sessions, &uids, &n); assert_se(r >= 0); printf("n_sessions = %i\n", r); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); printf("uids ="); for (k = 0; k < (int) n; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r); free(session); free(state); free(session2); free(seat); r = sd_get_seats(&seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("n_seats = %i\n", r); printf("seats = %s\n", t); free(t); assert_se(sd_get_seats(NULL) == r); r = sd_seat_get_active(NULL, &t, NULL); assert_se(r >= 0); printf("active session on current seat = %s\n", t); free(t); r = sd_get_sessions(&sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("n_sessions = %i\n", r); printf("sessions = %s\n", t); free(t); assert_se(sd_get_sessions(NULL) == r); r = sd_get_uids(&uids); assert_se(r >= 0); printf("uids ="); for (k = 0; k < r; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); printf("n_uids = %i\n", r); assert_se(sd_get_uids(NULL) == r); r = sd_get_machine_names(&machines); assert_se(r >= 0); assert_se(r == (int) strv_length(machines)); assert_se(t = strv_join(machines, ", ")); strv_free(machines); printf("n_machines = %i\n", r); printf("machines = %s\n", t); free(t); r = sd_login_monitor_new("session", &m); assert_se(r >= 0); for (n = 0; n < 5; n++) { usec_t timeout, nw; zero(pollfd); assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0); assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0); assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0); nw = now(CLOCK_MONOTONIC); r = poll(&pollfd, 1, timeout == (uint64_t) -1 ? -1 : timeout > nw ? (int) ((timeout - nw) / 1000) : 0); assert_se(r >= 0); sd_login_monitor_flush(m); printf("Wake!\n"); } sd_login_monitor_unref(m); }
// ret: // 0: change to another session // 1: stay current session int switch_to_greeter(gchar *seat_path, struct user_session_dbus *usd) { int ret = 1; char **sessions = NULL; char *seat = NULL; char *display = NULL; char *cur_seat = NULL; gchar **strlist = NULL; char *new_user_session = NULL; GDBusProxy *seat_proxy = NULL; GDBusProxy *login1_proxy = NULL; GError *error = NULL; if (!g_variant_is_object_path(seat_path)) { g_warning("switchtogreeter:invalid object path\n"); return 1; } seat_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.DisplayManager", seat_path, "org.freedesktop.DisplayManager.Seat", NULL, &error); if (error != NULL) { g_warning("switchtogreeter:seat proxy %s\n", error->message); g_error_free(error); return 1; } error = NULL; printf("current seat path: %s\n", seat_path); strlist = g_strsplit(seat_path, "/", -1); while (*strlist) { cur_seat = *strlist; strlist++; } printf("current seat: %s\n", cur_seat); printf("usd->session_path: %s\n", usd->session_path); printf("usd->username: %s\n", usd->username); if (usd->session_path != NULL && usd->username != NULL) { sd_uid_get_sessions(name_to_uid(usd->username), 0, &sessions); while (*sessions) { sd_session_get_display(*sessions, &display); if (NULL == display){ sessions++; continue; } sd_session_get_seat(*sessions, &seat); printf("session display %s %s\n", *sessions, display); if (seat != NULL && (0 == g_ascii_strcasecmp(seat, cur_seat))) { new_user_session = *sessions; printf("switchtogreeter: find sessions path %s %s \n", new_user_session, seat); break; } sessions++; } if (NULL == new_user_session) { g_error("switchtogreeter: can not find user sessions %s \n", usd->username); return 1; } printf("find user session: %s\n", new_user_session); login1_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", NULL, &error); g_dbus_proxy_call_sync(login1_proxy, "ActivateSession", g_variant_new("(s)", new_user_session), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); ret = 0; } else if (usd->username != NULL) { printf("SwitchToUser\n"); usd->session_path = ""; g_dbus_proxy_call_sync(seat_proxy, "SwitchToUser", g_variant_new("(ss)", usd->username, usd->session_path), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); ret = 1; } else { g_dbus_proxy_call_sync(seat_proxy, "SwitchToGreeter", g_variant_new("()"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); ret = 1; } if (error != NULL) { g_warning("switchtogreeter:seat switchtogreeter %s\n", error->message); g_error_free(error); } error = NULL; g_object_unref(seat_proxy); return ret; }
static int modeset_new(Modeset **out) { _cleanup_(modeset_freep) Modeset *m = NULL; int r; assert(out); m = new0(Modeset, 1); if (!m) return log_oom(); r = sd_pid_get_session(getpid(), &m->session); if (r < 0) return log_error_errno(r, "Cannot retrieve logind session: %m"); r = sd_session_get_seat(m->session, &m->seat); if (r < 0) return log_error_errno(r, "Cannot retrieve seat of logind session: %m"); m->my_tty = is_my_tty(m->session); m->managed = m->my_tty && geteuid() > 0; m->r = rand() % 0xff; m->g = rand() % 0xff; m->b = rand() % 0xff; m->r_up = m->g_up = m->b_up = true; r = sd_event_default(&m->event); if (r < 0) return r; r = sd_bus_open_system(&m->bus); if (r < 0) return r; r = sd_bus_attach_event(m->bus, m->event, SD_EVENT_PRIORITY_NORMAL); if (r < 0) return r; r = sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1); if (r < 0) return r; r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); if (r < 0) return r; r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); if (r < 0) return r; r = sd_event_add_exit(m->event, &m->exit_src, modeset_exit_fn, m); if (r < 0) return r; /* schedule before sd-bus close */ r = sd_event_source_set_priority(m->exit_src, -10); if (r < 0) return r; r = sysview_context_new(&m->sysview, SYSVIEW_CONTEXT_SCAN_LOGIND | SYSVIEW_CONTEXT_SCAN_DRM, m->event, m->bus, NULL); if (r < 0) return r; r = grdev_context_new(&m->grdev, m->event, m->bus); if (r < 0) return r; *out = m; m = NULL; return 0; }
int main(int argc, char* argv[]) { int r, k; uid_t u, u2; char *seat, *type, *class, *display; char *session; char *state; char *session2; char *t; char **seats, **sessions; uid_t *uids; unsigned n; struct pollfd pollfd; sd_login_monitor *m; assert_se(sd_pid_get_session(0, &session) == 0); printf("session = %s\n", session); assert_se(sd_pid_get_owner_uid(0, &u2) == 0); printf("user = %lu\n", (unsigned long) u2); r = sd_uid_get_sessions(u2, false, &sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); assert_se(r == sd_uid_get_sessions(u2, false, NULL)); r = sd_uid_get_seats(u2, false, &seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("seats = %s\n", t); free(t); assert_se(r == sd_uid_get_seats(u2, false, NULL)); r = sd_session_is_active(session); assert_se(r >= 0); printf("active = %s\n", yes_no(r)); r = sd_session_get_state(session, &state); assert_se(r >= 0); printf("state = %s\n", state); free(state); assert_se(sd_session_get_uid(session, &u) >= 0); printf("uid = %lu\n", (unsigned long) u); assert_se(u == u2); assert_se(sd_session_get_type(session, &type) >= 0); printf("type = %s\n", type); free(type); assert_se(sd_session_get_class(session, &class) >= 0); printf("class = %s\n", class); free(class); assert_se(sd_session_get_display(session, &display) >= 0); printf("display = %s\n", display); free(display); assert_se(sd_session_get_seat(session, &seat) >= 0); printf("seat = %s\n", seat); r = sd_seat_can_multi_session(seat); assert_se(r >= 0); printf("can do multi session = %s\n", yes_no(r)); r = sd_seat_can_tty(seat); assert_se(r >= 0); printf("can do tty = %s\n", yes_no(r)); r = sd_seat_can_graphical(seat); assert_se(r >= 0); printf("can do graphical = %s\n", yes_no(r)); assert_se(sd_uid_get_state(u, &state) >= 0); printf("state = %s\n", state); assert_se(sd_uid_is_on_seat(u, 0, seat) > 0); k = sd_uid_is_on_seat(u, 1, seat); assert_se(k >= 0); assert_se(!!r == !!r); assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0); printf("session2 = %s\n", session2); printf("uid2 = %lu\n", (unsigned long) u2); r = sd_seat_get_sessions(seat, &sessions, &uids, &n); assert_se(r >= 0); printf("n_sessions = %i\n", r); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); printf("uids ="); for (k = 0; k < (int) n; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r); free(session); free(state); free(session2); free(seat); r = sd_get_seats(&seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("n_seats = %i\n", r); printf("seats = %s\n", t); free(t); assert_se(sd_get_seats(NULL) == r); r = sd_seat_get_active(NULL, &t, NULL); assert_se(r >= 0); printf("active session on current seat = %s\n", t); free(t); r = sd_get_sessions(&sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("n_sessions = %i\n", r); printf("sessions = %s\n", t); free(t); assert_se(sd_get_sessions(NULL) == r); r = sd_get_uids(&uids); assert_se(r >= 0); printf("uids ="); for (k = 0; k < r; k++) printf(" %lu", (unsigned long) uids[k]); printf("\n"); free(uids); printf("n_uids = %i\n", r); assert_se(sd_get_uids(NULL) == r); r = sd_login_monitor_new("session", &m); assert_se(r >= 0); zero(pollfd); pollfd.fd = sd_login_monitor_get_fd(m); pollfd.events = POLLIN; for (n = 0; n < 5; n++) { r = poll(&pollfd, 1, -1); assert_se(r >= 0); sd_login_monitor_flush(m); printf("Wake!\n"); } sd_login_monitor_unref(m); return 0; }
static int launcher_logind_connect(struct weston_launcher **out, struct weston_compositor *compositor, int tty, const char *seat_id, bool sync_drm) { struct launcher_logind *wl; struct wl_event_loop *loop; char *t; int r; wl = zalloc(sizeof(*wl)); if (wl == NULL) { r = -ENOMEM; goto err_out; } wl->base.iface = &launcher_logind_iface; wl->compositor = compositor; wl->sync_drm = sync_drm; wl->seat = strdup(seat_id); if (!wl->seat) { r = -ENOMEM; goto err_wl; } r = sd_pid_get_session(getpid(), &wl->sid); if (r < 0) { weston_log("logind: not running in a systemd session\n"); goto err_seat; } t = NULL; r = sd_session_get_seat(wl->sid, &t); if (r < 0) { weston_log("logind: failed to get session seat\n"); free(t); goto err_session; } else if (strcmp(seat_id, t)) { weston_log("logind: weston's seat '%s' differs from session-seat '%s'\n", seat_id, t); r = -EINVAL; free(t); goto err_session; } free(t); r = weston_sd_session_get_vt(wl->sid, &wl->vtnr); if (r < 0) { weston_log("logind: session not running on a VT\n"); goto err_session; } else if (tty > 0 && wl->vtnr != (unsigned int )tty) { weston_log("logind: requested VT --tty=%d differs from real session VT %u\n", tty, wl->vtnr); r = -EINVAL; goto err_session; } loop = wl_display_get_event_loop(compositor->wl_display); r = weston_dbus_open(loop, DBUS_BUS_SYSTEM, &wl->dbus, &wl->dbus_ctx); if (r < 0) { weston_log("logind: cannot connect to system dbus\n"); goto err_session; } r = launcher_logind_setup_dbus(wl); if (r < 0) goto err_dbus; r = launcher_logind_take_control(wl); if (r < 0) goto err_dbus_cleanup; r = launcher_logind_activate(wl); if (r < 0) goto err_dbus_cleanup; weston_log("logind: session control granted\n"); * (struct launcher_logind **) out = wl; return 0; err_dbus_cleanup: launcher_logind_destroy_dbus(wl); err_dbus: weston_dbus_close(wl->dbus, wl->dbus_ctx); err_session: free(wl->sid); err_seat: free(wl->seat); err_wl: free(wl); err_out: weston_log("logind: cannot setup systemd-logind helper (%d), using legacy fallback\n", r); errno = -r; return -1; }
static void test_login(void) { _cleanup_close_pair_ int pair[2] = { -1, -1 }; _cleanup_free_ char *pp = NULL, *qq = NULL; int r, k; uid_t u, u2; char *seat, *type, *class, *display, *remote_user, *remote_host, *display_session; char *session; char *state; char *session2; char *t; char **seats, **sessions, **machines; uid_t *uids; unsigned n; struct pollfd pollfd; sd_login_monitor *m = NULL; assert_se(sd_pid_get_session(0, &session) == 0); printf("session = %s\n", session); assert_se(sd_pid_get_owner_uid(0, &u2) == 0); printf("user = "******"\n", u2); display_session = NULL; r = sd_uid_get_display(u2, &display_session); assert_se(r >= 0 || r == -ENXIO); printf("user's display session = %s\n", strna(display_session)); free(display_session); assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == 0); sd_peer_get_session(pair[0], &pp); sd_peer_get_session(pair[1], &qq); assert_se(streq_ptr(pp, qq)); r = sd_uid_get_sessions(u2, false, &sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); assert_se(r == sd_uid_get_sessions(u2, false, NULL)); r = sd_uid_get_seats(u2, false, &seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("seats = %s\n", t); free(t); assert_se(r == sd_uid_get_seats(u2, false, NULL)); r = sd_session_is_active(session); assert_se(r >= 0); printf("active = %s\n", yes_no(r)); r = sd_session_is_remote(session); assert_se(r >= 0); printf("remote = %s\n", yes_no(r)); r = sd_session_get_state(session, &state); assert_se(r >= 0); printf("state = %s\n", state); free(state); assert_se(sd_session_get_uid(session, &u) >= 0); printf("uid = "UID_FMT"\n", u); assert_se(u == u2); assert_se(sd_session_get_type(session, &type) >= 0); printf("type = %s\n", type); free(type); assert_se(sd_session_get_class(session, &class) >= 0); printf("class = %s\n", class); free(class); display = NULL; r = sd_session_get_display(session, &display); assert_se(r >= 0 || r == -ENXIO); printf("display = %s\n", strna(display)); free(display); remote_user = NULL; r = sd_session_get_remote_user(session, &remote_user); assert_se(r >= 0 || r == -ENXIO); printf("remote_user = %s\n", strna(remote_user)); free(remote_user); remote_host = NULL; r = sd_session_get_remote_host(session, &remote_host); assert_se(r >= 0 || r == -ENXIO); printf("remote_host = %s\n", strna(remote_host)); free(remote_host); assert_se(sd_session_get_seat(session, &seat) >= 0); printf("seat = %s\n", seat); r = sd_seat_can_multi_session(seat); assert_se(r >= 0); printf("can do multi session = %s\n", yes_no(r)); r = sd_seat_can_tty(seat); assert_se(r >= 0); printf("can do tty = %s\n", yes_no(r)); r = sd_seat_can_graphical(seat); assert_se(r >= 0); printf("can do graphical = %s\n", yes_no(r)); assert_se(sd_uid_get_state(u, &state) >= 0); printf("state = %s\n", state); assert_se(sd_uid_is_on_seat(u, 0, seat) > 0); k = sd_uid_is_on_seat(u, 1, seat); assert_se(k >= 0); assert_se(!!r == !!r); assert_se(sd_seat_get_active(seat, &session2, &u2) >= 0); printf("session2 = %s\n", session2); printf("uid2 = "UID_FMT"\n", u2); r = sd_seat_get_sessions(seat, &sessions, &uids, &n); assert_se(r >= 0); printf("n_sessions = %i\n", r); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("sessions = %s\n", t); free(t); printf("uids ="); for (k = 0; k < (int) n; k++) printf(" "UID_FMT, uids[k]); printf("\n"); free(uids); assert_se(sd_seat_get_sessions(seat, NULL, NULL, NULL) == r); free(session); free(state); free(session2); free(seat); r = sd_get_seats(&seats); assert_se(r >= 0); assert_se(r == (int) strv_length(seats)); assert_se(t = strv_join(seats, ", ")); strv_free(seats); printf("n_seats = %i\n", r); printf("seats = %s\n", t); free(t); assert_se(sd_get_seats(NULL) == r); r = sd_seat_get_active(NULL, &t, NULL); assert_se(r >= 0); printf("active session on current seat = %s\n", t); free(t); r = sd_get_sessions(&sessions); assert_se(r >= 0); assert_se(r == (int) strv_length(sessions)); assert_se(t = strv_join(sessions, ", ")); strv_free(sessions); printf("n_sessions = %i\n", r); printf("sessions = %s\n", t); free(t); assert_se(sd_get_sessions(NULL) == r); r = sd_get_uids(&uids); assert_se(r >= 0); printf("uids ="); for (k = 0; k < r; k++) printf(" "UID_FMT, uids[k]); printf("\n"); free(uids); printf("n_uids = %i\n", r); assert_se(sd_get_uids(NULL) == r); r = sd_get_machine_names(&machines); assert_se(r >= 0); assert_se(r == (int) strv_length(machines)); assert_se(t = strv_join(machines, ", ")); strv_free(machines); printf("n_machines = %i\n", r); printf("machines = %s\n", t); free(t); r = sd_login_monitor_new("session", &m); assert_se(r >= 0); for (n = 0; n < 5; n++) { usec_t timeout, nw; zero(pollfd); assert_se((pollfd.fd = sd_login_monitor_get_fd(m)) >= 0); assert_se((pollfd.events = sd_login_monitor_get_events(m)) >= 0); assert_se(sd_login_monitor_get_timeout(m, &timeout) >= 0); nw = now(CLOCK_MONOTONIC); r = poll(&pollfd, 1, timeout == (uint64_t) -1 ? -1 : timeout > nw ? (int) ((timeout - nw) / 1000) : 0); assert_se(r >= 0); sd_login_monitor_flush(m); printf("Wake!\n"); } sd_login_monitor_unref(m); }
static int evcat_new(Evcat **out) { _cleanup_(evcat_freep) Evcat *e = NULL; int r; assert(out); e = new0(Evcat, 1); if (!e) return log_oom(); r = sd_pid_get_session(getpid(), &e->session); if (r < 0) return log_error_errno(r, "Cannot retrieve logind session: %m"); r = sd_session_get_seat(e->session, &e->seat); if (r < 0) return log_error_errno(r, "Cannot retrieve seat of logind session: %m"); e->managed = is_managed(e->session); r = sd_event_default(&e->event); if (r < 0) return r; r = sd_bus_open_system(&e->bus); if (r < 0) return r; r = sd_bus_attach_event(e->bus, e->event, SD_EVENT_PRIORITY_NORMAL); if (r < 0) return r; r = sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1); if (r < 0) return r; r = sd_event_add_signal(e->event, NULL, SIGTERM, NULL, NULL); if (r < 0) return r; r = sd_event_add_signal(e->event, NULL, SIGINT, NULL, NULL); if (r < 0) return r; r = sysview_context_new(&e->sysview, SYSVIEW_CONTEXT_SCAN_LOGIND | SYSVIEW_CONTEXT_SCAN_EVDEV, e->event, e->bus, NULL); if (r < 0) return r; r = idev_context_new(&e->idev, e->event, e->bus); if (r < 0) return r; *out = e; e = NULL; return 0; }
Eina_Bool _ecore_drm_logind_connect(Ecore_Drm_Device *dev) { #ifdef HAVE_SYSTEMD char *seat; /* get session id */ if (sd_pid_get_session(getpid(), &dev->session) < 0) { ERR("Could not get systemd session: %m"); return EINA_FALSE; } if (sd_session_get_seat(dev->session, &seat) < 0) { ERR("Could not get systemd seat: %m"); return EINA_FALSE; } else if (strcmp(dev->seat, seat)) { ERR("Session seat '%s' differs from device seat '%s'", seat, dev->seat); free(seat); return EINA_FALSE; } free(seat); if (!_ecore_drm_logind_vt_get(dev)) return EINA_FALSE; #endif if (!_ecore_drm_dbus_init(dev)) return EINA_FALSE; /* take control of session */ if (!_ecore_drm_dbus_session_take()) { ERR("Could not take control of session"); goto take_err; } /* setup vt */ if (!_ecore_drm_logind_vt_setup(dev)) { ERR("Could not setup vt '%d'", dev->vt); goto vt_err; } /* setup handler for vt signals */ if (!dev->tty.event_hdlr) { dev->tty.event_hdlr = ecore_event_handler_add(ECORE_EVENT_SIGNAL_USER, _ecore_drm_logind_cb_vt_signal, dev); } if (!active_hdlr) { active_hdlr = ecore_event_handler_add(ECORE_DRM_EVENT_ACTIVATE, _ecore_drm_logind_cb_activate, dev); } return EINA_TRUE; vt_err: _ecore_drm_dbus_session_release(); take_err: _ecore_drm_dbus_shutdown(); return EINA_FALSE; }
/** * udisks_daemon_util_on_same_seat: * @daemon: A #UDisksDaemon. * @object: The #GDBusObject that the call is on or %NULL. * @process: The process to check for. * * Checks whether the device represented by @object (if any) is plugged into * a seat where the caller represented by @process is logged in. * * This works if @object is a drive or a block object. * * Returns: %TRUE if @object and @process is on the same seat, %FALSE otherwise. */ gboolean udisks_daemon_util_on_same_seat (UDisksDaemon *daemon, UDisksObject *object, pid_t process) { #if !defined(HAVE_LIBSYSTEMD_LOGIN) /* if we don't have systemd, assume it's always the same seat */ return TRUE; #else gboolean ret = FALSE; char *session = NULL; char *seat = NULL; const gchar *drive_seat; UDisksObject *drive_object = NULL; UDisksDrive *drive = NULL; /* if we don't have logind, assume it's always the same seat */ if (!LOGIND_AVAILABLE()) return TRUE; if (UDISKS_IS_LINUX_BLOCK_OBJECT (object)) { UDisksLinuxBlockObject *linux_block_object; UDisksBlock *block; linux_block_object = UDISKS_LINUX_BLOCK_OBJECT (object); block = udisks_object_get_block (UDISKS_OBJECT (linux_block_object)); if (block != NULL) { drive_object = udisks_daemon_find_object (daemon, udisks_block_get_drive (block)); g_object_unref (block); } } else if (UDISKS_IS_LINUX_DRIVE_OBJECT (object)) { drive_object = g_object_ref (object); } if (drive_object == NULL) goto out; drive = udisks_object_get_drive (UDISKS_OBJECT (drive_object)); if (drive == NULL) goto out; /* It's not unexpected to not find a session, nor a seat associated with @process */ if (sd_pid_get_session (process, &session) == 0) sd_session_get_seat (session, &seat); /* If we don't know the seat of the caller, we assume the device is always on another seat */ if (seat == NULL) goto out; drive_seat = udisks_drive_get_seat (drive); if (g_strcmp0 (seat, drive_seat) == 0) { ret = TRUE; goto out; } out: free (seat); free (session); g_clear_object (&drive_object); g_clear_object (&drive); return ret; #endif /* HAVE_LIBSYSTEMD_LOGIN */ }