<commit_before>
/*
 * Copyright (C) <NAME>
 * Copyright (C) NGINX, Inc.
 */


#include <nxt_auto_config.h>
#include <nxt_types.h>
#include <nxt_clang.h>
#include <nxt_random.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#if (NXT_HAVE_GETRANDOM)
#include <sys/syscall.h>
#include <linux/random.h>
#endif


/*
 * The pseudorandom generator based on OpenBSD arc4random.  Although
 * it is usually stated that arc4random uses RC4 pseudorandom generation
 * algorithm they are actually different in nxt_random_add().
 */


#define NXT_RANDOM_KEY_SIZE  128


nxt_inline uint8_t nxt_random_byte(nxt_random_t *r);


void
nxt_random_init(nxt_random_t *r, nxt_pid_t pid)
{
    nxt_uint_t  i;

    r->count = 0;
    r->pid = pid;
    r->i = 0;
    r->j = 0;

    for (i = 0; i < 256; i++) {
        r->s[i] = i;
    }
}


void
nxt_random_stir(nxt_random_t *r, nxt_pid_t pid)
{
    int             fd;
    ssize_t         n;
    struct timeval  tv;
    union {
        uint32_t    value[3];
        u_char      bytes[NXT_RANDOM_KEY_SIZE];
    } key;

    if (r->pid == 0) {
        nxt_random_init(r, pid);
    }

    r->pid = pid;

    n = 0;

#if (NXT_HAVE_GETRANDOM)

    /* Linux 3.17 getrandom(), it is not available in Glibc. */

    n = syscall(SYS_getrandom, key, NXT_RANDOM_KEY_SIZE, 0));

#endif

    if (n != NXT_RANDOM_KEY_SIZE) {
        fd = open("/dev/urandom", O_RDONLY);

        if (fd >= 0) {
            n = read(fd, &key, NXT_RANDOM_KEY_SIZE);
            (void) close(fd);
        }
    }

    if (n != NXT_RANDOM_KEY_SIZE) {
        (void) gettimeofday(&tv, NULL);

        /* XOR with stack garbage. */

        key.value[0] ^= tv.tv_usec;
        key.value[1] ^= tv.tv_sec;
        key.value[2] ^= getpid();<commit_msg>Fixed building on modern Linux kernels.<commit_after>
/*
 * Copyright (C) <NAME>
 * Copyright (C) NGINX, Inc.
 */


#include <nxt_auto_config.h>
#include <nxt_types.h>
#include <nxt_clang.h>
#include <nxt_random.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#if (NXT_HAVE_GETRANDOM)
#include <sys/syscall.h>
#include <linux/random.h>
#endif


/*
 * The pseudorandom generator based on OpenBSD arc4random.  Although
 * it is usually stated that arc4random uses RC4 pseudorandom generation
 * algorithm they are actually different in nxt_random_add().
 */


#define NXT_RANDOM_KEY_SIZE  128


nxt_inline uint8_t nxt_random_byte(nxt_random_t *r);


void
nxt_random_init(nxt_random_t *r, nxt_pid_t pid)
{
    nxt_uint_t  i;

    r->count = 0;
    r->pid = pid;
    r->i = 0;
    r->j = 0;

    for (i = 0; i < 256; i++) {
        r->s[i] = i;
    }
}


void
nxt_random_stir(nxt_random_t *r, nxt_pid_t pid)
{
    int             fd;
    ssize_t         n;
    struct timeval  tv;
    union {
        uint32_t    value[3];
        u_char      bytes[NXT_RANDOM_KEY_SIZE];
    } key;

    if (r->pid == 0) {
        nxt_random_init(r, pid);
    }

    r->pid = pid;

    n = 0;

#if (NXT_HAVE_GETRANDOM)

    /* Linux 3.17 getrandom(), it is not available in Glibc. */

    n = syscall(SYS_getrandom, key, NXT_RANDOM_KEY_SIZE, 0);

#endif

    if (n != NXT_RANDOM_KEY_SIZE) {
        fd = open("/dev/urandom", O_RDONLY);

        if (fd >= 0) {
            n = read(fd, &key, NXT_RANDOM_KEY_SIZE);
            (void) close(fd);
        }
    }

    if (n != NXT_RANDOM_KEY_SIZE) {
        (void) gettimeofday(&tv, NULL);

        /* XOR with stack garbage. */

        key.value[0] ^= tv.tv_usec;
        key.value[1] ^= tv.tv_sec;
        key.value[2] ^= getpid();
{'fraction_non_alphanumeric': 0.13787234042553193, 'fraction_numerical': 0.015319148936170212, 'mean_word_length': 2.4133591481122942, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1019224', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1435, 'n_words': 322}