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.

References close, fd(), setitimer(), generate_unaccent_rules::stdout, and write.

Referenced by BackendRun(), BackendStartup(), do_start_bgworker(), pgarch_start(), pgstat_start(), StartAutoVacLauncher(), StartAutoVacWorker(), StartChildProcess(), and SysLogger_Start().

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 }
#define write(a, b, c)
Definition: win32.h:14
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