PostgreSQL Source Code  git master
fork_process.c
Go to the documentation of this file.
1 /*
2  * fork_process.c
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.
6  *
7  * Copyright (c) 1996-2019, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  * src/backend/postmaster/fork_process.c
11  */
12 #include "postgres.h"
13 
14 #include <fcntl.h>
15 #include <time.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <unistd.h>
19 #ifdef USE_OPENSSL
20 #include <openssl/rand.h>
21 #endif
22 
24 
25 #ifndef WIN32
26 /*
27  * Wrapper for fork(). Return values are the same as those for fork():
28  * -1 if the fork failed, 0 in the child process, and the PID of the
29  * child in the parent process.
30  */
31 pid_t
33 {
34  pid_t result;
35  const char *oomfilename;
36 
37 #ifdef LINUX_PROFILE
38  struct itimerval prof_itimer;
39 #endif
40 
41  /*
42  * Flush stdio channels just before fork, to avoid double-output problems.
43  * Ideally we'd use fflush(NULL) here, but there are still a few non-ANSI
44  * stdio libraries out there (like SunOS 4.1.x) that coredump if we do.
45  * Presently stdout and stderr are the only stdio output channels used by
46  * the postmaster, so fflush'ing them should be sufficient.
47  */
48  fflush(stdout);
49  fflush(stderr);
50 
51 #ifdef LINUX_PROFILE
52 
53  /*
54  * Linux's fork() resets the profiling timer in the child process. If we
55  * want to profile child processes then we need to save and restore the
56  * timer setting. This is a waste of time if not profiling, however, so
57  * only do it if commanded by specific -DLINUX_PROFILE switch.
58  */
59  getitimer(ITIMER_PROF, &prof_itimer);
60 #endif
61 
62  result = fork();
63  if (result == 0)
64  {
65  /* fork succeeded, in child */
66 #ifdef LINUX_PROFILE
67  setitimer(ITIMER_PROF, &prof_itimer, NULL);
68 #endif
69 
70  /*
71  * By default, Linux tends to kill the postmaster in out-of-memory
72  * situations, because it blames the postmaster for the sum of child
73  * process sizes *including shared memory*. (This is unbelievably
74  * stupid, but the kernel hackers seem uninterested in improving it.)
75  * Therefore it's often a good idea to protect the postmaster by
76  * setting its OOM score adjustment negative (which has to be done in
77  * a root-owned startup script). Since the adjustment is inherited by
78  * child processes, this would ordinarily mean that all the
79  * postmaster's children are equally protected against OOM kill, which
80  * is not such a good idea. So we provide this code to allow the
81  * children to change their OOM score adjustments again. Both the
82  * file name to write to and the value to write are controlled by
83  * environment variables, which can be set by the same startup script
84  * that did the original adjustment.
85  */
86  oomfilename = getenv("PG_OOM_ADJUST_FILE");
87 
88  if (oomfilename != NULL)
89  {
90  /*
91  * Use open() not stdio, to ensure we control the open flags. Some
92  * Linux security environments reject anything but O_WRONLY.
93  */
94  int fd = open(oomfilename, O_WRONLY, 0);
95 
96  /* We ignore all errors */
97  if (fd >= 0)
98  {
99  const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
100  int rc;
101 
102  if (oomvalue == NULL) /* supply a useful default */
103  oomvalue = "0";
104 
105  rc = write(fd, oomvalue, strlen(oomvalue));
106  (void) rc;
107  close(fd);
108  }
109  }
110 
111  /*
112  * Make sure processes do not share OpenSSL randomness state.
113  */
114 #ifdef USE_OPENSSL
115  RAND_cleanup();
116 #endif
117  }
118 
119  return result;
120 }
121 
122 #endif /* ! WIN32 */
#define write(a, b, c)
Definition: win32.h:14
pid_t fork_process(void)
Definition: fork_process.c:32
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: timer.c:86
#define close(a)
Definition: win32.h:12