chiark / gitweb /
shared: fix a misspelling of "journalctl"
[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 #include "libudev.h"
24
25 #include "alloc-util.h"
26 #include "locale-util.h"
27 #include "path-util.h"
28 #include "string-util.h"
29 #include "sysfs-show.h"
30 #include "terminal-util.h"
31 #include "udev-util.h"
32 #include "util.h"
33
34 static int show_sysfs_one(
35                 struct udev *udev,
36                 const char *seat,
37                 struct udev_list_entry **item,
38                 const char *sub,
39                 const char *prefix,
40                 unsigned n_columns) {
41
42         assert(udev);
43         assert(seat);
44         assert(item);
45         assert(prefix);
46
47         while (*item) {
48                 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
49                 struct udev_list_entry *next, *lookahead;
50                 const char *sn, *name, *sysfs, *subsystem, *sysname;
51                 _cleanup_free_ char *k = NULL, *l = NULL;
52                 bool is_master;
53
54                 sysfs = udev_list_entry_get_name(*item);
55                 if (!path_startswith(sysfs, sub))
56                         return 0;
57
58                 d = udev_device_new_from_syspath(udev, sysfs);
59                 if (!d) {
60                         *item = udev_list_entry_get_next(*item);
61                         continue;
62                 }
63
64                 sn = udev_device_get_property_value(d, "ID_SEAT");
65                 if (isempty(sn))
66                         sn = "seat0";
67
68                 /* Explicitly also check for tag 'seat' here */
69                 if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
70                         *item = udev_list_entry_get_next(*item);
71                         continue;
72                 }
73
74                 is_master = udev_device_has_tag(d, "master-of-seat");
75
76                 name = udev_device_get_sysattr_value(d, "name");
77                 if (!name)
78                         name = udev_device_get_sysattr_value(d, "id");
79                 subsystem = udev_device_get_subsystem(d);
80                 sysname = udev_device_get_sysname(d);
81
82                 /* Look if there's more coming after this */
83                 lookahead = next = udev_list_entry_get_next(*item);
84                 while (lookahead) {
85                         const char *lookahead_sysfs;
86
87                         lookahead_sysfs = udev_list_entry_get_name(lookahead);
88
89                         if (path_startswith(lookahead_sysfs, sub) &&
90                             !path_startswith(lookahead_sysfs, sysfs)) {
91                                 _cleanup_udev_device_unref_ struct udev_device *lookahead_d = NULL;
92
93                                 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
94                                 if (lookahead_d) {
95                                         const char *lookahead_sn;
96
97                                         lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
98                                         if (isempty(lookahead_sn))
99                                                 lookahead_sn = "seat0";
100
101                                         if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
102                                                 break;
103                                 }
104                         }
105
106                         lookahead = udev_list_entry_get_next(lookahead);
107                 }
108
109                 k = ellipsize(sysfs, n_columns, 20);
110                 if (!k)
111                         return -ENOMEM;
112
113                 printf("%s%s%s\n", prefix, draw_special_char(lookahead ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT), k);
114
115                 if (asprintf(&l,
116                              "%s%s:%s%s%s%s",
117                              is_master ? "[MASTER] " : "",
118                              subsystem, sysname,
119                              name ? " \"" : "", strempty(name), name ? "\"" : "") < 0)
120                         return -ENOMEM;
121
122                 free(k);
123                 k = ellipsize(l, n_columns, 70);
124                 if (!k)
125                         return -ENOMEM;
126
127                 printf("%s%s%s\n", prefix, lookahead ? draw_special_char(DRAW_TREE_VERTICAL) : "  ", k);
128
129                 *item = next;
130                 if (*item) {
131                         _cleanup_free_ char *p = NULL;
132
133                         p = strappend(prefix, lookahead ? draw_special_char(DRAW_TREE_VERTICAL) : "  ");
134                         if (!p)
135                                 return -ENOMEM;
136
137                         show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
138                 }
139         }
140
141         return 0;
142 }
143
144 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
145         _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
146         _cleanup_udev_unref_ struct udev *udev = NULL;
147         struct udev_list_entry *first = NULL;
148         int r;
149
150         if (n_columns <= 0)
151                 n_columns = columns();
152
153         if (!prefix)
154                 prefix = "";
155
156         if (isempty(seat))
157                 seat = "seat0";
158
159         udev = udev_new();
160         if (!udev)
161                 return -ENOMEM;
162
163         e = udev_enumerate_new(udev);
164         if (!e)
165                 return -ENOMEM;
166
167         if (!streq(seat, "seat0"))
168                 r = udev_enumerate_add_match_tag(e, seat);
169         else
170                 r = udev_enumerate_add_match_tag(e, "seat");
171         if (r < 0)
172                 return r;
173
174         r = udev_enumerate_add_match_is_initialized(e);
175         if (r < 0)
176                 return r;
177
178         r = udev_enumerate_scan_devices(e);
179         if (r < 0)
180                 return r;
181
182         first = udev_enumerate_get_list_entry(e);
183         if (first)
184                 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
185         else
186                 printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT), "(none)");
187
188         return r;
189 }