PostgreSQL Source Code  git master
fork_process.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

pid_t fork_process (void)
 

Function Documentation

◆ fork_process()

pid_t fork_process ( void  )

Definition at line 32 of file fork_process.c.

33 {
34  pid_t result;
35  const char *oomfilename;
36  sigset_t save_mask;
37 
38 #ifdef LINUX_PROFILE
39  struct itimerval prof_itimer;
40 #endif
41 
42  /*
43  * Flush stdio channels just before fork, to avoid double-output problems.
44  */
45  fflush(NULL);
46 
47 #ifdef LINUX_PROFILE
48 
49  /*
50  * Linux's fork() resets the profiling timer in the child process. If we
51  * want to profile child processes then we need to save and restore the
52  * timer setting. This is a waste of time if not profiling, however, so
53  * only do it if commanded by specific -DLINUX_PROFILE switch.
54  */
55  getitimer(ITIMER_PROF, &prof_itimer);
56 #endif
57 
58  /*
59  * We start postmaster children with signals blocked. This allows them to
60  * install their own handlers before unblocking, to avoid races where they
61  * might run the postmaster's handler and miss an important control
62  * signal. With more analysis this could potentially be relaxed.
63  */
64  sigprocmask(SIG_SETMASK, &BlockSig, &save_mask);
65  result = fork();
66  if (result == 0)
67  {
68  /* fork succeeded, in child */
69 #ifdef LINUX_PROFILE
70  setitimer(ITIMER_PROF, &prof_itimer, NULL);
71 #endif
72 
73  /*
74  * By default, Linux tends to kill the postmaster in out-of-memory
75  * situations, because it blames the postmaster for the sum of child
76  * process sizes *including shared memory*. (This is unbelievably
77  * stupid, but the kernel hackers seem uninterested in improving it.)
78  * Therefore it's often a good idea to protect the postmaster by
79  * setting its OOM score adjustment negative (which has to be done in
80  * a root-owned startup script). Since the adjustment is inherited by
81  * child processes, this would ordinarily mean that all the
82  * postmaster's children are equally protected against OOM kill, which
83  * is not such a good idea. So we provide this code to allow the
84  * children to change their OOM score adjustments again. Both the
85  * file name to write to and the value to write are controlled by
86  * environment variables, which can be set by the same startup script
87  * that did the original adjustment.
88  */
89  oomfilename = getenv("PG_OOM_ADJUST_FILE");
90 
91  if (oomfilename != NULL)
92  {
93  /*
94  * Use open() not stdio, to ensure we control the open flags. Some
95  * Linux security environments reject anything but O_WRONLY.
96  */
97  int fd = open(oomfilename, O_WRONLY, 0);
98 
99  /* We ignore all errors */
100  if (fd >= 0)
101  {
102  const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
103  int rc;
104 
105  if (oomvalue == NULL) /* supply a useful default */
106  oomvalue = "0";
107 
108  rc = write(fd, oomvalue, strlen(oomvalue));
109  (void) rc;
110  close(fd);
111  }
112  }
113 
114  /* do post-fork initialization for random number generation */
116  }
117  else
118  {
119  /* in parent, restore signal mask */
120  sigprocmask(SIG_SETMASK, &save_mask, NULL);
121  }
122 
123  return result;
124 }
sigset_t BlockSig
Definition: pqsignal.c:23
#define close(a)
Definition: win32.h:12
#define write(a, b, c)
Definition: win32.h:14
static void const char fflush(stdout)
void pg_strong_random_init(void)
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

References BlockSig, close, fd(), fflush(), pg_strong_random_init(), setitimer(), and write.

Referenced by postmaster_child_launch().