#lang scheme/base

(require scheme/system)
(provide open-output-process)

;; Together with spawn.c this gets rid of zombie processes. Child
;; processes get killed whenever their stdin is closed.

(define (open-output-process . cmdline)
  (define out (open-output-file "/tmp/output-process.log" #:exists 'append))
  (let-values
      (((proc
         stdout
         stdin
         stderr) (apply subprocess
                        out #f out
                        "/usr/local/bin/spawn"
                        cmdline)))
    (subprocess-wait proc)
    ;;;; Can't detect exec errors!
    ;; (unless (= 0 (subprocess-status proc))
    ;;  (error 'dfork-error))
    stdin))

;; It doesn't look like it's so straightforward to do this with the
;; FFI. Also, there will be code duplication from
;; plt/src/mzscheme/src/port.c which performs some action after fork
;; and before exec.

