popen.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ;; popen emulation, for non-stdio based ports.
  2. ;;;; Copyright (C) 1998, 1999, 2000, 2001, 2003, 2006 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 2.1 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (ice-9 popen)
  19. :export (port/pid-table open-pipe* open-pipe close-pipe open-input-pipe
  20. open-output-pipe open-input-output-pipe))
  21. (define (make-rw-port read-port write-port)
  22. (make-soft-port
  23. (vector
  24. (lambda (c) (write-char c write-port))
  25. (lambda (s) (display s write-port))
  26. (lambda () (force-output write-port))
  27. (lambda () (read-char read-port))
  28. (lambda () (close-port read-port) (close-port write-port)))
  29. "r+"))
  30. ;; a guardian to ensure the cleanup is done correctly when
  31. ;; an open pipe is gc'd or a close-port is used.
  32. (define pipe-guardian (make-guardian))
  33. ;; a weak hash-table to store the process ids.
  34. (define port/pid-table (make-weak-key-hash-table 31))
  35. (define (ensure-fdes port mode)
  36. (or (false-if-exception (fileno port))
  37. (open-fdes *null-device* mode)))
  38. ;; run a process connected to an input, an output or an
  39. ;; input/output port
  40. ;; mode: OPEN_READ, OPEN_WRITE or OPEN_BOTH
  41. ;; returns port/pid pair.
  42. (define (open-process mode prog . args)
  43. (let* ((reading (or (equal? mode OPEN_READ)
  44. (equal? mode OPEN_BOTH)))
  45. (writing (or (equal? mode OPEN_WRITE)
  46. (equal? mode OPEN_BOTH)))
  47. (c2p (if reading (pipe) #f)) ; child to parent
  48. (p2c (if writing (pipe) #f))) ; parent to child
  49. (if c2p (setvbuf (cdr c2p) _IONBF))
  50. (if p2c (setvbuf (cdr p2c) _IONBF))
  51. (let ((pid (primitive-fork)))
  52. (cond ((= pid 0)
  53. ;; child
  54. (set-batch-mode?! #t)
  55. ;; select the three file descriptors to be used as
  56. ;; standard descriptors 0, 1, 2 for the new
  57. ;; process. They are pipes to/from the parent or taken
  58. ;; from the current Scheme input/output/error ports if
  59. ;; possible.
  60. (let ((input-fdes (if writing
  61. (fileno (car p2c))
  62. (ensure-fdes (current-input-port)
  63. O_RDONLY)))
  64. (output-fdes (if reading
  65. (fileno (cdr c2p))
  66. (ensure-fdes (current-output-port)
  67. O_WRONLY)))
  68. (error-fdes (ensure-fdes (current-error-port)
  69. O_WRONLY)))
  70. ;; close all file descriptors in ports inherited from
  71. ;; the parent except for the three selected above.
  72. ;; this is to avoid causing problems for other pipes in
  73. ;; the parent.
  74. ;; use low-level system calls, not close-port or the
  75. ;; scsh routines, to avoid side-effects such as
  76. ;; flushing port buffers or evicting ports.
  77. (port-for-each (lambda (pt-entry)
  78. (false-if-exception
  79. (let ((pt-fileno (fileno pt-entry)))
  80. (if (not (or (= pt-fileno input-fdes)
  81. (= pt-fileno output-fdes)
  82. (= pt-fileno error-fdes)))
  83. (close-fdes pt-fileno))))))
  84. ;; Copy the three selected descriptors to the standard
  85. ;; descriptors 0, 1, 2, if not already there
  86. (cond ((not (= input-fdes 0))
  87. (if (= output-fdes 0)
  88. (set! output-fdes (dup->fdes 0)))
  89. (if (= error-fdes 0)
  90. (set! error-fdes (dup->fdes 0)))
  91. (dup2 input-fdes 0)
  92. ;; it's possible input-fdes is error-fdes
  93. (if (not (= input-fdes error-fdes))
  94. (close-fdes input-fdes))))
  95. (cond ((not (= output-fdes 1))
  96. (if (= error-fdes 1)
  97. (set! error-fdes (dup->fdes 1)))
  98. (dup2 output-fdes 1)
  99. ;; it's possible output-fdes is error-fdes
  100. (if (not (= output-fdes error-fdes))
  101. (close-fdes output-fdes))))
  102. (cond ((not (= error-fdes 2))
  103. (dup2 error-fdes 2)
  104. (close-fdes error-fdes)))
  105. (apply execlp prog prog args)))
  106. (else
  107. ;; parent
  108. (if c2p (close-port (cdr c2p)))
  109. (if p2c (close-port (car p2c)))
  110. (cons (cond ((not writing) (car c2p))
  111. ((not reading) (cdr p2c))
  112. (else (make-rw-port (car c2p)
  113. (cdr p2c))))
  114. pid))))))
  115. (define (open-pipe* mode command . args)
  116. "Executes the program @var{command} with optional arguments
  117. @var{args} (all strings) in a subprocess.
  118. A port to the process (based on pipes) is created and returned.
  119. @var{modes} specifies whether an input, an output or an input-output
  120. port to the process is created: it should be the value of
  121. @code{OPEN_READ}, @code{OPEN_WRITE} or @code{OPEN_BOTH}."
  122. (let* ((port/pid (apply open-process mode command args))
  123. (port (car port/pid)))
  124. (pipe-guardian port)
  125. (hashq-set! port/pid-table port (cdr port/pid))
  126. port))
  127. (define (open-pipe command mode)
  128. "Executes the shell command @var{command} (a string) in a subprocess.
  129. A port to the process (based on pipes) is created and returned.
  130. @var{modes} specifies whether an input, an output or an input-output
  131. port to the process is created: it should be the value of
  132. @code{OPEN_READ}, @code{OPEN_WRITE} or @code{OPEN_BOTH}."
  133. (open-pipe* mode "/bin/sh" "-c" command))
  134. (define (fetch-pid port)
  135. (let ((pid (hashq-ref port/pid-table port)))
  136. (hashq-remove! port/pid-table port)
  137. pid))
  138. (define (close-process port/pid)
  139. (close-port (car port/pid))
  140. (cdr (waitpid (cdr port/pid))))
  141. ;; for the background cleanup handler: just clean up without reporting
  142. ;; errors. also avoids blocking the process: if the child isn't ready
  143. ;; to be collected, puts it back into the guardian's live list so it
  144. ;; can be tried again the next time the cleanup runs.
  145. (define (close-process-quietly port/pid)
  146. (catch 'system-error
  147. (lambda ()
  148. (close-port (car port/pid)))
  149. (lambda args #f))
  150. (catch 'system-error
  151. (lambda ()
  152. (let ((pid/status (waitpid (cdr port/pid) WNOHANG)))
  153. (cond ((= (car pid/status) 0)
  154. ;; not ready for collection
  155. (pipe-guardian (car port/pid))
  156. (hashq-set! port/pid-table
  157. (car port/pid) (cdr port/pid))))))
  158. (lambda args #f)))
  159. (define (close-pipe p)
  160. "Closes the pipe created by @code{open-pipe}, then waits for the process
  161. to terminate and returns its status value, @xref{Processes, waitpid}, for
  162. information on how to interpret this value."
  163. (let ((pid (fetch-pid p)))
  164. (if (not pid)
  165. (error "close-pipe: pipe not in table"))
  166. (close-process (cons p pid))))
  167. (define reap-pipes
  168. (lambda ()
  169. (let loop ((p (pipe-guardian)))
  170. (cond (p
  171. ;; maybe removed already by close-pipe.
  172. (let ((pid (fetch-pid p)))
  173. (if pid
  174. (close-process-quietly (cons p pid))))
  175. (loop (pipe-guardian)))))))
  176. (add-hook! after-gc-hook reap-pipes)
  177. (define (open-input-pipe command)
  178. "Equivalent to @code{open-pipe} with mode @code{OPEN_READ}"
  179. (open-pipe command OPEN_READ))
  180. (define (open-output-pipe command)
  181. "Equivalent to @code{open-pipe} with mode @code{OPEN_WRITE}"
  182. (open-pipe command OPEN_WRITE))
  183. (define (open-input-output-pipe command)
  184. "Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}"
  185. (open-pipe command OPEN_BOTH))