pipe.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #define INCL_DOSERRORS
  17. #include "apr_arch_file_io.h"
  18. #include "apr_file_io.h"
  19. #include "apr_general.h"
  20. #include "apr_lib.h"
  21. #include "apr_strings.h"
  22. #include "apr_portable.h"
  23. #include <string.h>
  24. #include <process.h>
  25. APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool)
  26. {
  27. ULONG filedes[2];
  28. ULONG rc, action;
  29. static int id = 0;
  30. char pipename[50];
  31. sprintf(pipename, "/pipe/%d.%d", getpid(), id++);
  32. rc = DosCreateNPipe(pipename, filedes, NP_ACCESS_INBOUND, NP_NOWAIT|1, 4096, 4096, 0);
  33. if (rc)
  34. return APR_FROM_OS_ERROR(rc);
  35. rc = DosConnectNPipe(filedes[0]);
  36. if (rc && rc != ERROR_PIPE_NOT_CONNECTED) {
  37. DosClose(filedes[0]);
  38. return APR_FROM_OS_ERROR(rc);
  39. }
  40. rc = DosOpen (pipename, filedes+1, &action, 0, FILE_NORMAL,
  41. OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  42. OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE,
  43. NULL);
  44. if (rc) {
  45. DosClose(filedes[0]);
  46. return APR_FROM_OS_ERROR(rc);
  47. }
  48. (*in) = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t));
  49. rc = DosCreateEventSem(NULL, &(*in)->pipeSem, DC_SEM_SHARED, FALSE);
  50. if (rc) {
  51. DosClose(filedes[0]);
  52. DosClose(filedes[1]);
  53. return APR_FROM_OS_ERROR(rc);
  54. }
  55. rc = DosSetNPipeSem(filedes[0], (HSEM)(*in)->pipeSem, 1);
  56. if (!rc) {
  57. rc = DosSetNPHState(filedes[0], NP_WAIT);
  58. }
  59. if (rc) {
  60. DosClose(filedes[0]);
  61. DosClose(filedes[1]);
  62. DosCloseEventSem((*in)->pipeSem);
  63. return APR_FROM_OS_ERROR(rc);
  64. }
  65. (*in)->pool = pool;
  66. (*in)->filedes = filedes[0];
  67. (*in)->fname = apr_pstrdup(pool, pipename);
  68. (*in)->isopen = TRUE;
  69. (*in)->buffered = FALSE;
  70. (*in)->flags = 0;
  71. (*in)->pipe = 1;
  72. (*in)->timeout = -1;
  73. (*in)->blocking = BLK_ON;
  74. apr_pool_cleanup_register(pool, *in, apr_file_cleanup, apr_pool_cleanup_null);
  75. (*out) = (apr_file_t *)apr_palloc(pool, sizeof(apr_file_t));
  76. (*out)->pool = pool;
  77. (*out)->filedes = filedes[1];
  78. (*out)->fname = apr_pstrdup(pool, pipename);
  79. (*out)->isopen = TRUE;
  80. (*out)->buffered = FALSE;
  81. (*out)->flags = 0;
  82. (*out)->pipe = 1;
  83. (*out)->timeout = -1;
  84. (*out)->blocking = BLK_ON;
  85. apr_pool_cleanup_register(pool, *out, apr_file_cleanup, apr_pool_cleanup_null);
  86. return APR_SUCCESS;
  87. }
  88. APR_DECLARE(apr_status_t) apr_file_pipe_create_ex(apr_file_t **in,
  89. apr_file_t **out,
  90. apr_int32_t blocking,
  91. apr_pool_t *pool)
  92. {
  93. apr_status_t status;
  94. if ((status = apr_file_pipe_create(in, out, pool)) != APR_SUCCESS)
  95. return status;
  96. switch (blocking) {
  97. case APR_FULL_BLOCK:
  98. break;
  99. case APR_READ_BLOCK:
  100. apr_file_pipe_timeout_set(*out, 0);
  101. break;
  102. case APR_WRITE_BLOCK:
  103. apr_file_pipe_timeout_set(*in, 0);
  104. break;
  105. default:
  106. apr_file_pipe_timeout_set(*out, 0);
  107. apr_file_pipe_timeout_set(*in, 0);
  108. }
  109. return APR_SUCCESS;
  110. }
  111. APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename, apr_fileperms_t perm, apr_pool_t *pool)
  112. {
  113. /* Not yet implemented, interface not suitable */
  114. return APR_ENOTIMPL;
  115. }
  116. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
  117. {
  118. if (thepipe->pipe == 1) {
  119. thepipe->timeout = timeout;
  120. if (thepipe->timeout >= 0) {
  121. if (thepipe->blocking != BLK_OFF) {
  122. thepipe->blocking = BLK_OFF;
  123. return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_NOWAIT));
  124. }
  125. }
  126. else if (thepipe->timeout == -1) {
  127. if (thepipe->blocking != BLK_ON) {
  128. thepipe->blocking = BLK_ON;
  129. return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_WAIT));
  130. }
  131. }
  132. }
  133. return APR_EINVAL;
  134. }
  135. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
  136. {
  137. if (thepipe->pipe == 1) {
  138. *timeout = thepipe->timeout;
  139. return APR_SUCCESS;
  140. }
  141. return APR_EINVAL;
  142. }
  143. APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
  144. apr_os_file_t *thefile,
  145. int register_cleanup,
  146. apr_pool_t *pool)
  147. {
  148. (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
  149. (*file)->pool = pool;
  150. (*file)->isopen = TRUE;
  151. (*file)->pipe = 1;
  152. (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
  153. (*file)->timeout = -1;
  154. (*file)->filedes = *thefile;
  155. if (register_cleanup) {
  156. apr_pool_cleanup_register(pool, *file, apr_file_cleanup,
  157. apr_pool_cleanup_null);
  158. }
  159. return APR_SUCCESS;
  160. }
  161. APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
  162. apr_os_file_t *thefile,
  163. apr_pool_t *pool)
  164. {
  165. return apr_os_pipe_put_ex(file, thefile, 0, pool);
  166. }