]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/sysfs-show.c
Merge pull request #33237 from bluca/dlopen_deps
[thirdparty/systemd.git] / src / login / sysfs-show.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4
5 #include "sd-device.h"
6
7 #include "alloc-util.h"
8 #include "device-enumerator-private.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 if (sd_device_get_property_value(dev_list[*i_dev], "ID_SEAT", &sn) < 0 || isempty(sn))
51 sn = "seat0";
52
53 /* Explicitly also check for tag 'seat' here */
54 if (!streq(seat, sn) ||
55 sd_device_has_current_tag(dev_list[*i_dev], "seat") <= 0 ||
56 sd_device_get_subsystem(dev_list[*i_dev], &subsystem) < 0 ||
57 sd_device_get_sysname(dev_list[*i_dev], &sysname) < 0) {
58 (*i_dev)++;
59 continue;
60 }
61
62 is_master = sd_device_has_current_tag(dev_list[*i_dev], "master-of-seat") > 0;
63
64 if (sd_device_get_sysattr_value(dev_list[*i_dev], "name", &name) < 0)
65 (void) sd_device_get_sysattr_value(dev_list[*i_dev], "id", &name);
66
67 /* Look if there's more coming after this */
68 for (lookahead = *i_dev + 1; lookahead < n_dev; lookahead++) {
69 const char *lookahead_sysfs;
70
71 if (sd_device_get_syspath(dev_list[lookahead], &lookahead_sysfs) < 0)
72 continue;
73
74 if (path_startswith(lookahead_sysfs, sub) &&
75 !path_startswith(lookahead_sysfs, sysfs)) {
76 const char *lookahead_sn;
77
78 if (sd_device_get_property_value(dev_list[lookahead], "ID_SEAT", &lookahead_sn) < 0 ||
79 isempty(lookahead_sn))
80 lookahead_sn = "seat0";
81
82 if (streq(seat, lookahead_sn) && sd_device_has_current_tag(dev_list[lookahead], "seat") > 0)
83 break;
84 }
85 }
86
87 k = ellipsize(sysfs, max_width, 20);
88 if (!k)
89 return -ENOMEM;
90
91 printf("%s%s%s\n", prefix, special_glyph(lookahead < n_dev ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT), k);
92
93 if (asprintf(&l,
94 "%s%s:%s%s%s%s",
95 is_master ? "[MASTER] " : "",
96 subsystem, sysname,
97 name ? " \"" : "", strempty(name), name ? "\"" : "") < 0)
98 return -ENOMEM;
99
100 free(k);
101 k = ellipsize(l, max_width, 70);
102 if (!k)
103 return -ENOMEM;
104
105 printf("%s%s%s\n", prefix, lookahead < n_dev ? special_glyph(SPECIAL_GLYPH_TREE_VERTICAL) : " ", k);
106
107 if (++(*i_dev) < n_dev) {
108 _cleanup_free_ char *p = NULL;
109
110 p = strjoin(prefix, lookahead < n_dev ? special_glyph(SPECIAL_GLYPH_TREE_VERTICAL) : " ");
111 if (!p)
112 return -ENOMEM;
113
114 r = show_sysfs_one(seat, dev_list, i_dev, n_dev, sysfs, p,
115 n_columns == UINT_MAX || n_columns < 2 ? n_columns : n_columns - 2,
116 flags);
117 if (r < 0)
118 return r;
119 }
120
121 }
122
123 return 0;
124 }
125
126 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) {
127 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
128 size_t n_dev = 0, i = 0;
129 sd_device **dev_list;
130 int r;
131
132 if (n_columns <= 0)
133 n_columns = columns();
134
135 prefix = strempty(prefix);
136
137 if (isempty(seat))
138 seat = "seat0";
139
140 r = sd_device_enumerator_new(&e);
141 if (r < 0)
142 return r;
143
144 r = sd_device_enumerator_allow_uninitialized(e);
145 if (r < 0)
146 return r;
147
148 r = sd_device_enumerator_add_match_tag(e, streq(seat, "seat0") ? "seat" : seat);
149 if (r < 0)
150 return r;
151
152 r = device_enumerator_scan_devices(e);
153 if (r < 0)
154 return r;
155
156 dev_list = device_enumerator_get_devices(e, &n_dev);
157
158 if (dev_list && n_dev > 0)
159 show_sysfs_one(seat, dev_list, &i, n_dev, "/", prefix, n_columns, flags);
160 else
161 printf("%s%s%s\n", prefix, special_glyph(SPECIAL_GLYPH_TREE_RIGHT), "(none)");
162
163 return 0;
164 }