pipe.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "win32/apr_arch_file_io.h"
  17. #include "apr_file_io.h"
  18. #include "apr_general.h"
  19. #include "apr_strings.h"
  20. #if APR_HAVE_ERRNO_H
  21. #include <errno.h>
  22. #endif
  23. #include <string.h>
  24. #include <stdio.h>
  25. #if APR_HAVE_SYS_TYPES_H
  26. #include <sys/types.h>
  27. #endif
  28. #if APR_HAVE_SYS_STAT_H
  29. #include <sys/stat.h>
  30. #endif
  31. #include "apr_arch_misc.h"
  32. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
  33. {
  34. /* Always OK to unset timeouts */
  35. if (timeout == -1) {
  36. thepipe->timeout = timeout;
  37. return APR_SUCCESS;
  38. }
  39. if (!thepipe->pipe) {
  40. return APR_ENOTIMPL;
  41. }
  42. if (timeout && !(thepipe->pOverlapped)) {
  43. /* Cannot be nonzero if a pipe was opened blocking
  44. */
  45. return APR_EINVAL;
  46. }
  47. thepipe->timeout = timeout;
  48. return APR_SUCCESS;
  49. }
  50. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
  51. {
  52. /* Always OK to get the timeout (even if it's unset ... -1) */
  53. *timeout = thepipe->timeout;
  54. return APR_SUCCESS;
  55. }
  56. APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *p)
  57. {
  58. /* Unix creates full blocking pipes. */
  59. return apr_create_nt_pipe(in, out, APR_FULL_BLOCK, p);
  60. }
  61. /* apr_create_nt_pipe()
  62. * An internal (for now) APR function used by apr_proc_create()
  63. * when setting up pipes to communicate with the child process.
  64. * apr_create_nt_pipe() allows setting the blocking mode of each end of
  65. * the pipe when the pipe is created (rather than after the pipe is created).
  66. * A pipe handle must be opened in full async i/o mode in order to
  67. * emulate Unix non-blocking pipes with timeouts.
  68. *
  69. * In general, we don't want to enable child side pipe handles for async i/o.
  70. * This prevents us from enabling both ends of the pipe for async i/o in
  71. * apr_file_pipe_create.
  72. *
  73. * Why not use NamedPipes on NT which support setting pipe state to
  74. * non-blocking? On NT, even though you can set a pipe non-blocking,
  75. * there is no clean way to set event driven non-zero timeouts (e.g select(),
  76. * WaitForSinglelObject, et. al. will not detect pipe i/o). On NT, you
  77. * have to poll the pipe to detect i/o on a non-blocking pipe.
  78. */
  79. apr_status_t apr_create_nt_pipe(apr_file_t **in, apr_file_t **out,
  80. apr_int32_t blocking_mode,
  81. apr_pool_t *p)
  82. {
  83. #ifdef _WIN32_WCE
  84. return APR_ENOTIMPL;
  85. #else
  86. SECURITY_ATTRIBUTES sa;
  87. static unsigned long id = 0;
  88. DWORD dwPipeMode;
  89. DWORD dwOpenMode;
  90. char name[50];
  91. sa.nLength = sizeof(sa);
  92. sa.bInheritHandle = TRUE;
  93. sa.lpSecurityDescriptor = NULL;
  94. (*in) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
  95. (*in)->pool = p;
  96. (*in)->fname = NULL;
  97. (*in)->pipe = 1;
  98. (*in)->timeout = -1;
  99. (*in)->ungetchar = -1;
  100. (*in)->eof_hit = 0;
  101. (*in)->filePtr = 0;
  102. (*in)->bufpos = 0;
  103. (*in)->dataRead = 0;
  104. (*in)->direction = 0;
  105. (*in)->pOverlapped = NULL;
  106. (void) apr_pollset_create(&(*in)->pollset, 1, p, 0);
  107. (*out) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
  108. (*out)->pool = p;
  109. (*out)->fname = NULL;
  110. (*out)->pipe = 1;
  111. (*out)->timeout = -1;
  112. (*out)->ungetchar = -1;
  113. (*out)->eof_hit = 0;
  114. (*out)->filePtr = 0;
  115. (*out)->bufpos = 0;
  116. (*out)->dataRead = 0;
  117. (*out)->direction = 0;
  118. (*out)->pOverlapped = NULL;
  119. (void) apr_pollset_create(&(*out)->pollset, 1, p, 0);
  120. if (apr_os_level >= APR_WIN_NT) {
  121. /* Create the read end of the pipe */
  122. dwOpenMode = PIPE_ACCESS_INBOUND;
  123. if (blocking_mode == APR_WRITE_BLOCK /* READ_NONBLOCK */
  124. || blocking_mode == APR_FULL_NONBLOCK) {
  125. dwOpenMode |= FILE_FLAG_OVERLAPPED;
  126. (*in)->pOverlapped = (OVERLAPPED*) apr_pcalloc(p, sizeof(OVERLAPPED));
  127. (*in)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  128. }
  129. dwPipeMode = 0;
  130. sprintf(name, "\\\\.\\pipe\\apr-pipe-%u.%lu", getpid(), id++);
  131. (*in)->filehand = CreateNamedPipe(name,
  132. dwOpenMode,
  133. dwPipeMode,
  134. 1, //nMaxInstances,
  135. 0, //nOutBufferSize,
  136. 65536, //nInBufferSize,
  137. 1, //nDefaultTimeOut,
  138. &sa);
  139. /* Create the write end of the pipe */
  140. dwOpenMode = FILE_ATTRIBUTE_NORMAL;
  141. if (blocking_mode == APR_READ_BLOCK /* WRITE_NONBLOCK */
  142. || blocking_mode == APR_FULL_NONBLOCK) {
  143. dwOpenMode |= FILE_FLAG_OVERLAPPED;
  144. (*out)->pOverlapped = (OVERLAPPED*) apr_pcalloc(p, sizeof(OVERLAPPED));
  145. (*out)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  146. }
  147. (*out)->filehand = CreateFile(name,
  148. GENERIC_WRITE, // access mode
  149. 0, // share mode
  150. &sa, // Security attributes
  151. OPEN_EXISTING, // dwCreationDisposition
  152. dwOpenMode, // Pipe attributes
  153. NULL); // handle to template file
  154. }
  155. else {
  156. /* Pipes on Win9* are blocking. Live with it. */
  157. if (!CreatePipe(&(*in)->filehand, &(*out)->filehand, &sa, 65536)) {
  158. return apr_get_os_error();
  159. }
  160. }
  161. apr_pool_cleanup_register((*in)->pool, (void *)(*in), file_cleanup,
  162. apr_pool_cleanup_null);
  163. apr_pool_cleanup_register((*out)->pool, (void *)(*out), file_cleanup,
  164. apr_pool_cleanup_null);
  165. return APR_SUCCESS;
  166. #endif /* _WIN32_WCE */
  167. }
  168. APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
  169. apr_fileperms_t perm,
  170. apr_pool_t *pool)
  171. {
  172. /* Not yet implemented, interface not suitable.
  173. * Win32 requires the named pipe to be *opened* at the time it's
  174. * created, and to do so, blocking or non blocking must be elected.
  175. */
  176. return APR_ENOTIMPL;
  177. }
  178. /* XXX: Problem; we need to choose between blocking and nonblocking based
  179. * on how *thefile was opened, and we don't have that information :-/
  180. * Hack; assume a blocking socket, since the most common use for the fn
  181. * would be to handle stdio-style or blocking pipes. Win32 doesn't have
  182. * select() blocking for pipes anyways :(
  183. */
  184. APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
  185. apr_os_file_t *thefile,
  186. int register_cleanup,
  187. apr_pool_t *pool)
  188. {
  189. (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
  190. (*file)->pool = pool;
  191. (*file)->pipe = 1;
  192. (*file)->timeout = -1;
  193. (*file)->ungetchar = -1;
  194. (*file)->filehand = *thefile;
  195. (void) apr_pollset_create(&(*file)->pollset, 1, pool, 0);
  196. if (register_cleanup) {
  197. apr_pool_cleanup_register(pool, *file, file_cleanup,
  198. apr_pool_cleanup_null);
  199. }
  200. return APR_SUCCESS;
  201. }
  202. APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
  203. apr_os_file_t *thefile,
  204. apr_pool_t *pool)
  205. {
  206. return apr_os_pipe_put_ex(file, thefile, 0, pool);
  207. }