1515#include <zephyr/fs/fs_sys.h>
1616#include <zephyr/fs/littlefs.h>
1717
18+ #include <zephyr/net/socket.h>
19+ #include <zephyr/posix/fcntl.h> // F_GETFL, F_SETFL, O_NONBLOCK
20+ // #include <zephyr/net/socket_select.h>
21+
1822/* Notes:
1923 * This is the implementation of a POSIX-like file system interface for Zephyr.
2024 * To manage our file descriptors, we created a struct `zephyr_fs_desc` that
@@ -273,6 +277,16 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
273277{
274278 struct zephyr_fs_desc * ptr = NULL ;
275279
280+ /* Sockets: reflect current non-blocking state */
281+ if (handle -> is_sock ) {
282+ int cur = zsock_fcntl (handle -> fd , F_GETFL , 0 );
283+ if (cur < 0 )
284+ return convert_errno (errno );
285+ if (cur & O_NONBLOCK )
286+ * flags |= __WASI_FDFLAG_NONBLOCK ;
287+ return __WASI_ESUCCESS ;
288+ }
289+
276290 if (os_is_virtual_fd (handle -> fd )) {
277291 * flags = 0 ;
278292 return __WASI_ESUCCESS ;
@@ -283,31 +297,38 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
283297 if ((ptr -> file .flags & FS_O_APPEND ) != 0 ) {
284298 * flags |= __WASI_FDFLAG_APPEND ;
285299 }
286- /* Others flags:
287- * - __WASI_FDFLAG_DSYNC
288- * - __WASI_FDFLAG_RSYNC
289- * - __WASI_FDFLAG_SYNC
290- * - __WASI_FDFLAG_NONBLOCK
291- * Have no equivalent in Zephyr.
292- */
300+
293301 return __WASI_ESUCCESS ;
294302}
295303
296304__wasi_errno_t
297305os_file_set_fdflags (os_file_handle handle , __wasi_fdflags_t flags )
298306{
299- if (os_is_virtual_fd (handle -> fd )) {
307+ /* Sockets: set/clear O_NONBLOCK */
308+ if (handle -> is_sock ) {
309+ int cur = zsock_fcntl (handle -> fd , F_GETFL , 0 );
310+ if (cur < 0 )
311+ return convert_errno (errno );
312+
313+ bool want_nb = (flags & __WASI_FDFLAG_NONBLOCK ) != 0 ;
314+ int newf = want_nb ? (cur | O_NONBLOCK ) : (cur & ~O_NONBLOCK );
315+
316+ if (zsock_fcntl (handle -> fd , F_SETFL , newf ) < 0 ) {
317+ return convert_errno (errno );
318+ }
300319 return __WASI_ESUCCESS ;
301320 }
302321
303- struct zephyr_fs_desc * ptr = NULL ;
322+ /* Virtual stdio */
323+ if (os_is_virtual_fd (handle -> fd ))
324+ return __WASI_ESUCCESS ;
304325
326+ /* Regular files: keep existing behavior */
327+ struct zephyr_fs_desc * ptr = NULL ;
305328 GET_FILE_SYSTEM_DESCRIPTOR (handle -> fd , ptr );
306-
307- if ((flags & __WASI_FDFLAG_APPEND ) != 0 ) {
329+ if ((flags & __WASI_FDFLAG_APPEND ) != 0 )
308330 ptr -> file .flags |= FS_O_APPEND ;
309- }
310- /* Same as above */
331+
311332 return __WASI_ESUCCESS ;
312333}
313334
0 commit comments