PostgreSQL Source Code git master
Loading...
Searching...
No Matches
launch_backend.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * launch_backend.c
4 * Functions for launching backends and other postmaster child
5 * processes.
6 *
7 * On Unix systems, a new child process is launched with fork(). It inherits
8 * all the global variables and data structures that had been initialized in
9 * the postmaster. After forking, the child process closes the file
10 * descriptors that are not needed in the child process, and sets up the
11 * mechanism to detect death of the parent postmaster process, etc. After
12 * that, it calls the right Main function depending on the kind of child
13 * process.
14 *
15 * In EXEC_BACKEND mode, which is used on Windows but can be enabled on other
16 * platforms for testing, the child process is launched by fork() + exec() (or
17 * CreateProcess() on Windows). It does not inherit the state from the
18 * postmaster, so it needs to re-attach to the shared memory, re-initialize
19 * global variables, reload the config file etc. to get the process to the
20 * same state as after fork() on a Unix system.
21 *
22 *
23 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
25 *
26 * IDENTIFICATION
27 * src/backend/postmaster/launch_backend.c
28 *
29 *-------------------------------------------------------------------------
30 */
31
32#include "postgres.h"
33
34#include <unistd.h>
35
36#include "libpq/libpq-be.h"
37#include "miscadmin.h"
40#include "postmaster/bgwriter.h"
42#include "postmaster/pgarch.h"
44#include "postmaster/startup.h"
50#include "storage/dsm.h"
51#include "storage/io_worker.h"
52#include "storage/pg_shmem.h"
54#include "utils/memutils.h"
55
56#ifdef EXEC_BACKEND
57#include "nodes/queryjumble.h"
58#include "storage/pg_shmem.h"
59#include "storage/spin.h"
60#endif
61
62
63#ifdef EXEC_BACKEND
64
65#include "common/file_utils.h"
66#include "storage/fd.h"
67#include "storage/lwlock.h"
68#include "storage/pmsignal.h"
69#include "storage/proc.h"
70#include "storage/procsignal.h"
71#include "tcop/tcopprot.h"
73
74/* Type for a socket that can be inherited to a client process */
75#ifdef WIN32
76typedef struct
77{
78 SOCKET origsocket; /* Original socket value, or PGINVALID_SOCKET
79 * if not a socket */
82#else
83typedef int InheritableSocket;
84#endif
85
86/*
87 * Structure contains all variables passed to exec:ed backends
88 */
89typedef struct
90{
91 char DataDir[MAXPGPATH];
92#ifndef WIN32
93 unsigned long UsedShmemSegID;
94#else
97#endif
98 void *UsedShmemSegAddr;
99#ifdef USE_INJECTION_POINTS
101#endif
104 char **LWLockTrancheNames;
105 int *LWLockCounter;
110 volatile PMSignalData *PMSignalState;
116 bool redirection_done;
117 bool IsBinaryUpgrade;
118 bool query_id_enabled;
119 int max_safe_fds;
120 int MaxBackends;
122#ifdef WIN32
126#else
128 int syslogPipe[2];
129#endif
132
133 int MyPMChildSlot;
134
135 /*
136 * These are only used by backend processes, but are here because passing
137 * a socket needs some special handling on Windows. 'client_sock' is an
138 * explicit argument to postmaster_child_launch, but is stored in
139 * MyClientSocket in the child process.
140 */
143
144 /*
145 * Extra startup data, content depends on the child process.
146 */
147 size_t startup_data_len;
150
151#define SizeOfBackendParameters(startup_data_len) (offsetof(BackendParameters, startup_data) + startup_data_len)
152
153static void read_backend_variables(char *id, void **startup_data, size_t *startup_data_len);
155
156static bool save_backend_variables(BackendParameters *param, int child_slot,
160#endif
161 const void *startup_data, size_t startup_data_len);
162
163static pid_t internal_forkexec(BackendType child_kind, int child_slot,
164 const void *startup_data, size_t startup_data_len,
166
167#endif /* EXEC_BACKEND */
168
169/*
170 * Information needed to launch different kinds of child processes.
171 */
172typedef struct
173{
174 const char *name;
175 void (*main_fn) (const void *startup_data, size_t startup_data_len);
178
180#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach) \
181 [bktype] = {description, main_func, shmem_attach},
183#undef PG_PROCTYPE
184};
185
186const char *
191
192/*
193 * Start a new postmaster child process.
194 *
195 * The child process will be restored to roughly the same state whether
196 * EXEC_BACKEND is used or not: it will be attached to shared memory if
197 * appropriate, and fds and other resources that we've inherited from
198 * postmaster that are not needed in a child process have been closed.
199 *
200 * 'child_slot' is the PMChildFlags array index reserved for the child
201 * process. 'startup_data' is an optional contiguous chunk of data that is
202 * passed to the child process.
203 */
204pid_t
206 void *startup_data, size_t startup_data_len,
208{
209 pid_t pid;
210
212
213 /* Capture time Postmaster initiates process creation for logging */
216
217#ifdef EXEC_BACKEND
218 pid = internal_forkexec(child_type, child_slot,
220 /* the child process will arrive in SubPostmasterMain */
221#else /* !EXEC_BACKEND */
222 pid = fork_process();
223 if (pid == 0) /* child */
224 {
226
227 /* Capture and transfer timings that may be needed for logging */
229 {
231 ((BackendStartupData *) startup_data)->socket_created;
233 ((BackendStartupData *) startup_data)->fork_started;
235 }
236
237 /* Close the postmaster's sockets */
239
240 /* Detangle from postmaster */
242
243 /* Detach shared memory if not needed. */
244 if (!child_process_kinds[child_type].shmem_attach)
245 {
248 }
249
250 /*
251 * Enter the Main function with TopMemoryContext. The startup data is
252 * allocated in PostmasterContext, so we cannot release it here yet.
253 * The Main function will do it after it's done handling the startup
254 * data.
255 */
257
258 MyPMChildSlot = child_slot;
259 if (client_sock)
260 {
263 }
264
265 /*
266 * Run the appropriate Main function
267 */
269 pg_unreachable(); /* main_fn never returns */
270 }
271#endif /* EXEC_BACKEND */
272 return pid;
273}
274
275#ifdef EXEC_BACKEND
276#ifndef WIN32
277
278/*
279 * internal_forkexec non-win32 implementation
280 *
281 * - writes out backend variables to the parameter file
282 * - fork():s, and then exec():s the child process
283 */
284static pid_t
286 const void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
287{
288 static unsigned long tmpBackendFileNum = 0;
289 pid_t pid;
291 size_t paramsz;
292 BackendParameters *param;
293 FILE *fp;
294 char *argv[4];
295 char forkav[MAXPGPATH];
296
297 /*
298 * Use palloc0 to make sure padding bytes are initialized, to prevent
299 * Valgrind from complaining about writing uninitialized bytes to the
300 * file. This isn't performance critical, and the win32 implementation
301 * initializes the padding bytes to zeros, so do it even when not using
302 * Valgrind.
303 */
305 param = palloc0(paramsz);
307 {
308 pfree(param);
309 return -1; /* log made by save_backend_variables */
310 }
311
312 /* Calculate name for temp file */
313 snprintf(tmpfilename, MAXPGPATH, "%s/%s.backend_var.%d.%lu",
316
317 /* Open file */
319 if (!fp)
320 {
321 /*
322 * As in OpenTemporaryFileInTablespace, try to make the temp-file
323 * directory, ignoring errors.
324 */
326
328 if (!fp)
329 {
330 ereport(LOG,
332 errmsg("could not create file \"%s\": %m",
333 tmpfilename)));
334 pfree(param);
335 return -1;
336 }
337 }
338
339 if (fwrite(param, paramsz, 1, fp) != 1)
340 {
341 ereport(LOG,
343 errmsg("could not write to file \"%s\": %m", tmpfilename)));
344 FreeFile(fp);
345 pfree(param);
346 return -1;
347 }
348 pfree(param);
349
350 /* Release file */
351 if (FreeFile(fp))
352 {
353 ereport(LOG,
355 errmsg("could not write to file \"%s\": %m", tmpfilename)));
356 return -1;
357 }
358
359 /* set up argv properly */
360 argv[0] = "postgres";
361 snprintf(forkav, MAXPGPATH, "--forkchild=%d", (int) child_kind);
362 argv[1] = forkav;
363 /* Insert temp file name after --forkchild argument */
364 argv[2] = tmpfilename;
365 argv[3] = NULL;
366
367 /* Fire off execv in child */
368 if ((pid = fork_process()) == 0)
369 {
370 if (execv(postgres_exec_path, argv) < 0)
371 {
372 ereport(LOG,
373 (errmsg("could not execute server process \"%s\": %m",
375 /* We're already in the child process here, can't return */
376 exit(1);
377 }
378 }
379
380 return pid; /* Parent returns pid, or -1 on fork failure */
381}
382#else /* WIN32 */
383
384/*
385 * internal_forkexec win32 implementation
386 *
387 * - starts backend using CreateProcess(), in suspended state
388 * - writes out backend variables to the parameter file
389 * - during this, duplicates handles and sockets required for
390 * inheritance into the new process
391 * - resumes execution of the new process once the backend parameter
392 * file is complete.
393 */
394static pid_t
396 const void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
397{
398 int retry_count = 0;
401 char cmdLine[MAXPGPATH * 2];
403 BackendParameters *param;
405 size_t paramsz;
406 char paramHandleStr[32];
407 int l;
408
410
411 /* Resume here if we need to retry */
412retry:
413
414 /* Set up shared memory for parameter passing */
415 ZeroMemory(&sa, sizeof(sa));
416 sa.nLength = sizeof(sa);
417 sa.bInheritHandle = TRUE;
419 &sa,
421 0,
422 paramsz,
423 NULL);
425 {
426 ereport(LOG,
427 (errmsg("could not create backend parameter file mapping: error code %lu",
428 GetLastError())));
429 return -1;
430 }
432 if (!param)
433 {
434 ereport(LOG,
435 (errmsg("could not map backend parameter memory: error code %lu",
436 GetLastError())));
438 return -1;
439 }
440
441 /* Format the cmd line */
442#ifdef _WIN64
444#else
446#endif
447 l = snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\" --forkchild=%d %s",
449 if (l >= sizeof(cmdLine))
450 {
451 ereport(LOG,
452 (errmsg("subprocess command line too long")));
453 UnmapViewOfFile(param);
455 return -1;
456 }
457
458 memset(&pi, 0, sizeof(pi));
459 memset(&si, 0, sizeof(si));
460 si.cb = sizeof(si);
461
462 /*
463 * Create the subprocess in a suspended state. This will be resumed later,
464 * once we have written out the parameter file.
465 */
467 NULL, NULL, &si, &pi))
468 {
469 ereport(LOG,
470 (errmsg("CreateProcess() call failed: %m (error code %lu)",
471 GetLastError())));
472 UnmapViewOfFile(param);
474 return -1;
475 }
476
477 if (!save_backend_variables(param, child_slot, client_sock,
478 pi.hProcess, pi.dwProcessId,
480 {
481 /*
482 * log made by save_backend_variables, but we have to clean up the
483 * mess with the half-started process
484 */
485 if (!TerminateProcess(pi.hProcess, 255))
486 ereport(LOG,
487 (errmsg_internal("could not terminate unstarted process: error code %lu",
488 GetLastError())));
489 CloseHandle(pi.hProcess);
490 CloseHandle(pi.hThread);
491 UnmapViewOfFile(param);
493 return -1; /* log made by save_backend_variables */
494 }
495
496 /* Drop the parameter shared memory that is now inherited to the backend */
497 if (!UnmapViewOfFile(param))
498 ereport(LOG,
499 (errmsg("could not unmap view of backend parameter file: error code %lu",
500 GetLastError())));
502 ereport(LOG,
503 (errmsg("could not close handle to backend parameter file: error code %lu",
504 GetLastError())));
505
506 /*
507 * Reserve the memory region used by our main shared memory segment before
508 * we resume the child process. Normally this should succeed, but if ASLR
509 * is active then it might sometimes fail due to the stack or heap having
510 * gotten mapped into that range. In that case, just terminate the
511 * process and retry.
512 */
514 {
515 /* pgwin32_ReserveSharedMemoryRegion already made a log entry */
516 if (!TerminateProcess(pi.hProcess, 255))
517 ereport(LOG,
518 (errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
519 GetLastError())));
520 CloseHandle(pi.hProcess);
521 CloseHandle(pi.hThread);
522 if (++retry_count < 100)
523 goto retry;
524 ereport(LOG,
525 (errmsg("giving up after too many tries to reserve shared memory"),
526 errhint("This might be caused by ASLR or antivirus software.")));
527 return -1;
528 }
529
530 /*
531 * Now that the backend variables are written out, we start the child
532 * thread so it can start initializing while we set up the rest of the
533 * parent state.
534 */
535 if (ResumeThread(pi.hThread) == -1)
536 {
537 if (!TerminateProcess(pi.hProcess, 255))
538 {
539 ereport(LOG,
540 (errmsg_internal("could not terminate unstartable process: error code %lu",
541 GetLastError())));
542 CloseHandle(pi.hProcess);
543 CloseHandle(pi.hThread);
544 return -1;
545 }
546 CloseHandle(pi.hProcess);
547 CloseHandle(pi.hThread);
548 ereport(LOG,
549 (errmsg_internal("could not resume thread of unstarted process: error code %lu",
550 GetLastError())));
551 return -1;
552 }
553
554 /* Set up notification when the child process dies */
555 pgwin32_register_deadchild_callback(pi.hProcess, pi.dwProcessId);
556
557 /* Don't close pi.hProcess, it's owned by the deadchild callback now */
558
559 CloseHandle(pi.hThread);
560
561 return pi.dwProcessId;
562}
563#endif /* WIN32 */
564
565/*
566 * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
567 * to what it would be if we'd simply forked on Unix, and then
568 * dispatch to the appropriate place.
569 *
570 * The first two command line arguments are expected to be "--forkchild=<kind>",
571 * where <kind> indicates which process type we are to become, and
572 * the name of a variables file that we can read to load data that would
573 * have been inherited by fork() on Unix.
574 */
575void
576SubPostmasterMain(int argc, char *argv[])
577{
578 void *startup_data;
579 size_t startup_data_len;
580 char *child_kind;
582 TimestampTz fork_end;
583
584 /* In EXEC_BACKEND case we will not have inherited these settings */
587
588 /*
589 * Capture the end of process creation for logging. We don't include the
590 * time spent copying data from shared memory and setting up the backend.
591 */
592 fork_end = GetCurrentTimestamp();
593
594 /* Setup essential subsystems (to ensure elog() behaves sanely) */
596
597 /* Check we got appropriate args */
598 if (argc != 3)
599 elog(FATAL, "invalid subpostmaster invocation");
600
601 /*
602 * Parse the --forkchild argument to find our process type. We rely with
603 * malice aforethought on atoi returning 0 (B_INVALID) on error.
604 */
605 if (strncmp(argv[1], "--forkchild=", 12) != 0)
606 elog(FATAL, "invalid subpostmaster invocation (--forkchild argument missing)");
607 child_kind = argv[1] + 12;
610 elog(ERROR, "unknown child kind %s", child_kind);
612
613 /* Read in the variables file */
615
616 /* Close the postmaster's sockets (as soon as we know them) */
618
619 /* Setup as postmaster child */
621
622 /*
623 * If appropriate, physically re-attach to shared memory segment. We want
624 * to do this before going any further to ensure that we can attach at the
625 * same address the postmaster used. On the other hand, if we choose not
626 * to re-attach, we may have other cleanup to do.
627 *
628 * If testing EXEC_BACKEND on Linux, you should run this as root before
629 * starting the postmaster:
630 *
631 * sysctl -w kernel.randomize_va_space=0
632 *
633 * This prevents using randomized stack and code addresses that cause the
634 * child process's memory map to be different from the parent's, making it
635 * sometimes impossible to attach to shared memory at the desired address.
636 * Return the setting to its old value (usually '1' or '2') when finished.
637 */
638 if (child_process_kinds[child_type].shmem_attach)
640 else
642
643 /* Read in remaining GUC variables */
645
646 /* Capture and transfer timings that may be needed for log_connections */
648 {
650 ((BackendStartupData *) startup_data)->socket_created;
652 ((BackendStartupData *) startup_data)->fork_started;
653 conn_timing.fork_end = fork_end;
654 }
655
656 /*
657 * Check that the data directory looks valid, which will also check the
658 * privileges on the data directory and update our umask and file/group
659 * variables for creating files later. Note: this should really be done
660 * before we create any files or directories.
661 */
662 checkDataDir();
663
664 /*
665 * (re-)read control file, as it contains config. The postmaster will
666 * already have read this, but this process doesn't know about that.
667 */
669
670 /*
671 * Reload any libraries that were preloaded by the postmaster. Since we
672 * exec'd this process, those libraries didn't come along with us; but we
673 * should load them into all child processes to be consistent with the
674 * non-EXEC_BACKEND behavior.
675 */
677
678 /* Restore basic shared memory pointers */
679 if (UsedShmemSegAddr != NULL)
681
682 /*
683 * Run the appropriate Main function
684 */
686 pg_unreachable(); /* main_fn never returns */
687}
688
689#ifndef WIN32
690#define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
691#define read_inheritable_socket(dest, src) (*(dest) = *(src))
692#else
693static bool write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
696static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
697#endif
698
699
700/* Save critical backend variables into the BackendParameters struct */
701static bool
703 int child_slot, const ClientSocket *client_sock,
706#endif
707 const void *startup_data, size_t startup_data_len)
708{
709 if (client_sock)
710 memcpy(&param->client_sock, client_sock, sizeof(ClientSocket));
711 else
712 memset(&param->client_sock, 0, sizeof(ClientSocket));
713 if (!write_inheritable_socket(&param->inh_sock,
715 childPid))
717
718 strlcpy(param->DataDir, DataDir, MAXPGPATH);
719
720 param->MyPMChildSlot = child_slot;
721
722#ifdef WIN32
723 param->ShmemProtectiveRegion = ShmemProtectiveRegion;
724#endif
725 param->UsedShmemSegID = UsedShmemSegID;
726 param->UsedShmemSegAddr = UsedShmemSegAddr;
727
728#ifdef USE_INJECTION_POINTS
729 param->ActiveInjectionPoints = ActiveInjectionPoints;
730#endif
731
732 param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
733 param->NamedLWLockTrancheRequestArray = NamedLWLockTrancheRequestArray;
734 param->LWLockTrancheNames = LWLockTrancheNames;
735 param->LWLockCounter = LWLockCounter;
736 param->MainLWLockArray = MainLWLockArray;
737 param->ProcGlobal = ProcGlobal;
738 param->AuxiliaryProcs = AuxiliaryProcs;
739 param->PreparedXactProcs = PreparedXactProcs;
740 param->PMSignalState = PMSignalState;
741 param->ProcSignal = ProcSignal;
742
743 param->PostmasterPid = PostmasterPid;
744 param->PgStartTime = PgStartTime;
745 param->PgReloadTime = PgReloadTime;
746 param->first_syslogger_file_time = first_syslogger_file_time;
747
748 param->redirection_done = redirection_done;
749 param->IsBinaryUpgrade = IsBinaryUpgrade;
750 param->query_id_enabled = query_id_enabled;
751 param->max_safe_fds = max_safe_fds;
752
753 param->MaxBackends = MaxBackends;
754 param->num_pmchild_slots = num_pmchild_slots;
755
756#ifdef WIN32
757 param->PostmasterHandle = PostmasterHandle;
758 if (!write_duplicated_handle(&param->initial_signal_pipe,
761 return false;
762#else
763 memcpy(&param->postmaster_alive_fds, &postmaster_alive_fds,
764 sizeof(postmaster_alive_fds));
765#endif
766
767 memcpy(&param->syslogPipe, &syslogPipe, sizeof(syslogPipe));
768
769 strlcpy(param->my_exec_path, my_exec_path, MAXPGPATH);
770
771 strlcpy(param->pkglib_path, pkglib_path, MAXPGPATH);
772
773 param->startup_data_len = startup_data_len;
774 if (startup_data_len > 0)
775 memcpy(param->startup_data, startup_data, startup_data_len);
776
777 return true;
778}
779
780#ifdef WIN32
781/*
782 * Duplicate a handle for usage in a child process, and write the child
783 * process instance of the handle to the parameter file.
784 */
785static bool
787{
789
791 src,
793 &hChild,
794 0,
795 TRUE,
797 {
798 ereport(LOG,
799 (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %lu",
800 GetLastError())));
801 return false;
802 }
803
804 *dest = hChild;
805 return true;
806}
807
808/*
809 * Duplicate a socket for usage in a child process, and write the resulting
810 * structure to the parameter file.
811 * This is required because a number of LSPs (Layered Service Providers) very
812 * common on Windows (antivirus, firewalls, download managers etc) break
813 * straight socket inheritance.
814 */
815static bool
817{
818 dest->origsocket = src;
819 if (src != 0 && src != PGINVALID_SOCKET)
820 {
821 /* Actual socket */
822 if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
823 {
824 ereport(LOG,
825 (errmsg("could not duplicate socket %d for use in backend: error code %d",
826 (int) src, WSAGetLastError())));
827 return false;
828 }
829 }
830 return true;
831}
832
833/*
834 * Read a duplicate socket structure back, and get the socket descriptor.
835 */
836static void
838{
839 SOCKET s;
840
841 if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
842 {
843 /* Not a real socket! */
844 *dest = src->origsocket;
845 }
846 else
847 {
848 /* Actual socket, so create from structure */
852 &src->wsainfo,
853 0,
854 0);
855 if (s == INVALID_SOCKET)
856 {
857 write_stderr("could not create inherited socket: error code %d\n",
859 exit(1);
860 }
861 *dest = s;
862
863 /*
864 * To make sure we don't get two references to the same socket, close
865 * the original one. (This would happen when inheritance actually
866 * works..
867 */
868 closesocket(src->origsocket);
869 }
870}
871#endif
872
873static void
875{
876 BackendParameters param;
877
878#ifndef WIN32
879 /* Non-win32 implementation reads from file */
880 FILE *fp;
881
882 /* Open file */
883 fp = AllocateFile(id, PG_BINARY_R);
884 if (!fp)
885 {
886 write_stderr("could not open backend variables file \"%s\": %m\n", id);
887 exit(1);
888 }
889
890 if (fread(&param, sizeof(param), 1, fp) != 1)
891 {
892 write_stderr("could not read from backend variables file \"%s\": %m\n", id);
893 exit(1);
894 }
895
896 /* read startup data */
897 *startup_data_len = param.startup_data_len;
898 if (param.startup_data_len > 0)
899 {
901 if (fread(*startup_data, *startup_data_len, 1, fp) != 1)
902 {
903 write_stderr("could not read startup data from backend variables file \"%s\": %m\n",
904 id);
905 exit(1);
906 }
907 }
908 else
910
911 /* Release file */
912 FreeFile(fp);
913 if (unlink(id) != 0)
914 {
915 write_stderr("could not remove file \"%s\": %m\n", id);
916 exit(1);
917 }
918#else
919 /* Win32 version uses mapped file */
922
923#ifdef _WIN64
924 paramHandle = (HANDLE) _atoi64(id);
925#else
926 paramHandle = (HANDLE) atol(id);
927#endif
929 if (!paramp)
930 {
931 write_stderr("could not map view of backend variables: error code %lu\n",
932 GetLastError());
933 exit(1);
934 }
935
936 memcpy(&param, paramp, sizeof(BackendParameters));
937
938 /* read startup data */
939 *startup_data_len = param.startup_data_len;
940 if (param.startup_data_len > 0)
941 {
942 *startup_data = palloc(paramp->startup_data_len);
943 memcpy(*startup_data, paramp->startup_data, param.startup_data_len);
944 }
945 else
947
949 {
950 write_stderr("could not unmap view of backend variables: error code %lu\n",
951 GetLastError());
952 exit(1);
953 }
954
956 {
957 write_stderr("could not close handle to backend parameter variables: error code %lu\n",
958 GetLastError());
959 exit(1);
960 }
961#endif
962
964}
965
966/* Restore critical backend variables from the BackendParameters struct */
967static void
969{
970 if (param->client_sock.sock != PGINVALID_SOCKET)
971 {
973 memcpy(MyClientSocket, &param->client_sock, sizeof(ClientSocket));
974 read_inheritable_socket(&MyClientSocket->sock, &param->inh_sock);
975 }
976
977 SetDataDir(param->DataDir);
978
979 MyPMChildSlot = param->MyPMChildSlot;
980
981#ifdef WIN32
982 ShmemProtectiveRegion = param->ShmemProtectiveRegion;
983#endif
984 UsedShmemSegID = param->UsedShmemSegID;
985 UsedShmemSegAddr = param->UsedShmemSegAddr;
986
987#ifdef USE_INJECTION_POINTS
988 ActiveInjectionPoints = param->ActiveInjectionPoints;
989#endif
990
991 NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
992 NamedLWLockTrancheRequestArray = param->NamedLWLockTrancheRequestArray;
993 LWLockTrancheNames = param->LWLockTrancheNames;
994 LWLockCounter = param->LWLockCounter;
995 MainLWLockArray = param->MainLWLockArray;
996 ProcGlobal = param->ProcGlobal;
997 AuxiliaryProcs = param->AuxiliaryProcs;
998 PreparedXactProcs = param->PreparedXactProcs;
999 PMSignalState = param->PMSignalState;
1000 ProcSignal = param->ProcSignal;
1001
1002 PostmasterPid = param->PostmasterPid;
1003 PgStartTime = param->PgStartTime;
1004 PgReloadTime = param->PgReloadTime;
1005 first_syslogger_file_time = param->first_syslogger_file_time;
1006
1007 redirection_done = param->redirection_done;
1008 IsBinaryUpgrade = param->IsBinaryUpgrade;
1009 query_id_enabled = param->query_id_enabled;
1010 max_safe_fds = param->max_safe_fds;
1011
1012 MaxBackends = param->MaxBackends;
1013 num_pmchild_slots = param->num_pmchild_slots;
1014
1015#ifdef WIN32
1016 PostmasterHandle = param->PostmasterHandle;
1017 pgwin32_initial_signal_pipe = param->initial_signal_pipe;
1018#else
1019 memcpy(&postmaster_alive_fds, &param->postmaster_alive_fds,
1020 sizeof(postmaster_alive_fds));
1021#endif
1022
1023 memcpy(&syslogPipe, &param->syslogPipe, sizeof(syslogPipe));
1024
1025 strlcpy(my_exec_path, param->my_exec_path, MAXPGPATH);
1026
1027 strlcpy(pkglib_path, param->pkglib_path, MAXPGPATH);
1028
1029 /*
1030 * We need to restore fd.c's counts of externally-opened FDs; to avoid
1031 * confusion, be sure to do this after restoring max_safe_fds. (Note:
1032 * BackendInitialize will handle this for (*client_sock)->sock.)
1033 */
1034#ifndef WIN32
1035 if (postmaster_alive_fds[0] >= 0)
1037 if (postmaster_alive_fds[1] >= 0)
1039#endif
1040}
1041
1042#endif /* EXEC_BACKEND */
TimestampTz PgReloadTime
Definition timestamp.c:57
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
TimestampTz PgStartTime
Definition timestamp.c:54
ConnectionTiming conn_timing
#define write_stderr(str)
Definition parallel.c:186
#define PG_BINARY_R
Definition c.h:1311
#define Assert(condition)
Definition c.h:885
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:492
#define pg_unreachable()
Definition c.h:353
#define PG_BINARY_W
Definition c.h:1312
int64 TimestampTz
Definition timestamp.h:39
@ DestNone
Definition dest.h:87
void dsm_detach_all(void)
Definition dsm.c:775
int errcode_for_file_access(void)
Definition elog.c:897
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define LOG
Definition elog.h:31
int errhint(const char *fmt,...) pg_attribute_printf(1
#define FATAL
Definition elog.h:41
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
int MakePGDirectory(const char *directoryName)
Definition fd.c:3962
int max_safe_fds
Definition fd.c:159
int FreeFile(FILE *file)
Definition fd.c:2826
void ReserveExternalFD(void)
Definition fd.c:1206
FILE * AllocateFile(const char *name, const char *mode)
Definition fd.c:2627
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_TEMP_FILES_DIR
Definition file_utils.h:63
#define PG_TEMP_FILE_PREFIX
Definition file_utils.h:64
pid_t fork_process(void)
struct ClientSocket * MyClientSocket
Definition globals.c:50
bool IsBinaryUpgrade
Definition globals.c:121
int MyPMChildSlot
Definition globals.c:54
pid_t PostmasterPid
Definition globals.c:106
int MyProcPid
Definition globals.c:47
char pkglib_path[MAXPGPATH]
Definition globals.c:82
bool IsUnderPostmaster
Definition globals.c:120
int MaxBackends
Definition globals.c:146
char * DataDir
Definition globals.c:71
bool IsPostmasterEnvironment
Definition globals.c:119
char my_exec_path[MAXPGPATH]
Definition globals.c:81
void InitializeGUCOptions(void)
Definition guc.c:1407
#define false
static child_process_kind child_process_kinds[]
pid_t postmaster_child_launch(BackendType child_type, int child_slot, void *startup_data, size_t startup_data_len, const ClientSocket *client_sock)
const char * PostmasterChildName(BackendType child_type)
char ** LWLockTrancheNames
Definition lwlock.c:154
int NamedLWLockTrancheRequests
Definition lwlock.c:192
NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition lwlock.c:193
LWLockPadded * MainLWLockArray
Definition lwlock.c:161
int * LWLockCounter
Definition lwlock.c:199
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
MemoryContext TopMemoryContext
Definition mcxt.c:166
void * palloc(Size size)
Definition mcxt.c:1387
#define IsExternalConnectionBackend(backend_type)
Definition miscadmin.h:405
#define BACKEND_NUM_TYPES
Definition miscadmin.h:377
BackendType
Definition miscadmin.h:338
@ B_LOGGER
Definition miscadmin.h:374
void InitPostmasterChild(void)
Definition miscinit.c:96
void process_shared_preload_libraries(void)
Definition miscinit.c:1851
void checkDataDir(void)
Definition miscinit.c:296
BackendType MyBackendType
Definition miscinit.c:64
void SetDataDir(const char *dir)
Definition miscinit.c:389
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define MAXPGPATH
int64 pg_time_t
Definition pgtime.h:23
NON_EXEC_STATIC int num_pmchild_slots
Definition pmchild.c:55
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition pmsignal.c:84
#define sprintf
Definition port.h:262
#define snprintf
Definition port.h:260
#define PGINVALID_SOCKET
Definition port.h:31
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
#define closesocket
Definition port.h:397
CommandDest whereToSendOutput
Definition postgres.c:93
bool redirection_done
Definition postmaster.c:375
int postmaster_alive_fds[2]
Definition postmaster.c:483
void ClosePostmasterPorts(bool am_syslogger)
static int fb(int x)
NON_EXEC_STATIC ProcSignalHeader * ProcSignal
Definition procsignal.c:107
bool query_id_enabled
void InitShmemAllocator(PGShmemHeader *seghdr)
Definition shmem.c:122
HANDLE pgwin32_create_signal_listener(pid_t pid)
Definition signal.c:227
HANDLE pgwin32_initial_signal_pipe
Definition signal.c:28
PGPROC * PreparedXactProcs
Definition proc.c:72
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition proc.c:71
PROC_HDR * ProcGlobal
Definition proc.c:70
pgsocket sock
Definition libpq-be.h:250
TimestampTz socket_create
TimestampTz fork_start
Definition proc.h:176
void(* main_fn)(const void *startup_data, size_t startup_data_len)
NON_EXEC_STATIC pg_time_t first_syslogger_file_time
Definition syslogger.c:87
int syslogPipe[2]
Definition syslogger.c:114
void PGSharedMemoryDetach(void)
Definition sysv_shmem.c:972
unsigned long UsedShmemSegID
Definition sysv_shmem.c:95
void * UsedShmemSegAddr
Definition sysv_shmem.c:96
void PGSharedMemoryReAttach(void)
void * ShmemProtectiveRegion
Definition win32_shmem.c:42
int pgwin32_ReserveSharedMemoryRegion(HANDLE hChild)
void PGSharedMemoryNoReAttach(void)
void LocalProcessControlFile(bool reset)
Definition xlog.c:4939