Merge pull request #28365 from DaanDeMeyer/udevadm-query
Various fixes and improvements
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
static int device_is_power_sink(sd_device *device) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
bool found_source = false, found_sink = false;
|
||||
sd_device *parent, *d;
|
||||
sd_device *parent;
|
||||
int r;
|
||||
|
||||
assert(device);
|
||||
@@ -109,7 +109,6 @@ static bool battery_is_discharging(sd_device *d) {
|
||||
int on_ac_power(void) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
bool found_ac_online = false, found_discharging_battery = false;
|
||||
sd_device *d;
|
||||
int r;
|
||||
|
||||
r = sd_device_enumerator_new(&e);
|
||||
@@ -234,7 +233,6 @@ int battery_read_capacity_percentage(sd_device *dev) {
|
||||
int battery_is_discharging_and_low(void) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
bool unsure = false, found_low = false;
|
||||
sd_device *dev;
|
||||
int r;
|
||||
|
||||
/* We have not used battery capacity_level since value is set to full
|
||||
|
||||
@@ -107,7 +107,6 @@ int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
|
||||
static int block_device_get_originating(sd_device *dev, sd_device **ret) {
|
||||
_cleanup_(sd_device_unrefp) sd_device *first_found = NULL;
|
||||
const char *suffix;
|
||||
sd_device *child;
|
||||
dev_t devnum = 0; /* avoid false maybe-uninitialized warning */
|
||||
|
||||
/* For the specified block device tries to chase it through the layers, in case LUKS-style DM
|
||||
@@ -665,7 +664,6 @@ int block_device_remove_all_partitions(sd_device *dev, int fd) {
|
||||
_cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
|
||||
_cleanup_close_ int fd_close = -EBADF;
|
||||
bool has_partitions = false;
|
||||
sd_device *part;
|
||||
int r, k = 0;
|
||||
|
||||
assert(dev || fd >= 0);
|
||||
|
||||
@@ -143,7 +143,6 @@ int devnode_acl_all(const char *seat,
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
_cleanup_set_free_ Set *nodes = NULL;
|
||||
_cleanup_closedir_ DIR *dir = NULL;
|
||||
sd_device *d;
|
||||
char *n;
|
||||
int r;
|
||||
|
||||
|
||||
+22
-45
@@ -2828,10 +2828,8 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) {
|
||||
/* If sorting is requested, let's calculate an index table we use to lookup the actual index to display with. */
|
||||
|
||||
sorted = new(size_t, n_rows);
|
||||
if (!sorted) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!sorted)
|
||||
return -ENOMEM;
|
||||
|
||||
for (size_t i = 0; i < n_rows; i++)
|
||||
sorted[i] = i * t->n_columns;
|
||||
@@ -2846,10 +2844,10 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) {
|
||||
assert(display_columns > 0);
|
||||
|
||||
elements = new0(JsonVariant*, display_columns * 2);
|
||||
if (!elements) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!elements)
|
||||
return -ENOMEM;
|
||||
|
||||
CLEANUP_ARRAY(elements, (size_t) { display_columns * 2 }, json_variant_unref_many);
|
||||
|
||||
for (size_t j = 0; j < display_columns; j++) {
|
||||
_cleanup_free_ char *mangled = NULL;
|
||||
@@ -2863,21 +2861,21 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) {
|
||||
if (!n) {
|
||||
r = table_make_json_field_name(t, ASSERT_PTR(t->data[c]), &mangled);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
n = mangled;
|
||||
}
|
||||
|
||||
r = json_variant_new_string(elements + j*2, n);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
|
||||
rows = new0(JsonVariant*, n_rows-1);
|
||||
if (!rows) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!rows)
|
||||
return -ENOMEM;
|
||||
|
||||
CLEANUP_ARRAY(rows, (size_t) { n_rows - 1 }, json_variant_unref_many);
|
||||
|
||||
for (size_t i = 1; i < n_rows; i++) {
|
||||
TableData **row;
|
||||
@@ -2898,28 +2896,15 @@ static int table_to_json_regular(Table *t, JsonVariant **ret) {
|
||||
|
||||
r = table_data_to_json(d, elements + k);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
|
||||
r = json_variant_new_object(rows + i - 1, elements, display_columns * 2);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
|
||||
r = json_variant_new_array(ret, rows, n_rows - 1);
|
||||
|
||||
finish:
|
||||
if (rows) {
|
||||
json_variant_unref_many(rows, n_rows-1);
|
||||
free(rows);
|
||||
}
|
||||
|
||||
if (elements) {
|
||||
json_variant_unref_many(elements, display_columns*2);
|
||||
free(elements);
|
||||
}
|
||||
|
||||
return r;
|
||||
return json_variant_new_array(ret, rows, n_rows - 1);
|
||||
}
|
||||
|
||||
static int table_to_json_vertical(Table *t, JsonVariant **ret) {
|
||||
@@ -2937,10 +2922,10 @@ static int table_to_json_vertical(Table *t, JsonVariant **ret) {
|
||||
assert(t->n_cells % t->n_columns == 0);
|
||||
|
||||
elements = new0(JsonVariant *, t->n_cells);
|
||||
if (!elements) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
if (!elements)
|
||||
return -ENOMEM;
|
||||
|
||||
CLEANUP_ARRAY(elements, n_elements, json_variant_unref_many);
|
||||
|
||||
for (size_t i = t->n_columns; i < t->n_cells; i++) {
|
||||
|
||||
@@ -2952,7 +2937,7 @@ static int table_to_json_vertical(Table *t, JsonVariant **ret) {
|
||||
if (!n) {
|
||||
r = table_make_json_field_name(t, ASSERT_PTR(t->data[i]), &mangled);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
n = mangled;
|
||||
}
|
||||
@@ -2961,20 +2946,12 @@ static int table_to_json_vertical(Table *t, JsonVariant **ret) {
|
||||
} else
|
||||
r = table_data_to_json(t->data[i], elements + n_elements);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
n_elements++;
|
||||
}
|
||||
|
||||
r = json_variant_new_object(ret, elements, n_elements);
|
||||
|
||||
finish:
|
||||
if (elements) {
|
||||
json_variant_unref_many(elements, n_elements);
|
||||
free(elements);
|
||||
}
|
||||
|
||||
return r;
|
||||
return json_variant_new_object(ret, elements, n_elements);
|
||||
}
|
||||
|
||||
int table_to_json(Table *t, JsonVariant **ret) {
|
||||
|
||||
+3
-2
@@ -881,6 +881,8 @@ void json_variant_unref_many(JsonVariant **array, size_t n) {
|
||||
|
||||
for (size_t i = 0; i < n; i++)
|
||||
json_variant_unref(array[i]);
|
||||
|
||||
free(array);
|
||||
}
|
||||
|
||||
const char *json_variant_string(JsonVariant *v) {
|
||||
@@ -2879,8 +2881,7 @@ typedef struct JsonStack {
|
||||
static void json_stack_release(JsonStack *s) {
|
||||
assert(s);
|
||||
|
||||
json_variant_unref_many(s->elements, s->n_elements);
|
||||
s->elements = mfree(s->elements);
|
||||
CLEANUP_ARRAY(s->elements, s->n_elements, json_variant_unref_many);
|
||||
}
|
||||
|
||||
static int json_parse_internal(
|
||||
|
||||
+47
-53
@@ -936,11 +936,27 @@ void json_escape(
|
||||
}
|
||||
}
|
||||
|
||||
struct json_data {
|
||||
typedef struct JsonData {
|
||||
JsonVariant* name;
|
||||
size_t n_values;
|
||||
JsonVariant* values[];
|
||||
};
|
||||
} JsonData;
|
||||
|
||||
static JsonData* json_data_free(struct JsonData *d) {
|
||||
if (!d)
|
||||
return NULL;
|
||||
|
||||
json_variant_unref(d->name);
|
||||
|
||||
FOREACH_ARRAY(v, d->values, d->n_values)
|
||||
json_variant_unref(*v);
|
||||
|
||||
return mfree(d);
|
||||
}
|
||||
|
||||
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(json_data_hash_ops_free,
|
||||
char, string_hash_func, string_compare_func,
|
||||
JsonData, json_data_free);
|
||||
|
||||
static int update_json_data(
|
||||
Hashmap *h,
|
||||
@@ -950,7 +966,7 @@ static int update_json_data(
|
||||
size_t size) {
|
||||
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
|
||||
struct json_data *d;
|
||||
JsonData *d = NULL;
|
||||
int r;
|
||||
|
||||
assert(name);
|
||||
@@ -970,9 +986,9 @@ static int update_json_data(
|
||||
|
||||
d = hashmap_get(h, name);
|
||||
if (d) {
|
||||
struct json_data *w;
|
||||
JsonData *w;
|
||||
|
||||
w = realloc(d, offsetof(struct json_data, values) + sizeof(JsonVariant*) * (d->n_values + 1));
|
||||
w = realloc(d, offsetof(JsonData, values) + sizeof(JsonVariant*) * (d->n_values + 1));
|
||||
if (!w)
|
||||
return log_oom();
|
||||
|
||||
@@ -985,7 +1001,7 @@ static int update_json_data(
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to allocate JSON name variant: %m");
|
||||
|
||||
d = malloc0(offsetof(struct json_data, values) + sizeof(JsonVariant*));
|
||||
d = malloc0(offsetof(JsonData, values) + sizeof(JsonVariant*));
|
||||
if (!d)
|
||||
return log_oom();
|
||||
|
||||
@@ -1049,12 +1065,12 @@ static int output_json(
|
||||
|
||||
char usecbuf[CONST_MAX(DECIMAL_STR_MAX(usec_t), DECIMAL_STR_MAX(uint64_t))];
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *object = NULL;
|
||||
_cleanup_hashmap_free_ Hashmap *h = NULL;
|
||||
sd_id128_t journal_boot_id, seqnum_id;
|
||||
_cleanup_free_ char *cursor = NULL;
|
||||
usec_t realtime, monotonic;
|
||||
JsonVariant **array = NULL;
|
||||
struct json_data *d;
|
||||
Hashmap *h = NULL;
|
||||
JsonData *d;
|
||||
uint64_t seqnum;
|
||||
size_t n = 0;
|
||||
int r;
|
||||
@@ -1083,36 +1099,36 @@ static int output_json(
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get seqnum: %m");
|
||||
|
||||
h = hashmap_new(&string_hash_ops);
|
||||
h = hashmap_new(&json_data_hash_ops_free);
|
||||
if (!h)
|
||||
return log_oom();
|
||||
|
||||
r = update_json_data(h, flags, "__CURSOR", cursor, SIZE_MAX);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
xsprintf(usecbuf, USEC_FMT, realtime);
|
||||
r = update_json_data(h, flags, "__REALTIME_TIMESTAMP", usecbuf, SIZE_MAX);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
xsprintf(usecbuf, USEC_FMT, monotonic);
|
||||
r = update_json_data(h, flags, "__MONOTONIC_TIMESTAMP", usecbuf, SIZE_MAX);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = update_json_data(h, flags, "_BOOT_ID", SD_ID128_TO_STRING(journal_boot_id), SIZE_MAX);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
xsprintf(usecbuf, USEC_FMT, seqnum);
|
||||
r = update_json_data(h, flags, "__SEQNUM", usecbuf, SIZE_MAX);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
r = update_json_data(h, flags, "__SEQNUM_ID", SD_ID128_TO_STRING(seqnum_id), SIZE_MAX);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
|
||||
for (;;) {
|
||||
const void *data;
|
||||
@@ -1121,26 +1137,23 @@ static int output_json(
|
||||
r = sd_journal_enumerate_data(j, &data, &size);
|
||||
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
|
||||
log_debug_errno(r, "Skipping message we can't read: %m");
|
||||
r = 0;
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to read journal: %m");
|
||||
goto finish;
|
||||
return 0;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to read journal: %m");
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
r = update_json_data_split(h, flags, output_fields, data, size);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
return r;
|
||||
}
|
||||
|
||||
array = new(JsonVariant*, hashmap_size(h)*2);
|
||||
if (!array) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
if (!array)
|
||||
return log_oom();
|
||||
|
||||
CLEANUP_ARRAY(array, n, json_variant_unref_many);
|
||||
|
||||
HASHMAP_FOREACH(d, h) {
|
||||
assert(d->n_values > 0);
|
||||
@@ -1153,41 +1166,22 @@ static int output_json(
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *q = NULL;
|
||||
|
||||
r = json_variant_new_array(&q, d->values, d->n_values);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to create JSON array: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to create JSON array: %m");
|
||||
|
||||
array[n++] = TAKE_PTR(q);
|
||||
}
|
||||
}
|
||||
|
||||
r = json_variant_new_object(&object, array, n);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to allocate JSON object: %m");
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to allocate JSON object: %m");
|
||||
|
||||
json_variant_dump(object,
|
||||
output_mode_to_json_format_flags(mode) |
|
||||
(FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0),
|
||||
f, NULL);
|
||||
|
||||
r = 0;
|
||||
|
||||
finish:
|
||||
while ((d = hashmap_steal_first(h))) {
|
||||
json_variant_unref(d->name);
|
||||
json_variant_unref_many(d->values, d->n_values);
|
||||
free(d);
|
||||
}
|
||||
|
||||
hashmap_free(h);
|
||||
|
||||
json_variant_unref_many(array, n);
|
||||
free(array);
|
||||
|
||||
return r;
|
||||
return json_variant_dump(object,
|
||||
output_mode_to_json_format_flags(mode) |
|
||||
(FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0),
|
||||
f, NULL);
|
||||
}
|
||||
|
||||
static int output_cat_field(
|
||||
|
||||
@@ -143,7 +143,6 @@ int get_capacity_by_name(Hashmap *capacities_by_name, const char *name) {
|
||||
int fetch_batteries_capacity_by_name(Hashmap **ret) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
_cleanup_hashmap_free_ Hashmap *batteries_capacity_by_name = NULL;
|
||||
sd_device *dev;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
@@ -334,7 +333,6 @@ int estimate_battery_discharge_rate_per_hour(
|
||||
usec_t after_timestamp) {
|
||||
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
sd_device *dev;
|
||||
bool trunc = true;
|
||||
int r;
|
||||
|
||||
@@ -394,7 +392,6 @@ int estimate_battery_discharge_rate_per_hour(
|
||||
int get_total_suspend_interval(Hashmap *last_capacity, usec_t *ret) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
usec_t total_suspend_interval = 0;
|
||||
sd_device *dev;
|
||||
int r;
|
||||
|
||||
assert(last_capacity);
|
||||
@@ -450,7 +447,6 @@ int get_total_suspend_interval(Hashmap *last_capacity, usec_t *ret) {
|
||||
/* Return true if all batteries have acpi_btp support */
|
||||
int battery_trip_point_alarm_exists(void) {
|
||||
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
||||
sd_device *dev;
|
||||
int r;
|
||||
|
||||
r = battery_enumerator_new(&e);
|
||||
|
||||
@@ -3862,7 +3862,9 @@ int tpm2_make_pcr_json_array(uint32_t pcr_mask, JsonVariant **ret) {
|
||||
r = 0;
|
||||
|
||||
finish:
|
||||
json_variant_unref_many(pcr_array, n_pcrs);
|
||||
FOREACH_ARRAY(v, pcr_array, n_pcrs)
|
||||
json_variant_unref(*v);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -165,8 +165,8 @@ static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device,
|
||||
if (data->devlink) {
|
||||
const char *devlink;
|
||||
|
||||
FOREACH_DEVICE_DEVLINK(device, devlink)
|
||||
if (path_equal(devlink, data->devlink))
|
||||
FOREACH_DEVICE_DEVLINK(device, link)
|
||||
if (path_equal(link, data->devlink))
|
||||
goto found;
|
||||
|
||||
if (sd_device_get_devname(device, &devlink) >= 0 && path_equal(devlink, data->devlink))
|
||||
|
||||
Reference in New Issue
Block a user