blob: 44ad629cdf66570896cab298f01336d3d40b53b8 [file] [log] [blame]
Willy Tarreau67b5a162019-08-11 16:38:56 +02001/*
2 * Event sink management
3 *
4 * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +020021#include <sys/mman.h>
22#include <errno.h>
23#include <fcntl.h>
24
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <import/ist.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020027#include <haproxy/applet.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020028#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020029#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020031#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <haproxy/log.h>
Willy Tarreau817538e2021-05-08 20:20:21 +020033#include <haproxy/proxy.h>
Willy Tarreaud2ad57c2020-06-03 19:43:35 +020034#include <haproxy/ring.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020035#include <haproxy/sc_strm.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020036#include <haproxy/signal.h>
Willy Tarreauba2f73d2020-06-03 20:02:28 +020037#include <haproxy/sink.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/time.h>
Willy Tarreau4bad5e22021-05-08 13:05:30 +020040#include <haproxy/tools.h>
Willy Tarreau67b5a162019-08-11 16:38:56 +020041
42struct list sink_list = LIST_HEAD_INIT(sink_list);
43
Emeric Brund6e581d2022-09-13 16:16:30 +020044/* sink proxies list */
45struct proxy *sink_proxies_list;
46
Emeric Brun99c453d2020-05-25 15:01:04 +020047struct sink *cfg_sink;
48
Willy Tarreau67b5a162019-08-11 16:38:56 +020049struct sink *sink_find(const char *name)
50{
51 struct sink *sink;
52
53 list_for_each_entry(sink, &sink_list, sink_list)
54 if (strcmp(sink->name, name) == 0)
55 return sink;
56 return NULL;
57}
58
59/* creates a new sink and adds it to the list, it's still generic and not fully
60 * initialized. Returns NULL on allocation failure. If another one already
61 * exists with the same name, it will be returned. The caller can detect it as
62 * a newly created one has type SINK_TYPE_NEW.
63 */
Emeric Brun54648852020-07-06 15:54:06 +020064static struct sink *__sink_new(const char *name, const char *desc, int fmt)
Willy Tarreau67b5a162019-08-11 16:38:56 +020065{
66 struct sink *sink;
67
68 sink = sink_find(name);
69 if (sink)
70 goto end;
71
Emeric Brun494c5052020-05-28 11:13:15 +020072 sink = calloc(1, sizeof(*sink));
Willy Tarreau67b5a162019-08-11 16:38:56 +020073 if (!sink)
74 goto end;
75
Emeric Brun99c453d2020-05-25 15:01:04 +020076 sink->name = strdup(name);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010077 if (!sink->name)
78 goto err;
79
Emeric Brun99c453d2020-05-25 15:01:04 +020080 sink->desc = strdup(desc);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010081 if (!sink->desc)
82 goto err;
83
Willy Tarreau67b5a162019-08-11 16:38:56 +020084 sink->fmt = fmt;
85 sink->type = SINK_TYPE_NEW;
Christopher Fauleta63a5c22019-11-15 15:10:12 +010086 sink->maxlen = BUFSIZE;
Willy Tarreau67b5a162019-08-11 16:38:56 +020087 /* address will be filled by the caller if needed */
Willy Tarreau973e6622019-08-20 11:57:52 +020088 sink->ctx.fd = -1;
Willy Tarreau67b5a162019-08-11 16:38:56 +020089 sink->ctx.dropped = 0;
90 HA_RWLOCK_INIT(&sink->ctx.lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020091 LIST_APPEND(&sink_list, &sink->sink_list);
Willy Tarreau67b5a162019-08-11 16:38:56 +020092 end:
93 return sink;
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010094
95 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +010096 ha_free(&sink->name);
97 ha_free(&sink->desc);
98 ha_free(&sink);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010099
100 return NULL;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200101}
102
Willy Tarreau973e6622019-08-20 11:57:52 +0200103/* creates a sink called <name> of type FD associated to fd <fd>, format <fmt>,
104 * and description <desc>. Returns NULL on allocation failure or conflict.
105 * Perfect duplicates are merged (same type, fd, and name).
106 */
Emeric Brun54648852020-07-06 15:54:06 +0200107struct sink *sink_new_fd(const char *name, const char *desc, enum log_fmt fmt, int fd)
Willy Tarreau973e6622019-08-20 11:57:52 +0200108{
109 struct sink *sink;
110
111 sink = __sink_new(name, desc, fmt);
112 if (!sink || (sink->type == SINK_TYPE_FD && sink->ctx.fd == fd))
113 goto end;
114
115 if (sink->type != SINK_TYPE_NEW) {
116 sink = NULL;
117 goto end;
118 }
119
120 sink->type = SINK_TYPE_FD;
121 sink->ctx.fd = fd;
122 end:
123 return sink;
124}
125
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200126/* creates a sink called <name> of type BUF of size <size>, format <fmt>,
127 * and description <desc>. Returns NULL on allocation failure or conflict.
128 * Perfect duplicates are merged (same type and name). If sizes differ, the
129 * largest one is kept.
130 */
Emeric Brun54648852020-07-06 15:54:06 +0200131struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt, size_t size)
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200132{
133 struct sink *sink;
134
135 sink = __sink_new(name, desc, fmt);
136 if (!sink)
137 goto fail;
138
139 if (sink->type == SINK_TYPE_BUFFER) {
140 /* such a buffer already exists, we may have to resize it */
141 if (!ring_resize(sink->ctx.ring, size))
142 goto fail;
143 goto end;
144 }
145
146 if (sink->type != SINK_TYPE_NEW) {
147 /* already exists of another type */
148 goto fail;
149 }
150
151 sink->ctx.ring = ring_new(size);
152 if (!sink->ctx.ring) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200153 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +0200154 free(sink->name);
155 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200156 free(sink);
157 goto fail;
158 }
159
160 sink->type = SINK_TYPE_BUFFER;
161 end:
162 return sink;
163 fail:
164 return NULL;
165}
166
Willy Tarreau67b5a162019-08-11 16:38:56 +0200167/* tries to send <nmsg> message parts (up to 8, ignored above) from message
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500168 * array <msg> to sink <sink>. Formatting according to the sink's preference is
Willy Tarreau8f240232019-08-27 16:41:06 +0200169 * done here. Lost messages are NOT accounted for. It is preferable to call
170 * sink_write() instead which will also try to emit the number of dropped
171 * messages when there are any. It returns >0 if it could write anything,
172 * <=0 otherwise.
Willy Tarreau67b5a162019-08-11 16:38:56 +0200173 */
Emeric Brun54648852020-07-06 15:54:06 +0200174 ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
175 int level, int facility, struct ist *metadata)
176 {
177 struct ist *pfx = NULL;
Willy Tarreaua1426de2019-08-27 14:21:02 +0200178 size_t npfx = 0;
Emeric Brunbd163812020-05-06 14:33:46 +0200179
Emeric Brun54648852020-07-06 15:54:06 +0200180 if (sink->fmt == LOG_FORMAT_RAW)
Emeric Brunbd163812020-05-06 14:33:46 +0200181 goto send;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200182
Emeric Brun54648852020-07-06 15:54:06 +0200183 pfx = build_log_header(sink->fmt, level, facility, metadata, &npfx);
Emeric Brunbd163812020-05-06 14:33:46 +0200184
185send:
Willy Tarreau973e6622019-08-20 11:57:52 +0200186 if (sink->type == SINK_TYPE_FD) {
Willy Tarreau8f240232019-08-27 16:41:06 +0200187 return fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200188 }
189 else if (sink->type == SINK_TYPE_BUFFER) {
Willy Tarreau8f240232019-08-27 16:41:06 +0200190 return ring_write(sink->ctx.ring, sink->maxlen, pfx, npfx, msg, nmsg);
Willy Tarreau973e6622019-08-20 11:57:52 +0200191 }
Willy Tarreau8f240232019-08-27 16:41:06 +0200192 return 0;
193}
Willy Tarreau67b5a162019-08-11 16:38:56 +0200194
Willy Tarreau8f240232019-08-27 16:41:06 +0200195/* Tries to emit a message indicating the number of dropped events. In case of
196 * success, the amount of drops is reduced by as much. It's supposed to be
197 * called under an exclusive lock on the sink to avoid multiple produces doing
198 * the same. On success, >0 is returned, otherwise <=0 on failure.
199 */
Emeric Brun54648852020-07-06 15:54:06 +0200200int sink_announce_dropped(struct sink *sink, int facility)
Willy Tarreau8f240232019-08-27 16:41:06 +0200201{
Emeric Brun54648852020-07-06 15:54:06 +0200202 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
203 static THREAD_LOCAL pid_t curr_pid;
204 static THREAD_LOCAL char pidstr[16];
Willy Tarreau8f240232019-08-27 16:41:06 +0200205 unsigned int dropped;
206 struct buffer msg;
207 struct ist msgvec[1];
208 char logbuf[64];
209
210 while (unlikely((dropped = sink->ctx.dropped) > 0)) {
211 chunk_init(&msg, logbuf, sizeof(logbuf));
212 chunk_printf(&msg, "%u event%s dropped", dropped, dropped > 1 ? "s" : "");
213 msgvec[0] = ist2(msg.area, msg.data);
Emeric Brunbd163812020-05-06 14:33:46 +0200214
Emeric Brun54648852020-07-06 15:54:06 +0200215 if (!metadata[LOG_META_HOST].len) {
216 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +0100217 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +0200218 }
219
220 if (!metadata[LOG_META_TAG].len)
221 metadata[LOG_META_TAG] = ist2(global.log_tag.area, global.log_tag.data);
222
223 if (unlikely(curr_pid != getpid()))
224 metadata[LOG_META_PID].len = 0;
225
226 if (!metadata[LOG_META_PID].len) {
227 curr_pid = getpid();
228 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
229 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
230 }
231
232 if (__sink_write(sink, msgvec, 1, LOG_NOTICE, facility, metadata) <= 0)
Willy Tarreau8f240232019-08-27 16:41:06 +0200233 return 0;
234 /* success! */
235 HA_ATOMIC_SUB(&sink->ctx.dropped, dropped);
236 }
237 return 1;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200238}
239
Willy Tarreau9f830d72019-08-26 18:17:04 +0200240/* parse the "show events" command, returns 1 if a message is returned, otherwise zero */
241static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private)
242{
243 struct sink *sink;
Willy Tarreaucba88382022-05-05 15:18:57 +0200244 uint ring_flags;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200245 int arg;
Willy Tarreau9f830d72019-08-26 18:17:04 +0200246
247 args++; // make args[1] the 1st arg
248
249 if (!*args[1]) {
250 /* no arg => report the list of supported sink */
Willy Tarreau1d181e42019-08-30 11:17:01 +0200251 chunk_printf(&trash, "Supported events sinks are listed below. Add -w(wait), -n(new). Any key to stop\n");
Willy Tarreau9f830d72019-08-26 18:17:04 +0200252 list_for_each_entry(sink, &sink_list, sink_list) {
253 chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n",
254 sink->name,
255 sink->type == SINK_TYPE_NEW ? "init" :
256 sink->type == SINK_TYPE_FD ? "fd" :
257 sink->type == SINK_TYPE_BUFFER ? "buffer" : "?",
258 sink->ctx.dropped, sink->desc);
259 }
260
261 trash.area[trash.data] = 0;
262 return cli_msg(appctx, LOG_WARNING, trash.area);
263 }
264
265 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
266 return 1;
267
268 sink = sink_find(args[1]);
269 if (!sink)
270 return cli_err(appctx, "No such event sink");
271
272 if (sink->type != SINK_TYPE_BUFFER)
273 return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink");
274
Willy Tarreaucba88382022-05-05 15:18:57 +0200275 ring_flags = 0;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200276 for (arg = 2; *args[arg]; arg++) {
277 if (strcmp(args[arg], "-w") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200278 ring_flags |= RING_WF_WAIT_MODE;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200279 else if (strcmp(args[arg], "-n") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200280 ring_flags |= RING_WF_SEEK_NEW;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200281 else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200282 ring_flags |= RING_WF_WAIT_MODE | RING_WF_SEEK_NEW;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200283 else
284 return cli_err(appctx, "unknown option");
285 }
Willy Tarreaucba88382022-05-05 15:18:57 +0200286 return ring_attach_cli(sink->ctx.ring, appctx, ring_flags);
Willy Tarreau9f830d72019-08-26 18:17:04 +0200287}
288
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500289/* Pre-configures a ring proxy to emit connections */
Emeric Brun494c5052020-05-28 11:13:15 +0200290void sink_setup_proxy(struct proxy *px)
291{
292 px->last_change = now.tv_sec;
Christopher Faulet11a707a2022-10-24 15:10:18 +0200293 px->cap = PR_CAP_BE;
Emeric Brun494c5052020-05-28 11:13:15 +0200294 px->maxconn = 0;
295 px->conn_retries = 1;
296 px->timeout.server = TICK_ETERNITY;
297 px->timeout.client = TICK_ETERNITY;
298 px->timeout.connect = TICK_ETERNITY;
299 px->accept = NULL;
300 px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Emeric Brund6e581d2022-09-13 16:16:30 +0200301 px->next = sink_proxies_list;
302 sink_proxies_list = px;
Emeric Brun494c5052020-05-28 11:13:15 +0200303}
304
305/*
Willy Tarreau42cc8312022-05-04 20:42:23 +0200306 * IO Handler to handle message push to syslog tcp server.
307 * It takes its context from appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200308 */
309static void sink_forward_io_handler(struct appctx *appctx)
310{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200311 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau0eca5392022-05-27 10:44:25 +0200312 struct stream *s = __sc_strm(sc);
Emeric Brun494c5052020-05-28 11:13:15 +0200313 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200314 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200315 struct ring *ring = sink->ctx.ring;
316 struct buffer *buf = &ring->buf;
317 uint64_t msg_len;
Willy Tarreau53bfab02022-08-04 17:18:54 +0200318 size_t len, cnt, ofs, last_ofs;
Emeric Brun494c5052020-05-28 11:13:15 +0200319 int ret = 0;
320
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500321 /* if stopping was requested, close immediately */
Emeric Brun494c5052020-05-28 11:13:15 +0200322 if (unlikely(stopping))
323 goto close;
324
Christopher Fauletda89e9b2023-01-04 14:11:10 +0100325 if (unlikely(sc_ic(sc)->flags & CF_SHUTW))
Emeric Brun494c5052020-05-28 11:13:15 +0200326 goto close;
327
328 /* con closed by server side */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200329 if ((sc_oc(sc)->flags & CF_SHUTW))
Emeric Brun494c5052020-05-28 11:13:15 +0200330 goto close;
331
332 /* if the connection is not established, inform the stream that we want
333 * to be notified whenever the connection completes.
334 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200335 if (sc_opposite(sc)->state < SC_ST_EST) {
Willy Tarreau90e8b452022-05-25 18:21:43 +0200336 applet_need_more_data(appctx);
Willy Tarreaub23edc82022-05-24 16:49:03 +0200337 se_need_remote_conn(appctx->sedesc);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200338 applet_have_more_data(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200339 return;
340 }
341
342 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
343 if (appctx != sft->appctx) {
344 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
345 goto close;
346 }
347 ofs = sft->ofs;
348
349 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
350 LIST_DEL_INIT(&appctx->wait_entry);
351 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
352
353 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
354
355 /* explanation for the initialization below: it would be better to do
356 * this in the parsing function but this would occasionally result in
357 * dropped events because we'd take a reference on the oldest message
358 * and keep it while being scheduled. Thus instead let's take it the
359 * first time we enter here so that we have a chance to pass many
360 * existing messages before grabbing a reference to a location. This
361 * value cannot be produced after initialization.
362 */
363 if (unlikely(ofs == ~0)) {
364 ofs = 0;
365
Willy Tarreau4781b152021-04-06 13:53:36 +0200366 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200367 ofs += ring->ofs;
368 }
369
Emeric Brun494c5052020-05-28 11:13:15 +0200370 /* in this loop, ofs always points to the counter byte that precedes
371 * the message so that we can take our reference there if we have to
372 * stop before the end (ret=0).
373 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200374 if (sc_opposite(sc)->state == SC_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100375 /* we were already there, adjust the offset to be relative to
376 * the buffer's head and remove us from the counter.
377 */
378 ofs -= ring->ofs;
379 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200380 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100381
Emeric Brun494c5052020-05-28 11:13:15 +0200382 ret = 1;
383 while (ofs + 1 < b_data(buf)) {
384 cnt = 1;
385 len = b_peek_varint(buf, ofs + cnt, &msg_len);
386 if (!len)
387 break;
388 cnt += len;
389 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
390
391 if (unlikely(msg_len + 1 > b_size(&trash))) {
392 /* too large a message to ever fit, let's skip it */
393 ofs += cnt + msg_len;
394 continue;
395 }
396
397 chunk_reset(&trash);
398 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
399 trash.data += len;
400 trash.area[trash.data++] = '\n';
401
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200402 if (applet_putchk(appctx, &trash) == -1) {
Emeric Brun494c5052020-05-28 11:13:15 +0200403 ret = 0;
404 break;
405 }
406 ofs += cnt + msg_len;
407 }
408
Willy Tarreau4781b152021-04-06 13:53:36 +0200409 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200410 ofs += ring->ofs;
411 sft->ofs = ofs;
Willy Tarreau53bfab02022-08-04 17:18:54 +0200412 last_ofs = ring->ofs;
Emeric Brun494c5052020-05-28 11:13:15 +0200413 }
414 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
415
416 if (ret) {
417 /* let's be woken up once new data arrive */
418 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200419 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Willy Tarreau53bfab02022-08-04 17:18:54 +0200420 ofs = ring->ofs;
Emeric Brun494c5052020-05-28 11:13:15 +0200421 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau53bfab02022-08-04 17:18:54 +0200422 if (ofs != last_ofs) {
423 /* more data was added into the ring between the
424 * unlock and the lock, and the writer might not
425 * have seen us. We need to reschedule a read.
426 */
427 applet_have_more_data(appctx);
428 } else
429 applet_have_no_more_data(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200430 }
431 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
432
433 /* always drain data from server */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200434 co_skip(sc_oc(sc), sc_oc(sc)->output);
Emeric Brun494c5052020-05-28 11:13:15 +0200435 return;
436
437close:
Willy Tarreau0eca5392022-05-27 10:44:25 +0200438 sc_shutw(sc);
439 sc_shutr(sc);
Emeric Brun494c5052020-05-28 11:13:15 +0200440}
441
Emeric Brun97556472020-05-30 01:42:45 +0200442/*
443 * IO Handler to handle message push to syslog tcp server
444 * using octet counting frames
Willy Tarreau42cc8312022-05-04 20:42:23 +0200445 * It takes its context from appctx->svcctx.
Emeric Brun97556472020-05-30 01:42:45 +0200446 */
447static void sink_forward_oc_io_handler(struct appctx *appctx)
448{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200449 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau0eca5392022-05-27 10:44:25 +0200450 struct stream *s = __sc_strm(sc);
Emeric Brun97556472020-05-30 01:42:45 +0200451 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200452 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun97556472020-05-30 01:42:45 +0200453 struct ring *ring = sink->ctx.ring;
454 struct buffer *buf = &ring->buf;
455 uint64_t msg_len;
456 size_t len, cnt, ofs;
457 int ret = 0;
458 char *p;
459
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500460 /* if stopping was requested, close immediately */
Emeric Brun97556472020-05-30 01:42:45 +0200461 if (unlikely(stopping))
462 goto close;
463
Emeric Brun97556472020-05-30 01:42:45 +0200464 /* an error was detected */
Christopher Fauletda89e9b2023-01-04 14:11:10 +0100465 if (unlikely(sc_ic(sc)->flags & CF_SHUTW))
Emeric Brun97556472020-05-30 01:42:45 +0200466 goto close;
467
468 /* con closed by server side */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200469 if ((sc_oc(sc)->flags & CF_SHUTW))
Emeric Brun97556472020-05-30 01:42:45 +0200470 goto close;
471
472 /* if the connection is not established, inform the stream that we want
473 * to be notified whenever the connection completes.
474 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200475 if (sc_opposite(sc)->state < SC_ST_EST) {
Willy Tarreau90e8b452022-05-25 18:21:43 +0200476 applet_need_more_data(appctx);
Willy Tarreaub23edc82022-05-24 16:49:03 +0200477 se_need_remote_conn(appctx->sedesc);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200478 applet_have_more_data(appctx);
Emeric Brun97556472020-05-30 01:42:45 +0200479 return;
480 }
481
482 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
483 if (appctx != sft->appctx) {
484 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
485 goto close;
486 }
487 ofs = sft->ofs;
488
489 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
490 LIST_DEL_INIT(&appctx->wait_entry);
491 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
492
493 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
494
495 /* explanation for the initialization below: it would be better to do
496 * this in the parsing function but this would occasionally result in
497 * dropped events because we'd take a reference on the oldest message
498 * and keep it while being scheduled. Thus instead let's take it the
499 * first time we enter here so that we have a chance to pass many
500 * existing messages before grabbing a reference to a location. This
501 * value cannot be produced after initialization.
502 */
503 if (unlikely(ofs == ~0)) {
504 ofs = 0;
505
Willy Tarreau4781b152021-04-06 13:53:36 +0200506 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200507 ofs += ring->ofs;
508 }
509
Emeric Brun97556472020-05-30 01:42:45 +0200510 /* in this loop, ofs always points to the counter byte that precedes
511 * the message so that we can take our reference there if we have to
512 * stop before the end (ret=0).
513 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200514 if (sc_opposite(sc)->state == SC_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100515 /* we were already there, adjust the offset to be relative to
516 * the buffer's head and remove us from the counter.
517 */
518 ofs -= ring->ofs;
519 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200520 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100521
Emeric Brun97556472020-05-30 01:42:45 +0200522 ret = 1;
523 while (ofs + 1 < b_data(buf)) {
524 cnt = 1;
525 len = b_peek_varint(buf, ofs + cnt, &msg_len);
526 if (!len)
527 break;
528 cnt += len;
529 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
530
531 chunk_reset(&trash);
532 p = ulltoa(msg_len, trash.area, b_size(&trash));
533 if (p) {
534 trash.data = (p - trash.area) + 1;
535 *p = ' ';
536 }
537
538 if (!p || (trash.data + msg_len > b_size(&trash))) {
539 /* too large a message to ever fit, let's skip it */
540 ofs += cnt + msg_len;
541 continue;
542 }
543
544 trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt);
545
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200546 if (applet_putchk(appctx, &trash) == -1) {
Emeric Brun97556472020-05-30 01:42:45 +0200547 ret = 0;
548 break;
549 }
550 ofs += cnt + msg_len;
551 }
552
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200554 ofs += ring->ofs;
555 sft->ofs = ofs;
556 }
557 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
558
559 if (ret) {
560 /* let's be woken up once new data arrive */
561 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200562 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun97556472020-05-30 01:42:45 +0200563 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200564 applet_have_no_more_data(appctx);
Emeric Brun97556472020-05-30 01:42:45 +0200565 }
566 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
567
568 /* always drain data from server */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200569 co_skip(sc_oc(sc), sc_oc(sc)->output);
Emeric Brun97556472020-05-30 01:42:45 +0200570 return;
571
572close:
Willy Tarreau0eca5392022-05-27 10:44:25 +0200573 sc_shutw(sc);
574 sc_shutr(sc);
Emeric Brun97556472020-05-30 01:42:45 +0200575}
576
Emeric Brun494c5052020-05-28 11:13:15 +0200577void __sink_forward_session_deinit(struct sink_forward_target *sft)
578{
Willy Tarreau0698c802022-05-11 14:09:57 +0200579 struct stream *s = appctx_strm(sft->appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200580 struct sink *sink;
581
Emeric Brun494c5052020-05-28 11:13:15 +0200582 sink = strm_fe(s)->parent;
583 if (!sink)
584 return;
585
586 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock);
587 LIST_DEL_INIT(&sft->appctx->wait_entry);
588 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock);
589
590 sft->appctx = NULL;
591 task_wakeup(sink->forward_task, TASK_WOKEN_MSG);
592}
593
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200594static int sink_forward_session_init(struct appctx *appctx)
595{
596 struct sink_forward_target *sft = appctx->svcctx;
597 struct stream *s;
598 struct sockaddr_storage *addr = NULL;
599
600 if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr)))
601 goto out_error;
602
603 if (appctx_finalize_startup(appctx, sft->sink->forward_px, &BUF_NULL) == -1)
604 goto out_free_addr;
605
606 s = appctx_strm(appctx);
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200607 s->scb->dst = addr;
Willy Tarreaucb041662022-05-17 19:44:42 +0200608 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200609
610 s->target = &sft->srv->obj_type;
611 s->flags = SF_ASSIGNED;
612
613 s->do_log = NULL;
614 s->uniq_id = 0;
615
616 s->res.flags |= CF_READ_DONTWAIT;
Christopher Faulet2ca4cc12023-02-22 14:22:56 +0100617 applet_expect_no_data(appctx);
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200618 sft->appctx = appctx;
619
620 return 0;
621
622 out_free_addr:
623 sockaddr_free(&addr);
624 out_error:
625 return -1;
626}
Emeric Brun494c5052020-05-28 11:13:15 +0200627
628static void sink_forward_session_release(struct appctx *appctx)
629{
Willy Tarreau42cc8312022-05-04 20:42:23 +0200630 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200631
632 if (!sft)
633 return;
634
635 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
636 if (sft->appctx == appctx)
637 __sink_forward_session_deinit(sft);
638 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
639}
640
641static struct applet sink_forward_applet = {
642 .obj_type = OBJ_TYPE_APPLET,
643 .name = "<SINKFWD>", /* used for logging */
644 .fct = sink_forward_io_handler,
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200645 .init = sink_forward_session_init,
Emeric Brun494c5052020-05-28 11:13:15 +0200646 .release = sink_forward_session_release,
647};
648
Emeric Brun97556472020-05-30 01:42:45 +0200649static struct applet sink_forward_oc_applet = {
650 .obj_type = OBJ_TYPE_APPLET,
651 .name = "<SINKFWDOC>", /* used for logging */
652 .fct = sink_forward_oc_io_handler,
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200653 .init = sink_forward_session_init,
Emeric Brun97556472020-05-30 01:42:45 +0200654 .release = sink_forward_session_release,
655};
656
Emeric Brun494c5052020-05-28 11:13:15 +0200657/*
658 * Create a new peer session in assigned state (connect will start automatically)
Willy Tarreau42cc8312022-05-04 20:42:23 +0200659 * It sets its context into appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200660 */
661static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft)
662{
Emeric Brun494c5052020-05-28 11:13:15 +0200663 struct appctx *appctx;
Emeric Brun97556472020-05-30 01:42:45 +0200664 struct applet *applet = &sink_forward_applet;
665
666 if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING)
667 applet = &sink_forward_oc_applet;
Emeric Brun494c5052020-05-28 11:13:15 +0200668
Christopher Faulet6095d572022-05-16 17:09:48 +0200669 appctx = appctx_new_here(applet, NULL);
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100670 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100671 goto out_close;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200672 appctx->svcctx = (void *)sft;
Emeric Brun494c5052020-05-28 11:13:15 +0200673
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200674 if (appctx_init(appctx) == -1)
Christopher Faulet92202da2022-05-11 12:22:10 +0200675 goto out_free_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100676
Emeric Brun494c5052020-05-28 11:13:15 +0200677 return appctx;
678
679 /* Error unrolling */
Emeric Brun494c5052020-05-28 11:13:15 +0200680 out_free_appctx:
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200681 appctx_free_on_early_error(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200682 out_close:
683 return NULL;
684}
685
686/*
687 * Task to handle connctions to forward servers
688 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100689static struct task *process_sink_forward(struct task * task, void *context, unsigned int state)
Emeric Brun494c5052020-05-28 11:13:15 +0200690{
691 struct sink *sink = (struct sink *)context;
692 struct sink_forward_target *sft = sink->sft;
693
694 task->expire = TICK_ETERNITY;
695
696 if (!stopping) {
697 while (sft) {
698 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
699 /* if appctx is NULL, start a new session */
700 if (!sft->appctx)
701 sft->appctx = sink_forward_session_create(sink, sft);
702 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
703 sft = sft->next;
704 }
705 }
706 else {
707 while (sft) {
708 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
709 /* awake applet to perform a clean close */
710 if (sft->appctx)
711 appctx_wakeup(sft->appctx);
712 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
713 sft = sft->next;
714 }
715 }
716
717 return task;
718}
719/*
720 * Init task to manage connctions to forward servers
721 *
722 * returns 0 in case of error.
723 */
724int sink_init_forward(struct sink *sink)
725{
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200726 sink->forward_task = task_new_anywhere();
Emeric Brun494c5052020-05-28 11:13:15 +0200727 if (!sink->forward_task)
728 return 0;
729
730 sink->forward_task->process = process_sink_forward;
731 sink->forward_task->context = (void *)sink;
732 sink->forward_sighandler = signal_register_task(0, sink->forward_task, 0);
733 task_wakeup(sink->forward_task, TASK_WOKEN_INIT);
734 return 1;
735}
Willy Tarreau32872db2022-08-31 18:52:17 +0200736
737/* This tries to rotate a file-backed ring, but only if it contains contents.
738 * This way empty rings will not cause backups to be overwritten and it's safe
739 * to reload multiple times. That's only best effort, failures are silently
740 * ignored.
741 */
742void sink_rotate_file_backed_ring(const char *name)
743{
744 struct ring ring;
745 char *oldback;
746 int ret;
747 int fd;
748
749 fd = open(name, O_RDONLY);
750 if (fd < 0)
751 return;
752
753 /* check for contents validity */
754 ret = read(fd, &ring, sizeof(ring));
755 close(fd);
756
757 if (ret != sizeof(ring))
758 goto rotate;
759
760 /* contents are present, we want to keep them => rotate. Note that
761 * an empty ring buffer has one byte (the marker).
762 */
763 if (ring.buf.data > 1)
764 goto rotate;
765
766 /* nothing to keep, let's scratch the file and preserve the backup */
767 return;
768
769 rotate:
770 oldback = NULL;
771 memprintf(&oldback, "%s.bak", name);
772 if (oldback) {
773 /* try to rename any possibly existing ring file to
774 * ".bak" and delete remains of older ones. This will
775 * ensure we don't wipe useful debug info upon restart.
776 */
777 unlink(oldback);
778 if (rename(name, oldback) < 0)
779 unlink(oldback);
780 ha_free(&oldback);
781 }
782}
783
Emeric Brun99c453d2020-05-25 15:01:04 +0200784/*
785 * Parse "ring" section and create corresponding sink buffer.
786 *
787 * The function returns 0 in success case, otherwise, it returns error
788 * flags.
789 */
790int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
791{
792 int err_code = 0;
793 const char *inv;
794 size_t size = BUFSIZE;
Emeric Brun494c5052020-05-28 11:13:15 +0200795 struct proxy *p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200796
Willy Tarreau18d13062022-08-11 16:12:11 +0200797 if (strcmp(args[0], "ring") == 0) { /* new ring section */
Emeric Brun99c453d2020-05-25 15:01:04 +0200798 if (!*args[1]) {
799 ha_alert("parsing [%s:%d] : missing ring name.\n", file, linenum);
800 err_code |= ERR_ALERT | ERR_FATAL;
801 goto err;
802 }
803
804 inv = invalid_char(args[1]);
805 if (inv) {
806 ha_alert("parsing [%s:%d] : invalid ring name '%s' (character '%c' is not permitted).\n", file, linenum, args[1], *inv);
807 err_code |= ERR_ALERT | ERR_FATAL;
808 goto err;
809 }
810
811 if (sink_find(args[1])) {
812 ha_alert("parsing [%s:%d] : sink named '%s' already exists.\n", file, linenum, args[1]);
813 err_code |= ERR_ALERT | ERR_FATAL;
814 goto err;
815 }
816
Emeric Brun54648852020-07-06 15:54:06 +0200817 cfg_sink = sink_new_buf(args[1], args[1], LOG_FORMAT_RAW, size);
Emeric Brun99c453d2020-05-25 15:01:04 +0200818 if (!cfg_sink || cfg_sink->type != SINK_TYPE_BUFFER) {
819 ha_alert("parsing [%s:%d] : unable to create a new sink buffer for ring '%s'.\n", file, linenum, args[1]);
820 err_code |= ERR_ALERT | ERR_FATAL;
821 goto err;
822 }
Emeric Brun494c5052020-05-28 11:13:15 +0200823
824 /* allocate new proxy to handle forwards */
825 p = calloc(1, sizeof *p);
826 if (!p) {
827 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
828 err_code |= ERR_ALERT | ERR_FATAL;
829 goto err;
830 }
831
832 init_new_proxy(p);
833 sink_setup_proxy(p);
834 p->parent = cfg_sink;
835 p->id = strdup(args[1]);
836 p->conf.args.file = p->conf.file = strdup(file);
837 p->conf.args.line = p->conf.line = linenum;
838 cfg_sink->forward_px = p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200839 }
840 else if (strcmp(args[0], "size") == 0) {
Willy Tarreau18d13062022-08-11 16:12:11 +0200841 if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)) {
842 ha_alert("parsing [%s:%d] : 'size' directive not usable with this type of sink.\n", file, linenum);
843 err_code |= ERR_ALERT | ERR_FATAL;
844 goto err;
845 }
846
Emeric Brun99c453d2020-05-25 15:01:04 +0200847 size = atol(args[1]);
848 if (!size) {
849 ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
850 err_code |= ERR_ALERT | ERR_FATAL;
851 goto err;
852 }
853
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +0200854 if (cfg_sink->store) {
855 ha_alert("parsing [%s:%d] : cannot resize an already mapped file, please specify 'size' before 'backing-file'.\n", file, linenum);
856 err_code |= ERR_ALERT | ERR_FATAL;
857 goto err;
858 }
859
Willy Tarreau18d13062022-08-11 16:12:11 +0200860 if (size < cfg_sink->ctx.ring->buf.size) {
861 ha_warning("parsing [%s:%d] : ignoring new size '%llu' that is smaller than current size '%llu' for ring '%s'.\n",
862 file, linenum, (ullong)size, (ullong)cfg_sink->ctx.ring->buf.size, cfg_sink->name);
863 err_code |= ERR_ALERT | ERR_FATAL;
864 goto err;
865 }
866
867 if (!ring_resize(cfg_sink->ctx.ring, size)) {
868 ha_alert("parsing [%s:%d] : fail to set sink buffer size '%llu' for ring '%s'.\n", file, linenum,
869 (ullong)cfg_sink->ctx.ring->buf.size, cfg_sink->name);
Emeric Brun99c453d2020-05-25 15:01:04 +0200870 err_code |= ERR_ALERT | ERR_FATAL;
871 goto err;
872 }
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +0200873 }
874 else if (strcmp(args[0], "backing-file") == 0) {
875 /* This tries to mmap file <file> for size <size> and to use it as a backing store
876 * for ring <ring>. Existing data are delete. NULL is returned on error.
877 */
878 const char *backing = args[1];
879 size_t size;
880 void *area;
881 int fd;
882
883 if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)) {
884 ha_alert("parsing [%s:%d] : 'backing-file' only usable with existing rings.\n", file, linenum);
885 err_code |= ERR_ALERT | ERR_FATAL;
886 goto err;
887 }
888
889 if (cfg_sink->store) {
890 ha_alert("parsing [%s:%d] : 'backing-file' already specified for ring '%s' (was '%s').\n", file, linenum, cfg_sink->name, cfg_sink->store);
891 err_code |= ERR_ALERT | ERR_FATAL;
892 goto err;
893 }
894
Willy Tarreau32872db2022-08-31 18:52:17 +0200895 /* let's check if the file exists and is not empty. That's the
896 * only condition under which we'll trigger a rotate, so that
897 * config checks, reloads, or restarts that don't emit anything
898 * do not rotate it again.
899 */
900 sink_rotate_file_backed_ring(backing);
Willy Tarreauded77cc2022-08-12 15:38:20 +0200901
Willy Tarreau8e877052022-08-12 15:03:12 +0200902 fd = open(backing, O_RDWR | O_CREAT, 0600);
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +0200903 if (fd < 0) {
904 ha_alert("parsing [%s:%d] : cannot open backing-file '%s' for ring '%s': %s.\n", file, linenum, backing, cfg_sink->name, strerror(errno));
905 err_code |= ERR_ALERT | ERR_FATAL;
906 goto err;
907 }
908
909 size = (cfg_sink->ctx.ring->buf.size + 4095UL) & -4096UL;
910 if (ftruncate(fd, size) != 0) {
911 close(fd);
912 ha_alert("parsing [%s:%d] : could not adjust size of backing-file for ring '%s': %s.\n", file, linenum, cfg_sink->name, strerror(errno));
913 err_code |= ERR_ALERT | ERR_FATAL;
914 goto err;
915 }
916
917 area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
918 if (area == MAP_FAILED) {
919 close(fd);
920 ha_alert("parsing [%s:%d] : failed to use '%s' as a backing file for ring '%s': %s.\n", file, linenum, backing, cfg_sink->name, strerror(errno));
921 err_code |= ERR_ALERT | ERR_FATAL;
922 goto err;
923 }
924
925 /* we don't need the file anymore */
926 close(fd);
927 cfg_sink->store = strdup(backing);
928
929 /* never fails */
930 ring_free(cfg_sink->ctx.ring);
931 cfg_sink->ctx.ring = ring_make_from_area(area, size);
Emeric Brun99c453d2020-05-25 15:01:04 +0200932 }
Emeric Brun494c5052020-05-28 11:13:15 +0200933 else if (strcmp(args[0],"server") == 0) {
Willy Tarreau1b662aa2022-11-16 18:56:34 +0100934 if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)) {
935 ha_alert("parsing [%s:%d] : unable to create server '%s'.\n", file, linenum, args[1]);
936 err_code |= ERR_ALERT | ERR_FATAL;
937 goto err;
938 }
939
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100940 err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL,
941 SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
Emeric Brun494c5052020-05-28 11:13:15 +0200942 }
943 else if (strcmp(args[0],"timeout") == 0) {
944 if (!cfg_sink || !cfg_sink->forward_px) {
945 ha_alert("parsing [%s:%d] : unable to set timeout '%s'.\n", file, linenum, args[1]);
946 err_code |= ERR_ALERT | ERR_FATAL;
947 goto err;
948 }
949
950 if (strcmp(args[1], "connect") == 0 ||
951 strcmp(args[1], "server") == 0) {
952 const char *res;
953 unsigned int tout;
954
955 if (!*args[2]) {
956 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
957 file, linenum, args[0], args[1]);
958 err_code |= ERR_ALERT | ERR_FATAL;
959 goto err;
960 }
961 res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
962 if (res == PARSE_TIME_OVER) {
963 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
964 file, linenum, args[2], args[0], args[1]);
965 err_code |= ERR_ALERT | ERR_FATAL;
966 goto err;
967 }
968 else if (res == PARSE_TIME_UNDER) {
969 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
970 file, linenum, args[2], args[0], args[1]);
971 err_code |= ERR_ALERT | ERR_FATAL;
972 goto err;
973 }
974 else if (res) {
975 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
976 file, linenum, *res, args[0], args[1]);
977 err_code |= ERR_ALERT | ERR_FATAL;
978 goto err;
979 }
Christopher Faulet321d1002022-10-19 16:26:21 +0200980 if (args[1][0] == 'c')
Emeric Brun494c5052020-05-28 11:13:15 +0200981 cfg_sink->forward_px->timeout.connect = tout;
982 else
983 cfg_sink->forward_px->timeout.server = tout;
984 }
985 }
Emeric Brun99c453d2020-05-25 15:01:04 +0200986 else if (strcmp(args[0],"format") == 0) {
987 if (!cfg_sink) {
988 ha_alert("parsing [%s:%d] : unable to set format '%s'.\n", file, linenum, args[1]);
989 err_code |= ERR_ALERT | ERR_FATAL;
990 goto err;
991 }
992
Emeric Brun54648852020-07-06 15:54:06 +0200993 cfg_sink->fmt = get_log_format(args[1]);
994 if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) {
Emeric Brun99c453d2020-05-25 15:01:04 +0200995 ha_alert("parsing [%s:%d] : unknown format '%s'.\n", file, linenum, args[1]);
996 err_code |= ERR_ALERT | ERR_FATAL;
997 goto err;
998 }
999 }
1000 else if (strcmp(args[0],"maxlen") == 0) {
1001 if (!cfg_sink) {
1002 ha_alert("parsing [%s:%d] : unable to set event max length '%s'.\n", file, linenum, args[1]);
1003 err_code |= ERR_ALERT | ERR_FATAL;
1004 goto err;
1005 }
1006
1007 cfg_sink->maxlen = atol(args[1]);
1008 if (!cfg_sink->maxlen) {
1009 ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
1010 err_code |= ERR_ALERT | ERR_FATAL;
1011 goto err;
1012 }
1013 }
1014 else if (strcmp(args[0],"description") == 0) {
1015 if (!cfg_sink) {
1016 ha_alert("parsing [%s:%d] : unable to set description '%s'.\n", file, linenum, args[1]);
1017 err_code |= ERR_ALERT | ERR_FATAL;
1018 goto err;
1019 }
1020
1021 if (!*args[1]) {
1022 ha_alert("parsing [%s:%d] : missing ring description text.\n", file, linenum);
1023 err_code |= ERR_ALERT | ERR_FATAL;
1024 goto err;
1025 }
1026
1027 free(cfg_sink->desc);
1028
1029 cfg_sink->desc = strdup(args[1]);
1030 if (!cfg_sink->desc) {
1031 ha_alert("parsing [%s:%d] : fail to set description '%s'.\n", file, linenum, args[1]);
1032 err_code |= ERR_ALERT | ERR_FATAL;
1033 goto err;
1034 }
1035 }
Emeric Brun9f2ff3a2020-05-29 15:47:52 +02001036 else {
1037 ha_alert("parsing [%s:%d] : unknown statement '%s'.\n", file, linenum, args[0]);
1038 err_code |= ERR_ALERT | ERR_FATAL;
1039 goto err;
1040 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001041
1042err:
1043 return err_code;
1044}
1045
Emeric Brun94aab062021-04-02 10:41:36 +02001046/* Creates an new sink buffer from a log server.
1047 *
1048 * It uses the logsrvaddress to declare a forward
1049 * server for this buffer. And it initializes the
1050 * forwarding.
1051 *
1052 * The function returns a pointer on the
1053 * allocated struct sink if allocate
1054 * and initialize succeed, else if it fails
1055 * it returns NULL.
1056 *
1057 * Note: the sink is created using the name
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05001058 * specified into logsrv->ring_name
Emeric Brun94aab062021-04-02 10:41:36 +02001059 */
1060struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
1061{
1062 struct proxy *p = NULL;
1063 struct sink *sink = NULL;
1064 struct server *srv = NULL;
1065 struct sink_forward_target *sft = NULL;
Emeric Brun94aab062021-04-02 10:41:36 +02001066
1067 /* allocate new proxy to handle
1068 * forward to a stream server
1069 */
1070 p = calloc(1, sizeof *p);
1071 if (!p) {
1072 goto error;
1073 }
1074
1075 init_new_proxy(p);
1076 sink_setup_proxy(p);
1077 p->id = strdup(logsrv->ring_name);
1078 p->conf.args.file = p->conf.file = strdup(logsrv->conf.file);
1079 p->conf.args.line = p->conf.line = logsrv->conf.line;
1080
Christopher Fauletd08a25b2022-10-24 15:53:01 +02001081 /* Set default connect and server timeout */
1082 p->timeout.connect = MS_TO_TICKS(1000);
1083 p->timeout.server = MS_TO_TICKS(5000);
1084
Emeric Brun94aab062021-04-02 10:41:36 +02001085 /* allocate a new server to forward messages
1086 * from ring buffer
1087 */
1088 srv = new_server(p);
1089 if (!srv)
1090 goto error;
1091
1092 /* init server */
1093 srv->id = strdup(logsrv->ring_name);
1094 srv->conf.file = strdup(logsrv->conf.file);
1095 srv->conf.line = logsrv->conf.line;
1096 srv->addr = logsrv->addr;
1097 srv->svc_port = get_host_port(&logsrv->addr);
1098 HA_SPIN_INIT(&srv->lock);
1099
1100 /* process per thread init */
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02001101 if (srv_init_per_thr(srv) == -1)
Emeric Brun94aab062021-04-02 10:41:36 +02001102 goto error;
1103
Emeric Brun94aab062021-04-02 10:41:36 +02001104 /* the servers are linked backwards
1105 * first into proxy
1106 */
1107 p->srv = srv;
1108 srv->next = p->srv;
1109
1110 /* allocate sink_forward_target descriptor */
1111 sft = calloc(1, sizeof(*sft));
1112 if (!sft)
1113 goto error;
1114
1115 /* init sink_forward_target offset */
1116 sft->srv = srv;
1117 sft->appctx = NULL;
1118 sft->ofs = ~0;
1119 HA_SPIN_INIT(&sft->lock);
1120
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05001121 /* prepare description for the sink */
Emeric Brun94aab062021-04-02 10:41:36 +02001122 chunk_reset(&trash);
1123 chunk_printf(&trash, "created from logserver declared into '%s' at line %d", logsrv->conf.file, logsrv->conf.line);
1124
1125 /* allocate a new sink buffer */
1126 sink = sink_new_buf(logsrv->ring_name, trash.area, logsrv->format, BUFSIZE);
1127 if (!sink || sink->type != SINK_TYPE_BUFFER) {
1128 goto error;
1129 }
1130
1131 /* link sink_forward_target to proxy */
1132 sink->forward_px = p;
1133 p->parent = sink;
1134
1135 /* insert into sink_forward_targets
1136 * list into sink
1137 */
Christopher Faulet2ae25ea2022-05-12 14:50:09 +02001138 sft->sink = sink;
Emeric Brun94aab062021-04-02 10:41:36 +02001139 sft->next = sink->sft;
1140 sink->sft = sft;
1141
1142 /* mark server as an attached reader to the ring */
1143 if (!ring_attach(sink->ctx.ring)) {
1144 /* should never fail since there is
1145 * only one reader
1146 */
1147 goto error;
1148 }
1149
1150 /* initialize sink buffer forwarding */
1151 if (!sink_init_forward(sink))
1152 goto error;
1153
1154 /* reset familyt of logsrv to consider the ring buffer target */
1155 logsrv->addr.ss_family = AF_UNSPEC;
1156
1157 return sink;
1158error:
1159 if (p) {
1160 if (p->id)
1161 free(p->id);
1162 if (p->conf.file)
1163 free(p->conf.file);
1164
1165 free(p);
1166 }
1167
1168 if (srv) {
1169 if (srv->id)
1170 free(srv->id);
1171 if (srv->conf.file)
1172 free((void *)srv->conf.file);
1173 if (srv->per_thr)
1174 free(srv->per_thr);
1175 free(srv);
1176 }
1177
1178 if (sft)
1179 free(sft);
1180
1181 if (sink) {
1182 if (sink->ctx.ring)
1183 ring_free(sink->ctx.ring);
1184
Willy Tarreau2b718102021-04-21 07:32:39 +02001185 LIST_DELETE(&sink->sink_list);
Emeric Brun94aab062021-04-02 10:41:36 +02001186 free(sink->name);
1187 free(sink->desc);
1188 free(sink);
1189 }
1190
1191 return NULL;
1192}
1193
Emeric Brun99c453d2020-05-25 15:01:04 +02001194/*
1195 * Post parsing "ring" section.
1196 *
1197 * The function returns 0 in success case, otherwise, it returns error
1198 * flags.
1199 */
1200int cfg_post_parse_ring()
1201{
1202 int err_code = 0;
Emeric Brun494c5052020-05-28 11:13:15 +02001203 struct server *srv;
Emeric Brun99c453d2020-05-25 15:01:04 +02001204
1205 if (cfg_sink && (cfg_sink->type == SINK_TYPE_BUFFER)) {
1206 if (cfg_sink->maxlen > b_size(&cfg_sink->ctx.ring->buf)) {
1207 ha_warning("ring '%s' event max length '%u' exceeds size, forced to size '%lu'.\n",
Willy Tarreau570a22b2020-06-02 12:00:46 +02001208 cfg_sink->name, cfg_sink->maxlen, (unsigned long)b_size(&cfg_sink->ctx.ring->buf));
Emeric Brun99c453d2020-05-25 15:01:04 +02001209 cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf);
1210 err_code |= ERR_ALERT;
1211 }
Emeric Brun494c5052020-05-28 11:13:15 +02001212
1213 /* prepare forward server descriptors */
1214 if (cfg_sink->forward_px) {
1215 srv = cfg_sink->forward_px->srv;
1216 while (srv) {
1217 struct sink_forward_target *sft;
Emeric Brun99c453d2020-05-25 15:01:04 +02001218
Emeric Brun494c5052020-05-28 11:13:15 +02001219 /* allocate sink_forward_target descriptor */
1220 sft = calloc(1, sizeof(*sft));
1221 if (!sft) {
1222 ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n",srv->id, cfg_sink->name);
1223 err_code |= ERR_ALERT | ERR_FATAL;
1224 break;
1225 }
1226 sft->srv = srv;
1227 sft->appctx = NULL;
1228 sft->ofs = ~0; /* init ring offset */
Christopher Faulet96417f32022-08-04 16:00:13 +02001229 sft->sink = cfg_sink;
Emeric Brun494c5052020-05-28 11:13:15 +02001230 sft->next = cfg_sink->sft;
1231 HA_SPIN_INIT(&sft->lock);
1232
1233 /* mark server attached to the ring */
1234 if (!ring_attach(cfg_sink->ctx.ring)) {
1235 ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, cfg_sink->name);
1236 err_code |= ERR_ALERT | ERR_FATAL;
1237 }
1238 cfg_sink->sft = sft;
1239 srv = srv->next;
1240 }
1241 sink_init_forward(cfg_sink);
1242 }
1243 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001244 cfg_sink = NULL;
1245
1246 return err_code;
1247}
1248
1249/* resolve sink names at end of config. Returns 0 on success otherwise error
1250 * flags.
1251*/
1252int post_sink_resolve()
1253{
Christopher Fauletfc633b62020-11-06 15:24:23 +01001254 int err_code = ERR_NONE;
Emeric Brun99c453d2020-05-25 15:01:04 +02001255 struct logsrv *logsrv, *logb;
1256 struct sink *sink;
1257 struct proxy *px;
1258
1259 list_for_each_entry_safe(logsrv, logb, &global.logsrvs, list) {
1260 if (logsrv->type == LOG_TARGET_BUFFER) {
1261 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001262 if (!sink) {
1263 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1264 * means we must allocate a sink
1265 * buffer to send messages to this logsrv
1266 */
1267 if (logsrv->addr.ss_family != AF_UNSPEC) {
1268 sink = sink_new_from_logsrv(logsrv);
1269 if (!sink) {
1270 ha_alert("global stream log server declared in file '%s' at line %d cannot be initialized'.\n",
1271 logsrv->conf.file, logsrv->conf.line);
1272 err_code |= ERR_ALERT | ERR_FATAL;
1273 }
1274 }
1275 else {
1276 ha_alert("global log server declared in file '%s' at line %d uses unknown ring named '%s'.\n",
1277 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1278 err_code |= ERR_ALERT | ERR_FATAL;
1279 }
1280 }
1281 else if (sink->type != SINK_TYPE_BUFFER) {
1282 ha_alert("global log server declared in file '%s' at line %d uses incompatible ring '%s'.\n",
1283 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001284 err_code |= ERR_ALERT | ERR_FATAL;
1285 }
1286 logsrv->sink = sink;
1287 }
Emeric Brun94aab062021-04-02 10:41:36 +02001288
Emeric Brun99c453d2020-05-25 15:01:04 +02001289 }
1290
1291 for (px = proxies_list; px; px = px->next) {
1292 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1293 if (logsrv->type == LOG_TARGET_BUFFER) {
1294 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001295 if (!sink) {
1296 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1297 * means we must allocate a sink
1298 * buffer to send messages to this logsrv
1299 */
1300 if (logsrv->addr.ss_family != AF_UNSPEC) {
1301 sink = sink_new_from_logsrv(logsrv);
1302 if (!sink) {
1303 ha_alert("log server declared in proxy section '%s' file '%s' at line %d cannot be initialized'.\n",
1304 px->id, logsrv->conf.file, logsrv->conf.line);
1305 err_code |= ERR_ALERT | ERR_FATAL;
1306 }
1307 }
1308 else {
1309 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1310 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1311 err_code |= ERR_ALERT | ERR_FATAL;
1312 }
1313 }
1314 else if (sink->type != SINK_TYPE_BUFFER) {
1315 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses incomatible ring named '%s'.\n",
1316 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001317 err_code |= ERR_ALERT | ERR_FATAL;
1318 }
1319 logsrv->sink = sink;
1320 }
1321 }
1322 }
Emeric Brun12941c82020-07-07 14:19:42 +02001323
1324 for (px = cfg_log_forward; px; px = px->next) {
1325 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1326 if (logsrv->type == LOG_TARGET_BUFFER) {
1327 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001328 if (!sink) {
1329 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1330 * means we must allocate a sink
1331 * buffer to send messages to this logsrv
1332 */
1333 if (logsrv->addr.ss_family != AF_UNSPEC) {
1334 sink = sink_new_from_logsrv(logsrv);
1335 if (!sink) {
1336 ha_alert("log server declared in log-forward section '%s' file '%s' at line %d cannot be initialized'.\n",
1337 px->id, logsrv->conf.file, logsrv->conf.line);
1338 err_code |= ERR_ALERT | ERR_FATAL;
1339 }
1340 }
1341 else {
1342 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1343 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1344 err_code |= ERR_ALERT | ERR_FATAL;
1345 }
1346 }
1347 else if (sink->type != SINK_TYPE_BUFFER) {
1348 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1349 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun12941c82020-07-07 14:19:42 +02001350 err_code |= ERR_ALERT | ERR_FATAL;
1351 }
1352 logsrv->sink = sink;
1353 }
1354 }
1355 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001356 return err_code;
1357}
1358
1359
Willy Tarreau973e6622019-08-20 11:57:52 +02001360static void sink_init()
1361{
Emeric Brun54648852020-07-06 15:54:06 +02001362 sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1);
1363 sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2);
1364 sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001365}
1366
1367static void sink_deinit()
1368{
1369 struct sink *sink, *sb;
1370
1371 list_for_each_entry_safe(sink, sb, &sink_list, sink_list) {
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +02001372 if (sink->type == SINK_TYPE_BUFFER) {
Willy Tarreaufb9a4762023-01-24 12:11:41 +01001373 if (sink->store) {
1374 size_t size = (sink->ctx.ring->buf.size + 4095UL) & -4096UL;
1375 void *area = (sink->ctx.ring->buf.area - sizeof(*sink->ctx.ring));
1376
1377 msync(area, size, MS_SYNC);
1378 munmap(area, size);
Willy Tarreaub9191092023-01-26 15:34:31 +01001379 ha_free(&sink->store);
Willy Tarreaufb9a4762023-01-24 12:11:41 +01001380 }
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +02001381 else
1382 ring_free(sink->ctx.ring);
1383 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001384 LIST_DELETE(&sink->sink_list);
Willy Tarreau09727ee2023-01-26 15:46:08 +01001385 task_destroy(sink->forward_task);
Emeric Brun99c453d2020-05-25 15:01:04 +02001386 free(sink->name);
1387 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001388 free(sink);
1389 }
Willy Tarreau973e6622019-08-20 11:57:52 +02001390}
1391
1392INITCALL0(STG_REGISTER, sink_init);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001393REGISTER_POST_DEINIT(sink_deinit);
Willy Tarreau973e6622019-08-20 11:57:52 +02001394
Willy Tarreau9f830d72019-08-26 18:17:04 +02001395static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001396 { { "show", "events", NULL }, "show events [<sink>] [-w] [-n] : show event sink state", cli_parse_show_events, NULL, NULL },
Willy Tarreau9f830d72019-08-26 18:17:04 +02001397 {{},}
1398}};
1399
1400INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1401
Emeric Brun99c453d2020-05-25 15:01:04 +02001402/* config parsers for this section */
1403REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring);
1404REGISTER_POST_CHECK(post_sink_resolve);
1405
Willy Tarreau67b5a162019-08-11 16:38:56 +02001406/*
1407 * Local variables:
1408 * c-indent-level: 8
1409 * c-basic-offset: 8
1410 * End:
1411 */