Consolidate non cancellable open call
[glibc.git] / sysdeps / unix / sysv / linux / fips-private.h
blob3a83a0f158c529780d8deab0f3a86f5640366e4c
1 /* FIPS compliance status test for GNU/Linux systems.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _FIPS_PRIVATE_H
20 #define _FIPS_PRIVATE_H
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <not-cancel.h>
27 #include <stdbool.h>
29 /* Return true if FIPS mode is enabled. See
30 sysdeps/generic/fips-private.h for more information. */
32 static bool
33 fips_enabled_p (void)
35 static enum
37 FIPS_UNTESTED = 0,
38 FIPS_ENABLED = 1,
39 FIPS_DISABLED = -1,
40 FIPS_TEST_FAILED = -2
41 } checked;
43 if (checked == FIPS_UNTESTED)
45 int fd = __open_nocancel ("/proc/sys/crypto/fips_enabled", O_RDONLY);
47 if (fd != -1)
49 /* This is more than enough, the file contains a single integer. */
50 char buf[32];
51 ssize_t n;
52 n = TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, sizeof (buf) - 1));
53 close_not_cancel_no_status (fd);
55 if (n > 0)
57 /* Terminate the string. */
58 buf[n] = '\0';
60 char *endp;
61 long int res = strtol (buf, &endp, 10);
62 if (endp != buf && (*endp == '\0' || *endp == '\n'))
63 checked = (res > 0) ? FIPS_ENABLED : FIPS_DISABLED;
67 if (checked == FIPS_UNTESTED)
68 checked = FIPS_TEST_FAILED;
71 return checked == FIPS_ENABLED;
74 #endif /* _FIPS_PRIVATE_H */