Here are a few examples of "broken" drivers:
- opamp_fops
- lcddev_fops
- g_pca9635pw_fileops
- g_foc_fops
- notectl_fops
- powerled_fops
- g_rptun_devops
- g_video_fops
The list is not complete. But since the "register_driver()" does not return
an error if both fops read and write pointers are NULL we are still in the
middle of discussion.
I just want to understand the algorithm to get it fixed. So let me
summarise again and if nobody has any objections I will implement the
change:
1. Zero "oflags" should be considered as illegal and "open" call should
return "-EACCES" in this case.
Yes, POSIX requires this:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html "
/"... Applications shall specify exactly one of the first five
values (file access modes) below in the value of //oflag//:/
/O_EXEC/
/Open for execute only (non-directory files). The result is
unspecified if this flag is applied to a directory./
/O_RDONLY/
/Open for reading only./
/O_RDWR/
/Open for reading and writing. The result is undefined if this
flag is applied to a FIFO./
/O_SEARCH/
/Open directory for search only. The result is unspecified if
this flag is applied to a non-directory file./
/O_WRONLY/
/Open for writing only."/
(Assuming that the above are all non-zero, of course). O_EXEC and
O_SEARCH are required by the current POSIX spec but is not implemented
in NuttX.
2. The "register_driver()" should check if both "fops" read and write
pointers are NULL and return an error if such a situation is detected.
I don't believe this is necessary in general. Perhaps only if
DEBUG_FEATURES is enabled? Or perhaps a DEBUG assertion?
3. Driver register should check if "mode" value is consistent with provided
"fops" and if "read" method is NULL but "mode" contains "r" the error
should be returned. The same for write and "w" permission.
An alternative, more POSIX-like implementation would flesh out the mode
logic. fs_registerdriver.s, for example, has a mode argument that is
retained in the inode. The mode could be used to determine if VFS
entity is read-able or write-able.
That would be a lot of work and most file systems do not support
permissions but would take us a long way toward a Unix-like security
system with proper permissions.
4. Update all the drivers that have both read and write methods as NULL and
add "dummy_read()" or "dummy_write" handler. Optionally in order to save
space the "fops_dummy_read" and "fops_dummy_write" can be added into
"include/nuttx/fs/fs.h" so drivers will not need to duplicate the code and
can reference a common implementation.
NOTE that the dummy read method returns EOF, but the dummy write returns
the write size. write never returns zero unless nbyte is zero:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
Greg