LCOV - differential code coverage report
Current view: top level - src/backend/postmaster - fork_process.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 58.8 % 17 10 7 10
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 1 1 1
Baseline: 16@8cea358b128 Branches: 37.5 % 8 3 5 3
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 58.8 % 17 10 7 10
Function coverage date bins:
(240..) days: 100.0 % 1 1 1
Branch coverage date bins:
(240..) days: 37.5 % 8 3 5 3

 Age         Owner                    Branch data    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-2024, 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
 6975 neilc@samurai.com          32                 :CBC       19834 : fork_process(void)
                                 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                 :                :      */
  594 tgl@sss.pgh.pa.us          45                 :          19834 :     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                 :                :      */
  458 tmunro@postgresql.or       64                 :          19834 :     sigprocmask(SIG_SETMASK, &BlockSig, &save_mask);
 6975 neilc@samurai.com          65                 :          19834 :     result = fork();
 6674 bruce@momjian.us           66         [ +  + ]:          39527 :     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                 :                :          */
 3588 tgl@sss.pgh.pa.us          89                 :          19693 :         oomfilename = getenv("PG_OOM_ADJUST_FILE");
                                 90                 :                : 
                                 91         [ -  + ]:          19693 :         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                 :                :              */
 3588 tgl@sss.pgh.pa.us          97                 :UBC           0 :             int         fd = open(oomfilename, O_WRONLY, 0);
                                 98                 :                : 
                                 99                 :                :             /* We ignore all errors */
 5207                           100         [ #  # ]:              0 :             if (fd >= 0)
                                101                 :                :             {
 3588                           102                 :              0 :                 const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
                                103                 :                :                 int         rc;
                                104                 :                : 
                                105         [ #  # ]:              0 :                 if (oomvalue == NULL)   /* supply a useful default */
                                106                 :              0 :                     oomvalue = "0";
                                107                 :                : 
                                108                 :              0 :                 rc = write(fd, oomvalue, strlen(oomvalue));
                                109                 :                :                 (void) rc;
 5207                           110                 :              0 :                 close(fd);
                                111                 :                :             }
                                112                 :                :         }
                                113                 :                : 
                                114                 :                :         /* do post-fork initialization for random number generation */
 1255 magnus@hagander.net       115                 :CBC       19693 :         pg_strong_random_init();
                                116                 :                :     }
                                117                 :                :     else
                                118                 :                :     {
                                119                 :                :         /* in parent, restore signal mask */
  458 tmunro@postgresql.or      120                 :          19834 :         sigprocmask(SIG_SETMASK, &save_mask, NULL);
                                121                 :                :     }
                                122                 :                : 
 6975 neilc@samurai.com         123                 :          39527 :     return result;
                                124                 :                : }
                                125                 :                : 
                                126                 :                : #endif                          /* ! WIN32 */
        

Generated by: LCOV version 2.1-beta2-3-g6141622