]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/sysfs-show.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
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"
20 static int show_sysfs_one(
23 struct udev_list_entry
**item
,
36 if (flags
& OUTPUT_FULL_WIDTH
)
37 max_width
= (size_t) -1;
38 else if (n_columns
< 10)
41 max_width
= n_columns
;
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
;
50 sysfs
= udev_list_entry_get_name(*item
);
51 if (!path_startswith(sysfs
, sub
))
54 d
= udev_device_new_from_syspath(udev
, sysfs
);
56 *item
= udev_list_entry_get_next(*item
);
60 sn
= udev_device_get_property_value(d
, "ID_SEAT");
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
);
70 is_master
= udev_device_has_tag(d
, "master-of-seat");
72 name
= udev_device_get_sysattr_value(d
, "name");
74 name
= udev_device_get_sysattr_value(d
, "id");
75 subsystem
= udev_device_get_subsystem(d
);
76 sysname
= udev_device_get_sysname(d
);
78 /* Look if there's more coming after this */
79 lookahead
= next
= udev_list_entry_get_next(*item
);
81 const char *lookahead_sysfs
;
83 lookahead_sysfs
= udev_list_entry_get_name(lookahead
);
85 if (path_startswith(lookahead_sysfs
, sub
) &&
86 !path_startswith(lookahead_sysfs
, sysfs
)) {
87 _cleanup_(udev_device_unrefp
) struct udev_device
*lookahead_d
= NULL
;
89 lookahead_d
= udev_device_new_from_syspath(udev
, lookahead_sysfs
);
91 const char *lookahead_sn
;
93 lookahead_sn
= udev_device_get_property_value(d
, "ID_SEAT");
94 if (isempty(lookahead_sn
))
95 lookahead_sn
= "seat0";
97 if (streq(seat
, lookahead_sn
) && udev_device_has_tag(lookahead_d
, "seat"))
102 lookahead
= udev_list_entry_get_next(lookahead
);
105 k
= ellipsize(sysfs
, max_width
, 20);
109 printf("%s%s%s\n", prefix
, special_glyph(lookahead
? TREE_BRANCH
: TREE_RIGHT
), k
);
113 is_master
? "[MASTER] " : "",
115 name
? " \"" : "", strempty(name
), name
? "\"" : "") < 0)
119 k
= ellipsize(l
, max_width
, 70);
123 printf("%s%s%s\n", prefix
, lookahead
? special_glyph(TREE_VERTICAL
) : " ", k
);
127 _cleanup_free_
char *p
= NULL
;
129 p
= strappend(prefix
, lookahead
? special_glyph(TREE_VERTICAL
) : " ");
133 show_sysfs_one(udev
, seat
, item
, sysfs
, p
,
134 n_columns
== (unsigned) -1 || n_columns
< 2 ? n_columns
: n_columns
- 2,
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
;
149 n_columns
= columns();
151 prefix
= strempty(prefix
);
160 e
= udev_enumerate_new(udev
);
164 if (!streq(seat
, "seat0"))
165 r
= udev_enumerate_add_match_tag(e
, seat
);
167 r
= udev_enumerate_add_match_tag(e
, "seat");
171 r
= udev_enumerate_add_match_is_initialized(e
);
175 r
= udev_enumerate_scan_devices(e
);
179 first
= udev_enumerate_get_list_entry(e
);
181 show_sysfs_one(udev
, seat
, &first
, "/", prefix
, n_columns
, flags
);
183 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), "(none)");