PostgreSQL Source Code  git master
fork_process.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "postmaster/fork_process.h"
Include dependency graph for fork_process.c:

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 33 of file fork_process.c.

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

Referenced by postmaster_child_launch().