1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <inttypes.h>
5 #include <stdbool.h>
6 #include <sys/types.h>
7 
8 #include "sd-id128.h"
9 #include "sd-journal.h"
10 
11 #include "hashmap.h"
12 #include "journal-def.h"
13 #include "journal-file.h"
14 #include "list.h"
15 #include "set.h"
16 
17 typedef struct Match Match;
18 typedef struct Location Location;
19 typedef struct Directory Directory;
20 
21 typedef enum MatchType {
22         MATCH_DISCRETE,
23         MATCH_OR_TERM,
24         MATCH_AND_TERM
25 } MatchType;
26 
27 struct Match {
28         MatchType type;
29         Match *parent;
30         LIST_FIELDS(Match, matches);
31 
32         /* For concrete matches */
33         char *data;
34         size_t size;
35         uint64_t hash; /* old-style jenkins hash. New-style siphash is different per file, hence won't be cached here */
36 
37         /* For terms */
38         LIST_HEAD(Match, matches);
39 };
40 
41 struct Location {
42         LocationType type;
43 
44         bool seqnum_set:1;
45         bool realtime_set:1;
46         bool monotonic_set:1;
47         bool xor_hash_set:1;
48 
49         uint64_t seqnum;
50         sd_id128_t seqnum_id;
51 
52         uint64_t realtime;
53 
54         uint64_t monotonic;
55         sd_id128_t boot_id;
56 
57         uint64_t xor_hash;
58 };
59 
60 struct Directory {
61         char *path;
62         int wd;
63         bool is_root;
64         unsigned last_seen_generation;
65 };
66 
67 struct sd_journal {
68         int toplevel_fd;
69 
70         char *path;
71         char *prefix;
72         char *namespace;
73 
74         OrderedHashmap *files;
75         IteratedCache *files_cache;
76         MMapCache *mmap;
77 
78         Location current_location;
79 
80         JournalFile *current_file;
81         uint64_t current_field;
82 
83         Match *level0, *level1, *level2;
84 
85         pid_t original_pid;
86 
87         int inotify_fd;
88         unsigned current_invalidate_counter, last_invalidate_counter;
89         usec_t last_process_usec;
90         unsigned generation;
91 
92         /* Iterating through unique fields and their data values */
93         char *unique_field;
94         JournalFile *unique_file;
95         uint64_t unique_offset;
96 
97         /* Iterating through known fields */
98         JournalFile *fields_file;
99         uint64_t fields_offset;
100         uint64_t fields_hash_table_index;
101         char *fields_buffer;
102 
103         int flags;
104 
105         bool on_network:1;
106         bool no_new_files:1;
107         bool no_inotify:1;
108         bool unique_file_lost:1; /* File we were iterating over got
109                                     removed, and there were no more
110                                     files, so sd_j_enumerate_unique
111                                     will return a value equal to 0. */
112         bool fields_file_lost:1;
113         bool has_runtime_files:1;
114         bool has_persistent_files:1;
115 
116         size_t data_threshold;
117 
118         Hashmap *directories_by_path;
119         Hashmap *directories_by_wd;
120 
121         Hashmap *errors;
122 };
123 
124 char *journal_make_match_string(sd_journal *j);
125 void journal_print_header(sd_journal *j);
126 
127 #define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval)                     \
128         for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; )
129 
130 /* All errors that we might encounter while extracting a field that are not real errors,
131  * but only mean that the field is too large or we don't support the compression. */
JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(int r)132 static inline bool JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(int r) {
133         return IN_SET(abs(r),
134                       ENOBUFS,          /* Field or decompressed field too large */
135                       E2BIG,            /* Field too large for pointer width */
136                       EPROTONOSUPPORT); /* Unsupported compression */
137 }
138