backend/session: add a timeout waiting for GPUs
If a GPU doesn't show up in 10s, bail out and return zero GPUs.
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
+#include <time.h>
#include <wayland-server-core.h>
#include <wlr/backend/session.h>
#include <wlr/backend/session/interface.h>
#include "backend/session/session.h"
#include "util/signal.h"
+#define WAIT_GPU_TIMEOUT 10000 // ms
+
extern const struct session_impl session_libseat;
extern const struct session_impl session_logind;
extern const struct session_impl session_direct;
@@ -322,6 +325,12 @@ static struct udev_enumerate *enumerate_drm_cards(struct udev *udev) {
return en;
}
+static uint64_t get_current_time_ms(void) {
+ struct timespec ts = {0};
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
+}
+
struct find_gpus_add_handler {
bool added;
struct wl_listener listener;
@@ -356,16 +365,24 @@ size_t wlr_session_find_gpus(struct wlr_session *session,
handler.listener.notify = find_gpus_handle_add;
wl_signal_add(&session->events.add_drm_card, &handler.listener);
+ uint64_t started_at = get_current_time_ms();
+ uint64_t timeout = WAIT_GPU_TIMEOUT;
struct wl_event_loop *event_loop =
wl_display_get_event_loop(session->display);
while (!handler.added) {
- int ret = wl_event_loop_dispatch(event_loop, -1);
+ int ret = wl_event_loop_dispatch(event_loop, (int)timeout);
if (ret < 0) {
wlr_log_errno(WLR_ERROR, "Failed to wait for DRM card device: "
"wl_event_loop_dispatch failed");
udev_enumerate_unref(en);
return -1;
}
+
+ uint64_t now = get_current_time_ms();
+ if (now >= started_at + WAIT_GPU_TIMEOUT) {
+ break;
+ }
+ timeout = started_at + WAIT_GPU_TIMEOUT - now;
}
wl_list_remove(&handler.listener.link);
