-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zvfs: improve libc FILE to integer fd abstraction #83386
base: main
Are you sure you want to change the base?
zvfs: improve libc FILE to integer fd abstraction #83386
Conversation
d69d5a7
to
de629fa
Compare
The function signature for open() in libc-hooks.c was incorrect. Additionally, the ALIAS_OF() macro causes a compile error, because it simplifies the open function signature to int open(), which is a conflicting definition. Use the correct definition to get around compile errors. Signed-off-by: Chris Friedt <[email protected]>
de629fa
to
6733b5d
Compare
@tagunil / @evgeniy-paltsev - ARC mwdt might need a similar change. I would be happy to include it here if you have suggestions. |
I generally like the idea, but I need time to check if/how it would work for our legacy MW libc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does z_libc_file_alloc
differ from the POSIX fdopen
function? How does z_libc_file_get_fd
differ from the POSIX fileno
function?
Mostly by name. These functions can't depend on POSIX. |
@tejlmand - do you know if there are similar changes necessary for the armclang toolchain? If so, I would be happy to include them here. |
So, the picolibc (and newlib) implementations could just be wrappers? |
What would the picolibc and newlib implementations be wrappers around? Maybe I've misunderstood the question. |
Alternatively, just add fdopen and fileno to the Zephyr API and use them directly. |
That would introduce a POSIX dependency (as well as a dependency cycle), which is what we're trying to avoid in this situation. It could maybe work if there were similar libc-internal functions that were not aliases of In other words, here, we are limited to ISO C only and non-POSIX functions. |
So all we need to do is expose fileno and fdopen as Zephyr functions from the C library, just as we do for other Zephyr APIs which came from POSIX, like |
Not exactly - strnlen() and strtok_r() have zero side-effects and do not deal with management or validation of kernel resources. Some of the kernel side of this might eventually hop the fence to syscall neighbourhood, and it seems like there would be some dependency cycles formed by calling userspace-facing APIs. From kernel space.
I realize it's tempting to hack things, blur the lines and take shortcuts, for the sake of size-optimization, but I feel it's the wrong approach. Especially because "Zephyr is not a POSIX OS". I think it's ok to call functions here that are not directly part of POSIX APIs though, so maybe if there are aliases that exist, those can be used instead (even though it's technically still the same code). Here is a question: if, for example, only |
Define fcntl.h constants in terms of zvfs constants. Signed-off-by: Chris Friedt <[email protected]>
52c48ca
to
b1685eb
Compare
I tried using the aliases as well as the POSIX symbols here, but it unfortunately introduced an infinite recursion. I also tried using the option Probably the existing change should suffice, for now. |
Previously, there was an implicit assumption that Zephyr's internal struct fd_entry * was synonymous with FILE * from the C library. This is generally not the case and aliasing these two distinct types was preventing a fair bit of functionality from Just Working - namely stdio function calls like fgets() and fopen(). The problem count be seen directly when trying to use a function like zvfs_fdopen(). Instead of aliasing the two types, require that all Zephyr C libraries provide 1. FILE *z_libc_file_alloc(int fd, const char *mode) Allocate and populate the required fields of a FILE object 2. int z_libc_file_get_fd(FILE *fp) Convert a FILE* object to an integer file descriptor. For Picolibc and Newlib-based C libraries, these functions set and get the integer file descriptor from a field of the internal FILE object representation. For the minimal C library, these functions convert between array index and struct fd_entry pointers. Signed-off-by: Chris Friedt <[email protected]>
b1685eb
to
f1bdef2
Compare
|
Previously, there was an implicit assumption that Zephyr's internal
struct fd_entry *
was synonymous withFILE *
from the C library.This is generally not the case and aliasing these two distinct types was preventing a fair bit of functionality from Just Working - namely stdio function calls like
fgets()
andfopen()
. The problem count be seen directly when trying to use a function likezvfs_fdopen()
.Instead of aliasing the two types, require that all Zephyr C libraries provide
FILE *z_libc_file_alloc(int fd, const char *mode)
, to allocate and populate the required fields of aFILE
objectint z_libc_file_get_fd(FILE *fp)
, to convert aFILE*
object to an integer file descriptor.For Picolibc and Newlib-based C libraries, these functions set and get the integer file descriptor from a field of the internal
FILE
object representation.For the minimal C library, these functions convert between array index and
struct fd_entry *
.