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 32 39170 : 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 : */ 45 39170 : 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 : */ 64 39170 : sigprocmask(SIG_SETMASK, &BlockSig, &save_mask); 65 39170 : result = fork(); 66 74836 : 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 : */ 89 35672 : oomfilename = getenv("PG_OOM_ADJUST_FILE"); 90 : 91 35672 : 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 : */ 97 0 : int fd = open(oomfilename, O_WRONLY, 0); 98 : 99 : /* We ignore all errors */ 100 0 : if (fd >= 0) 101 : { 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; 110 0 : close(fd); 111 : } 112 : } 113 : 114 : /* do post-fork initialization for random number generation */ 115 35672 : pg_strong_random_init(); 116 : } 117 : else 118 : { 119 : /* in parent, restore signal mask */ 120 39164 : sigprocmask(SIG_SETMASK, &save_mask, NULL); 121 : } 122 : 123 74836 : return result; 124 : } 125 : 126 : #endif /* ! WIN32 */