test-close-on-exec 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/sh
  2. exec guile -q -s "$0" "$@"
  3. !#
  4. ;;; Exercise the 'e' flag to 'open-file' (O_CLOEXEC).
  5. (unless (provided? 'fork)
  6. (exit 77))
  7. (define file
  8. (string-append (or (getenv "TMPDIR") "/tmp")
  9. "/guile-test-close-on-exec-"
  10. (number->string (getpid)) ".txt"))
  11. ;;; Since fcntl(2) F_GETFD does not return flags such as O_CLOEXEC,
  12. ;;; create a child process, call 'exec', and make sure it doesn't
  13. ;;; inherit the file descriptor.
  14. (let ((port (open-file file "we")))
  15. (display "Hello!\n" port)
  16. (let ((pid (primitive-fork)))
  17. (if (zero? pid)
  18. (dynamic-wind
  19. (const #t)
  20. (lambda ()
  21. (execlp "guile" "guile" "-c"
  22. (object->string
  23. `(catch #t
  24. (lambda ()
  25. (fdopen ,(fileno port) "w")
  26. (primitive-exit 0))
  27. (lambda (key . args)
  28. (pk 'child-exception args)
  29. (if (and (eq? key 'system-error)
  30. (= EBADF (system-error-errno (cons key args))))
  31. (primitive-exit 1)
  32. (primitive-exit 2)))))))
  33. (lambda ()
  34. (primitive-exit 3)))
  35. (let ((status (pk 'child-status (cdr (waitpid pid)))))
  36. (false-if-exception (delete-file file))
  37. (exit (equal? (status:exit-val status) 1))))))