vici_logger.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2014 Martin Willi
  3. * Copyright (C) 2014 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "vici_logger.h"
  16. #include "vici_builder.h"
  17. #include <daemon.h>
  18. #include <threading/mutex.h>
  19. #include <processing/jobs/callback_job.h>
  20. typedef struct private_vici_logger_t private_vici_logger_t;
  21. /**
  22. * Private data of an vici_logger_t object.
  23. */
  24. struct private_vici_logger_t {
  25. /**
  26. * Public vici_logger_t interface.
  27. */
  28. vici_logger_t public;
  29. /**
  30. * Dispatcher
  31. */
  32. vici_dispatcher_t *dispatcher;
  33. /**
  34. * Recursiveness avoidance counter
  35. */
  36. int recursive;
  37. /**
  38. * List of messages to raise async events
  39. */
  40. linked_list_t *queue;
  41. /**
  42. * Mutex to synchronize logging
  43. */
  44. mutex_t *mutex;
  45. };
  46. /**
  47. * Async callback to raise events for queued messages
  48. */
  49. static job_requeue_t raise_events(private_vici_logger_t *this)
  50. {
  51. vici_message_t *message;
  52. u_int count;
  53. this->mutex->lock(this->mutex);
  54. count = this->queue->get_count(this->queue);
  55. this->queue->remove_first(this->queue, (void**)&message);
  56. this->mutex->unlock(this->mutex);
  57. if (count > 0)
  58. {
  59. this->dispatcher->raise_event(this->dispatcher, "log", 0, message);
  60. }
  61. if (count > 1)
  62. {
  63. return JOB_REQUEUE_DIRECT;
  64. }
  65. return JOB_REQUEUE_NONE;
  66. }
  67. /**
  68. * Queue a message for async processing
  69. */
  70. static void queue_messsage(private_vici_logger_t *this, vici_message_t *message)
  71. {
  72. this->queue->insert_last(this->queue, message);
  73. if (this->queue->get_count(this->queue) == 1)
  74. {
  75. lib->processor->queue_job(lib->processor, (job_t*)
  76. callback_job_create((callback_job_cb_t)raise_events,
  77. this, NULL, NULL));
  78. }
  79. }
  80. METHOD(logger_t, log_, void,
  81. private_vici_logger_t *this, debug_t group, level_t level, int thread,
  82. ike_sa_t* ike_sa, const char *msg)
  83. {
  84. if (!this->dispatcher->has_event_listeners(this->dispatcher, "log"))
  85. {
  86. return;
  87. }
  88. this->mutex->lock(this->mutex);
  89. /* avoid recursive invocations by the vici subsystem */
  90. if (this->recursive++ == 0)
  91. {
  92. vici_message_t *message;
  93. vici_builder_t *builder;
  94. builder = vici_builder_create();
  95. builder->add_kv(builder, "group", "%N", debug_names, group);
  96. builder->add_kv(builder, "level", "%d", level);
  97. builder->add_kv(builder, "thread", "%d", thread);
  98. if (ike_sa)
  99. {
  100. builder->add_kv(builder, "ikesa-name", "%s",
  101. ike_sa->get_name(ike_sa));
  102. builder->add_kv(builder, "ikesa-uniqueid", "%u",
  103. ike_sa->get_unique_id(ike_sa));
  104. }
  105. builder->add_kv(builder, "msg", "%s", msg);
  106. message = builder->finalize(builder);
  107. if (message)
  108. {
  109. queue_messsage(this, message);
  110. }
  111. }
  112. this->recursive--;
  113. this->mutex->unlock(this->mutex);
  114. }
  115. METHOD(logger_t, get_level, level_t,
  116. private_vici_logger_t *this, debug_t group)
  117. {
  118. /* anything higher might produce a loop as sending messages or listening
  119. * for clients might cause log messages itself */
  120. return LEVEL_CTRL;
  121. }
  122. /**
  123. * (Un-)register dispatcher functions/events
  124. */
  125. static void manage_commands(private_vici_logger_t *this, bool reg)
  126. {
  127. this->dispatcher->manage_event(this->dispatcher, "log", reg);
  128. }
  129. METHOD(vici_logger_t, destroy, void,
  130. private_vici_logger_t *this)
  131. {
  132. manage_commands(this, FALSE);
  133. this->queue->destroy_offset(this->queue, offsetof(vici_message_t, destroy));
  134. this->mutex->destroy(this->mutex);
  135. free(this);
  136. }
  137. /**
  138. * See header
  139. */
  140. vici_logger_t *vici_logger_create(vici_dispatcher_t *dispatcher)
  141. {
  142. private_vici_logger_t *this;
  143. INIT(this,
  144. .public = {
  145. .logger = {
  146. .log = _log_,
  147. .get_level = _get_level,
  148. },
  149. .destroy = _destroy,
  150. },
  151. .dispatcher = dispatcher,
  152. .queue = linked_list_create(),
  153. .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
  154. );
  155. manage_commands(this, TRUE);
  156. return &this->public;
  157. }