mike121 pushed a commit to branch main
in repository guile.

commit 9c86c5936e6eda65e4e38e95d582ee0d2360af47
Author: Michael Gran <spk...@yahoo.com>
AuthorDate: Tue Jun 20 15:38:54 2023 -0700

    Allow piped-process and system* to exist when fork is undefined
    
    piped-process only uses fork to match legacy behavior, but on systems
    that never had fork, there is no need to match that behavior.
    piped-process and system* can be provided without fork.
    
    * libguile/posix.c (piped_process): allow function definition without 
HAVE_FORK,
        but stub out internal dummy process with HAVE_FORK
      (restore_sigaction, scm_dynwind_sigaction, scm_system_star): don't
        require HAVE_FORK
      (scm_init_popen): don't require HAVE_FORK
      (scm_init_posix): don't require HAVE_FORK to add posix feature or
        register popen extension
---
 NEWS             | 8 ++++++++
 libguile/posix.c | 9 ++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index dafad95c4..dae2e0ff7 100644
--- a/NEWS
+++ b/NEWS
@@ -60,6 +60,14 @@ capability to search for "libfoo" as "msys-foo.dll" on MSYS.
 The load-foreign-library option #:rename-on-cygwin? has been changed to
 #:host-type-rename?, and handles both Cygwin and MSYS.
 
+** Make piped-process and system* available on systems without fork
+
+Now that piped-process and system* are implemented in terms of
+`posix_spawn', they can be made available on systems without fork().
+Note that currently Guile does use fork in piped-process to set exit
+codes, so piped-process on systems without fork will have a different
+behavior with regards to exit codes.
+
 * Performance improvements
 
 ** `copy-file` now relies on `sendfile` rather than a read/write loop
diff --git a/libguile/posix.c b/libguile/posix.c
index 3d38bc310..6e9fd4b7c 100644
--- a/libguile/posix.c
+++ b/libguile/posix.c
@@ -1529,7 +1529,6 @@ SCM_DEFINE (scm_spawn_process, "spawn", 2, 0, 1,
 }
 #undef FUNC_NAME
 
-#ifdef HAVE_FORK
 static int
 piped_process (pid_t *pid, SCM prog, SCM args, SCM from, SCM to)
 #define FUNC_NAME "piped-process"
@@ -1630,11 +1629,13 @@ scm_piped_process (SCM prog, SCM args, SCM from, SCM to)
       /* Create a dummy process that exits with value 127 to mimic the
          previous fork + exec implementation.  TODO: This is a
          compatibility shim to remove in the next stable series.  */
+#ifdef HAVE_FORK
       pid = fork ();
       if (pid == -1)
         SCM_SYSERROR;
       if (pid == 0)
         _exit (127);
+#endif /* HAVE_FORK */
     }
 
   return scm_from_int (pid);
@@ -1690,7 +1691,6 @@ SCM_DEFINE (scm_system_star, "system*", 0, 0, 1,
   return scm_from_int (status);
 }
 #undef FUNC_NAME
-#endif /* HAVE_FORK */
 
 #ifdef HAVE_UNAME
 SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
@@ -2566,13 +2566,12 @@ SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
 #endif /* HAVE_GETHOSTNAME */
 
 
-#ifdef HAVE_FORK
 static void
 scm_init_popen (void)
 {
   scm_c_define_gsubr ("piped-process", 2, 2, 0, scm_piped_process);
 }
-#endif /* HAVE_FORK */
+
 
 void
 scm_init_posix ()
@@ -2690,10 +2689,10 @@ scm_init_posix ()
 
 #ifdef HAVE_FORK
   scm_add_feature ("fork");
+#endif /* HAVE_FORK */
   scm_add_feature ("popen");
   scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
                             "scm_init_popen",
                            (scm_t_extension_init_func) scm_init_popen,
                            NULL);
-#endif /* HAVE_FORK */
 }

Reply via email to