master
c 50 lines 1.76 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "log2journal.h"
4
5 void injection_cleanup(INJECTION *inj) {
6 hashed_key_cleanup(&inj->key);
7 replace_pattern_cleanup(&inj->value);
8 }
9
10 static inline bool log_job_injection_replace(INJECTION *inj, const char *key, size_t key_len, const char *value, size_t value_len) {
11 if(key_len > JOURNAL_MAX_KEY_LEN)
12 l2j_log("WARNING: injection key '%.*s' is too long for journal. Will be truncated.", (int)key_len, key);
13
14 if(value_len > JOURNAL_MAX_VALUE_LEN)
15 l2j_log(
16 "WARNING: injection value of key '%.*s' is too long for journal. Will be truncated.", (int)key_len, key);
17
18 hashed_key_set(&inj->key, key, key_len);
19 char *v = strndupz(value, value_len);
20 bool ret = replace_pattern_set(&inj->value, v);
21 freez(v);
22
23 return ret;
24 }
25
26 bool log_job_injection_add(LOG_JOB *jb, const char *key, size_t key_len, const char *value, size_t value_len, bool unmatched) {
27 if (unmatched) {
28 if (jb->unmatched.injections.used >= MAX_INJECTIONS) {
29 l2j_log("Error: too many unmatched injections. You can inject up to %d lines.", MAX_INJECTIONS);
30 return false;
31 }
32 }
33 else {
34 if (jb->injections.used >= MAX_INJECTIONS) {
35 l2j_log("Error: too many injections. You can inject up to %d lines.", MAX_INJECTIONS);
36 return false;
37 }
38 }
39
40 bool ret;
41 if (unmatched) {
42 ret = log_job_injection_replace(&jb->unmatched.injections.keys[jb->unmatched.injections.used++],
43 key, key_len, value, value_len);
44 } else {
45 ret = log_job_injection_replace(&jb->injections.keys[jb->injections.used++],
46 key, key_len, value, value_len);
47 }
48
49 return ret;
50 }