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.

util.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #include <sys/param.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <fcntl.h>
  7. #include <netdb.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <stdarg.h>
  11. #include <sys/file.h>
  12. #include <syslog.h>
  13. #include <glib.h>
  14. #include "config.h"
  15. #ifdef HAVE_LIBUTIL_H
  16. #include <libutil.h>
  17. #endif
  18. #include "util.h"
  19. #include "cfg_file.h"
  20. sig_atomic_t do_reopen_log = 0;
  21. int
  22. event_make_socket_nonblocking (int fd)
  23. {
  24. if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
  25. return -1;
  26. }
  27. return 0;
  28. }
  29. static int
  30. make_socket_ai (struct addrinfo *ai)
  31. {
  32. struct linger linger;
  33. int fd, on = 1, r;
  34. int serrno;
  35. /* Create listen socket */
  36. fd = socket(AF_INET, SOCK_STREAM, 0);
  37. if (fd == -1) {
  38. return (-1);
  39. }
  40. if (event_make_socket_nonblocking(fd) < 0)
  41. goto out;
  42. if (fcntl(fd, F_SETFD, 1) == -1) {
  43. goto out;
  44. }
  45. setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
  46. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
  47. linger.l_onoff = 1;
  48. linger.l_linger = 5;
  49. setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
  50. r = bind(fd, ai->ai_addr, ai->ai_addrlen);
  51. if (r == -1) {
  52. if (errno != EINPROGRESS) {
  53. goto out;
  54. }
  55. }
  56. return (fd);
  57. out:
  58. serrno = errno;
  59. close(fd);
  60. errno = serrno;
  61. return (-1);
  62. }
  63. int
  64. make_socket (const char *address, u_short port)
  65. {
  66. int fd;
  67. struct addrinfo ai, *aitop = NULL;
  68. char strport[NI_MAXSERV];
  69. int ai_result;
  70. memset (&ai, 0, sizeof (ai));
  71. ai.ai_family = AF_INET;
  72. ai.ai_socktype = SOCK_STREAM;
  73. ai.ai_flags = AI_PASSIVE;
  74. snprintf (strport, sizeof (strport), "%d", port);
  75. if ((ai_result = getaddrinfo (address, strport, &ai, &aitop)) != 0) {
  76. return (-1);
  77. }
  78. fd = make_socket_ai (aitop);
  79. freeaddrinfo (aitop);
  80. return (fd);
  81. }
  82. int
  83. make_unix_socket (const char *path, struct sockaddr_un *addr)
  84. {
  85. size_t len = strlen (path);
  86. int sock;
  87. if (len > sizeof (addr->sun_path) - 1) return -1;
  88. #ifdef FREEBSD
  89. addr->sun_len = sizeof (struct sockaddr_un);
  90. #endif
  91. addr->sun_family = AF_UNIX;
  92. strncpy (addr->sun_path, path, len);
  93. sock = socket (PF_LOCAL, SOCK_STREAM, 0);
  94. if (sock != -1) {
  95. if (bind (sock, (struct sockaddr *) addr, sizeof (struct sockaddr_un)) == -1) return -1;
  96. }
  97. return sock;
  98. }
  99. void
  100. read_cmd_line (int argc, char **argv, struct config_file *cfg)
  101. {
  102. int ch;
  103. while ((ch = getopt(argc, argv, "hfc:")) != -1) {
  104. switch (ch) {
  105. case 'f':
  106. cfg->no_fork = 1;
  107. break;
  108. case 'c':
  109. if (optarg && cfg->cfg_name) {
  110. cfg->cfg_name = memory_pool_strdup (cfg->cfg_pool, optarg);
  111. }
  112. break;
  113. case 'h':
  114. case '?':
  115. default:
  116. /* Show help message and exit */
  117. printf ("Rspamd version " RVERSION "\n"
  118. "Usage: rspamd [-h] [-n] [-f] [-c config_file]\n"
  119. "-h: This help message\n"
  120. "-f: Do not daemonize main process\n"
  121. "-c: Specify config file (./rspamd.conf is used by default)\n");
  122. exit (0);
  123. break;
  124. }
  125. }
  126. }
  127. int
  128. write_pid (struct rspamd_main *main)
  129. {
  130. pid_t pid;
  131. main->pfh = pidfile_open (main->cfg->pid_file, 0644, &pid);
  132. if (main->pfh == NULL) {
  133. return -1;
  134. }
  135. pidfile_write (main->pfh);
  136. return 0;
  137. }
  138. void
  139. init_signals (struct sigaction *signals, sig_t sig_handler)
  140. {
  141. /* Setting up signal handlers */
  142. /* SIGUSR1 - reopen config file */
  143. /* SIGUSR2 - worker is ready for accept */
  144. sigemptyset(&signals->sa_mask);
  145. sigaddset(&signals->sa_mask, SIGTERM);
  146. sigaddset(&signals->sa_mask, SIGINT);
  147. sigaddset(&signals->sa_mask, SIGHUP);
  148. sigaddset(&signals->sa_mask, SIGCHLD);
  149. sigaddset(&signals->sa_mask, SIGUSR1);
  150. sigaddset(&signals->sa_mask, SIGUSR2);
  151. signals->sa_handler = sig_handler;
  152. sigaction (SIGTERM, signals, NULL);
  153. sigaction (SIGINT, signals, NULL);
  154. sigaction (SIGHUP, signals, NULL);
  155. sigaction (SIGCHLD, signals, NULL);
  156. sigaction (SIGUSR1, signals, NULL);
  157. sigaction (SIGUSR2, signals, NULL);
  158. }
  159. void
  160. pass_signal_worker (struct workq *workers, int signo)
  161. {
  162. struct rspamd_worker *cur;
  163. TAILQ_FOREACH (cur, workers, next) {
  164. kill (cur->pid, signo);
  165. }
  166. }
  167. void convert_to_lowercase (char *str, unsigned int size)
  168. {
  169. while (size--) {
  170. *str = tolower (*str);
  171. str ++;
  172. }
  173. }
  174. #ifndef HAVE_SETPROCTITLE
  175. static char *title_buffer = 0;
  176. static size_t title_buffer_size = 0;
  177. static char *title_progname, *title_progname_full;
  178. int
  179. setproctitle (const char *fmt, ...)
  180. {
  181. if (!title_buffer || !title_buffer_size) {
  182. errno = ENOMEM;
  183. return -1;
  184. }
  185. memset (title_buffer, '\0', title_buffer_size);
  186. ssize_t written;
  187. if (fmt) {
  188. ssize_t written2;
  189. va_list ap;
  190. written = snprintf (title_buffer, title_buffer_size, "%s: ", title_progname);
  191. if (written < 0 || (size_t) written >= title_buffer_size)
  192. return -1;
  193. va_start (ap, fmt);
  194. written2 =
  195. vsnprintf (title_buffer + written,
  196. title_buffer_size - written, fmt, ap);
  197. va_end (ap);
  198. if (written2 < 0
  199. || (size_t) written2 >= title_buffer_size - written)
  200. return -1;
  201. } else {
  202. written =
  203. snprintf (title_buffer, title_buffer_size, "%s",
  204. title_progname);
  205. if (written < 0 || (size_t) written >= title_buffer_size)
  206. return -1;
  207. }
  208. written = strlen (title_buffer);
  209. memset (title_buffer + written, '\0', title_buffer_size - written);
  210. return 0;
  211. }
  212. /*
  213. It has to be _init function, because __attribute__((constructor))
  214. functions gets called without arguments.
  215. */
  216. int
  217. init_title (int argc, char *argv[], char *envp[])
  218. {
  219. char *begin_of_buffer = 0, *end_of_buffer = 0;
  220. int i;
  221. for (i = 0; i < argc; ++i) {
  222. if (!begin_of_buffer)
  223. begin_of_buffer = argv[i];
  224. if (!end_of_buffer || end_of_buffer + 1 == argv[i])
  225. end_of_buffer = argv[i] + strlen (argv[i]);
  226. }
  227. for (i = 0; envp[i]; ++i) {
  228. if (!begin_of_buffer)
  229. begin_of_buffer = envp[i];
  230. if (!end_of_buffer || end_of_buffer + 1 == envp[i])
  231. end_of_buffer = envp[i] + strlen(envp[i]);
  232. }
  233. if (!end_of_buffer)
  234. return 0;
  235. char **new_environ = g_malloc ((i + 1) * sizeof (envp[0]));
  236. if (!new_environ)
  237. return 0;
  238. for (i = 0; envp[i]; ++i) {
  239. if (!(new_environ[i] = g_strdup (envp[i])))
  240. goto cleanup_enomem;
  241. }
  242. new_environ[i] = 0;
  243. if (program_invocation_name) {
  244. title_progname_full = g_strdup (program_invocation_name);
  245. if (!title_progname_full)
  246. goto cleanup_enomem;
  247. char *p = strrchr (title_progname_full, '/');
  248. if (p)
  249. title_progname = p + 1;
  250. else
  251. title_progname = title_progname_full;
  252. program_invocation_name = title_progname_full;
  253. program_invocation_short_name = title_progname;
  254. }
  255. environ = new_environ;
  256. title_buffer = begin_of_buffer;
  257. title_buffer_size = end_of_buffer - begin_of_buffer;
  258. return 0;
  259. cleanup_enomem:
  260. for (--i; i >= 0; --i) {
  261. g_free (new_environ[i]);
  262. }
  263. g_free (new_environ);
  264. return 0;
  265. }
  266. #endif
  267. #ifndef HAVE_PIDFILE
  268. extern char * __progname;
  269. static int _pidfile_remove (struct pidfh *pfh, int freeit);
  270. static int
  271. pidfile_verify (struct pidfh *pfh)
  272. {
  273. struct stat sb;
  274. if (pfh == NULL || pfh->pf_fd == -1)
  275. return (-1);
  276. /*
  277. * Check remembered descriptor.
  278. */
  279. if (fstat (pfh->pf_fd, &sb) == -1)
  280. return (errno);
  281. if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
  282. return -1;
  283. return 0;
  284. }
  285. static int
  286. pidfile_read (const char *path, pid_t *pidptr)
  287. {
  288. char buf[16], *endptr;
  289. int error, fd, i;
  290. fd = open (path, O_RDONLY);
  291. if (fd == -1)
  292. return (errno);
  293. i = read (fd, buf, sizeof(buf) - 1);
  294. error = errno; /* Remember errno in case close() wants to change it. */
  295. close (fd);
  296. if (i == -1)
  297. return error;
  298. else if (i == 0)
  299. return EAGAIN;
  300. buf[i] = '\0';
  301. *pidptr = strtol (buf, &endptr, 10);
  302. if (endptr != &buf[i])
  303. return EINVAL;
  304. return 0;
  305. }
  306. struct pidfh *
  307. pidfile_open (const char *path, mode_t mode, pid_t *pidptr)
  308. {
  309. struct pidfh *pfh;
  310. struct stat sb;
  311. int error, fd, len, count;
  312. struct timespec rqtp;
  313. pfh = g_malloc (sizeof(*pfh));
  314. if (pfh == NULL)
  315. return NULL;
  316. if (path == NULL)
  317. len = snprintf (pfh->pf_path, sizeof(pfh->pf_path),
  318. "/var/run/%s.pid", __progname);
  319. else
  320. len = snprintf (pfh->pf_path, sizeof(pfh->pf_path),
  321. "%s", path);
  322. if (len >= (int)sizeof (pfh->pf_path)) {
  323. g_free (pfh);
  324. errno = ENAMETOOLONG;
  325. return NULL;
  326. }
  327. /*
  328. * Open the PID file and obtain exclusive lock.
  329. * We truncate PID file here only to remove old PID immediatelly,
  330. * PID file will be truncated again in pidfile_write(), so
  331. * pidfile_write() can be called multiple times.
  332. */
  333. fd = open (pfh->pf_path,
  334. O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
  335. flock (fd, LOCK_EX | LOCK_NB);
  336. if (fd == -1) {
  337. count = 0;
  338. rqtp.tv_sec = 0;
  339. rqtp.tv_nsec = 5000000;
  340. if (errno == EWOULDBLOCK && pidptr != NULL) {
  341. again:
  342. errno = pidfile_read (pfh->pf_path, pidptr);
  343. if (errno == 0)
  344. errno = EEXIST;
  345. else if (errno == EAGAIN) {
  346. if (++count <= 3) {
  347. nanosleep (&rqtp, 0);
  348. goto again;
  349. }
  350. }
  351. }
  352. g_free (pfh);
  353. return NULL;
  354. }
  355. /*
  356. * Remember file information, so in pidfile_write() we are sure we write
  357. * to the proper descriptor.
  358. */
  359. if (fstat (fd, &sb) == -1) {
  360. error = errno;
  361. unlink (pfh->pf_path);
  362. close (fd);
  363. g_free (pfh);
  364. errno = error;
  365. return NULL;
  366. }
  367. pfh->pf_fd = fd;
  368. pfh->pf_dev = sb.st_dev;
  369. pfh->pf_ino = sb.st_ino;
  370. return pfh;
  371. }
  372. int
  373. pidfile_write (struct pidfh *pfh)
  374. {
  375. char pidstr[16];
  376. int error, fd;
  377. /*
  378. * Check remembered descriptor, so we don't overwrite some other
  379. * file if pidfile was closed and descriptor reused.
  380. */
  381. errno = pidfile_verify (pfh);
  382. if (errno != 0) {
  383. /*
  384. * Don't close descriptor, because we are not sure if it's ours.
  385. */
  386. return -1;
  387. }
  388. fd = pfh->pf_fd;
  389. /*
  390. * Truncate PID file, so multiple calls of pidfile_write() are allowed.
  391. */
  392. if (ftruncate (fd, 0) == -1) {
  393. error = errno;
  394. _pidfile_remove (pfh, 0);
  395. errno = error;
  396. return -1;
  397. }
  398. snprintf (pidstr, sizeof(pidstr), "%u", getpid ());
  399. if (pwrite (fd, pidstr, strlen (pidstr), 0) != (ssize_t)strlen (pidstr)) {
  400. error = errno;
  401. _pidfile_remove (pfh, 0);
  402. errno = error;
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. int
  408. pidfile_close (struct pidfh *pfh)
  409. {
  410. int error;
  411. error = pidfile_verify (pfh);
  412. if (error != 0) {
  413. errno = error;
  414. return -1;
  415. }
  416. if (close (pfh->pf_fd) == -1)
  417. error = errno;
  418. g_free (pfh);
  419. if (error != 0) {
  420. errno = error;
  421. return -1;
  422. }
  423. return 0;
  424. }
  425. static int
  426. _pidfile_remove (struct pidfh *pfh, int freeit)
  427. {
  428. int error;
  429. error = pidfile_verify (pfh);
  430. if (error != 0) {
  431. errno = error;
  432. return -1;
  433. }
  434. if (unlink (pfh->pf_path) == -1)
  435. error = errno;
  436. if (flock (pfh->pf_fd, LOCK_UN) == -1) {
  437. if (error == 0)
  438. error = errno;
  439. }
  440. if (close (pfh->pf_fd) == -1) {
  441. if (error == 0)
  442. error = errno;
  443. }
  444. if (freeit)
  445. g_free (pfh);
  446. else
  447. pfh->pf_fd = -1;
  448. if (error != 0) {
  449. errno = error;
  450. return -1;
  451. }
  452. return 0;
  453. }
  454. int
  455. pidfile_remove (struct pidfh *pfh)
  456. {
  457. return (_pidfile_remove (pfh, 1));
  458. }
  459. #endif
  460. /*
  461. * Functions for parsing expressions
  462. */
  463. struct expression_stack {
  464. char op;
  465. struct expression_stack *next;
  466. };
  467. /*
  468. * Push operand or operator to stack
  469. */
  470. static struct expression_stack*
  471. push_expression_stack (memory_pool_t *pool, struct expression_stack *head, char op)
  472. {
  473. struct expression_stack *new;
  474. new = memory_pool_alloc (pool, sizeof (struct expression_stack));
  475. new->op = op;
  476. new->next = head;
  477. return new;
  478. }
  479. /*
  480. * Delete symbol from stack, return pointer to operand or operator (casted to void* )
  481. */
  482. static char
  483. delete_expression_stack (struct expression_stack **head)
  484. {
  485. struct expression_stack *cur;
  486. char res;
  487. if(*head == NULL) return 0;
  488. cur = *head;
  489. res = cur->op;
  490. *head = cur->next;
  491. return res;
  492. }
  493. /*
  494. * Return operation priority
  495. */
  496. static int
  497. logic_priority (char a)
  498. {
  499. switch (a) {
  500. case '!':
  501. return 3;
  502. case '|':
  503. case '&':
  504. return 2;
  505. case '(':
  506. return 1;
  507. default:
  508. return 0;
  509. }
  510. }
  511. /*
  512. * Return 0 if symbol is not operation symbol (operand)
  513. * Return 1 if symbol is operation symbol
  514. */
  515. static int
  516. is_operation_symbol (char a)
  517. {
  518. switch (a) {
  519. case '!':
  520. case '&':
  521. case '|':
  522. case '(':
  523. case ')':
  524. return 1;
  525. default:
  526. return 0;
  527. }
  528. }
  529. static void
  530. insert_expression (memory_pool_t *pool, struct expression **head, int type, char op, void *operand)
  531. {
  532. struct expression *new, *cur;
  533. new = memory_pool_alloc (pool, sizeof (struct expression));
  534. new->type = type;
  535. if (new->type == EXPR_OPERAND) {
  536. new->content.operand = operand;
  537. }
  538. else {
  539. new->content.operation = op;
  540. }
  541. new->next = NULL;
  542. if (!*head) {
  543. *head = new;
  544. }
  545. else {
  546. cur = *head;
  547. while (cur->next) {
  548. cur = cur->next;
  549. }
  550. cur->next = new;
  551. }
  552. }
  553. /*
  554. * Make inverse polish record for specified expression
  555. * Memory is allocated from given pool
  556. */
  557. struct expression*
  558. parse_expression (memory_pool_t *pool, char *line)
  559. {
  560. struct expression *expr = NULL;
  561. struct expression_stack *stack = NULL;
  562. char *p, *c, *str, op, in_regexp = 0;
  563. if (line == NULL || pool == NULL) {
  564. return NULL;
  565. }
  566. p = line;
  567. c = p;
  568. while (*p) {
  569. if (is_operation_symbol (*p) && !in_regexp) {
  570. if (c != p) {
  571. /* Copy operand */
  572. str = memory_pool_alloc (pool, p - c + 1);
  573. strlcpy (str, c, (p - c + 1));
  574. insert_expression (pool, &expr, EXPR_OPERAND, 0, str);
  575. }
  576. if (*p == ')') {
  577. if (stack == NULL) {
  578. return NULL;
  579. }
  580. /* Pop all operators from stack to nearest '(' or to head */
  581. while (stack->op != '(') {
  582. op = delete_expression_stack (&stack);
  583. if (op != '(') {
  584. insert_expression (pool, &expr, EXPR_OPERATION, op, NULL);
  585. }
  586. }
  587. }
  588. else if (*p == '(') {
  589. /* Push it to stack */
  590. stack = push_expression_stack (pool, stack, *p);
  591. }
  592. else {
  593. if (stack == NULL) {
  594. stack = push_expression_stack (pool, stack, *p);
  595. }
  596. /* Check priority of logic operation */
  597. else {
  598. if (logic_priority (stack->op) < logic_priority (*p)) {
  599. stack = push_expression_stack (pool, stack, *p);
  600. }
  601. else {
  602. /* Pop all operations that have higher priority than this one */
  603. while((stack != NULL) && (logic_priority (stack->op) >= logic_priority (*p))) {
  604. op = delete_expression_stack (&stack);
  605. if (op != '(') {
  606. insert_expression (pool, &expr, EXPR_OPERATION, op, NULL);
  607. }
  608. }
  609. stack = push_expression_stack (pool, stack, *p);
  610. }
  611. }
  612. }
  613. c = p + 1;
  614. }
  615. if (*p == '/' && (p == line || *(p - 1) != '\\')) {
  616. in_regexp = !in_regexp;
  617. }
  618. p++;
  619. }
  620. /* Write last operand if it exists */
  621. if (c != p) {
  622. /* Copy operand */
  623. str = memory_pool_alloc (pool, p - c + 1);
  624. strlcpy (str, c, (p - c + 1));
  625. insert_expression (pool, &expr, EXPR_OPERAND, 0, str);
  626. }
  627. /* Pop everything from stack */
  628. while(stack != NULL) {
  629. op = delete_expression_stack (&stack);
  630. if (op != '(') {
  631. insert_expression (pool, &expr, EXPR_OPERATION, op, NULL);
  632. }
  633. }
  634. return expr;
  635. }
  636. /* Logging utility functions */
  637. int
  638. open_log (struct config_file *cfg)
  639. {
  640. switch (cfg->log_type) {
  641. case RSPAMD_LOG_CONSOLE:
  642. /* Do nothing with console */
  643. return 0;
  644. case RSPAMD_LOG_SYSLOG:
  645. openlog ("rspamd", LOG_NDELAY | LOG_PID, cfg->log_facility);
  646. return 0;
  647. case RSPAMD_LOG_FILE:
  648. cfg->log_fd = open (cfg->log_file, O_CREAT | O_WRONLY | O_APPEND);
  649. if (cfg->log_fd == -1) {
  650. msg_err ("open_log: cannot open desired log file: %s, %m", cfg->log_file);
  651. return -1;
  652. }
  653. return 0;
  654. }
  655. }
  656. int
  657. reopen_log (struct config_file *cfg)
  658. {
  659. do_reopen_log = 0;
  660. switch (cfg->log_type) {
  661. case RSPAMD_LOG_CONSOLE:
  662. /* Do nothing with console */
  663. return 0;
  664. case RSPAMD_LOG_SYSLOG:
  665. closelog ();
  666. break;
  667. case RSPAMD_LOG_FILE:
  668. close (cfg->log_fd);
  669. break;
  670. }
  671. return open_log (cfg);
  672. }
  673. void
  674. syslog_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg)
  675. {
  676. struct config_file *cfg = (struct config_file *)arg;
  677. if (do_reopen_log) {
  678. reopen_log (cfg);
  679. }
  680. if (log_level <= cfg->log_level) {
  681. if (log_level >= G_LOG_LEVEL_DEBUG) {
  682. syslog (LOG_DEBUG, message);
  683. }
  684. else if (log_level >= G_LOG_LEVEL_INFO) {
  685. syslog (LOG_INFO, message);
  686. }
  687. else if (log_level >= G_LOG_LEVEL_WARNING) {
  688. syslog (LOG_WARNING, message);
  689. }
  690. else if (log_level >= G_LOG_LEVEL_CRITICAL) {
  691. syslog (LOG_ERR, message);
  692. }
  693. }
  694. }
  695. void
  696. file_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg)
  697. {
  698. struct config_file *cfg = (struct config_file *)arg;
  699. char tmpbuf[128];
  700. int r;
  701. struct iovec out[3];
  702. if (cfg->log_fd == -1) {
  703. return;
  704. }
  705. if (do_reopen_log) {
  706. reopen_log (cfg);
  707. }
  708. if (log_level <= cfg->log_level) {
  709. r = snprintf (tmpbuf, sizeof (tmpbuf), "#%d: %d rspamd ", (int)getpid (), (int)time (NULL));
  710. out[0].iov_base = tmpbuf;
  711. out[0].iov_len = r;
  712. out[1].iov_base = (char *)message;
  713. out[1].iov_len = strlen (message);
  714. out[2].iov_base = "\r\n";
  715. out[2].iov_len = 2;
  716. writev (cfg->log_fd, out, sizeof (out) / sizeof (out[0]));
  717. }
  718. }
  719. /*
  720. * vi:ts=4
  721. */