summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-device
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-02-10 22:15:01 +0100
committerLennart Poettering <lennart@poettering.net>2021-02-18 18:20:56 +0100
commita11300221482da7ffe7be2d75d508ddd411814f6 (patch)
tree009163435be6a2878d026e70439d731a1587a6e9 /src/libsystemd/sd-device
parentsd-device: don't compare pointers with numeric zero (diff)
downloadsystemd-a11300221482da7ffe7be2d75d508ddd411814f6.tar.xz
systemd-a11300221482da7ffe7be2d75d508ddd411814f6.zip
sd-device: add sd_device_get_action() + sd_device_get_seqnum() + sd_device_new_from_stat_rdev()
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.
Diffstat (limited to 'src/libsystemd/sd-device')
-rw-r--r--src/libsystemd/sd-device/device-internal.h2
-rw-r--r--src/libsystemd/sd-device/device-private.c66
-rw-r--r--src/libsystemd/sd-device/device-private.h20
-rw-r--r--src/libsystemd/sd-device/sd-device.c42
4 files changed, 57 insertions, 73 deletions
diff --git a/src/libsystemd/sd-device/device-internal.h b/src/libsystemd/sd-device/device-internal.h
index 3321c8e2dc..c1a81e3b41 100644
--- a/src/libsystemd/sd-device/device-internal.h
+++ b/src/libsystemd/sd-device/device-internal.h
@@ -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 */
diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c
index c39ac3f3b9..b14f43aae5 100644
--- a/src/libsystemd/sd-device/device-private.c
+++ b/src/libsystemd/sd-device/device-private.c
@@ -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);
}
diff --git a/src/libsystemd/sd-device/device-private.h b/src/libsystemd/sd-device/device-private.h
index e8bf2f547e..ec76f772e5 100644
--- a/src/libsystemd/sd-device/device-private.h
+++ b/src/libsystemd/sd-device/device-private.h
@@ -10,22 +10,8 @@
#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);
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c
index a3a498f9e2..bf9397c03d 100644
--- a/src/libsystemd/sd-device/sd-device.c
+++ b/src/libsystemd/sd-device/sd-device.c
@@ -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);