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 <errno.h>
  20. #include <pal.h>
  21. #include <pal_error.h>
  22. #include <shim_handle.h>
  23. #include <shim_internal.h>
  24. #include <shim_ipc.h>
  25. #include <shim_profile.h>
  26. #include <shim_thread.h>
  27. #include <shim_utils.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(
  101. "Child process %u got disconnected: assuming that child exited and "
  102. "forcing %d of its threads to exit\n",
  103. vmid & 0xFFFF, exited_threads_cnt);
  104. }
  105. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_turnaround, ipc);
  106. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_send, ipc);
  107. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_callback, ipc);
  108. /* The exiting thread of this process calls this function to broadcast
  109. * IPC_CLD_EXIT notification to its parent process (technically, to all
  110. * processes of type DIRPRT or DIRCLD but the only interesting case is
  111. * the notification of parent). */
  112. int ipc_cld_exit_send(IDTYPE ppid, IDTYPE tid, unsigned int exitcode, unsigned int term_signal) {
  113. __attribute__((unused)) unsigned long send_time = GET_PROFILE_INTERVAL();
  114. BEGIN_PROFILE_INTERVAL_SET(send_time);
  115. size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_cld_exit));
  116. struct shim_ipc_msg* msg = __alloca(total_msg_size);
  117. init_ipc_msg(msg, IPC_CLD_EXIT, total_msg_size, 0);
  118. struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit*)&msg->msg;
  119. msgin->ppid = ppid;
  120. msgin->tid = tid;
  121. msgin->exitcode = exitcode;
  122. msgin->term_signal = term_signal;
  123. #ifdef PROFILE
  124. msgin->time = send_time;
  125. #endif
  126. debug("IPC broadcast: IPC_CLD_EXIT(%u, %u, %d, %u)\n", ppid, tid, exitcode, term_signal);
  127. int ret = broadcast_ipc(msg, IPC_PORT_DIRPRT | IPC_PORT_DIRCLD,
  128. /*exclude_port=*/NULL);
  129. SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
  130. return ret;
  131. }
  132. /* IPC helper thread invokes this callback on an IPC_CLD_EXIT message received
  133. * from a specific thread msgin->tid of the exiting child process with vmid
  134. * msg->src. The thread of the exiting child process informs about its exit
  135. * code in msgin->exit_code and its terminating signal in msgin->term_signal.
  136. *
  137. * The callback finds this remote thread of the child process among our
  138. * process's threads/simple threads (recall that parent process maintains
  139. * remote child threads in its thread list, marking them as in_vm == false).
  140. * The remote thread is "virtually" exited: SIGCHLD is generated for the
  141. * parent thread and exit events are arranged for subsequent wait4().
  142. */
  143. int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
  144. __UNUSED(port);
  145. int ret = 0;
  146. struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit*)&msg->msg;
  147. #ifdef PROFILE
  148. unsigned long time = msgin->time;
  149. if (!time)
  150. time = GET_PROFILE_INTERVAL();
  151. #endif
  152. BEGIN_PROFILE_INTERVAL_SET(time);
  153. SAVE_PROFILE_INTERVAL(ipc_cld_exit_turnaround);
  154. debug("IPC callback from %u: IPC_CLD_EXIT(%u, %u, %d, %u)\n", msg->src & 0xFFFF, msgin->ppid,
  155. msgin->tid, msgin->exitcode, msgin->term_signal);
  156. /* message cannot come from our own threads (from ourselves as process) */
  157. assert(msg->src != cur_process.vmid);
  158. /* First try to find remote thread which sent this message among normal
  159. * threads. In the common case, we (as parent process) keep remote child
  160. * threads in the thread list. But sometimes the message can arrive twice
  161. * or very late, such that the corresponding remote thread was already
  162. * exited and deleted; in such cases, we fall back to simple threads. */
  163. struct shim_thread* thread = lookup_thread(msgin->tid);
  164. if (thread) {
  165. lock(&thread->lock);
  166. thread->exit_code = -msgin->exitcode;
  167. thread->term_signal = msgin->term_signal;
  168. #ifdef PROFILE
  169. thread->exit_time = time;
  170. #endif
  171. unlock(&thread->lock);
  172. /* Remote thread is "virtually" exited: SIGCHLD is generated for the
  173. * parent thread and exit events are arranged for subsequent wait4(). */
  174. ret = thread_exit(thread, /*send_ipc=*/false);
  175. put_thread(thread);
  176. } else {
  177. /* Uncommon case: remote child thread was already exited and deleted
  178. * (probably because the same message was already received earlier).
  179. * Find or create a simple thread for a sole purpose of arranging
  180. * exit events for subsequent wait4(). */
  181. struct shim_simple_thread* sthread = lookup_simple_thread(msgin->tid);
  182. if (!sthread) {
  183. sthread = get_new_simple_thread();
  184. sthread->vmid = msg->src;
  185. sthread->tid = msgin->tid;
  186. add_simple_thread(sthread);
  187. }
  188. lock(&sthread->lock);
  189. sthread->is_alive = false;
  190. sthread->exit_code = -msgin->exitcode;
  191. sthread->term_signal = msgin->term_signal;
  192. #ifdef PROFILE
  193. sthread->exit_time = time;
  194. #endif
  195. unlock(&sthread->lock);
  196. DkEventSet(sthread->exit_event); /* for wait4(msgin->tid) */
  197. put_simple_thread(sthread);
  198. }
  199. SAVE_PROFILE_INTERVAL(ipc_cld_exit_callback);
  200. return ret;
  201. }
  202. DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
  203. #ifdef PROFILE
  204. int ipc_cld_profile_send(void) {
  205. struct shim_ipc_port* port = NULL;
  206. IDTYPE dest = (IDTYPE)-1;
  207. /* port and dest are initialized to parent process */
  208. lock(&cur_process.lock);
  209. if (cur_process.parent && (port = cur_process.parent->port)) {
  210. get_ipc_port(port);
  211. dest = cur_process.parent->vmid;
  212. }
  213. unlock(&cur_process.lock);
  214. if (!port || (dest == (IDTYPE)-1))
  215. return -ESRCH;
  216. unsigned long time = GET_PROFILE_INTERVAL();
  217. size_t nsending = 0;
  218. for (size_t i = 0; i < N_PROFILE; i++) {
  219. switch (PROFILES[i].type) {
  220. case OCCURENCE:
  221. if (atomic_read(&PROFILES[i].val.occurence.count))
  222. nsending++;
  223. break;
  224. case INTERVAL:
  225. if (atomic_read(&PROFILES[i].val.interval.count))
  226. nsending++;
  227. break;
  228. case CATEGORY:
  229. break;
  230. }
  231. }
  232. size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_cld_profile) +
  233. sizeof(struct profile_val) * nsending);
  234. struct shim_ipc_msg* msg = __alloca(total_msg_size);
  235. init_ipc_msg(msg, IPC_CLD_PROFILE, total_msg_size, dest);
  236. struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile*)&msg->msg;
  237. size_t nsent = 0;
  238. for (size_t i = 0; i < N_PROFILE && nsent < nsending; i++) {
  239. switch (PROFILES[i].type) {
  240. case OCCURENCE: {
  241. unsigned long count = atomic_read(&PROFILES[i].val.occurence.count);
  242. if (count) {
  243. msgin->profile[nsent].idx = i + 1;
  244. msgin->profile[nsent].val.occurence.count = count;
  245. debug("Send %s: %lu times\n", PROFILES[i].name, count);
  246. nsent++;
  247. }
  248. break;
  249. }
  250. case INTERVAL: {
  251. unsigned long count = atomic_read(&PROFILES[i].val.interval.count);
  252. if (count) {
  253. msgin->profile[nsent].idx = i + 1;
  254. msgin->profile[nsent].val.interval.count = count;
  255. msgin->profile[nsent].val.interval.time =
  256. atomic_read(&PROFILES[i].val.interval.time);
  257. debug("Send %s: %lu times, %lu msec\n", PROFILES[i].name, count,
  258. msgin->profile[nsent].val.interval.time);
  259. nsent++;
  260. }
  261. break;
  262. }
  263. case CATEGORY:
  264. break;
  265. }
  266. }
  267. msgin->time = time;
  268. msgin->nprofile = nsent;
  269. debug("IPC send to %u: IPC_CLD_PROFILE\n", dest & 0xFFFF);
  270. int ret = send_ipc_message(msg, port);
  271. put_ipc_port(port);
  272. return ret;
  273. }
  274. int ipc_cld_profile_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
  275. debug("IPC callback from %u: IPC_CLD_PROFILE\n", msg->src & 0xFFFF);
  276. struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile*)&msg->msg;
  277. for (int i = 0; i < msgin->nprofile; i++) {
  278. int idx = msgin->profile[i].idx;
  279. if (idx == 0)
  280. break;
  281. idx--;
  282. switch (PROFILES[idx].type) {
  283. case OCCURENCE:
  284. debug("Receive %s: %u times\n", PROFILES[idx].name,
  285. msgin->profile[i].val.occurence.count);
  286. atomic_add(msgin->profile[i].val.occurence.count,
  287. &PROFILES[idx].val.occurence.count);
  288. break;
  289. case INTERVAL:
  290. debug("Receive %s: %u times, %lu msec\n", PROFILES[idx].name,
  291. msgin->profile[i].val.interval.count, msgin->profile[i].val.interval.time);
  292. atomic_add(msgin->profile[i].val.interval.count, &PROFILES[idx].val.interval.count);
  293. atomic_add(msgin->profile[i].val.interval.time, &PROFILES[idx].val.interval.time);
  294. break;
  295. case CATEGORY:
  296. break;
  297. }
  298. }
  299. SAVE_PROFILE_INTERVAL_SINCE(ipc_send_profile, msgin->time);
  300. return 0;
  301. }
  302. #endif