chiark / gitweb /
Prep v231.2: Add mor debug messages to find out, why 'loginctl suspend' isn't working.
[elogind.git] / src / login / sysfs-show.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <string.h>
22
23 #if 0 /// elogind needs the systems udev header
24 #include "libudev.h"
25 #else
26 #include <libudev.h>
27 #endif // 0
28
29 #include "alloc-util.h"
30 #include "locale-util.h"
31 #include "path-util.h"
32 #include "string-util.h"
33 #include "sysfs-show.h"
34 #include "terminal-util.h"
35 #include "udev-util.h"
36 #include "util.h"
37
38 static int show_sysfs_one(
39                 struct udev *udev,
40                 const char *seat,
41                 struct udev_list_entry **item,
42                 const char *sub,
43                 const char *prefix,
44                 unsigned n_columns) {
45
46         assert(udev);
47         assert(seat);
48         assert(item);
49         assert(prefix);
50
51         while (*item) {
52                 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
53                 struct udev_list_entry *next, *lookahead;
54                 const char *sn, *name, *sysfs, *subsystem, *sysname;
55                 _cleanup_free_ char *k = NULL, *l = NULL;
56                 bool is_master;
57
58                 sysfs = udev_list_entry_get_name(*item);
59                 if (!path_startswith(sysfs, sub))
60                         return 0;
61
62                 d = udev_device_new_from_syspath(udev, sysfs);
63                 if (!d) {
64                         *item = udev_list_entry_get_next(*item);
65                         continue;
66                 }
67
68                 sn = udev_device_get_property_value(d, "ID_SEAT");
69                 if (isempty(sn))
70                         sn = "seat0";
71
72                 /* Explicitly also check for tag 'seat' here */
73                 if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
74                         *item = udev_list_entry_get_next(*item);
75                         continue;
76                 }
77
78                 is_master = udev_device_has_tag(d, "master-of-seat");
79
80                 name = udev_device_get_sysattr_value(d, "name");
81                 if (!name)
82                         name = udev_device_get_sysattr_value(d, "id");
83                 subsystem = udev_device_get_subsystem(d);
84                 sysname = udev_device_get_sysname(d);
85
86                 /* Look if there's more coming after this */
87                 lookahead = next = udev_list_entry_get_next(*item);
88                 while (lookahead) {
89                         const char *lookahead_sysfs;
90
91                         lookahead_sysfs = udev_list_entry_get_name(lookahead);
92
93                         if (path_startswith(lookahead_sysfs, sub) &&
94                             !path_startswith(lookahead_sysfs, sysfs)) {
95                                 _cleanup_udev_device_unref_ struct udev_device *lookahead_d = NULL;
96
97                                 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
98                                 if (lookahead_d) {
99                                         const char *lookahead_sn;
100
101                                         lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
102                                         if (isempty(lookahead_sn))
103                                                 lookahead_sn = "seat0";
104
105                                         if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
106                                                 break;
107                                 }
108                         }
109
110                         lookahead = udev_list_entry_get_next(lookahead);
111                 }
112
113                 k = ellipsize(sysfs, n_columns, 20);
114                 if (!k)
115                         return -ENOMEM;
116
117                 printf("%s%s%s\n", prefix, special_glyph(lookahead ? TREE_BRANCH : TREE_RIGHT), k);
118
119                 if (asprintf(&l,
120                              "%s%s:%s%s%s%s",
121                              is_master ? "[MASTER] " : "",
122                              subsystem, sysname,
123                              name ? " \"" : "", strempty(name), name ? "\"" : "") < 0)
124                         return -ENOMEM;
125
126                 free(k);
127                 k = ellipsize(l, n_columns, 70);
128                 if (!k)
129                         return -ENOMEM;
130
131                 printf("%s%s%s\n", prefix, lookahead ? special_glyph(TREE_VERTICAL) : "  ", k);
132
133                 *item = next;
134                 if (*item) {
135                         _cleanup_free_ char *p = NULL;
136
137                         p = strappend(prefix, lookahead ? special_glyph(TREE_VERTICAL) : "  ");
138                         if (!p)
139                                 return -ENOMEM;
140
141                         show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
142                 }
143         }
144
145         return 0;
146 }
147
148 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
149         _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
150         _cleanup_udev_unref_ struct udev *udev = NULL;
151         struct udev_list_entry *first = NULL;
152         int r;
153
154         if (n_columns <= 0)
155                 n_columns = columns();
156
157         if (!prefix)
158                 prefix = "";
159
160         if (isempty(seat))
161                 seat = "seat0";
162
163         udev = udev_new();
164         if (!udev)
165                 return -ENOMEM;
166
167         e = udev_enumerate_new(udev);
168         if (!e)
169                 return -ENOMEM;
170
171         if (!streq(seat, "seat0"))
172                 r = udev_enumerate_add_match_tag(e, seat);
173         else
174                 r = udev_enumerate_add_match_tag(e, "seat");
175         if (r < 0)
176                 return r;
177
178         r = udev_enumerate_add_match_is_initialized(e);
179         if (r < 0)
180                 return r;
181
182         r = udev_enumerate_scan_devices(e);
183         if (r < 0)
184                 return r;
185
186         first = udev_enumerate_get_list_entry(e);
187         if (first)
188                 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
189         else
190                 printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)");
191
192         return r;
193 }