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
|
From d9395f7d876f7dfaaae25867c88d1e1f684589de Mon Sep 17 00:00:00 2001
From: Cristian Stoica <cristian.stoica@freescale.com>
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 <cristian.stoica@freescale.com>
---
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
|