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, library or
91           module instance.
92
93       -EADDRNOTAVAIL
94           The read pointer is not positioned at a valid entry;
95           sd_journal_next(3) or a related call has not been called at least
96           once.
97
98       -ENOENT
99           The current entry does not include the specified field.
100
101       -ENOBUFS
102           A compressed entry is too large.
103
104       -E2BIG
105           The data field is too large for this computer architecture (e.g.
106           above 4 GB on a 32-bit architecture).
107
108       -EPROTONOSUPPORT
109           The journal is compressed with an unsupported method or the journal
110           uses an unsupported feature.
111
112       -EBADMSG
113           The journal is corrupted (possibly just the entry being iterated
114           over).
115
116       -EIO
117           An I/O error was reported by the kernel.
118

NOTES

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

EXAMPLES

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

SEE ALSO

169       systemd(1), systemd.journal-fields(7), sd-journal(3),
170       sd_journal_open(3), sd_journal_enumerate_fields(3),
171       sd_journal_get_data(3), sd_journal_add_match(3)
172
173
174
175systemd 254                                         SD_JOURNAL_QUERY_UNIQUE(3)
Impressum