You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger_console.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2023 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "logger.h"
  18. #include "libserver/cfg_file.h"
  19. #include "libcryptobox/cryptobox.h"
  20. #include "unix-std.h"
  21. #include "logger_private.h"
  22. #define CONSOLE_LOG_QUARK g_quark_from_static_string("console_logger")
  23. static const char lf_chr = '\n';
  24. struct rspamd_console_logger_priv {
  25. int fd;
  26. int crit_fd;
  27. };
  28. /* Copy & paste :( */
  29. static inline void
  30. log_time(double now, rspamd_logger_t *rspamd_log, char *timebuf,
  31. size_t len)
  32. {
  33. time_t sec = (time_t) now;
  34. gsize r;
  35. struct tm tms;
  36. rspamd_localtime(sec, &tms);
  37. r = strftime(timebuf, len, "%F %H:%M:%S", &tms);
  38. if (rspamd_log->flags & RSPAMD_LOG_FLAG_USEC) {
  39. char usec_buf[16];
  40. rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f",
  41. now - (double) sec);
  42. rspamd_snprintf(timebuf + r, len - r,
  43. "%s", usec_buf + 1);
  44. }
  45. }
  46. void *
  47. rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
  48. uid_t uid, gid_t gid, GError **err)
  49. {
  50. struct rspamd_console_logger_priv *priv;
  51. priv = g_malloc0(sizeof(*priv));
  52. if (logger->flags & RSPAMD_LOG_FLAG_RSPAMADM) {
  53. priv->fd = dup(STDOUT_FILENO);
  54. priv->crit_fd = dup(STDERR_FILENO);
  55. }
  56. else {
  57. priv->fd = dup(STDERR_FILENO);
  58. priv->crit_fd = priv->fd;
  59. }
  60. if (priv->fd == -1) {
  61. g_set_error(err, CONSOLE_LOG_QUARK, errno,
  62. "open_log: cannot dup console fd: %s\n",
  63. strerror(errno));
  64. rspamd_log_console_dtor(logger, priv);
  65. return NULL;
  66. }
  67. if (!isatty(priv->fd)) {
  68. if (logger->flags & RSPAMD_LOG_FLAG_COLOR) {
  69. /* Disable colors for not a tty */
  70. logger->flags &= ~RSPAMD_LOG_FLAG_COLOR;
  71. }
  72. }
  73. return priv;
  74. }
  75. void *
  76. rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
  77. gpointer arg, uid_t uid, gid_t gid, GError **err)
  78. {
  79. struct rspamd_console_logger_priv *npriv;
  80. npriv = rspamd_log_console_init(logger, cfg, uid, gid, err);
  81. if (npriv) {
  82. /* Close old */
  83. rspamd_log_console_dtor(logger, arg);
  84. }
  85. return npriv;
  86. }
  87. void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg)
  88. {
  89. struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *) arg;
  90. if (priv->fd != -1) {
  91. if (priv->fd != priv->crit_fd) {
  92. /* Two different FD case */
  93. if (close(priv->crit_fd) == -1) {
  94. rspamd_fprintf(stderr, "cannot close log crit_fd %d: %s\n",
  95. priv->crit_fd, strerror(errno));
  96. }
  97. }
  98. if (close(priv->fd) == -1) {
  99. rspamd_fprintf(stderr, "cannot close log fd %d: %s\n",
  100. priv->fd, strerror(errno));
  101. }
  102. /* Avoid the next if to be executed as crit_fd is equal to fd */
  103. priv->crit_fd = -1;
  104. }
  105. if (priv->crit_fd != -1) {
  106. if (close(priv->crit_fd) == -1) {
  107. rspamd_fprintf(stderr, "cannot close log crit_fd %d: %s\n",
  108. priv->crit_fd, strerror(errno));
  109. }
  110. }
  111. g_free(priv);
  112. }
  113. bool rspamd_log_console_log(const char *module, const char *id,
  114. const char *function,
  115. int level_flags,
  116. const char *message,
  117. gsize mlen,
  118. rspamd_logger_t *rspamd_log,
  119. gpointer arg)
  120. {
  121. struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *) arg;
  122. int fd, r;
  123. double now;
  124. if (level_flags & G_LOG_LEVEL_CRITICAL) {
  125. fd = priv->crit_fd;
  126. }
  127. else {
  128. /* Use stderr if we are in rspamadm mode and severity is more than WARNING */
  129. if ((rspamd_log->flags & RSPAMD_LOG_FLAG_RSPAMADM) && (level_flags & G_LOG_LEVEL_WARNING)) {
  130. fd = priv->crit_fd;
  131. }
  132. else {
  133. fd = priv->fd;
  134. }
  135. }
  136. #ifndef DISABLE_PTHREAD_MUTEX
  137. if (rspamd_log->mtx) {
  138. rspamd_mempool_lock_mutex(rspamd_log->mtx);
  139. }
  140. else {
  141. rspamd_file_lock(fd, FALSE);
  142. }
  143. #else
  144. rspamd_file_lock(fd, FALSE);
  145. #endif
  146. now = rspamd_get_calendar_ticks();
  147. struct rspamd_logger_iov_ctx iov_ctx;
  148. memset(&iov_ctx, 0, sizeof(iov_ctx));
  149. rspamd_log_fill_iov(&iov_ctx, now, module, id,
  150. function, level_flags, message,
  151. mlen, rspamd_log);
  152. again:
  153. r = writev(fd, iov_ctx.iov, iov_ctx.niov);
  154. if (r == -1) {
  155. if (errno == EAGAIN || errno == EINTR) {
  156. goto again;
  157. }
  158. if (rspamd_log->mtx) {
  159. rspamd_mempool_unlock_mutex(rspamd_log->mtx);
  160. }
  161. else {
  162. rspamd_file_unlock(fd, FALSE);
  163. }
  164. rspamd_log_iov_free(&iov_ctx);
  165. return false;
  166. }
  167. if (rspamd_log->mtx) {
  168. rspamd_mempool_unlock_mutex(rspamd_log->mtx);
  169. }
  170. else {
  171. rspamd_file_unlock(fd, FALSE);
  172. }
  173. rspamd_log_iov_free(&iov_ctx);
  174. return true;
  175. }