2 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
10 #include "native_client/src/public/name_service.h"
11 #include "native_client/src/untrusted/nacl/nacl_random.h"
12 #include "native_client/src/untrusted/irt/irt.h"
13 #include "native_client/src/untrusted/irt/irt_private.h"
14 #include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h"
16 static int secure_random_fd = -1;
18 static int prepare_secure_random(void) {
19 if (secure_random_fd == -1) {
21 int status = irt_nameservice_lookup("SecureRandom", O_RDONLY, &fd);
22 if (status != NACL_NAME_SERVICE_SUCCESS) {
25 if (!__sync_bool_compare_and_swap(&secure_random_fd, -1, fd)) {
27 * We raced with another thread. It already installed an fd.
29 NACL_SYSCALL(close)(fd);
35 int nacl_secure_random_init(void) {
39 int nacl_secure_random(void *buf, size_t count, size_t *nread) {
40 int error = prepare_secure_random();
42 int rv = NACL_GC_WRAP_SYSCALL(NACL_SYSCALL(read)(secure_random_fd,
52 const struct nacl_irt_random nacl_irt_random = {