sd-device: make sd_device_get_*() return -ENOENT if the values are not set
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 1 Sep 2018 09:05:27 +0000 (18:05 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 23 Sep 2018 08:18:19 +0000 (17:18 +0900)
src/libsystemd/sd-device/device-enumerator.c
src/libsystemd/sd-device/device-private.c
src/libsystemd/sd-device/sd-device.c
src/libudev/libudev-device.c

index 27cc358..c7e5bf4 100644 (file)
@@ -478,18 +478,6 @@ static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator,
                         continue;
                 }
 
-                k = sd_device_get_devnum(device, &devnum);
-                if (k < 0) {
-                        r = k;
-                        continue;
-                }
-
-                k = sd_device_get_ifindex(device, &ifindex);
-                if (k < 0) {
-                        r = k;
-                        continue;
-                }
-
                 k = sd_device_get_is_initialized(device, &initialized);
                 if (k < 0) {
                         r = k;
@@ -508,7 +496,8 @@ static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator,
                  */
                 if (!enumerator->match_allow_uninitialized &&
                     !initialized &&
-                    (major(devnum) > 0 || ifindex > 0))
+                    (sd_device_get_devnum(device, &devnum) >= 0 ||
+                     (sd_device_get_ifindex(device, &ifindex) >= 0 && ifindex > 0)))
                         continue;
 
                 if (!match_parent(enumerator, device))
index 1e41773..eb27346 100644 (file)
@@ -277,6 +277,9 @@ int device_get_devnode_mode(sd_device *device, mode_t *mode) {
         if (r < 0)
                 return r;
 
+        if (device->devmode == (mode_t) -1)
+                return -ENOENT;
+
         *mode = device->devmode;
 
         return 0;
@@ -292,6 +295,9 @@ int device_get_devnode_uid(sd_device *device, uid_t *uid) {
         if (r < 0)
                 return r;
 
+        if (device->devuid == (uid_t) -1)
+                return -ENOENT;
+
         *uid = device->devuid;
 
         return 0;
@@ -327,6 +333,9 @@ int device_get_devnode_gid(sd_device *device, gid_t *gid) {
         if (r < 0)
                 return r;
 
+        if (device->devgid == (gid_t) -1)
+                return -ENOENT;
+
         *gid = device->devgid;
 
         return 0;
@@ -723,6 +732,9 @@ int device_get_watch_handle(sd_device *device, int *handle) {
         if (r < 0)
                 return r;
 
+        if (device->watch_handle < 0)
+                return -ENOENT;
+
         *handle = device->watch_handle;
 
         return 0;
index b020e0d..1eb71e1 100644 (file)
@@ -38,6 +38,10 @@ int device_new_aux(sd_device **ret) {
         *device = (sd_device) {
                 .n_ref = 1,
                 .watch_handle = -1,
+                .ifindex = -1,
+                .devmode = (mode_t) -1,
+                .devuid = (uid_t) -1,
+                .devgid = (gid_t) -1,
         };
 
         *ret = device;
@@ -575,6 +579,9 @@ _public_ int sd_device_get_ifindex(sd_device *device, int *ifindex) {
         if (r < 0)
                 return r;
 
+        if (device->ifindex < 0)
+                return -ENOENT;
+
         *ifindex = device->ifindex;
 
         return 0;
@@ -839,6 +846,9 @@ _public_ int sd_device_get_devtype(sd_device *device, const char **devtype) {
         if (r < 0)
                 return r;
 
+        if (!device->devtype)
+                return -ENOENT;
+
         *devtype = device->devtype;
 
         return 0;
@@ -886,6 +896,9 @@ _public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
         if (r < 0)
                 return r;
 
+        if (major(device->devnum) <= 0)
+                return -ENOENT;
+
         *devnum = device->devnum;
 
         return 0;
@@ -1053,6 +1066,9 @@ _public_ int sd_device_get_sysnum(sd_device *device, const char **ret) {
                         return r;
         }
 
+        if (!device->sysnum)
+                return -ENOENT;
+
         *ret = device->sysnum;
 
         return 0;
@@ -1216,15 +1232,7 @@ int device_get_id_filename(sd_device *device, const char **ret) {
                 if (r < 0)
                         return r;
 
-                r = sd_device_get_devnum(device, &devnum);
-                if (r < 0)
-                        return r;
-
-                r = sd_device_get_ifindex(device, &ifindex);
-                if (r < 0)
-                        return r;
-
-                if (major(devnum) > 0) {
+                if (sd_device_get_devnum(device, &devnum) >= 0) {
                         assert(subsystem);
 
                         /* use dev_t — b259:131072, c254:0 */
@@ -1233,7 +1241,7 @@ int device_get_id_filename(sd_device *device, const char **ret) {
                                      major(devnum), minor(devnum));
                         if (r < 0)
                                 return -ENOMEM;
-                } else if (ifindex > 0) {
+                } else if (sd_device_get_ifindex(device, &ifindex) >= 0 && ifindex > 0) {
                         /* use netdev ifindex — n3 */
                         r = asprintf(&id, "n%u", ifindex);
                         if (r < 0)
index 2cd5b48..1c3d67a 100644 (file)
@@ -85,7 +85,8 @@ _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device) {
 
         r = sd_device_get_devnum(udev_device->device, &devnum);
         if (r < 0) {
-                errno = -r;
+                if (r != -ENOENT)
+                        errno = -r;
                 return makedev(0, 0);
         }
 
@@ -131,7 +132,8 @@ _public_ const char *udev_device_get_devtype(struct udev_device *udev_device) {
 
         r = sd_device_get_devtype(udev_device->device, &devtype);
         if (r < 0) {
-                errno = -r;
+                if (r != -ENOENT)
+                        errno = -r;
                 return NULL;
         }
 
@@ -582,7 +584,8 @@ _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device) {
 
         r = sd_device_get_sysnum(udev_device->device, &sysnum);
         if (r < 0) {
-                errno = -r;
+                if (r != -ENOENT)
+                        errno = -r;
                 return NULL;
         }