-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogtofile_bgw.c
157 lines (131 loc) · 4.15 KB
/
logtofile_bgw.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
/*-------------------------------------------------------------------------
*
* logtofile_bgw.c
* Background worker for logtofile
*
* Copyright (c) 2020-2024, Francisco Miguel Biete Banon
*
* This code is released under the PostgreSQL licence, as given at
* http://www.postgresql.org/about/licence/
*-------------------------------------------------------------------------
*/
#include "logtofile_bgw.h"
/* these are always necessary for a bgworker */
#include <miscadmin.h>
#include <postmaster/bgworker.h>
#include <storage/ipc.h>
#include <storage/latch.h>
#include <storage/lwlock.h>
#include <storage/proc.h>
#include <storage/shm_mq.h>
#include <storage/shm_toc.h>
#include <storage/shmem.h>
#if (PG_VERSION_NUM >= 140000)
#include <utils/backend_status.h>
#include <utils/wait_event.h>
#else
#include <pgstat.h>
#endif
#include <utils/guc.h>
#include <utils/memutils.h>
#include <utils/timestamp.h>
#include "logtofile_filename.h"
#include "logtofile_shmem.h"
#include "logtofile_vars.h"
/* global settings */
static bool PgAuditLogToFileReloadConfig = false;
/* flags set by signal handlers */
static volatile sig_atomic_t got_sigterm = false;
/* forward declaration private functions */
static void pgauditlogtofile_sighup(SIGNAL_ARGS);
static void pgauditlogtofile_sigterm(SIGNAL_ARGS);
/**
* @brief Main entry point for the background worker
* @param arg: unused
* @return void
*/
void PgAuditLogToFileMain(Datum arg)
{
int sleep_ms = SECS_PER_MINUTE * 1000;
MemoryContext PgAuditLogToFileContext = NULL;
pqsignal(SIGHUP, pgauditlogtofile_sighup);
pqsignal(SIGINT, SIG_IGN);
pqsignal(SIGTERM, pgauditlogtofile_sigterm);
BackgroundWorkerUnblockSignals();
pgstat_report_appname("pgauditlogtofile launcher");
PgAuditLogToFileContext = AllocSetContextCreate(CurrentMemoryContext, "pgauditlogtofile loop context",
ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE);
ereport(LOG, (errmsg("pgauditlogtofile worker started")));
MemoryContextSwitchTo(PgAuditLogToFileContext);
while (1)
{
int rc;
CHECK_FOR_INTERRUPTS();
if (guc_pgaudit_ltf_log_rotation_age < MINS_PER_HOUR)
{
// very small rotation, wake up frequently - this has a performance impact,
// but rotation every a few minutes should only be done for testing
sleep_ms = 10000;
}
ereport(DEBUG5, (errmsg("pgauditlogtofile bgw loop")));
if (PgAuditLogToFileReloadConfig)
{
ereport(DEBUG3, (errmsg("pgauditlogtofile bgw loop reload cfg")));
ProcessConfigFile(PGC_SIGHUP);
PgAuditLogToFile_calculate_current_filename();
PgAuditLogToFile_set_next_rotation_time();
ereport(DEBUG3, (errmsg("pgauditlogtofile bgw loop new filename %s", pgaudit_ltf_shm->filename)));
PgAuditLogToFileReloadConfig = false;
}
else
{
if (PgAuditLogToFile_needs_rotate_file())
{
ereport(DEBUG3, (errmsg("pgauditlogtofile bgw loop needs rotation %s", pgaudit_ltf_shm->filename)));
PgAuditLogToFile_calculate_current_filename();
PgAuditLogToFile_set_next_rotation_time();
ereport(DEBUG3, (errmsg("pgauditlogtofile bgw loop new filename %s", pgaudit_ltf_shm->filename)));
}
}
/* shutdown if requested */
if (got_sigterm)
break;
rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, sleep_ms,
PG_WAIT_EXTENSION);
if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
ResetLatch(&MyProc->procLatch);
}
MemoryContextReset(PgAuditLogToFileContext);
ereport(LOG, (errmsg("pgauditlogtofile worker shutting down")));
proc_exit(0);
}
/* private functions */
/**
* @brief Signal handler for SIGHUP
* @param signal_arg: signal number
* @return void
*/
static void
pgauditlogtofile_sigterm(SIGNAL_ARGS)
{
got_sigterm = true;
if (MyProc != NULL)
{
SetLatch(&MyProc->procLatch);
}
}
/**
* @brief Signal handler for SIGTERM
* @param signal_arg: signal number
* @return void
*/
static void
pgauditlogtofile_sighup(SIGNAL_ARGS)
{
PgAuditLogToFileReloadConfig = true;
if (MyProc != NULL)
{
SetLatch(&MyProc->procLatch);
}
}