chiark / gitweb /
loginctl: show seat master and uaccess devices in seat-status output
[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
30 static int show_sysfs_one(
31                 struct udev *udev,
32                 const char *seat,
33                 struct udev_list_entry **item,
34                 const char *sub,
35                 const char *prefix,
36                 unsigned n_columns) {
37
38         assert(udev);
39         assert(seat);
40         assert(item);
41         assert(prefix);
42
43         while (*item) {
44                 struct udev_list_entry *next, *lookahead;
45                 struct udev_device *d;
46                 const char *sn, *name, *sysfs, *subsystem, *sysname;
47                 char *l, *k;
48                 bool is_master, uaccess;
49
50                 sysfs = udev_list_entry_get_name(*item);
51                 if (!path_startswith(sysfs, sub))
52                         return 0;
53
54                 d = udev_device_new_from_syspath(udev, sysfs);
55                 if (!d) {
56                         *item = udev_list_entry_get_next(*item);
57                         continue;
58                 }
59
60                 sn = udev_device_get_property_value(d, "ID_SEAT");
61                 if (isempty(sn))
62                         sn = "seat0";
63
64                 /* Explicitly also check for tag 'seat' here */
65                 if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
66                         udev_device_unref(d);
67                         *item = udev_list_entry_get_next(*item);
68                         continue;
69                 }
70
71                 is_master = udev_device_has_tag(d, "seat-master");
72                 uaccess = udev_device_has_tag(d, "uaccess");
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                                 struct udev_device *lookahead_d;
90
91                                 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
92                                 if (lookahead_d) {
93                                         const char *lookahead_sn;
94                                         bool found;
95
96                                         lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
97                                         if (isempty(lookahead_sn))
98                                                 lookahead_sn = "seat0";
99
100                                         found = streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat");
101                                         udev_device_unref(lookahead_d);
102
103                                         if (found)
104                                                 break;
105                                 }
106                         }
107
108                         lookahead = udev_list_entry_get_next(lookahead);
109                 }
110
111                 k = ellipsize(sysfs, n_columns, 20);
112                 printf("%s%s%s\n", prefix, draw_special_char(lookahead ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT),
113                                    k ? k : sysfs);
114                 free(k);
115
116                 if (asprintf(&l,
117                              "%s%s%s:%s%s%s%s",
118                              is_master ? "[MASTER] " : "", uaccess ? "[ACL] " : "",
119                              subsystem, sysname,
120                              name ? " \"" : "", name ? name : "", name ? "\"" : "") < 0) {
121                         udev_device_unref(d);
122                         return -ENOMEM;
123                 }
124
125                 k = ellipsize(l, n_columns, 70);
126                 printf("%s%s%s\n", prefix, lookahead ? draw_special_char(DRAW_TREE_VERT) : "  ",
127                                    k ? k : l);
128                 free(k);
129                 free(l);
130
131                 *item = next;
132                 if (*item) {
133                         char *p;
134
135                         p = strappend(prefix, lookahead ? draw_special_char(DRAW_TREE_VERT) : "  ");
136                         show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2);
137                         free(p);
138                 }
139
140                 udev_device_unref(d);
141         }
142
143         return 0;
144 }
145
146 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
147         struct udev *udev;
148         struct udev_list_entry *first = NULL;
149         struct udev_enumerate *e;
150         int r;
151
152         if (n_columns <= 0)
153                 n_columns = columns();
154
155         if (!prefix)
156                 prefix = "";
157
158         if (isempty(seat))
159                 seat = "seat0";
160
161         udev = udev_new();
162         if (!udev)
163                 return -ENOMEM;
164
165         e = udev_enumerate_new(udev);
166         if (!e) {
167                 r = -ENOMEM;
168                 goto finish;
169         }
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
176         if (r < 0)
177                 goto finish;
178
179         r = udev_enumerate_scan_devices(e);
180         if (r < 0)
181                 goto finish;
182
183         first = udev_enumerate_get_list_entry(e);
184         if (first)
185                 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
186
187 finish:
188         if (e)
189                 udev_enumerate_unref(e);
190
191         if (udev)
192                 udev_unref(udev);
193
194         return r;
195 }