udevadm: check for oom and other style fixes

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-02-09 12:04:03 +01:00
parent 792cc203a6
commit 6bcc09bea1

View file

@ -35,8 +35,9 @@
static int verbose; static int verbose;
static int dry_run; static int dry_run;
static void exec_list(struct udev_enumerate *udev_enumerate, const char *action, Set *settle_set) { static int exec_list(struct udev_enumerate *udev_enumerate, const char *action, Set *settle_set) {
struct udev_list_entry *entry; struct udev_list_entry *entry;
int r;
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) { udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
char filename[UTIL_PATH_SIZE]; char filename[UTIL_PATH_SIZE];
@ -53,12 +54,17 @@ static void exec_list(struct udev_enumerate *udev_enumerate, const char *action,
fd = open(filename, O_WRONLY|O_CLOEXEC); fd = open(filename, O_WRONLY|O_CLOEXEC);
if (fd < 0) if (fd < 0)
continue; continue;
if (settle_set != NULL) if (settle_set) {
set_put_strdup(settle_set, syspath); r = set_put_strdup(settle_set, syspath);
if (r < 0)
return log_oom();
}
if (write(fd, action, strlen(action)) < 0) if (write(fd, action, strlen(action)) < 0)
log_debug_errno(errno, "error writing '%s' to '%s': %m", action, filename); log_debug_errno(errno, "error writing '%s' to '%s': %m", action, filename);
close(fd); close(fd);
} }
return 0;
} }
static const char *keyval(const char *str, const char **val, char *buf, size_t size) { static const char *keyval(const char *str, const char **val, char *buf, size_t size) {
@ -137,7 +143,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
int c, r; int c, r;
udev_enumerate = udev_enumerate_new(udev); udev_enumerate = udev_enumerate_new(udev);
if (udev_enumerate == NULL) if (!udev_enumerate)
return 1; return 1;
while ((c = getopt_long(argc, argv, "vnt:c:s:S:a:A:p:g:y:b:wVh", options, NULL)) >= 0) { while ((c = getopt_long(argc, argv, "vnt:c:s:S:a:A:p:g:y:b:wVh", options, NULL)) >= 0) {
@ -226,7 +232,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
_cleanup_udev_device_unref_ struct udev_device *dev; _cleanup_udev_device_unref_ struct udev_device *dev;
dev = find_device(udev, optarg, "/sys"); dev = find_device(udev, optarg, "/sys");
if (dev == NULL) { if (!dev) {
log_error("unable to open the device '%s'", optarg); log_error("unable to open the device '%s'", optarg);
return 2; return 2;
} }
@ -246,7 +252,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
_cleanup_udev_device_unref_ struct udev_device *dev; _cleanup_udev_device_unref_ struct udev_device *dev;
dev = find_device(udev, optarg, "/dev/"); dev = find_device(udev, optarg, "/dev/");
if (dev == NULL) { if (!dev) {
log_error("unable to open the device '%s'", optarg); log_error("unable to open the device '%s'", optarg);
return 2; return 2;
} }
@ -276,7 +282,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
_cleanup_udev_device_unref_ struct udev_device *dev; _cleanup_udev_device_unref_ struct udev_device *dev;
dev = find_device(udev, argv[optind], NULL); dev = find_device(udev, argv[optind], NULL);
if (dev == NULL) { if (!dev) {
log_error("unable to open the device '%s'", argv[optind]); log_error("unable to open the device '%s'", argv[optind]);
return 2; return 2;
} }
@ -296,7 +302,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
} }
udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
if (udev_monitor == NULL) { if (!udev_monitor) {
log_error("error: unable to create netlink socket"); log_error("error: unable to create netlink socket");
return 3; return 3;
} }
@ -314,7 +320,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
} }
settle_set = set_new(&string_hash_ops); settle_set = set_new(&string_hash_ops);
if (settle_set == NULL) { if (!settle_set) {
log_oom(); log_oom();
return 1; return 1;
} }
@ -330,7 +336,9 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
default: default:
assert_not_reached("device_type"); assert_not_reached("device_type");
} }
exec_list(udev_enumerate, action, settle_set); r = exec_list(udev_enumerate, action, settle_set);
if (r < 0)
return 1;
while (!set_isempty(settle_set)) { while (!set_isempty(settle_set)) {
int fdcount; int fdcount;
@ -350,7 +358,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
const char *syspath = NULL; const char *syspath = NULL;
device = udev_monitor_receive_device(udev_monitor); device = udev_monitor_receive_device(udev_monitor);
if (device == NULL) if (!device)
continue; continue;
syspath = udev_device_get_syspath(device); syspath = udev_device_get_syspath(device);