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