drm: Rework device removal signal

By moving the remove signal right before where we assert that the assert signal
list is empty, we create a strict contract requiring the remove signals to remove
themselves. We ensure this is true by calling `backend_destroy()` upon removal,
making sure that we don't double free the device fd.

Signed-off-by: liupeng <liupeng01@kylinos.cn>
This commit is contained in:
liupeng 2026-05-08 20:47:19 +08:00
parent 36cd9344e1
commit 510cda868d
2 changed files with 14 additions and 7 deletions

View file

@ -65,7 +65,9 @@ static void backend_destroy(struct wlr_backend *backend) {
} }
free(drm->name); free(drm->name);
wlr_session_close_file(drm->session, drm->dev); if (drm->dev) {
wlr_session_close_file(drm->session, drm->dev);
}
wl_event_source_remove(drm->drm_event); wl_event_source_remove(drm->drm_event);
free(drm); free(drm);
} }
@ -152,6 +154,7 @@ static void handle_dev_remove(struct wl_listener *listener, void *data) {
struct wlr_drm_backend *drm = wl_container_of(listener, drm, dev_remove); struct wlr_drm_backend *drm = wl_container_of(listener, drm, dev_remove);
wlr_log(WLR_INFO, "Destroying DRM backend for %s", drm->name); wlr_log(WLR_INFO, "Destroying DRM backend for %s", drm->name);
drm->dev = NULL;
backend_destroy(&drm->backend); backend_destroy(&drm->backend);
} }

View file

@ -227,14 +227,19 @@ static int handle_udev_event(int fd, uint32_t mask, void *data) {
} }
} }
} else if (strcmp(action, "remove") == 0) { } else if (strcmp(action, "remove") == 0) {
bool found = false;
struct wlr_device *dev; struct wlr_device *dev;
wl_list_for_each(dev, &session->devices, link) { wl_list_for_each(dev, &session->devices, link) {
if (dev->dev == devnum) { if (dev->dev == devnum) {
wlr_log(WLR_DEBUG, "DRM device %s removed", sysname); found = true;
wl_signal_emit_mutable(&dev->events.remove, NULL);
break; break;
} }
} }
if (found) {
wlr_log(WLR_DEBUG, "DRM device %s removed", sysname);
wlr_session_close_file(session, dev);
}
} }
out: out:
@ -375,11 +380,10 @@ void wlr_session_close_file(struct wlr_session *session,
wlr_log_errno(WLR_ERROR, "Failed to close device %d", dev->device_id); wlr_log_errno(WLR_ERROR, "Failed to close device %d", dev->device_id);
} }
wl_signal_emit_mutable(&dev->events.remove, NULL);
assert(wl_list_empty(&dev->events.change.listener_list)); assert(wl_list_empty(&dev->events.change.listener_list));
// TODO: assert that the "remove" listener list is empty as well. Listeners assert(wl_list_empty(&dev->events.remove.listener_list));
// will typically call wlr_session_close_file() in response, and
// wl_signal_emit_mutable() installs two phantom listeners, so we'd count
// these two.
close(dev->fd); close(dev->fd);
wl_list_remove(&dev->link); wl_list_remove(&dev->link);