forked from laurenz/pgreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
1634 lines (1458 loc) · 43.9 KB
/
parse.c
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
#include "pgreplay.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
/* long enough to contain the beginning of a log line */
#define BUFLEN 1024
/* separates log line entries */
#define SEPCHAR '|'
/* types of log entries */
typedef enum {
log_debug5,
log_debug4,
log_debug3,
log_debug2,
log_debug1,
log_info,
log_notice,
log_warning,
log_error,
log_log,
log_fatal,
log_panic,
log_unknown
} log_type;
/* type for functions parsing the next log entry */
typedef int (parse_log_entry_func)(struct timeval *, char *, char *, uint64_t *, log_type *, char **, char **);
/* functions for parsing stderr and CSV log entries */
static parse_log_entry_func parse_errlog_entry;
static parse_log_entry_func parse_csvlog_entry;
static parse_log_entry_func * const parse_log_entry[2] = {
&parse_errlog_entry,
&parse_csvlog_entry
};
/* used to remember prepared statements */
struct prep_stmt {
char *name;
struct prep_stmt *next;
};
/* used to remember "open" connections */
struct connection {
uint64_t session_id;
struct connection *next;
struct prep_stmt *statements;
};
/* hash structure for "open" connections */
static struct connection * open_conn[256] = { NULL };
/* indicates whether we are parsing a CSV file */
static int csv;
/* start and end timestamp for parsing log entries */
static const char *start_time, *end_time;
/* database and username filters for parsing log entries */
static const char *database_only, *username_only;
/* file which we parse */
static int infile;
/* line number for error messages */
static unsigned long lineno = 0;
/* offset for time values (what mktime(3) makes of 2000-01-01 00:00:00)
used to make timestamps independent of local time and broken mktime */
static time_t epoch;
/* time of the first and last statement that we parse */
static struct timeval first_stmt_time, last_stmt_time;
/* statistics */
static unsigned long stat_simple = 0; /* simple statements */
static unsigned long stat_copy = 0; /* copy statements */
static unsigned long stat_param = 0; /* parametrized statements */
static unsigned long stat_named = 0; /* different named statements */
static unsigned long stat_execnamed = 0; /* named statement executions */
static unsigned long stat_fastpath = 0; /* fast-path function calls */
static unsigned long stat_cancel = 0; /* cancel requests */
/* a version of strcpy that handles overlapping strings well */
static char *overlap_strcpy(char *dest, const char *src) {
register char c;
while((c = *(src++))) {
*(dest++) = c;
}
*dest = '\0';
return dest;
}
/* convert a string to a log_type */
static log_type to_log_type(const char* s) {
/* compare in order of expected likelyhood for performance */
if (! strcmp(s, "LOG")) {
return log_log;
} else if (! strcmp(s, "ERROR")) {
return log_error;
} else if (! strcmp(s, "STATEMENT")) {
return log_unknown;
} else if (! strcmp(s, "DETAIL")) {
return log_unknown;
} else if (! strcmp(s, "HINT")) {
return log_unknown;
} else if (! strcmp(s, "FATAL")) {
return log_fatal;
} else if (! strcmp(s, "WARNING")) {
return log_warning;
} else if (! strcmp(s, "NOTICE")) {
return log_notice;
} else if (! strcmp(s, "INFO")) {
return log_info;
} else if (! strcmp(s, "PANIC")) {
return log_panic;
} else if (! strcmp(s, "DEBUG1")) {
return log_debug1;
} else if (! strcmp(s, "DEBUG2")) {
return log_debug2;
} else if (! strcmp(s, "DEBUG3")) {
return log_debug3;
} else if (! strcmp(s, "DEBUG4")) {
return log_debug4;
} else if (! strcmp(s, "DEBUG5")) {
return log_debug5;
} else {
return log_unknown;
}
}
/* Parses a timestamp (ignoring the time zone part).
If "dest" is not null, the parsed time will be returned there.
Return value is NULL on success, else an error message */
const char * parse_time(const char *source, struct timeval *dest) {
int i;
static struct tm tm; /* initialize with zeros */
char s[24] = { '\0' }; /* modifiable copy of source */
static char errmsg[BUFLEN];
/* format of timestamp part */
static const char format[]="nnnn-nn-nn nn:nn:nn.nnn";
/* check timestamp for validity */
if (!source) {
strcpy(errmsg, "NULL passed as timestamp string");
return errmsg;
}
if (strlen(source) < strlen(format)) {
sprintf(errmsg, "timestamp string is less than %lu characters long", (unsigned long)strlen(format));
return errmsg;
}
if (strlen(source) >= BUFLEN) {
sprintf(errmsg, "timestamp string is more than %d characters long", BUFLEN-1);
return errmsg;
}
for (i=0; i<strlen(format); ++i) {
switch (format[i]) {
case 'n':
if ((source[i] < '0') || (source[i] > '9')) {
sprintf(errmsg, "character %d in timestamp string is '%c', expected digit", i+1, source[i]);
return errmsg;
} else
s[i] = source[i];
break;
default:
if (source[i] != format[i]) {
sprintf(errmsg, "character %d in timestamp string is '%c', expected '%c'", i+1, source[i], format[i]);
return errmsg;
} else
s[i] = '\0'; /* tokenize parts */
}
}
/* parse time into 'tm' */
tm.tm_year = atoi(s) - 1900;
tm.tm_mon = atoi(s + 5) - 1;
tm.tm_mday = atoi(s + 8);
tm.tm_hour = atoi(s + 11);
tm.tm_min = atoi(s + 14);
tm.tm_sec = atoi(s + 17);
tm.tm_isdst = 0; /* ignore daylight savings time */
if (dest) {
dest->tv_sec = mktime(&tm) - epoch;
dest->tv_usec = atoi(s + 20) * 1000;
}
return NULL;
}
static char * parse_session(const char *source, uint64_t *dest) {
char s[BUFLEN]; /* modifiable copy of source */
static char errmsg[BUFLEN];
char *s1 = NULL, c;
uint32_t part1, part2;
int i;
/* check input for validity */
if (!source) {
strcpy(errmsg, "NULL passed as session id string");
return errmsg;
}
if (strlen(source) > BUFLEN -1) {
sprintf(errmsg, "session id string is more than %d characters long", BUFLEN);
return errmsg;
}
for (i=0; i<=strlen(source); ++i) {
c = source[i];
if (('.' == c) && (! s1)) {
s[i] = '\0';
s1 = s + i + 1;
} else if (((c < '0') || (c > '9')) && ((c < 'a') || (c > 'f')) && ('\0' != c)) {
sprintf(errmsg, "character %d in session id string is '%c', expected hex digit", i+1, c);
return errmsg;
} else
s[i] = c;
}
if (! s1) {
strcpy(errmsg, "Missing \".\" in session id string");
return errmsg;
}
if ((strlen(s) > 8) || (strlen(s1) > 8)) {
strcpy(errmsg, "none of the parts of a session id string may be longer than 8 hex digits");
return errmsg;
}
/* convert both parts */
sscanf(s, UINT32_FORMAT, &part1);
sscanf(s1, UINT32_FORMAT, &part2);
*dest = (((uint64_t)part1) << 32) + part2;
return NULL;
}
/* reads one log entry from the input file
the result is a malloc'ed string that must be freed
a return value of NULL means that there was an error */
static char * read_log_line() {
char *line, buf[BUFLEN] = { '\0' }, *p;
int len, escaped = 0, nl_found = 0, line_size = 0, i, l;
ssize_t bytes_read;
/* this will contain stuff we have read from the file but not used yet */
static char peekbuf[BUFLEN] = { '\0' };
static int peeklen = 0;
debug(3, "Entering read_log_line, current line number %lu\n", lineno+1);
/* pre-allocate the result to length 1 */
if (NULL == (line = malloc(1))) {
fprintf(stderr, "Cannot allocate 1 byte of memory\n");
return NULL;
}
*line = '\0';
while (! nl_found) {
/* if there were any chars left from the last invokation, use them first */
len = peeklen;
if (len) {
strcpy(buf, peekbuf);
peekbuf[0] = '\0';
}
peeklen = 0;
/* read from file until buf is full (at most) */
if (len < BUFLEN - 1) {
if (-1 == (bytes_read = read(infile, buf + len, BUFLEN - 1 - len))) {
perror("Error reading from input file");
return NULL;
}
len += bytes_read;
buf[len] = '\0';
}
/* if there is still nothing, we're done */
if (0 == len) {
debug(2, "Encountered EOF%s\n", "");
debug(3, "Leaving read_log_line%s\n", "");
return line;
}
/* search the string for unescaped newlines */
for (p=buf; *p!='\0'; ++p) {
if (csv && ('"' == *p)) {
escaped = !escaped;
}
/* keep up with line count */
lineno += ('\n' == *p);
/* look for unescaped newline */
if (!escaped && ('\n' == *p)) {
/* if a newline is found, truncate the string
and prepend the rest to peekbuf */
l = len - (++p - buf);
/* right shift peekbuf by l */
for (i=peeklen; i>=0; --i) {
peekbuf[l+i] = peekbuf[i];
}
strncpy(peekbuf, p, l);
*p = '\0';
peeklen += len - (p - buf);
len = p - buf;
if (csv) {
/* for a CSV file, this must be the end of the log entry */
nl_found = 1;
break; /* out from the for loop */
} else {
/* in a stderr log file, we must check for a
continuation line (newline + tab) */
/* first, make sure there is something to peek at */
if (0 == peeklen) {
/* try to read one more byte from the file */
if (-1 == (bytes_read = read(infile, peekbuf, 1))) {
perror("Error reading from input file");
return NULL;
}
if (0 == bytes_read) {
/* EOF means end of log entry */
nl_found = 1;
break; /* out from the for loop */
} else {
peeklen = bytes_read;
peekbuf[peeklen] = '\0';
}
}
/* then check for a continuation tab */
if ('\t' == *peekbuf) {
/* continuation line, remove tab
and copy peekbuf back to buf */
strncpy(p--, peekbuf + 1, BUFLEN - 1 - len);
if (peeklen > BUFLEN - len) {
overlap_strcpy(peekbuf, peekbuf + (BUFLEN - len));
peeklen = peeklen - BUFLEN + len;
len = BUFLEN - 1;
} else {
*peekbuf = '\0';
len += peeklen - 1;
peeklen = 0;
}
buf[len] = '\0';
} else {
/* end of log entry reached */
nl_found = 1;
break; /* out from the for loop */
}
}
}
}
/* extend result line and append buf */
line_size += len;
if (NULL == (p = realloc(line, line_size+1))) {
fprintf(stderr, "Cannot allocate %d bytes of memory\n", line_size);
free(line); line = NULL;
return NULL;
}
line = p;
strcat(line, buf);
*buf = '\0';
len = 0;
}
/* remove trailing newline in result if present */
if ('\n' == line[line_size - 1]) {
line[line_size - 1] = '\0';
}
debug(3, "Leaving read_log_line%s\n", "");
return line;
}
/* parses the next stderr log entry (and maybe a detail message after that)
timestamp, user, database, session ID, log message type, log message
and detail message are returned in the respective parameters
"message" and "detail" are malloc'ed if they are not NULL
return values: -1 (error), 0 (end-of-file), or 1 (success) */
static int parse_errlog_entry(struct timeval *time, char *user, char *database, uint64_t *session_id, log_type *type, char **message, char **detail) {
char *line = NULL, *part2, *part3, *part4, *part5, *part6;
const char *errmsg;
int i, skip_line = 0;
static int dump_found = 0;
/* if not NULL, contains the next log entry to parse */
static char* keepline = NULL;
debug(3, "Entering parse_errlog_entry%s\n", "");
/* initialize message and detail with NULL */
*message = NULL;
*detail = NULL;
/* use cached line or read next line from log file */
if (keepline) {
line = keepline;
keepline = NULL;
} else {
/* read lines until we are between start_time and end_time */
do {
if (line) {
free(line);
}
if (NULL == (line = read_log_line())) {
return -1;
}
/* is it the start of a memory dump? */
if (0 == strncmp(line, "TopMemoryContext: ", 18)) {
fprintf(stderr, "Found memory dump in line %lu\n", lineno);
dump_found = 1;
skip_line = 1;
} else {
/* if there is a dump and the line starts blank,
assume the line is part of the dump
*/
if (dump_found && (' ' == *line)) {
skip_line = 1;
} else {
skip_line = 0;
}
}
} while (('\0' != *line)
&& (skip_line
|| (start_time && (strncmp(line, start_time, 23) < 0))));
}
/* check for EOF */
if (('\0' == *line) || (end_time && (strncmp(line, end_time, 23) > 0))) {
free(line);
debug(3, "Leaving parse_errlog_entry%s\n", "");
return 0;
}
/* split line on | in six pieces: time, user, database, session ID, log entry type, rest */
if (NULL == (part2 = strchr(line, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: no \"%c\" found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part2++) = '\0';
}
if (NULL == (part3 = strchr(part2, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: second \"%c\" not found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part3++) = '\0';
}
if (NULL == (part4 = strchr(part3, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: third \"%c\" not found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part4++) = '\0';
}
if (NULL == (part5 = strchr(part4, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: fourth \"%c\" not found - log_line_prefix may be wrong\n", lineno, SEPCHAR);
free(line);
return -1;
} else {
*(part5++) = '\0';
}
if (NULL == (part6 = strstr(part5, ": "))) {
fprintf(stderr, "Error parsing line %lu: log message does not begin with a log type\n", lineno);
free(line);
return -1;
} else {
*part6 = '\0';
part6 += 3;
}
/* first part is the time, parse it into parameter */
if ((errmsg = parse_time(line, time))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* second part is the username, copy to parameter */
if (NAMELEN < strlen(part2)) {
fprintf(stderr, "Error parsing line %lu: username exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(user, part2);
}
/* third part is the database, copy to parameter */
if (NAMELEN < strlen(part3)) {
fprintf(stderr, "Error parsing line %lu: database name exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(database, part3);
}
/* fourth part is the session ID, copy to parameter */
if ((errmsg = parse_session(part4, session_id))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* fifth part is the log type, copy to parameter */
*type = to_log_type(part5);
/* sixth part is the log message */
overlap_strcpy(line, part6);
*message = line;
/* read the next log entry so that we can peek at it */
line = NULL;
do {
if (NULL != line) {
free(line);
}
if (NULL == (line = read_log_line())) {
free(*message);
*message = NULL;
return -1;
}
/* is it the start of a memory dump? */
if (0 == strncmp(line, "TopMemoryContext: ", 18)) {
fprintf(stderr, "Found memory dump in line %lu\n", lineno);
dump_found = 1;
skip_line = 1;
} else {
/* if there is a dump and the line starts blank,
assume the line is part of the dump
*/
if (dump_found && (' ' == *line)) {
skip_line = 1;
} else {
skip_line = 0;
}
}
} while (('\0' != *line) && skip_line);
if ('\0' == *line) {
/* EOF, that's ok */
keepline = line;
} else {
/* skip four | to the fifth part */
part2 = line;
for (i=0; i<4; ++i) {
if (NULL == (part2 = strchr(part2, SEPCHAR))) {
fprintf(stderr, "Error parsing line %lu: only %d \"%c\" found - log_line_prefix may be wrong\n", lineno, i, SEPCHAR);
free(*message);
free(line);
*message = NULL;
return -1;
} else {
++part2;
}
}
/* check if it is a DETAIL */
if (strncmp(part2, "DETAIL: ", 9)) {
/* if not, remember the line for the next pass */
keepline = line;
} else {
debug(2, "Found a DETAIL message%s\n", "");
/* set the return parameter to the detail message */
overlap_strcpy(line, part2 + 9);
*detail = line;
}
}
debug(3, "Leaving parse_errlog_entry%s\n", "");
return 1;
}
/* parses the next CSV log entry
timestamp, user, database, session ID, log message type, log message
and detail message are returned in the respective parameters
"message" is malloc'ed, "detail" not
return values: -1 (error), 0 (end-of-file), or 1 (success) */
static int parse_csvlog_entry(struct timeval *time, char *user, char *database, uint64_t *session_id, log_type *type, char **message, char **detail) {
char *line = NULL, *part[16], *p1, *p2;
const char *errmsg;
int i, escaped = 0;
debug(3, "Entering parse_csvlog_entry%s\n", "");
/* initialize message and detail with NULL */
*message = NULL;
*detail = NULL;
/* read next line after start timestamp from log file */
do {
if (line) {
free(line);
}
if (NULL == (line = read_log_line())) {
return -1;
}
} while (('\0' != *line)
&& (start_time && (strncmp(line, start_time, 23) < 0)));
/* check for EOF */
if (('\0' == *line) || (end_time && (strncmp(line, end_time, 23) > 0))) {
free(line);
debug(3, "Leaving parse_errlog_entry%s\n", "");
return 0;
}
/* parse first 15 parts from the CSV record */
part[0] = p1 = line;
for (i=1; i<16; ++i) {
p2 = p1;
/* copy p1 to p2 until we hit an unescaped comma,
remove escaping double quotes */
while (escaped || (',' != *p1)) {
switch (*p1) {
case '\0':
fprintf(stderr, "Error parsing line %lu: comma number %d not found (or unmatched quotes)\n", lineno, i);
free(line);
return -1;
case '"':
/* don't copy the first double quote */
if (!escaped && (p1 != part[i-1])) {
*(p2++) = '"';
}
++p1;
escaped = !escaped;
break;
default:
*(p2++) = *(p1++);
}
}
*p2 = '\0';
part[i] = ++p1;
}
/* first part is the time, parse it into parameter */
if ((errmsg = parse_time(part[0], time))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* second part is the username, copy to parameter */
if (NAMELEN < strlen(part[1])) {
fprintf(stderr, "Error parsing line %lu: username exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(user, part[1]);
}
/* third part is the database, copy to parameter */
if (NAMELEN < strlen(part[2])) {
fprintf(stderr, "Error parsing line %lu: database name exceeds %d characters\n", lineno, NAMELEN);
free(line);
return -1;
} else {
strcpy(database, part[2]);
}
/* sixth part is the session ID, copy to parameter */
if ((errmsg = parse_session(part[5], session_id))) {
fprintf(stderr, "Error parsing line %lu: %s\n", lineno, errmsg);
free(line);
return -1;
}
/* twelfth part is the log type, copy to parameter */
*type = to_log_type(part[11]);
/* fourteenth part is the message, assign to output parameter */
overlap_strcpy(line, part[13]);
*message = line;
/* detail is the fifteenth part of the line, if not empty */
*detail = part[14];
if ('\0' == **detail) {
*detail = NULL;
}
debug(3, "Leaving parse_csvlog_entry%s\n", "");
return 1;
}
/* add (malloc) the prepared statement name to the list of
prepared statements for the connection
returns 0 if the statement already existed, 1 if it was added and -1 if there was an error */
static int add_pstmt(struct connection * conn, char const *name) {
struct prep_stmt *pstmt = conn->statements;
int rc;
debug(3, "Entering add_pstmt for statement \"%s\"\n", name);
if ('\0' == *name) {
/* the empty statement will never be stored, but should be prepared */
rc = 1;
/* count for statistics */
++stat_param;
} else {
while (pstmt && strcmp(pstmt->name, name)) {
pstmt = pstmt->next;
}
if (pstmt) {
/* statement already prepared */
debug(2, "Prepared statement is already in list%s\n", "");
rc = 0;
} else {
debug(2, "Adding prepared statement to list%s\n", "");
/* add statement name to linked list */
if (NULL == (pstmt = malloc(sizeof(struct prep_stmt)))) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", (unsigned long)sizeof(struct prep_stmt));
return -1;
}
if (NULL == (pstmt->name = malloc(strlen(name) + 1))) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", (unsigned long)strlen(name) + 1);
free(pstmt);
return -1;
}
strcpy(pstmt->name, name);
pstmt->next = conn->statements;
conn->statements = pstmt;
rc = 1;
/* count for statistics */
++stat_named;
}
/* count for statistics */
++stat_execnamed;
}
debug(3, "Leaving add_pstmt%s\n", "");
return rc;
}
/* remove (free) the prepared statement name to the list of
prepared statements for the connection */
static void remove_pstmt(struct connection * conn, char const *name) {
struct prep_stmt *pstmt = conn->statements, *pstmt2 = NULL;
debug(3, "Entering remove_pstmt for statement \"%s\"\n", name);
while (pstmt && strcmp(pstmt->name, name)) {
pstmt2 = pstmt; /*remember previous */
pstmt = pstmt->next;
}
if (pstmt) {
if (pstmt2) {
pstmt2->next = pstmt->next;
} else {
conn->statements = pstmt->next;
}
free(pstmt->name);
free(pstmt);
} else {
debug(2, "Prepared statement not found%s\n", "");
}
debug(3, "Leaving remove_pstmt%s\n", "");
return;
}
static void remove_all_pstmts(struct connection * conn) {
struct prep_stmt *pstmt = conn->statements, *pstmt2 = NULL;
debug(3, "Entering remove_all_pstmts%s\n", "");
while(pstmt) {
pstmt2 = pstmt;
pstmt = pstmt->next;
free(pstmt2->name);
free(pstmt2);
}
debug(3, "Leaving remove_all_pstmts%s\n", "");
return;
}
/* remove all "COPY" and "SET client_encoding" statements;
for DEALLOCATE statements, try to remove prepared statement */
/* maximum number of tokens we need to analyze a statement */
#define MAX_TOKENS 3
static int filter_bad_statements(char *line, struct connection *conn) {
char *statement = line, *p = line, token[MAX_TOKENS][NAMELEN + 1],
*q = NULL, *quote, *h;
int comment_depth, tokens = 0, ok = 1, i, nameindex, quotelen;
debug(3, "Entering filter_bad_statements%s\n", "");
for (i=0; i<MAX_TOKENS; ++i) {
token[i][0] = '\0';
}
while (ok) {
if (('\0' == *p) || (';' == *p)) {
/* end of a statement found */
if (tokens > 0) {
/* count parsed simple statements */
++stat_simple;
/* remove statements that won't work */
if (! strcmp("copy", token[0])) {
fprintf(stderr, "Warning: COPY statement ignored in line %lu\n", lineno);
/* replace statement with blanks */
while (statement < p) {
*(statement++) = ' ';
}
/* count for statistics */
++stat_copy;
} else if ((tokens > 1) && (! strcmp("set", token[0])) && (! strcmp("client_encoding", token[1]))) {
fprintf(stderr, "Warning: \"SET client_encoding\" statement ignored in line %lu\n", lineno);
/* replace statement with blanks */
while (statement < p) {
*(statement++) = ' ';
}
} else if (! strcmp("deallocate", token[0])) {
/* there coule be a "prepare" in the second token, should be ignored */
if (strcmp("prepare", token[1])) {
nameindex = 1;
} else {
nameindex = 2;
}
if (strcmp("all", token[nameindex])) {
/* deallocate single statement */
debug(2, "Deallocating prepared statement \"%s\"\n", token[nameindex]);
remove_pstmt(conn, token[nameindex]);
} else {
/* deallocate all prepared statements */
debug(2, "Deallocating all prepared statements%s\n", "");
remove_all_pstmts(conn);
}
}
}
/* break out of loop if end-of-line is reached */
if ('\0' == *p) {
break;
}
/* else prepare for next statement */
statement = ++p;
for (i=0; i<MAX_TOKENS; ++i) {
token[i][0] = '\0';
}
tokens = 0;
} else if ((('E' == *p) || ('e' == *p)) && ('\'' == p[1])) {
/* special string constant; skip to end */
++p;
while ('\0' != *(++p)) {
if ('\'' == *p) {
if ('\'' == p[1]) {
/* regular escaped apostrophe */
++p;
} else {
break;
}
}
if (('\\' == *p) && (('\'' == p[1]) || ('\\' == p[1]))) {
/* backslash escaped apostrophe or backslash */
++p;
}
}
if ('\0' == *p) {
fprintf(stderr, "Error: string literal not closed near line %lu\n"
"Hint: retry with%s the -q option\n",
lineno, (backslash_quote ? "out" : ""));
ok = 0;
} else {
++p;
}
} else if ('\'' == *p) {
/* simple string constant; skip to end */
while ('\0' != *(++p)) {
if ('\'' == *p) {
if ('\'' == p[1]) {
/* regular escaped apostrophe */
++p;
} else {
break;
}
}
if (backslash_quote && ('\\' == *p) && (('\'' == p[1]) || ('\\' == p[1]))) {
/* backslash escaped apostrophe or backslash */
++p;
}
}
if ('\0' == *p) {
fprintf(stderr, "Error: string literal not closed near line %lu\n"
"Hint: retry with%s the -q option\n",
lineno, (backslash_quote ? "out" : ""));
ok = 0;
} else {
++p;
}
} else if (('$' == *p) && (('0' > *(p+1)) || (('9' < *(p+1))))) {
/* dollar quoted string constant; skip to end */
quote = p++;
while (('$' != *p) && ('\0' != *p)) {
++p;
}
if ('\0' == *p) {
fprintf(stderr, "Error: end of dollar quote not found in line %lu\n", lineno);
ok = 0;
} else {
quotelen = p - quote;
*p = '\0';
h = p;
do {
h = strstr(++h, quote);
} while ((NULL != h) && ('$' != *(h + quotelen)));
*p = '$';
if (NULL == h) {
fprintf(stderr, "Error: end of dollar quoted string found in line %lu\n", lineno);
ok = 0;
} else {
p = h + (quotelen + 1);
}
}
} else if (('-' == *p) && ('-' == p[1])) {
/* comment; skip to end of line or statement */
while (('\n' != *p) && ('\0' != *p)) {
++p;
}
} else if (('/' == *p) && ('*' == p[1])) {
/* comment, skip to matching end */
p += 2;
comment_depth = 1; /* comments can be nested */
while (0 != comment_depth) {
if ('\0' == *p) {
fprintf(stderr, "Error: comment not closed in line %lu\n", lineno);
ok = 0;
break;
} else if (('*' == *p) && ('/' == p[1])) {
--comment_depth;
p += 2;
} else if (('/' == *p) && ('*' == p[1])) {
++comment_depth;
p += 2;
} else {
++p;
}
}
} else if ('"' == *p) {
/* quoted identifier, copy to token if necessary */
if (tokens < MAX_TOKENS) {
q = token[tokens];
}
while (1) {
++p;
if ('\0' == *p) {
fprintf(stderr, "Error: quoted identifier not closed in line %lu\n", lineno);
ok = 0;
break;
} else if ('"' == *p) {
if ('"' == p[1]) {
/* double " means a single " in a quoted identifier */
if ((tokens < MAX_TOKENS) && (q - token[tokens] < NAMELEN)) {
*(q++) = '"';
}
++p;
} else {
/* end of token */
if (tokens < MAX_TOKENS) {
*q = '\0';
++tokens;
}