]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: add sd_device_get_action() + sd_device_get_seqnum() + sd_device_new_from_s...
authorLennart Poettering <lennart@poettering.net>
Wed, 10 Feb 2021 21:15:01 +0000 (22:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Feb 2021 17:20:56 +0000 (18:20 +0100)
To make sd-device properly usable for all programs we need to provide an
API for the "action" field of an event, it's one of the most relevant
ones, and it was so far missing.

This also adds sd_device_get_seqnum(), which isn't that interesting,
except for generating pretty debug output, which we use it ourselves
for.

This also makes device_new_from_stat_rdev() public, as it is truly
useful, as we can see in our own uses of it, and I think is fairly
generic to show up in the public APIs.

27 files changed:
src/core/device.c
src/core/swap.c
src/home/homed-manager.c
src/journal/journalctl.c
src/libsystemd/libsystemd.sym
src/libsystemd/sd-device/device-internal.h
src/libsystemd/sd-device/device-private.c
src/libsystemd/sd-device/device-private.h
src/libsystemd/sd-device/sd-device.c
src/libudev/libudev-device.c
src/login/logind-core.c
src/login/logind.c
src/network/networkd-manager.c
src/shared/dissect-image.c
src/shared/udev-util.c
src/shared/udev-util.h
src/systemd/sd-device.h
src/test/test-tables.c
src/udev/fido_id/fido_id.c
src/udev/net/link-config.c
src/udev/udev-event.c
src/udev/udev-rules.c
src/udev/udevadm-monitor.c
src/udev/udevadm-test.c
src/udev/udevadm-trigger.c
src/udev/udevadm-util.c
src/udev/udevd.c

index db39479c8fc4c9a1e38a65b503bcea138cbee1f6..356c389c55c1569124cc693c9495fc163c6acb26 100644 (file)
@@ -7,7 +7,6 @@
 #include "bus-error.h"
 #include "dbus-device.h"
 #include "dbus-unit.h"
-#include "device-private.h"
 #include "device-util.h"
 #include "device.h"
 #include "log.h"
@@ -916,8 +915,8 @@ static int device_remove_old(Manager *m, sd_device *dev) {
 }
 
 static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
+        sd_device_action_t action;
         Manager *m = userdata;
-        DeviceAction action;
         const char *sysfs;
         int r;
 
@@ -930,22 +929,22 @@ static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *
                 return 0;
         }
 
-        r = device_get_action(dev, &action);
+        r = sd_device_get_action(dev, &action);
         if (r < 0) {
                 log_device_error_errno(dev, r, "Failed to get udev action: %m");
                 return 0;
         }
 
-        if (!IN_SET(action, DEVICE_ACTION_ADD, DEVICE_ACTION_REMOVE, DEVICE_ACTION_MOVE))
+        if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_REMOVE, SD_DEVICE_MOVE))
                 device_propagate_reload_by_sysfs(m, sysfs);
 
-        if (action == DEVICE_ACTION_MOVE)
+        if (action == SD_DEVICE_MOVE)
                 (void) device_remove_old(m, dev);
 
         /* A change event can signal that a device is becoming ready, in particular if the device is using
          * the SYSTEMD_READY logic in udev so we need to reach the else block of the following if, even for
          * change events */
-        if (action == DEVICE_ACTION_REMOVE) {
+        if (action == SD_DEVICE_REMOVE) {
                 r = swap_process_device_remove(m, dev);
                 if (r < 0)
                         log_device_warning_errno(dev, r, "Failed to process swap device remove event, ignoring: %m");
@@ -1013,7 +1012,7 @@ static int validate_node(Manager *m, const char *node, sd_device **ret) {
         } else {
                 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
 
-                r = device_new_from_stat_rdev(&dev, &st);
+                r = sd_device_new_from_stat_rdev(&dev, &st);
                 if (r == -ENOENT) {
                         *ret = NULL;
                         return 1; /* good! (though missing) */
index 5746940fa8771a15f33dcc09eeb41f263bbf9c9d..a81b1928b89986fc432b4fa4d6e14da5b5a57701 100644 (file)
@@ -10,7 +10,6 @@
 #include "alloc-util.h"
 #include "dbus-swap.h"
 #include "dbus-unit.h"
-#include "device-private.h"
 #include "device-util.h"
 #include "device.h"
 #include "escape.h"
@@ -307,7 +306,7 @@ static int swap_load_devnode(Swap *s) {
         if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
                 return 0;
 
-        r = device_new_from_stat_rdev(&d, &st);
+        r = sd_device_new_from_stat_rdev(&d, &st);
         if (r < 0) {
                 log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
                                     "Failed to allocate device for swap %s: %m", s->what);
@@ -510,7 +509,7 @@ static int swap_process_new(Manager *m, const char *device, int prio, bool set_f
         if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
                 return 0;
 
-        r = device_new_from_stat_rdev(&d, &st);
+        r = sd_device_new_from_stat_rdev(&d, &st);
         if (r < 0) {
                 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
                                "Failed to allocate device for swap %s: %m", device);
index 1b13002627f54cf0372c78d710dc5d2736c6edfa..f0c844b0cb0bfdc2e11dd0dd79d586bf8b83e9ec 100644 (file)
@@ -1234,7 +1234,7 @@ static int manager_on_device(sd_device_monitor *monitor, sd_device *d, void *use
         assert(m);
         assert(d);
 
-        if (device_for_action(d, DEVICE_ACTION_REMOVE)) {
+        if (device_for_action(d, SD_DEVICE_REMOVE)) {
                 const char *sysfs;
                 Home *h;
 
index 3de3b4e26f06acec5e3c88d69002fa08451d733e..823cf0a25f42e3a2b581e518d0026a3265732b3e 100644 (file)
@@ -30,7 +30,6 @@
 #include "catalog.h"
 #include "chattr-util.h"
 #include "def.h"
-#include "device-private.h"
 #include "dissect-image.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -207,7 +206,7 @@ static int add_matches_for_device(sd_journal *j, const char *devpath) {
         if (stat(devpath, &st) < 0)
                 return log_error_errno(errno, "Couldn't stat file: %m");
 
-        r = device_new_from_stat_rdev(&device, &st);
+        r = sd_device_new_from_stat_rdev(&device, &st);
         if (r < 0)
                 return log_error_errno(r, "Failed to get device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
 
index f9970a2e52db1e931c01e9da7a6706d6d3b1e30c..fbe9803d42c205c416a92aece96e904b356ee91f 100644 (file)
@@ -745,4 +745,8 @@ global:
         sd_event_source_set_ratelimit;
         sd_event_source_get_ratelimit;
         sd_event_source_is_ratelimited;
+
+        sd_device_get_action;
+        sd_device_get_seqnum;
+        sd_device_new_from_stat_rdev;
 } LIBSYSTEMD_247;
index 3321c8e2dc6542b93f5709a0aee2e89fd5f70c4c..c1a81e3b41a1119d92921875f4d3fd6f3e93d1cf 100644 (file)
@@ -76,7 +76,7 @@ struct sd_device {
         gid_t devgid;
 
         /* only set when device is passed through netlink */
-        DeviceAction action;
+        sd_device_action_t action;
         uint64_t seqnum;
 
         bool parent_set:1; /* no need to try to reload parent */
index c39ac3f3b959619333477b6622aae16060df2802..b14f43aae53444913e792c15e8f6b6d56f6de20d 100644 (file)
@@ -183,20 +183,8 @@ static int device_set_devgid(sd_device *device, const char *gid) {
         return 0;
 }
 
-int device_get_action(sd_device *device, DeviceAction *action) {
-        assert(device);
-
-        if (device->action < 0)
-                return -ENOENT;
-
-        if (action)
-                *action = device->action;
-
-        return 0;
-}
-
 static int device_set_action(sd_device *device, const char *action) {
-        DeviceAction a;
+        sd_device_action_t a;
         int r;
 
         assert(device);
@@ -206,7 +194,7 @@ static int device_set_action(sd_device *device, const char *action) {
         if (a < 0)
                 return a;
 
-        r = device_add_property_internal(device, "ACTION", action);
+        r = device_add_property_internal(device, "ACTION", device_action_to_string(a));
         if (r < 0)
                 return r;
 
@@ -215,18 +203,6 @@ static int device_set_action(sd_device *device, const char *action) {
         return 0;
 }
 
-int device_get_seqnum(sd_device *device, uint64_t *seqnum) {
-        assert(device);
-
-        if (device->seqnum == 0)
-                return -ENOENT;
-
-        if (seqnum)
-                *seqnum = device->seqnum;
-
-        return 0;
-}
-
 static int device_set_seqnum(sd_device *device, const char *str) {
         uint64_t seqnum;
         int r;
@@ -724,22 +700,6 @@ int device_new_from_synthetic_event(sd_device **new_device, const char *syspath,
         return 0;
 }
 
-int device_new_from_stat_rdev(sd_device **ret, const struct stat *st) {
-        char type;
-
-        assert(ret);
-        assert(st);
-
-        if (S_ISBLK(st->st_mode))
-                type = 'b';
-        else if (S_ISCHR(st->st_mode))
-                type = 'c';
-        else
-                return -ENOTTY;
-
-        return sd_device_new_from_devnum(ret, type, st->st_rdev);
-}
-
 int device_copy_properties(sd_device *device_dst, sd_device *device_src) {
         const char *property, *value;
         int r;
@@ -1003,19 +963,19 @@ int device_delete_db(sd_device *device) {
         return 0;
 }
 
-static const char* const device_action_table[_DEVICE_ACTION_MAX] = {
-        [DEVICE_ACTION_ADD]     = "add",
-        [DEVICE_ACTION_REMOVE]  = "remove",
-        [DEVICE_ACTION_CHANGE]  = "change",
-        [DEVICE_ACTION_MOVE]    = "move",
-        [DEVICE_ACTION_ONLINE]  = "online",
-        [DEVICE_ACTION_OFFLINE] = "offline",
-        [DEVICE_ACTION_BIND]    = "bind",
-        [DEVICE_ACTION_UNBIND]  = "unbind",
+static const char* const device_action_table[_SD_DEVICE_ACTION_MAX] = {
+        [SD_DEVICE_ADD]     = "add",
+        [SD_DEVICE_REMOVE]  = "remove",
+        [SD_DEVICE_CHANGE]  = "change",
+        [SD_DEVICE_MOVE]    = "move",
+        [SD_DEVICE_ONLINE]  = "online",
+        [SD_DEVICE_OFFLINE] = "offline",
+        [SD_DEVICE_BIND]    = "bind",
+        [SD_DEVICE_UNBIND]  = "unbind",
 };
 
-DEFINE_STRING_TABLE_LOOKUP(device_action, DeviceAction);
+DEFINE_STRING_TABLE_LOOKUP(device_action, sd_device_action_t);
 
 void dump_device_action_table(void) {
-        DUMP_STRING_TABLE(device_action, DeviceAction, _DEVICE_ACTION_MAX);
+        DUMP_STRING_TABLE(device_action, sd_device_action_t, _SD_DEVICE_ACTION_MAX);
 }
index e8bf2f547e8727538a631365115d0b63dbe5931d..ec76f772e5ff5386c2a3b3f354d2a93dcc4d1f14 100644 (file)
 
 #include "macro.h"
 
-typedef enum DeviceAction {
-        DEVICE_ACTION_ADD,
-        DEVICE_ACTION_REMOVE,
-        DEVICE_ACTION_CHANGE,
-        DEVICE_ACTION_MOVE,
-        DEVICE_ACTION_ONLINE,
-        DEVICE_ACTION_OFFLINE,
-        DEVICE_ACTION_BIND,
-        DEVICE_ACTION_UNBIND,
-        _DEVICE_ACTION_MAX,
-        _DEVICE_ACTION_INVALID = -EINVAL,
-} DeviceAction;
-
 int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len);
 int device_new_from_strv(sd_device **ret, char **strv);
-int device_new_from_stat_rdev(sd_device **ret, const struct stat *st);
 
 int device_get_id_filename(sd_device *device, const char **ret);
 
@@ -34,8 +20,6 @@ int device_get_watch_handle(sd_device *device, int *handle);
 int device_get_devnode_mode(sd_device *device, mode_t *mode);
 int device_get_devnode_uid(sd_device *device, uid_t *uid);
 int device_get_devnode_gid(sd_device *device, gid_t *gid);
-int device_get_action(sd_device *device, DeviceAction *action);
-int device_get_seqnum(sd_device *device, uint64_t *seqnum);
 
 void device_seal(sd_device *device);
 void device_set_is_initialized(sd_device *device);
@@ -73,6 +57,6 @@ static inline int device_read_db(sd_device *device) {
         return device_read_db_internal(device, false);
 }
 
-DeviceAction device_action_from_string(const char *s) _pure_;
-const char *device_action_to_string(DeviceAction a) _const_;
+sd_device_action_t device_action_from_string(const char *s) _pure_;
+const char *device_action_to_string(sd_device_action_t a) _const_;
 void dump_device_action_table(void);
index a3a498f9e248a521378fe65a44dc5657c50c7c5b..bf9397c03d21bf7f5138b68ac2b2fb2aca612504 100644 (file)
@@ -43,7 +43,7 @@ int device_new_aux(sd_device **ret) {
                 .devmode = (mode_t) -1,
                 .devuid = (uid_t) -1,
                 .devgid = (gid_t) -1,
-                .action = _DEVICE_ACTION_INVALID,
+                .action = _SD_DEVICE_ACTION_INVALID,
         };
 
         *ret = device;
@@ -316,6 +316,22 @@ _public_ int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *s
         return -ENODEV;
 }
 
+_public_ int sd_device_new_from_stat_rdev(sd_device **ret, const struct stat *st) {
+        char type;
+
+        assert_return(ret, -EINVAL);
+        assert_return(st, -EINVAL);
+
+        if (S_ISBLK(st->st_mode))
+                type = 'b';
+        else if (S_ISCHR(st->st_mode))
+                type = 'c';
+        else
+                return -ENOTTY;
+
+        return sd_device_new_from_devnum(ret, type, st->st_rdev);
+}
+
 int device_set_devtype(sd_device *device, const char *devtype) {
         _cleanup_free_ char *t = NULL;
         int r;
@@ -1052,6 +1068,30 @@ _public_ int sd_device_get_sysnum(sd_device *device, const char **ret) {
         return 0;
 }
 
+_public_ int sd_device_get_action(sd_device *device, sd_device_action_t *ret) {
+        assert_return(device, -EINVAL);
+
+        if (device->action < 0)
+                return -ENOENT;
+
+        if (ret)
+                *ret = device->action;
+
+        return 0;
+}
+
+_public_ int sd_device_get_seqnum(sd_device *device, uint64_t *ret) {
+        assert_return(device, -EINVAL);
+
+        if (device->seqnum == 0)
+                return -ENOENT;
+
+        if (ret)
+                *ret = device->seqnum;
+
+        return 0;
+}
+
 static bool is_valid_tag(const char *tag) {
         assert(tag);
 
index 34543a8b2ed658f848b9721d332ef6b5afa9113f..67f4b48da117a417c8f4e4f3fa511fbefcca1cbc 100644 (file)
@@ -82,7 +82,7 @@ _public_ unsigned long long udev_device_get_seqnum(struct udev_device *udev_devi
 
         assert_return_errno(udev_device, 0, EINVAL);
 
-        if (device_get_seqnum(udev_device->device, &seqnum) < 0)
+        if (sd_device_get_seqnum(udev_device->device, &seqnum) < 0)
                 return 0;
 
         return seqnum;
@@ -693,11 +693,11 @@ _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct ud
  * Returns: the kernel action value, or #NULL if there is no action value available.
  **/
 _public_ const char *udev_device_get_action(struct udev_device *udev_device) {
-        DeviceAction action;
+        sd_device_action_t action;
 
         assert_return_errno(udev_device, NULL, EINVAL);
 
-        if (device_get_action(udev_device->device, &action) < 0)
+        if (sd_device_get_action(udev_device->device, &action) < 0)
                 return NULL;
 
         return device_action_to_string(action);
index 3595d7a77322a3df5d1ea8984ec2bd3098a4cad9..2ecf2120fdbe5105affac2631e7e4e1c54b1f6ab 100644 (file)
@@ -246,7 +246,7 @@ int manager_process_seat_device(Manager *m, sd_device *d) {
 
         assert(m);
 
-        if (device_for_action(d, DEVICE_ACTION_REMOVE) ||
+        if (device_for_action(d, SD_DEVICE_REMOVE) ||
             sd_device_has_current_tag(d, "seat") <= 0) {
                 const char *syspath;
 
@@ -317,7 +317,7 @@ int manager_process_button_device(Manager *m, sd_device *d) {
         if (r < 0)
                 return r;
 
-        if (device_for_action(d, DEVICE_ACTION_REMOVE) ||
+        if (device_for_action(d, SD_DEVICE_REMOVE) ||
             sd_device_has_current_tag(d, "power-switch") <= 0) {
 
                 b = hashmap_get(m->buttons, sysname);
index 3dae6955548459363898e6c49269274c7bdcacd9..dac0bd2728de852628e1c38a8b7ab5535c7d556c 100644 (file)
@@ -578,7 +578,7 @@ static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *dev
 
         if (sd_device_get_sysname(device, &name) >= 0 &&
             startswith(name, "vcsa") &&
-            device_for_action(device, DEVICE_ACTION_REMOVE))
+            device_for_action(device, SD_DEVICE_REMOVE))
                 seat_preallocate_vts(m->seat0);
 
         return 0;
index 70c5c30b30eababd768db5fe8ad7c92b1ac6c4fb..a6f343a1f63709d9fa3bb481d6c3f3b5a444ae08 100644 (file)
@@ -182,15 +182,15 @@ int manager_connect_bus(Manager *m) {
 }
 
 static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
+        sd_device_action_t action;
         Manager *m = userdata;
-        DeviceAction action;
         Link *link = NULL;
         int r, ifindex;
 
         assert(m);
         assert(device);
 
-        r = device_get_action(device, &action);
+        r = sd_device_get_action(device, &action);
         if (r < 0) {
                 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
                 return 0;
@@ -199,7 +199,7 @@ static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *devi
         /* Ignore the "remove" uevent â€” let's remove a device only if rtnetlink says so. All other uevents
          * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
          * still exists â€” and we are interested in that. */
-        if (action == DEVICE_ACTION_REMOVE)
+        if (action == SD_DEVICE_REMOVE)
                 return 0;
 
         r = sd_device_get_ifindex(device, &ifindex);
index 2799f4f268ef43b82751c18e5ed15cca9ed0a2a7..6d24c3fd9ff149a9dac5e3acc0e92bcefb30f2fb 100644 (file)
@@ -247,7 +247,7 @@ static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device,
 
         assert(w);
 
-        if (device_for_action(device, DEVICE_ACTION_REMOVE))
+        if (device_for_action(device, SD_DEVICE_REMOVE))
                 return 0;
 
         r = sd_device_get_parent(device, &pp);
index 61decb80fa2b1aecd24524e8f59426f730f6091f..3a3f0198fdb20778d8060987255fc27e75d58a8a 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "alloc-util.h"
 #include "device-nodes.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "env-file.h"
 #include "escape.h"
@@ -175,7 +176,7 @@ static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device,
          * (And yes, we only need to special case REMOVE. It's the only "negative" event type, where a device
          * ceases to exist. All other event types are "positive": the device exists and is registered in the
          * udev database, thus whenever we see the event, we can consider it initialized.) */
-        if (device_for_action(device, DEVICE_ACTION_REMOVE))
+        if (device_for_action(device, SD_DEVICE_REMOVE))
                 return 0;
 
         if (data->sysname && sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname))
@@ -318,26 +319,29 @@ int device_is_renaming(sd_device *dev) {
         return true;
 }
 
-bool device_for_action(sd_device *dev, DeviceAction action) {
-        DeviceAction a;
+bool device_for_action(sd_device *dev, sd_device_action_t a) {
+        sd_device_action_t b;
 
         assert(dev);
 
-        if (device_get_action(dev, &a) < 0)
+        if (a < 0)
                 return false;
 
-        return a == action;
+        if (sd_device_get_action(dev, &b) < 0)
+                return false;
+
+        return a == b;
 }
 
 void log_device_uevent(sd_device *device, const char *str) {
-        DeviceAction action = _DEVICE_ACTION_INVALID;
+        sd_device_action_t action = _SD_DEVICE_ACTION_INVALID;
         uint64_t seqnum = 0;
 
         if (!DEBUG_LOGGING)
                 return;
 
-        (void) device_get_seqnum(device, &seqnum);
-        (void) device_get_action(device, &action);
+        (void) sd_device_get_seqnum(device, &seqnum);
+        (void) sd_device_get_action(device, &action);
         log_device_debug(device, "%s%s(SEQNUM=%"PRIu64", ACTION=%s)",
                          strempty(str), isempty(str) ? "" : " ",
                          seqnum, strna(device_action_to_string(action)));
index 5fce03b25f304f5548953b08ca0024b5ff9e5c0d..c2cf7caa67054ef025a4ff538b45eec3aef28cc6 100644 (file)
@@ -3,7 +3,6 @@
 
 #include "sd-device.h"
 
-#include "device-private.h"
 #include "time-util.h"
 
 #define UDEV_NAME_SIZE   512
@@ -35,7 +34,8 @@ static inline int udev_parse_config(void) {
 int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t deadline, sd_device **ret);
 int device_wait_for_devlink(const char *path, const char *subsystem, usec_t deadline, sd_device **ret);
 int device_is_renaming(sd_device *dev);
-bool device_for_action(sd_device *dev, DeviceAction action);
+
+bool device_for_action(sd_device *dev, sd_device_action_t action);
 
 void log_device_uevent(sd_device *device, const char *str);
 
index 78fe5841609c6652adf3e74004cddc46397de6a2..af0e26e2d76dd4db4b5017077e2dc324d9cba25e 100644 (file)
@@ -17,7 +17,9 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <errno.h>
 #include <inttypes.h>
+#include <sys/stat.h>
 #include <sys/sysmacros.h>
 #include <sys/types.h>
 
@@ -31,6 +33,20 @@ typedef struct sd_device sd_device;
 typedef struct sd_device_enumerator sd_device_enumerator;
 typedef struct sd_device_monitor sd_device_monitor;
 
+typedef enum sd_device_action_t {
+        SD_DEVICE_ADD,
+        SD_DEVICE_REMOVE,
+        SD_DEVICE_CHANGE,
+        SD_DEVICE_MOVE,
+        SD_DEVICE_ONLINE,
+        SD_DEVICE_OFFLINE,
+        SD_DEVICE_BIND,
+        SD_DEVICE_UNBIND,
+        _SD_DEVICE_ACTION_MAX,
+        _SD_DEVICE_ACTION_INVALID = -EINVAL,
+        _SD_ENUM_FORCE_S64(DEVICE_ACTION),
+} sd_device_action_t;
+
 /* callback */
 
 typedef int (*sd_device_monitor_handler_t)(sd_device_monitor *m, sd_device *device, void *userdata);
@@ -44,6 +60,7 @@ int sd_device_new_from_syspath(sd_device **ret, const char *syspath);
 int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum);
 int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *subsystem, const char *sysname);
 int sd_device_new_from_device_id(sd_device **ret, const char *id);
+int sd_device_new_from_stat_rdev(sd_device **ret, const struct stat *st);
 
 int sd_device_get_parent(sd_device *child, sd_device **ret);
 int sd_device_get_parent_with_subsystem_devtype(sd_device *child, const char *subsystem, const char *devtype, sd_device **ret);
@@ -58,6 +75,8 @@ int sd_device_get_devpath(sd_device *device, const char **ret);
 int sd_device_get_devname(sd_device *device, const char **ret);
 int sd_device_get_sysname(sd_device *device, const char **ret);
 int sd_device_get_sysnum(sd_device *device, const char **ret);
+int sd_device_get_action(sd_device *device, sd_device_action_t *ret);
+int sd_device_get_seqnum(sd_device *device, uint64_t *ret);
 
 int sd_device_get_is_initialized(sd_device *device);
 int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *usec);
index cc93bbbc749263fda4930b4f25c40cb82279360e..3e5df0459004a8b04030899270449acb9bc96b8d 100644 (file)
@@ -50,7 +50,7 @@ int main(int argc, char **argv) {
         test_table(collect_mode, COLLECT_MODE);
         test_table(condition_result, CONDITION_RESULT);
         test_table(condition_type, CONDITION_TYPE);
-        test_table(device_action, DEVICE_ACTION);
+        test_table(device_action, SD_DEVICE_ACTION);
         test_table(device_state, DEVICE_STATE);
         test_table(dns_over_tls_mode, DNS_OVER_TLS_MODE);
         test_table(dnssec_mode, DNSSEC_MODE);
@@ -125,5 +125,7 @@ int main(int argc, char **argv) {
 
         test_table_sparse(object_compressed, OBJECT_COMPRESSED);
 
+        assert_cc(sizeof(sd_device_action_t) == sizeof(int64_t));
+
         return EXIT_SUCCESS;
 }
index f14b81d30df9354bd1da0854e964036d60575bc8..a9f5f8f8a64a0a6c934216a13deac260506b7aac 100644 (file)
@@ -14,7 +14,6 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "device-internal.h"
 #include "device-private.h"
 #include "device-util.h"
 #include "fd-util.h"
index f2ca1bb56105ec6159537e92df8f7f10ebe14006..10d64d526085302aa3489c1107bc07d083e08a3b 100644 (file)
@@ -594,7 +594,7 @@ static int link_config_apply_alternative_names(sd_netlink **rtnl, const link_con
 
 int link_config_apply(link_config_ctx *ctx, const link_config *config, sd_device *device, const char **ret_name) {
         const char *new_name;
-        DeviceAction a;
+        sd_device_action_t a;
         int r;
 
         assert(ctx);
@@ -602,11 +602,11 @@ int link_config_apply(link_config_ctx *ctx, const link_config *config, sd_device
         assert(device);
         assert(ret_name);
 
-        r = device_get_action(device, &a);
+        r = sd_device_get_action(device, &a);
         if (r < 0)
                 return log_device_error_errno(device, r, "Failed to get ACTION= property: %m");
 
-        if (!IN_SET(a, DEVICE_ACTION_ADD, DEVICE_ACTION_BIND, DEVICE_ACTION_MOVE)) {
+        if (!IN_SET(a, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) {
                 log_device_debug(device, "Skipping to apply .link settings on '%s' uevent.", device_action_to_string(a));
 
                 r = sd_device_get_sysname(device, ret_name);
@@ -624,7 +624,7 @@ int link_config_apply(link_config_ctx *ctx, const link_config *config, sd_device
         if (r < 0)
                 return r;
 
-        if (a == DEVICE_ACTION_MOVE) {
+        if (a == SD_DEVICE_MOVE) {
                 log_device_debug(device, "Skipping to apply Name= and NamePolicy= on '%s' uevent.", device_action_to_string(a));
 
                 r = sd_device_get_sysname(device, &new_name);
index 8aeaf92e41cd05e7681dd11efaf1cdb4d326da12..78a999d00a1ed316b1f809aa5e16cb9525270f4a 100644 (file)
@@ -831,7 +831,7 @@ static int rename_netif(UdevEvent *event) {
         if (streq(event->name, oldname))
                 return 0; /* The interface name is already requested name. */
 
-        if (!device_for_action(dev, DEVICE_ACTION_ADD))
+        if (!device_for_action(dev, SD_DEVICE_ADD))
                 return 0; /* Rename the interface only when it is added. */
 
         r = sd_device_get_ifindex(dev, &ifindex);
@@ -904,7 +904,7 @@ static int update_devnode(UdevEvent *event) {
                 /* If group is set, but mode is not set, "upgrade" mode for the group. */
                 event->mode = 0660;
 
-        bool apply_mac = device_for_action(dev, DEVICE_ACTION_ADD);
+        bool apply_mac = device_for_action(dev, SD_DEVICE_ADD);
 
         return udev_node_add(dev, apply_mac, event->mode, event->uid, event->gid, event->seclabel_list);
 }
@@ -975,7 +975,7 @@ int udev_event_execute_rules(UdevEvent *event,
                              Hashmap *properties_list,
                              UdevRules *rules) {
         const char *subsystem;
-        DeviceAction action;
+        sd_device_action_t action;
         sd_device *dev;
         int r;
 
@@ -988,11 +988,11 @@ int udev_event_execute_rules(UdevEvent *event,
         if (r < 0)
                 return log_device_error_errno(dev, r, "Failed to get subsystem: %m");
 
-        r = device_get_action(dev, &action);
+        r = sd_device_get_action(dev, &action);
         if (r < 0)
                 return log_device_error_errno(dev, r, "Failed to get ACTION: %m");
 
-        if (action == DEVICE_ACTION_REMOVE) {
+        if (action == SD_DEVICE_REMOVE) {
                 event_execute_rules_on_remove(event, timeout_usec, timeout_signal, properties_list, rules);
                 return 0;
         }
@@ -1009,7 +1009,7 @@ int udev_event_execute_rules(UdevEvent *event,
                 /* Disable watch during event processing. */
                 (void) udev_watch_end(event->dev_db_clone);
 
-        if (action == DEVICE_ACTION_MOVE) {
+        if (action == SD_DEVICE_MOVE) {
                 r = udev_event_on_move(event->dev);
                 if (r < 0)
                         return r;
index 78184b4136d21bf7cae7389795f1d3b8571f1670..0f10116a997075600d9d3311e3b9f638b266c05e 100644 (file)
@@ -6,6 +6,7 @@
 #include "architecture.h"
 #include "conf-files.h"
 #include "def.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "dirent-util.h"
 #include "escape.h"
@@ -1545,9 +1546,9 @@ static int udev_rule_apply_token_to_event(
 
         switch (token->type) {
         case TK_M_ACTION: {
-                DeviceAction a;
+                sd_device_action_t a;
 
-                r = device_get_action(dev, &a);
+                r = sd_device_get_action(dev, &a);
                 if (r < 0)
                         return log_rule_error_errno(dev, rules, r, "Failed to get uevent action type: %m");
 
@@ -2226,14 +2227,14 @@ static int udev_rule_apply_line_to_event(
         UdevRuleLineType mask = LINE_HAS_GOTO | LINE_UPDATE_SOMETHING;
         UdevRuleToken *token, *next_token;
         bool parents_done = false;
-        DeviceAction action;
+        sd_device_action_t action;
         int r;
 
-        r = device_get_action(event->dev, &action);
+        r = sd_device_get_action(event->dev, &action);
         if (r < 0)
                 return r;
 
-        if (action != DEVICE_ACTION_REMOVE) {
+        if (action != SD_DEVICE_REMOVE) {
                 if (sd_device_get_devnum(event->dev, NULL) >= 0)
                         mask |= LINE_HAS_DEVLINK;
 
index 1a8a1a5b3b5b19bdb03031f145a4416b8e11f700..00b03c550d568e9be1e9c6df52f818bce3c1050b 100644 (file)
@@ -27,7 +27,7 @@ static Set *arg_tag_filter = NULL;
 static Hashmap *arg_subsystem_filter = NULL;
 
 static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
-        DeviceAction action = _DEVICE_ACTION_INVALID;
+        sd_device_action_t action = _SD_DEVICE_ACTION_INVALID;
         const char *devpath = NULL, *subsystem = NULL;
         MonitorNetlinkGroup group = PTR_TO_INT(userdata);
         struct timespec ts;
@@ -35,7 +35,7 @@ static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device,
         assert(device);
         assert(IN_SET(group, MONITOR_GROUP_UDEV, MONITOR_GROUP_KERNEL));
 
-        (void) device_get_action(device, &action);
+        (void) sd_device_get_action(device, &action);
         (void) sd_device_get_devpath(device, &devpath);
         (void) sd_device_get_subsystem(device, &subsystem);
 
index e28378eea81c4156fe4f5e7f2448ac1d02c11093..bbee79c88e222094dcadb5c9e327940786be1611 100644 (file)
@@ -54,7 +54,7 @@ static int parse_argv(int argc, char *argv[]) {
         while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
                 switch (c) {
                 case 'a': {
-                        DeviceAction a;
+                        sd_device_action_t a;
 
                         if (streq(optarg, "help")) {
                                 dump_device_action_table();
index a4588cb4e4f5097c68268de8a052b366ff534c3a..908c27495f8da95d000572927a03ba2e1cda21a3 100644 (file)
@@ -211,7 +211,7 @@ int trigger_main(int argc, char *argv[], void *userdata) {
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type --type=%s", optarg);
                         break;
                 case 'c': {
-                        DeviceAction a;
+                        sd_device_action_t a;
 
                         if (streq(optarg, "help")) {
                                 dump_device_action_table();
index 39d0c7eb3f142206c5bbdf885a097fa23572ee68..10191d88ba9dbc22967c3687c44f65e4e2c62deb 100644 (file)
@@ -20,7 +20,7 @@ static int find_device_from_path(const char *path, sd_device **ret) {
                 if (stat(path, &st) < 0)
                         return -errno;
 
-                return device_new_from_stat_rdev(ret, &st);
+                return sd_device_new_from_stat_rdev(ret, &st);
         }
 
         return -EINVAL;
index 9772246aeb2298c800453d7b4fe804f6902ae11e..0657bf1b6da9634ee4cdbc9789eb079dcce43284 100644 (file)
@@ -325,16 +325,12 @@ static int worker_lock_block_device(sd_device *dev, int *ret_fd) {
         assert(dev);
         assert(ret_fd);
 
-        /*
-         * Take a shared lock on the device node; this establishes
-         * a concept of device "ownership" to serialize device
-         * access. External processes holding an exclusive lock will
-         * cause udev to skip the event handling; in the case udev
-         * acquired the lock, the external process can block until
-         * udev has finished its event handling.
-         */
+        /* Take a shared lock on the device node; this establishes a concept of device "ownership" to
+         * serialize device access. External processes holding an exclusive lock will cause udev to skip the
+         * event handling; in the case udev acquired the lock, the external process can block until udev has
+         * finished its event handling. */
 
-        if (device_for_action(dev, DEVICE_ACTION_REMOVE))
+        if (device_for_action(dev, SD_DEVICE_REMOVE))
                 return 0;
 
         r = sd_device_get_subsystem(dev, &val);
@@ -392,7 +388,7 @@ static int worker_mark_block_device_read_only(sd_device *dev) {
         /* Do this only once, when the block device is new. If the device is later retriggered let's not
          * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit
          * for specific devices only. */
-        if (!device_for_action(dev, DEVICE_ACTION_ADD))
+        if (!device_for_action(dev, SD_DEVICE_ADD))
                 return 0;
 
         r = sd_device_get_subsystem(dev, &val);
@@ -694,7 +690,7 @@ static int event_queue_insert(Manager *manager, sd_device *dev) {
         assert(manager->pid == getpid_cached());
 
         /* We only accepts devices received by device monitor. */
-        r = device_get_seqnum(dev, &seqnum);
+        r = sd_device_get_seqnum(dev, &seqnum);
         if (r < 0)
                 return r;