1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-28 21:47:38 +03:00

core,logind: libudev usage modernizations

Always use cleanup logic and don't eat up errors returned by libudev
This commit is contained in:
Lennart Poettering 2013-12-18 17:13:42 +01:00
parent e120204729
commit 06acf2d46a
3 changed files with 72 additions and 88 deletions

View File

@ -202,9 +202,10 @@ finish:
}
static int loopback_list_get(MountPoint **head) {
_cleanup_udev_unref_ struct udev *udev;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
int r;
assert(head);
@ -216,13 +217,21 @@ static int loopback_list_get(MountPoint **head) {
if (!e)
return -ENOMEM;
if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
udev_enumerate_add_match_sysname(e, "loop*") < 0 ||
udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0)
return -EIO;
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0)
return r;
if (udev_enumerate_scan_devices(e) < 0)
return -EIO;
r = udev_enumerate_add_match_sysname(e, "loop*");
if (r < 0)
return r;
r = udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL);
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
@ -257,9 +266,10 @@ static int loopback_list_get(MountPoint **head) {
}
static int dm_list_get(MountPoint **head) {
_cleanup_udev_unref_ struct udev *udev;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
int r;
assert(head);
@ -271,15 +281,19 @@ static int dm_list_get(MountPoint **head) {
if (!e)
return -ENOMEM;
if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
udev_enumerate_add_match_sysname(e, "dm-*") < 0)
return -EIO;
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0)
return r;
if (udev_enumerate_scan_devices(e) < 0)
return -EIO;
r = udev_enumerate_add_match_sysname(e, "dm-*");
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
MountPoint *m;
_cleanup_udev_device_unref_ struct udev_device *d;

View File

@ -42,6 +42,7 @@
#include "bus-error.h"
#include "logind.h"
#include "bus-errors.h"
#include "udev-util.h"
static int property_get_idle_hint(
sd_bus *bus,
@ -1059,29 +1060,25 @@ static int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *us
}
static int trigger_device(Manager *m, struct udev_device *d) {
struct udev_enumerate *e;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *first, *item;
int r;
assert(m);
e = udev_enumerate_new(m->udev);
if (!e) {
r = -ENOMEM;
goto finish;
}
if (!e)
return -ENOMEM;
if (d) {
if (udev_enumerate_add_match_parent(e, d) < 0) {
r = -EIO;
goto finish;
}
r = udev_enumerate_add_match_parent(e, d);
if (r < 0)
return r;
}
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
}
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
@ -1091,27 +1088,19 @@ static int trigger_device(Manager *m, struct udev_device *d) {
p = udev_list_entry_get_name(item);
t = strappend(p, "/uevent");
if (!t) {
r = -ENOMEM;
goto finish;
}
if (!t)
return -ENOMEM;
write_string_file(t, "change");
}
r = 0;
finish:
if (e)
udev_enumerate_unref(e);
return r;
return 0;
}
static int attach_device(Manager *m, const char *seat, const char *sysfs) {
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
_cleanup_free_ char *rule = NULL, *file = NULL;
const char *id_for_seat;
struct udev_device *d;
int r;
assert(m);
@ -1122,40 +1111,26 @@ static int attach_device(Manager *m, const char *seat, const char *sysfs) {
if (!d)
return -ENODEV;
if (!udev_device_has_tag(d, "seat")) {
r = -ENODEV;
goto finish;
}
if (!udev_device_has_tag(d, "seat"))
return -ENODEV;
id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
if (!id_for_seat) {
r = -ENODEV;
goto finish;
}
if (!id_for_seat)
return -ENODEV;
if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
r = -ENOMEM;
goto finish;
}
if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
return -ENOMEM;
if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
r = -ENOMEM;
goto finish;
}
if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
return -ENOMEM;
mkdir_p_label("/etc/udev/rules.d", 0755);
label_init("/etc");
r = write_string_file_atomic_label(file, rule);
if (r < 0)
goto finish;
return r;
r = trigger_device(m, d);
finish:
if (d)
udev_device_unref(d);
return r;
return trigger_device(m, d);
}
static int flush_devices(Manager *m) {

View File

@ -42,10 +42,10 @@ static int show_sysfs_one(
assert(prefix);
while (*item) {
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
struct udev_list_entry *next, *lookahead;
struct udev_device *d;
const char *sn, *name, *sysfs, *subsystem, *sysname;
char *l, *k;
_cleanup_free_ char *k = NULL, *l = NULL;
bool is_master;
sysfs = udev_list_entry_get_name(*item);
@ -64,7 +64,6 @@ static int show_sysfs_one(
/* Explicitly also check for tag 'seat' here */
if (!streq(seat, sn) || !udev_device_has_tag(d, "seat")) {
udev_device_unref(d);
*item = udev_list_entry_get_next(*item);
continue;
}
@ -86,21 +85,17 @@ static int show_sysfs_one(
if (path_startswith(lookahead_sysfs, sub) &&
!path_startswith(lookahead_sysfs, sysfs)) {
struct udev_device *lookahead_d;
_cleanup_udev_device_unref_ struct udev_device *lookahead_d = NULL;
lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
if (lookahead_d) {
const char *lookahead_sn;
bool found;
lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
if (isempty(lookahead_sn))
lookahead_sn = "seat0";
found = streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat");
udev_device_unref(lookahead_d);
if (found)
if (streq(seat, lookahead_sn) && udev_device_has_tag(lookahead_d, "seat"))
break;
}
}
@ -109,43 +104,43 @@ static int show_sysfs_one(
}
k = ellipsize(sysfs, n_columns, 20);
printf("%s%s%s\n", prefix, draw_special_char(lookahead ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT),
k ? k : sysfs);
free(k);
if (!k)
return -ENOMEM;
printf("%s%s%s\n", prefix, draw_special_char(lookahead ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT), k);
if (asprintf(&l,
"%s%s:%s%s%s%s",
is_master ? "[MASTER] " : "",
subsystem, sysname,
name ? " \"" : "", name ? name : "", name ? "\"" : "") < 0) {
udev_device_unref(d);
name ? " \"" : "", name ? name : "", name ? "\"" : "") < 0)
return -ENOMEM;
}
k = ellipsize(l, n_columns, 70);
printf("%s%s%s\n", prefix, lookahead ? draw_special_char(DRAW_TREE_VERT) : " ",
k ? k : l);
free(k);
free(l);
k = ellipsize(l, n_columns, 70);
if (!k)
return -ENOMEM;
printf("%s%s%s\n", prefix, lookahead ? draw_special_char(DRAW_TREE_VERT) : " ", k);
*item = next;
if (*item) {
char *p;
_cleanup_free_ char *p = NULL;
p = strappend(prefix, lookahead ? draw_special_char(DRAW_TREE_VERT) : " ");
show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2);
free(p);
}
if (!p)
return -ENOMEM;
udev_device_unref(d);
show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
}
}
return 0;
}
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
_cleanup_udev_unref_ struct udev *udev;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
struct udev_list_entry *first = NULL;
int r;