Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit164adc4

Browse files
author
Neil Conway
committed
Refactor fork()-related code. We need to do various housekeeping tasks
before we can invoke fork() -- flush stdio buffers, save and restore theprofiling timer on Linux with LINUX_PROFILE, and handle BeOS stuff. Thispatch moves that code into a single function, fork_process(), instead ofduplicating it at the various callsites of fork().This patch doesn't address the EXEC_BACKEND case; there is room forfurther cleanup there.
1 parente829f82 commit164adc4

File tree

7 files changed

+108
-151
lines changed

7 files changed

+108
-151
lines changed

‎src/backend/port/beos/support.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ beos_startup(int argc, char **argv)
265265

266266

267267

268-
/* The behavior of fork isborken on beos regarding shared memory. In fact
268+
/* The behavior of fork isbroken on beos regarding shared memory. In fact
269269
all shared memory areas are clones in copy on write mode in the new process.
270270
271271
We need to do a remapping of these areas. Just afer the fork we performe the

‎src/backend/postmaster/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
# Makefile for src/backend/postmaster
55
#
66
# IDENTIFICATION
7-
# $PostgreSQL: pgsql/src/backend/postmaster/Makefile,v 1.19 2004/08/05 23:32:10 tgl Exp $
7+
# $PostgreSQL: pgsql/src/backend/postmaster/Makefile,v 1.20 2005/03/10 07:14:03 neilc Exp $
88
#
99
#-------------------------------------------------------------------------
1010

1111
subdir = src/backend/postmaster
1212
top_builddir = ../../..
1313
include$(top_builddir)/src/Makefile.global
1414

15-
OBJS =postmaster.obgwriter.o pgstat.opgarch.o syslogger.o
15+
OBJS =bgwriter.ofork_process.opgarch.opgstat.opostmaster.o syslogger.o
1616

1717
all: SUBSYS.o
1818

‎src/backend/postmaster/fork_process.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.1 2005/03/10 07:14:03 neilc Exp $
11+
*/
12+
#include"postgres.h"
13+
#include"postmaster/fork_process.h"
14+
15+
#include<unistd.h>
16+
17+
/*
18+
* Wrapper for fork(). Return values are the same as those for fork():
19+
* -1 if the fork failed, 0 in the child process, and the PID of the
20+
* child in the parent process.
21+
*/
22+
pid_t
23+
fork_process(void)
24+
{
25+
pid_tresult;
26+
#ifdefLINUX_PROFILE
27+
structitimervalprof_itimer;
28+
#endif
29+
30+
/*
31+
* Flush stdio channels just before fork, to avoid double-output
32+
* problems. Ideally we'd use fflush(NULL) here, but there are still a
33+
* few non-ANSI stdio libraries out there (like SunOS 4.1.x) that
34+
* coredump if we do. Presently stdout and stderr are the only stdio
35+
* output channels used by the postmaster, so fflush'ing them should
36+
* be sufficient.
37+
*/
38+
fflush(stdout);
39+
fflush(stderr);
40+
41+
#ifdefLINUX_PROFILE
42+
/*
43+
* Linux's fork() resets the profiling timer in the child process. If
44+
* we want to profile child processes then we need to save and restore
45+
* the timer setting. This is a waste of time if not profiling,
46+
* however, so only do it if commanded by specific -DLINUX_PROFILE
47+
* switch.
48+
*/
49+
getitimer(ITIMER_PROF,&prof_itimer);
50+
#endif
51+
52+
#ifdef__BEOS__
53+
/* Specific beos actions before backend startup */
54+
beos_before_backend_startup();
55+
#endif
56+
57+
result=fork();
58+
if (result== (pid_t)-1)
59+
{
60+
/* fork failed */
61+
#ifdef__BEOS__
62+
/* Specific beos backend startup actions */
63+
beos_backend_startup_failed();
64+
#endif
65+
}
66+
elseif (result==0)
67+
{
68+
/* fork succeeded, in child */
69+
#ifdefLINUX_PROFILE
70+
setitimer(ITIMER_PROF,&prof_itimer,NULL);
71+
#endif
72+
73+
#ifdef__BEOS__
74+
/* Specific beos backend startup actions */
75+
beos_backend_startup();
76+
#endif
77+
}
78+
79+
returnresult;
80+
}

‎src/backend/postmaster/pgarch.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*
2121
* IDENTIFICATION
22-
* $PostgreSQL: pgsql/src/backend/postmaster/pgarch.c,v 1.14 2004/12/31 22:00:40 pgsql Exp $
22+
* $PostgreSQL: pgsql/src/backend/postmaster/pgarch.c,v 1.15 2005/03/10 07:14:03 neilc Exp $
2323
*
2424
*-------------------------------------------------------------------------
2525
*/
@@ -34,6 +34,7 @@
3434
#include"access/xlog_internal.h"
3535
#include"libpq/pqsignal.h"
3636
#include"miscadmin.h"
37+
#include"postmaster/fork_process.h"
3738
#include"postmaster/pgarch.h"
3839
#include"postmaster/postmaster.h"
3940
#include"storage/fd.h"
@@ -141,36 +142,20 @@ pgarch_start(void)
141142
return0;
142143
last_pgarch_start_time=curtime;
143144

144-
fflush(stdout);
145-
fflush(stderr);
146-
147-
#ifdef__BEOS__
148-
/* Specific beos actions before backend startup */
149-
beos_before_backend_startup();
150-
#endif
151-
152145
#ifdefEXEC_BACKEND
153146
switch ((pgArchPid=pgarch_forkexec()))
154147
#else
155-
switch ((pgArchPid=fork()))
148+
switch ((pgArchPid=fork_process()))
156149
#endif
157150
{
158151
case-1:
159-
#ifdef__BEOS__
160-
/* Specific beos actions */
161-
beos_backend_startup_failed();
162-
#endif
163152
ereport(LOG,
164153
(errmsg("could not fork archiver: %m")));
165154
return0;
166155

167156
#ifndefEXEC_BACKEND
168157
case0:
169158
/* in postmaster child ... */
170-
#ifdef__BEOS__
171-
/* Specific beos actions after backend startup */
172-
beos_backend_startup();
173-
#endif
174159
/* Close the postmaster's sockets */
175160
ClosePostmasterPorts(false);
176161

‎src/backend/postmaster/postmaster.c

Lines changed: 11 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.445 2005/02/22 04:36:36 momjian Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.446 2005/03/10 07:14:03 neilc Exp $
4141
*
4242
* NOTES
4343
*
@@ -92,6 +92,8 @@
9292
#include<DNSServiceDiscovery/DNSServiceDiscovery.h>
9393
#endif
9494

95+
#include"access/xlog.h"
96+
#include"bootstrap/bootstrap.h"
9597
#include"catalog/pg_control.h"
9698
#include"catalog/pg_database.h"
9799
#include"commands/async.h"
@@ -103,23 +105,22 @@
103105
#include"libpq/pqsignal.h"
104106
#include"miscadmin.h"
105107
#include"nodes/nodes.h"
106-
#include"postmaster/postmaster.h"
108+
#include"pgstat.h"
109+
#include"postmaster/fork_process.h"
107110
#include"postmaster/pgarch.h"
111+
#include"postmaster/postmaster.h"
108112
#include"postmaster/syslogger.h"
113+
#include"storage/bufmgr.h"
109114
#include"storage/fd.h"
110115
#include"storage/ipc.h"
111116
#include"storage/pg_shmem.h"
112117
#include"storage/pmsignal.h"
113118
#include"storage/proc.h"
114-
#include"storage/bufmgr.h"
115-
#include"access/xlog.h"
116119
#include"tcop/tcopprot.h"
117120
#include"utils/builtins.h"
118121
#include"utils/guc.h"
119122
#include"utils/memutils.h"
120123
#include"utils/ps_status.h"
121-
#include"bootstrap/bootstrap.h"
122-
#include"pgstat.h"
123124

124125
#ifdefEXEC_BACKEND
125126
#include"storage/spin.h"
@@ -1008,16 +1009,7 @@ pmdaemonize(void)
10081009
inti;
10091010
pid_tpid;
10101011

1011-
#ifdefLINUX_PROFILE
1012-
structitimervalprof_itimer;
1013-
#endif
1014-
1015-
#ifdefLINUX_PROFILE
1016-
/* see comments in BackendStartup */
1017-
getitimer(ITIMER_PROF,&prof_itimer);
1018-
#endif
1019-
1020-
pid=fork();
1012+
pid=fork_process();
10211013
if (pid== (pid_t)-1)
10221014
{
10231015
write_stderr("%s: could not fork background process: %s\n",
@@ -1030,10 +1022,6 @@ pmdaemonize(void)
10301022
_exit(0);
10311023
}
10321024

1033-
#ifdefLINUX_PROFILE
1034-
setitimer(ITIMER_PROF,&prof_itimer,NULL);
1035-
#endif
1036-
10371025
MyProcPid=PostmasterPid=getpid();/* reset PID vars to child */
10381026

10391027
/* GH: If there's no setsid(), we hopefully don't need silent mode.
@@ -2382,10 +2370,6 @@ BackendStartup(Port *port)
23822370
Backend*bn;/* for backend cleanup */
23832371
pid_tpid;
23842372

2385-
#ifdefLINUX_PROFILE
2386-
structitimervalprof_itimer;
2387-
#endif
2388-
23892373
/*
23902374
* Compute the cancel key that will be assigned to this backend. The
23912375
* backend will have its own copy in the forked-off process' value of
@@ -2409,54 +2393,13 @@ BackendStartup(Port *port)
24092393
/* Pass down canAcceptConnections state (kluge for EXEC_BACKEND case) */
24102394
port->canAcceptConnections=canAcceptConnections();
24112395

2412-
/*
2413-
* Flush stdio channels just before fork, to avoid double-output
2414-
* problems. Ideally we'd use fflush(NULL) here, but there are still a
2415-
* few non-ANSI stdio libraries out there (like SunOS 4.1.x) that
2416-
* coredump if we do. Presently stdout and stderr are the only stdio
2417-
* output channels used by the postmaster, so fflush'ing them should
2418-
* be sufficient.
2419-
*/
2420-
fflush(stdout);
2421-
fflush(stderr);
2422-
24232396
#ifdefEXEC_BACKEND
2424-
24252397
pid=backend_forkexec(port);
2426-
24272398
#else/* !EXEC_BACKEND */
2428-
2429-
#ifdefLINUX_PROFILE
2430-
2431-
/*
2432-
* Linux's fork() resets the profiling timer in the child process. If
2433-
* we want to profile child processes then we need to save and restore
2434-
* the timer setting. This is a waste of time if not profiling,
2435-
* however, so only do it if commanded by specific -DLINUX_PROFILE
2436-
* switch.
2437-
*/
2438-
getitimer(ITIMER_PROF,&prof_itimer);
2439-
#endif
2440-
2441-
#ifdef__BEOS__
2442-
/* Specific beos actions before backend startup */
2443-
beos_before_backend_startup();
2444-
#endif
2445-
2446-
pid=fork();
2447-
2399+
pid=fork_process();
24482400
if (pid==0)/* child */
24492401
{
2450-
#ifdefLINUX_PROFILE
2451-
setitimer(ITIMER_PROF,&prof_itimer,NULL);
2452-
#endif
2453-
2454-
#ifdef__BEOS__
2455-
/* Specific beos backend startup actions */
2456-
beos_backend_startup();
2457-
#endif
24582402
free(bn);
2459-
24602403
proc_exit(BackendRun(port));
24612404
}
24622405
#endif/* EXEC_BACKEND */
@@ -2466,10 +2409,6 @@ BackendStartup(Port *port)
24662409
/* in parent, fork failed */
24672410
intsave_errno=errno;
24682411

2469-
#ifdef__BEOS__
2470-
/* Specific beos backend startup actions */
2471-
beos_backend_startup_failed();
2472-
#endif
24732412
free(bn);
24742413
errno=save_errno;
24752414
ereport(LOG,
@@ -2945,7 +2884,7 @@ internal_forkexec(int argc, char *argv[], Port *port)
29452884
argv[2]=tmpfilename;
29462885

29472886
/* Fire off execv in child */
2948-
if ((pid=fork())==0)
2887+
if ((pid=fork_process())==0)
29492888
{
29502889
if (execv(postgres_exec_path,argv)<0)
29512890
{
@@ -3465,10 +3404,6 @@ StartChildProcess(int xlop)
34653404
intac=0;
34663405
charxlbuf[32];
34673406

3468-
#ifdefLINUX_PROFILE
3469-
structitimervalprof_itimer;
3470-
#endif
3471-
34723407
/*
34733408
* Set up command-line arguments for subprocess
34743409
*/
@@ -3488,41 +3423,13 @@ StartChildProcess(int xlop)
34883423
av[ac]=NULL;
34893424
Assert(ac<lengthof(av));
34903425

3491-
/*
3492-
* Flush stdio channels (see comments in BackendStartup)
3493-
*/
3494-
fflush(stdout);
3495-
fflush(stderr);
3496-
34973426
#ifdefEXEC_BACKEND
3498-
34993427
pid=postmaster_forkexec(ac,av);
3500-
35013428
#else/* !EXEC_BACKEND */
3502-
3503-
#ifdefLINUX_PROFILE
3504-
/* see comments in BackendStartup */
3505-
getitimer(ITIMER_PROF,&prof_itimer);
3506-
#endif
3507-
3508-
#ifdef__BEOS__
3509-
/* Specific beos actions before backend startup */
3510-
beos_before_backend_startup();
3511-
#endif
3512-
3513-
pid=fork();
3429+
pid=fork_process();
35143430

35153431
if (pid==0)/* child */
35163432
{
3517-
#ifdefLINUX_PROFILE
3518-
setitimer(ITIMER_PROF,&prof_itimer,NULL);
3519-
#endif
3520-
3521-
#ifdef__BEOS__
3522-
/* Specific beos actions after backend startup */
3523-
beos_backend_startup();
3524-
#endif
3525-
35263433
IsUnderPostmaster= true;/* we are a postmaster subprocess
35273434
* now */
35283435

@@ -3546,11 +3453,6 @@ StartChildProcess(int xlop)
35463453
{
35473454
/* in parent, fork failed */
35483455
intsave_errno=errno;
3549-
3550-
#ifdef__BEOS__
3551-
/* Specific beos actions before backend startup */
3552-
beos_backend_startup_failed();
3553-
#endif
35543456
errno=save_errno;
35553457
switch (xlop)
35563458
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp