login/sysfs-show: replace udev_device by sd_device

This commit is contained in:
Yu Watanabe 2018-08-22 14:59:03 +09:00
parent 4f209af7da
commit e156d24b0a

View file

@ -3,31 +3,33 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "libudev.h" #include "sd-device.h"
#include "alloc-util.h" #include "alloc-util.h"
#include "device-enumerator-private.h"
#include "locale-util.h" #include "locale-util.h"
#include "path-util.h" #include "path-util.h"
#include "string-util.h" #include "string-util.h"
#include "sysfs-show.h" #include "sysfs-show.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "udev-util.h"
#include "util.h" #include "util.h"
static int show_sysfs_one( static int show_sysfs_one(
struct udev *udev,
const char *seat, const char *seat,
struct udev_list_entry **item, sd_device **dev_list,
size_t *i_dev,
size_t n_dev,
const char *sub, const char *sub,
const char *prefix, const char *prefix,
unsigned n_columns, unsigned n_columns,
OutputFlags flags) { OutputFlags flags) {
size_t max_width; size_t max_width;
int r;
assert(udev);
assert(seat); assert(seat);
assert(item); assert(dev_list);
assert(i_dev);
assert(prefix); assert(prefix);
if (flags & OUTPUT_FULL_WIDTH) if (flags & OUTPUT_FULL_WIDTH)
@ -37,73 +39,58 @@ static int show_sysfs_one(
else else
max_width = n_columns; max_width = n_columns;
while (*item) { while (*i_dev < n_dev) {
_cleanup_(udev_device_unrefp) struct udev_device *d = NULL; const char *sysfs, *sn, *name = NULL, *subsystem = NULL, *sysname = NULL;
struct udev_list_entry *next, *lookahead;
const char *sn, *name, *sysfs, *subsystem, *sysname;
_cleanup_free_ char *k = NULL, *l = NULL; _cleanup_free_ char *k = NULL, *l = NULL;
size_t lookahead;
bool is_master; bool is_master;
sysfs = udev_list_entry_get_name(*item); if (sd_device_get_syspath(dev_list[*i_dev], &sysfs) < 0 ||
if (!path_startswith(sysfs, sub)) !path_startswith(sysfs, sub))
return 0; return 0;
d = udev_device_new_from_syspath(udev, sysfs); if (sd_device_get_property_value(dev_list[*i_dev], "ID_SEAT", &sn) < 0 || isempty(sn))
if (!d) {
*item = udev_list_entry_get_next(*item);
continue;
}
sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(sn))
sn = "seat0"; sn = "seat0";
/* Explicitly also check for tag 'seat' here */ /* Explicitly also check for tag 'seat' here */
if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) { if (!streq(seat, sn) || sd_device_has_tag(dev_list[*i_dev], "seat") <= 0) {
*item = udev_list_entry_get_next(*item); (*i_dev)++;
continue; continue;
} }
is_master = udev_device_has_tag(d, "master-of-seat"); is_master = sd_device_has_tag(dev_list[*i_dev], "master-of-seat") > 0;
name = udev_device_get_sysattr_value(d, "name"); if (sd_device_get_sysattr_value(dev_list[*i_dev], "name", &name) < 0)
if (!name) (void) sd_device_get_sysattr_value(dev_list[*i_dev], "id", &name);
name = udev_device_get_sysattr_value(d, "id");
subsystem = udev_device_get_subsystem(d); (void) sd_device_get_subsystem(dev_list[*i_dev], &subsystem);
sysname = udev_device_get_sysname(d); (void) sd_device_get_sysname(dev_list[*i_dev], &sysname);
/* Look if there's more coming after this */ /* Look if there's more coming after this */
lookahead = next = udev_list_entry_get_next(*item); for (lookahead = *i_dev + 1; lookahead < n_dev; lookahead++) {
while (lookahead) {
const char *lookahead_sysfs; const char *lookahead_sysfs;
lookahead_sysfs = udev_list_entry_get_name(lookahead); if (sd_device_get_syspath(dev_list[lookahead], &lookahead_sysfs) < 0)
continue;
if (path_startswith(lookahead_sysfs, sub) && if (path_startswith(lookahead_sysfs, sub) &&
!path_startswith(lookahead_sysfs, sysfs)) { !path_startswith(lookahead_sysfs, sysfs)) {
_cleanup_(udev_device_unrefp) struct udev_device *lookahead_d = NULL; const char *lookahead_sn;
lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs); if (sd_device_get_property_value(dev_list[lookahead], "ID_SEAT", &lookahead_sn) < 0 ||
if (lookahead_d) { isempty(lookahead_sn))
const char *lookahead_sn; lookahead_sn = "seat0";
lookahead_sn = udev_device_get_property_value(d, "ID_SEAT"); if (streq(seat, lookahead_sn) && sd_device_has_tag(dev_list[lookahead], "seat") > 0)
if (isempty(lookahead_sn)) break;
lookahead_sn = "seat0";
if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
break;
}
} }
lookahead = udev_list_entry_get_next(lookahead);
} }
k = ellipsize(sysfs, max_width, 20); k = ellipsize(sysfs, max_width, 20);
if (!k) if (!k)
return -ENOMEM; return -ENOMEM;
printf("%s%s%s\n", prefix, special_glyph(lookahead ? TREE_BRANCH : TREE_RIGHT), k); printf("%s%s%s\n", prefix, special_glyph(lookahead < n_dev ? TREE_BRANCH : TREE_RIGHT), k);
if (asprintf(&l, if (asprintf(&l,
"%s%s:%s%s%s%s", "%s%s:%s%s%s%s",
@ -117,29 +104,31 @@ static int show_sysfs_one(
if (!k) if (!k)
return -ENOMEM; return -ENOMEM;
printf("%s%s%s\n", prefix, lookahead ? special_glyph(TREE_VERTICAL) : " ", k); printf("%s%s%s\n", prefix, lookahead < n_dev ? special_glyph(TREE_VERTICAL) : " ", k);
*item = next; if (++(*i_dev) < n_dev) {
if (*item) {
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
p = strappend(prefix, lookahead ? special_glyph(TREE_VERTICAL) : " "); p = strappend(prefix, lookahead < n_dev ? special_glyph(TREE_VERTICAL) : " ");
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
show_sysfs_one(udev, seat, item, sysfs, p, r = show_sysfs_one(seat, dev_list, i_dev, n_dev, sysfs, p,
n_columns == (unsigned) -1 || n_columns < 2 ? n_columns : n_columns - 2, n_columns == (unsigned) -1 || n_columns < 2 ? n_columns : n_columns - 2,
flags); flags);
if (r < 0)
return r;
} }
} }
return 0; return 0;
} }
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) { int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) {
_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; size_t n_dev = 0, n_allocated = 0, i = 0;
struct udev_list_entry *first = NULL; sd_device *d, **dev_list = NULL;
int r; int r;
if (n_columns <= 0) if (n_columns <= 0)
@ -150,34 +139,43 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputF
if (isempty(seat)) if (isempty(seat))
seat = "seat0"; seat = "seat0";
udev = udev_new(); r = sd_device_enumerator_new(&e);
if (!udev)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
if (!streq(seat, "seat0"))
r = udev_enumerate_add_match_tag(e, seat);
else
r = udev_enumerate_add_match_tag(e, "seat");
if (r < 0) if (r < 0)
return r; return r;
r = udev_enumerate_add_match_is_initialized(e); r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0) if (r < 0)
return r; return r;
r = udev_enumerate_scan_devices(e); r = sd_device_enumerator_add_match_tag(e, streq(seat, "seat0") ? "seat" : seat);
if (r < 0) if (r < 0)
return r; return r;
first = udev_enumerate_get_list_entry(e); r = device_enumerator_scan_devices(e);
if (first) if (r < 0)
show_sysfs_one(udev, seat, &first, "/", prefix, n_columns, flags); return r;
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) {
const char *syspath;
if (sd_device_get_syspath(d, &syspath) < 0)
continue;
if (!GREEDY_REALLOC(dev_list, n_allocated, n_dev + 2))
return -ENOMEM;
dev_list[n_dev++] = sd_device_ref(d);
dev_list[n_dev] = NULL;
}
if (n_dev > 0)
show_sysfs_one(seat, dev_list, &i, n_dev, "/", prefix, n_columns, flags);
else else
printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)"); printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)");
return r; for (i = 0; i < n_dev; i++)
sd_device_unref(dev_list[i]);
free(dev_list);
return 0;
} }