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 17:13:01 Functions: 100.0 % 1 1 1 1
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 100.0 % 2 2 2
Legend: Lines: hit not hit (180,240] days: 100.0 % 1 1 1
(240..) days: 50.0 % 14 7 1 4 2 7 5 6
Function coverage date bins:
(240..) days: 50.0 % 2 1 1 1

 Age         Owner                  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
 6604 neilc                      32 GIC       14038 : fork_process(void)
                                 33                 : {
                                 34                 :     pid_t       result;
 3217 tgl                        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                 :      */
  223 tgl                        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                 :      */
   87 tmunro                     64           14038 :     sigprocmask(SIG_SETMASK, &BlockSig, &save_mask);
 6604 neilc                      65 GIC       14038 :     result = fork();
 6303 bruce                      66           26766 :     if (result == 0)
                                 67                 :     {
                                 68                 :         /* fork succeeded, in child */
                                 69                 : #ifdef LINUX_PROFILE
 6604 neilc                      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                 :          */
 3217 tgl                        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
 4790 bruce                      95 ECB             :              * Linux security environments reject anything but O_WRONLY.
                                 96                 :              */
 3217 tgl                        97 LBC           0 :             int         fd = open(oomfilename, O_WRONLY, 0);
                                 98                 : 
                                 99                 :             /* We ignore all errors */
 4836 tgl                       100 UIC           0 :             if (fd >= 0)
                                101                 :             {
 3217                           102               0 :                 const char *oomvalue = getenv("PG_OOM_ADJUST_VALUE");
 3969 peter_e                   103 EUB             :                 int         rc;
                                104                 : 
 3217 tgl                       105 UIC           0 :                 if (oomvalue == NULL)   /* supply a useful default */
 3217 tgl                       106 UBC           0 :                     oomvalue = "0";
                                107                 : 
                                108               0 :                 rc = write(fd, oomvalue, strlen(oomvalue));
                                109                 :                 (void) rc;
 4836 tgl                       110 UIC           0 :                 close(fd);
 4836 tgl                       111 EUB             :             }
                                112                 :         }
                                113                 : 
  884 magnus                    114                 :         /* do post-fork initialization for random number generation */
  884 magnus                    115 GIC       12728 :         pg_strong_random_init();
 6604 neilc                     116 EUB             :     }
                                117                 :     else
                                118                 :     {
                                119                 :         /* in parent, restore signal mask */
   87 tmunro                    120 GNC       14038 :         sigprocmask(SIG_SETMASK, &save_mask, NULL);
                                121                 :     }
                                122                 : 
 6604 neilc                     123 GIC       26766 :     return result;
                                124                 : }
                                125                 : 
 2118 tgl                       126 ECB             : #endif                          /* ! WIN32 */
        

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