[PATCH] posix_spawn: factor out structs to an internal header

Jeremy Drake cygwin@jdrake.com
Tue Jun 17 20:40:12 GMT 2025


This will allow Cygwin to examine and handle the posix_spawnattr_t and
posix_spawn_file_actions_t arguments itself.

Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
---
I'm not subscribed to the newlib list but rathe to cygwin-patches

Context: I'm working on a patch to Cygwin to optimize posix_spawn where
possible to not use fork/exec but instead spawn the child process
directly.  Determining whether this is possible depends on examining the
parameters, and while the posix_spawnattr_t has "get" functions that can
be used to examine its members the posix_spawn_file_actions_t has no such
ability.

 newlib/libc/posix/posix_spawn.c | 45 +--------------------------
 newlib/libc/posix/posix_spawn.h | 54 +++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 44 deletions(-)
 create mode 100644 newlib/libc/posix/posix_spawn.h

diff --git a/newlib/libc/posix/posix_spawn.c b/newlib/libc/posix/posix_spawn.c
index 46e4e5369f..51ad23f825 100644
--- a/newlib/libc/posix/posix_spawn.c
+++ b/newlib/libc/posix/posix_spawn.c
@@ -102,56 +102,13 @@ Supporting OS subroutines required: <<_close>>, <<dup2>>, <<_fcntl>>,
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "posix_spawn.h"

 /* Only deal with a pointer to environ, to work around subtle bugs with shared
    libraries and/or small data systems where the user declares his own
    'environ'.  */
 static char ***p_environ = &environ;

-struct __posix_spawnattr {
-	short			sa_flags;
-	pid_t			sa_pgroup;
-	struct sched_param	sa_schedparam;
-	int			sa_schedpolicy;
-	sigset_t		sa_sigdefault;
-	sigset_t		sa_sigmask;
-};
-
-struct __posix_spawn_file_actions {
-	STAILQ_HEAD(, __posix_spawn_file_actions_entry) fa_list;
-};
-
-typedef struct __posix_spawn_file_actions_entry {
-	STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list;
-	enum {
-		FAE_OPEN,
-		FAE_DUP2,
-		FAE_CLOSE,
-		FAE_CHDIR,
-		FAE_FCHDIR
-	} fae_action;
-
-	int fae_fildes;
-	union {
-		struct {
-			char *path;
-#define fae_path	fae_data.open.path
-			int oflag;
-#define fae_oflag	fae_data.open.oflag
-			mode_t mode;
-#define fae_mode	fae_data.open.mode
-		} open;
-		struct {
-			int newfildes;
-#define fae_newfildes	fae_data.dup2.newfildes
-		} dup2;
-		char *dir;
-#define fae_dir		fae_data.dir
-		int dirfd;
-#define fae_dirfd		fae_data.dirfd
-	} fae_data;
-} posix_spawn_file_actions_entry_t;
-
 /*
  * Spawn routines
  */
diff --git a/newlib/libc/posix/posix_spawn.h b/newlib/libc/posix/posix_spawn.h
new file mode 100644
index 0000000000..b4cad1e523
--- /dev/null
+++ b/newlib/libc/posix/posix_spawn.h
@@ -0,0 +1,54 @@
+#ifndef _POSIX_SPAWN_H_
+#define	_POSIX_SPAWN_H_
+
+#include <sys/cdefs.h>
+#include <sys/sched.h>
+#include <sys/signal.h>
+#include <sys/types.h>
+#include <sys/queue.h>
+
+struct __posix_spawnattr {
+	short			sa_flags;
+	pid_t			sa_pgroup;
+	struct sched_param	sa_schedparam;
+	int			sa_schedpolicy;
+	sigset_t		sa_sigdefault;
+	sigset_t		sa_sigmask;
+};
+
+struct __posix_spawn_file_actions {
+	STAILQ_HEAD(, __posix_spawn_file_actions_entry) fa_list;
+};
+
+typedef struct __posix_spawn_file_actions_entry {
+	STAILQ_ENTRY(__posix_spawn_file_actions_entry) fae_list;
+	enum {
+		FAE_OPEN,
+		FAE_DUP2,
+		FAE_CLOSE,
+		FAE_CHDIR,
+		FAE_FCHDIR
+	} fae_action;
+
+	int fae_fildes;
+	union {
+		struct {
+			char *path;
+#define fae_path	fae_data.open.path
+			int oflag;
+#define fae_oflag	fae_data.open.oflag
+			mode_t mode;
+#define fae_mode	fae_data.open.mode
+		} open;
+		struct {
+			int newfildes;
+#define fae_newfildes	fae_data.dup2.newfildes
+		} dup2;
+		char *dir;
+#define fae_dir		fae_data.dir
+		int dirfd;
+#define fae_dirfd		fae_data.dirfd
+	} fae_data;
+} posix_spawn_file_actions_entry_t;
+
+#endif /* !_POSIX_SPAWN_H_ */
-- 
2.49.0.windows.1



More information about the Newlib mailing list