Subject: bug#52835: [PATCH 0/2] Fix spawning a child not setting standard fds properly

From: Ludovic Courtès <[email-masked]>
Date: Sun, 25 Dec 2022 18:03:48 +0100

Josselin Poiret <[email-masked]> skribis:

> * libguile/posix.c: Include spawn.h from Gnulib.
> (do_spawn, scm_spawn_process): New functions.
> * module/ice-9/spawn.scm: New file
> (spawn): New procedure.
> ---
>  libguile/posix.c       | 82 ++++++++++++++++++++++++++++++++++++++++++
>  libguile/posix.h       |  2 ++
>  module/ice-9/spawn.scm | 54 ++++++++++++++++++++++++++++
>  3 files changed, 138 insertions(+)

The new module should be added to ‘am/bootstrap.am’.

> +SCM_API SCM scm_spawn_process (SCM prog, SCM args, SCM env,
> +                               SCM in, SCM out, SCM err);

Let’s keep it ‘SCM_INTERNAL’.

> +(define* (spawn exec-file
> +                #:optional (args (list exec-file))
> +                #:key      (env (environ))
> +                           (in (current-input-port))
> +                           (out (current-output-port))
> +                           (err (current-error-port)))

s/exec-file/program/
s/args/arguments/
s/env/environment/

s/in/standard-input/
s/out/standard-output/
s/err/standard-error/

Maybe we could allow these two be either ports or file descriptors?

> +  "Spawns a new process running the program @var{exec} with arguments
> +@var{args}, in the environment specified by the list of environment
> +variable strings @var{env}, and with standard input, output and error
> +set to the ports specified by @var{in}, @var{out}, @var{err}.  Note that
> +the last part only works with fd-backed ports."
> +  (let* ((in (port-with-defaults in "r"))
> +         (out (port-with-defaults out "w"))
> +         (err (port-with-defaults err "w"))
> +         ;; Increment port revealed counts while to prevent ports GC'ing and
> +         ;; closing the associated fds while we spawn the process.
> +         (result (spawn* exec-file
> +                         args
> +                         env
> +                         (port->fdes in)
> +                         (port->fdes out)
> +                         (port->fdes err))))

I believe ‘spawn*’ is unbound here because it’s defined by
‘scm_init_popen’, which is called within the (ice-9 popen) module.

Ludo’.