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_restart_unique, SD_JOURNAL_FOREACH_UNIQUE - Read unique data
8       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_unique(sd_journal *j, const void **data,
16                                       size_t *length);
17
18       void sd_journal_restart_unique(sd_journal *j);
19
20       SD_JOURNAL_FOREACH_UNIQUE(sd_journal *j, const void *data,
21                                 size_t length);
22

DESCRIPTION

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

RETURN VALUE

65       sd_journal_query_unique() returns 0 on success or a negative
66       errno-style error code.  sd_journal_enumerate_unique() returns a
67       positive integer if the next field data has been read, 0 when no more
68       fields are known, or a negative errno-style error code.
69       sd_journal_restart_unique() returns nothing.
70

NOTES

72       All functions listed here are thread-agnostic and only a single
73       specific thread may operate on a given object during its entire
74       lifetime. It's safe to allocate multiple independent objects and use
75       each from a specific thread in parallel. However, it's not safe to
76       allocate such an object in one thread, and operate or free it from any
77       other, even if locking is used to ensure these threads don't operate on
78       it at the very same time.
79
80       These APIs are implemented as a shared library, which can be compiled
81       and linked to with the libsystemd pkg-config(1) file.
82

EXAMPLES

84       Use the SD_JOURNAL_FOREACH_UNIQUE macro to iterate through all values a
85       field of the journal can take. The following example lists all unit
86       names referenced in the journal:
87
88           #include <stdio.h>
89           #include <string.h>
90           #include <systemd/sd-journal.h>
91
92           int main(int argc, char *argv[]) {
93             sd_journal *j;
94             const void *d;
95             size_t l;
96             int r;
97
98             r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
99             if (r < 0) {
100               fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
101               return 1;
102             }
103             r = sd_journal_query_unique(j, "_SYSTEMD_UNIT");
104             if (r < 0) {
105               fprintf(stderr, "Failed to query journal: %s\n", strerror(-r));
106               return 1;
107             }
108             SD_JOURNAL_FOREACH_UNIQUE(j, d, l)
109               printf("%.*s\n", (int) l, (const char*) d);
110             sd_journal_close(j);
111             return 0;
112           }
113

SEE ALSO

115       systemd(1), systemd.journal-fields(7), sd-journal(3),
116       sd_journal_open(3), sd_journal_enumerate_fields(3),
117       sd_journal_get_data(3), sd_journal_add_match(3)
118
119
120
121systemd 243                                         SD_JOURNAL_QUERY_UNIQUE(3)
Impressum