3 * A simple wrapper on top of fork(). This does not handle the
4 * EXEC_BACKEND case; it might be extended to do so, but it would be
5 * considerably more complex.
7 * Copyright (c) 1996-2018, PostgreSQL Global Development Group
10 * src/backend/postmaster/fork_process.c
13 #include "postmaster/fork_process.h"
21 #include <openssl/rand.h>
26 * Wrapper for fork(). Return values are the same as those for fork():
27 * -1 if the fork failed, 0 in the child process, and the PID of the
28 * child in the parent process.
34 const char *oomfilename;
37 struct itimerval prof_itimer;
41 * Flush stdio channels just before fork, to avoid double-output problems.
42 * Ideally we'd use fflush(NULL) here, but there are still a few non-ANSI
43 * stdio libraries out there (like SunOS 4.1.x) that coredump if we do.
44 * Presently stdout and stderr are the only stdio output channels used by
45 * the postmaster, so fflush'ing them should be sufficient.
53 * Linux's fork() resets the profiling timer in the child process. If we
54 * want to profile child processes then we need to save and restore the
55 * timer setting. This is a waste of time if not profiling, however, so
56 * only do it if commanded by specific -DLINUX_PROFILE switch.
58 getitimer(ITIMER_PROF, &prof_itimer);
64 /* fork succeeded, in child */
66 setitimer(ITIMER_PROF, &prof_itimer, NULL);
70 * By default, Linux tends to kill the postmaster in out-of-memory
71 * situations, because it blames the postmaster for the sum of child
72 * process sizes *including shared memory*. (This is unbelievably
73 * stupid, but the kernel hackers seem uninterested in improving it.)
74 * Therefore it's often a good idea to protect the postmaster by
75 * setting its OOM score adjustment negative (which has to be done in
76 * a root-owned startup script). Since the adjustment is inherited by
77 * child processes, this would ordinarily mean that all the
78 * postmaster's children are equally protected against OOM kill, which
79 * is not such a good idea. So we provide this code to allow the
80 * children to change their OOM score adjustments again. Both the
81 * file name to write to and the value to write are controlled by
82 * environment variables, which can be set by the same startup script
83 * that did the original adjustment.
85 oomfilename = getenv("PG_OOM_ADJUST_FILE");
87 if (oomfilename != NULL)
90 * Use open() not stdio, to ensure we control the open flags. Some
91 * Linux security environments reject anything but O_WRONLY.
93 int fd = open(oomfilename, O_WRONLY, 0);
95 /* We ignore all errors */
98 const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
101 if (oomvalue == NULL) /* supply a useful default */
104 rc = write(fd, oomvalue, strlen(oomvalue));
111 * Make sure processes do not share OpenSSL randomness state.