Skip to content

Commit eb86030

Browse files
committed
sd-journal: add an API to enumerate known field names of the journal
This adds two new calls to get the list of all journal fields names currently in use. This is the low-level support to implement the feature requested in systemd#2176 in a more optimized way.
1 parent b72190e commit eb86030

File tree

8 files changed

+390
-27
lines changed

8 files changed

+390
-27
lines changed

Makefile-man.am

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ MANPAGES += \
7272
man/sd_id128_to_string.3 \
7373
man/sd_is_fifo.3 \
7474
man/sd_journal_add_match.3 \
75+
man/sd_journal_enumerate_fields.3 \
7576
man/sd_journal_get_catalog.3 \
7677
man/sd_journal_get_cursor.3 \
7778
man/sd_journal_get_cutoff_realtime_usec.3 \
@@ -237,6 +238,7 @@ MANPAGES_ALIAS += \
237238
man/SD_JOURNAL_FOREACH.3 \
238239
man/SD_JOURNAL_FOREACH_BACKWARDS.3 \
239240
man/SD_JOURNAL_FOREACH_DATA.3 \
241+
man/SD_JOURNAL_FOREACH_FIELD.3 \
240242
man/SD_JOURNAL_FOREACH_UNIQUE.3 \
241243
man/SD_JOURNAL_INVALIDATE.3 \
242244
man/SD_JOURNAL_LOCAL_ONLY.3 \
@@ -397,6 +399,7 @@ MANPAGES_ALIAS += \
397399
man/sd_journal_process.3 \
398400
man/sd_journal_reliable_fd.3 \
399401
man/sd_journal_restart_data.3 \
402+
man/sd_journal_restart_fields.3 \
400403
man/sd_journal_restart_unique.3 \
401404
man/sd_journal_seek_cursor.3 \
402405
man/sd_journal_seek_monotonic_usec.3 \
@@ -565,6 +568,7 @@ man/SD_JOURNAL_CURRENT_USER.3: man/sd_journal_open.3
565568
man/SD_JOURNAL_FOREACH.3: man/sd_journal_next.3
566569
man/SD_JOURNAL_FOREACH_BACKWARDS.3: man/sd_journal_next.3
567570
man/SD_JOURNAL_FOREACH_DATA.3: man/sd_journal_get_data.3
571+
man/SD_JOURNAL_FOREACH_FIELD.3: man/sd_journal_enumerate_fields.3
568572
man/SD_JOURNAL_FOREACH_UNIQUE.3: man/sd_journal_query_unique.3
569573
man/SD_JOURNAL_INVALIDATE.3: man/sd_journal_get_fd.3
570574
man/SD_JOURNAL_LOCAL_ONLY.3: man/sd_journal_open.3
@@ -725,6 +729,7 @@ man/sd_journal_printv.3: man/sd_journal_print.3
725729
man/sd_journal_process.3: man/sd_journal_get_fd.3
726730
man/sd_journal_reliable_fd.3: man/sd_journal_get_fd.3
727731
man/sd_journal_restart_data.3: man/sd_journal_get_data.3
732+
man/sd_journal_restart_fields.3: man/sd_journal_enumerate_fields.3
728733
man/sd_journal_restart_unique.3: man/sd_journal_query_unique.3
729734
man/sd_journal_seek_cursor.3: man/sd_journal_seek_head.3
730735
man/sd_journal_seek_monotonic_usec.3: man/sd_journal_seek_head.3
@@ -1017,6 +1022,9 @@ man/SD_JOURNAL_FOREACH_BACKWARDS.html: man/sd_journal_next.html
10171022
man/SD_JOURNAL_FOREACH_DATA.html: man/sd_journal_get_data.html
10181023
$(html-alias)
10191024

1025+
man/SD_JOURNAL_FOREACH_FIELD.html: man/sd_journal_enumerate_fields.html
1026+
$(html-alias)
1027+
10201028
man/SD_JOURNAL_FOREACH_UNIQUE.html: man/sd_journal_query_unique.html
10211029
$(html-alias)
10221030

@@ -1497,6 +1505,9 @@ man/sd_journal_reliable_fd.html: man/sd_journal_get_fd.html
14971505
man/sd_journal_restart_data.html: man/sd_journal_get_data.html
14981506
$(html-alias)
14991507

1508+
man/sd_journal_restart_fields.html: man/sd_journal_enumerate_fields.html
1509+
$(html-alias)
1510+
15001511
man/sd_journal_restart_unique.html: man/sd_journal_query_unique.html
15011512
$(html-alias)
15021513

@@ -2534,6 +2545,7 @@ EXTRA_DIST += \
25342545
man/sd_id128_to_string.xml \
25352546
man/sd_is_fifo.xml \
25362547
man/sd_journal_add_match.xml \
2548+
man/sd_journal_enumerate_fields.xml \
25372549
man/sd_journal_get_catalog.xml \
25382550
man/sd_journal_get_cursor.xml \
25392551
man/sd_journal_get_cutoff_realtime_usec.xml \

man/sd-journal.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
<citerefentry><refentrytitle>sd_journal_get_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
7878
<citerefentry><refentrytitle>sd_journal_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
7979
<citerefentry><refentrytitle>sd_journal_seek_head</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
80+
<citerefentry><refentrytitle>sd_journal_query_enumerate</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
81+
<citerefentry><refentrytitle>sd_journal_enumerate_fields</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8082
<citerefentry><refentrytitle>sd_journal_get_cursor</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8183
<citerefentry><refentrytitle>sd_journal_get_cutoff_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8284
<citerefentry><refentrytitle>sd_journal_get_cutoff_monotonic_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
@@ -111,6 +113,8 @@
111113
<citerefentry><refentrytitle>sd_journal_get_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
112114
<citerefentry><refentrytitle>sd_journal_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
113115
<citerefentry><refentrytitle>sd_journal_seek_head</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
116+
<citerefentry><refentrytitle>sd_journal_query_enumerate</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
117+
<citerefentry><refentrytitle>sd_journal_enumerate_fields</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
114118
<citerefentry><refentrytitle>sd_journal_get_cursor</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
115119
<citerefentry><refentrytitle>sd_journal_get_cutoff_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
116120
<citerefentry><refentrytitle>sd_journal_get_cutoff_monotonic_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?xml version='1.0'?> <!--*-nxml-*-->
2+
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3+
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4+
5+
<!--
6+
This file is part of systemd.
7+
8+
Copyright 2016 Lennart Poettering
9+
10+
systemd is free software; you can redistribute it and/or modify it
11+
under the terms of the GNU Lesser General Public License as published by
12+
the Free Software Foundation; either version 2.1 of the License, or
13+
(at your option) any later version.
14+
15+
systemd is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public License
21+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
22+
-->
23+
24+
<refentry id="sd_journal_enumerate_fields">
25+
26+
<refentryinfo>
27+
<title>sd_journal_enumerate_fields</title>
28+
<productname>systemd</productname>
29+
30+
<authorgroup>
31+
<author>
32+
<contrib>Developer</contrib>
33+
<firstname>Lennart</firstname>
34+
<surname>Poettering</surname>
35+
<email>lennart@poettering.net</email>
36+
</author>
37+
</authorgroup>
38+
</refentryinfo>
39+
40+
<refmeta>
41+
<refentrytitle>sd_journal_enumerate_fields</refentrytitle>
42+
<manvolnum>3</manvolnum>
43+
</refmeta>
44+
45+
<refnamediv>
46+
<refname>sd_journal_enumerate_fields</refname>
47+
<refname>sd_journal_restart_fields</refname>
48+
<refname>SD_JOURNAL_FOREACH_FIELD</refname>
49+
<refpurpose>Read used field names from the journal</refpurpose>
50+
</refnamediv>
51+
52+
<refsynopsisdiv>
53+
<funcsynopsis>
54+
<funcsynopsisinfo>#include &lt;systemd/sd-journal.h&gt;</funcsynopsisinfo>
55+
56+
<funcprototype>
57+
<funcdef>int <function>sd_journal_enumerate_fields</function></funcdef>
58+
<paramdef>sd_journal *<parameter>j</parameter></paramdef>
59+
<paramdef>const char **<parameter>field</parameter></paramdef>
60+
</funcprototype>
61+
62+
<funcprototype>
63+
<funcdef>void <function>sd_journal_restart_fields</function></funcdef>
64+
<paramdef>sd_journal *<parameter>j</parameter></paramdef>
65+
</funcprototype>
66+
67+
<funcprototype>
68+
<funcdef><function>SD_JOURNAL_FOREACH_FIELD</function></funcdef>
69+
<paramdef>sd_journal *<parameter>j</parameter></paramdef>
70+
<paramdef>const char *<parameter>field</parameter></paramdef>
71+
</funcprototype>
72+
73+
</funcsynopsis>
74+
</refsynopsisdiv>
75+
76+
<refsect1>
77+
<title>Description</title>
78+
79+
<para><function>sd_journal_enumerate_fields()</function> may be used to iterate through all field names used in the
80+
opened journal files. On each invocation the next field name is returned. The order of the returned field names is
81+
not defined. It takes two arguments: the journal context object, plus a pointer to a constant string pointer where
82+
the field name is stored in. The returned data is in a read-only memory map and is only valid until the next
83+
invocation of <function>sd_journal_enumerate_fields()</function>. Note that this call is subject to the data field
84+
size threshold as controlled by <function>sd_journal_set_data_threshold()</function>.</para>
85+
86+
<para><function>sd_journal_restart_fields()</function> resets the field name enumeration index to the beginning of
87+
the list. The next invocation of <function>sd_journal_enumerate_fields()</function> will return the first field
88+
name again.</para>
89+
90+
<para>The <function>SD_JOURNAL_FOREACH_FIELD()</function> macro may be used as a handy wrapper around
91+
<function>sd_journal_restart_fields()</function> and <function>sd_journal_enumerate_fields()</function>.</para>
92+
93+
<para>These functions currently are not influenced by matches set with <function>sd_journal_add_match()</function>
94+
but this might change in a later version of this software.</para>
95+
96+
<para>To retrieve the possible values a specific field can take use
97+
<citerefentry><refentrytitle>sd_journal_query_unique</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
98+
</refsect1>
99+
100+
<refsect1>
101+
<title>Return Value</title>
102+
103+
<para><function>sd_journal_enumerate_fields()</function> returns a
104+
positive integer if the next field name has been read, 0 when no
105+
more field names are known, or a negative errno-style error code.
106+
<function>sd_journal_restart_fields()</function> returns
107+
nothing.</para>
108+
</refsect1>
109+
110+
<refsect1>
111+
<title>Notes</title>
112+
113+
<para>The <function>sd_journal_enumerate_fields()</function> and <function>sd_journal_restart_fields()</function>
114+
interfaces are available as a shared library, which can be compiled and linked to with the
115+
<constant>libsystemd</constant> <citerefentry
116+
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
117+
</refsect1>
118+
119+
<refsect1>
120+
<title>Examples</title>
121+
122+
<para>Use the <function>SD_JOURNAL_FOREACH_FIELD</function> macro to iterate through all field names in use in the
123+
current journal.</para>
124+
125+
<programlisting>#include &lt;stdio.h&gt;
126+
#include &lt;string.h&gt;
127+
#include &lt;systemd/sd-journal.h&gt;
128+
129+
int main(int argc, char *argv[]) {
130+
sd_journal *j;
131+
const char *field;
132+
int r;
133+
134+
r = sd_journal_open(&amp;j, SD_JOURNAL_LOCAL_ONLY);
135+
if (r &lt; 0) {
136+
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
137+
return 1;
138+
}
139+
SD_JOURNAL_FOREACH_FIELD(j, field)
140+
printf("%s\n", field);
141+
sd_journal_close(j);
142+
return 0;
143+
}</programlisting>
144+
145+
</refsect1>
146+
147+
<refsect1>
148+
<title>See Also</title>
149+
150+
<para>
151+
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
152+
<citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
153+
<citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
154+
<citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
155+
<citerefentry><refentrytitle>sd_journal_query_unique</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
156+
<citerefentry><refentrytitle>sd_journal_get_data</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
157+
<citerefentry><refentrytitle>sd_journal_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>
158+
</para>
159+
</refsect1>
160+
161+
</refentry>

man/sd_journal_query_unique.xml

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@
128128
<para>Note that these functions currently are not influenced by
129129
matches set with <function>sd_journal_add_match()</function> but
130130
this might change in a later version of this software.</para>
131+
132+
<para>To enumerate all field names currently in use (and thus all suitable field parameters for
133+
<function>sd_journal_query_unique()</function>), use the
134+
<citerefentry><refentrytitle>sd_journal_enumerate_fields</refentrytitle><manvolnum>3</manvolnum></citerefentry>
135+
call.</para>
131136
</refsect1>
132137

133138
<refsect1>
@@ -167,25 +172,25 @@
167172
#include &lt;systemd/sd-journal.h&gt;
168173

169174
int main(int argc, char *argv[]) {
170-
sd_journal *j;
171-
const void *d;
172-
size_t l;
173-
int r;
174-
175-
r = sd_journal_open(&amp;j, SD_JOURNAL_LOCAL_ONLY);
176-
if (r &lt; 0) {
177-
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
178-
return 1;
179-
}
180-
r = sd_journal_query_unique(j, "_SYSTEMD_UNIT");
181-
if (r &lt; 0) {
182-
fprintf(stderr, "Failed to query journal: %s\n", strerror(-r));
183-
return 1;
184-
}
185-
SD_JOURNAL_FOREACH_UNIQUE(j, d, l)
186-
printf("%.*s\n", (int) l, (const char*) d);
187-
sd_journal_close(j);
188-
return 0;
175+
sd_journal *j;
176+
const void *d;
177+
size_t l;
178+
int r;
179+
180+
r = sd_journal_open(&amp;j, SD_JOURNAL_LOCAL_ONLY);
181+
if (r &lt; 0) {
182+
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
183+
return 1;
184+
}
185+
r = sd_journal_query_unique(j, "_SYSTEMD_UNIT");
186+
if (r &lt; 0) {
187+
fprintf(stderr, "Failed to query journal: %s\n", strerror(-r));
188+
return 1;
189+
}
190+
SD_JOURNAL_FOREACH_UNIQUE(j, d, l)
191+
printf("%.*s\n", (int) l, (const char*) d);
192+
sd_journal_close(j);
193+
return 0;
189194
}</programlisting>
190195

191196
</refsect1>
@@ -198,6 +203,7 @@ int main(int argc, char *argv[]) {
198203
<citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
199204
<citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
200205
<citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
206+
<citerefentry><refentrytitle>sd_journal_enumerate_fields</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
201207
<citerefentry><refentrytitle>sd_journal_get_data</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
202208
<citerefentry><refentrytitle>sd_journal_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>
203209
</para>

src/journal/journal-internal.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,27 @@ struct sd_journal {
103103
unsigned current_invalidate_counter, last_invalidate_counter;
104104
usec_t last_process_usec;
105105

106+
/* Iterating through unique fields and their data values */
106107
char *unique_field;
107108
JournalFile *unique_file;
108109
uint64_t unique_offset;
109110

111+
/* Iterating through known fields */
112+
JournalFile *fields_file;
113+
uint64_t fields_offset;
114+
uint64_t fields_hash_table_index;
115+
char *fields_buffer;
116+
size_t fields_buffer_allocated;
117+
110118
int flags;
111119

112-
bool on_network;
113-
bool no_new_files;
114-
bool unique_file_lost; /* File we were iterating over got
115-
removed, and there were no more
116-
files, so sd_j_enumerate_unique
117-
will return a value equal to 0. */
120+
bool on_network:1;
121+
bool no_new_files:1;
122+
bool unique_file_lost:1; /* File we were iterating over got
123+
removed, and there were no more
124+
files, so sd_j_enumerate_unique
125+
will return a value equal to 0. */
126+
bool fields_file_lost:1;
118127
bool has_runtime_files:1;
119128
bool has_persistent_files:1;
120129

0 commit comments

Comments
 (0)