Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions

random.c File Reference

#include "postgres.h"
#include "px.h"
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
Include dependency graph for random.c:

Go to the source code of this file.

Defines

#define RND_BYTES   32
#define TRY_UNIXSTD

Functions

static uint8try_unix_std (uint8 *dst)
unsigned px_acquire_system_randomness (uint8 *dst)

Define Documentation

#define RND_BYTES   32

Definition at line 37 of file random.c.

#define TRY_UNIXSTD

Definition at line 161 of file random.c.


Function Documentation

unsigned px_acquire_system_randomness ( uint8 dst  ) 

Definition at line 227 of file random.c.

References try_unix_std().

Referenced by system_reseed().

{
    uint8      *p = dst;

#ifdef TRY_DEV_RANDOM
    p = try_dev_random(p);
#endif
#ifdef TRY_WIN32_GENRAND
    p = try_win32_genrand(p);
#endif
#ifdef TRY_WIN32_PERFC
    p = try_win32_perfc(p);
#endif
#ifdef TRY_UNIXSTD
    p = try_unix_std(p);
#endif
    return p - dst;
}

static uint8* try_unix_std ( uint8 dst  )  [static]

Definition at line 175 of file random.c.

References gettimeofday(), NULL, px_alloc, px_find_digest(), px_free, px_md_finish, px_md_free, px_md_update, and random().

Referenced by px_acquire_system_randomness().

{
    pid_t       pid;
    int         x;
    PX_MD      *md;
    struct timeval tv;
    int         res;

    /* process id */
    pid = getpid();
    memcpy(dst, (uint8 *) &pid, sizeof(pid));
    dst += sizeof(pid);

    /* time */
    gettimeofday(&tv, NULL);
    memcpy(dst, (uint8 *) &tv, sizeof(tv));
    dst += sizeof(tv);

    /* pointless, but should not hurt */
    x = random();
    memcpy(dst, (uint8 *) &x, sizeof(x));
    dst += sizeof(x);

    /* let's be desperate */
    res = px_find_digest("sha1", &md);
    if (res >= 0)
    {
        uint8      *ptr;
        uint8       stack[8192];
        int         alloc = 32 * 1024;

        px_md_update(md, stack, sizeof(stack));
        ptr = px_alloc(alloc);
        px_md_update(md, ptr, alloc);
        px_free(ptr);

        px_md_finish(md, dst);
        px_md_free(md);

        dst += 20;
    }

    return dst;
}