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
94
95
96
97
98
99
100
|
From a44701abd995b3db80001d0c5d88e9ead05972c1 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 24/26] 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
Change-Id: I980170969410381973ce75f6679a4a1401738847
Signed-off-by: Cristian Stoica <cristian.stoica@freescale.com>
Reviewed-on: http://git.am.freescale.net:8181/34219
---
crypto/engine/eng_cryptodev.c | 50 +++++++++++++++++++++----------------------
1 file changed, 24 insertions(+), 26 deletions(-)
diff --git a/crypto/engine/eng_cryptodev.c b/crypto/engine/eng_cryptodev.c
index dceb4f5..b74fc7c 100644
--- a/crypto/engine/eng_cryptodev.c
+++ b/crypto/engine/eng_cryptodev.c
@@ -306,47 +306,45 @@ static void ctr64_inc(unsigned char *counter) {
if (c) return;
} while (n);
}
-/*
- * Return a fd if /dev/crypto seems usable, 0 otherwise.
- */
-static int
-open_dev_crypto(void)
+
+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)
+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.3.5
|