23#include "lib/stringinfo.h"
24#include "postmaster/bgworker.h"
25#include "postmaster/postmaster.h"
26#include "storage/latch.h"
27#include "storage/ipc.h"
41#if PG_VERSION_NUM < 120000
45#define WL_EXIT_ON_PM_DEATH WL_POSTMASTER_DEATH
53 int save_errno = errno;
61 int save_errno = errno;
94 const char *p = strstr(tmpl,
"{endpoint}");
99 appendBinaryStringInfo(&s, tmpl, p - tmpl);
100 appendStringInfoString(&s, endpoint);
101 appendStringInfoString(&s, p + strlen(
"{endpoint}"));
109#ifdef PROVSQL_NO_SUBPROCESS
120 execl(
"/bin/sh",
"sh",
"-c", cmd, (
char *) NULL);
123 setpgid(child, child);
130#ifdef PROVSQL_NO_SUBPROCESS
137 killpg(child, SIGKILL);
138 do { r = waitpid(child, &status, 0); }
while (r < 0 && errno == EINTR);
152 events |= WL_TIMEOUT;
153 rc = WaitLatch(MyLatch, events, timeout_ms, PG_WAIT_EXTENSION);
155 return (rc & WL_POSTMASTER_DEATH) != 0;
162#ifdef PROVSQL_INPROCESS_STORE
176 BackgroundWorkerUnblockSignals();
178 snprintf(endpoint,
sizeof(endpoint),
"unix:/tmp/.provsql-kcmcp-%d.sock",
190 ProcessConfigFile(PGC_SIGHUP);
216 "not launching the managed KCMCP server");
227 provsql_log(
"managed KCMCP server started (pid %d) on %s",
228 (
int) child, endpoint);
240#ifdef PROVSQL_NO_SUBPROCESS
241 w = -1; (void) status;
243 w = waitpid(child, &status, WNOHANG);
247 provsql_log(
"managed KCMCP server (pid %d) exited; relaunching",
262#ifndef PROVSQL_INPROCESS_STORE
263 BackgroundWorker worker;
264 memset(&worker, 0,
sizeof(worker));
265 snprintf(worker.bgw_name, BGW_MAXLEN,
"ProvSQL KCMCP Supervisor");
266#if PG_VERSION_NUM >= 110000
267 snprintf(worker.bgw_type, BGW_MAXLEN,
"ProvSQL KCMCP");
269 worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
270 worker.bgw_start_time = BgWorkerStart_PostmasterStart;
271 worker.bgw_restart_time = 1;
272 snprintf(worker.bgw_library_name, BGW_MAXLEN,
"provsql");
273 snprintf(worker.bgw_function_name, BGW_MAXLEN,
"provsql_kcmcp_worker");
274 worker.bgw_main_arg = (Datum) 0;
275 worker.bgw_notify_pid = 0;
276 RegisterBackgroundWorker(&worker);
static volatile sig_atomic_t got_sigterm
static void publish_endpoint(const char *endpoint)
static char * build_server_command(const char *tmpl, const char *endpoint)
void RegisterProvSQLKCMCPWorker(void)
Register the supervisor background worker that launches and supervises the managed KCMCP server; call...
const char * provsql_kcmcp_managed_endpoint(void)
Read the live endpoint of the managed KCMCP server from shared memory (e.g.
static void kcmcp_sigterm(SIGNAL_ARGS)
static volatile sig_atomic_t got_sighup
static pid_t launch_server(const char *cmd)
#define WL_EXIT_ON_PM_DEATH
void provsql_kcmcp_worker(Datum ignored)
static void kcmcp_sighup(SIGNAL_ARGS)
static void kill_server(pid_t child)
static bool kcmcp_wait(long timeout_ms)
char * provsql_kcmcp_server
Launch command for the managed KCMCP server (with a {endpoint} placeholder); controlled by the provsq...
Build-configuration switches shared across the C and C++ sources.
Uniform error-reporting macros for ProvSQL.
#define provsql_log(fmt,...)
Write a ProvSQL message to the server log only.
#define provsql_warning(fmt,...)
Emit a ProvSQL warning message (execution continues).
void provsql_shmem_unlock(void)
Release the ProvSQL LWLock.
void provsql_shmem_lock_exclusive(void)
Acquire the ProvSQL LWLock in exclusive mode.
provsqlSharedState * provsql_shared_state
Pointer to the ProvSQL shared-memory segment (set in provsql_shmem_startup).
void provsql_shmem_lock_shared(void)
Acquire the ProvSQL LWLock in shared mode.
Shared-memory segment and inter-process pipe management.
Core types, constants, and utilities shared across ProvSQL.