shim_ipc_helper.c 30 KB

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