]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/sysfs-show.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.git] / src / login / sysfs-show.c
CommitLineData
24310c11
LP
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
5430f7f2
LP
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
24310c11
LP
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
5430f7f2 16 Lesser General Public License for more details.
24310c11 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
24310c11
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <string.h>
24310c11 24
07630cea
LP
25#include "libudev.h"
26
b5efdb8a 27#include "alloc-util.h"
8752c575 28#include "locale-util.h"
9eb977db 29#include "path-util.h"
07630cea
LP
30#include "string-util.h"
31#include "sysfs-show.h"
288a74cc 32#include "terminal-util.h"
07630cea
LP
33#include "udev-util.h"
34#include "util.h"
24310c11 35
24310c11
LP
36static int show_sysfs_one(
37 struct udev *udev,
38 const char *seat,
39 struct udev_list_entry **item,
40 const char *sub,
41 const char *prefix,
42 unsigned n_columns) {
43
44 assert(udev);
45 assert(seat);
46 assert(item);
47 assert(prefix);
48
49 while (*item) {
06acf2d4 50 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
24310c11 51 struct udev_list_entry *next, *lookahead;
88e3dc90 52 const char *sn, *name, *sysfs, *subsystem, *sysname;
06acf2d4 53 _cleanup_free_ char *k = NULL, *l = NULL;
5d1fb81b 54 bool is_master;
24310c11
LP
55
56 sysfs = udev_list_entry_get_name(*item);
57 if (!path_startswith(sysfs, sub))
58 return 0;
59
60 d = udev_device_new_from_syspath(udev, sysfs);
61 if (!d) {
62 *item = udev_list_entry_get_next(*item);
63 continue;
64 }
65
66 sn = udev_device_get_property_value(d, "ID_SEAT");
67 if (isempty(sn))
68 sn = "seat0";
69
d239d84a 70 /* Explicitly also check for tag 'seat' here */
309c2a2c 71 if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
24310c11
LP
72 *item = udev_list_entry_get_next(*item);
73 continue;
74 }
75
2d96536d 76 is_master = udev_device_has_tag(d, "master-of-seat");
d239d84a 77
24310c11
LP
78 name = udev_device_get_sysattr_value(d, "name");
79 if (!name)
80 name = udev_device_get_sysattr_value(d, "id");
81 subsystem = udev_device_get_subsystem(d);
82 sysname = udev_device_get_sysname(d);
83
84 /* Look if there's more coming after this */
85 lookahead = next = udev_list_entry_get_next(*item);
86 while (lookahead) {
87 const char *lookahead_sysfs;
88
89 lookahead_sysfs = udev_list_entry_get_name(lookahead);
90
91 if (path_startswith(lookahead_sysfs, sub) &&
92 !path_startswith(lookahead_sysfs, sysfs)) {
06acf2d4 93 _cleanup_udev_device_unref_ struct udev_device *lookahead_d = NULL;
24310c11
LP
94
95 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
96 if (lookahead_d) {
97 const char *lookahead_sn;
24310c11
LP
98
99 lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
100 if (isempty(lookahead_sn))
101 lookahead_sn = "seat0";
102
06acf2d4 103 if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
24310c11
LP
104 break;
105 }
106 }
107
108 lookahead = udev_list_entry_get_next(lookahead);
109 }
110
88e3dc90 111 k = ellipsize(sysfs, n_columns, 20);
06acf2d4
LP
112 if (!k)
113 return -ENOMEM;
114
115 printf("%s%s%s\n", prefix, draw_special_char(lookahead ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT), k);
24310c11 116
88e3dc90 117 if (asprintf(&l,
5d1fb81b
LP
118 "%s%s:%s%s%s%s",
119 is_master ? "[MASTER] " : "",
88e3dc90 120 subsystem, sysname,
5cfee414 121 name ? " \"" : "", strempty(name), name ? "\"" : "") < 0)
88e3dc90 122 return -ENOMEM;
88e3dc90 123
88e3dc90 124 free(k);
06acf2d4
LP
125 k = ellipsize(l, n_columns, 70);
126 if (!k)
127 return -ENOMEM;
128
7c83910b 129 printf("%s%s%s\n", prefix, lookahead ? draw_special_char(DRAW_TREE_VERTICAL) : " ", k);
24310c11
LP
130
131 *item = next;
132 if (*item) {
06acf2d4 133 _cleanup_free_ char *p = NULL;
24310c11 134
7c83910b 135 p = strappend(prefix, lookahead ? draw_special_char(DRAW_TREE_VERTICAL) : " ");
06acf2d4
LP
136 if (!p)
137 return -ENOMEM;
24310c11 138
06acf2d4
LP
139 show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
140 }
24310c11
LP
141 }
142
143 return 0;
144}
145
146int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
1ca208fb 147 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
06acf2d4 148 _cleanup_udev_unref_ struct udev *udev = NULL;
24310c11 149 struct udev_list_entry *first = NULL;
24310c11
LP
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);
1ca208fb 166 if (!e)
a051da35 167 return -ENOMEM;
24310c11
LP
168
169 if (!streq(seat, "seat0"))
170 r = udev_enumerate_add_match_tag(e, seat);
171 else
172 r = udev_enumerate_add_match_tag(e, "seat");
e1202047
LP
173 if (r < 0)
174 return r;
24310c11 175
e1202047 176 r = udev_enumerate_add_match_is_initialized(e);
3e085b6c 177 if (r < 0)
1ca208fb 178 return r;
3e085b6c 179
24310c11
LP
180 r = udev_enumerate_scan_devices(e);
181 if (r < 0)
1ca208fb 182 return r;
24310c11
LP
183
184 first = udev_enumerate_get_list_entry(e);
185 if (first)
186 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
ddae67fa
LP
187 else
188 printf("%s%s%s\n", prefix, draw_special_char(DRAW_TREE_RIGHT), "(none)");
24310c11 189
24310c11
LP
190 return r;
191}