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_file.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 FILE_LOG_QUARK g_quark_from_static_string("file_logger")
  23. struct rspamd_file_logger_priv {
  24. gint fd;
  25. struct {
  26. uint32_t size;
  27. uint32_t used;
  28. u_char *buf;
  29. } io_buf;
  30. gboolean throttling;
  31. gchar *log_file;
  32. gboolean is_buffered;
  33. gboolean log_severity;
  34. time_t throttling_time;
  35. uint32_t repeats;
  36. uint64_t last_line_cksum;
  37. gchar *saved_message;
  38. gsize saved_mlen;
  39. gchar *saved_function;
  40. gchar *saved_module;
  41. gchar *saved_id;
  42. guint saved_loglevel;
  43. };
  44. /**
  45. * Calculate checksum for log line (used for repeating logic)
  46. */
  47. static inline uint64_t
  48. rspamd_log_calculate_cksum(const gchar *message, size_t mlen)
  49. {
  50. return rspamd_cryptobox_fast_hash(message, mlen, rspamd_hash_seed());
  51. }
  52. /*
  53. * Write a line to log file (unbuffered)
  54. */
  55. static bool
  56. direct_write_log_line(rspamd_logger_t *rspamd_log,
  57. struct rspamd_file_logger_priv *priv,
  58. void *data,
  59. gsize count,
  60. gboolean is_iov,
  61. gint level_flags)
  62. {
  63. struct iovec *iov;
  64. const gchar *line;
  65. glong r;
  66. gint fd;
  67. gboolean locked = FALSE;
  68. iov = (struct iovec *) data;
  69. fd = priv->fd;
  70. if (!rspamd_log->no_lock) {
  71. gsize tlen;
  72. if (is_iov) {
  73. tlen = 0;
  74. for (guint i = 0; i < count; i++) {
  75. tlen += iov[i].iov_len;
  76. }
  77. }
  78. else {
  79. tlen = count;
  80. }
  81. if (tlen > PIPE_BUF) {
  82. locked = TRUE;
  83. #ifndef DISABLE_PTHREAD_MUTEX
  84. if (rspamd_log->mtx) {
  85. rspamd_mempool_lock_mutex(rspamd_log->mtx);
  86. }
  87. else {
  88. rspamd_file_lock(fd, FALSE);
  89. }
  90. #else
  91. rspamd_file_lock(fd, FALSE);
  92. #endif
  93. }
  94. }
  95. if (is_iov) {
  96. r = writev(fd, iov, count);
  97. }
  98. else {
  99. line = (const gchar *) data;
  100. r = write(fd, line, count);
  101. }
  102. if (locked) {
  103. #ifndef DISABLE_PTHREAD_MUTEX
  104. if (rspamd_log->mtx) {
  105. rspamd_mempool_unlock_mutex(rspamd_log->mtx);
  106. }
  107. else {
  108. rspamd_file_unlock(fd, FALSE);
  109. }
  110. #else
  111. rspamd_file_unlock(fd, FALSE);
  112. #endif
  113. }
  114. if (r == -1) {
  115. /* We cannot write message to file, so we need to detect error and make decision */
  116. if (errno == EINTR) {
  117. /* Try again */
  118. return direct_write_log_line(rspamd_log, priv, data, count, is_iov, level_flags);
  119. }
  120. if (errno == EFAULT || errno == EINVAL || errno == EFBIG ||
  121. errno == ENOSPC) {
  122. /* Rare case */
  123. priv->throttling = TRUE;
  124. priv->throttling_time = time(NULL);
  125. }
  126. else if (errno == EPIPE || errno == EBADF) {
  127. /* We write to some pipe and it disappears, disable logging or we has opened bad file descriptor */
  128. rspamd_log->enabled = FALSE;
  129. }
  130. return false;
  131. }
  132. else if (priv->throttling) {
  133. priv->throttling = FALSE;
  134. }
  135. return true;
  136. }
  137. /**
  138. * Fill buffer with message (limits must be checked BEFORE this call)
  139. */
  140. static void
  141. fill_buffer(rspamd_logger_t *rspamd_log,
  142. struct rspamd_file_logger_priv *priv,
  143. const struct iovec *iov, gint iovcnt)
  144. {
  145. gint i;
  146. for (i = 0; i < iovcnt; i++) {
  147. memcpy(priv->io_buf.buf + priv->io_buf.used,
  148. iov[i].iov_base,
  149. iov[i].iov_len);
  150. priv->io_buf.used += iov[i].iov_len;
  151. }
  152. }
  153. static void
  154. rspamd_log_flush(rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *priv)
  155. {
  156. if (priv->is_buffered) {
  157. direct_write_log_line(rspamd_log,
  158. priv,
  159. priv->io_buf.buf,
  160. priv->io_buf.used,
  161. FALSE,
  162. rspamd_log->log_level);
  163. priv->io_buf.used = 0;
  164. }
  165. }
  166. /*
  167. * Write message to buffer or to file (using direct_write_log_line function)
  168. */
  169. static bool
  170. file_log_helper(rspamd_logger_t *rspamd_log,
  171. struct rspamd_file_logger_priv *priv,
  172. const struct iovec *iov,
  173. guint iovcnt,
  174. gint level_flags)
  175. {
  176. size_t len = 0;
  177. guint i;
  178. if (!priv->is_buffered) {
  179. /* Write string directly */
  180. return direct_write_log_line(rspamd_log, priv, (void *) iov, iovcnt,
  181. TRUE, level_flags);
  182. }
  183. else {
  184. /* Calculate total length */
  185. for (i = 0; i < iovcnt; i++) {
  186. len += iov[i].iov_len;
  187. }
  188. /* Fill buffer */
  189. if (priv->io_buf.size < len) {
  190. /* Buffer is too small to hold this string, so write it directly */
  191. rspamd_log_flush(rspamd_log, priv);
  192. return direct_write_log_line(rspamd_log, priv, (void *) iov, iovcnt,
  193. TRUE, level_flags);
  194. }
  195. else if (priv->io_buf.used + len >= priv->io_buf.size) {
  196. /* Buffer is full, try to write it directly */
  197. rspamd_log_flush(rspamd_log, priv);
  198. fill_buffer(rspamd_log, priv, iov, iovcnt);
  199. }
  200. else {
  201. /* Copy incoming string to buffer */
  202. fill_buffer(rspamd_log, priv, iov, iovcnt);
  203. }
  204. }
  205. return true;
  206. }
  207. static void
  208. rspamd_log_reset_repeated(rspamd_logger_t *rspamd_log,
  209. struct rspamd_file_logger_priv *priv)
  210. {
  211. gchar tmpbuf[256];
  212. gssize r;
  213. if (priv->repeats > REPEATS_MIN) {
  214. r = rspamd_snprintf(tmpbuf,
  215. sizeof(tmpbuf),
  216. "Last message repeated %ud times",
  217. priv->repeats - REPEATS_MIN);
  218. priv->repeats = 0;
  219. if (priv->saved_message) {
  220. rspamd_log_file_log(priv->saved_module,
  221. priv->saved_id,
  222. priv->saved_function,
  223. priv->saved_loglevel | RSPAMD_LOG_FORCED,
  224. priv->saved_message,
  225. priv->saved_mlen,
  226. rspamd_log,
  227. priv);
  228. g_free(priv->saved_message);
  229. g_free(priv->saved_function);
  230. g_free(priv->saved_module);
  231. g_free(priv->saved_id);
  232. priv->saved_message = NULL;
  233. priv->saved_function = NULL;
  234. priv->saved_module = NULL;
  235. priv->saved_id = NULL;
  236. }
  237. /* It is safe to use temporary buffer here as it is not static */
  238. rspamd_log_file_log(NULL, NULL,
  239. G_STRFUNC,
  240. priv->saved_loglevel | RSPAMD_LOG_FORCED,
  241. tmpbuf,
  242. r,
  243. rspamd_log,
  244. priv);
  245. rspamd_log_flush(rspamd_log, priv);
  246. }
  247. }
  248. static gint
  249. rspamd_try_open_log_fd(rspamd_logger_t *rspamd_log,
  250. struct rspamd_file_logger_priv *priv,
  251. uid_t uid, gid_t gid,
  252. GError **err)
  253. {
  254. gint fd;
  255. fd = open(priv->log_file,
  256. O_CREAT | O_WRONLY | O_APPEND,
  257. S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
  258. if (fd == -1) {
  259. g_set_error(err, FILE_LOG_QUARK, errno,
  260. "open_log: cannot open desired log file: %s, %s\n",
  261. priv->log_file, strerror(errno));
  262. return -1;
  263. }
  264. if (uid != -1 || gid != -1) {
  265. if (fchown(fd, uid, gid) == -1) {
  266. g_set_error(err, FILE_LOG_QUARK, errno,
  267. "open_log: cannot chown desired log file: %s, %s\n",
  268. priv->log_file, strerror(errno));
  269. close(fd);
  270. return -1;
  271. }
  272. }
  273. return fd;
  274. }
  275. void *
  276. rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
  277. uid_t uid, gid_t gid, GError **err)
  278. {
  279. struct rspamd_file_logger_priv *priv;
  280. if (!cfg || !cfg->cfg_name) {
  281. g_set_error(err, FILE_LOG_QUARK, EINVAL,
  282. "no log file specified");
  283. return NULL;
  284. }
  285. priv = g_malloc0(sizeof(*priv));
  286. if (cfg->log_buffered) {
  287. if (cfg->log_buf_size != 0) {
  288. priv->io_buf.size = cfg->log_buf_size;
  289. }
  290. else {
  291. priv->io_buf.size = LOGBUF_LEN;
  292. }
  293. priv->is_buffered = TRUE;
  294. priv->io_buf.buf = g_malloc(priv->io_buf.size);
  295. }
  296. if (cfg->log_file) {
  297. priv->log_file = g_strdup(cfg->log_file);
  298. }
  299. priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY);
  300. priv->fd = rspamd_try_open_log_fd(logger, priv, uid, gid, err);
  301. if (priv->fd == -1) {
  302. rspamd_log_file_dtor(logger, priv);
  303. return NULL;
  304. }
  305. return priv;
  306. }
  307. void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg)
  308. {
  309. struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg;
  310. rspamd_log_reset_repeated(logger, priv);
  311. rspamd_log_flush(logger, priv);
  312. if (priv->fd != -1) {
  313. if (close(priv->fd) == -1) {
  314. rspamd_fprintf(stderr, "cannot close log fd %d: %s; log file = %s\n",
  315. priv->fd, strerror(errno), priv->log_file);
  316. }
  317. }
  318. g_free(priv->log_file);
  319. g_free(priv);
  320. }
  321. bool rspamd_log_file_log(const gchar *module, const gchar *id,
  322. const gchar *function,
  323. gint level_flags,
  324. const gchar *message,
  325. gsize mlen,
  326. rspamd_logger_t *rspamd_log,
  327. gpointer arg)
  328. {
  329. struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg;
  330. gdouble now;
  331. uint64_t cksum;
  332. gboolean got_time = FALSE;
  333. if (!(level_flags & RSPAMD_LOG_FORCED) && !rspamd_log->enabled) {
  334. return false;
  335. }
  336. /* Check throttling due to write errors */
  337. if (!(level_flags & RSPAMD_LOG_FORCED) && priv->throttling) {
  338. now = rspamd_get_calendar_ticks();
  339. if (priv->throttling_time != now) {
  340. priv->throttling_time = now;
  341. got_time = TRUE;
  342. }
  343. else {
  344. /* Do not try to write to file too often while throttling */
  345. return false;
  346. }
  347. }
  348. /* Check repeats */
  349. cksum = rspamd_log_calculate_cksum(message, mlen);
  350. if (cksum == priv->last_line_cksum) {
  351. priv->repeats++;
  352. if (priv->repeats > REPEATS_MIN && priv->repeats <
  353. REPEATS_MAX) {
  354. /* Do not log anything but save message for future */
  355. if (priv->saved_message == NULL) {
  356. priv->saved_function = g_strdup(function);
  357. priv->saved_mlen = mlen;
  358. priv->saved_message = g_malloc(mlen);
  359. memcpy(priv->saved_message, message, mlen);
  360. if (module) {
  361. priv->saved_module = g_strdup(module);
  362. }
  363. if (id) {
  364. priv->saved_id = g_strdup(id);
  365. }
  366. priv->saved_loglevel = level_flags;
  367. }
  368. return true;
  369. }
  370. else if (priv->repeats > REPEATS_MAX) {
  371. rspamd_log_reset_repeated(rspamd_log, priv);
  372. bool ret = rspamd_log_file_log(module, id,
  373. function,
  374. level_flags,
  375. message,
  376. mlen,
  377. rspamd_log,
  378. priv);
  379. /* Probably we have more repeats in future */
  380. priv->repeats = REPEATS_MIN + 1;
  381. return ret;
  382. }
  383. }
  384. else {
  385. /* Reset counter if new message differs from saved message */
  386. priv->last_line_cksum = cksum;
  387. if (priv->repeats > REPEATS_MIN) {
  388. rspamd_log_reset_repeated(rspamd_log, priv);
  389. return rspamd_log_file_log(module, id,
  390. function,
  391. level_flags,
  392. message,
  393. mlen,
  394. rspamd_log,
  395. arg);
  396. }
  397. else {
  398. priv->repeats = 0;
  399. }
  400. }
  401. if (!got_time) {
  402. now = rspamd_get_calendar_ticks();
  403. }
  404. struct rspamd_logger_iov_ctx iov_ctx;
  405. memset(&iov_ctx, 0, sizeof(iov_ctx));
  406. rspamd_log_fill_iov(&iov_ctx, now, module, id, function, level_flags, message,
  407. mlen, rspamd_log);
  408. bool ret = file_log_helper(rspamd_log, priv, iov_ctx.iov, iov_ctx.niov, level_flags);
  409. rspamd_log_iov_free(&iov_ctx);
  410. return ret;
  411. }
  412. void *
  413. rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
  414. gpointer arg, uid_t uid, gid_t gid, GError **err)
  415. {
  416. struct rspamd_file_logger_priv *npriv;
  417. if (!cfg->cfg_name) {
  418. g_set_error(err, FILE_LOG_QUARK, EINVAL,
  419. "no log file specified");
  420. return NULL;
  421. }
  422. npriv = rspamd_log_file_init(logger, cfg, uid, gid, err);
  423. if (npriv) {
  424. /* Close old */
  425. rspamd_log_file_dtor(logger, arg);
  426. }
  427. return npriv;
  428. }
  429. bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg,
  430. gpointer arg, GError **err)
  431. {
  432. struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg;
  433. rspamd_log_reset_repeated(logger, priv);
  434. rspamd_log_flush(logger, priv);
  435. return true;
  436. }