shim_ipc_child.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_ipc_child.c
  15. *
  16. * This file contains functions and callbacks to handle IPC between parent
  17. * processes and their children.
  18. */
  19. #include <shim_internal.h>
  20. #include <shim_thread.h>
  21. #include <shim_handle.h>
  22. #include <shim_ipc.h>
  23. #include <shim_utils.h>
  24. #include <shim_profile.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. struct thread_info {
  29. IDTYPE vmid;
  30. unsigned int exitcode;
  31. unsigned int term_signal;
  32. };
  33. /* walk_simple_thread_list callback; exit each simple thread of child process vmid. */
  34. static int child_sthread_exit(struct shim_simple_thread* thread, void* arg, bool* unlocked) {
  35. __UNUSED(unlocked); /* FYI: notifies about unlocked thread_list_lock */
  36. struct thread_info* info = (struct thread_info *) arg;
  37. int found_exiting_thread = 0;
  38. lock(&thread->lock);
  39. if (thread->vmid == info->vmid) {
  40. found_exiting_thread = 1;
  41. if (thread->is_alive) {
  42. thread->exit_code = -info->exitcode;
  43. thread->term_signal = info->term_signal;
  44. thread->is_alive = false;
  45. /* arrange exit event for subsequent wait4(thread->tid) */
  46. DkEventSet(thread->exit_event);
  47. }
  48. }
  49. unlock(&thread->lock);
  50. return found_exiting_thread;
  51. }
  52. /* walk_thread_list callback; exit each thread of child process vmid. */
  53. static int child_thread_exit(struct shim_thread* thread, void* arg, bool* unlocked) {
  54. __UNUSED(unlocked); /* FYI: notifies about unlocked thread_list_lock */
  55. struct thread_info* info = (struct thread_info *) arg;
  56. int found_exiting_thread = 0;
  57. lock(&thread->lock);
  58. if (thread->vmid == info->vmid) {
  59. found_exiting_thread = 1;
  60. if (thread->is_alive) {
  61. thread->exit_code = -info->exitcode;
  62. thread->term_signal = info->term_signal;
  63. unlock(&thread->lock);
  64. /* remote thread is "virtually" exited: SIGCHLD is generated for
  65. * the parent thread and exit events are arranged for subsequent
  66. * wait4(). */
  67. thread_exit(thread, false);
  68. goto out;
  69. }
  70. }
  71. unlock(&thread->lock);
  72. out:
  73. return found_exiting_thread;
  74. }
  75. /* IPC helper thread invokes this fini function when main IPC port for
  76. * communication with child process is disconnected/removed by host OS.
  77. *
  78. * Similarly to benign case of receiving an explicit IPC_CLD_EXIT message
  79. * from exiting remote thread (see ipc_cld_exit_callback()), we want to
  80. * delete all remote threads associated with disconnected child process.
  81. */
  82. void ipc_port_with_child_fini(struct shim_ipc_port* port, IDTYPE vmid, unsigned int exitcode) {
  83. __UNUSED(port);
  84. /* NOTE: IPC port may be closed by host OS because the child process
  85. * exited on host OS (and so host OS closed all its sockets).
  86. * This may happen before arrival of the "expected" IPC_CLD_EXIT
  87. * message from child process. Ideally, we would inspect whether
  88. * we previously sent SIGINT/SIGTERM/SIGKILL to this child and
  89. * use the corresponding termination signal. For now, we simply
  90. * report that child process was killed by SIGKILL. */
  91. struct thread_info info = { .vmid = vmid, .exitcode = exitcode, .term_signal = SIGKILL };
  92. /* message cannot come from our own threads (from ourselves as process) */
  93. assert(vmid != cur_process.vmid);
  94. int ret;
  95. int exited_threads_cnt = 0;
  96. if ((ret = walk_thread_list(&child_thread_exit, &info)) > 0)
  97. exited_threads_cnt += ret;
  98. if ((ret = walk_simple_thread_list(&child_sthread_exit, &info)) > 0)
  99. exited_threads_cnt += ret;
  100. debug("Child process %u got disconnected: assuming that child exited and "
  101. "forcing %d of its threads to exit\n", vmid & 0xFFFF, exited_threads_cnt);
  102. }
  103. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_turnaround, ipc);
  104. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_send, ipc);
  105. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_callback, ipc);
  106. /* The exiting thread of this process calls this function to broadcast
  107. * IPC_CLD_EXIT notification to its parent process (technically, to all
  108. * processes of type DIRPRT or DIRCLD but the only interesting case is
  109. * the notification of parent). */
  110. int ipc_cld_exit_send(IDTYPE ppid, IDTYPE tid, unsigned int exitcode, unsigned int term_signal) {
  111. __attribute__((unused)) unsigned long send_time = GET_PROFILE_INTERVAL();
  112. BEGIN_PROFILE_INTERVAL_SET(send_time);
  113. size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_cld_exit));
  114. struct shim_ipc_msg* msg = __alloca(total_msg_size);
  115. init_ipc_msg(msg, IPC_CLD_EXIT, total_msg_size, 0);
  116. struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit *) &msg->msg;
  117. msgin->ppid = ppid;
  118. msgin->tid = tid;
  119. msgin->exitcode = exitcode;
  120. msgin->term_signal = term_signal;
  121. #ifdef PROFILE
  122. msgin->time = send_time;
  123. #endif
  124. debug("IPC broadcast: IPC_CLD_EXIT(%u, %u, %d, %u)\n",
  125. ppid, tid, exitcode, term_signal);
  126. int ret = broadcast_ipc(msg, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD, /*exclude_port=*/NULL);
  127. SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
  128. return ret;
  129. }
  130. /* IPC helper thread invokes this callback on an IPC_CLD_EXIT message received
  131. * from a specific thread msgin->tid of the exiting child process with vmid
  132. * msg->src. The thread of the exiting child process informs about its exit
  133. * code in msgin->exit_code and its terminating signal in msgin->term_signal.
  134. *
  135. * The callback finds this remote thread of the child process among our
  136. * process's threads/simple threads (recall that parent process maintains
  137. * remote child threads in its thread list, marking them as in_vm == false).
  138. * The remote thread is "virtually" exited: SIGCHLD is generated for the
  139. * parent thread and exit events are arranged for subsequent wait4().
  140. */
  141. int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
  142. __UNUSED(port);
  143. int ret = 0;
  144. struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit *) &msg->msg;
  145. #ifdef PROFILE
  146. unsigned long time = msgin->time;
  147. if (!time)
  148. time = GET_PROFILE_INTERVAL();
  149. #endif
  150. BEGIN_PROFILE_INTERVAL_SET(time);
  151. SAVE_PROFILE_INTERVAL(ipc_cld_exit_turnaround);
  152. debug("IPC callback from %u: IPC_CLD_EXIT(%u, %u, %d, %u)\n",
  153. msg->src & 0xFFFF, msgin->ppid, msgin->tid, msgin->exitcode, msgin->term_signal);
  154. /* message cannot come from our own threads (from ourselves as process) */
  155. assert(msg->src != cur_process.vmid);
  156. /* First try to find remote thread which sent this message among normal
  157. * threads. In the common case, we (as parent process) keep remote child
  158. * threads in the thread list. But sometimes the message can arrive twice
  159. * or very late, such that the corresponding remote thread was already
  160. * exited and deleted; in such cases, we fall back to simple threads. */
  161. struct shim_thread* thread = lookup_thread(msgin->tid);
  162. if (thread) {
  163. lock(&thread->lock);
  164. thread->exit_code = -msgin->exitcode;
  165. thread->term_signal = msgin->term_signal;
  166. #ifdef PROFILE
  167. thread->exit_time = time;
  168. #endif
  169. unlock(&thread->lock);
  170. /* Remote thread is "virtually" exited: SIGCHLD is generated for the
  171. * parent thread and exit events are arranged for subsequent wait4(). */
  172. ret = thread_exit(thread, /*send_ipc=*/false);
  173. put_thread(thread);
  174. } else {
  175. /* Uncommon case: remote child thread was already exited and deleted
  176. * (probably because the same message was already received earlier).
  177. * Find or create a simple thread for a sole purpose of arranging
  178. * exit events for subsequent wait4(). */
  179. struct shim_simple_thread* sthread = lookup_simple_thread(msgin->tid);
  180. if (!sthread) {
  181. sthread = get_new_simple_thread();
  182. sthread->vmid = msg->src;
  183. sthread->tid = msgin->tid;
  184. add_simple_thread(sthread);
  185. }
  186. lock(&sthread->lock);
  187. sthread->is_alive = false;
  188. sthread->exit_code = -msgin->exitcode;
  189. sthread->term_signal = msgin->term_signal;
  190. #ifdef PROFILE
  191. sthread->exit_time = time;
  192. #endif
  193. unlock(&sthread->lock);
  194. DkEventSet(sthread->exit_event); /* for wait4(msgin->tid) */
  195. put_simple_thread(sthread);
  196. }
  197. SAVE_PROFILE_INTERVAL(ipc_cld_exit_callback);
  198. return ret;
  199. }
  200. DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
  201. #ifdef PROFILE
  202. int ipc_cld_profile_send(void) {
  203. struct shim_ipc_port* port = NULL;
  204. IDTYPE dest = (IDTYPE)-1;
  205. /* port and dest are initialized to parent process */
  206. lock(&cur_process.lock);
  207. if (cur_process.parent && (port = cur_process.parent->port)) {
  208. get_ipc_port(port);
  209. dest = cur_process.parent->vmid;
  210. }
  211. unlock(&cur_process.lock);
  212. if (!port || (dest == (IDTYPE)-1))
  213. return -ESRCH;
  214. unsigned long time = GET_PROFILE_INTERVAL();
  215. size_t nsending = 0;
  216. for (size_t i = 0; i < N_PROFILE; i++)
  217. switch (PROFILES[i].type) {
  218. case OCCURENCE:
  219. if (atomic_read(&PROFILES[i].val.occurence.count))
  220. nsending++;
  221. break;
  222. case INTERVAL:
  223. if (atomic_read(&PROFILES[i].val.interval.count))
  224. nsending++;
  225. break;
  226. case CATEGORY:
  227. break;
  228. }
  229. size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_cld_profile) +
  230. sizeof(struct profile_val) * nsending);
  231. struct shim_ipc_msg* msg = __alloca(total_msg_size);
  232. init_ipc_msg(msg, IPC_CLD_PROFILE, total_msg_size, dest);
  233. struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile *)&msg->msg;
  234. size_t nsent = 0;
  235. for (size_t i = 0; i < N_PROFILE && nsent < nsending; i++)
  236. switch (PROFILES[i].type) {
  237. case OCCURENCE: {
  238. unsigned long count = atomic_read(&PROFILES[i].val.occurence.count);
  239. if (count) {
  240. msgin->profile[nsent].idx = i + 1;
  241. msgin->profile[nsent].val.occurence.count = count;
  242. debug("Send %s: %lu times\n", PROFILES[i].name, count);
  243. nsent++;
  244. }
  245. break;
  246. }
  247. case INTERVAL: {
  248. unsigned long count = atomic_read(&PROFILES[i].val.interval.count);
  249. if (count) {
  250. msgin->profile[nsent].idx = i + 1;
  251. msgin->profile[nsent].val.interval.count = count;
  252. msgin->profile[nsent].val.interval.time =
  253. atomic_read(&PROFILES[i].val.interval.time);
  254. debug("Send %s: %lu times, %lu msec\n", PROFILES[i].name,
  255. count, msgin->profile[nsent].val.interval.time);
  256. nsent++;
  257. }
  258. break;
  259. }
  260. case CATEGORY:
  261. break;
  262. }
  263. msgin->time = time;
  264. msgin->nprofile = nsent;
  265. debug("IPC send to %u: IPC_CLD_PROFILE\n", dest & 0xFFFF);
  266. int ret = send_ipc_message(msg, port);
  267. put_ipc_port(port);
  268. return ret;
  269. }
  270. int ipc_cld_profile_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
  271. debug("IPC callback from %u: IPC_CLD_PROFILE\n", msg->src & 0xFFFF);
  272. struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile *)&msg->msg;
  273. for (int i = 0; i < msgin->nprofile; i++) {
  274. int idx = msgin->profile[i].idx;
  275. if (idx == 0)
  276. break;
  277. idx--;
  278. switch (PROFILES[idx].type) {
  279. case OCCURENCE:
  280. debug("Receive %s: %u times\n", PROFILES[idx].name,
  281. msgin->profile[i].val.occurence.count);
  282. atomic_add(msgin->profile[i].val.occurence.count,
  283. &PROFILES[idx].val.occurence.count);
  284. break;
  285. case INTERVAL:
  286. debug("Receive %s: %u times, %lu msec\n", PROFILES[idx].name,
  287. msgin->profile[i].val.interval.count,
  288. msgin->profile[i].val.interval.time);
  289. atomic_add(msgin->profile[i].val.interval.count,
  290. &PROFILES[idx].val.interval.count);
  291. atomic_add(msgin->profile[i].val.interval.time,
  292. &PROFILES[idx].val.interval.time);
  293. break;
  294. case CATEGORY:
  295. break;
  296. }
  297. }
  298. SAVE_PROFILE_INTERVAL_SINCE(ipc_send_profile, msgin->time);
  299. return 0;
  300. }
  301. #endif