1SD_JOURNAL_QUERY_UNIQUE(3) sd_journal_query_unique SD_JOURNAL_QUERY_UNIQUE(3)
2
3
4
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
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
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
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
72 All functions listed here are thread-agnostic and only a single thread
73 may operate on a given sd_journal object.
74
75 These APIs are implemented as a shared library, which can be compiled
76 and linked to with the libsystemd pkg-config(1) file.
77
79 Use the SD_JOURNAL_FOREACH_UNIQUE macro to iterate through all values a
80 field of the journal can take. The following example lists all unit
81 names referenced in the journal:
82
83 #include <stdio.h>
84 #include <string.h>
85 #include <systemd/sd-journal.h>
86
87 int main(int argc, char *argv[]) {
88 sd_journal *j;
89 const void *d;
90 size_t l;
91 int r;
92
93 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
94 if (r < 0) {
95 fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
96 return 1;
97 }
98 r = sd_journal_query_unique(j, "_SYSTEMD_UNIT");
99 if (r < 0) {
100 fprintf(stderr, "Failed to query journal: %s\n", strerror(-r));
101 return 1;
102 }
103 SD_JOURNAL_FOREACH_UNIQUE(j, d, l)
104 printf("%.*s\n", (int) l, (const char*) d);
105 sd_journal_close(j);
106 return 0;
107 }
108
110 systemd(1), systemd.journal-fields(7), sd-journal(3),
111 sd_journal_open(3), sd_journal_enumerate_fields(3),
112 sd_journal_get_data(3), sd_journal_add_match(3)
113
114
115
116systemd 239 SD_JOURNAL_QUERY_UNIQUE(3)