From: Nikos Mavrogiannopoulos Date: Wed, 22 Oct 2014 10:19:25 +0000 (+0200) Subject: added gnutls_fd_in_use() to check whether a file descriptor is in use X-Git-Tag: gnutls_3_4_0~764 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c12530bacc89485e807ec18ae455f4e190b383f;p=thirdparty%2Fgnutls.git added gnutls_fd_in_use() to check whether a file descriptor is in use --- diff --git a/lib/gnutls_state.c b/lib/gnutls_state.c index 40be26e847..4f06ea4b5b 100644 --- a/lib/gnutls_state.c +++ b/lib/gnutls_state.c @@ -1162,6 +1162,28 @@ int gnutls_session_is_resumed(gnutls_session_t session) 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. diff --git a/lib/includes/gnutls/gnutls.h.in b/lib/includes/gnutls/gnutls.h.in index 7ddc5738ac..158aaed10c 100644 --- a/lib/includes/gnutls/gnutls.h.in +++ b/lib/includes/gnutls/gnutls.h.in @@ -2163,6 +2163,9 @@ void gnutls_certificate_set_pin_function(gnutls_certificate_credentials_t, gnutls_pin_callback_t fn, void *userdata); +/* FD-related functions */ +unsigned gnutls_fd_in_use(int fd); + /* FIPS140-2 related functions */ int gnutls_fips140_mode_enabled(void); diff --git a/lib/nettle/rnd-common.c b/lib/nettle/rnd-common.c index 1ea52ad80f..26d4c31440 100644 --- a/lib/nettle/rnd-common.c +++ b/lib/nettle/rnd-common.c @@ -126,7 +126,7 @@ void _rnd_system_entropy_deinit(void) #include #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) { @@ -136,7 +136,7 @@ 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) { @@ -167,7 +167,7 @@ int _rnd_get_system_entropy_egd(void* _rnd, size_t size) 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"); @@ -189,22 +189,22 @@ int _rnd_system_entropy_init(void) { 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 @@ -217,9 +217,9 @@ fallback: 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 diff --git a/lib/random.h b/lib/random.h index 0aa154527b..8c755f8997 100644 --- a/lib/random.h +++ b/lib/random.h @@ -48,4 +48,8 @@ inline static void _gnutls_rnd_refresh(void) void _gnutls_rnd_deinit(void); int _gnutls_rnd_init(void); +#ifndef _WIN32 +extern int _gnutls_urandom_fd; +#endif + #endif