1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "locale-util.h"
28 #include "path-util.h"
29 #include "string-util.h"
30 #include "sysfs-show.h"
31 #include "terminal-util.h"
32 #include "udev-util.h"
35 static int show_sysfs_one(
38 struct udev_list_entry
**item
,
51 if (flags
& OUTPUT_FULL_WIDTH
)
52 max_width
= (size_t) -1;
53 else if (n_columns
< 10)
56 max_width
= n_columns
;
59 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
60 struct udev_list_entry
*next
, *lookahead
;
61 const char *sn
, *name
, *sysfs
, *subsystem
, *sysname
;
62 _cleanup_free_
char *k
= NULL
, *l
= NULL
;
65 sysfs
= udev_list_entry_get_name(*item
);
66 if (!path_startswith(sysfs
, sub
))
69 d
= udev_device_new_from_syspath(udev
, sysfs
);
71 *item
= udev_list_entry_get_next(*item
);
75 sn
= udev_device_get_property_value(d
, "ID_SEAT");
79 /* Explicitly also check for tag 'seat' here */
80 if (!streq(seat
, sn
) || !udev_device_has_tag(d
, "seat")) {
81 *item
= udev_list_entry_get_next(*item
);
85 is_master
= udev_device_has_tag(d
, "master-of-seat");
87 name
= udev_device_get_sysattr_value(d
, "name");
89 name
= udev_device_get_sysattr_value(d
, "id");
90 subsystem
= udev_device_get_subsystem(d
);
91 sysname
= udev_device_get_sysname(d
);
93 /* Look if there's more coming after this */
94 lookahead
= next
= udev_list_entry_get_next(*item
);
96 const char *lookahead_sysfs
;
98 lookahead_sysfs
= udev_list_entry_get_name(lookahead
);
100 if (path_startswith(lookahead_sysfs
, sub
) &&
101 !path_startswith(lookahead_sysfs
, sysfs
)) {
102 _cleanup_udev_device_unref_
struct udev_device
*lookahead_d
= NULL
;
104 lookahead_d
= udev_device_new_from_syspath(udev
, lookahead_sysfs
);
106 const char *lookahead_sn
;
108 lookahead_sn
= udev_device_get_property_value(d
, "ID_SEAT");
109 if (isempty(lookahead_sn
))
110 lookahead_sn
= "seat0";
112 if (streq(seat
, lookahead_sn
) && udev_device_has_tag(lookahead_d
, "seat"))
117 lookahead
= udev_list_entry_get_next(lookahead
);
120 k
= ellipsize(sysfs
, max_width
, 20);
124 printf("%s%s%s\n", prefix
, special_glyph(lookahead
? TREE_BRANCH
: TREE_RIGHT
), k
);
128 is_master
? "[MASTER] " : "",
130 name
? " \"" : "", strempty(name
), name
? "\"" : "") < 0)
134 k
= ellipsize(l
, max_width
, 70);
138 printf("%s%s%s\n", prefix
, lookahead
? special_glyph(TREE_VERTICAL
) : " ", k
);
142 _cleanup_free_
char *p
= NULL
;
144 p
= strappend(prefix
, lookahead
? special_glyph(TREE_VERTICAL
) : " ");
148 show_sysfs_one(udev
, seat
, item
, sysfs
, p
,
149 n_columns
== (unsigned) -1 || n_columns
< 2 ? n_columns
: n_columns
- 2,
157 int show_sysfs(const char *seat
, const char *prefix
, unsigned n_columns
, OutputFlags flags
) {
158 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
159 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
160 struct udev_list_entry
*first
= NULL
;
164 n_columns
= columns();
166 prefix
= strempty(prefix
);
175 e
= udev_enumerate_new(udev
);
179 if (!streq(seat
, "seat0"))
180 r
= udev_enumerate_add_match_tag(e
, seat
);
182 r
= udev_enumerate_add_match_tag(e
, "seat");
186 r
= udev_enumerate_add_match_is_initialized(e
);
190 r
= udev_enumerate_scan_devices(e
);
194 first
= udev_enumerate_get_list_entry(e
);
196 show_sysfs_one(udev
, seat
, &first
, "/", prefix
, n_columns
, flags
);
198 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), "(none)");