pipe.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "fspr_arch_file_io.h"
  18. #include "fspr_file_io.h"
  19. #include "fspr_general.h"
  20. #include "fspr_lib.h"
  21. #include "fspr_strings.h"
  22. #include "fspr_portable.h"
  23. #include <string.h>
  24. #include <process.h>
  25. APR_DECLARE(fspr_status_t) fspr_file_pipe_create(fspr_file_t **in, fspr_file_t **out, fspr_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) = (fspr_file_t *)fspr_palloc(pool, sizeof(fspr_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 = fspr_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. fspr_pool_cleanup_register(pool, *in, fspr_file_cleanup, fspr_pool_cleanup_null);
  75. (*out) = (fspr_file_t *)fspr_palloc(pool, sizeof(fspr_file_t));
  76. (*out)->pool = pool;
  77. (*out)->filedes = filedes[1];
  78. (*out)->fname = fspr_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. fspr_pool_cleanup_register(pool, *out, fspr_file_cleanup, fspr_pool_cleanup_null);
  86. return APR_SUCCESS;
  87. }
  88. APR_DECLARE(fspr_status_t) fspr_file_namedpipe_create(const char *filename, fspr_fileperms_t perm, fspr_pool_t *pool)
  89. {
  90. /* Not yet implemented, interface not suitable */
  91. return APR_ENOTIMPL;
  92. }
  93. APR_DECLARE(fspr_status_t) fspr_file_pipe_timeout_set(fspr_file_t *thepipe, fspr_interval_time_t timeout)
  94. {
  95. if (thepipe->pipe == 1) {
  96. thepipe->timeout = timeout;
  97. if (thepipe->timeout >= 0) {
  98. if (thepipe->blocking != BLK_OFF) {
  99. thepipe->blocking = BLK_OFF;
  100. return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_NOWAIT));
  101. }
  102. }
  103. else if (thepipe->timeout == -1) {
  104. if (thepipe->blocking != BLK_ON) {
  105. thepipe->blocking = BLK_ON;
  106. return APR_FROM_OS_ERROR(DosSetNPHState(thepipe->filedes, NP_WAIT));
  107. }
  108. }
  109. }
  110. return APR_EINVAL;
  111. }
  112. APR_DECLARE(fspr_status_t) fspr_file_pipe_timeout_get(fspr_file_t *thepipe, fspr_interval_time_t *timeout)
  113. {
  114. if (thepipe->pipe == 1) {
  115. *timeout = thepipe->timeout;
  116. return APR_SUCCESS;
  117. }
  118. return APR_EINVAL;
  119. }
  120. APR_DECLARE(fspr_status_t) fspr_os_pipe_put_ex(fspr_file_t **file,
  121. fspr_os_file_t *thefile,
  122. int register_cleanup,
  123. fspr_pool_t *pool)
  124. {
  125. (*file) = fspr_pcalloc(pool, sizeof(fspr_file_t));
  126. (*file)->pool = pool;
  127. (*file)->isopen = TRUE;
  128. (*file)->pipe = 1;
  129. (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
  130. (*file)->timeout = -1;
  131. (*file)->filedes = *thefile;
  132. if (register_cleanup) {
  133. fspr_pool_cleanup_register(pool, *file, fspr_file_cleanup,
  134. fspr_pool_cleanup_null);
  135. }
  136. return APR_SUCCESS;
  137. }
  138. APR_DECLARE(fspr_status_t) fspr_os_pipe_put(fspr_file_t **file,
  139. fspr_os_file_t *thefile,
  140. fspr_pool_t *pool)
  141. {
  142. return fspr_os_pipe_put_ex(file, thefile, 0, pool);
  143. }