backend/session: allow wlr_session_find_gpus to return an error

Sometimes wlr_session_find_gpus will encounter an error. This is
different from finding zero GPUs.

On error, wlr_session_find_gpus already returns -1. However, this is
casted to size_t, so callers uncorrectly assume this is a success.

Instead, make wlr_session_find_gpus return a ssize_t and allow callers
to handle the error accordingly.
This commit is contained in:
Simon Ser 2020-12-28 10:52:40 +01:00
parent 7febdc7334
commit e8d56ca415
3 changed files with 12 additions and 7 deletions

View file

@ -150,11 +150,16 @@ static struct wlr_backend *attempt_noop_backend(struct wl_display *display) {
static struct wlr_backend *attempt_drm_backend(struct wl_display *display, static struct wlr_backend *attempt_drm_backend(struct wl_display *display,
struct wlr_backend *backend, struct wlr_session *session) { struct wlr_backend *backend, struct wlr_session *session) {
struct wlr_device *gpus[8]; struct wlr_device *gpus[8];
size_t num_gpus = wlr_session_find_gpus(session, 8, gpus); ssize_t num_gpus = wlr_session_find_gpus(session, 8, gpus);
struct wlr_backend *primary_drm = NULL; if (num_gpus < 0) {
wlr_log(WLR_ERROR, "Failed to find GPUs");
return NULL;
}
wlr_log(WLR_INFO, "Found %zu GPUs", num_gpus); wlr_log(WLR_INFO, "Found %zu GPUs", num_gpus);
for (size_t i = 0; i < num_gpus; ++i) { struct wlr_backend *primary_drm = NULL;
for (size_t i = 0; i < (size_t)num_gpus; ++i) {
struct wlr_backend *drm = wlr_drm_backend_create(display, session, struct wlr_backend *drm = wlr_drm_backend_create(display, session,
gpus[i], primary_drm); gpus[i], primary_drm);
if (!drm) { if (!drm) {

View file

@ -278,12 +278,12 @@ out_dev:
return NULL; return NULL;
} }
static size_t explicit_find_gpus(struct wlr_session *session, static ssize_t explicit_find_gpus(struct wlr_session *session,
size_t ret_len, struct wlr_device *ret[static ret_len], const char *str) { size_t ret_len, struct wlr_device *ret[static ret_len], const char *str) {
char *gpus = strdup(str); char *gpus = strdup(str);
if (!gpus) { if (!gpus) {
wlr_log_errno(WLR_ERROR, "Allocation failed"); wlr_log_errno(WLR_ERROR, "Allocation failed");
return 0; return -1;
} }
size_t i = 0; size_t i = 0;
@ -345,7 +345,7 @@ static void find_gpus_handle_add(struct wl_listener *listener, void *data) {
/* Tries to find the primary GPU by checking for the "boot_vga" attribute. /* Tries to find the primary GPU by checking for the "boot_vga" attribute.
* If it's not found, it returns the first valid GPU it finds. * If it's not found, it returns the first valid GPU it finds.
*/ */
size_t wlr_session_find_gpus(struct wlr_session *session, ssize_t wlr_session_find_gpus(struct wlr_session *session,
size_t ret_len, struct wlr_device **ret) { size_t ret_len, struct wlr_device **ret) {
const char *explicit = getenv("WLR_DRM_DEVICES"); const char *explicit = getenv("WLR_DRM_DEVICES");
if (explicit) { if (explicit) {

View file

@ -96,7 +96,7 @@ void wlr_session_close_file(struct wlr_session *session,
*/ */
bool wlr_session_change_vt(struct wlr_session *session, unsigned vt); bool wlr_session_change_vt(struct wlr_session *session, unsigned vt);
size_t wlr_session_find_gpus(struct wlr_session *session, ssize_t wlr_session_find_gpus(struct wlr_session *session,
size_t ret_len, struct wlr_device **ret); size_t ret_len, struct wlr_device **ret);
#endif #endif