pipe.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <stdio.h>
  17. #include <nks/fsio.h>
  18. #include <nks/errno.h>
  19. #include "apr_arch_file_io.h"
  20. #include "apr_strings.h"
  21. #include "apr_portable.h"
  22. #include "apr_arch_inherit.h"
  23. static apr_status_t pipeblock(apr_file_t *thepipe)
  24. {
  25. #ifdef USE_FLAGS
  26. unsigned long flags;
  27. if (fcntl(thepipe->filedes, F_GETFL, &flags) != -1)
  28. {
  29. flags &= ~FNDELAY;
  30. fcntl(thepipe->filedes, F_SETFL, flags);
  31. }
  32. #else
  33. errno = 0;
  34. fcntl(thepipe->filedes, F_SETFL, 0);
  35. #endif
  36. if (errno)
  37. return errno;
  38. thepipe->blocking = BLK_ON;
  39. return APR_SUCCESS;
  40. }
  41. static apr_status_t pipenonblock(apr_file_t *thepipe)
  42. {
  43. #ifdef USE_FLAGS
  44. unsigned long flags;
  45. errno = 0;
  46. if (fcntl(thepipe->filedes, F_GETFL, &flags) != -1)
  47. {
  48. flags |= FNDELAY;
  49. fcntl(thepipe->filedes, F_SETFL, flags);
  50. }
  51. #else
  52. errno = 0;
  53. fcntl(thepipe->filedes, F_SETFL, FNDELAY);
  54. #endif
  55. if (errno)
  56. return errno;
  57. thepipe->blocking = BLK_OFF;
  58. return APR_SUCCESS;
  59. }
  60. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
  61. {
  62. if (thepipe->is_pipe == 1) {
  63. thepipe->timeout = timeout;
  64. if (timeout >= 0) {
  65. if (thepipe->blocking != BLK_OFF) { /* blocking or unknown state */
  66. return pipenonblock(thepipe);
  67. }
  68. }
  69. else {
  70. if (thepipe->blocking != BLK_ON) { /* non-blocking or unknown state */
  71. return pipeblock(thepipe);
  72. }
  73. }
  74. return APR_SUCCESS;
  75. }
  76. return APR_EINVAL;
  77. }
  78. APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
  79. {
  80. if (thepipe->is_pipe == 1) {
  81. *timeout = thepipe->timeout;
  82. return APR_SUCCESS;
  83. }
  84. return APR_EINVAL;
  85. }
  86. APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
  87. apr_os_file_t *thefile,
  88. int register_cleanup,
  89. apr_pool_t *pool)
  90. {
  91. int *dafile = thefile;
  92. (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
  93. (*file)->pool = pool;
  94. (*file)->eof_hit = 0;
  95. (*file)->is_pipe = 1;
  96. (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
  97. (*file)->timeout = -1;
  98. (*file)->ungetchar = -1; /* no char avail */
  99. (*file)->filedes = *dafile;
  100. if (!register_cleanup) {
  101. (*file)->flags = APR_FOPEN_NOCLEANUP;
  102. }
  103. (*file)->buffered = 0;
  104. #if APR_HAS_THREADS
  105. (*file)->thlock = NULL;
  106. #endif
  107. if (register_cleanup) {
  108. apr_pool_cleanup_register((*file)->pool, (void *)(*file),
  109. apr_unix_file_cleanup,
  110. apr_pool_cleanup_null);
  111. }
  112. return APR_SUCCESS;
  113. }
  114. APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
  115. apr_os_file_t *thefile,
  116. apr_pool_t *pool)
  117. {
  118. return apr_os_pipe_put_ex(file, thefile, 0, pool);
  119. }
  120. APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool)
  121. {
  122. int filedes[2];
  123. if (pipe(filedes) == -1) {
  124. return errno;
  125. }
  126. (*in) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
  127. (*out) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
  128. (*in)->pool =
  129. (*out)->pool = pool;
  130. (*in)->filedes = filedes[0];
  131. (*out)->filedes = filedes[1];
  132. (*in)->flags = APR_INHERIT;
  133. (*out)->flags = APR_INHERIT;
  134. (*in)->is_pipe =
  135. (*out)->is_pipe = 1;
  136. (*out)->fname =
  137. (*in)->fname = NULL;
  138. (*in)->buffered =
  139. (*out)->buffered = 0;
  140. (*in)->blocking =
  141. (*out)->blocking = BLK_ON;
  142. (*in)->timeout =
  143. (*out)->timeout = -1;
  144. (*in)->ungetchar = -1;
  145. (*in)->thlock =
  146. (*out)->thlock = NULL;
  147. (void) apr_pollset_create(&(*in)->pollset, 1, pool, 0);
  148. (void) apr_pollset_create(&(*out)->pollset, 1, pool, 0);
  149. apr_pool_cleanup_register((*in)->pool, (void *)(*in), apr_unix_file_cleanup,
  150. apr_pool_cleanup_null);
  151. apr_pool_cleanup_register((*out)->pool, (void *)(*out), apr_unix_file_cleanup,
  152. apr_pool_cleanup_null);
  153. return APR_SUCCESS;
  154. }
  155. APR_DECLARE(apr_status_t) apr_file_pipe_create_ex(apr_file_t **in,
  156. apr_file_t **out,
  157. apr_int32_t blocking,
  158. apr_pool_t *pool)
  159. {
  160. apr_status_t status;
  161. if ((status = apr_file_pipe_create(in, out, pool)) != APR_SUCCESS)
  162. return status;
  163. switch (blocking) {
  164. case APR_FULL_BLOCK:
  165. break;
  166. case APR_READ_BLOCK:
  167. apr_file_pipe_timeout_set(*out, 0);
  168. break;
  169. case APR_WRITE_BLOCK:
  170. apr_file_pipe_timeout_set(*in, 0);
  171. break;
  172. default:
  173. apr_file_pipe_timeout_set(*out, 0);
  174. apr_file_pipe_timeout_set(*in, 0);
  175. }
  176. return APR_SUCCESS;
  177. }
  178. APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
  179. apr_fileperms_t perm, apr_pool_t *pool)
  180. {
  181. return APR_ENOTIMPL;
  182. }