PostgreSQL Source Code: src/include/postmaster/syslogger.h File Reference (original) (raw)
#include <limits.h>
Go to the source code of this file.
Macros | |
---|---|
#define | PIPE_CHUNK_SIZE 512 |
#define | PIPE_HEADER_SIZE offsetof(PipeProtoHeader, data) |
#define | PIPE_MAX_PAYLOAD ((int) (PIPE_CHUNK_SIZE - PIPE_HEADER_SIZE)) |
#define | PIPE_PROTO_IS_LAST 0x01 /* last chunk of message? */ |
#define | PIPE_PROTO_DEST_STDERR 0x10 |
#define | PIPE_PROTO_DEST_CSVLOG 0x20 |
#define | PIPE_PROTO_DEST_JSONLOG 0x40 |
#define | LOG_METAINFO_DATAFILE "current_logfiles" |
#define | LOG_METAINFO_DATAFILE_TMP LOG_METAINFO_DATAFILE ".tmp" |
Functions | |
---|---|
int | SysLogger_Start (int child_slot) |
void | write_syslogger_file (const char *buffer, int count, int destination) |
pg_noreturn void | SysLoggerMain (const void *startup_data, size_t startup_data_len) |
bool | CheckLogrotateSignal (void) |
void | RemoveLogrotateSignalFiles (void) |
Variables | |
---|---|
PGDLLIMPORT bool | Logging_collector |
PGDLLIMPORT int | Log_RotationAge |
PGDLLIMPORT int | Log_RotationSize |
PGDLLIMPORT char * | Log_directory |
PGDLLIMPORT char * | Log_filename |
PGDLLIMPORT bool | Log_truncate_on_rotation |
PGDLLIMPORT int | Log_file_mode |
PGDLLIMPORT int | syslogPipe [2] |
◆ LOG_METAINFO_DATAFILE
#define LOG_METAINFO_DATAFILE "current_logfiles"
◆ LOG_METAINFO_DATAFILE_TMP
◆ PIPE_CHUNK_SIZE
#define PIPE_CHUNK_SIZE 512
◆ PIPE_HEADER_SIZE
◆ PIPE_MAX_PAYLOAD
◆ PIPE_PROTO_DEST_CSVLOG
#define PIPE_PROTO_DEST_CSVLOG 0x20
◆ PIPE_PROTO_DEST_JSONLOG
#define PIPE_PROTO_DEST_JSONLOG 0x40
◆ PIPE_PROTO_DEST_STDERR
#define PIPE_PROTO_DEST_STDERR 0x10
◆ PIPE_PROTO_IS_LAST
#define PIPE_PROTO_IS_LAST 0x01 /* last chunk of message? */
◆ CheckLogrotateSignal()
bool CheckLogrotateSignal | ( | void | ) |
---|
◆ RemoveLogrotateSignalFiles()
void RemoveLogrotateSignalFiles | ( | void | ) |
---|
◆ SysLogger_Start()
int SysLogger_Start | ( | int | child_slot | ) |
---|
Definition at line 593 of file syslogger.c.
594{
595 pid_t sysloggerPid;
597#ifdef EXEC_BACKEND
599#endif
600
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620#ifndef WIN32
622 {
626 errmsg("could not create pipe for syslog: %m")));
627 }
628#else
630 {
631 SECURITY_ATTRIBUTES sa;
632
633 memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
634 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
635 sa.bInheritHandle = TRUE;
636
640 errmsg("could not create pipe for syslog: %m")));
641 }
642#endif
643
644
645
646
648
649
650
651
652
653
654
655
656
657
658
659
660
662
664
666
668
669
670
671
672
673
675 {
677
679
681 }
682
683
684
685
686
687
689 {
691
693
695 }
696
697#ifdef EXEC_BACKEND
702 &startup_data, sizeof(startup_data), NULL);
703#else
705 NULL, 0, NULL);
706#endif
707
708 if (sysloggerPid == -1)
709 {
711 (errmsg("could not fork system logger: %m")));
712 return 0;
713 }
714
715
716
717
719 {
720#ifdef WIN32
721 int fd;
722#endif
723
724
725
726
727
728
730 (errmsg("redirecting log output to logging collector process"),
731 errhint("Future log output will appear in directory \"%s\".",
733
734#ifndef WIN32
739 errmsg("could not redirect stdout: %m")));
740 fflush(stderr);
744 errmsg("could not redirect stderr: %m")));
745
748#else
749
750
751
752
753
754
755 fflush(stderr);
756 fd = _open_osfhandle((intptr_t) syslogPipe[1],
757 _O_APPEND | _O_BINARY);
761 errmsg("could not redirect stderr: %m")));
764
765
766
767
768
769
771#endif
773 }
774
775
779 {
782 }
784 {
787 }
788 return (int) sysloggerPid;
789}
int errcode_for_socket_access(void)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errmsg(const char *fmt,...)
#define LOG_DESTINATION_JSONLOG
#define ereport(elevel,...)
#define LOG_DESTINATION_CSVLOG
int MakePGDirectory(const char *directoryName)
Assert(PointerIsAligned(start, uint64))
pid_t postmaster_child_launch(BackendType child_type, int child_slot, const void *startup_data, size_t startup_data_len, ClientSocket *client_sock)
void pfree(void *pointer)
static int fd(const char *x, int i)
static char * logfile_getname(pg_time_t timestamp, const char *suffix)
static FILE * logfile_open(const char *filename, const char *mode, bool allow_errors)
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
static FILE * jsonlogFile
References Assert(), B_LOGGER, close, csvlogFile, SysloggerStartupData::csvlogFile, ereport, errcode_for_file_access(), errcode_for_socket_access(), errhint(), errmsg(), FATAL, fd(), filename, first_syslogger_file_time, jsonlogFile, SysloggerStartupData::jsonlogFile, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_JSONLOG, Log_directory, logfile_getname(), logfile_open(), Logging_collector, MakePGDirectory(), pfree(), postmaster_child_launch(), redirection_done, STDERR_FILENO, generate_unaccent_rules::stdout, STDOUT_FILENO, syslogFile, SysloggerStartupData::syslogFile, and syslogPipe.
Referenced by StartSysLogger().
◆ SysLoggerMain()
pg_noreturn void SysLoggerMain | ( | const void * | startup_data, |
---|---|---|---|
size_t | startup_data_len | ||
) |
Definition at line 165 of file syslogger.c.
166{
167#ifndef WIN32
169 int bytes_in_logbuffer = 0;
170#endif
171 char *currentLogDir;
172 char *currentLogFilename;
173 int currentLogRotationAge;
176
177
178
179
180
181
182
183
184#ifdef EXEC_BACKEND
185 {
187
188 Assert(startup_data_len == sizeof(*slsdata));
192 }
193#else
194 Assert(startup_data_len == 0);
195#endif
196
197
198
199
200
202 {
205 }
206
208
211
212
213
214
215
216
217
218
220 {
221 int fd = open(DEVNULL, O_WRONLY, 0);
222
223
224
225
226
227
228
229
230
231
232
233
236 if (fd != -1)
237 {
241 }
242 }
243
244
245
246
247
248
249#ifdef WIN32
250 else
252#endif
253
254
255
256
257
258
259#ifndef WIN32
263#else
267#endif
268
269
270
271
272
273
274
275
276
278
286
287
288
289
291
292 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
293
294#ifdef WIN32
295
296 InitializeCriticalSection(&sysloggerSection);
297 EnterCriticalSection(&sysloggerSection);
298
299 threadHandle = (HANDLE) _beginthreadex(NULL, 0, pipeThread, NULL, 0, NULL);
300 if (threadHandle == 0)
301 elog(FATAL, "could not create syslogger data transfer thread: %m");
302#endif
303
304
305
306
307
308
314
315
319
322
323
324
325
326
327
329
330
331
332
333
334
335
336
337
338
339
342#ifndef WIN32
344#endif
345
346
347 for (;;)
348 {
349 bool time_based_rotation = false;
350 int size_rotation_for = 0;
351 long cur_timeout;
353
354#ifndef WIN32
355 int rc;
356#endif
357
358
360
361
362
363
365 {
368
369
370
371
372
373
375 {
376 pfree(currentLogDir);
379
380
381
382
384 }
385 if (strcmp(Log_filename, currentLogFilename) != 0)
386 {
387 pfree(currentLogFilename);
390 }
391
392
393
394
395
399
400
401
402
403
407
408
409
410
411
413 {
416 }
417
418
419
420
421
423 {
426 }
427
428
429
430
431
432
434 }
435
437 {
438
442 }
443
445 {
446
448 {
451 }
454 {
457 }
460 {
463 }
464 }
465
467 {
468
469
470
471
472 if (!time_based_rotation && size_rotation_for == 0)
476 logfile_rotate(time_based_rotation, size_rotation_for);
477 }
478
479
480
481
482
483
484
485
486
487
488
489
490
492 {
494
496 if (delay > 0)
497 {
498 if (delay > INT_MAX / 1000)
499 delay = INT_MAX / 1000;
500 cur_timeout = delay * 1000L;
501 }
502 else
503 cur_timeout = 0;
504 }
505 else
506 cur_timeout = -1L;
507
508
509
510
511#ifndef WIN32
513 WAIT_EVENT_SYSLOGGER_MAIN);
514
516 {
517 int bytesRead;
518
520 logbuffer + bytes_in_logbuffer,
521 sizeof(logbuffer) - bytes_in_logbuffer);
522 if (bytesRead < 0)
523 {
524 if (errno != EINTR)
527 errmsg("could not read from logger pipe: %m")));
528 }
529 else if (bytesRead > 0)
530 {
531 bytes_in_logbuffer += bytesRead;
533 continue;
534 }
535 else
536 {
537
538
539
540
541
542
544
545
547 }
548 }
549#else
550
551
552
553
554
555
556
557
558
559
560 LeaveCriticalSection(&sysloggerSection);
561
563 WAIT_EVENT_SYSLOGGER_MAIN);
564
565 EnterCriticalSection(&sysloggerSection);
566#endif
567
569 {
570
571
572
573
576
577
578
579
580
581
582
583
585 }
586 }
587}
Datum now(PG_FUNCTION_ARGS)
int errmsg_internal(const char *fmt,...)
#define LOG_DESTINATION_STDERR
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ConfigReloadPending
void SignalHandlerForConfigReload(SIGNAL_ARGS)
void ResetLatch(Latch *latch)
char * pstrdup(const char *in)
MemoryContext PostmasterContext
void MemoryContextDelete(MemoryContext context)
BackendType MyBackendType
CommandDest whereToSendOutput
void init_ps_display(const char *fixed_part)
static bool rotation_disabled
static void logfile_rotate(bool time_based_rotation, int size_rotation_for)
static bool pipe_eof_seen
static void update_metainfo_datafile(void)
static char * last_csv_file_name
static void process_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
static pg_time_t next_rotation_time
static volatile sig_atomic_t rotation_requested
static void sigUsr1Handler(SIGNAL_ARGS)
static void set_next_rotation_time(void)
static void flush_pipe_input(char *logbuffer, int *bytes_in_logbuffer)
static char * last_json_file_name
static char * last_sys_file_name
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
#define WL_SOCKET_READABLE
References AddWaitEventToSet(), Assert(), B_LOGGER, close, ConfigReloadPending, CreateWaitEventSet(), csvlogFile, SysloggerStartupData::csvlogFile, DEBUG1, DestNone, DEVNULL, EINTR, elog, ereport, errcode_for_socket_access(), errmsg(), errmsg_internal(), WaitEvent::events, FATAL, fd(), first_syslogger_file_time, flush_pipe_input(), ftello, init_ps_display(), jsonlogFile, SysloggerStartupData::jsonlogFile, last_csv_file_name, last_json_file_name, last_sys_file_name, LOG, Log_destination, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_JSONLOG, LOG_DESTINATION_STDERR, Log_directory, Log_filename, Log_RotationAge, Log_RotationSize, logfile_getname(), logfile_rotate(), MakePGDirectory(), MemoryContextDelete(), MyBackendType, MyLatch, MyStartTime, next_rotation_time, now(), pfree(), PGC_SIGHUP, PGINVALID_SOCKET, pgoff_t, pipe_eof_seen, PostmasterContext, pqsignal, proc_exit(), process_pipe_input(), ProcessConfigFile(), pstrdup(), read, READ_BUF_SIZE, redirection_done, ResetLatch(), rotation_disabled, rotation_requested, set_next_rotation_time(), SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SIGPIPE, SIGQUIT, SIGUSR1, sigUsr1Handler(), SIGUSR2, STDERR_FILENO, STDOUT_FILENO, syslogFile, SysloggerStartupData::syslogFile, syslogPipe, UnBlockSig, update_metainfo_datafile(), WaitEventSetWait(), whereToSendOutput, WL_LATCH_SET, and WL_SOCKET_READABLE.
◆ write_syslogger_file()
void write_syslogger_file | ( | const char * | buffer, |
---|---|---|---|
int | count, | ||
int | destination | ||
) |
Definition at line 1093 of file syslogger.c.
1094{
1095 int rc;
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1115 else
1117
1118 rc = fwrite(buffer, 1, count, logfile);
1119
1120
1121
1122
1123
1124
1125
1126 if (rc != count)
1127 write_stderr("could not write to log file: %m\n");
1128}
#define write_stderr(str)
References csvlogFile, jsonlogFile, LOG_DESTINATION_CSVLOG, LOG_DESTINATION_JSONLOG, logfile, syslogFile, and write_stderr.
Referenced by flush_pipe_input(), process_pipe_input(), send_message_to_server_log(), write_csvlog(), and write_jsonlog().