From 8d24cd530425f3860c4a55a987b9d74f029c8d91 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 20 Nov 2014 13:36:32 +1000 Subject: [PATCH] path: keep the udev context around We need it for each device anyway, keep the ref around. Makes error handling a bit easier, we don't need to handle failing udev_new() and reduce the danger of mis-refcounting it. Signed-off-by: Peter Hutterer Reviewed-by: Hans de Goede --- src/path.c | 31 ++++++++++++++++++------------- src/path.h | 1 + 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/path.c b/src/path.c index 3752751..f986afd 100644 --- a/src/path.c +++ b/src/path.c @@ -110,22 +110,18 @@ path_seat_get_named(struct path_input *input, } static int -path_get_udev_properties(const char *path, +path_get_udev_properties(struct udev *udev, + const char *path, char **sysname, char **syspath, char **seat_name, char **seat_logical_name) { - struct udev *udev = NULL; struct udev_device *device = NULL; struct stat st; const char *seat; int rc = -1; - udev = udev_new(); - if (!udev) - goto out; - if (stat(path, &st) < 0) goto out; @@ -147,8 +143,6 @@ path_get_udev_properties(const char *path, out: if (device) udev_device_unref(device); - if (udev) - udev_unref(udev); return rc; } @@ -160,8 +154,12 @@ path_device_enable(struct path_input *input, const char *devnode) char *sysname = NULL, *syspath = NULL; char *seat_name = NULL, *seat_logical_name = NULL; - if (path_get_udev_properties(devnode, &sysname, &syspath, - &seat_name, &seat_logical_name) == -1) { + if (path_get_udev_properties(input->udev, + devnode, + &sysname, + &syspath, + &seat_name, + &seat_logical_name) == -1) { log_info(&input->base, "failed to obtain sysname for device '%s'.\n", devnode); @@ -229,6 +227,8 @@ path_input_destroy(struct libinput *input) struct path_input *path_input = (struct path_input*)input; struct path_device *dev, *tmp; + udev_unref(path_input->udev); + list_for_each_safe(dev, tmp, &path_input->path_list, link) { free(dev->path); free(dev); @@ -247,20 +247,25 @@ libinput_path_create_context(const struct libinput_interface *interface, void *user_data) { struct path_input *input; + struct udev *udev; if (!interface) return NULL; - input = zalloc(sizeof *input); - if (!input) + udev = udev_new(); + if (!udev) return NULL; - if (libinput_init(&input->base, interface, + input = zalloc(sizeof *input); + if (!input || + libinput_init(&input->base, interface, &interface_backend, user_data) != 0) { + udev_unref(udev); free(input); return NULL; } + input->udev = udev; list_init(&input->path_list); return &input->base; diff --git a/src/path.h b/src/path.h index 779d823..999601b 100644 --- a/src/path.h +++ b/src/path.h @@ -28,6 +28,7 @@ struct path_input { struct libinput base; + struct udev *udev; struct list path_list; }; -- 2.7.4