LCOV - code coverage report
Current view: top level - src/backend/postmaster - fork_process.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 9 16 56.2 %
Date: 2020-08-05 08:07:15 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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-2020, 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             : 
      23             : #include "postmaster/fork_process.h"
      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
      32       15002 : fork_process(void)
      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       15002 :     fflush(stdout);
      49       15002 :     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       15002 :     result = fork();
      63       28324 :     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       13328 :         oomfilename = getenv("PG_OOM_ADJUST_FILE");
      87             : 
      88       13328 :         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           0 :             int         fd = open(oomfilename, O_WRONLY, 0);
      95             : 
      96             :             /* We ignore all errors */
      97           0 :             if (fd >= 0)
      98             :             {
      99           0 :                 const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
     100             :                 int         rc;
     101             : 
     102           0 :                 if (oomvalue == NULL)   /* supply a useful default */
     103           0 :                     oomvalue = "0";
     104             : 
     105           0 :                 rc = write(fd, oomvalue, strlen(oomvalue));
     106             :                 (void) rc;
     107           0 :                 close(fd);
     108             :             }
     109             :         }
     110             : 
     111             :         /*
     112             :          * Make sure processes do not share OpenSSL randomness state. This is
     113             :          * no longer required in OpenSSL 1.1.1 and later versions, but until
     114             :          * we drop support for version < 1.1.1 we need to do this.
     115             :          */
     116             : #ifdef USE_OPENSSL
     117       13328 :         RAND_poll();
     118             : #endif
     119             :     }
     120             : 
     121       28324 :     return result;
     122             : }
     123             : 
     124             : #endif                          /* ! WIN32 */

Generated by: LCOV version 1.13