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