File: rngd_threads.c

package info (click to toggle)
rng-tools-debian 2.6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: ansic: 2,690; sh: 200; makefile: 58
file content (114 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * rngd_threads.c -- Common thread code
 * Copyright (C) 2004 Henrique de Moraes Holschuh <hmh@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _GNU_SOURCE

#ifndef HAVE_CONFIG_H
#error Invalid or missing autoconf build environment
#endif

#include "rng-tools-config.h"

#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/mman.h>

#include <assert.h>

#include "rngd.h"
#include "fips.h"
#include "stats.h"
#include "exits.h"
#include "rngd_threads.h"

/* Buffers for RNG data */
int rng_buffers = 0;		/* number of active buffers */
rng_buffer_t **rng_buf = NULL;	/* vector of pointers to the buffers */

/*
 * FIFOs to pass blocks among the threads
 */
struct buffer_queues buffer_queues;

/*
 * Get the amount of data in a FIFO
 */
int getbuffifo_count(struct buf_fifo *fifo)
{
	int count;

	assert(fifo != NULL);

	pthread_mutex_lock(&(fifo->mutex));
	count = (fifo->head >= fifo->tail) ?
		 fifo->head - fifo->tail :
		 GETBUFFIFO_SIZE + fifo->head - fifo->tail;
	pthread_mutex_unlock(&(fifo->mutex));
	return count;
}

/* handy malloc()/calloc() error handler */
void *test_malloc(void *p) {
	if (!p) {
		message(LOG_ERR, "cannot allocate buffers");
		die(EXIT_OSERR);
	}
	return p;
}

/*
 *  Init the RNG buffer structures
 */

#define BUFFIFO_INIT(fifo) do { \
	pthread_mutex_init(&buffer_queues.fifo.mutex, NULL); \
	buffer_queues.fifo.head = buffer_queues.fifo.tail = 0; \
	buffer_queues.fifo.data = test_malloc(calloc(GETBUFFIFO_SIZE, \
		sizeof(*buffer_queues.fifo.data))); \
} while (0)

void init_rng_buffers(int n)
{
	int i;

	assert(n <= MAX_RNG_BUFFERS);

	rng_buffers = n;

	BUFFIFO_INIT(empty);
	BUFFIFO_INIT(full);
	BUFFIFO_INIT(accepted);
	BUFFIFO_INIT(rejected);

	rng_buf = test_malloc(calloc(rng_buffers, sizeof(*rng_buf)));

	for (i = 0; i < rng_buffers; i++) {
		BUFFIFO_WRITE(empty, i);

		rng_buf[i] = test_malloc(malloc(FIPS_RNG_BUFFER_SIZE));
		if (mlock(rng_buf[i], FIPS_RNG_BUFFER_SIZE)) {
			message_strerr(LOG_ERR, errno, "cannot lock buffers");
                        die(EXIT_OSERR);
		}
	}
}