return 0;
}
+/**
+ * gnutls_fd_in_use:
+ * @fd: is a file descriptor to check against
+ *
+ * Check whether the provided file descriptor is used by
+ * gnutls internally. That is whether is used for /dev/urandom
+ * etc. That function is to be used by applications that close()
+ * all file descriptors on application startup.
+ *
+ * Returns: non zero if this fd is in use, or a zero otherwise.
+ **/
+unsigned gnutls_fd_in_use(int fd)
+{
+#ifdef _WIN32
+ return 0;
+#else
+ if (fd >= 0 && fd == _gnutls_urandom_fd)
+ return 1;
+ return 0;
+#endif
+}
+
/**
* gnutls_session_resumption_requested:
* @session: is a #gnutls_session_t structure.
#include <locks.h>
#include "egd.h"
-static int device_fd = -1;
+int _gnutls_urandom_fd = -1;
static int _rnd_get_system_entropy_urandom(void* _rnd, size_t size)
{
for (done = 0; done < size;) {
int res;
do {
- res = read(device_fd, rnd + done, size - done);
+ res = read(_gnutls_urandom_fd, rnd + done, size - done);
} while (res < 0 && errno == EINTR);
if (res <= 0) {
for (done = 0; done < size;) {
res =
- _rndegd_read(&device_fd, rnd + done, size - done);
+ _rndegd_read(&_gnutls_urandom_fd, rnd + done, size - done);
if (res <= 0) {
if (res < 0) {
_gnutls_debug_log("Failed to read egd.\n");
{
int old;
- device_fd = open("/dev/urandom", O_RDONLY);
- if (device_fd < 0) {
+ _gnutls_urandom_fd = open("/dev/urandom", O_RDONLY);
+ if (_gnutls_urandom_fd < 0) {
_gnutls_debug_log("Cannot open urandom!\n");
goto fallback;
}
- old = fcntl(device_fd, F_GETFD);
+ old = fcntl(_gnutls_urandom_fd, F_GETFD);
if (old != -1)
- fcntl(device_fd, F_SETFD, old | FD_CLOEXEC);
+ fcntl(_gnutls_urandom_fd, F_SETFD, old | FD_CLOEXEC);
_rnd_get_system_entropy = _rnd_get_system_entropy_urandom;
return 0;
fallback:
- device_fd = _rndegd_connect_socket();
- if (device_fd < 0) {
+ _gnutls_urandom_fd = _rndegd_connect_socket();
+ if (_gnutls_urandom_fd < 0) {
_gnutls_debug_log("Cannot open egd socket!\n");
return
gnutls_assert_val
void _rnd_system_entropy_deinit(void)
{
- if (device_fd >= 0) {
- close(device_fd);
- device_fd = -1;
+ if (_gnutls_urandom_fd >= 0) {
+ close(_gnutls_urandom_fd);
+ _gnutls_urandom_fd = -1;
}
}
#endif