Skip to content

Instantly share code, notes, and snippets.

@cfriedt
Created July 5, 2024 11:43
Show Gist options
  • Save cfriedt/458a60d2827f230e709543fc33b73aa2 to your computer and use it in GitHub Desktop.
Save cfriedt/458a60d2827f230e709543fc33b73aa2 to your computer and use it in GitHub Desktop.
From 398403cec5a0167c0d027196913b17759cc1a2a1 Mon Sep 17 00:00:00 2001
From: Chris Friedt <cfriedt@tenstorrent.com>
Date: Tue, 2 Jul 2024 20:13:49 -0400
Subject: [PATCH] fdtable: avoid pulling in posix header & types
Although fdtable sat a layer below posix, as part of
the base os, it was reaching up a layer for posix types,
creating another dependency cycle.
Additionally, <sys/select.h> no longer has
dependencies on networking, so there should be no
reason to include anything other than fdtable.h .
Generally, it would be best to use only ISO or native
zephyr types below POSIX, and these should likely be
removed after v3.7.0 .
Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
---
include/zephyr/posix/sys/select.h | 6 ++----
include/zephyr/sys/fdtable.h | 26 +++++++++++++++++++++++---
2 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/include/zephyr/posix/sys/select.h b/include/zephyr/posix/sys/select.h
index 78d900f5316b2f..f0521db58341de 100644
--- a/include/zephyr/posix/sys/select.h
+++ b/include/zephyr/posix/sys/select.h
@@ -6,15 +6,13 @@
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_SELECT_H_
#define ZEPHYR_INCLUDE_POSIX_SYS_SELECT_H_
-#include <zephyr/net/socket_types.h>
-#include <zephyr/net/socket_select.h>
+#include <zephyr/sys/fdtable.h>
#ifdef __cplusplus
extern "C" {
#endif
-#undef fd_set
-#define fd_set zsock_fd_set
+typedef struct zvfs_fd_set fd_set;
#define FD_SETSIZE ZVFS_FD_SETSIZE
diff --git a/include/zephyr/sys/fdtable.h b/include/zephyr/sys/fdtable.h
index 37b25d57c75eb2..0b770b19c433ea 100644
--- a/include/zephyr/sys/fdtable.h
+++ b/include/zephyr/sys/fdtable.h
@@ -7,13 +7,33 @@
#define ZEPHYR_INCLUDE_SYS_FDTABLE_H_
#include <stdarg.h>
-#include <sys/types.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
-/* FIXME: For native_posix ssize_t, off_t. */
-#include <zephyr/fs/fs.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/util.h>
+/*
+ * Note: avoid using POSIX headers and types in layers below POSIX.
+ * e.g. <sys/types.h>, ssize_t, off_t.
+ */
+
+#ifndef _SSIZE_T_DECLARED
+/* avoid warning when 'long' is used for both 32 and 64-bit */
+#ifdef CONFIG_64BIT
+typedef long ssize_t;
+#else
+typedef int ssize_t;
+#endif
+#define _SSIZE_T_DECLARED
+#endif
+
+#ifndef _OFF_T_DECLARED
+typedef long off_t;
+#define _OFF_T_DECLARED
+#endif
+
/* File mode bits */
#define ZVFS_MODE_IFMT 0170000
#define ZVFS_MODE_UNSPEC 0000000
@cfriedt
Copy link
Author

cfriedt commented Jul 7, 2024

This was the workaround for the layering violation in Zephyr that passed all tests prior to the posix device io and posix fd mgmt PRs getting reverted.

The correct solution is at
zephyrproject-rtos/zephyr#75348

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment