101-add-stub-udev_device_new_from_subsystem_sysname.patch 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. --- a/src/libudev.c
  2. +++ b/src/libudev.c
  3. @@ -26,6 +26,7 @@ struct udev_device {
  4. struct udev *udev;
  5. int refcount;
  6. char syspath[32];
  7. + char sysfspath[64];
  8. dev_t devnum;
  9. char const *sysname;
  10. char const *action;
  11. @@ -115,6 +116,29 @@ udev_device_new_from_devnum(struct udev
  12. return NULL;
  13. }
  14. +struct udev_device *
  15. +udev_device_new_from_subsystem_sysname(
  16. + struct udev *udev, const char *subsystem, const char *sysname)
  17. +{
  18. + struct udev_device *u;
  19. + char sysfsname[64];
  20. + struct stat st;
  21. +
  22. + snprintf(sysfsname, sizeof(sysfsname), "/sys/bus/%s/devices/%s/", subsystem, sysname);
  23. + if (stat(sysfsname, &st) == 0)
  24. + {
  25. + char sysfsdev[64];
  26. + u = calloc(1, sizeof(struct udev_device));
  27. + strncpy(u->sysfspath, sysfsname, sizeof(u->sysfspath));
  28. +
  29. + return u;
  30. + }
  31. + else
  32. + {
  33. + return NULL;
  34. + }
  35. +}
  36. +
  37. char const *
  38. udev_device_get_devnode(struct udev_device *udev_device)
  39. {
  40. @@ -132,6 +156,20 @@ udev_device_get_devnum(struct udev_devic
  41. char const *
  42. udev_device_get_driver(struct udev_device *udev_device)
  43. {
  44. + if (udev_device->sysfspath)
  45. + {
  46. + char driverlnp[64];
  47. + char driverlnk[32];
  48. + snprintf(driverlnp, sizeof(driverlnp), "%s/driver",
  49. + udev_device->sysfspath);
  50. + if (readlink(driverlnp, driverlnk, sizeof(driver)))
  51. + {
  52. + char *drivernm;
  53. + drivernm = strrchr(driverlnk, '/');
  54. + if (drivernm)
  55. + return ++drivernm;
  56. + }
  57. + }
  58. return NULL;
  59. }
  60. --- a/src/libudev.h
  61. +++ b/src/libudev.h
  62. @@ -30,6 +30,8 @@ struct udev_device *udev_device_new_from
  63. struct udev *udev, char const *syspath);
  64. struct udev_device *udev_device_new_from_devnum(
  65. struct udev *udev, char type, dev_t devnum);
  66. +struct udev_device *udev_device_new_from_subsystem_sysname(
  67. + struct udev *udev, const char *subsystem, const char *sysname);
  68. char const *udev_device_get_driver(struct udev_device *udev_device);
  69. char const *udev_device_get_syspath(struct udev_device *udev_device);
  70. char const *udev_device_get_sysname(struct udev_device *udev_device);