shim_ipc_helper.c 30 KB

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