]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/sysfs-show.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / login / sysfs-show.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <string.h>
8
9 #include "libudev.h"
10
11 #include "alloc-util.h"
12 #include "locale-util.h"
13 #include "path-util.h"
14 #include "string-util.h"
15 #include "sysfs-show.h"
16 #include "terminal-util.h"
17 #include "udev-util.h"
18 #include "util.h"
19
20 static int show_sysfs_one(
21 struct udev *udev,
22 const char *seat,
23 struct udev_list_entry **item,
24 const char *sub,
25 const char *prefix,
26 unsigned n_columns,
27 OutputFlags flags) {
28
29 size_t max_width;
30
31 assert(udev);
32 assert(seat);
33 assert(item);
34 assert(prefix);
35
36 if (flags & OUTPUT_FULL_WIDTH)
37 max_width = (size_t) -1;
38 else if (n_columns < 10)
39 max_width = 10;
40 else
41 max_width = n_columns;
42
43 while (*item) {
44 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
45 struct udev_list_entry *next, *lookahead;
46 const char *sn, *name, *sysfs, *subsystem, *sysname;
47 _cleanup_free_ char *k = NULL, *l = NULL;
48 bool is_master;
49
50 sysfs = udev_list_entry_get_name(*item);
51 if (!path_startswith(sysfs, sub))
52 return 0;
53
54 d = udev_device_new_from_syspath(udev, sysfs);
55 if (!d) {
56 *item = udev_list_entry_get_next(*item);
57 continue;
58 }
59
60 sn = udev_device_get_property_value(d, "ID_SEAT");
61 if (isempty(sn))
62 sn = "seat0";
63
64 /* Explicitly also check for tag 'seat' here */
65 if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
66 *item = udev_list_entry_get_next(*item);
67 continue;
68 }
69
70 is_master = udev_device_has_tag(d, "master-of-seat");
71
72 name = udev_device_get_sysattr_value(d, "name");
73 if (!name)
74 name = udev_device_get_sysattr_value(d, "id");
75 subsystem = udev_device_get_subsystem(d);
76 sysname = udev_device_get_sysname(d);
77
78 /* Look if there's more coming after this */
79 lookahead = next = udev_list_entry_get_next(*item);
80 while (lookahead) {
81 const char *lookahead_sysfs;
82
83 lookahead_sysfs = udev_list_entry_get_name(lookahead);
84
85 if (path_startswith(lookahead_sysfs, sub) &&
86 !path_startswith(lookahead_sysfs, sysfs)) {
87 _cleanup_(udev_device_unrefp) struct udev_device *lookahead_d = NULL;
88
89 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
90 if (lookahead_d) {
91 const char *lookahead_sn;
92
93 lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
94 if (isempty(lookahead_sn))
95 lookahead_sn = "seat0";
96
97 if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
98 break;
99 }
100 }
101
102 lookahead = udev_list_entry_get_next(lookahead);
103 }
104
105 k = ellipsize(sysfs, max_width, 20);
106 if (!k)
107 return -ENOMEM;
108
109 printf("%s%s%s\n", prefix, special_glyph(lookahead ? TREE_BRANCH : TREE_RIGHT), k);
110
111 if (asprintf(&l,
112 "%s%s:%s%s%s%s",
113 is_master ? "[MASTER] " : "",
114 subsystem, sysname,
115 name ? " \"" : "", strempty(name), name ? "\"" : "") < 0)
116 return -ENOMEM;
117
118 free(k);
119 k = ellipsize(l, max_width, 70);
120 if (!k)
121 return -ENOMEM;
122
123 printf("%s%s%s\n", prefix, lookahead ? special_glyph(TREE_VERTICAL) : " ", k);
124
125 *item = next;
126 if (*item) {
127 _cleanup_free_ char *p = NULL;
128
129 p = strappend(prefix, lookahead ? special_glyph(TREE_VERTICAL) : " ");
130 if (!p)
131 return -ENOMEM;
132
133 show_sysfs_one(udev, seat, item, sysfs, p,
134 n_columns == (unsigned) -1 || n_columns < 2 ? n_columns : n_columns - 2,
135 flags);
136 }
137 }
138
139 return 0;
140 }
141
142 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) {
143 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
144 _cleanup_(udev_unrefp) struct udev *udev = NULL;
145 struct udev_list_entry *first = NULL;
146 int r;
147
148 if (n_columns <= 0)
149 n_columns = columns();
150
151 prefix = strempty(prefix);
152
153 if (isempty(seat))
154 seat = "seat0";
155
156 udev = udev_new();
157 if (!udev)
158 return -ENOMEM;
159
160 e = udev_enumerate_new(udev);
161 if (!e)
162 return -ENOMEM;
163
164 if (!streq(seat, "seat0"))
165 r = udev_enumerate_add_match_tag(e, seat);
166 else
167 r = udev_enumerate_add_match_tag(e, "seat");
168 if (r < 0)
169 return r;
170
171 r = udev_enumerate_add_match_is_initialized(e);
172 if (r < 0)
173 return r;
174
175 r = udev_enumerate_scan_devices(e);
176 if (r < 0)
177 return r;
178
179 first = udev_enumerate_get_list_entry(e);
180 if (first)
181 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns, flags);
182 else
183 printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)");
184
185 return r;
186 }