1SD_JOURNAL_QUERY_UNIQUE(3)  sd_journal_query_unique SD_JOURNAL_QUERY_UNIQUE(3)
2
3
4

NAME

6       sd_journal_query_unique, sd_journal_enumerate_unique,
7       sd_journal_enumerate_available_unique, sd_journal_restart_unique,
8       SD_JOURNAL_FOREACH_UNIQUE - Read unique data fields from the journal
9

SYNOPSIS

11       #include <systemd/sd-journal.h>
12
13       int sd_journal_query_unique(sd_journal *j, const char *field);
14
15       int sd_journal_enumerate_available_unique(sd_journal *j,
16                                                 const void **data,
17                                                 size_t *length);
18
19       int sd_journal_enumerate_unique(sd_journal *j, const void **data,
20                                       size_t *length);
21
22       void sd_journal_restart_unique(sd_journal *j);
23
24       SD_JOURNAL_FOREACH_UNIQUE(sd_journal *j, const void *data,
25                                 size_t length);
26

DESCRIPTION

28       sd_journal_query_unique() queries the journal for all unique values the
29       specified field can take. It takes two arguments: the journal to query
30       and the field name to look for. Well-known field names are listed on
31       systemd.journal-fields(7), but any field can be specified. Field names
32       must be specified without a trailing "=". After this function has been
33       executed successfully the field values may be queried using
34       sd_journal_enumerate_unique() and
35       sd_journal_enumerate_available_unique(). Invoking one of those calls
36       will change the field name being queried and reset the enumeration
37       index to the first field value that matches.
38
39       sd_journal_enumerate_unique() may be used to iterate through all data
40       fields which match the previously selected field name as set with
41       sd_journal_query_unique(). On each invocation the next field data
42       matching the field name is returned. The order of the returned data
43       fields is not defined. It takes three arguments: the journal object,
44       plus a pair of pointers to pointer/size variables where the data object
45       and its size shall be stored. The returned data is in a read-only
46       memory map and is only valid until the next invocation of
47       sd_journal_enumerate_unique(). Note that the data returned will be
48       prefixed with the field name and "=". Note that this call is subject to
49       the data field size threshold as controlled by
50       sd_journal_set_data_threshold() and only the initial part of the field
51       up to the threshold is returned. An error is returned for fields which
52       cannot be retrieved. See the error list below for details.
53
54       sd_journal_enumerate_available_unique() is similar to
55       sd_journal_enumerate_unique(), but silently skips any fields which may
56       be valid, but are too large or not supported by current implementation.
57
58       sd_journal_restart_unique() resets the data enumeration index to the
59       beginning of the list. The next invocation of
60       sd_journal_enumerate_unique() will return the first field data matching
61       the field name again.
62
63       Note that the SD_JOURNAL_FOREACH_UNIQUE() macro may be used as a handy
64       wrapper around sd_journal_restart_unique() and
65       sd_journal_enumerate_available_unique().
66
67       Note that these functions currently are not influenced by matches set
68       with sd_journal_add_match() but this might change in a later version of
69       this software.
70
71       To enumerate all field names currently in use (and thus all suitable
72       field parameters for sd_journal_query_unique()), use the
73       sd_journal_enumerate_fields(3) call.
74

RETURN VALUE

76       sd_journal_query_unique() returns 0 on success or a negative
77       errno-style error code.  sd_journal_enumerate_unique() and
78       sd_journal_query_available_unique() return a positive integer if the
79       next field data has been read, 0 when no more fields remain, or a
80       negative errno-style error code.  sd_journal_restart_unique() doesn't
81       return anything.
82
83   Errors
84       Returned errors may indicate the following problems:
85
86       -EINVAL
87           One of the required parameters is NULL or invalid.
88
89       -ECHILD
90           The journal object was created in a different process.
91
92       -EADDRNOTAVAIL
93           The read pointer is not positioned at a valid entry;
94           sd_journal_next(3) or a related call has not been called at least
95           once.
96
97       -ENOENT
98           The current entry does not include the specified field.
99
100       -ENOBUFS
101           A compressed entry is too large.
102
103       -E2BIG
104           The data field is too large for this computer architecture (e.g.
105           above 4 GB on a 32-bit architecture).
106
107       -EPROTONOSUPPORT
108           The journal is compressed with an unsupported method or the journal
109           uses an unsupported feature.
110
111       -EBADMSG
112           The journal is corrupted (possibly just the entry being iterated
113           over).
114
115       -EIO
116           An I/O error was reported by the kernel.
117

NOTES

119       All functions listed here are thread-agnostic and only a single
120       specific thread may operate on a given object during its entire
121       lifetime. It's safe to allocate multiple independent objects and use
122       each from a specific thread in parallel. However, it's not safe to
123       allocate such an object in one thread, and operate or free it from any
124       other, even if locking is used to ensure these threads don't operate on
125       it at the very same time.
126
127       These APIs are implemented as a shared library, which can be compiled
128       and linked to with the libsystemd pkg-config(1) file.
129

EXAMPLES

131       Use the SD_JOURNAL_FOREACH_UNIQUE() macro to iterate through all values
132       a field of the journal can take (and which can be accessed on the given
133       architecture and are not compressed with an unsupported mechanism). The
134       following example lists all unit names referenced in the journal:
135
136           /* SPDX-License-Identifier: CC0-1.0 */
137
138           #include <stdio.h>
139           #include <string.h>
140           #include <systemd/sd-journal.h>
141
142           int main(int argc, char *argv[]) {
143             sd_journal *j;
144             const void *d;
145             size_t l;
146             int r;
147
148             r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
149             if (r < 0) {
150               fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
151               return 1;
152             }
153             r = sd_journal_query_unique(j, "_SYSTEMD_UNIT");
154             if (r < 0) {
155               fprintf(stderr, "Failed to query journal: %s\n", strerror(-r));
156               return 1;
157             }
158             SD_JOURNAL_FOREACH_UNIQUE(j, d, l)
159               printf("%.*s\n", (int) l, (const char*) d);
160             sd_journal_close(j);
161             return 0;
162           }
163

SEE ALSO

165       systemd(1), systemd.journal-fields(7), sd-journal(3),
166       sd_journal_open(3), sd_journal_enumerate_fields(3),
167       sd_journal_get_data(3), sd_journal_add_match(3)
168
169
170
171systemd 251                                         SD_JOURNAL_QUERY_UNIQUE(3)
Impressum