From 71a399f20f149dccbd0291af465b3fd1cac24f6d Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 26 Jul 2019 09:00:05 +1000 Subject: [PATCH] ratbagd: encode the test device iteration in the test device name The sequence triggered by the ratbagctl.test suite is: - DBus call Commit() - ratbagd schedules the callback as idle func - DBus call ResetTestDevice() - ratbagd cleans up the test device - callback is called on next idle - boom! the device is already freed To avoid this, we need to take a ref of the device while it's being scheduled for the commit handling. But that blocks the object paths for new test devices because the test device name is encoded in those paths (like "hidraw" for real devices) and it doesn't change. So let's add a count to the test device name to make sure the new device doesn't re-use the old device's paths. Signed-off-by: Peter Hutterer --- ratbagd/ratbagd-device.c | 3 ++- ratbagd/ratbagd-test.c | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ratbagd/ratbagd-device.c b/ratbagd/ratbagd-device.c index bc8d0cad..d4411f99 100644 --- a/ratbagd/ratbagd-device.c +++ b/ratbagd/ratbagd-device.c @@ -174,6 +174,7 @@ static void ratbagd_device_commit_pending(void *data) r = ratbag_device_commit(device->lib_device); if (r < 0) ratbagd_device_resync(device, device->ctx->bus); + ratbagd_device_unref(device); } static int ratbagd_device_commit(sd_bus_message *m, @@ -184,7 +185,7 @@ static int ratbagd_device_commit(sd_bus_message *m, ratbagd_schedule_task(device->ctx, ratbagd_device_commit_pending, - device); + ratbagd_device_ref(device)); CHECK_CALL(sd_bus_reply_method_return(m, "u", 0)); diff --git a/ratbagd/ratbagd-test.c b/ratbagd/ratbagd-test.c index fca90192..d41e3e22 100644 --- a/ratbagd/ratbagd-test.c +++ b/ratbagd/ratbagd-test.c @@ -178,10 +178,12 @@ int ratbagd_reset_test_device(sd_bus_message *m, void *userdata, sd_bus_error *error) { + static int count; static struct ratbagd_device *ratbagd_test_device = NULL; struct ratbagd *ctx = userdata; struct ratbag_device *device; int r; + char devicename[64]; if (ratbagd_test_device) { ratbagd_device_unlink(ratbagd_test_device); @@ -196,7 +198,8 @@ int ratbagd_reset_test_device(sd_bus_message *m, device = ratbag_device_new_test_device(ctx->lib_ctx, &ratbagd_test_device_descr); - r = ratbagd_device_new(&ratbagd_test_device, ctx, "test_device", device); + snprintf(devicename, sizeof(devicename), "test_device_%d", count++); + r = ratbagd_device_new(&ratbagd_test_device, ctx, devicename, device); /* the ratbagd_device takes its own reference, drop ours */ ratbag_device_unref(device);