PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
backend_random.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Size BackendRandomShmemSize (void)
 
void BackendRandomShmemInit (void)
 
bool pg_backend_random (char *dst, int len)
 

Function Documentation

void BackendRandomShmemInit ( void  )

Definition at line 83 of file backend_random.c.

References Assert, BackendRandomShmemSize(), BackendRandomShmemStruct::initialized, IsUnderPostmaster, and ShmemInitStruct().

Referenced by CreateSharedMemoryAndSemaphores().

84 {
85  bool found;
86 
88  ShmemInitStruct("Backend PRNG state",
90  &found);
91 
92  if (!IsUnderPostmaster)
93  {
94  Assert(!found);
95 
97  }
98  else
99  Assert(found);
100 }
static BackendRandomShmemStruct * BackendRandomShmem
Size BackendRandomShmemSize(void)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
bool IsUnderPostmaster
Definition: globals.c:100
#define Assert(condition)
Definition: c.h:671
Size BackendRandomShmemSize ( void  )

Definition at line 77 of file backend_random.c.

Referenced by BackendRandomShmemInit(), and CreateSharedMemoryAndSemaphores().

78 {
79  return sizeof(BackendRandomShmemStruct);
80 }
bool pg_backend_random ( char *  dst,
int  len 
)

Definition at line 103 of file backend_random.c.

References Assert, gettimeofday(), i, BackendRandomShmemStruct::initialized, IsPostmasterEnvironment, IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyCancelKey, NULL, pg_jrand48(), and BackendRandomShmemStruct::seed.

Referenced by CheckMD5Auth(), CheckRADIUSAuth(), chkpass_in(), pg_random_uuid(), pgp_s2k_fill(), px_gen_salt(), and write_prefix().

104 {
105  int i;
106  char *end = dst + len;
107 
108  /* should not be called in postmaster */
110 
111  LWLockAcquire(BackendRandomLock, LW_EXCLUSIVE);
112 
113  /*
114  * Seed the PRNG on the first use.
115  */
117  {
118  struct timeval now;
119 
120  gettimeofday(&now, NULL);
121 
122  BackendRandomShmem->seed[0] = now.tv_sec;
123  BackendRandomShmem->seed[1] = (unsigned short) (now.tv_usec);
124  BackendRandomShmem->seed[2] = (unsigned short) (now.tv_usec >> 16);
125 
126  /*
127  * Mix in the cancel key, generated by the postmaster. This adds
128  * what little entropy the postmaster had to the seed.
129  */
131  BackendRandomShmem->seed[1] ^= (MyCancelKey >> 16);
132 
134  }
135 
136  for (i = 0; dst < end; i++)
137  {
138  uint32 r;
139  int j;
140 
141  /*
142  * pg_jrand48 returns a 32-bit integer. Fill the next 4 bytes from it.
143  */
145 
146  for (j = 0; j < 4 && dst < end; j++)
147  {
148  *(dst++) = (char) (r & 0xFF);
149  r >>= 8;
150  }
151  }
152  LWLockRelease(BackendRandomLock);
153 
154  return true;
155 }
static BackendRandomShmemStruct * BackendRandomShmem
bool IsPostmasterEnvironment
Definition: globals.c:99
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
int32 MyCancelKey
Definition: globals.c:41
bool IsUnderPostmaster
Definition: globals.c:100
unsigned int uint32
Definition: c.h:265
unsigned short seed[3]
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
long pg_jrand48(unsigned short xseed[3])
Definition: erand48.c:95
int i
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1533