sd_sys/
journal.rs

1// sd-sys: FFI bindings to systemd for sd-id128 & sd-journal
2// Copyright (C) 2020 Christian Klaue [mail@ck76.de]
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16use super::id128::sd_id128;
17use libc::{c_char, c_int, c_void, iovec, size_t};
18
19/// FFI data type mapping for sd-journal as defined in libsystemd
20///
21/// This data type should rarely be used directly. Crate sd-journal defines a
22/// wrapper `Journal`.
23#[allow(non_camel_case_types)]
24#[repr(C)]
25pub struct sd_journal {
26    _unused: [u8; 0]
27}
28
29#[allow(clippy::identity_op)]
30pub const SD_JOURNAL_LOCAL_ONLY: c_int = 1 << 0;
31pub const SD_JOURNAL_RUNTIME_ONLY: c_int = 1 << 1;
32pub const SD_JOURNAL_SYSTEM: c_int = 1 << 2;
33pub const SD_JOURNAL_CURRENT_USER: c_int = 1 << 3;
34pub const SD_JOURNAL_OS_ROOT: c_int = 1 << 4;
35pub const SD_JOURNAL_ALL_NAMESPACES: c_int = 1 << 5;
36pub const SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE: c_int = 1 << 6;
37
38pub const LOG_EMERG: c_int = 0;
39pub const LOG_ALERT: c_int = 1;
40pub const LOG_CRIT: c_int = 2;
41pub const LOG_ERR: c_int = 3;
42pub const LOG_WARNING: c_int = 4;
43pub const LOG_NOTICE: c_int = 5;
44pub const LOG_INFO: c_int = 6;
45pub const LOG_DEBUG: c_int = 7;
46
47pub const SD_JOURNAL_NOP: c_int = 0;
48pub const SD_JOURNAL_APPEND: c_int = 1;
49pub const SD_JOURNAL_INVALIDATE: c_int = 2;
50
51extern "C" {
52    /// `int sd_journal_print(int priority, const char *format, …);`
53    ///
54    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_print.html#>
55    pub fn sd_journal_print(priority: c_int, message: *const c_char, ...) -> c_int;
56    /// `int sd_journal_sendv(const struct iovec *iov, int n);`
57    ///
58    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_print.html#>
59    pub fn sd_journal_sendv(entry: *const iovec, count: c_int) -> c_int;
60    // not implemented:
61    // int sd_journal_printv(int priority, const char *format, va_list ap);
62    // int sd_journal_send(const char *format, …);
63    // int sd_journal_perror(const char *message);
64    // int sd_journal_print_with_location(const char *file, const char *line,
65    //                 const char *func, int priority, const char *format, …);
66    // int sd_journal_printv_with_location(int priority, const char *file, const
67    //          char *line, const char *func, const char *format, va_list ap);
68    // int sd_journal_send_with_location(const char *file, const char *line, const
69    //                                  char *func, const char *format, …);
70    // int sd_journal_sendv_with_location(const char *file, const char *line,
71    //                     const char *func, const struct iovec *iov, int n);
72    // int sd_journal_perror_with_location(const char *file, const char *line, const
73    //                                  char *func, const char *message);
74
75    // <https://www.freedesktop.org/software/systemd/man/sd_journal_stream_fd.html#>
76    // not implemented:
77    // int sd_journal_stream_fd(const char *identifier, int priority, int
78    //                                  level_prefix);
79
80    /// `int sd_journal_open(sd_journal **ret, int flags);`
81    ///
82    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_open.html#>
83    pub fn sd_journal_open(journal: *mut *mut sd_journal, flags: c_int) -> c_int;
84    /// `int sd_journal_open_namespace(sd_journal **ret, const char *namespace,
85    ///                                 int flags);`
86    ///
87    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_open.html#>
88    pub fn sd_journal_open_namespace(journal: *mut *mut sd_journal,
89                                     namespace: *const c_char,
90                                     flags: c_int)
91                                     -> c_int;
92    /// `int sd_journal_open_directory(sd_journal **ret, const char *path,
93    ///                                 int flags);`
94    ///
95    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_open.html#>
96    pub fn sd_journal_open_directory(journal: *mut *mut sd_journal,
97                                     path: *const c_char,
98                                     flags: c_int)
99                                     -> c_int;
100    /// `int sd_journal_open_files(sd_journal **ret, const char **paths,
101    ///                                 int flags);`
102    ///
103    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_open.html#>
104    pub fn sd_journal_open_files(journal: *mut *mut sd_journal,
105                                 paths: *const *const c_char,
106                                 flags: c_int)
107                                 -> c_int;
108    /// `void sd_journal_close(sd_journal *j);`
109    ///
110    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_open.html#>
111    pub fn sd_journal_close(journal: *mut sd_journal);
112    // not implemented:
113    // int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags);
114    // int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds,
115    //                                  int flags);
116    /// `int sd_journal_next(sd_journal *j);`
117    ///
118    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_next.html#>
119    pub fn sd_journal_next(journal: *mut sd_journal) -> c_int;
120    /// `int sd_journal_previous(sd_journal *j);`
121    ///
122    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_next.html#>
123    pub fn sd_journal_previous(journal: *mut sd_journal) -> c_int;
124    /// `int sd_journal_next_skip(sd_journal *j, uint64_t skip);`
125    ///
126    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_next.html#>
127    pub fn sd_journal_next_skip(journal: *mut sd_journal, skip: u64) -> c_int;
128    /// `int sd_journal_previous_skip(sd_journal *j, uint64_t skip);`
129    ///
130    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_next.html#>
131    pub fn sd_journal_previous_skip(journal: *mut sd_journal, skip: u64) -> c_int;
132    /// `int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *usec);`
133    ///
134    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_realtime_usec.html#>
135    pub fn sd_journal_get_realtime_usec(journal: *mut sd_journal, usec: *mut u64) -> c_int;
136    /// `int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *usec,
137    ///                                 sd_id128_t *boot_id);`
138    ///
139    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_realtime_usec.html#>
140    pub fn sd_journal_get_monotonic_usec(journal: *mut sd_journal,
141                                         usec: *mut u64,
142                                         boot_id: *mut sd_id128)
143                                         -> c_int;
144    /// `int sd_journal_add_match(sd_journal *j, const void *data,
145    ///                                 size_t size);`
146    ///
147    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_add_match.html#>
148    pub fn sd_journal_add_match(journal: *mut sd_journal,
149                                data: *const c_void,
150                                len: size_t)
151                                -> c_int;
152    /// `int sd_journal_add_disjunction(sd_journal *j);`
153    ///
154    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_add_match.html#>
155    pub fn sd_journal_add_disjunction(journal: *mut sd_journal) -> c_int;
156    /// `int sd_journal_add_conjunction(sd_journal *j);`
157    ///
158    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_add_match.html#>
159    pub fn sd_journal_add_conjunction(journal: *mut sd_journal) -> c_int;
160    /// `void sd_journal_flush_matches(sd_journal *j);`
161    ///
162    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_add_match.html#>
163    pub fn sd_journal_flush_matches(journal: *mut sd_journal);
164    /// `int sd_journal_seek_head(sd_journal *j);`
165    ///
166    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_seek_head.html#>
167    pub fn sd_journal_seek_head(journal: *mut sd_journal) -> c_int;
168    /// `int sd_journal_seek_tail(sd_journal *j);`
169    ///
170    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_seek_head.html#>
171    pub fn sd_journal_seek_tail(journal: *mut sd_journal) -> c_int;
172    /// `int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id,
173    ///                                 uint64_t usec);`
174    ///
175    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_seek_head.html#>
176    pub fn sd_journal_seek_monotonic_usec(journal: *mut sd_journal,
177                                          boot_id: sd_id128,
178                                          usec: u64)
179                                          -> c_int;
180    /// `int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);`
181    ///
182    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_seek_head.html#>
183    pub fn sd_journal_seek_realtime_usec(journal: *mut sd_journal, usec: u64) -> c_int;
184    /// `int sd_journal_seek_cursor(sd_journal *j, const char *cursor);`
185    ///
186    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_seek_head.html#>
187    pub fn sd_journal_seek_cursor(journal: *mut sd_journal, cursor: *const c_char) -> c_int;
188    /// `int sd_journal_enumerate_fields(sd_journal *j, const char **field);`
189    ///
190    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_enumerate_fields.html#>
191    pub fn sd_journal_enumerate_fields(journal: *mut sd_journal,
192                                       fields: *mut *const c_char)
193                                       -> c_int;
194    /// `void sd_journal_restart_fields(sd_journal *j);`
195    ///
196    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_enumerate_fields.html#>
197    pub fn sd_journal_restart_fields(journal: *mut sd_journal);
198    /// `int sd_journal_get_cursor(sd_journal *j, char **cursor);`
199    ///
200    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_cursor.html#>
201    pub fn sd_journal_get_cursor(journal: *mut sd_journal, cursor: *mut *mut c_char) -> c_int;
202    /// `int sd_journal_test_cursor(sd_journal *j, const char *cursor);`
203    ///
204    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_cursor.html#>
205    pub fn sd_journal_test_cursor(journal: *mut sd_journal, cursor: *const c_char) -> c_int;
206    /// `int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
207    ///                                 uint64_t *to);`
208    ///
209    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_cutoff_realtime_usec.html#>
210    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_cutoff_monotonic_usec.html#>
211    pub fn sd_journal_get_cutoff_realtime_usec(journal: *mut sd_journal,
212                                               from: *mut u64,
213                                               to: *mut u64)
214                                               -> c_int;
215    /// `int sd_journal_get_cutoff_monotonic_usec(sd_journal *j,
216    ///                     sd_id128_t boot_id, uint64_t *from, uint64_t *to);`
217    ///
218    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_cutoff_realtime_usec.html#>
219    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_cutoff_monotonic_usec.html#>
220    pub fn sd_journal_get_cutoff_monotonic_usec(journal: *mut sd_journal,
221                                                boot_id: sd_id128,
222                                                from: *mut u64,
223                                                to: *mut u64)
224                                                -> c_int;
225    /// `int sd_journal_get_usage(sd_journal *j, uint64_t *bytes);`
226    ///
227    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_usage.html#>
228    pub fn sd_journal_get_usage(journal: *mut sd_journal, size: *mut u64) -> c_int;
229    /// `int sd_journal_get_catalog(sd_journal *j, char **ret);`
230    ///
231    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_catalog.html#>
232    pub fn sd_journal_get_catalog(journal: *mut sd_journal, catalog: *mut *mut c_char) -> c_int;
233    /// `int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret);`
234    ///
235    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_catalog.html#>
236    pub fn sd_journal_get_catalog_for_message_id(id: sd_id128, catalog: *mut *mut c_char) -> c_int;
237    /// `int sd_journal_get_fd(sd_journal *j);`
238    ///
239    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html#>
240    pub fn sd_journal_get_fd(journal: *mut sd_journal) -> c_int;
241    /// `int sd_journal_get_events(sd_journal *j);``
242    ///
243    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html#>
244    pub fn sd_journal_get_events(journal: *mut sd_journal) -> c_int;
245    /// `int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec);`
246    ///
247    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html#>
248    pub fn sd_journal_get_timeout(journal: *mut sd_journal, timeout: *mut u64) -> c_int;
249    /// `int sd_journal_process(sd_journal *j);`
250    ///
251    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html#>
252    pub fn sd_journal_process(journal: *mut sd_journal) -> c_int;
253    /// `int sd_journal_wait(sd_journal *j, uint64_t timeout_usec);`
254    ///
255    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html#>
256    pub fn sd_journal_wait(journal: *mut sd_journal, timeout: u64) -> c_int;
257    /// `int sd_journal_reliable_fd(sd_journal *j);`
258    ///
259    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_fd.html#>
260    pub fn sd_journal_reliable_fd(journal: *mut sd_journal) -> c_int;
261    /// `int sd_journal_has_runtime_files(sd_journal *j);`
262    ///
263    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_has_runtime_files.html#>
264    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_has_persistent_files.html#>
265    pub fn sd_journal_has_runtime_files(journal: *mut sd_journal) -> c_int;
266    /// `int sd_journal_has_persistent_files(sd_journal *j);`
267    ///
268    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_has_runtime_files.html#>
269    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_has_persistent_files.html#>
270    pub fn sd_journal_has_persistent_files(journal: *mut sd_journal) -> c_int;
271    /// `int sd_journal_get_data(sd_journal *j, const char *field,
272    ///                                 const void **data, size_t *length);`
273    ///
274    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_data.html#>
275    pub fn sd_journal_get_data(journal: *mut sd_journal,
276                               field: *const c_char,
277                               data: *mut *const c_void,
278                               length: *mut size_t)
279                               -> c_int;
280    /// `int sd_journal_enumerate_data(sd_journal *j, const void **data,
281    ///                                 size_t *length);`
282    ///
283    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_data.html#>
284    pub fn sd_journal_enumerate_data(journal: *mut sd_journal,
285                                     data: *mut *const c_void,
286                                     length: *mut size_t)
287                                     -> c_int;
288    /// `int sd_journal_enumerate_available_data(sd_journal *j,
289    ///                                 const void **data, size_t *length);`
290    ///
291    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_data.html#>
292    pub fn sd_journal_enumerate_available_data(journal: *mut sd_journal,
293                                               data: *mut *const c_void,
294                                               length: *mut size_t)
295                                               -> c_int;
296    /// `void sd_journal_restart_data(sd_journal *j);`
297    ///
298    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_data.html#>
299    pub fn sd_journal_restart_data(journal: *mut sd_journal);
300    /// `int sd_journal_set_data_threshold(sd_journal *j, size_t sz);`
301    ///
302    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_data.html#>
303    pub fn sd_journal_set_data_threshold(journal: *mut sd_journal, size: size_t) -> c_int;
304    /// `int sd_journal_get_data_threshold(sd_journal *j, size_t *sz);`
305    ///
306    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_get_data.html#>
307    pub fn sd_journal_get_data_threshold(journal: *mut sd_journal, size: *mut size_t) -> c_int;
308    /// `int sd_journal_query_unique(sd_journal *j, const char *field);`
309    ///
310    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_query_unique.html#>
311    pub fn sd_journal_query_unique(journal: *mut sd_journal, field: *const c_char) -> c_int;
312    /// `int sd_journal_enumerate_available_unique(sd_journal *j,
313    ///                                 const void **data, size_t *length);`
314    ///
315    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_query_unique.html#>
316    pub fn sd_journal_enumerate_available_unique(journal: *mut sd_journal,
317                                                 data: *mut *const c_void,
318                                                 length: *mut size_t)
319                                                 -> c_int;
320    /// `int sd_journal_enumerate_unique(sd_journal *j, const void **data,
321    ///                                 size_t *length);`
322    ///
323    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_query_unique.html#>
324    pub fn sd_journal_enumerate_unique(journal: *mut sd_journal,
325                                       data: *mut *const c_void,
326                                       length: *mut size_t)
327                                       -> c_int;
328    /// `void sd_journal_restart_unique(sd_journal *j);`
329    ///
330    /// <https://www.freedesktop.org/software/systemd/man/sd_journal_query_unique.html#>
331    pub fn sd_journal_restart_unique(journal: *mut sd_journal);
332}