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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
|
/*
* Event sink management
*
* Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, version 2.1
* exclusively.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <import/ist.h>
#include <haproxy/api.h>
#include <haproxy/applet.h>
#include <haproxy/cfgparse.h>
#include <haproxy/cli.h>
#include <haproxy/errors.h>
#include <haproxy/list.h>
#include <haproxy/log.h>
#include <haproxy/proxy.h>
#include <haproxy/ring.h>
#include <haproxy/sc_strm.h>
#include <haproxy/signal.h>
#include <haproxy/sink.h>
#include <haproxy/stconn.h>
#include <haproxy/time.h>
#include <haproxy/tools.h>
struct list sink_list = LIST_HEAD_INIT(sink_list);
/* sink proxies list */
struct proxy *sink_proxies_list;
struct sink *cfg_sink;
struct sink *sink_find(const char *name)
{
struct sink *sink;
list_for_each_entry(sink, &sink_list, sink_list)
if (strcmp(sink->name, name) == 0)
return sink;
return NULL;
}
/* creates a new sink and adds it to the list, it's still generic and not fully
* initialized. Returns NULL on allocation failure. If another one already
* exists with the same name, it will be returned. The caller can detect it as
* a newly created one has type SINK_TYPE_NEW.
*/
static struct sink *__sink_new(const char *name, const char *desc, int fmt)
{
struct sink *sink;
sink = sink_find(name);
if (sink)
goto end;
sink = calloc(1, sizeof(*sink));
if (!sink)
goto end;
sink->name = strdup(name);
if (!sink->name)
goto err;
sink->desc = strdup(desc);
if (!sink->desc)
goto err;
sink->fmt = fmt;
sink->type = SINK_TYPE_NEW;
sink->maxlen = BUFSIZE;
/* address will be filled by the caller if needed */
sink->ctx.fd = -1;
sink->ctx.dropped = 0;
HA_RWLOCK_INIT(&sink->ctx.lock);
LIST_APPEND(&sink_list, &sink->sink_list);
end:
return sink;
err:
ha_free(&sink->name);
ha_free(&sink->desc);
ha_free(&sink);
return NULL;
}
/* creates a sink called <name> of type FD associated to fd <fd>, format <fmt>,
* and description <desc>. Returns NULL on allocation failure or conflict.
* Perfect duplicates are merged (same type, fd, and name).
*/
struct sink *sink_new_fd(const char *name, const char *desc, enum log_fmt fmt, int fd)
{
struct sink *sink;
sink = __sink_new(name, desc, fmt);
if (!sink || (sink->type == SINK_TYPE_FD && sink->ctx.fd == fd))
goto end;
if (sink->type != SINK_TYPE_NEW) {
sink = NULL;
goto end;
}
sink->type = SINK_TYPE_FD;
sink->ctx.fd = fd;
end:
return sink;
}
/* creates a sink called <name> of type BUF of size <size>, format <fmt>,
* and description <desc>. Returns NULL on allocation failure or conflict.
* Perfect duplicates are merged (same type and name). If sizes differ, the
* largest one is kept.
*/
struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt, size_t size)
{
struct sink *sink;
sink = __sink_new(name, desc, fmt);
if (!sink)
goto fail;
if (sink->type == SINK_TYPE_BUFFER) {
/* such a buffer already exists, we may have to resize it */
if (!ring_resize(sink->ctx.ring, size))
goto fail;
goto end;
}
if (sink->type != SINK_TYPE_NEW) {
/* already exists of another type */
goto fail;
}
sink->ctx.ring = ring_new(size);
if (!sink->ctx.ring) {
LIST_DELETE(&sink->sink_list);
free(sink->name);
free(sink->desc);
free(sink);
goto fail;
}
sink->type = SINK_TYPE_BUFFER;
end:
return sink;
fail:
return NULL;
}
/* tries to send <nmsg> message parts from message array <msg> to sink <sink>.
* Formatting according to the sink's preference is done here, unless sink->fmt
* is unspecified, in which case the caller formatting will be used instead.
* Lost messages are NOT accounted for. It is preferable to call sink_write()
* instead which will also try to emit the number of dropped messages when there
* are any.
*
* It will stop writing at <maxlen> instead of sink->maxlen if <maxlen> is
* positive and inferior to sink->maxlen.
*
* It returns >0 if it could write anything, <=0 otherwise.
*/
ssize_t __sink_write(struct sink *sink, struct log_header hdr,
size_t maxlen, const struct ist msg[], size_t nmsg)
{
struct ist *pfx = NULL;
size_t npfx = 0;
if (sink->fmt == LOG_FORMAT_RAW)
goto send;
if (sink->fmt != LOG_FORMAT_UNSPEC)
hdr.format = sink->fmt; /* sink format prevails over log one */
pfx = build_log_header(hdr, &npfx);
send:
if (!maxlen)
maxlen = ~0;
if (sink->type == SINK_TYPE_FD) {
return fd_write_frag_line(sink->ctx.fd, MIN(maxlen, sink->maxlen), pfx, npfx, msg, nmsg, 1);
}
else if (sink->type == SINK_TYPE_BUFFER) {
return ring_write(sink->ctx.ring, MIN(maxlen, sink->maxlen), pfx, npfx, msg, nmsg);
}
return 0;
}
/* Tries to emit a message indicating the number of dropped events.
* The log header of the original message that we tried to emit is reused
* here with the only difference that we override the log level. This is
* possible since the announce message will be sent from the same context.
*
* In case of success, the amount of drops is reduced by as much. It's supposed
* to be called under an exclusive lock on the sink to avoid multiple producers
* doing the same. On success, >0 is returned, otherwise <=0 on failure.
*/
int sink_announce_dropped(struct sink *sink, struct log_header hdr)
{
unsigned int dropped;
struct buffer msg;
struct ist msgvec[1];
char logbuf[64];
while (unlikely((dropped = sink->ctx.dropped) > 0)) {
chunk_init(&msg, logbuf, sizeof(logbuf));
chunk_printf(&msg, "%u event%s dropped", dropped, dropped > 1 ? "s" : "");
msgvec[0] = ist2(msg.area, msg.data);
hdr.level = LOG_NOTICE; /* override level but keep original log header data */
if (__sink_write(sink, hdr, 0, msgvec, 1) <= 0)
return 0;
/* success! */
HA_ATOMIC_SUB(&sink->ctx.dropped, dropped);
}
return 1;
}
/* parse the "show events" command, returns 1 if a message is returned, otherwise zero */
static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private)
{
struct sink *sink;
uint ring_flags;
int arg;
args++; // make args[1] the 1st arg
if (!*args[1]) {
/* no arg => report the list of supported sink */
chunk_printf(&trash, "Supported events sinks are listed below. Add -w(wait), -n(new). Any key to stop\n");
list_for_each_entry(sink, &sink_list, sink_list) {
chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n",
sink->name,
sink->type == SINK_TYPE_NEW ? "init" :
sink->type == SINK_TYPE_FD ? "fd" :
sink->type == SINK_TYPE_BUFFER ? "buffer" : "?",
sink->ctx.dropped, sink->desc);
}
trash.area[trash.data] = 0;
return cli_msg(appctx, LOG_WARNING, trash.area);
}
if (!cli_has_level(appctx, ACCESS_LVL_OPER))
return 1;
sink = sink_find(args[1]);
if (!sink)
return cli_err(appctx, "No such event sink");
if (sink->type != SINK_TYPE_BUFFER)
return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink");
ring_flags = 0;
for (arg = 2; *args[arg]; arg++) {
if (strcmp(args[arg], "-w") == 0)
ring_flags |= RING_WF_WAIT_MODE;
else if (strcmp(args[arg], "-n") == 0)
ring_flags |= RING_WF_SEEK_NEW;
else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0)
ring_flags |= RING_WF_WAIT_MODE | RING_WF_SEEK_NEW;
else
return cli_err(appctx, "unknown option");
}
return ring_attach_cli(sink->ctx.ring, appctx, ring_flags);
}
/* Pre-configures a ring proxy to emit connections */
void sink_setup_proxy(struct proxy *px)
{
px->last_change = ns_to_sec(now_ns);
px->cap = PR_CAP_BE;
px->maxconn = 0;
px->conn_retries = 1;
px->timeout.server = TICK_ETERNITY;
px->timeout.client = TICK_ETERNITY;
px->timeout.connect = TICK_ETERNITY;
px->accept = NULL;
px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
px->next = sink_proxies_list;
sink_proxies_list = px;
}
/*
* IO Handler to handle message push to syslog tcp server.
* It takes its context from appctx->svcctx.
*/
static void sink_forward_io_handler(struct appctx *appctx)
{
struct stconn *sc = appctx_sc(appctx);
struct sink_forward_target *sft = appctx->svcctx;
struct sink *sink = sft->sink;
struct ring *ring = sink->ctx.ring;
struct buffer *buf = &ring->buf;
uint64_t msg_len;
size_t len, cnt, ofs, last_ofs;
int ret = 0;
if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW))))
goto out;
/* if stopping was requested, close immediately */
if (unlikely(stopping))
goto close;
/* if the connection is not established, inform the stream that we want
* to be notified whenever the connection completes.
*/
if (sc_opposite(sc)->state < SC_ST_EST) {
applet_need_more_data(appctx);
se_need_remote_conn(appctx->sedesc);
applet_have_more_data(appctx);
goto out;
}
HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
if (appctx != sft->appctx) {
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
goto close;
}
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
LIST_DEL_INIT(&appctx->wait_entry);
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
HA_RWLOCK_RDLOCK(RING_LOCK, &ring->lock);
/* explanation for the initialization below: it would be better to do
* this in the parsing function but this would occasionally result in
* dropped events because we'd take a reference on the oldest message
* and keep it while being scheduled. Thus instead let's take it the
* first time we enter here so that we have a chance to pass many
* existing messages before grabbing a reference to a location. This
* value cannot be produced after initialization.
*/
if (unlikely(sft->ofs == ~0)) {
sft->ofs = b_peek_ofs(buf, 0);
HA_ATOMIC_INC(b_orig(buf) + sft->ofs);
}
/* we were already there, adjust the offset to be relative to
* the buffer's head and remove us from the counter.
*/
ofs = sft->ofs - b_head_ofs(buf);
if (sft->ofs < b_head_ofs(buf))
ofs += b_size(buf);
BUG_ON(ofs >= buf->size);
HA_ATOMIC_DEC(b_peek(buf, ofs));
/* in this loop, ofs always points to the counter byte that precedes
* the message so that we can take our reference there if we have to
* stop before the end (ret=0).
*/
ret = 1;
while (ofs + 1 < b_data(buf)) {
cnt = 1;
len = b_peek_varint(buf, ofs + cnt, &msg_len);
if (!len)
break;
cnt += len;
BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
if (unlikely(msg_len + 1 > b_size(&trash))) {
/* too large a message to ever fit, let's skip it */
ofs += cnt + msg_len;
continue;
}
chunk_reset(&trash);
len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
trash.data += len;
trash.area[trash.data++] = '\n';
if (applet_putchk(appctx, &trash) == -1) {
ret = 0;
break;
}
ofs += cnt + msg_len;
}
HA_ATOMIC_INC(b_peek(buf, ofs));
last_ofs = b_tail_ofs(buf);
sft->ofs = b_peek_ofs(buf, ofs);
HA_RWLOCK_RDUNLOCK(RING_LOCK, &ring->lock);
if (ret) {
/* let's be woken up once new data arrive */
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
ofs = b_tail_ofs(buf);
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
if (ofs != last_ofs) {
/* more data was added into the ring between the
* unlock and the lock, and the writer might not
* have seen us. We need to reschedule a read.
*/
applet_have_more_data(appctx);
} else
applet_have_no_more_data(appctx);
}
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
out:
/* always drain data from server */
co_skip(sc_oc(sc), sc_oc(sc)->output);
return;
close:
se_fl_set(appctx->sedesc, SE_FL_EOS|SE_FL_EOI);
}
/*
* IO Handler to handle message push to syslog tcp server
* using octet counting frames
* It takes its context from appctx->svcctx.
*/
static void sink_forward_oc_io_handler(struct appctx *appctx)
{
struct stconn *sc = appctx_sc(appctx);
struct sink_forward_target *sft = appctx->svcctx;
struct sink *sink = sft->sink;
struct ring *ring = sink->ctx.ring;
struct buffer *buf = &ring->buf;
uint64_t msg_len;
size_t len, cnt, ofs;
int ret = 0;
char *p;
if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW))))
goto out;
/* if stopping was requested, close immediately */
if (unlikely(stopping))
goto close;
/* if the connection is not established, inform the stream that we want
* to be notified whenever the connection completes.
*/
if (sc_opposite(sc)->state < SC_ST_EST) {
applet_need_more_data(appctx);
se_need_remote_conn(appctx->sedesc);
applet_have_more_data(appctx);
goto out;
}
HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
if (appctx != sft->appctx) {
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
goto close;
}
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
LIST_DEL_INIT(&appctx->wait_entry);
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
HA_RWLOCK_RDLOCK(RING_LOCK, &ring->lock);
/* explanation for the initialization below: it would be better to do
* this in the parsing function but this would occasionally result in
* dropped events because we'd take a reference on the oldest message
* and keep it while being scheduled. Thus instead let's take it the
* first time we enter here so that we have a chance to pass many
* existing messages before grabbing a reference to a location. This
* value cannot be produced after initialization.
*/
if (unlikely(sft->ofs == ~0)) {
sft->ofs = b_peek_ofs(buf, 0);
HA_ATOMIC_INC(b_orig(buf) + sft->ofs);
}
/* we were already there, adjust the offset to be relative to
* the buffer's head and remove us from the counter.
*/
ofs = sft->ofs - b_head_ofs(buf);
if (sft->ofs < b_head_ofs(buf))
ofs += b_size(buf);
BUG_ON(ofs >= buf->size);
HA_ATOMIC_DEC(b_peek(buf, ofs));
/* in this loop, ofs always points to the counter byte that precedes
* the message so that we can take our reference there if we have to
* stop before the end (ret=0).
*/
ret = 1;
while (ofs + 1 < b_data(buf)) {
cnt = 1;
len = b_peek_varint(buf, ofs + cnt, &msg_len);
if (!len)
break;
cnt += len;
BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
chunk_reset(&trash);
p = ulltoa(msg_len, trash.area, b_size(&trash));
if (p) {
trash.data = (p - trash.area) + 1;
*p = ' ';
}
if (!p || (trash.data + msg_len > b_size(&trash))) {
/* too large a message to ever fit, let's skip it */
ofs += cnt + msg_len;
continue;
}
trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt);
if (applet_putchk(appctx, &trash) == -1) {
ret = 0;
break;
}
ofs += cnt + msg_len;
}
HA_ATOMIC_INC(b_peek(buf, ofs));
sft->ofs = b_peek_ofs(buf, ofs);
HA_RWLOCK_RDUNLOCK(RING_LOCK, &ring->lock);
if (ret) {
/* let's be woken up once new data arrive */
HA_RWLOCK_WRLOCK(RING_LOCK, &ring->lock);
LIST_APPEND(&ring->waiters, &appctx->wait_entry);
HA_RWLOCK_WRUNLOCK(RING_LOCK, &ring->lock);
applet_have_no_more_data(appctx);
}
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
out:
/* always drain data from server */
co_skip(sc_oc(sc), sc_oc(sc)->output);
return;
close:
se_fl_set(appctx->sedesc, SE_FL_EOS|SE_FL_EOI);
goto out;
}
void __sink_forward_session_deinit(struct sink_forward_target *sft)
{
struct sink *sink;
sink = sft->sink;
if (!sink)
return;
HA_RWLOCK_WRLOCK(RING_LOCK, &sink->ctx.ring->lock);
LIST_DEL_INIT(&sft->appctx->wait_entry);
HA_RWLOCK_WRUNLOCK(RING_LOCK, &sink->ctx.ring->lock);
sft->appctx = NULL;
task_wakeup(sink->forward_task, TASK_WOKEN_MSG);
}
static int sink_forward_session_init(struct appctx *appctx)
{
struct sink_forward_target *sft = appctx->svcctx;
struct stream *s;
struct sockaddr_storage *addr = NULL;
if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr)))
goto out_error;
/* srv port should be learned from srv->svc_port not from srv->addr */
set_host_port(addr, sft->srv->svc_port);
if (appctx_finalize_startup(appctx, sft->srv->proxy, &BUF_NULL) == -1)
goto out_free_addr;
s = appctx_strm(appctx);
s->scb->dst = addr;
s->scb->flags |= (SC_FL_RCV_ONCE|SC_FL_NOLINGER);
s->target = &sft->srv->obj_type;
s->flags = SF_ASSIGNED;
s->do_log = NULL;
s->uniq_id = 0;
applet_expect_no_data(appctx);
sft->appctx = appctx;
return 0;
out_free_addr:
sockaddr_free(&addr);
out_error:
return -1;
}
static void sink_forward_session_release(struct appctx *appctx)
{
struct sink_forward_target *sft = appctx->svcctx;
if (!sft)
return;
HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
if (sft->appctx == appctx)
__sink_forward_session_deinit(sft);
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
}
static struct applet sink_forward_applet = {
.obj_type = OBJ_TYPE_APPLET,
.name = "<SINKFWD>", /* used for logging */
.fct = sink_forward_io_handler,
.init = sink_forward_session_init,
.release = sink_forward_session_release,
};
static struct applet sink_forward_oc_applet = {
.obj_type = OBJ_TYPE_APPLET,
.name = "<SINKFWDOC>", /* used for logging */
.fct = sink_forward_oc_io_handler,
.init = sink_forward_session_init,
.release = sink_forward_session_release,
};
/*
* Create a new peer session in assigned state (connect will start automatically)
* It sets its context into appctx->svcctx.
*/
static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft)
{
struct appctx *appctx;
struct applet *applet = &sink_forward_applet;
if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING)
applet = &sink_forward_oc_applet;
appctx = appctx_new_here(applet, NULL);
if (!appctx)
goto out_close;
appctx->svcctx = (void *)sft;
if (appctx_init(appctx) == -1)
goto out_free_appctx;
return appctx;
/* Error unrolling */
out_free_appctx:
appctx_free_on_early_error(appctx);
out_close:
return NULL;
}
/*
* Task to handle connections to forward servers
*/
static struct task *process_sink_forward(struct task * task, void *context, unsigned int state)
{
struct sink *sink = (struct sink *)context;
struct sink_forward_target *sft = sink->sft;
task->expire = TICK_ETERNITY;
if (!stopping) {
while (sft) {
HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
/* if appctx is NULL, start a new session */
if (!sft->appctx)
sft->appctx = sink_forward_session_create(sink, sft);
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
sft = sft->next;
}
}
else {
while (sft) {
HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
/* awake applet to perform a clean close */
if (sft->appctx)
appctx_wakeup(sft->appctx);
HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
sft = sft->next;
}
}
return task;
}
/*
* Init task to manage connections to forward servers
*
* returns 0 in case of error.
*/
int sink_init_forward(struct sink *sink)
{
sink->forward_task = task_new_anywhere();
if (!sink->forward_task)
return 0;
sink->forward_task->process = process_sink_forward;
sink->forward_task->context = (void *)sink;
sink->forward_sighandler = signal_register_task(0, sink->forward_task, 0);
task_wakeup(sink->forward_task, TASK_WOKEN_INIT);
return 1;
}
/* This tries to rotate a file-backed ring, but only if it contains contents.
* This way empty rings will not cause backups to be overwritten and it's safe
* to reload multiple times. That's only best effort, failures are silently
* ignored.
*/
void sink_rotate_file_backed_ring(const char *name)
{
struct ring ring;
char *oldback;
int ret;
int fd;
fd = open(name, O_RDONLY);
if (fd < 0)
return;
/* check for contents validity */
ret = read(fd, &ring, sizeof(ring));
close(fd);
if (ret != sizeof(ring))
goto rotate;
/* contents are present, we want to keep them => rotate. Note that
* an empty ring buffer has one byte (the marker).
*/
if (ring.buf.data > 1)
goto rotate;
/* nothing to keep, let's scratch the file and preserve the backup */
return;
rotate:
oldback = NULL;
memprintf(&oldback, "%s.bak", name);
if (oldback) {
/* try to rename any possibly existing ring file to
* ".bak" and delete remains of older ones. This will
* ensure we don't wipe useful debug info upon restart.
*/
unlink(oldback);
if (rename(name, oldback) < 0)
unlink(oldback);
ha_free(&oldback);
}
}
/* helper function to completely deallocate a sink struct
*/
static void sink_free(struct sink *sink)
{
struct sink_forward_target *sft_next;
if (!sink)
return;
if (sink->type == SINK_TYPE_BUFFER) {
if (sink->store) {
size_t size = (sink->ctx.ring->buf.size + 4095UL) & -4096UL;
void *area = (sink->ctx.ring->buf.area - sizeof(*sink->ctx.ring));
msync(area, size, MS_SYNC);
munmap(area, size);
ha_free(&sink->store);
}
else
ring_free(sink->ctx.ring);
}
LIST_DEL_INIT(&sink->sink_list); // remove from parent list
task_destroy(sink->forward_task);
free_proxy(sink->forward_px);
ha_free(&sink->name);
ha_free(&sink->desc);
while (sink->sft) {
sft_next = sink->sft->next;
ha_free(&sink->sft);
sink->sft = sft_next;
}
ha_free(&sink);
}
/* Helper function to create new high-level ring buffer (as in ring section from
* the config): will create a new sink of buf type, and a new forward proxy,
* which will be stored in forward_px to know that the sink is responsible for
* it.
*
* Returns NULL on failure
*/
static struct sink *sink_new_ringbuf(const char *id, const char *description,
const char *file, int linenum, char **err_msg)
{
struct sink *sink;
struct proxy *p = NULL; // forward_px
/* allocate new proxy to handle forwards */
p = calloc(1, sizeof(*p));
if (!p) {
memprintf(err_msg, "out of memory");
goto err;
}
init_new_proxy(p);
sink_setup_proxy(p);
p->id = strdup(id);
p->conf.args.file = p->conf.file = strdup(file);
p->conf.args.line = p->conf.line = linenum;
sink = sink_new_buf(id, description, LOG_FORMAT_RAW, BUFSIZE);
if (!sink) {
memprintf(err_msg, "unable to create a new sink buffer for ring '%s'", id);
goto err;
}
/* link sink to proxy */
sink->forward_px = p;
return sink;
err:
free_proxy(p);
return NULL;
}
/* helper function: add a new server to an existing sink
*
* Returns 1 on success and 0 on failure
*/
static int sink_add_srv(struct sink *sink, struct server *srv)
{
struct sink_forward_target *sft;
/* allocate new sink_forward_target descriptor */
sft = calloc(1, sizeof(*sft));
if (!sft) {
ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n", srv->id, sink->name);
return 0;
}
sft->srv = srv;
sft->appctx = NULL;
sft->ofs = ~0; /* init ring offset */
sft->sink = sink;
sft->next = sink->sft;
HA_SPIN_INIT(&sft->lock);
/* mark server attached to the ring */
if (!ring_attach(sink->ctx.ring)) {
ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, sink->name);
ha_free(&sft);
return 0;
}
sink->sft = sft;
return 1;
}
/* Finalize sink struct to ensure configuration consistency and
* allocate final struct members
*
* Returns ERR_NONE on success, ERR_WARN on warning
* Returns a composition of ERR_ALERT, ERR_ABORT, ERR_FATAL on failure
*/
static int sink_finalize(struct sink *sink)
{
int err_code = ERR_NONE;
struct server *srv;
if (sink && (sink->type == SINK_TYPE_BUFFER)) {
if (!sink->maxlen)
sink->maxlen = ~0; // maxlen not set: no implicit truncation
else if (sink->maxlen > ring_max_payload(sink->ctx.ring)) {
/* maxlen set by user however it doesn't fit: set to max value */
ha_warning("ring '%s' event max length '%u' exceeds max payload size, forced to '%lu'.\n",
sink->name, sink->maxlen, (unsigned long)ring_max_payload(sink->ctx.ring));
sink->maxlen = ring_max_payload(sink->ctx.ring);
err_code |= ERR_WARN;
}
/* prepare forward server descriptors */
if (sink->forward_px) {
/* sink proxy is set: register all servers from the proxy */
srv = sink->forward_px->srv;
while (srv) {
if (!sink_add_srv(sink, srv)) {
err_code |= ERR_ALERT | ERR_FATAL;
break;
}
srv = srv->next;
}
}
/* init forwarding if at least one sft is registered */
if (sink->sft && sink_init_forward(sink) == 0) {
ha_alert("error when trying to initialize sink buffer forwarding.\n");
err_code |= ERR_ALERT | ERR_FATAL;
}
}
return err_code;
}
/*
* Parse "ring" section and create corresponding sink buffer.
*
* The function returns 0 in success case, otherwise, it returns error
* flags.
*/
int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
{
int err_code = 0;
char *err_msg = NULL;
const char *inv;
if (strcmp(args[0], "ring") == 0) { /* new ring section */
if (!*args[1]) {
ha_alert("parsing [%s:%d] : missing ring name.\n", file, linenum);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
inv = invalid_char(args[1]);
if (inv) {
ha_alert("parsing [%s:%d] : invalid ring name '%s' (character '%c' is not permitted).\n", file, linenum, args[1], *inv);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (sink_find(args[1])) {
ha_alert("parsing [%s:%d] : sink named '%s' already exists.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
cfg_sink = sink_new_ringbuf(args[1], args[1], file, linenum, &err_msg);
if (!cfg_sink) {
ha_alert("parsing [%s:%d] : %s.\n", file, linenum, err_msg);
ha_free(&err_msg);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
/* set maxlen value to 0 for now, we rely on this in postparsing
* to know if it was explicitly set using the "maxlen" parameter
*/
cfg_sink->maxlen = 0;
}
else if (strcmp(args[0], "size") == 0) {
size_t size;
if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)) {
ha_alert("parsing [%s:%d] : 'size' directive not usable with this type of sink.\n", file, linenum);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
size = atol(args[1]);
if (!size) {
ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (cfg_sink->store) {
ha_alert("parsing [%s:%d] : cannot resize an already mapped file, please specify 'size' before 'backing-file'.\n", file, linenum);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (size < cfg_sink->ctx.ring->buf.size) {
ha_warning("parsing [%s:%d] : ignoring new size '%llu' that is smaller than current size '%llu' for ring '%s'.\n",
file, linenum, (ullong)size, (ullong)cfg_sink->ctx.ring->buf.size, cfg_sink->name);
err_code |= ERR_WARN;
goto err;
}
if (!ring_resize(cfg_sink->ctx.ring, size)) {
ha_alert("parsing [%s:%d] : fail to set sink buffer size '%llu' for ring '%s'.\n", file, linenum,
(ullong)cfg_sink->ctx.ring->buf.size, cfg_sink->name);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
}
else if (strcmp(args[0], "backing-file") == 0) {
/* This tries to mmap file <file> for size <size> and to use it as a backing store
* for ring <ring>. Existing data are delete. NULL is returned on error.
*/
const char *backing = args[1];
size_t size;
void *area;
int fd;
if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)) {
ha_alert("parsing [%s:%d] : 'backing-file' only usable with existing rings.\n", file, linenum);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (cfg_sink->store) {
ha_alert("parsing [%s:%d] : 'backing-file' already specified for ring '%s' (was '%s').\n", file, linenum, cfg_sink->name, cfg_sink->store);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
/* let's check if the file exists and is not empty. That's the
* only condition under which we'll trigger a rotate, so that
* config checks, reloads, or restarts that don't emit anything
* do not rotate it again.
*/
sink_rotate_file_backed_ring(backing);
fd = open(backing, O_RDWR | O_CREAT, 0600);
if (fd < 0) {
ha_alert("parsing [%s:%d] : cannot open backing-file '%s' for ring '%s': %s.\n", file, linenum, backing, cfg_sink->name, strerror(errno));
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
size = (cfg_sink->ctx.ring->buf.size + 4095UL) & -4096UL;
if (ftruncate(fd, size) != 0) {
close(fd);
ha_alert("parsing [%s:%d] : could not adjust size of backing-file for ring '%s': %s.\n", file, linenum, cfg_sink->name, strerror(errno));
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (area == MAP_FAILED) {
close(fd);
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));
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
/* we don't need the file anymore */
close(fd);
cfg_sink->store = strdup(backing);
/* never fails */
ring_free(cfg_sink->ctx.ring);
cfg_sink->ctx.ring = ring_make_from_area(area, size);
}
else if (strcmp(args[0],"server") == 0) {
if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)) {
ha_alert("parsing [%s:%d] : unable to create server '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL,
SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
}
else if (strcmp(args[0],"timeout") == 0) {
if (!cfg_sink || !cfg_sink->forward_px) {
ha_alert("parsing [%s:%d] : unable to set timeout '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (strcmp(args[1], "connect") == 0 ||
strcmp(args[1], "server") == 0) {
const char *res;
unsigned int tout;
if (!*args[2]) {
ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
file, linenum, args[0], args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
if (res == PARSE_TIME_OVER) {
ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
file, linenum, args[2], args[0], args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
else if (res == PARSE_TIME_UNDER) {
ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
file, linenum, args[2], args[0], args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
else if (res) {
ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
file, linenum, *res, args[0], args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (args[1][0] == 'c')
cfg_sink->forward_px->timeout.connect = tout;
else
cfg_sink->forward_px->timeout.server = tout;
}
}
else if (strcmp(args[0],"format") == 0) {
if (!cfg_sink) {
ha_alert("parsing [%s:%d] : unable to set format '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
cfg_sink->fmt = get_log_format(args[1]);
if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) {
ha_alert("parsing [%s:%d] : unknown format '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
}
else if (strcmp(args[0],"maxlen") == 0) {
if (!cfg_sink) {
ha_alert("parsing [%s:%d] : unable to set event max length '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
cfg_sink->maxlen = atol(args[1]);
if (!cfg_sink->maxlen) {
ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
}
else if (strcmp(args[0],"description") == 0) {
if (!cfg_sink) {
ha_alert("parsing [%s:%d] : unable to set description '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
if (!*args[1]) {
ha_alert("parsing [%s:%d] : missing ring description text.\n", file, linenum);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
free(cfg_sink->desc);
cfg_sink->desc = strdup(args[1]);
if (!cfg_sink->desc) {
ha_alert("parsing [%s:%d] : fail to set description '%s'.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
}
else {
ha_alert("parsing [%s:%d] : unknown statement '%s'.\n", file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL;
goto err;
}
err:
return err_code;
}
/* Creates a new sink buffer from a logger.
*
* It uses the logger's address to declare a forward
* server for this buffer. And it initializes the
* forwarding.
*
* The function returns a pointer on the
* allocated struct sink if allocate
* and initialize succeed, else if it fails
* it returns NULL.
*
* Note: the sink is created using the name
* specified into logger->target.ring_name
*/
struct sink *sink_new_from_logger(struct logger *logger)
{
struct sink *sink = NULL;
struct server *srv = NULL;
char *err_msg = NULL;
/* prepare description for the sink */
chunk_reset(&trash);
chunk_printf(&trash, "created from log directive declared into '%s' at line %d", logger->conf.file, logger->conf.line);
/* allocate a new sink buffer */
sink = sink_new_ringbuf(logger->target.ring_name, trash.area, logger->conf.file, logger->conf.line, &err_msg);
if (!sink) {
ha_alert("%s.\n", err_msg);
ha_free(&err_msg);
goto error;
}
/* ring format normally defaults to RAW, but here we set ring format
* to UNSPEC to inherit from caller format in sink_write() since we
* cannot customize implicit ring settings
*/
sink->fmt = LOG_FORMAT_UNSPEC;
/* for the same reason, we disable sink->maxlen to inherit from caller
* maxlen in sink_write()
*/
sink->maxlen = 0;
/* Set default connect and server timeout for sink forward proxy */
sink->forward_px->timeout.connect = MS_TO_TICKS(1000);
sink->forward_px->timeout.server = MS_TO_TICKS(5000);
/* allocate a new server to forward messages
* from ring buffer
*/
srv = new_server(sink->forward_px);
if (!srv)
goto error;
/* init server */
srv->id = strdup(logger->target.ring_name);
srv->conf.file = strdup(logger->conf.file);
srv->conf.line = logger->conf.line;
srv->addr = *logger->target.addr;
srv->svc_port = get_host_port(logger->target.addr);
HA_SPIN_INIT(&srv->lock);
/* process per thread init */
if (srv_init_per_thr(srv) == -1)
goto error;
/* link srv with sink forward proxy: the servers are linked
* backwards first into proxy
*/
srv->next = sink->forward_px->srv;
sink->forward_px->srv = srv;
if (sink_finalize(sink) & ERR_CODE)
goto error_final;
return sink;
error:
srv_drop(srv);
error_final:
sink_free(sink);
return NULL;
}
/* This function is pretty similar to sink_from_logger():
* But instead of creating a forward proxy and server from a logger struct
* it uses already existing srv to create the forwarding sink, so most of
* the initialization is bypassed.
*
* The function returns a pointer on the
* allocated struct sink if allocate
* and initialize succeed, else if it fails
* it returns NULL.
*
* <from> allows to specify a string that will be inserted into the sink
* description to describe where it was created from.
* Note: the sink is created using the name
* specified into srv->id
*/
struct sink *sink_new_from_srv(struct server *srv, const char *from)
{
struct sink *sink = NULL;
int bufsize = (srv->log_bufsize) ? srv->log_bufsize : BUFSIZE;
/* prepare description for the sink */
chunk_reset(&trash);
chunk_printf(&trash, "created from %s declared into '%s' at line %d", from, srv->conf.file, srv->conf.line);
/* directly create a sink of BUF type, and use UNSPEC log format to
* inherit from caller fmt in sink_write()
*/
sink = sink_new_buf(srv->id, trash.area, LOG_FORMAT_UNSPEC, bufsize);
if (!sink) {
ha_alert("unable to create a new sink buffer for server '%s'.\n", srv->id);
goto error;
}
/* we disable sink->maxlen to inherit from caller
* maxlen in sink_write()
*/
sink->maxlen = 0;
/* add server to sink */
if (!sink_add_srv(sink, srv))
goto error;
if (sink_finalize(sink) & ERR_CODE)
goto error;
return sink;
error:
sink_free(sink);
return NULL;
}
/*
* Post parsing "ring" section.
*
* The function returns 0 in success case, otherwise, it returns error
* flags.
*/
int cfg_post_parse_ring()
{
int err_code;
err_code = sink_finalize(cfg_sink);
cfg_sink = NULL;
return err_code;
}
/* function: resolve a single logger target of BUFFER type
*
* Returns err_code which defaults to ERR_NONE and can be set to a combination
* of ERR_WARN, ERR_ALERT, ERR_FATAL and ERR_ABORT in case of errors.
* <msg> could be set at any time (it will usually be set on error, but
* could also be set when no error occurred to report a diag warning), thus is
* up to the caller to check it and to free it.
*/
int sink_resolve_logger_buffer(struct logger *logger, char **msg)
{
struct log_target *target = &logger->target;
int err_code = ERR_NONE;
struct sink *sink;
BUG_ON(target->type != LOG_TARGET_BUFFER || (target->flags & LOG_TARGET_FL_RESOLVED));
if (target->addr) {
sink = sink_new_from_logger(logger);
if (!sink) {
memprintf(msg, "cannot be initialized (failed to create implicit ring)");
err_code |= ERR_ALERT | ERR_FATAL;
goto end;
}
ha_free(&target->addr); /* we no longer need this */
}
else {
sink = sink_find(target->ring_name);
if (!sink) {
memprintf(msg, "uses unknown ring named '%s'", target->ring_name);
err_code |= ERR_ALERT | ERR_FATAL;
goto end;
}
else if (sink->type != SINK_TYPE_BUFFER) {
memprintf(msg, "uses incompatible ring '%s'", target->ring_name);
err_code |= ERR_ALERT | ERR_FATAL;
goto end;
}
}
/* consistency checks */
if (sink && logger->maxlen > ring_max_payload(sink->ctx.ring)) {
memprintf(msg, "uses a max length which exceeds ring capacity ('%s' supports %lu bytes at most)",
target->ring_name, (unsigned long)ring_max_payload(sink->ctx.ring));
}
else if (sink && logger->maxlen > sink->maxlen) {
memprintf(msg, "uses a ring with a smaller maxlen than the one specified on the log directive ('%s' has maxlen = %d), logs will be truncated according to the lowest maxlen between the two",
target->ring_name, sink->maxlen);
}
end:
ha_free(&target->ring_name); /* sink is resolved and will replace ring_name hint */
target->sink = sink;
return err_code;
}
static void sink_init()
{
sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1);
sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2);
sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576);
}
static void sink_deinit()
{
struct sink *sink, *sb;
list_for_each_entry_safe(sink, sb, &sink_list, sink_list)
sink_free(sink);
}
INITCALL0(STG_REGISTER, sink_init);
REGISTER_POST_DEINIT(sink_deinit);
static struct cli_kw_list cli_kws = {{ },{
{ { "show", "events", NULL }, "show events [<sink>] [-w] [-n] : show event sink state", cli_parse_show_events, NULL, NULL },
{{},}
}};
INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
/* config parsers for this section */
REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring);
/*
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* End:
*/
|