Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cb8539f

Browse files
committed
Attached is a patch that fixes these leaks, and does a couple other
things as well: * Computes and saves a cancel key for each backend. * fflush before forking, to eliminate double-buffering problems between postmaster and backends. Other cleanups. Tom Lane
1 parent 3912b75 commit cb8539f

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.84 1998/06/08 22:28:26 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.85 1998/06/09 04:06:12 momjian Exp $
1414
*
1515
* NOTES
1616
*
@@ -113,6 +113,7 @@
113113
typedef struct bkend
114114
{
115115
int pid; /* process id of backend */
116+
long cancel_key; /* cancel key for cancels for this backend */
116117
} Backend;
117118

118119
/* list of active backends. For garbage collection only now. */
@@ -198,7 +199,14 @@ static sigset_t oldsigmask,
198199
static int orgsigmask = sigblock(0);
199200
#endif
200201

202+
/*
203+
* State for assigning random salts and cancel keys.
204+
* Also, the global MyCancelKey passes the cancel key assigned to a given
205+
* backend from the postmaster to that backend (via fork).
206+
*/
207+
201208
static unsigned int random_seed = 0;
209+
long MyCancelKey = 0;
202210

203211
extern char *optarg;
204212
extern int optind,
@@ -612,17 +620,22 @@ ServerLoop(void)
612620
return (STATUS_ERROR);
613621
}
614622

615-
if (random_seed == 0)
623+
/*
624+
* Select a random seed at the time of first receiving a request.
625+
*/
626+
while (random_seed == 0)
616627
{
617628
gettimeofday(&later, &tz);
618629

619630
/*
620631
* We are not sure how much precision is in tv_usec, so we
621-
* swap the nibbles of 'later' and XOR them with 'now'
632+
* swap the nibbles of 'later' and XOR them with 'now'.
633+
* On the off chance that the result is 0, we loop until
634+
* it isn't.
622635
*/
623636
random_seed = now.tv_usec ^
624637
((later.tv_usec << 16) |
625-
((unsigned int)(later.tv_usec & 0xffff0000) >> 16));
638+
((later.tv_usec >> 16) & 0xffff));
626639
}
627640

628641
/*
@@ -1085,6 +1098,14 @@ BackendStartup(Port *port)
10851098
}
10861099
#endif
10871100

1101+
/*
1102+
* Compute the cancel key that will be assigned to this backend.
1103+
* The backend will have its own copy in the forked-off process'
1104+
* value of MyCancelKey, so that it can transmit the key to the
1105+
* frontend.
1106+
*/
1107+
MyCancelKey = PostmasterRandom();
1108+
10881109
if (DebugLvl > 2)
10891110
{
10901111
char **p;
@@ -1098,17 +1119,21 @@ BackendStartup(Port *port)
10981119
fprintf(stderr, "-----------------------------------------\n");
10991120
}
11001121

1122+
/* Flush all stdio channels just before fork,
1123+
* to avoid double-output problems.
1124+
*/
1125+
fflush(NULL);
1126+
11011127
if ((pid = fork()) == 0)
11021128
{ /* child */
11031129
if (DoBackend(port))
11041130
{
11051131
fprintf(stderr, "%s child[%d]: BackendStartup: backend startup failed\n",
1106-
progname, pid);
1107-
/* use _exit to keep from double-flushing stdio */
1108-
_exit(1);
1132+
progname, (int) getpid());
1133+
exit(1);
11091134
}
11101135
else
1111-
_exit(0);
1136+
exit(0);
11121137
}
11131138

11141139
/* in parent */
@@ -1140,6 +1165,7 @@ BackendStartup(Port *port)
11401165
}
11411166

11421167
bn->pid = pid;
1168+
bn->cancel_key = MyCancelKey;
11431169
DLAddHead(BackendList, DLNewElem(bn));
11441170

11451171
ActiveBackends = TRUE;

0 commit comments

Comments
 (0)