Coverage Report

Created: 2026-06-10 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/entropy.c
Line
Count
Source
1
/* Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
2
 *
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at:
6
 *
7
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15
16
#include <config.h>
17
18
#include "entropy.h"
19
20
#include <errno.h>
21
#include <fcntl.h>
22
#include <unistd.h>
23
#include "util.h"
24
#include "socket-util.h"
25
#include "openvswitch/vlog.h"
26
27
VLOG_DEFINE_THIS_MODULE(entropy);
28
29
static const char urandom[] = "/dev/urandom";
30
31
/* Initializes 'buffer' with 'n' bytes of high-quality random numbers.  Returns
32
 * 0 if successful, otherwise a positive errno value or EOF on error. */
33
int
34
get_entropy(void *buffer, size_t n)
35
0
{
36
0
    size_t bytes_read;
37
0
    int error;
38
0
    int fd;
39
40
0
    fd = open(urandom, O_RDONLY);
41
0
    if (fd < 0) {
42
0
        VLOG_ERR("%s: open failed (%s)", urandom, ovs_strerror(errno));
43
0
        return errno ? errno : EINVAL;
44
0
    }
45
46
0
    error = read_fully(fd, buffer, n, &bytes_read);
47
0
    close(fd);
48
49
0
    if (error) {
50
0
        VLOG_ERR("%s: read error (%s)", urandom, ovs_retval_to_string(error));
51
0
    }
52
0
    return error;
53
0
}
54
55
/* Initializes 'buffer' with 'n' bytes of high-quality random numbers.  Exits
56
 * if an error occurs. */
57
void
58
get_entropy_or_die(void *buffer, size_t n)
59
0
{
60
0
    int error = get_entropy(buffer, n);
61
0
    if (error) {
62
0
        VLOG_FATAL("%s: read error (%s)",
63
0
                   urandom, ovs_retval_to_string(error));
64
0
    }
65
0
}