/src/libreoffice/sal/osl/unx/random.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | */ |
9 | | |
10 | | #include <oslrandom.h> |
11 | | |
12 | | #include <assert.h> |
13 | | #include <errno.h> |
14 | | #include <fcntl.h> |
15 | | #include <unistd.h> |
16 | | #include <dlfcn.h> |
17 | | |
18 | | bool osl_get_system_random_data(char* buffer, size_t desired_len) |
19 | 155k | { |
20 | 155k | int fd; |
21 | | |
22 | 155k | assert(buffer); |
23 | | |
24 | 155k | static int (*lok_open_urandom)() |
25 | 155k | = reinterpret_cast<int (*)()>(dlsym(RTLD_DEFAULT, "lok_open_urandom")); |
26 | 155k | if (!lok_open_urandom || (fd = lok_open_urandom()) < 0) |
27 | 155k | fd = open("/dev/urandom", O_RDONLY); |
28 | | |
29 | 155k | if (fd != -1) |
30 | 155k | { |
31 | 311k | while (desired_len) |
32 | 155k | { |
33 | 155k | ssize_t nb_read; |
34 | 155k | if ((nb_read = read(fd, buffer, desired_len)) < 0) |
35 | 0 | { |
36 | 0 | if (errno != EINTR) |
37 | 0 | { |
38 | 0 | close(fd); |
39 | 0 | return false; |
40 | 0 | } |
41 | 0 | } |
42 | 155k | else |
43 | 155k | { |
44 | 155k | buffer += nb_read; |
45 | 155k | desired_len -= nb_read; |
46 | 155k | } |
47 | 155k | } |
48 | 155k | close(fd); |
49 | 155k | return true; |
50 | 155k | } |
51 | 0 | return false; |
52 | 155k | } |
53 | | |
54 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |