OSDN Git Service

Standard pgindent run for 8.1.
[pg-rex/syncrep.git] / src / backend / postmaster / fork_process.c
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-2005, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/postmaster/fork_process.c,v 1.4 2005/10/15 02:49:23 momjian Exp $
11  */
12 #include "postgres.h"
13 #include "postmaster/fork_process.h"
14
15 #include <time.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18
19 #ifndef WIN32
20 /*
21  * Wrapper for fork(). Return values are the same as those for fork():
22  * -1 if the fork failed, 0 in the child process, and the PID of the
23  * child in the parent process.
24  */
25 pid_t
26 fork_process(void)
27 {
28         pid_t           result;
29
30 #ifdef LINUX_PROFILE
31         struct itimerval prof_itimer;
32 #endif
33
34         /*
35          * Flush stdio channels just before fork, to avoid double-output problems.
36          * Ideally we'd use fflush(NULL) here, but there are still a few non-ANSI
37          * stdio libraries out there (like SunOS 4.1.x) that coredump if we do.
38          * Presently stdout and stderr are the only stdio output channels used by
39          * the postmaster, so fflush'ing them should be sufficient.
40          */
41         fflush(stdout);
42         fflush(stderr);
43
44 #ifdef LINUX_PROFILE
45
46         /*
47          * Linux's fork() resets the profiling timer in the child process. If we
48          * want to profile child processes then we need to save and restore the
49          * timer setting.  This is a waste of time if not profiling, however, so
50          * only do it if commanded by specific -DLINUX_PROFILE switch.
51          */
52         getitimer(ITIMER_PROF, &prof_itimer);
53 #endif
54
55 #ifdef __BEOS__
56         /* Specific beos actions before backend startup */
57         beos_before_backend_startup();
58 #endif
59
60         result = fork();
61         if (result == (pid_t) -1)
62         {
63                 /* fork failed */
64 #ifdef __BEOS__
65                 /* Specific beos backend startup actions */
66                 beos_backend_startup_failed();
67 #endif
68         }
69         else if (result == 0)
70         {
71                 /* fork succeeded, in child */
72 #ifdef LINUX_PROFILE
73                 setitimer(ITIMER_PROF, &prof_itimer, NULL);
74 #endif
75
76 #ifdef __BEOS__
77                 /* Specific beos backend startup actions */
78                 beos_backend_startup();
79 #endif
80         }
81
82         return result;
83 }
84
85 #endif   /* ! WIN32 */