log2journal-inject.c 1.8 KB

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