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