10
10
*
11
11
*
12
12
* 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 $
14
14
*
15
15
* NOTES
16
16
*
113
113
typedef struct bkend
114
114
{
115
115
int pid ; /* process id of backend */
116
+ long cancel_key ; /* cancel key for cancels for this backend */
116
117
} Backend ;
117
118
118
119
/* list of active backends. For garbage collection only now. */
@@ -198,7 +199,14 @@ static sigset_t oldsigmask,
198
199
static int orgsigmask = sigblock (0 );
199
200
#endif
200
201
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
+
201
208
static unsigned int random_seed = 0 ;
209
+ long MyCancelKey = 0 ;
202
210
203
211
extern char * optarg ;
204
212
extern int optind ,
@@ -612,17 +620,22 @@ ServerLoop(void)
612
620
return (STATUS_ERROR );
613
621
}
614
622
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 )
616
627
{
617
628
gettimeofday (& later , & tz );
618
629
619
630
/*
620
631
* 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.
622
635
*/
623
636
random_seed = now .tv_usec ^
624
637
((later .tv_usec << 16 ) |
625
- ((unsigned int )( later .tv_usec & 0xffff0000 ) >> 16 ));
638
+ ((later .tv_usec >> 16 ) & 0xffff ));
626
639
}
627
640
628
641
/*
@@ -1085,6 +1098,14 @@ BackendStartup(Port *port)
1085
1098
}
1086
1099
#endif
1087
1100
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
+
1088
1109
if (DebugLvl > 2 )
1089
1110
{
1090
1111
char * * p ;
@@ -1098,17 +1119,21 @@ BackendStartup(Port *port)
1098
1119
fprintf (stderr , "-----------------------------------------\n" );
1099
1120
}
1100
1121
1122
+ /* Flush all stdio channels just before fork,
1123
+ * to avoid double-output problems.
1124
+ */
1125
+ fflush (NULL );
1126
+
1101
1127
if ((pid = fork ()) == 0 )
1102
1128
{ /* child */
1103
1129
if (DoBackend (port ))
1104
1130
{
1105
1131
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 );
1109
1134
}
1110
1135
else
1111
- _exit (0 );
1136
+ exit (0 );
1112
1137
}
1113
1138
1114
1139
/* in parent */
@@ -1140,6 +1165,7 @@ BackendStartup(Port *port)
1140
1165
}
1141
1166
1142
1167
bn -> pid = pid ;
1168
+ bn -> cancel_key = MyCancelKey ;
1143
1169
DLAddHead (BackendList , DLNewElem (bn ));
1144
1170
1145
1171
ActiveBackends = TRUE;
0 commit comments