LCOV - differential code coverage report
Current view: top level - src/backend/postmaster - fork_process.c (source / functions) Coverage Total Hit LBC UIC UBC GIC GNC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 58.8 % 17 10 1 4 2 7 3 5 8 1
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 1 1 1 1
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  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-2023, 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 <signal.h>
      16                 : #include <time.h>
      17                 : #include <sys/stat.h>
      18                 : #include <sys/time.h>
      19                 : #include <unistd.h>
      20                 : 
      21                 : #include "libpq/pqsignal.h"
      22                 : #include "postmaster/fork_process.h"
      23                 : 
      24                 : #ifndef WIN32
      25                 : /*
      26                 :  * Wrapper for fork(). Return values are the same as those for fork():
      27                 :  * -1 if the fork failed, 0 in the child process, and the PID of the
      28                 :  * child in the parent process.  Signals are blocked while forking, so
      29                 :  * the child must unblock.
      30                 :  */
      31                 : pid_t
      32 GIC       14038 : fork_process(void)
      33                 : {
      34                 :     pid_t       result;
      35 ECB             :     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 GNC       14038 :     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 signal.
      62                 :      * With more analysis this could potentially be relaxed.
      63                 :      */
      64           14038 :     sigprocmask(SIG_SETMASK, &BlockSig, &save_mask);
      65 GIC       14038 :     result = fork();
      66           26766 :     if (result == 0)
      67                 :     {
      68                 :         /* fork succeeded, in child */
      69                 : #ifdef LINUX_PROFILE
      70 ECB             :         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 GIC       12728 :         oomfilename = getenv("PG_OOM_ADJUST_FILE");
      90                 : 
      91           12728 :         if (oomfilename != NULL)
      92                 :         {
      93                 :             /*
      94                 :              * Use open() not stdio, to ensure we control the open flags. Some
      95 ECB             :              * Linux security environments reject anything but O_WRONLY.
      96                 :              */
      97 LBC           0 :             int         fd = open(oomfilename, O_WRONLY, 0);
      98                 : 
      99                 :             /* We ignore all errors */
     100 UIC           0 :             if (fd >= 0)
     101                 :             {
     102               0 :                 const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
     103 EUB             :                 int         rc;
     104                 : 
     105 UIC           0 :                 if (oomvalue == NULL)   /* supply a useful default */
     106 UBC           0 :                     oomvalue = "0";
     107                 : 
     108               0 :                 rc = write(fd, oomvalue, strlen(oomvalue));
     109                 :                 (void) rc;
     110 UIC           0 :                 close(fd);
     111 EUB             :             }
     112                 :         }
     113                 : 
     114                 :         /* do post-fork initialization for random number generation */
     115 GIC       12728 :         pg_strong_random_init();
     116 EUB             :     }
     117                 :     else
     118                 :     {
     119                 :         /* in parent, restore signal mask */
     120 GNC       14038 :         sigprocmask(SIG_SETMASK, &save_mask, NULL);
     121                 :     }
     122                 : 
     123 GIC       26766 :     return result;
     124                 : }
     125                 : 
     126 ECB             : #endif                          /* ! WIN32 */
        

Generated by: LCOV version v1.16-55-g56c0a2a