| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-device.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "device-enumerator-private.h" |
| 7 |
#include "device-private.h" |
| 8 |
#include "device-util.h" |
| 9 |
#include "glyph-util.h" |
| 10 |
#include "path-util.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "sysfs-show.h" |
| 13 |
#include "terminal-util.h" |
| 14 |
|
| 15 |
static int show_sysfs_one( |
| 16 |
const char *seat, |
| 17 |
sd_device **dev_list, |
| 18 |
size_t *i_dev, |
| 19 |
size_t n_dev, |
| 20 |
const char *sub, |
| 21 |
const char *prefix, |
| 22 |
unsigned n_columns, |
| 23 |
OutputFlags flags) { |
| 24 |
|
| 25 |
size_t max_width; |
| 26 |
int r; |
| 27 |
|
| 28 |
assert(seat); |
| 29 |
assert(dev_list); |
| 30 |
assert(i_dev); |
| 31 |
assert(prefix); |
| 32 |
|
| 33 |
if (flags & OUTPUT_FULL_WIDTH) |
| 34 |
max_width = SIZE_MAX; |
| 35 |
else if (n_columns < 10) |
| 36 |
max_width = 10; |
| 37 |
else |
| 38 |
max_width = n_columns; |
| 39 |
|
| 40 |
while (*i_dev < n_dev) { |
| 41 |
const char *sysfs, *sn, *name = NULL, *subsystem, *sysname; |
| 42 |
_cleanup_free_ char *k = NULL, *l = NULL; |
| 43 |
size_t lookahead; |
| 44 |
bool is_master; |
| 45 |
|
| 46 |
if (sd_device_get_syspath(dev_list[*i_dev], &sysfs) < 0 || |
| 47 |
!path_startswith(sysfs, sub)) |
| 48 |
return 0; |
| 49 |
|
| 50 |
r = device_get_seat(dev_list[*i_dev], &sn); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
/* Explicitly also check for tag 'seat' here */ |
| 55 |
if (!streq(seat, sn) || |
| 56 |
sd_device_has_current_tag(dev_list[*i_dev], "seat") <= 0 || |
| 57 |
sd_device_get_subsystem(dev_list[*i_dev], &subsystem) < 0 || |
| 58 |
sd_device_get_sysname(dev_list[*i_dev], &sysname) < 0) { |
| 59 |
(*i_dev)++; |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
is_master = sd_device_has_current_tag(dev_list[*i_dev], "master-of-seat") > 0; |
| 64 |
|
| 65 |
if (device_get_sysattr_safe_string(dev_list[*i_dev], "name", &name) < 0) |
| 66 |
(void) device_get_sysattr_safe_string(dev_list[*i_dev], "id", &name); |
| 67 |
|
| 68 |
/* Look if there's more coming after this */ |
| 69 |
for (lookahead = *i_dev + 1; lookahead < n_dev; lookahead++) { |
| 70 |
const char *lookahead_sysfs; |
| 71 |
|
| 72 |
if (sd_device_get_syspath(dev_list[lookahead], &lookahead_sysfs) < 0) |
| 73 |
continue; |
| 74 |
|
| 75 |
if (path_startswith(lookahead_sysfs, sub) && |
| 76 |
!path_startswith(lookahead_sysfs, sysfs)) { |
| 77 |
const char *lookahead_sn; |
| 78 |
|
| 79 |
r = device_get_seat(dev_list[lookahead], &lookahead_sn); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
|
| 83 |
if (streq(seat, lookahead_sn) && sd_device_has_current_tag(dev_list[lookahead], "seat") > 0) |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
k = ellipsize(sysfs, max_width, 20); |
| 89 |
if (!k) |
| 90 |
return -ENOMEM; |
| 91 |
|
| 92 |
printf("%s%s%s\n", prefix, glyph(lookahead < n_dev ? GLYPH_TREE_BRANCH : GLYPH_TREE_RIGHT), k); |
| 93 |
|
| 94 |
if (asprintf(&l, |
| 95 |
"%s%s:%s%s%s%s", |
| 96 |
is_master ? "[MASTER] " : "", |
| 97 |
subsystem, sysname, |
| 98 |
name ? " \"" : "", strempty(name), name ? "\"" : "") < 0) |
| 99 |
return -ENOMEM; |
| 100 |
|
| 101 |
free(k); |
| 102 |
k = ellipsize(l, max_width, 70); |
| 103 |
if (!k) |
| 104 |
return -ENOMEM; |
| 105 |
|
| 106 |
printf("%s%s%s\n", prefix, lookahead < n_dev ? glyph(GLYPH_TREE_VERTICAL) : " ", k); |
| 107 |
|
| 108 |
if (++(*i_dev) < n_dev) { |
| 109 |
_cleanup_free_ char *p = NULL; |
| 110 |
|
| 111 |
p = strjoin(prefix, lookahead < n_dev ? glyph(GLYPH_TREE_VERTICAL) : " "); |
| 112 |
if (!p) |
| 113 |
return -ENOMEM; |
| 114 |
|
| 115 |
r = show_sysfs_one(seat, dev_list, i_dev, n_dev, sysfs, p, |
| 116 |
n_columns == UINT_MAX || n_columns < 2 ? n_columns : n_columns - 2, |
| 117 |
flags); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) { |
| 128 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 129 |
size_t n_dev = 0, i = 0; |
| 130 |
sd_device **dev_list; |
| 131 |
int r; |
| 132 |
|
| 133 |
if (n_columns <= 0) |
| 134 |
n_columns = columns(); |
| 135 |
|
| 136 |
prefix = strempty(prefix); |
| 137 |
|
| 138 |
if (isempty(seat)) |
| 139 |
seat = "seat0"; |
| 140 |
|
| 141 |
r = sd_device_enumerator_new(&e); |
| 142 |
if (r < 0) |
| 143 |
return r; |
| 144 |
|
| 145 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 146 |
if (r < 0) |
| 147 |
return r; |
| 148 |
|
| 149 |
r = sd_device_enumerator_add_match_tag(e, streq(seat, "seat0") ? "seat" : seat); |
| 150 |
if (r < 0) |
| 151 |
return r; |
| 152 |
|
| 153 |
r = device_enumerator_scan_devices(e); |
| 154 |
if (r < 0) |
| 155 |
return r; |
| 156 |
|
| 157 |
dev_list = device_enumerator_get_devices(e, &n_dev); |
| 158 |
|
| 159 |
if (dev_list && n_dev > 0) |
| 160 |
show_sysfs_one(seat, dev_list, &i, n_dev, "/", prefix, n_columns, flags); |
| 161 |
else |
| 162 |
printf("%s%s%s\n", prefix, glyph(GLYPH_TREE_RIGHT), "(none)"); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |