pipe.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "fspr_arch_file_io.h"
  20. #include "fspr_strings.h"
  21. #include "fspr_portable.h"
  22. #include "fspr_arch_inherit.h"
  23. static fspr_status_t pipeblock(fspr_file_t *thepipe)
  24. {
  25. #ifdef USE_FLAGS
  26. int err;
  27. unsigned long flags;
  28. if (fcntl(thepipe->filedes, F_GETFL, &flags) != -1)
  29. {
  30. flags &= ~FNDELAY;
  31. fcntl(thepipe->filedes, F_SETFL, flags);
  32. }
  33. #else
  34. errno = 0;
  35. fcntl(thepipe->filedes, F_SETFL, 0);
  36. #endif
  37. if (errno)
  38. return errno;
  39. thepipe->blocking = BLK_ON;
  40. return APR_SUCCESS;
  41. }
  42. static fspr_status_t pipenonblock(fspr_file_t *thepipe)
  43. {
  44. #ifdef USE_FLAGS
  45. int err;
  46. unsigned long flags;
  47. errno = 0;
  48. if (fcntl(thepipe->filedes, F_GETFL, &flags) != -1)
  49. {
  50. flags |= FNDELAY;
  51. fcntl(thepipe->filedes, F_SETFL, flags);
  52. }
  53. #else
  54. errno = 0;
  55. fcntl(thepipe->filedes, F_SETFL, FNDELAY);
  56. #endif
  57. if (errno)
  58. return errno;
  59. thepipe->blocking = BLK_OFF;
  60. return APR_SUCCESS;
  61. }
  62. APR_DECLARE(fspr_status_t) fspr_file_pipe_timeout_set(fspr_file_t *thepipe, fspr_interval_time_t timeout)
  63. {
  64. if (thepipe->is_pipe == 1) {
  65. thepipe->timeout = timeout;
  66. if (timeout >= 0) {
  67. if (thepipe->blocking != BLK_OFF) { /* blocking or unknown state */
  68. return pipenonblock(thepipe);
  69. }
  70. }
  71. else {
  72. if (thepipe->blocking != BLK_ON) { /* non-blocking or unknown state */
  73. return pipeblock(thepipe);
  74. }
  75. }
  76. return APR_SUCCESS;
  77. }
  78. return APR_EINVAL;
  79. }
  80. APR_DECLARE(fspr_status_t) fspr_file_pipe_timeout_get(fspr_file_t *thepipe, fspr_interval_time_t *timeout)
  81. {
  82. if (thepipe->is_pipe == 1) {
  83. *timeout = thepipe->timeout;
  84. return APR_SUCCESS;
  85. }
  86. return APR_EINVAL;
  87. }
  88. APR_DECLARE(fspr_status_t) fspr_os_pipe_put_ex(fspr_file_t **file,
  89. fspr_os_file_t *thefile,
  90. int register_cleanup,
  91. fspr_pool_t *pool)
  92. {
  93. int *dafile = thefile;
  94. (*file) = fspr_pcalloc(pool, sizeof(fspr_file_t));
  95. (*file)->pool = pool;
  96. (*file)->eof_hit = 0;
  97. (*file)->is_pipe = 1;
  98. (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
  99. (*file)->timeout = -1;
  100. (*file)->ungetchar = -1; /* no char avail */
  101. (*file)->filedes = *dafile;
  102. if (!register_cleanup) {
  103. (*file)->flags = APR_FILE_NOCLEANUP;
  104. }
  105. (*file)->buffered = 0;
  106. #if APR_HAS_THREADS
  107. (*file)->thlock = NULL;
  108. #endif
  109. if (register_cleanup) {
  110. fspr_pool_cleanup_register((*file)->pool, (void *)(*file),
  111. fspr_unix_file_cleanup,
  112. fspr_pool_cleanup_null);
  113. }
  114. return APR_SUCCESS;
  115. }
  116. APR_DECLARE(fspr_status_t) fspr_os_pipe_put(fspr_file_t **file,
  117. fspr_os_file_t *thefile,
  118. fspr_pool_t *pool)
  119. {
  120. return fspr_os_pipe_put_ex(file, thefile, 0, pool);
  121. }
  122. APR_DECLARE(fspr_status_t) fspr_file_pipe_create(fspr_file_t **in, fspr_file_t **out, fspr_pool_t *pool)
  123. {
  124. int filedes[2];
  125. int err;
  126. if (pipe(filedes) == -1) {
  127. return errno;
  128. }
  129. (*in) = (fspr_file_t *)fspr_pcalloc(pool, sizeof(fspr_file_t));
  130. (*out) = (fspr_file_t *)fspr_pcalloc(pool, sizeof(fspr_file_t));
  131. (*in)->pool =
  132. (*out)->pool = pool;
  133. (*in)->filedes = filedes[0];
  134. (*out)->filedes = filedes[1];
  135. (*in)->flags = APR_INHERIT;
  136. (*out)->flags = APR_INHERIT;
  137. (*in)->is_pipe =
  138. (*out)->is_pipe = 1;
  139. (*out)->fname =
  140. (*in)->fname = NULL;
  141. (*in)->buffered =
  142. (*out)->buffered = 0;
  143. (*in)->blocking =
  144. (*out)->blocking = BLK_ON;
  145. (*in)->timeout =
  146. (*out)->timeout = -1;
  147. (*in)->ungetchar = -1;
  148. (*in)->thlock =
  149. (*out)->thlock = NULL;
  150. (void) fspr_pollset_create(&(*in)->pollset, 1, pool, 0);
  151. (void) fspr_pollset_create(&(*out)->pollset, 1, pool, 0);
  152. fspr_pool_cleanup_register((*in)->pool, (void *)(*in), fspr_unix_file_cleanup,
  153. fspr_pool_cleanup_null);
  154. fspr_pool_cleanup_register((*out)->pool, (void *)(*out), fspr_unix_file_cleanup,
  155. fspr_pool_cleanup_null);
  156. return APR_SUCCESS;
  157. }
  158. APR_DECLARE(fspr_status_t) fspr_file_namedpipe_create(const char *filename,
  159. fspr_fileperms_t perm, fspr_pool_t *pool)
  160. {
  161. return APR_ENOTIMPL;
  162. }