]>
git.ipfire.org Git - thirdparty/rng-tools.git/blob - rngd_linux.c
2 * rngd_linux.c -- Entropy sink for the Linux Kernel (/dev/random)
4 * Copyright (C) 2001 Philipp Rumpf
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 #error Invalid or missing autoconf build environment
27 #include "rng-tools-config.h"
35 #include <sys/ioctl.h>
37 #include <sys/types.h>
42 #include <linux/types.h>
43 #include <linux/random.h>
49 #include "rngd_linux.h"
51 extern struct rng
*rng_list
;
53 /* Kernel output device */
57 * Get the default watermark
59 int default_watermark(void)
65 unsigned int wm
= 2048; /* Default guess */
67 f
= fopen("/proc/sys/kernel/random/poolsize", "r");
70 l
= fread(psbuf
, 1, sizeof psbuf
, f
);
71 if (ferror(f
) || !feof(f
) || l
== 0)
73 if (psbuf
[l
-1] != '\n')
76 ps
= strtoul(psbuf
, &p
, 0);
89 * Initialize the interface to the Linux Kernel
90 * entropy pool (through /dev/random)
92 * randomdev is the path to the random device
94 void init_kernel_rng(const char* randomdev
)
99 random_fd
= open(randomdev
, O_RDWR
);
100 if (random_fd
== -1) {
101 message(LOG_DAEMON
|LOG_ERR
, "can't open %s: %s",
102 randomdev
, strerror(errno
));
106 f
= fopen("/proc/sys/kernel/random/write_wakeup_threshold", "w");
110 fprintf(f
, "%u\n", arguments
->fill_watermark
);
111 /* Note | not || here... we always want to close the file */
112 err
= ferror(f
) | fclose(f
);
115 message(LOG_DAEMON
|LOG_WARNING
,
116 "unable to adjust write_wakeup_threshold: %s",
121 void random_add_entropy(void *buf
, size_t size
)
126 unsigned char data
[size
];
129 entropy
.ent_count
= size
* 8;
131 memcpy(entropy
.data
, buf
, size
);
133 if (ioctl(random_fd
, RNDADDENTROPY
, &entropy
) != 0) {
134 message(LOG_DAEMON
|LOG_ERR
, "RNDADDENTROPY failed: %s\n",
140 void random_sleep(void)
142 struct pollfd pfd
= {
150 void src_list_add(struct rng
*ent_src
)
159 iter
->next
= ent_src
;