popen.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;; popen emulation, for non-stdio based ports.
  2. ;;;; Copyright (C) 1998, 1999, 2000, 2001, 2003, 2006, 2010, 2011, 2012 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 3 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 (and (file-port? port)
  37. (not (port-closed? port))
  38. (fileno port))
  39. (open-fdes *null-device* mode)))
  40. ;; run a process connected to an input, an output or an
  41. ;; input/output port
  42. ;; mode: OPEN_READ, OPEN_WRITE or OPEN_BOTH
  43. ;; returns port/pid pair.
  44. (define (open-process mode prog . args)
  45. (let* ((reading (or (equal? mode OPEN_READ)
  46. (equal? mode OPEN_BOTH)))
  47. (writing (or (equal? mode OPEN_WRITE)
  48. (equal? mode OPEN_BOTH)))
  49. (c2p (if reading (pipe) #f)) ; child to parent
  50. (p2c (if writing (pipe) #f))) ; parent to child
  51. (if c2p (setvbuf (cdr c2p) _IONBF))
  52. (if p2c (setvbuf (cdr p2c) _IONBF))
  53. (let ((pid (primitive-fork)))
  54. (cond ((= pid 0)
  55. ;; child
  56. (ensure-batch-mode!)
  57. ;; select the three file descriptors to be used as
  58. ;; standard descriptors 0, 1, 2 for the new
  59. ;; process. They are pipes to/from the parent or taken
  60. ;; from the current Scheme input/output/error ports if
  61. ;; possible.
  62. (let ((input-fdes (if writing
  63. (fileno (car p2c))
  64. (ensure-fdes (current-input-port)
  65. O_RDONLY)))
  66. (output-fdes (if reading
  67. (fileno (cdr c2p))
  68. (ensure-fdes (current-output-port)
  69. O_WRONLY)))
  70. (error-fdes (ensure-fdes (current-error-port)
  71. O_WRONLY)))
  72. ;; close all file descriptors in ports inherited from
  73. ;; the parent except for the three selected above.
  74. ;; this is to avoid causing problems for other pipes in
  75. ;; the parent.
  76. ;; use low-level system calls, not close-port or the
  77. ;; scsh routines, to avoid side-effects such as
  78. ;; flushing port buffers or evicting ports.
  79. (port-for-each (lambda (pt-entry)
  80. (if (and (file-port? pt-entry)
  81. (not (port-closed? pt-entry))
  82. (not (file-port-close-on-exec? pt-entry)))
  83. (let ((pt-fileno (fileno pt-entry)))
  84. (if (not (or (= pt-fileno input-fdes)
  85. (= pt-fileno output-fdes)
  86. (= pt-fileno error-fdes)))
  87. (false-if-exception
  88. (close-fdes pt-fileno)))))))
  89. ;; Copy the three selected descriptors to the standard
  90. ;; descriptors 0, 1, 2, if not already there
  91. (cond ((not (= input-fdes 0))
  92. (if (= output-fdes 0)
  93. (set! output-fdes (dup->fdes 0)))
  94. (if (= error-fdes 0)
  95. (set! error-fdes (dup->fdes 0)))
  96. (dup2 input-fdes 0)
  97. ;; it's possible input-fdes is error-fdes
  98. (if (not (= input-fdes error-fdes))
  99. (close-fdes input-fdes))))
  100. (cond ((not (= output-fdes 1))
  101. (if (= error-fdes 1)
  102. (set! error-fdes (dup->fdes 1)))
  103. (dup2 output-fdes 1)
  104. ;; it's possible output-fdes is error-fdes
  105. (if (not (= output-fdes error-fdes))
  106. (close-fdes output-fdes))))
  107. (cond ((not (= error-fdes 2))
  108. (dup2 error-fdes 2)
  109. (close-fdes error-fdes)))
  110. (apply execlp prog prog args)))
  111. (else
  112. ;; parent
  113. (if c2p (close-port (cdr c2p)))
  114. (if p2c (close-port (car p2c)))
  115. (cons (cond ((not writing) (car c2p))
  116. ((not reading) (cdr p2c))
  117. (else (make-rw-port (car c2p)
  118. (cdr p2c))))
  119. pid))))))
  120. (define (open-pipe* mode command . args)
  121. "Executes the program @var{command} with optional arguments
  122. @var{args} (all strings) in a subprocess.
  123. A port to the process (based on pipes) is created and returned.
  124. @var{mode} specifies whether an input, an output or an input-output
  125. port to the process is created: it should be the value of
  126. @code{OPEN_READ}, @code{OPEN_WRITE} or @code{OPEN_BOTH}."
  127. (let* ((port/pid (apply open-process mode command args))
  128. (port (car port/pid)))
  129. (pipe-guardian port)
  130. (hashq-set! port/pid-table port (cdr port/pid))
  131. port))
  132. (define (open-pipe command mode)
  133. "Executes the shell command @var{command} (a string) in a subprocess.
  134. A port to the process (based on pipes) is created and returned.
  135. @var{mode} specifies whether an input, an output or an input-output
  136. port to the process is created: it should be the value of
  137. @code{OPEN_READ}, @code{OPEN_WRITE} or @code{OPEN_BOTH}."
  138. (open-pipe* mode "/bin/sh" "-c" command))
  139. (define (fetch-pid port)
  140. (let ((pid (hashq-ref port/pid-table port)))
  141. (hashq-remove! port/pid-table port)
  142. pid))
  143. (define (close-process port/pid)
  144. (close-port (car port/pid))
  145. (cdr (waitpid (cdr port/pid))))
  146. ;; for the background cleanup handler: just clean up without reporting
  147. ;; errors. also avoids blocking the process: if the child isn't ready
  148. ;; to be collected, puts it back into the guardian's live list so it
  149. ;; can be tried again the next time the cleanup runs.
  150. (define (close-process-quietly port/pid)
  151. (catch 'system-error
  152. (lambda ()
  153. (close-port (car port/pid)))
  154. (lambda args #f))
  155. (catch 'system-error
  156. (lambda ()
  157. (let ((pid/status (waitpid (cdr port/pid) WNOHANG)))
  158. (cond ((= (car pid/status) 0)
  159. ;; not ready for collection
  160. (pipe-guardian (car port/pid))
  161. (hashq-set! port/pid-table
  162. (car port/pid) (cdr port/pid))))))
  163. (lambda args #f)))
  164. (define (close-pipe p)
  165. "Closes the pipe created by @code{open-pipe}, then waits for the process
  166. to terminate and returns its status value, @xref{Processes, waitpid}, for
  167. information on how to interpret this value."
  168. (let ((pid (fetch-pid p)))
  169. (if (not pid)
  170. (error "close-pipe: pipe not in table"))
  171. (close-process (cons p pid))))
  172. (define reap-pipes
  173. (lambda ()
  174. (let loop ((p (pipe-guardian)))
  175. (cond (p
  176. ;; maybe removed already by close-pipe.
  177. (let ((pid (fetch-pid p)))
  178. (if pid
  179. (close-process-quietly (cons p pid))))
  180. (loop (pipe-guardian)))))))
  181. (add-hook! after-gc-hook reap-pipes)
  182. (define (open-input-pipe command)
  183. "Equivalent to @code{open-pipe} with mode @code{OPEN_READ}"
  184. (open-pipe command OPEN_READ))
  185. (define (open-output-pipe command)
  186. "Equivalent to @code{open-pipe} with mode @code{OPEN_WRITE}"
  187. (open-pipe command OPEN_WRITE))
  188. (define (open-input-output-pipe command)
  189. "Equivalent to @code{open-pipe} with mode @code{OPEN_BOTH}"
  190. (open-pipe command OPEN_BOTH))