1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

mount-tool: replace udev_device by sd_device

This commit is contained in:
Yu Watanabe 2018-08-22 13:36:15 +09:00
parent 9aadd28149
commit dcd265239b

View File

@ -2,9 +2,10 @@
#include <getopt.h> #include <getopt.h>
#include "libudev.h"
#include "sd-bus.h" #include "sd-bus.h"
#include "sd-device.h"
#include "device-enumerator-private.h"
#include "bus-error.h" #include "bus-error.h"
#include "bus-unit-util.h" #include "bus-unit-util.h"
#include "bus-util.h" #include "bus-util.h"
@ -21,7 +22,6 @@
#include "spawn-polkit-agent.h" #include "spawn-polkit-agent.h"
#include "stat-util.h" #include "stat-util.h"
#include "strv.h" #include "strv.h"
#include "udev-util.h"
#include "unit-def.h" #include "unit-def.h"
#include "unit-name.h" #include "unit-name.h"
#include "user-util.h" #include "user-util.h"
@ -921,8 +921,7 @@ static int stop_mounts(
} }
static int umount_by_device(sd_bus *bus, const char *what) { static int umount_by_device(sd_bus *bus, const char *what) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL; _cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_strv_free_ char **list = NULL; _cleanup_strv_free_ char **list = NULL;
struct stat st; struct stat st;
const char *v; const char *v;
@ -939,22 +938,20 @@ static int umount_by_device(sd_bus *bus, const char *what) {
return -ENOTBLK; return -ENOTBLK;
} }
udev = udev_new(); r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (!udev) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to get device from device number: %m");
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev); r = sd_device_get_property_value(d, "ID_FS_USAGE", &v);
if (!d) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to get device property: %m");
v = udev_device_get_property_value(d, "ID_FS_USAGE"); if (!streq(v, "filesystem")) {
if (!streq_ptr(v, "filesystem")) {
log_error("%s does not contain a known file system.", what); log_error("%s does not contain a known file system.", what);
return -EINVAL; return -EINVAL;
} }
v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE"); if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
if (!isempty(v))
r2 = stop_mounts(bus, v); r2 = stop_mounts(bus, v);
r = find_mount_points(what, &list); r = find_mount_points(what, &list);
@ -1042,7 +1039,7 @@ static int action_umount(
return r2; return r2;
} }
static int acquire_mount_type(struct udev_device *d) { static int acquire_mount_type(sd_device *d) {
const char *v; const char *v;
assert(d); assert(d);
@ -1050,8 +1047,7 @@ static int acquire_mount_type(struct udev_device *d) {
if (arg_mount_type) if (arg_mount_type)
return 0; return 0;
v = udev_device_get_property_value(d, "ID_FS_TYPE"); if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
if (isempty(v))
return 0; return 0;
arg_mount_type = strdup(v); arg_mount_type = strdup(v);
@ -1062,14 +1058,15 @@ static int acquire_mount_type(struct udev_device *d) {
return 1; return 1;
} }
static int acquire_mount_options(struct udev_device *d) { static int acquire_mount_options(sd_device *d) {
const char *v; const char *v;
assert(d);
if (arg_mount_options) if (arg_mount_options)
return 0; return 0;
v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS"); if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
if (isempty(v))
return 0; return 0;
arg_mount_options = strdup(v); arg_mount_options = strdup(v);
@ -1080,38 +1077,41 @@ static int acquire_mount_options(struct udev_device *d) {
return 1; return 1;
} }
static const char *get_model(struct udev_device *d) { static const char *get_model(sd_device *d) {
const char *model; const char *model;
assert(d); assert(d);
model = udev_device_get_property_value(d, "ID_MODEL_FROM_DATABASE"); if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0)
if (model)
return model; return model;
return udev_device_get_property_value(d, "ID_MODEL"); if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0)
return model;
return NULL;
} }
static const char* get_label(struct udev_device *d) { static const char* get_label(sd_device *d) {
const char *label; const char *label;
assert(d); assert(d);
label = udev_device_get_property_value(d, "ID_FS_LABEL"); if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
if (label)
return label; return label;
return udev_device_get_property_value(d, "ID_PART_ENTRY_NAME"); if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
return label;
return NULL;
} }
static int acquire_mount_where(struct udev_device *d) { static int acquire_mount_where(sd_device *d) {
const char *v; const char *v;
if (arg_mount_where) if (arg_mount_where)
return 0; return 0;
v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE"); if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
if (isempty(v)) {
_cleanup_free_ char *escaped = NULL; _cleanup_free_ char *escaped = NULL;
const char *name; const char *name;
@ -1121,8 +1121,7 @@ static int acquire_mount_where(struct udev_device *d) {
if (!name) { if (!name) {
const char *dn; const char *dn;
dn = udev_device_get_devnode(d); if (sd_device_get_devname(d, &dn) < 0)
if (!dn)
return 0; return 0;
name = basename(dn); name = basename(dn);
@ -1171,7 +1170,7 @@ static int acquire_mount_where_for_loop_dev(const char *loop_dev) {
return 1; return 1;
} }
static int acquire_description(struct udev_device *d) { static int acquire_description(sd_device *d) {
const char *model, *label; const char *model, *label;
if (arg_description) if (arg_description)
@ -1181,7 +1180,7 @@ static int acquire_description(struct udev_device *d) {
label = get_label(d); label = get_label(d);
if (!label) if (!label)
label = udev_device_get_property_value(d, "ID_PART_ENTRY_NUMBER"); (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
if (model && label) if (model && label)
arg_description = strjoin(model, " ", label); arg_description = strjoin(model, " ", label);
@ -1199,7 +1198,7 @@ static int acquire_description(struct udev_device *d) {
return 1; return 1;
} }
static int acquire_removable(struct udev_device *d) { static int acquire_removable(sd_device *d) {
const char *v; const char *v;
/* Shortcut this if there's no reason to check it */ /* Shortcut this if there's no reason to check it */
@ -1207,15 +1206,13 @@ static int acquire_removable(struct udev_device *d) {
return 0; return 0;
for (;;) { for (;;) {
v = udev_device_get_sysattr_value(d, "removable"); if (sd_device_get_sysattr_value(d, "removable", &v) > 0)
if (v)
break; break;
d = udev_device_get_parent(d); if (sd_device_get_parent(d, &d) < 0)
if (!d)
return 0; return 0;
if (!streq_ptr(udev_device_get_subsystem(d), "block")) if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
return 0; return 0;
} }
@ -1243,8 +1240,7 @@ static int acquire_removable(struct udev_device *d) {
} }
static int discover_loop_backing_file(void) { static int discover_loop_backing_file(void) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL; _cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
_cleanup_free_ char *loop_dev = NULL; _cleanup_free_ char *loop_dev = NULL;
struct stat st; struct stat st;
const char *v; const char *v;
@ -1284,16 +1280,11 @@ static int discover_loop_backing_file(void) {
return -EINVAL; return -EINVAL;
} }
udev = udev_new(); r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (!udev) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to get device from device number: %m");
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev); if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
if (!d)
return log_oom();
v = udev_device_get_property_value(d, "ID_FS_USAGE");
if (!streq_ptr(v, "filesystem")) {
log_error("%s does not contain a known file system.", arg_mount_what); log_error("%s does not contain a known file system.", arg_mount_what);
return -EINVAL; return -EINVAL;
} }
@ -1318,8 +1309,7 @@ static int discover_loop_backing_file(void) {
} }
static int discover_device(void) { static int discover_device(void) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL; _cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
struct stat st; struct stat st;
const char *v; const char *v;
int r; int r;
@ -1335,16 +1325,11 @@ static int discover_device(void) {
return -EINVAL; return -EINVAL;
} }
udev = udev_new(); r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
if (!udev) if (r < 0)
return log_oom(); return log_error_errno(r, "Failed to get device from device number: %m");
d = udev_device_new_from_devnum(udev, 'b', st.st_rdev); if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
if (!d)
return log_oom();
v = udev_device_get_property_value(d, "ID_FS_USAGE");
if (!streq_ptr(v, "filesystem")) {
log_error("%s does not contain a known file system.", arg_mount_what); log_error("%s does not contain a known file system.", arg_mount_what);
return -EINVAL; return -EINVAL;
} }
@ -1412,49 +1397,36 @@ static int list_devices(void) {
[COLUMN_UUID] = "UUID" [COLUMN_UUID] = "UUID"
}; };
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL; _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
_cleanup_(udev_unrefp) struct udev *udev = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
size_t n_allocated = 0, n = 0, i; size_t n_allocated = 0, n = 0, i;
size_t column_width[_COLUMN_MAX]; size_t column_width[_COLUMN_MAX];
struct item *items = NULL; struct item *items = NULL;
sd_device *d;
unsigned c; unsigned c;
int r; int r;
for (c = 0; c < _COLUMN_MAX; c++) for (c = 0; c < _COLUMN_MAX; c++)
column_width[c] = strlen(titles[c]); column_width[c] = strlen(titles[c]);
udev = udev_new(); r = sd_device_enumerator_new(&e);
if (!udev) if (r < 0)
return log_oom(); return log_oom();
e = udev_enumerate_new(udev); r = sd_device_enumerator_add_match_subsystem(e, "block", true);
if (!e)
return log_oom();
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to add block match: %m"); return log_error_errno(r, "Failed to add block match: %m");
r = udev_enumerate_add_match_property(e, "ID_FS_USAGE", "filesystem"); r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to add property match: %m"); return log_error_errno(r, "Failed to add property match: %m");
r = udev_enumerate_scan_devices(e); r = device_enumerator_scan_devices(e);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to scan devices: %m"); return log_error_errno(r, "Failed to enumerate devices: %m");
first = udev_enumerate_get_list_entry(e); FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
udev_list_entry_foreach(item, first) {
_cleanup_(udev_device_unrefp) struct udev_device *d;
struct item *j; struct item *j;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d) {
r = log_oom();
goto finish;
}
if (!GREEDY_REALLOC0(items, n_allocated, n+1)) { if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
r = log_oom(); r = log_oom();
goto finish; goto finish;
@ -1469,11 +1441,11 @@ static int list_devices(void) {
switch (c) { switch (c) {
case COLUMN_NODE: case COLUMN_NODE:
x = udev_device_get_devnode(d); (void) sd_device_get_devname(d, &x);
break; break;
case COLUMN_PATH: case COLUMN_PATH:
x = udev_device_get_property_value(d, "ID_PATH"); (void) sd_device_get_property_value(d, "ID_PATH", &x);
break; break;
case COLUMN_MODEL: case COLUMN_MODEL:
@ -1481,11 +1453,11 @@ static int list_devices(void) {
break; break;
case COLUMN_WWN: case COLUMN_WWN:
x = udev_device_get_property_value(d, "ID_WWN"); (void) sd_device_get_property_value(d, "ID_WWN", &x);
break; break;
case COLUMN_FSTYPE: case COLUMN_FSTYPE:
x = udev_device_get_property_value(d, "ID_FS_TYPE"); (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
break; break;
case COLUMN_LABEL: case COLUMN_LABEL:
@ -1493,7 +1465,7 @@ static int list_devices(void) {
break; break;
case COLUMN_UUID: case COLUMN_UUID:
x = udev_device_get_property_value(d, "ID_FS_UUID"); (void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
break; break;
} }