sd-device: reduce the number of implementations of device_read_db() we keep around

We had two very similar functions: device_read_db_aux and device_read_db,
and a number of wrappers for them:

device_read_db_aux
  ← device_read_db (in sd-device.c)
    ← all functions in sd-device.c, including sd_device_is_initialized

  ← device_read_db_force
     ← event_execute_rules_on_remove (in udev-event.c)

device_read_db (in device-private.c)
  ← functions in device_private.c (but not device_read_db_force):
    device_get_devnode_{mode,uid,gid}
    device_get_devlink_priority
    device_get_watch_handle
    device_clone_with_db
    ← called from udevadm, udev-{node,event,watch}.c

Before 7141e4f62c (sd-device: don't retry loading
uevent/db files more than once), the two implementations were the same. In that
commit, device_read_db_aux was changed. Those changes were reverted in the parent
commit, so the two implementations are now again the same except for superficial
differences. This commit removes device_read_db (in sd-device.c), and renames
device_read_db_aux to device_read_db_internal and makes everyone use this one
implementation. There should be no functional change.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-12-13 15:49:49 +01:00
parent cd53c8f97d
commit ebcc52fad6
5 changed files with 8 additions and 182 deletions

View file

@ -97,7 +97,6 @@ int device_new_aux(sd_device **ret);
int device_add_property_aux(sd_device *device, const char *key, const char *value, bool db);
int device_add_property_internal(sd_device *device, const char *key, const char *value);
int device_read_uevent_file(sd_device *device);
int device_read_db_aux(sd_device *device, bool force);
int device_set_syspath(sd_device *device, const char *_syspath, bool verify);
int device_set_ifindex(sd_device *device, const char *ifindex);

View file

@ -47,81 +47,6 @@ int device_add_property(sd_device *device, const char *key, const char *value) {
return 0;
}
static int device_add_property_internal_from_string(sd_device *device, const char *str) {
_cleanup_free_ char *key = NULL;
char *value;
assert(device);
assert(str);
key = strdup(str);
if (!key)
return -ENOMEM;
value = strchr(key, '=');
if (!value)
return -EINVAL;
*value = '\0';
if (isempty(++value))
value = NULL;
return device_add_property_internal(device, key, value);
}
static int handle_db_line(sd_device *device, char key, const char *value) {
char *path;
int r;
assert(device);
assert(value);
switch (key) {
case 'S':
path = strjoina("/dev/", value);
r = device_add_devlink(device, path);
if (r < 0)
return r;
break;
case 'L':
r = safe_atoi(value, &device->devlink_priority);
if (r < 0)
return r;
break;
case 'E':
r = device_add_property_internal_from_string(device, value);
if (r < 0)
return r;
break;
case 'G':
r = device_add_tag(device, value);
if (r < 0)
return r;
break;
case 'W':
r = safe_atoi(value, &device->watch_handle);
if (r < 0)
return r;
break;
case 'I':
r = device_set_usec_initialized(device, value);
if (r < 0)
return r;
break;
default:
log_device_debug(device, "sd-device: Unknown key '%c' in device db, ignoring", key);
}
return 0;
}
void device_set_devlink_priority(sd_device *device, int priority) {
assert(device);
@ -157,99 +82,6 @@ int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) {
return 0;
}
static int device_read_db(sd_device *device) {
_cleanup_free_ char *db = NULL;
char *path;
const char *id, *value;
char key;
size_t db_len;
unsigned i;
int r;
enum {
PRE_KEY,
KEY,
PRE_VALUE,
VALUE,
INVALID_LINE,
} state = PRE_KEY;
assert(device);
if (device->db_loaded || device->sealed)
return 0;
r = device_get_id_filename(device, &id);
if (r < 0)
return r;
path = strjoina("/run/udev/data/", id);
r = read_full_file(path, &db, &db_len);
if (r < 0) {
if (r == -ENOENT)
return 0;
else
return log_device_debug_errno(device, r, "sd-device: Failed to read db '%s': %m", path);
}
/* devices with a database entry are initialized */
device_set_is_initialized(device);
for (i = 0; i < db_len; i++) {
switch (state) {
case PRE_KEY:
if (!strchr(NEWLINE, db[i])) {
key = db[i];
state = KEY;
}
break;
case KEY:
if (db[i] != ':') {
log_device_debug(device, "sd-device: Invalid db entry with key '%c', ignoring", key);
state = INVALID_LINE;
} else {
db[i] = '\0';
state = PRE_VALUE;
}
break;
case PRE_VALUE:
value = &db[i];
state = VALUE;
break;
case INVALID_LINE:
if (strchr(NEWLINE, db[i]))
state = PRE_KEY;
break;
case VALUE:
if (strchr(NEWLINE, db[i])) {
db[i] = '\0';
r = handle_db_line(device, key, value);
if (r < 0)
log_device_debug_errno(device, r, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key, value);
state = PRE_KEY;
}
break;
default:
assert_not_reached("Invalid state when parsing db");
}
}
device->db_loaded = true;
return 0;
}
uint64_t device_get_properties_generation(sd_device *device) {
assert(device);
@ -1115,9 +947,3 @@ int device_delete_db(sd_device *device) {
return 0;
}
int device_read_db_force(sd_device *device) {
assert(device);
return device_read_db_aux(device, true);
}

View file

@ -49,4 +49,7 @@ int device_new_from_synthetic_event(sd_device **new_device, const char *syspath,
int device_tag_index(sd_device *dev, sd_device *dev_old, bool add);
int device_update_db(sd_device *device);
int device_delete_db(sd_device *device);
int device_read_db_force(sd_device *device);
int device_read_db_internal(sd_device *device, bool force);
static inline int device_read_db(sd_device *device) {
return device_read_db_internal(device, false);
}

View file

@ -1283,7 +1283,7 @@ int device_get_id_filename(sd_device *device, const char **ret) {
return 0;
}
int device_read_db_aux(sd_device *device, bool force) {
int device_read_db_internal(sd_device *device, bool force) {
_cleanup_free_ char *db = NULL;
char *path;
const char *id, *value;
@ -1300,6 +1300,8 @@ int device_read_db_aux(sd_device *device, bool force) {
INVALID_LINE,
} state = PRE_KEY;
assert(device);
if (device->db_loaded || (!force && device->sealed))
return 0;
@ -1374,10 +1376,6 @@ int device_read_db_aux(sd_device *device, bool force) {
return 0;
}
static int device_read_db(sd_device *device) {
return device_read_db_aux(device, false);
}
_public_ int sd_device_get_is_initialized(sd_device *device) {
int r;

View file

@ -785,7 +785,7 @@ static void event_execute_rules_on_remove(
sd_device *dev = event->dev;
int r;
r = device_read_db_force(dev);
r = device_read_db_internal(dev, true);
if (r < 0)
log_device_debug_errno(dev, r, "Failed to read database under /run/udev/data/: %m");