2
0

otherchild.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "apr.h"
  17. #include "apr_arch_misc.h"
  18. #include "apr_arch_threadproc.h"
  19. #include "apr_arch_file_io.h"
  20. #if APR_HAS_OTHER_CHILD
  21. #ifdef HAVE_TIME_H
  22. #include <sys/time.h>
  23. #endif
  24. #ifdef HAVE_SYS_SELECT_H
  25. #include <sys/select.h>
  26. #endif
  27. #if APR_HAVE_SYS_WAIT_H
  28. #include <sys/wait.h>
  29. #endif
  30. #ifdef BEOS
  31. #include <sys/socket.h> /* for fd_set definition! */
  32. #endif
  33. static apr_other_child_rec_t *other_children = NULL;
  34. static apr_status_t other_child_cleanup(void *data)
  35. {
  36. apr_other_child_rec_t **pocr, *nocr;
  37. for (pocr = &other_children; *pocr; pocr = &(*pocr)->next) {
  38. if ((*pocr)->data == data) {
  39. nocr = (*pocr)->next;
  40. (*(*pocr)->maintenance) (APR_OC_REASON_UNREGISTER, (*pocr)->data, -1);
  41. *pocr = nocr;
  42. /* XXX: um, well we've just wasted some space in pconf ? */
  43. return APR_SUCCESS;
  44. }
  45. }
  46. return APR_SUCCESS;
  47. }
  48. APR_DECLARE(void) apr_proc_other_child_register(apr_proc_t *proc,
  49. void (*maintenance) (int reason, void *, int status),
  50. void *data, apr_file_t *write_fd, apr_pool_t *p)
  51. {
  52. apr_other_child_rec_t *ocr;
  53. ocr = apr_palloc(p, sizeof(*ocr));
  54. ocr->p = p;
  55. ocr->proc = proc;
  56. ocr->maintenance = maintenance;
  57. ocr->data = data;
  58. if (write_fd == NULL) {
  59. ocr->write_fd = (apr_os_file_t) -1;
  60. }
  61. else {
  62. #ifdef WIN32
  63. /* This should either go away as part of eliminating apr_proc_probe_writable_fds
  64. * or write_fd should point to an apr_file_t
  65. */
  66. ocr->write_fd = write_fd->filehand;
  67. #else
  68. ocr->write_fd = write_fd->filedes;
  69. #endif
  70. }
  71. ocr->next = other_children;
  72. other_children = ocr;
  73. apr_pool_cleanup_register(p, ocr->data, other_child_cleanup,
  74. apr_pool_cleanup_null);
  75. }
  76. APR_DECLARE(void) apr_proc_other_child_unregister(void *data)
  77. {
  78. apr_other_child_rec_t *cur;
  79. cur = other_children;
  80. while (cur) {
  81. if (cur->data == data) {
  82. break;
  83. }
  84. cur = cur->next;
  85. }
  86. /* segfault if this function called with invalid parm */
  87. if (cur) apr_pool_cleanup_kill(cur->p, cur->data, other_child_cleanup);
  88. other_child_cleanup(data);
  89. }
  90. APR_DECLARE(apr_status_t) apr_proc_other_child_alert(apr_proc_t *proc,
  91. int reason,
  92. int status)
  93. {
  94. apr_other_child_rec_t *ocr, *nocr;
  95. for (ocr = other_children; ocr; ocr = nocr) {
  96. nocr = ocr->next;
  97. if (ocr->proc->pid != proc->pid)
  98. continue;
  99. ocr->proc = NULL;
  100. (*ocr->maintenance) (reason, ocr->data, status);
  101. return APR_SUCCESS;
  102. }
  103. return APR_EPROC_UNKNOWN;
  104. }
  105. APR_DECLARE(void) apr_proc_other_child_refresh(apr_other_child_rec_t *ocr,
  106. int reason)
  107. {
  108. /* Todo:
  109. * Implement code to detect if pipes are still alive.
  110. */
  111. #ifdef WIN32
  112. DWORD status;
  113. if (ocr->proc == NULL)
  114. return;
  115. if (!ocr->proc->hproc) {
  116. /* Already mopped up, perhaps we apr_proc_kill'ed it,
  117. * they should have already unregistered!
  118. */
  119. ocr->proc = NULL;
  120. (*ocr->maintenance) (APR_OC_REASON_LOST, ocr->data, -1);
  121. }
  122. else if (!GetExitCodeProcess(ocr->proc->hproc, &status)) {
  123. CloseHandle(ocr->proc->hproc);
  124. ocr->proc->hproc = NULL;
  125. ocr->proc = NULL;
  126. (*ocr->maintenance) (APR_OC_REASON_LOST, ocr->data, -1);
  127. }
  128. else if (status == STILL_ACTIVE) {
  129. (*ocr->maintenance) (reason, ocr->data, -1);
  130. }
  131. else {
  132. CloseHandle(ocr->proc->hproc);
  133. ocr->proc->hproc = NULL;
  134. ocr->proc = NULL;
  135. (*ocr->maintenance) (APR_OC_REASON_DEATH, ocr->data, status);
  136. }
  137. #else /* ndef Win32 */
  138. pid_t waitret;
  139. int status;
  140. if (ocr->proc == NULL)
  141. return;
  142. waitret = waitpid(ocr->proc->pid, &status, WNOHANG);
  143. if (waitret == ocr->proc->pid) {
  144. ocr->proc = NULL;
  145. (*ocr->maintenance) (APR_OC_REASON_DEATH, ocr->data, status);
  146. }
  147. else if (waitret == 0) {
  148. (*ocr->maintenance) (reason, ocr->data, -1);
  149. }
  150. else if (waitret == -1) {
  151. /* uh what the heck? they didn't call unregister? */
  152. ocr->proc = NULL;
  153. (*ocr->maintenance) (APR_OC_REASON_LOST, ocr->data, -1);
  154. }
  155. #endif
  156. }
  157. APR_DECLARE(void) apr_proc_other_child_refresh_all(int reason)
  158. {
  159. apr_other_child_rec_t *ocr, *next_ocr;
  160. for (ocr = other_children; ocr; ocr = next_ocr) {
  161. next_ocr = ocr->next;
  162. apr_proc_other_child_refresh(ocr, reason);
  163. }
  164. }
  165. #else /* !APR_HAS_OTHER_CHILD */
  166. APR_DECLARE(void) apr_proc_other_child_register(apr_proc_t *proc,
  167. void (*maintenance) (int reason, void *, int status),
  168. void *data, apr_file_t *write_fd, apr_pool_t *p)
  169. {
  170. return;
  171. }
  172. APR_DECLARE(void) apr_proc_other_child_unregister(void *data)
  173. {
  174. return;
  175. }
  176. APR_DECLARE(apr_status_t) apr_proc_other_child_alert(apr_proc_t *proc,
  177. int reason,
  178. int status)
  179. {
  180. return APR_ENOTIMPL;
  181. }
  182. APR_DECLARE(void) apr_proc_other_child_refresh(apr_other_child_rec_t *ocr,
  183. int reason)
  184. {
  185. return;
  186. }
  187. APR_DECLARE(void) apr_proc_other_child_refresh_all(int reason)
  188. {
  189. return;
  190. }
  191. #endif /* APR_HAS_OTHER_CHILD */