Debian Codegraph @

src/udev/udev-dump.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "alloc-util.h"
4 #include "ansi-color.h"
5 #include "device-private.h"
6 #include "device-util.h"
7 #include "devnum-util.h"
8 #include "format-util.h"
9 #include "hashmap.h"
10 #include "json-util.h"
11 #include "string-util.h"
12 #include "strv.h"
13 #include "udev-builtin.h"
14 #include "udev-dump.h"
15 #include "udev-event.h"
16 #include "user-util.h"
17
18 static void event_cache_written_value(Hashmap **values, const char *attr, const char *value) {
19 assert(values);
20
21 _unused_ _cleanup_free_ void *key = NULL;
22 free(hashmap_remove2(*values, attr, &key));
23
24 if (hashmap_put_strdup_full(values, &path_hash_ops_free_free, attr, value) < 0)
25 log_oom_debug();
26 }
27
28 void event_cache_written_sysattr(UdevEvent *event, const char *attr, const char *value) {
29 event_cache_written_value(&event->written_sysattrs, attr, value);
30 }
31
32 void event_cache_written_sysctl(UdevEvent *event, const char *attr, const char *value) {
33 event_cache_written_value(&event->written_sysctls, attr, value);
34 }
35
36 static int dump_event_json(UdevEvent *event, sd_json_format_flags_t flags, FILE *f) {
37 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
38 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
39 const char *str;
40 int r;
41
42 if (sd_device_get_devpath(dev, &str) >= 0) {
43 r = sd_json_variant_set_field_string(&v, "path", str);
44 if (r < 0)
45 return r;
46 }
47
48 if (sd_device_get_sysname(dev, &str) >= 0) {
49 r = sd_json_variant_set_field_string(&v, "name", str);
50 if (r < 0)
51 return r;
52 }
53
54 unsigned sysnum;
55 if (device_get_sysnum_unsigned(dev, &sysnum) >= 0) {
56 r = sd_json_variant_set_field_unsigned(&v, "number", sysnum);
57 if (r < 0)
58 return r;
59 }
60
61 if (sd_device_get_device_id(dev, &str) >= 0) {
62 r = sd_json_variant_set_field_string(&v, "id", str);
63 if (r < 0)
64 return r;
65 }
66
67 const char *subsys = NULL;
68 if (sd_device_get_subsystem(dev, &subsys) >= 0) {
69 r = sd_json_variant_set_field_string(&v, "subsystem", subsys);
70 if (r < 0)
71 return r;
72 }
73
74 if (sd_device_get_driver_subsystem(dev, &str) >= 0) {
75 r = sd_json_variant_set_field_string(&v, "driverSubsystem", str);
76 if (r < 0)
77 return r;
78 }
79
80 if (sd_device_get_devtype(dev, &str) >= 0) {
81 r = sd_json_variant_set_field_string(&v, "type", str);
82 if (r < 0)
83 return r;
84 }
85
86 if (sd_device_get_driver(dev, &str) >= 0) {
87 r = sd_json_variant_set_field_string(&v, "driver", str);
88 if (r < 0)
89 return r;
90 }
91
92 if (sd_device_get_devname(dev, &str) >= 0) {
93 _cleanup_(sd_json_variant_unrefp) sd_json_variant *node = NULL;
94
95 r = sd_json_variant_set_field_string(&node, "path", str);
96 if (r < 0)
97 return r;
98
99 r = sd_json_variant_set_field_string(&node, "type", streq_ptr(subsys, "block") ? "block" : "char");
100 if (r < 0)
101 return r;
102
103 dev_t devnum;
104 if (sd_device_get_devnum(dev, &devnum) >= 0) {
105 r = sd_json_variant_set_fieldb(&node, "rdev", JSON_BUILD_DEVNUM(devnum));
106 if (r < 0)
107 return r;
108 }
109
110 _cleanup_(sd_json_variant_unrefp) sd_json_variant *owner = NULL;
111
112 uid_t uid = event->uid;
113 if (!uid_is_valid(uid))
114 (void) device_get_devnode_uid(dev, &uid);
115 if (uid_is_valid(uid)) {
116 _cleanup_free_ char *user = uid_to_name(uid);
117 if (!user)
118 return -ENOMEM;
119
120 r = sd_json_variant_set_field_unsigned(&owner, "uid", uid);
121 if (r < 0)
122 return r;
123
124 r = sd_json_variant_set_field_string(&owner, "userName", user);
125 if (r < 0)
126 return r;
127 }
128
129 gid_t gid = event->gid;
130 if (!gid_is_valid(gid))
131 (void) device_get_devnode_gid(dev, &gid);
132 if (gid_is_valid(gid)) {
133 _cleanup_free_ char *group = gid_to_name(gid);
134 if (!group)
135 return -ENOMEM;
136
137 r = sd_json_variant_set_field_unsigned(&owner, "gid", gid);
138 if (r < 0)
139 return r;
140
141 r = sd_json_variant_set_field_string(&owner, "groupName", group);
142 if (r < 0)
143 return r;
144 }
145
146 r = json_variant_set_field_non_null(&node, "owner", owner);
147 if (r < 0)
148 return r;
149
150 mode_t mode = event->mode;
151 if (mode == MODE_INVALID)
152 (void) device_get_devnode_mode(dev, &mode);
153 if (mode != MODE_INVALID) {
154 char mode_str[STRLEN("0755")+1];
155 xsprintf(mode_str, "%04o", mode & ~S_IFMT);
156
157 r = sd_json_variant_set_field_string(&node, "mode", mode_str);
158 if (r < 0)
159 return r;
160 }
161
162 _cleanup_strv_free_ char **links = NULL;
163 FOREACH_DEVICE_DEVLINK(dev, devlink) {
164 r = strv_extend(&links, devlink);
165 if (r < 0)
166 return r;
167 }
168
169 if (!strv_isempty(links)) {
170 int prio = 0;
171 (void) device_get_devlink_priority(dev, &prio);
172
173 r = sd_json_variant_set_field_integer(&node, "symlinkPriority", prio);
174 if (r < 0)
175 return r;
176
177 r = sd_json_variant_set_field_strv(&node, "symlinks", strv_sort(links));
178 if (r < 0)
179 return r;
180 }
181
182 _cleanup_(sd_json_variant_unrefp) sd_json_variant *labels = NULL;
183 const char *name, *label;
184 ORDERED_HASHMAP_FOREACH_KEY(label, name, event->seclabel_list) {
185 r = sd_json_variant_append_arraybo(
186 &labels,
187 SD_JSON_BUILD_PAIR_STRING("name", name),
188 SD_JSON_BUILD_PAIR_STRING("label", label));
189 if (r < 0)
190 return r;
191 }
192
193 r = json_variant_set_field_non_null(&node, "securityLabels", labels);
194 if (r < 0)
195 return r;
196
197 r = sd_json_variant_set_field_boolean(&node, "inotifyWatch", event->inotify_watch);
198 if (r < 0)
199 return r;
200
201 r = json_variant_set_field_non_null(&v, "node", node);
202 if (r < 0)
203 return r;
204 }
205
206 int ifindex;
207 if (sd_device_get_ifindex(dev, &ifindex) >= 0) {
208 _cleanup_(sd_json_variant_unrefp) sd_json_variant *netif = NULL;
209
210 r = sd_json_variant_set_field_integer(&netif, "index", ifindex);
211 if (r < 0)
212 return r;
213
214 if (!isempty(event->name)) {
215 r = sd_json_variant_set_field_string(&netif, "name", event->name);
216 if (r < 0)
217 return r;
218 }
219
220 if (!strv_isempty(event->altnames)) {
221 r = sd_json_variant_set_field_strv(&netif, "alternativeNames", strv_sort(event->altnames));
222 if (r < 0)
223 return r;
224 }
225
226 r = json_variant_set_field_non_null(&v, "networkInterface", netif);
227 if (r < 0)
228 return r;
229 }
230
231 _cleanup_(sd_json_variant_unrefp) sd_json_variant *sysattrs = NULL;
232 const char *key, *value;
233 HASHMAP_FOREACH_KEY(value, key, event->written_sysattrs) {
234 r = sd_json_variant_append_arraybo(
235 &sysattrs,
236 SD_JSON_BUILD_PAIR_STRING("path", key),
237 SD_JSON_BUILD_PAIR_STRING("value", value));
238 if (r < 0)
239 return r;
240 }
241
242 r = json_variant_set_field_non_null(&v, "sysfsAttributes", sysattrs);
243 if (r < 0)
244 return r;
245
246 _cleanup_(sd_json_variant_unrefp) sd_json_variant *sysctls = NULL;
247 HASHMAP_FOREACH_KEY(value, key, event->written_sysctls) {
248 r = sd_json_variant_append_arraybo(
249 &sysctls,
250 SD_JSON_BUILD_PAIR_STRING("path", key),
251 SD_JSON_BUILD_PAIR_STRING("value", value));
252 if (r < 0)
253 return r;
254 }
255
256 r = json_variant_set_field_non_null(&v, "sysctl", sysctls);
257 if (r < 0)
258 return r;
259
260 _cleanup_strv_free_ char **tags = NULL;
261 FOREACH_DEVICE_TAG(dev, tag) {
262 r = strv_extend(&tags, tag);
263 if (r < 0)
264 return r;
265 }
266
267 if (!strv_isempty(tags)) {
268 r = sd_json_variant_set_field_strv(&v, "tags", strv_sort(tags));
269 if (r < 0)
270 return r;
271 }
272
273 tags = strv_free(tags);
274
275 FOREACH_DEVICE_CURRENT_TAG(dev, tag) {
276 r = strv_extend(&tags, tag);
277 if (r < 0)
278 return r;
279 }
280
281 if (!strv_isempty(tags)) {
282 r = sd_json_variant_set_field_strv(&v, "currentTags", strv_sort(tags));
283 if (r < 0)
284 return r;
285 }
286
287 char **properties;
288 if (device_get_properties_strv(dev, &properties) >= 0 && !strv_isempty(properties)) {
289 r = sd_json_variant_set_field_strv(&v, "properties", strv_sort(properties));
290 if (r < 0)
291 return r;
292 }
293
294 _cleanup_(sd_json_variant_unrefp) sd_json_variant *commands = NULL;
295 void *val;
296 const char *command;
297 ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) {
298 r = sd_json_variant_append_arraybo(
299 &commands,
300 SD_JSON_BUILD_PAIR_STRING("type", PTR_TO_UDEV_BUILTIN_CMD(val) >= 0 ? "builtin" : "program"),
301 SD_JSON_BUILD_PAIR_STRING("command", command));
302 if (r < 0)
303 return r;
304 }
305
306 r = json_variant_set_field_non_null(&v, "queuedCommands", commands);
307 if (r < 0)
308 return r;
309
310 return sd_json_variant_dump(v, flags, f, /* prefix= */ NULL);
311 }
312
313 int dump_event(UdevEvent *event, sd_json_format_flags_t flags, FILE *f) {
314 if (sd_json_format_enabled(flags))
315 return dump_event_json(event, flags, f);
316
317 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
318 const char *subsys = NULL, *str;
319
320 if (!f)
321 f = stdout;
322
323 if (sd_device_get_devpath(dev, &str) >= 0)
324 fprintf(f, "%sDevice path:%s\n %s\n", ansi_highlight(), ansi_normal(), str);
325
326 if (sd_device_get_sysname(dev, &str) >= 0)
327 fprintf(f, "%sDevice name:%s\n %s\n", ansi_highlight(), ansi_normal(), str);
328
329 if (sd_device_get_sysnum(dev, &str) >= 0)
330 fprintf(f, "%sDevice number:%s\n %s\n", ansi_highlight(), ansi_normal(), str);
331
332 if (sd_device_get_device_id(dev, &str) >= 0)
333 fprintf(f, "%sDevice ID:%s\n %s\n", ansi_highlight(), ansi_normal(), str);
334
335 if (sd_device_get_subsystem(dev, &subsys) >= 0) {
336 const char *driver_subsys = NULL;
337 (void) sd_device_get_driver_subsystem(dev, &driver_subsys);
338 fprintf(f, "%sSubsystem:%s\n %s%s%s%s\n", ansi_highlight(), ansi_normal(), subsys,
339 driver_subsys ? " (" : "",
340 strempty(driver_subsys),
341 driver_subsys ? ")" : "");
342 }
343
344 if (sd_device_get_devtype(dev, &str) >= 0)
345 fprintf(f, "%sDevice type:%s\n %s\n", ansi_highlight(), ansi_normal(), str);
346
347 if (sd_device_get_driver(dev, &str) >= 0)
348 fprintf(f, "%sDevice driver:%s\n %s\n", ansi_highlight(), ansi_normal(), str);
349
350 if (sd_device_get_devname(dev, &str) >= 0) {
351 dev_t devnum;
352 if (sd_device_get_devnum(dev, &devnum) >= 0)
353 fprintf(f, "%sDevice node:%s\n %s (%s "DEVNUM_FORMAT_STR")\n", ansi_highlight(), ansi_normal(), str,
354 streq_ptr(subsys, "block") ? "block" : "char",
355 DEVNUM_FORMAT_VAL(devnum));
356
357 uid_t uid = event->uid;
358 if (!uid_is_valid(uid))
359 (void) device_get_devnode_uid(dev, &uid);
360 if (uid_is_valid(uid)) {
361 _cleanup_free_ char *user = uid_to_name(uid);
362 fprintf(f, "%sDevice node owner user:%s\n %s (uid="UID_FMT")\n", ansi_highlight(), ansi_normal(), strna(user), uid);
363 }
364
365 gid_t gid = event->gid;
366 if (!gid_is_valid(gid))
367 (void) device_get_devnode_gid(dev, &gid);
368 if (gid_is_valid(gid)) {
369 _cleanup_free_ char *group = gid_to_name(gid);
370 fprintf(f, "%sDevice node owner group:%s\n %s (gid="GID_FMT")\n", ansi_highlight(), ansi_normal(), strna(group), gid);
371 }
372
373 mode_t mode = event->mode;
374 if (mode == MODE_INVALID)
375 (void) device_get_devnode_mode(dev, &mode);
376 if (mode != MODE_INVALID)
377 fprintf(f, "%sDevice node permission:%s\n %04o\n", ansi_highlight(), ansi_normal(), mode);
378
379 if (sd_device_get_devlink_first(dev)) {
380 int prio = 0;
381 (void) device_get_devlink_priority(dev, &prio);
382 fprintf(f, "%sDevice node symlinks:%s (priority=%i)\n", ansi_highlight(), ansi_normal(), prio);
383 FOREACH_DEVICE_DEVLINK(dev, devlink)
384 fprintf(f, " %s\n", devlink);
385 }
386
387 if (!ordered_hashmap_isempty(event->seclabel_list)) {
388 const char *name, *label;
389 fprintf(f, "%sDevice node security label:%s\n", ansi_highlight(), ansi_normal());
390 ORDERED_HASHMAP_FOREACH_KEY(label, name, event->seclabel_list)
391 fprintf(f, " %s : %s\n", name, label);
392 }
393
394 fprintf(f, "%sInotify watch:%s\n %s\n", ansi_highlight(), ansi_normal(), enabled_disabled(event->inotify_watch));
395 }
396
397 int ifindex;
398 if (sd_device_get_ifindex(dev, &ifindex) >= 0) {
399 fprintf(f, "%sNetwork interface index:%s\n %i\n", ansi_highlight(), ansi_normal(), ifindex);
400
401 if (!isempty(event->name))
402 fprintf(f, "%sNetwork interface name:%s\n %s\n", ansi_highlight(), ansi_normal(), event->name);
403
404 if (!strv_isempty(event->altnames)) {
405 bool space = true;
406 fprintf(f, "%sAlternative interface names:%s", ansi_highlight(), ansi_normal());
407 fputstrv(f, strv_sort(event->altnames), "\n ", &space);
408 fputs("\n", f);
409 }
410 }
411
412 if (!hashmap_isempty(event->written_sysattrs)) {
413 const char *key, *value;
414
415 fprintf(f, "%sWritten sysfs attributes:%s\n", ansi_highlight(), ansi_normal());
416 HASHMAP_FOREACH_KEY(value, key, event->written_sysattrs)
417 fprintf(f, " %s : %s\n", key, value);
418 }
419
420 if (!hashmap_isempty(event->written_sysctls)) {
421 const char *key, *value;
422
423 fprintf(f, "%sWritten sysctl entries:%s\n", ansi_highlight(), ansi_normal());
424 HASHMAP_FOREACH_KEY(value, key, event->written_sysctls)
425 fprintf(f, " %s : %s\n", key, value);
426 }
427
428 if (sd_device_get_tag_first(dev)) {
429 fprintf(f, "%sTags:%s\n", ansi_highlight(), ansi_normal());
430 FOREACH_DEVICE_TAG(dev, tag)
431 fprintf(f, " %s\n", tag);
432 }
433
434 if (sd_device_get_current_tag_first(dev)) {
435 fprintf(f, "%sCurrent Tags:%s\n", ansi_highlight(), ansi_normal());
436 FOREACH_DEVICE_CURRENT_TAG(dev, tag)
437 fprintf(f, " %s\n", tag);
438 }
439
440 char **properties;
441 if (device_get_properties_strv(dev, &properties) >= 0 && !strv_isempty(properties)) {
442 bool space = true;
443 fprintf(f, "%sProperties:%s", ansi_highlight(), ansi_normal());
444 fputstrv(f, strv_sort(properties), "\n ", &space);
445 fputs("\n", f);
446 }
447
448 if (!ordered_hashmap_isempty(event->run_list)) {
449 void *val;
450 const char *command;
451 fprintf(f, "%sQueued commands:%s\n", ansi_highlight(), ansi_normal());
452 ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) {
453 UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val);
454
455 if (builtin_cmd >= 0)
456 fprintf(f, " RUN{builtin} : %s\n", command);
457 else
458 fprintf(f, " RUN{program} : %s\n", command);
459 }
460 }
461
462 return 0;
463 }