2012-10-22 13:08:42 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2013-02-20 21:17:38 +00:00
|
|
|
#include "platform/globals.h"
|
2017-03-15 20:11:05 +00:00
|
|
|
#if defined(HOST_OS_MACOS)
|
2013-02-20 21:17:38 +00:00
|
|
|
|
|
|
|
#include <errno.h> // NOLINT
|
|
|
|
#include <fcntl.h> // NOLINT
|
2012-10-22 13:08:42 +00:00
|
|
|
|
|
|
|
#include "bin/crypto.h"
|
2016-03-16 17:01:00 +00:00
|
|
|
#include "bin/fdutils.h"
|
2014-03-17 13:51:11 +00:00
|
|
|
#include "platform/signal_blocker.h"
|
|
|
|
|
2013-04-25 14:22:30 +00:00
|
|
|
namespace dart {
|
|
|
|
namespace bin {
|
|
|
|
|
2012-10-22 13:08:42 +00:00
|
|
|
bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
|
2014-03-25 13:06:37 +00:00
|
|
|
ThreadSignalBlocker signal_blocker(SIGPROF);
|
2016-11-04 19:30:56 +00:00
|
|
|
intptr_t fd =
|
|
|
|
TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(open("/dev/urandom", O_RDONLY));
|
2014-09-25 23:39:30 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-25 13:06:37 +00:00
|
|
|
intptr_t bytes_read = 0;
|
|
|
|
do {
|
|
|
|
int res = TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(
|
|
|
|
read(fd, buffer + bytes_read, count - bytes_read));
|
|
|
|
if (res < 0) {
|
|
|
|
int err = errno;
|
|
|
|
VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(close(fd));
|
|
|
|
errno = err;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bytes_read += res;
|
|
|
|
} while (bytes_read < count);
|
|
|
|
VOID_TEMP_FAILURE_RETRY_NO_SIGNAL_BLOCKER(close(fd));
|
|
|
|
return true;
|
2012-10-22 13:08:42 +00:00
|
|
|
}
|
2013-02-20 21:17:38 +00:00
|
|
|
|
2013-04-25 14:22:30 +00:00
|
|
|
} // namespace bin
|
|
|
|
} // namespace dart
|
|
|
|
|
2017-03-15 20:11:05 +00:00
|
|
|
#endif // defined(HOST_OS_MACOS)
|