shim_ipc_helper.c 30 KB

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