|
37 | 37 | *
|
38 | 38 | *
|
39 | 39 | * IDENTIFICATION
|
40 |
| - * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.538 2007/08/03 20:06:50 tgl Exp $ |
| 40 | + * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.539 2007/08/04 03:15:49 tgl Exp $ |
41 | 41 | *
|
42 | 42 | * NOTES
|
43 | 43 | *
|
@@ -230,6 +230,7 @@ static volatile sig_atomic_t start_autovac_launcher = false;
|
230 | 230 | * backend from the postmaster to that backend (via fork).
|
231 | 231 | */
|
232 | 232 | static unsigned int random_seed = 0;
|
| 233 | +static struct timeval random_start_time; |
233 | 234 |
|
234 | 235 | extern char *optarg;
|
235 | 236 | extern int optind,
|
@@ -966,6 +967,8 @@ PostmasterMain(int argc, char *argv[])
|
966 | 967 | * Remember postmaster startup time
|
967 | 968 | */
|
968 | 969 | PgStartTime = GetCurrentTimestamp();
|
| 970 | + /* PostmasterRandom wants its own copy */ |
| 971 | + gettimeofday(&random_start_time, NULL); |
969 | 972 |
|
970 | 973 | /*
|
971 | 974 | * We're ready to rock and roll...
|
@@ -1140,10 +1143,7 @@ ServerLoop(void)
|
1140 | 1143 | int nSockets;
|
1141 | 1144 | time_t now,
|
1142 | 1145 | last_touch_time;
|
1143 |
| - struct timeval earlier, |
1144 |
| - later; |
1145 | 1146 |
|
1146 |
| - gettimeofday(&earlier, NULL); |
1147 | 1147 | last_touch_time = time(NULL);
|
1148 | 1148 |
|
1149 | 1149 | nSockets = initMasks(&readmask);
|
@@ -1194,24 +1194,6 @@ ServerLoop(void)
|
1194 | 1194 | */
|
1195 | 1195 | if (selres > 0)
|
1196 | 1196 | {
|
1197 |
| - /* |
1198 |
| - * Select a random seed at the time of first receiving a request. |
1199 |
| - */ |
1200 |
| - while (random_seed == 0) |
1201 |
| - { |
1202 |
| - gettimeofday(&later, NULL); |
1203 |
| - |
1204 |
| - /* |
1205 |
| - * We are not sure how much precision is in tv_usec, so we |
1206 |
| - * swap the high and low 16 bits of 'later' and XOR them with |
1207 |
| - * 'earlier'. On the off chance that the result is 0, we loop |
1208 |
| - * until it isn't. |
1209 |
| - */ |
1210 |
| - random_seed = earlier.tv_usec ^ |
1211 |
| - ((later.tv_usec << 16) | |
1212 |
| - ((later.tv_usec >> 16) & 0xffff)); |
1213 |
| - } |
1214 |
| - |
1215 | 1197 | for (i = 0; i < MAXLISTEN; i++)
|
1216 | 1198 | {
|
1217 | 1199 | if (ListenSocket[i] == -1)
|
@@ -2970,6 +2952,7 @@ BackendRun(Port *port)
|
2970 | 2952 | * a new random sequence in the random() library function.
|
2971 | 2953 | */
|
2972 | 2954 | random_seed = 0;
|
| 2955 | + random_start_time.tv_usec = 0; |
2973 | 2956 | /* slightly hacky way to get integer microseconds part of timestamptz */
|
2974 | 2957 | TimestampDifference(0, port->SessionStartTime, &secs, &usecs);
|
2975 | 2958 | srandom((unsigned int) (MyProcPid ^ usecs));
|
@@ -3778,13 +3761,29 @@ RandomSalt(char *cryptSalt, char *md5Salt)
|
3778 | 3761 | static long
|
3779 | 3762 | PostmasterRandom(void)
|
3780 | 3763 | {
|
3781 |
| - static bool initialized = false; |
3782 |
| - |
3783 |
| - if (!initialized) |
| 3764 | + /* |
| 3765 | + * Select a random seed at the time of first receiving a request. |
| 3766 | + */ |
| 3767 | + if (random_seed == 0) |
3784 | 3768 | {
|
3785 |
| - Assert(random_seed != 0); |
| 3769 | + do |
| 3770 | + { |
| 3771 | + struct timeval random_stop_time; |
| 3772 | + |
| 3773 | + gettimeofday(&random_stop_time, NULL); |
| 3774 | + /* |
| 3775 | + * We are not sure how much precision is in tv_usec, so we swap |
| 3776 | + * the high and low 16 bits of 'random_stop_time' and XOR them |
| 3777 | + * with 'random_start_time'. On the off chance that the result is |
| 3778 | + * 0, we loop until it isn't. |
| 3779 | + */ |
| 3780 | + random_seed = random_start_time.tv_usec ^ |
| 3781 | + ((random_stop_time.tv_usec << 16) | |
| 3782 | + ((random_stop_time.tv_usec >> 16) & 0xffff)); |
| 3783 | + } |
| 3784 | + while (random_seed == 0); |
| 3785 | + |
3786 | 3786 | srandom(random_seed);
|
3787 |
| - initialized = true; |
3788 | 3787 | }
|
3789 | 3788 |
|
3790 | 3789 | return random();
|
|
0 commit comments