shim_ipc_helper.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_ipc_helper.c
  17. *
  18. * This file contains codes to create a IPC helper thread inside library OS
  19. * and maintain bookkeeping of IPC ports.
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_utils.h>
  23. #include <shim_thread.h>
  24. #include <shim_handle.h>
  25. #include <shim_ipc.h>
  26. #include <shim_checkpoint.h>
  27. #include <shim_profile.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <linux_list.h>
  31. #define PORT_MGR_ALLOC 32
  32. #define PAGE_SIZE allocsize
  33. #define OBJ_TYPE struct shim_ipc_port
  34. #include "memmgr.h"
  35. static MEM_MGR port_mgr;
  36. static LIST_HEAD(pobj_list);
  37. #define PID_HASH_LEN 6
  38. #define PID_HASH_NUM (1 << PID_HASH_LEN)
  39. #define PID_HASH_MASK (PID_HASH_NUM - 1)
  40. #define PID_HASH(pid) ((pid) & PID_HASH_MASK)
  41. static struct hlist_head ipc_port_pool [PID_HASH_NUM];
  42. enum {
  43. HELPER_UNINITIALIZED, HELPER_DELAYED, HELPER_NOTALIVE,
  44. HELPER_ALIVE, HELPER_HANDEDOVER,
  45. };
  46. static struct shim_atomic ipc_helper_state;
  47. static struct shim_thread * ipc_helper_thread;
  48. static bool ipc_helper_update;
  49. static AEVENTTYPE ipc_helper_event;
  50. #define IN_HELPER() \
  51. (ipc_helper_thread && ipc_helper_thread == get_cur_thread())
  52. static LOCKTYPE ipc_helper_lock;
  53. static struct shim_ipc_port * broadcast_port;
  54. //#define DEBUG_REF
  55. static int init_ipc_port (struct shim_ipc_info * info, PAL_HANDLE hdl, int type)
  56. {
  57. if (!info)
  58. return 0;
  59. if (info->pal_handle == IPC_FORCE_RECONNECT) {
  60. info->pal_handle = NULL;
  61. if (!hdl && !qstrempty(&info->uri)) {
  62. debug("try reconnect port %s\n", qstrgetstr(&info->uri));
  63. hdl = DkStreamOpen(qstrgetstr(&info->uri),
  64. 0, 0, 0, 0);
  65. if (!hdl)
  66. return -PAL_ERRNO;
  67. }
  68. info->pal_handle = hdl;
  69. }
  70. if (!info->pal_handle)
  71. info->pal_handle = hdl;
  72. if (info->pal_handle)
  73. add_ipc_port_by_id(info->vmid == cur_process.vmid ? 0 : info->vmid,
  74. info->pal_handle, type, NULL, &info->port);
  75. return 0;
  76. }
  77. static void ipc_broadcast_exit (struct shim_ipc_port * port, IDTYPE vmid,
  78. unsigned exitcode)
  79. {
  80. if (port == broadcast_port) {
  81. master_lock();
  82. broadcast_port = NULL;
  83. put_ipc_port(port);
  84. master_unlock();
  85. }
  86. }
  87. int init_ipc_ports (void)
  88. {
  89. int ret = 0;
  90. if (!(port_mgr = create_mem_mgr(init_align_up(PORT_MGR_ALLOC))))
  91. return -ENOMEM;
  92. if ((ret = init_ipc_port(cur_process.self, NULL, IPC_PORT_SERVER)) < 0)
  93. return ret;
  94. if ((ret = init_ipc_port(cur_process.parent, PAL_CB(parent_process),
  95. IPC_PORT_DIRPRT|IPC_PORT_LISTEN)) < 0)
  96. return ret;
  97. if ((ret = init_ipc_port(cur_process.ns[PID_NS], NULL,
  98. IPC_PORT_PIDLDR|IPC_PORT_LISTEN)) < 0)
  99. return ret;
  100. if ((ret = init_ipc_port(cur_process.ns[SYSV_NS], NULL,
  101. IPC_PORT_SYSVLDR|IPC_PORT_LISTEN)) < 0)
  102. return ret;
  103. if (PAL_CB(broadcast_stream))
  104. add_ipc_port_by_id(0, PAL_CB(broadcast_stream), IPC_PORT_LISTEN,
  105. &ipc_broadcast_exit, &broadcast_port);
  106. return 0;
  107. }
  108. int init_ipc_helper (void)
  109. {
  110. bool need_helper = (atomic_read(&ipc_helper_state) == HELPER_DELAYED);
  111. atomic_set(&ipc_helper_state, HELPER_NOTALIVE);
  112. create_lock(ipc_helper_lock);
  113. create_event(&ipc_helper_event);
  114. if (need_helper)
  115. create_ipc_helper();
  116. return 0;
  117. }
  118. static void __get_ipc_port (struct shim_ipc_port * pobj)
  119. {
  120. #ifdef DEBUG_REF
  121. int ref_count = REF_INC(pobj->ref_count);
  122. debug("get ipc_port %p (handle %p, ref_count = %d)\n", pobj,
  123. pobj->pal_handle, ref_count);
  124. #else
  125. REF_INC(pobj->ref_count);
  126. #endif
  127. }
  128. static void __put_ipc_port (struct shim_ipc_port * pobj)
  129. {
  130. int ref_count = REF_DEC(pobj->ref_count);
  131. #ifdef DEBUG_REF
  132. debug("put ipc port %p (handle %p, ref_count = %d)\n", pobj,
  133. pobj->pal_handle, ref_count);
  134. #endif
  135. if (!ref_count) {
  136. if (pobj->pal_handle) {
  137. DkObjectClose(pobj->pal_handle);
  138. pobj->pal_handle = NULL;
  139. }
  140. free_mem_obj_to_mgr(port_mgr, pobj);
  141. }
  142. }
  143. static inline void restart_ipc_helper (bool need_create)
  144. {
  145. switch (atomic_read(&ipc_helper_state)) {
  146. case HELPER_UNINITIALIZED:
  147. atomic_set(&ipc_helper_state, HELPER_DELAYED);
  148. case HELPER_DELAYED:
  149. return;
  150. case HELPER_NOTALIVE:
  151. if (need_create)
  152. create_ipc_helper();
  153. return;
  154. case HELPER_ALIVE:
  155. if (IN_HELPER()) {
  156. ipc_helper_update = true;
  157. return;
  158. }
  159. debug("set ipc helper restart\n");
  160. set_event(&ipc_helper_event, 1);
  161. return;
  162. case HELPER_HANDEDOVER:
  163. ipc_helper_update = true;
  164. return;
  165. }
  166. }
  167. static bool __add_ipc_port (struct shim_ipc_port * port, IDTYPE vmid,
  168. int type, port_fini fini)
  169. {
  170. bool need_restart = false;
  171. assert(vmid != cur_process.vmid);
  172. if (vmid && !port->info.vmid) {
  173. port->info.vmid = vmid;
  174. port->update = true;
  175. }
  176. if (port->info.vmid && hlist_unhashed(&port->hlist)) {
  177. struct hlist_head * head = &ipc_port_pool[PID_HASH(vmid)];
  178. __get_ipc_port(port);
  179. hlist_add_head(&port->hlist, head);
  180. }
  181. if (!(port->info.type & IPC_PORT_IFPOLL) && (type & IPC_PORT_IFPOLL))
  182. need_restart = true;
  183. if ((port->info.type & type) != type) {
  184. port->info.type |= type;
  185. port->update = true;
  186. }
  187. if (fini && (type & ~IPC_PORT_IFPOLL)) {
  188. port_fini * cb = port->fini;
  189. for ( ; cb < port->fini + MAX_IPC_PORT_FINI_CB ; cb++)
  190. if (!*cb || *cb == fini)
  191. break;
  192. assert(cb < port->fini + MAX_IPC_PORT_FINI_CB);
  193. *cb = fini;
  194. }
  195. if (need_restart) {
  196. if (list_empty(&port->list)) {
  197. __get_ipc_port(port);
  198. list_add(&port->list, &pobj_list);
  199. port->recent = true;
  200. } else {
  201. if (!port->recent) {
  202. list_del_init(&port->list);
  203. list_add(&port->list, &pobj_list);
  204. port->recent = true;
  205. }
  206. }
  207. return true;
  208. } else {
  209. if (list_empty(&port->list)) {
  210. __get_ipc_port(port);
  211. list_add_tail(&port->list, &pobj_list);
  212. }
  213. return false;
  214. }
  215. }
  216. void add_ipc_port (struct shim_ipc_port * port, IDTYPE vmid, int type,
  217. port_fini fini)
  218. {
  219. debug("adding port %p (handle %p) for process %u (type=%04x)\n",
  220. port, port->pal_handle, port->info.vmid, type);
  221. lock(ipc_helper_lock);
  222. bool need_restart = __add_ipc_port(port, vmid, type, fini);
  223. unlock(ipc_helper_lock);
  224. if (need_restart)
  225. restart_ipc_helper(true);
  226. }
  227. static struct shim_ipc_port * __get_new_ipc_port (PAL_HANDLE hdl)
  228. {
  229. struct shim_ipc_port * port =
  230. get_mem_obj_from_mgr_enlarge(port_mgr,
  231. size_align_up(PORT_MGR_ALLOC));
  232. if (!port)
  233. return NULL;
  234. memset(port, 0, sizeof(struct shim_ipc_port));
  235. port->pal_handle = hdl;
  236. port->update = true;
  237. INIT_HLIST_NODE(&port->hlist);
  238. INIT_LIST_HEAD(&port->list);
  239. INIT_LIST_HEAD(&port->msgs);
  240. REF_SET(port->ref_count, 1);
  241. create_lock(port->msgs_lock);
  242. return port;
  243. }
  244. void add_ipc_port_by_id (IDTYPE vmid, PAL_HANDLE hdl, int type,
  245. port_fini fini, struct shim_ipc_port ** portptr)
  246. {
  247. debug("adding port (handle %p) for process %u (type %04x)\n",
  248. hdl, vmid, type);
  249. lock(ipc_helper_lock);
  250. struct hlist_head * head = vmid ? &ipc_port_pool[PID_HASH(vmid)] : NULL;
  251. struct shim_ipc_port * tmp, * port = NULL;
  252. struct hlist_node * pos;
  253. if (vmid)
  254. hlist_for_each_entry(tmp, pos, head, hlist)
  255. if (tmp->info.vmid == vmid && tmp->pal_handle == hdl) {
  256. port = tmp;
  257. __get_ipc_port(port);
  258. break;
  259. }
  260. if (!port)
  261. list_for_each_entry(tmp, &pobj_list, list)
  262. if (tmp->pal_handle == hdl) {
  263. port = tmp;
  264. __get_ipc_port(port);
  265. break;
  266. }
  267. if (!port && !(port = __get_new_ipc_port(hdl))) {
  268. *portptr = NULL;
  269. return;
  270. }
  271. bool need_restart = __add_ipc_port(port, vmid, type, fini);
  272. if (portptr)
  273. *portptr = port;
  274. else
  275. __put_ipc_port(port);
  276. unlock(ipc_helper_lock);
  277. if (need_restart)
  278. restart_ipc_helper(true);
  279. }
  280. static bool __del_ipc_port (struct shim_ipc_port * port, int type)
  281. {
  282. debug("deleting port %p (handle %p) for process %u\n",
  283. port, port->pal_handle, port->info.vmid);
  284. bool need_restart = false;
  285. type = type ? (type & port->info.type) : port->info.type;
  286. if ((type & IPC_PORT_KEEPALIVE) ^
  287. (port->info.type & IPC_PORT_KEEPALIVE))
  288. need_restart = true;
  289. /* if the port still have other usage, we will not remove the port */
  290. if (port->info.type & ~(type|IPC_PORT_IFPOLL|IPC_PORT_KEEPALIVE)) {
  291. debug("masking port %p (handle %p): type %x->%x\n",
  292. port, port->pal_handle, port->info.type, port->info.type & ~type);
  293. port->info.type &= ~type;
  294. goto out;
  295. }
  296. if (port->info.type & IPC_PORT_IFPOLL)
  297. need_restart = true;
  298. if (!list_empty(&port->list)) {
  299. list_del_init(&port->list);
  300. port->info.type &= IPC_PORT_IFPOLL;
  301. __put_ipc_port(port);
  302. }
  303. if (!hlist_unhashed(&port->hlist)) {
  304. hlist_del_init(&port->hlist);
  305. __put_ipc_port(port);
  306. }
  307. out:
  308. port->update = true;
  309. return need_restart;
  310. }
  311. void del_ipc_port (struct shim_ipc_port * port, int type)
  312. {
  313. lock(ipc_helper_lock);
  314. bool need_restart = __del_ipc_port(port, type);
  315. unlock(ipc_helper_lock);
  316. if (need_restart)
  317. restart_ipc_helper(false);
  318. }
  319. void del_ipc_port_by_id (IDTYPE vmid, int type)
  320. {
  321. struct hlist_head * head = &ipc_port_pool[PID_HASH(vmid)];
  322. struct shim_ipc_port * port;
  323. struct hlist_node * pos, * n;
  324. bool need_restart = false;
  325. lock(ipc_helper_lock);
  326. hlist_for_each_entry_safe(port, pos, n, head, hlist) {
  327. debug("port %p (handle %p) for process %u in list %p\n",
  328. port, port->pal_handle, port->info.vmid, head);
  329. if (port->info.vmid == vmid) {
  330. if (__del_ipc_port(port, type))
  331. need_restart = true;
  332. }
  333. }
  334. unlock(ipc_helper_lock);
  335. if (need_restart)
  336. restart_ipc_helper(false);
  337. }
  338. void del_ipc_port_fini (struct shim_ipc_port * port, unsigned int exitcode)
  339. {
  340. port_fini fini[MAX_IPC_PORT_FINI_CB];
  341. int nfini = 0;
  342. assert(REF_GET(port->ref_count) > 0);
  343. lock(ipc_helper_lock);
  344. IDTYPE vmid = port->info.vmid;
  345. for (int i = 0 ; i < MAX_IPC_PORT_FINI_CB ; i++)
  346. if (port->fini[i]) {
  347. fini[nfini++] = port->fini[i];
  348. port->fini[i] = NULL;
  349. }
  350. __get_ipc_port(port);
  351. bool need_restart = __del_ipc_port(port, 0);
  352. unlock(ipc_helper_lock);
  353. if (nfini) {
  354. for (int i = 0 ; i < nfini ; i++)
  355. (fini[i])(port, vmid, exitcode);
  356. }
  357. lock(port->msgs_lock);
  358. if (!list_empty(&port->list)) {
  359. struct shim_ipc_msg_obj * msg, * n;
  360. list_for_each_entry_safe(msg, n, &port->msgs, list) {
  361. list_del_init(&msg->list);
  362. msg->retval = -ECONNRESET;
  363. if (msg->thread)
  364. thread_wakeup(msg->thread);
  365. }
  366. }
  367. unlock(port->msgs_lock);
  368. put_ipc_port(port);
  369. assert(REF_GET(port->ref_count) > 0);
  370. if (need_restart)
  371. restart_ipc_helper(false);
  372. }
  373. static struct shim_ipc_port * __lookup_ipc_port (IDTYPE vmid, int type)
  374. {
  375. struct hlist_head * head = &ipc_port_pool[PID_HASH(vmid)];
  376. struct shim_ipc_port * tmp;
  377. struct hlist_node * pos;
  378. hlist_for_each_entry(tmp, pos, head, hlist)
  379. if (tmp->info.vmid == vmid && (!type || tmp->info.type & type)) {
  380. debug("found port %p (handle %p) for process %u (type %04x)\n",
  381. tmp, tmp->pal_handle, tmp->info.vmid, tmp->info.type);
  382. __get_ipc_port(tmp);
  383. return tmp;
  384. }
  385. return NULL;
  386. }
  387. struct shim_ipc_port * lookup_ipc_port (IDTYPE vmid, int type)
  388. {
  389. lock(ipc_helper_lock);
  390. struct shim_ipc_port * port = __lookup_ipc_port(vmid, type);
  391. unlock(ipc_helper_lock);
  392. return port;
  393. }
  394. void get_ipc_port (struct shim_ipc_port * port)
  395. {
  396. __get_ipc_port(port);
  397. }
  398. void put_ipc_port (struct shim_ipc_port * port)
  399. {
  400. __put_ipc_port(port);
  401. }
  402. void del_all_ipc_ports (int type)
  403. {
  404. struct shim_ipc_port * pobj, * n;
  405. bool need_restart = false;
  406. lock(ipc_helper_lock);
  407. list_for_each_entry_safe(pobj, n, &pobj_list, list)
  408. if (pobj->pal_handle && __del_ipc_port(pobj, type))
  409. need_restart = true;
  410. unlock(ipc_helper_lock);
  411. if (need_restart)
  412. restart_ipc_helper(false);
  413. }
  414. int broadcast_ipc (struct shim_ipc_msg * msg, struct shim_ipc_port ** exclude,
  415. int exsize, int target_type)
  416. {
  417. struct shim_ipc_port ** exend = exclude + exsize, ** ex;
  418. struct shim_ipc_port * pobj, * n;
  419. if (!target_type && broadcast_port) {
  420. for (ex = exclude ; ex < exend && *ex != broadcast_port ; ex++);
  421. if (ex != exend)
  422. return 0;
  423. debug("send to broadcast stream\n");
  424. get_ipc_port(broadcast_port);
  425. int ret = send_ipc_message(msg, broadcast_port);
  426. put_ipc_port(broadcast_port);
  427. if (!ret)
  428. return 0;
  429. }
  430. lock(ipc_helper_lock);
  431. list_for_each_entry_safe(pobj, n, &pobj_list, list) {
  432. debug("found port %p (handle %p) for process %u (type %04x)\n",
  433. pobj, pobj->pal_handle, pobj->info.vmid, pobj->info.type);
  434. if (pobj->info.type & target_type) {
  435. debug("broadcast to port %p (handle %p) for process %u "
  436. "(type %x, target %x)\n",
  437. pobj, pobj->pal_handle, pobj->info.vmid,
  438. pobj->info.type, target_type);
  439. if (exsize) {
  440. for (ex = exclude ; ex < exend && *ex != pobj ; ex++);
  441. if (ex != exend)
  442. continue;
  443. }
  444. msg->dst = pobj->info.vmid;
  445. /* has to be assigned, so shim_send_ipc_message will not try
  446. to grab ipc_helper_lock */
  447. send_ipc_message(msg, pobj);
  448. }
  449. }
  450. unlock(ipc_helper_lock);
  451. return 0;
  452. }
  453. static int ipc_resp_callback (IPC_CALLBACK_ARGS)
  454. {
  455. struct shim_ipc_resp * msgin = (struct shim_ipc_resp *) &msg->msg;
  456. debug("ipc callback from %u: IPC_RESP(%d)\n", msg->src, msgin->retval);
  457. if (!msg->seq)
  458. return msgin->retval;
  459. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  460. if (obj) {
  461. obj->retval = msgin->retval;
  462. if (obj->thread)
  463. thread_wakeup(obj->thread);
  464. return 0;
  465. }
  466. return msgin->retval;
  467. }
  468. static ipc_callback ipc_callbacks [IPC_CODE_NUM] = {
  469. /* RESP */ &ipc_resp_callback,
  470. /* FINDURI */ &ipc_finduri_callback,
  471. /* TELLURI */ &ipc_telluri_callback,
  472. /* CHECKPOINT */ &ipc_checkpoint_callback,
  473. /* parents and children */
  474. /* CLD_EXIT */ &ipc_cld_exit_callback,
  475. /* CLD_JOIN */ &ipc_cld_join_callback,
  476. #ifdef PROFILE
  477. /* CLD_PROFILE */ &ipc_cld_profile_callback,
  478. #endif
  479. /* pid namespace */
  480. IPC_NS_CALLBACKS(pid)
  481. /* PID_KILL */ &ipc_pid_kill_callback,
  482. /* PID_GETSTATUS */ &ipc_pid_getstatus_callback,
  483. /* PID_RETSTATUS */ &ipc_pid_retstatus_callback,
  484. /* PID_GETMETA */ &ipc_pid_getmeta_callback,
  485. /* PID_RETMETA */ &ipc_pid_retmeta_callback,
  486. /* PID_NOP */ &ipc_pid_nop_callback,
  487. /* PID_SENDRPC */ &ipc_pid_sendrpc_callback,
  488. /* sysv namespace */
  489. IPC_NS_CALLBACKS(sysv)
  490. IPC_NS_KEY_CALLBACKS(sysv)
  491. /* SYSV_DELRES */ &ipc_sysv_delres_callback,
  492. /* SYSV_MOVRES */ &ipc_sysv_movres_callback,
  493. /* SYSV_MSGSND */ &ipc_sysv_msgsnd_callback,
  494. /* SYSV_MSGRCV */ &ipc_sysv_msgrcv_callback,
  495. /* SYSV_MSGMOV */ &ipc_sysv_msgmov_callback,
  496. /* SYSV_SEMOP */ &ipc_sysv_semop_callback,
  497. /* SYSV_SEMCTL */ &ipc_sysv_semctl_callback,
  498. /* SYSV_SEMRET */ &ipc_sysv_semret_callback,
  499. /* SYSV_SEMMOV */ &ipc_sysv_semmov_callback,
  500. };
  501. int __response_ipc_message (struct shim_ipc_port * port, IDTYPE dest,
  502. int ret, unsigned long seq)
  503. {
  504. struct shim_ipc_msg * resp = create_ipc_resp_msg_on_stack(ret, dest, seq);
  505. ret = (ret == RESPONSE_CALLBACK) ? 0 : ret;
  506. debug("ipc send to %u: IPC_RESP(%d)\n", resp->dst, ret);
  507. struct shim_ipc_resp * msgin = (struct shim_ipc_resp *) &resp->msg;
  508. msgin->retval = ret;
  509. return send_ipc_message(resp, port);
  510. }
  511. /* not only ipc helper thread can receive messsage, anyone can
  512. receive message if they have acquired (locked) the port */
  513. int receive_ipc_message (struct shim_ipc_port * port, unsigned long seq,
  514. struct shim_ipc_msg ** msgptr)
  515. {
  516. int readahead = IPC_MSG_READAHEAD;
  517. int bufsize = IPC_MSG_MINIMAL_SIZE + readahead;
  518. struct shim_ipc_msg * msg = __alloca(bufsize);
  519. int expected_size;
  520. int bytes = 0, ret = 0;
  521. get_ipc_port(port);
  522. do {
  523. expected_size = IPC_MSG_MINIMAL_SIZE;
  524. while (bytes < expected_size) {
  525. retry_read:
  526. if (expected_size + readahead > bufsize) {
  527. while (expected_size + readahead > bufsize)
  528. bufsize *= 2;
  529. void * new_buff = __alloca(bufsize);
  530. memcpy(new_buff, msg, bytes);
  531. msg = new_buff;
  532. }
  533. if (!(ret = DkStreamRead(port->pal_handle, 0,
  534. expected_size - bytes + readahead,
  535. (void *) msg + bytes, NULL, 0)))
  536. break;
  537. bytes += ret;
  538. }
  539. if (!bytes) {
  540. if (PAL_NATIVE_ERRNO) {
  541. debug("port %p (handle %p) is removed at reading\n",
  542. port, port->pal_handle);
  543. del_ipc_port_fini(port, -ECHILD);
  544. ret = -PAL_ERRNO;
  545. }
  546. break;
  547. }
  548. debug("receive a message from port %p (handle %p): "
  549. "code=%d size=%d src=%u dst=%u seq=%lx\n",
  550. port, port->pal_handle,
  551. msg->code, msg->size, msg->src, msg->dst, msg->seq);
  552. expected_size = msg->size;
  553. if (bytes < expected_size)
  554. goto retry_read;
  555. if (msgptr && (!seq || msg->seq == seq)) {
  556. struct shim_ipc_msg * retmsg;
  557. if (*msgptr) {
  558. if (msg->size > (*msgptr)->size)
  559. msg->size = (*msgptr)->size;
  560. retmsg = *msgptr;
  561. } else {
  562. *msgptr = retmsg = malloc(msg->size);
  563. }
  564. memcpy(retmsg, msg, msg->size);
  565. return 0;
  566. }
  567. /* skip if the message comes from myself (it's possible because
  568. of the broadcast channel */
  569. if (msg->src == cur_process.vmid)
  570. goto next;
  571. ipc_callback callback = ipc_callbacks[msg->code];
  572. if (callback) {
  573. ret = (*callback) (msg, port);
  574. if ((ret < 0 || ret == RESPONSE_CALLBACK) && msg->seq)
  575. /* only helper thread sends back response */
  576. ret = __response_ipc_message(port, msg->src, ret, msg->seq);
  577. }
  578. next:
  579. if ((bytes -= expected_size) > 0)
  580. memmove(msg, (void *) msg + expected_size, bytes);
  581. } while (bytes > 0 || (seq && msg->seq != seq));
  582. if (msgptr)
  583. *msgptr = NULL;
  584. put_ipc_port(port);
  585. return ret;
  586. }
  587. #define IPC_HELPER_STACK_SIZE (allocsize * 4)
  588. #define IPC_HELPER_LIST_INIT_SIZE 32
  589. static void shim_ipc_helper (void * arg)
  590. {
  591. /* set ipc helper thread */
  592. struct shim_thread * self = (struct shim_thread *) arg;
  593. if (!arg)
  594. return;
  595. __libc_tcb_t tcb;
  596. allocate_tls(&tcb, self);
  597. debug_setbuf(&tcb.shim_tcb, true);
  598. lock(ipc_helper_lock);
  599. bool notme = (self != ipc_helper_thread);
  600. unlock(ipc_helper_lock);
  601. if (notme) {
  602. put_thread(self);
  603. DkThreadExit();
  604. return;
  605. }
  606. debug("ipc helper thread started\n");
  607. void * stack = allocate_stack(IPC_HELPER_STACK_SIZE, allocsize, false);
  608. if (!stack)
  609. goto end;
  610. self->stack_top = stack + IPC_HELPER_STACK_SIZE;
  611. self->stack = stack;
  612. switch_stack(stack + IPC_HELPER_STACK_SIZE);
  613. self = get_cur_thread();
  614. stack = self->stack;
  615. int port_num = 0, port_size = IPC_HELPER_LIST_INIT_SIZE;
  616. struct shim_ipc_port ** local_pobjs = stack, * pobj;
  617. PAL_HANDLE * local_ports;
  618. PAL_HANDLE ipc_event_handle = event_handle(&ipc_helper_event);
  619. int nalive = 0;
  620. PAL_HANDLE polled = NULL;
  621. int count = -1;
  622. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  623. local_ports[0] = ipc_event_handle;
  624. goto update_status;
  625. while (atomic_read(&ipc_helper_state) == HELPER_ALIVE ||
  626. nalive) {
  627. /* do a global poll on all the ports */
  628. polled = DkObjectsWaitAny(port_num + 1, local_ports, NO_TIMEOUT);
  629. if (!polled)
  630. continue;
  631. /* before we locking pobj list, at least we can look at the returned
  632. port if it is the ipc helper event */
  633. if (polled == ipc_event_handle) {
  634. clear_event(&ipc_helper_event);
  635. update_status:
  636. if (atomic_read(&ipc_helper_state) == HELPER_NOTALIVE)
  637. goto end;
  638. else
  639. goto update_list;
  640. }
  641. pobj = NULL;
  642. count = -1;
  643. for (int i = 0 ; i < port_num ; i++)
  644. if (polled == local_pobjs[i]->pal_handle) {
  645. pobj = local_pobjs[i];
  646. count = i;
  647. break;
  648. }
  649. if (!pobj)
  650. continue;
  651. /* if the polled port is a server port, accept a client and add it
  652. to the port list */
  653. if (pobj->private.type & IPC_PORT_SERVER) {
  654. PAL_HANDLE cli = DkStreamWaitForClient(polled);
  655. if (cli) {
  656. int type = (pobj->private.type & ~IPC_PORT_SERVER) |
  657. IPC_PORT_LISTEN;
  658. add_ipc_port_by_id(pobj->private.vmid, cli, type,
  659. NULL, NULL);
  660. } else {
  661. debug("port %p (handle %p) is removed at accepting\n",
  662. pobj, polled);
  663. del_ipc_port_fini(pobj, -ECHILD);
  664. }
  665. polled = NULL;
  666. count = -1;
  667. goto update_list;
  668. }
  669. PAL_STREAM_ATTR attr;
  670. if (!DkStreamAttributesQuerybyHandle(polled, &attr)) {
  671. debug("port %p (handle %p) is removed at querying\n",
  672. pobj, polled);
  673. del_ipc_port_fini(pobj, -PAL_ERRNO);
  674. goto update_list;
  675. }
  676. if (attr.readable)
  677. receive_ipc_message(pobj, 0, NULL);
  678. if (attr.disconnected) {
  679. debug("port %p (handle %p) is disconnected\n",
  680. pobj, polled);
  681. del_ipc_port_fini(pobj, -ECONNRESET);
  682. goto update_list;
  683. }
  684. if (!ipc_helper_update)
  685. continue;
  686. update_list:
  687. ipc_helper_update = false;
  688. lock(ipc_helper_lock);
  689. int compact = 0;
  690. /* first walk though all the polling ports and remove the one
  691. being deleted. */
  692. for (int i = 0 ; i < port_num ; i++) {
  693. struct shim_ipc_port * pobj = local_pobjs[i];
  694. if (list_empty(&pobj->list)) {
  695. if (polled == pobj->pal_handle) {
  696. polled = NULL;
  697. count = -1;
  698. }
  699. local_pobjs[i] = NULL;
  700. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  701. nalive--;
  702. __put_ipc_port(pobj);
  703. compact++;
  704. continue;
  705. }
  706. if (pobj->update) {
  707. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  708. if (!(pobj->private.type & IPC_PORT_KEEPALIVE))
  709. nalive--;
  710. } else {
  711. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  712. nalive++;
  713. }
  714. pobj->private = pobj->info;
  715. pobj->update = false;
  716. }
  717. if (compact) {
  718. if (polled == pobj->pal_handle)
  719. count -= compact;
  720. local_pobjs[i - compact] = pobj;
  721. local_ports[i - compact + 1] = pobj->pal_handle;
  722. }
  723. }
  724. port_num -= compact;
  725. list_for_each_entry(pobj, &pobj_list, list) {
  726. /* we only update among recently updated ports */
  727. if (!pobj->recent)
  728. break;
  729. if (pobj->update) {
  730. pobj->private = pobj->info;
  731. pobj->update = false;
  732. }
  733. assert(pobj->private.type & IPC_PORT_IFPOLL);
  734. if (port_num == port_size) {
  735. port_size *= 2;
  736. memmove(local_pobjs + port_size,
  737. local_ports,
  738. (port_num + 1) * sizeof(PAL_HANDLE));
  739. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  740. }
  741. pobj->recent = false;
  742. __get_ipc_port(pobj);
  743. local_pobjs[port_num] = pobj;
  744. local_ports[port_num + 1] = pobj->pal_handle;
  745. port_num++;
  746. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  747. nalive++;
  748. debug("listen to process %u on port %p (handle %p, type %04x)\n",
  749. pobj->private.vmid,
  750. pobj,
  751. pobj->pal_handle,
  752. pobj->private.type);
  753. }
  754. unlock(ipc_helper_lock);
  755. }
  756. for (int i = 0 ; i < port_num ; i++) {
  757. struct shim_ipc_port * pobj = local_pobjs[i];
  758. __put_ipc_port(pobj);
  759. }
  760. end:
  761. /* DP: Put our handle map reference */
  762. if (self->handle_map)
  763. put_handle_map(self->handle_map);
  764. if (atomic_read(&ipc_helper_state) == HELPER_HANDEDOVER) {
  765. debug("ipc helper thread is the last thread, process exiting\n");
  766. shim_clean();
  767. }
  768. atomic_xchg(&ipc_helper_state, HELPER_NOTALIVE);
  769. lock(ipc_helper_lock);
  770. ipc_helper_thread = NULL;
  771. unlock(ipc_helper_lock);
  772. put_thread(self);
  773. debug("ipc helper thread terminated\n");
  774. DkThreadExit();
  775. }
  776. int create_ipc_helper (void)
  777. {
  778. int ret = 0;
  779. if (atomic_read(&ipc_helper_state) == HELPER_ALIVE)
  780. return 0;
  781. /*
  782. * we are enabling multi-threading, must turn on threading
  783. * before grabbing any lock
  784. */
  785. enable_locking();
  786. struct shim_thread * new = get_new_internal_thread();
  787. if (!new)
  788. return -ENOMEM;
  789. lock(ipc_helper_lock);
  790. if (atomic_read(&ipc_helper_state) == HELPER_ALIVE) {
  791. unlock(ipc_helper_lock);
  792. put_thread(new);
  793. return 0;
  794. }
  795. ipc_helper_thread = new;
  796. atomic_xchg(&ipc_helper_state, HELPER_ALIVE);
  797. unlock(ipc_helper_lock);
  798. PAL_HANDLE handle = thread_create(shim_ipc_helper, new, 0);
  799. if (!handle) {
  800. ret = -PAL_ERRNO;
  801. lock(ipc_helper_lock);
  802. ipc_helper_thread = NULL;
  803. atomic_xchg(&ipc_helper_state, HELPER_NOTALIVE);
  804. unlock(ipc_helper_lock);
  805. put_thread(new);
  806. return ret;
  807. }
  808. new->pal_handle = handle;
  809. return 0;
  810. }
  811. int exit_with_ipc_helper (bool handover)
  812. {
  813. if (IN_HELPER() || atomic_read(&ipc_helper_state) != HELPER_ALIVE)
  814. return 0;
  815. lock(ipc_helper_lock);
  816. if (handover) {
  817. handover = false;
  818. struct shim_ipc_port * pobj;
  819. list_for_each_entry(pobj, &pobj_list, list)
  820. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  821. handover = true;
  822. break;
  823. }
  824. }
  825. unlock(ipc_helper_lock);
  826. int new_state = HELPER_NOTALIVE;
  827. if (handover) {
  828. debug("handing over to ipc helper\n");
  829. new_state = HELPER_HANDEDOVER;
  830. } else {
  831. debug("exiting ipc helper\n");
  832. }
  833. atomic_xchg(&ipc_helper_state, new_state);
  834. set_event(&ipc_helper_event, 1);
  835. return (new_state == HELPER_NOTALIVE) ? 0 : -EAGAIN;
  836. }
  837. int terminate_ipc_helper (void)
  838. {
  839. lock(ipc_helper_lock);
  840. struct shim_thread * thread = ipc_helper_thread;
  841. if (!thread) {
  842. unlock(ipc_helper_lock);
  843. return -ESRCH;
  844. }
  845. debug("terminating ipc helper\n");
  846. atomic_xchg(&ipc_helper_state, HELPER_NOTALIVE);
  847. set_event(&ipc_helper_event, 1);
  848. unlock(ipc_helper_lock);
  849. return 0;
  850. }