mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
feat(linux): provide getrandom implementation for macOS
This makes getrandom(2) usable when compiling with IDF_TARGET=linux on a macOS host.
This commit is contained in:
parent
a596ca56a8
commit
7b8f69404f
@ -3,5 +3,10 @@ if(NOT "${target}" STREQUAL "linux")
|
||||
return()
|
||||
endif()
|
||||
|
||||
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
|
||||
list(APPEND srcs getrandom.c)
|
||||
endif()
|
||||
|
||||
idf_component_register(INCLUDE_DIRS include
|
||||
REQUIRED_IDF_TARGETS linux)
|
||||
REQUIRED_IDF_TARGETS linux
|
||||
SRCS ${srcs})
|
||||
|
28
components/linux/getrandom.c
Normal file
28
components/linux/getrandom.c
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "sys/random.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
// getrandom() is not available on macOS, so we read from /dev/urandom instead.
|
||||
|
||||
int getrandom(void *buf, size_t buflen, unsigned int flags)
|
||||
{
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
ssize_t ret = read(fd, buf, buflen);
|
||||
close(fd);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
15
components/linux/include/sys/random.h
Normal file
15
components/linux/include/sys/random.h
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include_next "sys/random.h"
|
||||
|
||||
#if __APPLE__
|
||||
#include <stddef.h>
|
||||
|
||||
int getrandom(void *buf, size_t buflen, unsigned int flags);
|
||||
|
||||
#endif // __APPLE__
|
Loading…
x
Reference in New Issue
Block a user