From d9395f7d876f7dfaaae25867c88d1e1f684589de Mon Sep 17 00:00:00 2001 From: Cristian Stoica Date: Thu, 19 Feb 2015 16:43:29 +0200 Subject: [PATCH 21/48] cryptodev: do not cache file descriptor in 'open' The file descriptor returned by get_dev_crypto is cached after a successful return. The issue is, it is cached inside 'open_dev_crypto' which is no longer useful as a general purpose open("/dev/crypto") function. This patch is a refactoring that moves the caching operation from open_dev_crypto to get_dev_crypto and leaves the former as a simpler function true to its name Signed-off-by: Cristian Stoica --- crypto/engine/eng_cryptodev.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/crypto/engine/eng_cryptodev.c b/crypto/engine/eng_cryptodev.c index 14dcddf..75fca7f 100644 --- a/crypto/engine/eng_cryptodev.c +++ b/crypto/engine/eng_cryptodev.c @@ -391,45 +391,44 @@ static void ctr64_inc(unsigned char *counter) } while (n); } -/* - * Return a fd if /dev/crypto seems usable, 0 otherwise. - */ static int open_dev_crypto(void) { - static int fd = -1; + int fd; - if (fd == -1) { - if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1) - return (-1); - /* close on exec */ - if (fcntl(fd, F_SETFD, 1) == -1) { - close(fd); - fd = -1; - return (-1); - } + fd = open("/dev/crypto", O_RDWR, 0); + if (fd < 0) + return -1; + + /* close on exec */ + if (fcntl(fd, F_SETFD, 1) == -1) { + close(fd); + return -1; } - return (fd); + + return fd; } static int get_dev_crypto(void) { - int fd, retfd; + static int fd = -1; + int retfd; - if ((fd = open_dev_crypto()) == -1) - return (-1); -# ifndef CRIOGET_NOT_NEEDED + if (fd == -1) + fd = open_dev_crypto(); +# ifdef CRIOGET_NOT_NEEDED + return fd; +# else + if (fd == -1) + return -1; if (ioctl(fd, CRIOGET, &retfd) == -1) return (-1); - /* close on exec */ if (fcntl(retfd, F_SETFD, 1) == -1) { close(retfd); return (-1); } -# else - retfd = fd; + return retfd; # endif - return (retfd); } static void put_dev_crypto(int fd) -- 2.7.0