]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: make sd_journal_get_data() output params optional
authornoxiouz <atiurin@proton.me>
Tue, 7 Apr 2026 15:29:51 +0000 (16:29 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Wed, 8 Apr 2026 14:33:20 +0000 (16:33 +0200)
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>
man/sd_journal_get_data.xml
src/libsystemd/sd-journal/sd-journal.c

index e3c8e0b5cd99e05e3c8d920b6466e6f8aea88db0..a902d76c73e3b6768a34f010e5a29d99fdc78002 100644 (file)
 
     <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>,
           <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>
index befa1945176f349046cd095144820b1118885960..3e5185b23f7e08fbe89791f63b96459aae1aa0ce 100644 (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;
         }