sd-journal: make sd_journal_get_data() output params optional

Allow callers to pass NULL for ret_data and/or ret_size when they only
need to check whether a field exists.  Initialize provided output
pointers to safe defaults and update the manual page accordingly.

Propagate the NULL-ness through to journal_file_data_payload() so that
downstream helpers can optimize for the existence-check case.

Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
noxiouz
2026-04-08 16:33:20 +02:00
committed by Zbigniew Jędrzejewski-Szmek
parent f8e88be1d1
commit 329233dcbf
2 changed files with 14 additions and 7 deletions
+8 -2
View File
@@ -83,8 +83,10 @@
<para><function>sd_journal_get_data()</function> gets the data object associated with a specific field
from the current journal entry. It takes four arguments: the journal context object, a string with the
field name to request, plus a pair of pointers to pointer/size variables where the data object and its
size shall be stored in. The field name should be an entry field name. Well-known field names are listed in
field name to request, plus a pair of optional pointers to pointer/size variables where the data object and
its size shall be stored in. Either pointer may be <constant>NULL</constant>, in which case the corresponding
value is not returned (this is supported since version 261). The field name should be an entry field name.
Well-known field names are listed in
<citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
but any field can be specified. The returned data is in a read-only memory map and is only valid until
the next invocation of <function>sd_journal_get_data()</function>,
@@ -162,6 +164,10 @@
<term><constant>-EINVAL</constant></term>
<listitem><para>One of the required parameters is <constant>NULL</constant> or invalid.
For <function>sd_journal_get_data()</function>, only the journal context object and field name
are required non-<constant>NULL</constant>. For <function>sd_journal_enumerate_data()</function>
and <function>sd_journal_enumerate_available_data()</function>,
<parameter>ret_data</parameter> and <parameter>ret_size</parameter> are required as well.
</para>
<xi:include href="version-info.xml" xpointer="v246"/></listitem>
+6 -5
View File
@@ -2822,8 +2822,6 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(field, -EINVAL);
assert_return(ret_data, -EINVAL);
assert_return(ret_size, -EINVAL);
assert_return(field_is_valid(field), -EINVAL);
f = j->current_file;
@@ -2846,7 +2844,8 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
size_t l;
p = journal_file_entry_item_object_offset(f, o, i);
r = journal_file_data_payload(f, NULL, p, field, field_length, j->data_threshold, &d, &l);
r = journal_file_data_payload(f, NULL, p, field, field_length, j->data_threshold,
ret_data ? &d : NULL, ret_size ? &l : NULL);
if (r == 0)
continue;
if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
@@ -2856,8 +2855,10 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
if (r < 0)
return r;
*ret_data = d;
*ret_size = l;
if (ret_data)
*ret_data = d;
if (ret_size)
*ret_size = l;
return 0;
}