Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aux_children.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Auxiliary children support implementation.
  3. *
  4. * Copyright (c) 2017 The strace developers.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "defs.h"
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include "list.h"
  33. #include "aux_children.h"
  34. /* We store children as a linked list since there is not a lot of them and in
  35. * order to ease removal */
  36. struct aux_child {
  37. pid_t pid;
  38. const struct aux_child_handlers *handlers;
  39. void *signal_fn_data;
  40. void *exit_notify_fn_data;
  41. void *exit_wait_fn_data;
  42. struct list_item list;
  43. };
  44. static EMPTY_LIST(children);
  45. void
  46. register_aux_child_ex(pid_t pid, const struct aux_child_handlers *h,
  47. void *signal_fn_data, void *exit_notify_fn_data,
  48. void *exit_wait_fn_data)
  49. {
  50. static const struct aux_child_handlers default_handlers;
  51. struct aux_child *child;
  52. list_foreach(child, &children, list) {
  53. if (child->pid == pid)
  54. error_func_msg_and_die("Duplicate auxiliary child pid");
  55. }
  56. child = xzalloc(sizeof(*child));
  57. child->pid = pid;
  58. child->handlers = h ?: &default_handlers;
  59. child->signal_fn_data = signal_fn_data;
  60. child->exit_notify_fn_data = exit_notify_fn_data;
  61. child->exit_wait_fn_data = exit_wait_fn_data;
  62. list_append(&children, &child->list);
  63. }
  64. void
  65. remove_aux_child(pid_t pid)
  66. {
  67. struct aux_child *child;
  68. list_foreach(child, &children, list) {
  69. if (child->pid == pid) {
  70. list_remove(&child->list);
  71. free(child);
  72. }
  73. }
  74. }
  75. bool
  76. have_aux_children(void)
  77. {
  78. return !list_is_empty(&children);
  79. }
  80. enum aux_child_sig
  81. aux_children_signal(pid_t pid, int status)
  82. {
  83. struct aux_child *child;
  84. list_foreach(child, &children, list) {
  85. if (child->pid == pid) {
  86. aux_child_signal_fn sig_handler =
  87. child->handlers->signal_fn;
  88. if (!sig_handler)
  89. sig_handler = aux_child_sig_handler;
  90. return sig_handler(pid, status, child->signal_fn_data);
  91. }
  92. }
  93. return ACS_NONE;
  94. }
  95. void
  96. aux_children_exit_notify(int exit_code)
  97. {
  98. struct aux_child *cur;
  99. struct aux_child *tmp;
  100. list_foreach_safe(cur, &children, list, tmp) {
  101. if (cur->handlers->exit_notify_fn)
  102. cur->handlers->exit_notify_fn(cur->pid, exit_code,
  103. cur->exit_notify_fn_data);
  104. }
  105. }
  106. int
  107. aux_children_exit_wait(int exit_code)
  108. {
  109. int cnt = 0;
  110. struct aux_child *cur;
  111. struct aux_child *tmp;
  112. list_foreach_safe(cur, &children, list, tmp) {
  113. aux_child_exit_fn wait_handler = cur->handlers->exit_wait_fn;
  114. if (!wait_handler)
  115. wait_handler = aux_child_wait_handler;
  116. if (wait_handler(cur->pid, exit_code, cur->exit_wait_fn_data)
  117. == ACR_REMOVE_ME) {
  118. list_remove(&cur->list);
  119. free(cur);
  120. } else {
  121. cnt++;
  122. }
  123. }
  124. return cnt;
  125. }
  126. enum aux_child_sig
  127. aux_child_sig_handler(pid_t pid, int status, void *data)
  128. {
  129. if (!WIFSTOPPED(status))
  130. remove_aux_child(pid);
  131. return ACS_CONTINUE;
  132. }
  133. enum aux_child_ret
  134. aux_child_wait_handler(pid_t pid, int exit_code, void *data)
  135. {
  136. while (waitpid(pid, NULL, 0) < 0 && errno == EINTR)
  137. ;
  138. return ACR_REMOVE_ME;
  139. }