shim_ipc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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.c
  17. *
  18. * This file contains codes to maintain generic bookkeeping of IPC.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_ipc.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_unistd.h>
  27. #include <shim_profile.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <linux_list.h>
  31. #define ipc_info_mgr_ALLOC 32
  32. #define PAGE_SIZE allocsize
  33. #define OBJ_TYPE struct shim_ipc_info
  34. #include "memmgr.h"
  35. static MEM_MGR ipc_info_mgr;
  36. LOCKTYPE ipc_info_lock;
  37. struct shim_process cur_process;
  38. DEFINE_PROFILE_CATAGORY(ipc, );
  39. DEFINE_PROFILE_OCCURENCE(syscall_use_ipc, ipc);
  40. //#define DEBUG_REF
  41. int init_ipc_ports (void);
  42. int init_ns_pid (void);
  43. int init_ns_sysv (void);
  44. int init_ipc (void)
  45. {
  46. int ret = 0;
  47. create_lock(ipc_info_lock);
  48. if (!(ipc_info_mgr = create_mem_mgr(init_align_up(ipc_info_mgr_ALLOC))))
  49. return -ENOMEM;
  50. if ((ret = init_ipc_ports()) < 0)
  51. return ret;
  52. if ((ret = init_ns_pid()) < 0)
  53. return ret;
  54. if ((ret = init_ns_sysv()) < 0)
  55. return ret;
  56. return 0;
  57. }
  58. int prepare_ns_leaders (void)
  59. {
  60. int ret = 0;
  61. if ((ret = prepare_pid_leader()) < 0)
  62. return ret;
  63. if ((ret = prepare_sysv_leader()) < 0)
  64. return ret;
  65. return 0;
  66. }
  67. static struct shim_ipc_info * __get_new_ipc_info (IDTYPE vmid, const char * uri,
  68. size_t len)
  69. {
  70. struct shim_ipc_info * info =
  71. get_mem_obj_from_mgr_enlarge(ipc_info_mgr,
  72. size_align_up(ipc_info_mgr_ALLOC));
  73. if (!info)
  74. return NULL;
  75. memset(info, 0, sizeof(struct shim_ipc_info));
  76. if (vmid)
  77. info->vmid = vmid;
  78. if (uri)
  79. qstrsetstr(&info->uri, uri, len);
  80. REF_SET(info->ref_count, 1);
  81. INIT_HLIST_NODE(&info->hlist);
  82. return info;
  83. }
  84. struct shim_ipc_info * get_new_ipc_info (IDTYPE vmid, const char * uri,
  85. size_t len)
  86. {
  87. lock(ipc_info_lock);
  88. struct shim_ipc_info * info = __get_new_ipc_info(vmid, uri, len);
  89. unlock(ipc_info_lock);
  90. return info;
  91. }
  92. static void __get_ipc_info (struct shim_ipc_info * info)
  93. {
  94. #ifdef DEBUG_REF
  95. int ref_count = REF_INC(info->ref_count);
  96. debug("get port %p (vmid %u uri %s, ref_count = %d)\n", info,
  97. info->vmid, qstrgetstr(&info->uri), ref_count);
  98. #else
  99. REF_INC(info->ref_count);
  100. #endif
  101. }
  102. void get_ipc_info (struct shim_ipc_info * info)
  103. {
  104. __get_ipc_info(info);
  105. }
  106. static void unset_ipc_info (struct shim_ipc_info * info)
  107. {
  108. qstrfree(&info->uri);
  109. if (info->port)
  110. put_ipc_port(info->port);
  111. if (info->pal_handle)
  112. DkObjectClose(info->pal_handle);
  113. }
  114. static void __put_ipc_info (struct shim_ipc_info * info)
  115. {
  116. int ref_count = REF_DEC(info->ref_count);
  117. #ifdef DEBUG_REF
  118. debug("put port %p (vmid %u uri %s, ref_count = %d)\n", info,
  119. info->vmid, qstrgetstr(&info->uri), ref_count);
  120. #endif
  121. if (ref_count)
  122. return;
  123. unset_ipc_info(info);
  124. free_mem_obj_to_mgr(ipc_info_mgr, info);
  125. }
  126. void put_ipc_info (struct shim_ipc_info * info)
  127. {
  128. int ref_count = REF_DEC(info->ref_count);
  129. #ifdef DEBUG_REF
  130. debug("put port %p (vmid %u uri %s, ref_count = %d)\n", info,
  131. info->vmid, qstrgetstr(&info->uri), ref_count);
  132. #endif
  133. if (ref_count)
  134. return;
  135. unset_ipc_info(info);
  136. lock(ipc_info_lock);
  137. free_mem_obj_to_mgr(ipc_info_mgr, info);
  138. unlock(ipc_info_lock);
  139. }
  140. #define CLIENT_HASH_LEN 6
  141. #define CLIENT_HASH_NUM (1 << CLIENT_HASH_LEN)
  142. #define CLIENT_HASH_MASK (CLIENT_HASH_NUM - 1)
  143. #define CLIENT_HASH(vmid) ((vmid) & CLIENT_HASH_MASK)
  144. static struct hlist_head client_table [CLIENT_HASH_NUM];
  145. struct shim_ipc_info *
  146. lookup_and_alloc_client (IDTYPE vmid, const char * uri)
  147. {
  148. struct shim_ipc_info * p;
  149. struct hlist_head * head = client_table + CLIENT_HASH(vmid);
  150. struct hlist_node * pos;
  151. size_t len = strlen(uri);
  152. assert(vmid);
  153. lock(ipc_info_lock);
  154. hlist_for_each_entry(p, pos, head, hlist)
  155. if (p->vmid == vmid && !qstrcmpstr(&p->uri, uri, len)) {
  156. get_ipc_info(p);
  157. unlock(ipc_info_lock);
  158. return p;
  159. }
  160. unlock(ipc_info_lock);
  161. lock(ipc_info_lock);
  162. p = __get_new_ipc_info(vmid, uri, len);
  163. if (p) {
  164. hlist_add_head(&p->hlist, head);
  165. get_ipc_info(p);
  166. }
  167. unlock(ipc_info_lock);
  168. return p;
  169. }
  170. void put_client (struct shim_ipc_info * info)
  171. {
  172. lock(ipc_info_lock);
  173. __put_ipc_info(info);
  174. if (REF_GET(info->ref_count) == 1) {
  175. hlist_del_init(&info->hlist);
  176. __put_ipc_info(info);
  177. }
  178. unlock(ipc_info_lock);
  179. }
  180. struct shim_ipc_info * discover_client (struct shim_ipc_port * port,
  181. IDTYPE vmid)
  182. {
  183. struct shim_ipc_info * p;
  184. struct hlist_head * head = client_table + CLIENT_HASH(vmid);
  185. struct hlist_node * pos;
  186. assert(vmid);
  187. lock(ipc_info_lock);
  188. hlist_for_each_entry(p, pos, head, hlist)
  189. if (p->vmid == vmid && !qstrempty(&p->uri)) {
  190. __get_ipc_info(p);
  191. unlock(ipc_info_lock);
  192. return p;
  193. }
  194. unlock(ipc_info_lock);
  195. return NULL;
  196. if (!ipc_finduri_send(port, vmid, &p))
  197. return p;
  198. return NULL;
  199. }
  200. struct shim_process * create_new_process (bool inherit_parent)
  201. {
  202. struct shim_process * new_process = malloc(sizeof(struct shim_process));
  203. if (!new_process)
  204. return NULL;
  205. memset(new_process, 0, sizeof(struct shim_process));
  206. new_process->parent = get_new_ipc_info(cur_process.vmid, NULL, 0);
  207. if (!inherit_parent)
  208. return new_process;
  209. lock(cur_process.lock);
  210. if (cur_process.self)
  211. qstrcopy(&new_process->parent->uri, &cur_process.self->uri);
  212. for (int i = 0 ; i < TOTAL_NS ; i++)
  213. if (cur_process.ns[i])
  214. new_process->ns[i] =
  215. get_new_ipc_info(cur_process.ns[i]->vmid,
  216. qstrgetstr(&cur_process.ns[i]->uri),
  217. cur_process.ns[i]->uri.len);
  218. unlock(cur_process.lock);
  219. return new_process;
  220. }
  221. void destroy_process (struct shim_process * proc)
  222. {
  223. if (proc->self)
  224. put_ipc_info(proc->self);
  225. if (proc->parent)
  226. put_ipc_info(proc->parent);
  227. for (int i = 0 ; i < TOTAL_NS ; i++)
  228. if (proc->ns[i])
  229. put_ipc_info(proc->ns[i]);
  230. free(proc);
  231. }
  232. int __init_ipc_msg (struct shim_ipc_msg * msg, int code, int size, IDTYPE dest)
  233. {
  234. msg->code = code;
  235. msg->size = IPC_MSG_SIZE(size);
  236. msg->src = cur_process.vmid;
  237. msg->dst = dest;
  238. msg->seq = 0;
  239. return 0;
  240. }
  241. struct shim_ipc_msg * create_ipc_msg (int code, int size, IDTYPE dest)
  242. {
  243. struct shim_ipc_msg * msg = malloc(IPC_MSG_SIZE(size));
  244. if (msg && __init_ipc_msg(msg, code, size, dest)) {
  245. free(msg);
  246. msg = NULL;
  247. }
  248. return msg;
  249. }
  250. int __init_ipc_msg_duplex (struct shim_ipc_msg_obj * msg, int code, int size,
  251. IDTYPE dest)
  252. {
  253. __init_ipc_msg(&msg->msg, code, size, dest);
  254. msg->thread = NULL;
  255. INIT_LIST_HEAD(&msg->list);
  256. msg->retval = 0;
  257. msg->private = NULL;
  258. return 0;
  259. }
  260. struct shim_ipc_msg_obj *
  261. create_ipc_msg_duplex (int code, int size, IDTYPE dest)
  262. {
  263. struct shim_ipc_msg_obj * msg = malloc(IPC_MSGOBJ_SIZE(size));
  264. if (msg && __init_ipc_msg_duplex(msg, code, size, dest)) {
  265. free(msg);
  266. msg = NULL;
  267. }
  268. return msg;
  269. }
  270. int __init_ipc_resp_msg (struct shim_ipc_msg * resp, int ret,
  271. unsigned long seq)
  272. {
  273. struct shim_ipc_resp * resp_in = (struct shim_ipc_resp *) resp->msg;
  274. resp->seq = seq;
  275. resp_in->retval = ret;
  276. return 0;
  277. }
  278. struct shim_ipc_msg *
  279. create_ipc_resp_msg (int ret, IDTYPE dest, unsigned long seq)
  280. {
  281. struct shim_ipc_msg * resp =
  282. create_ipc_msg(IPC_RESP, sizeof(struct shim_ipc_resp), dest);
  283. if (resp && __init_ipc_resp_msg(resp, ret, seq)) {
  284. free(resp);
  285. resp = NULL;
  286. }
  287. return resp;
  288. }
  289. int send_ipc_message (struct shim_ipc_msg * msg, struct shim_ipc_port * port)
  290. {
  291. assert(msg->size >= IPC_MSG_MINIMAL_SIZE);
  292. msg->src = cur_process.vmid;
  293. int ret = DkStreamWrite(port->pal_handle, 0, msg->size, msg, NULL);
  294. if (ret == 0 && PAL_NATIVE_ERRNO) {
  295. debug("port %p (handle %p) is removed at sending\n", port,
  296. port->pal_handle);
  297. del_ipc_port_fini(port, -ECHILD);
  298. return -PAL_ERRNO;
  299. }
  300. return 0;
  301. }
  302. int close_ipc_message_duplex (struct shim_ipc_msg_obj * msg,
  303. struct shim_ipc_port * port)
  304. {
  305. if (port && !list_empty(&msg->list)) {
  306. lock(port->msgs_lock);
  307. list_del_init(&msg->list);
  308. unlock(port->msgs_lock);
  309. }
  310. if (msg->thread)
  311. put_thread(msg->thread);
  312. return 0;
  313. }
  314. static struct shim_atomic ipc_seq_counter;
  315. int send_ipc_message_duplex (struct shim_ipc_msg_obj * msg,
  316. struct shim_ipc_port * port, bool save,
  317. void * private_data)
  318. {
  319. atomic_inc(&ipc_seq_counter);
  320. msg->msg.seq = atomic_read(&ipc_seq_counter);
  321. if (save) {
  322. lock(port->msgs_lock);
  323. msg->private = private_data;
  324. list_add_tail(&msg->list, &port->msgs);
  325. unlock(port->msgs_lock);
  326. }
  327. int ret = send_ipc_message(&msg->msg, port);
  328. if (ret < 0) {
  329. if (save)
  330. close_ipc_message_duplex(msg, port);
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. struct shim_ipc_msg_obj * find_ipc_msg_duplex (struct shim_ipc_port * port,
  336. unsigned long seq)
  337. {
  338. struct shim_ipc_msg_obj * tmp, * found = NULL;
  339. lock(port->msgs_lock);
  340. list_for_each_entry(tmp, &port->msgs, list)
  341. if (tmp->msg.seq == seq) {
  342. found = tmp;
  343. list_del_init(&tmp->list);
  344. break;
  345. }
  346. unlock(port->msgs_lock);
  347. return found;
  348. }
  349. /* for convenience */
  350. int do_ipc_duplex (struct shim_ipc_msg_obj * msg,
  351. struct shim_ipc_port * port, unsigned long * seq,
  352. void * private_data)
  353. {
  354. int ret = 0;
  355. struct shim_thread * thread = get_cur_thread();
  356. assert(thread);
  357. if (!msg->thread)
  358. thread_setwait(&msg->thread, thread);
  359. ret = send_ipc_message_duplex(msg, port, true, private_data);
  360. if (seq)
  361. *seq = (ret < 0) ? 0 : msg->msg.seq;
  362. if (ret < 0)
  363. goto out;
  364. debug("wait for response (seq = %lu)\n", msg->msg.seq);
  365. thread_sleep();
  366. ret = msg->retval;
  367. out:
  368. close_ipc_message_duplex(msg, port);
  369. return ret;
  370. }
  371. struct shim_ipc_info * create_ipc_port (IDTYPE vmid, bool listen)
  372. {
  373. struct shim_ipc_info * proc = get_new_ipc_info(vmid, NULL, 0);
  374. if (!proc)
  375. return NULL;
  376. char uri[PIPE_URI_SIZE];
  377. if (create_pipe(NULL, uri, PIPE_URI_SIZE, &proc->pal_handle,
  378. &proc->uri) < 0) {
  379. put_ipc_info(proc);
  380. return NULL;
  381. }
  382. if (listen)
  383. add_ipc_port_by_id(0, proc->pal_handle, IPC_PORT_SERVER,
  384. NULL, &proc->port);
  385. return proc;
  386. }
  387. int create_ipc_location (struct shim_ipc_info ** info)
  388. {
  389. lock(cur_process.lock);
  390. int ret = -EACCES;
  391. if (cur_process.self)
  392. goto success;
  393. cur_process.self = create_ipc_port(cur_process.vmid, true);
  394. if (!cur_process.self)
  395. goto out;
  396. success:
  397. get_ipc_info(cur_process.self);
  398. *info = cur_process.self;
  399. ret = 0;
  400. out:
  401. unlock(cur_process.lock);
  402. return ret;
  403. }
  404. DEFINE_PROFILE_INTERVAL(ipc_finduri_send, ipc);
  405. DEFINE_PROFILE_INTERVAL(ipc_finduri_callback, ipc);
  406. int ipc_finduri_send (struct shim_ipc_port * port, IDTYPE dest,
  407. struct shim_ipc_info ** info)
  408. {
  409. BEGIN_PROFILE_INTERVAL();
  410. int ret;
  411. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  412. IPC_FINDURI, 0, dest);
  413. debug("ipc send to %u: IPC_FINDURI\n", dest);
  414. ret = do_ipc_duplex(msg, port, NULL, info);
  415. SAVE_PROFILE_INTERVAL(ipc_finduri_send);
  416. return ret;
  417. }
  418. int ipc_finduri_callback (IPC_CALLBACK_ARGS)
  419. {
  420. BEGIN_PROFILE_INTERVAL();
  421. int ret = 0;
  422. debug("ipc callback from %u: IPC_FINDURI\n", msg->src);
  423. struct shim_ipc_info * info;
  424. if ((ret = create_ipc_location(&info)) < 0)
  425. goto out;
  426. ret = ipc_telluri_send(port, msg->src, info);
  427. out:
  428. SAVE_PROFILE_INTERVAL(ipc_finduri_callback);
  429. return ret;
  430. }
  431. DEFINE_PROFILE_INTERVAL(ipc_telluri_send, ipc);
  432. DEFINE_PROFILE_INTERVAL(ipc_telluri_callback, ipc);
  433. int ipc_telluri_send (struct shim_ipc_port * port, IDTYPE dest,
  434. struct shim_ipc_info * info)
  435. {
  436. BEGIN_PROFILE_INTERVAL();
  437. int ret;
  438. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  439. IPC_TELLURI,
  440. info->uri.len, dest);
  441. struct shim_ipc_telluri * msgin =
  442. (struct shim_ipc_telluri *) &msg->msg;
  443. if (qstrempty(&info->uri)) {
  444. ret = -ENOENT;
  445. return ret;
  446. }
  447. memcpy(msgin->uri, qstrgetstr(&info->uri), info->uri.len + 1);
  448. debug("ipc send to %u: IPC_TELLURI(%s)\n", dest,
  449. qstrgetstr(&info->uri));
  450. ret = send_ipc_message(msg, port);
  451. SAVE_PROFILE_INTERVAL(ipc_telluri_send);
  452. return ret;
  453. }
  454. int ipc_telluri_callback (IPC_CALLBACK_ARGS)
  455. {
  456. BEGIN_PROFILE_INTERVAL();
  457. int ret = 0;
  458. struct shim_ipc_telluri * msgin =
  459. (struct shim_ipc_telluri *) &msg->msg;
  460. debug("ipc callback from %u: IPC_TELLURI(%s)\n", msg->src, msgin->uri);
  461. struct shim_ipc_info * info =
  462. lookup_and_alloc_client(msg->src, msgin->uri);
  463. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  464. if (obj) {
  465. if (info) {
  466. if (obj->private)
  467. *(struct shim_ipc_info **) obj->private = info;
  468. obj->retval = 0;
  469. } else {
  470. obj->retval = -ENOMEM;
  471. }
  472. if (obj->thread)
  473. thread_wakeup(obj->thread);
  474. }
  475. SAVE_PROFILE_INTERVAL(ipc_telluri_callback);
  476. return ret;
  477. }
  478. DEFINE_PROFILE_INTERVAL(ipc_checkpoint_send, ipc);
  479. DEFINE_PROFILE_INTERVAL(ipc_checkpoint_callback, ipc);
  480. int ipc_checkpoint_send (const char * cpdir, IDTYPE cpsession)
  481. {
  482. BEGIN_PROFILE_INTERVAL();
  483. int ret;
  484. int len = strlen(cpdir);
  485. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  486. IPC_CHECKPOINT,
  487. sizeof(struct shim_ipc_checkpoint)
  488. + len, 0);
  489. struct shim_ipc_checkpoint * msgin =
  490. (struct shim_ipc_checkpoint *) &msg->msg;
  491. msgin->cpsession = cpsession;
  492. memcpy(&msgin->cpdir, cpdir, len + 1);
  493. debug("ipc broadcast to all: IPC_CHECKPOINT(%u, %s)\n",
  494. cpsession, cpdir);
  495. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRCLD|IPC_PORT_DIRPRT);
  496. SAVE_PROFILE_INTERVAL(ipc_checkpoint_send);
  497. return ret;
  498. }
  499. int ipc_checkpoint_callback (IPC_CALLBACK_ARGS)
  500. {
  501. BEGIN_PROFILE_INTERVAL();
  502. int ret = 0;
  503. struct shim_ipc_checkpoint * msgin =
  504. (struct shim_ipc_checkpoint *) msg->msg;
  505. debug("ipc callback form %u: IPC_CHECKPOINT(%u, %s)\n", msg->src,
  506. msgin->cpsession, msgin->cpdir);
  507. ret = create_checkpoint(msgin->cpdir, &msgin->cpsession);
  508. if (ret < 0)
  509. goto out;
  510. kill_all_threads(NULL, msgin->cpsession, SIGCP);
  511. broadcast_ipc(msg, &port, 1, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  512. out:
  513. SAVE_PROFILE_INTERVAL(ipc_checkpoint_callback);
  514. return ret;
  515. }
  516. BEGIN_CP_FUNC(ipc_info)
  517. {
  518. assert(size == sizeof(struct shim_ipc_info));
  519. struct shim_ipc_info * port = (struct shim_ipc_info *) obj;
  520. struct shim_ipc_info * new_port = NULL;
  521. ptr_t off = GET_FROM_CP_MAP(obj);
  522. if (!off) {
  523. off = ADD_CP_OFFSET(sizeof(struct shim_ipc_info));
  524. new_port = (struct shim_ipc_info *) (base + off);
  525. memcpy(new_port, port, sizeof(struct shim_ipc_info));
  526. REF_SET(new_port->ref_count, 0);
  527. DO_CP_IN_MEMBER(qstr, new_port, uri);
  528. if (port->pal_handle &&
  529. port->pal_handle != IPC_FORCE_RECONNECT) {
  530. struct shim_palhdl_entry * entry;
  531. DO_CP(palhdl, port->pal_handle, &entry);
  532. entry->uri = &new_port->uri;
  533. entry->phandle = &new_port->pal_handle;
  534. }
  535. } else {
  536. new_port = (struct shim_ipc_info *) (base + off);
  537. }
  538. if (new_port && objp)
  539. *objp = (void *) new_port;
  540. }
  541. END_CP_FUNC_NO_RS(ipc_info)
  542. BEGIN_CP_FUNC(process)
  543. {
  544. assert(size == sizeof(struct shim_process));
  545. struct shim_process * proc = (struct shim_process *) obj;
  546. struct shim_process * new_proc = NULL;
  547. ptr_t off = GET_FROM_CP_MAP(obj);
  548. if (!off) {
  549. off = ADD_CP_OFFSET(sizeof(struct shim_process));
  550. ADD_TO_CP_MAP(obj, off);
  551. new_proc = (struct shim_process *) (base + off);
  552. memcpy(new_proc, proc, sizeof(struct shim_process));
  553. if (proc->self)
  554. DO_CP_MEMBER(ipc_info, proc, new_proc, self);
  555. if (proc->parent)
  556. DO_CP_MEMBER(ipc_info, proc, new_proc, parent);
  557. for (int i = 0 ; i < TOTAL_NS ; i++)
  558. if (proc->ns[i])
  559. DO_CP_MEMBER(ipc_info, proc, new_proc, ns[i]);
  560. ADD_CP_FUNC_ENTRY(off);
  561. } else {
  562. new_proc = (struct shim_process *) (base + off);
  563. }
  564. if (objp)
  565. *objp = (void *) new_proc;
  566. }
  567. END_CP_FUNC(process)
  568. BEGIN_RS_FUNC(process)
  569. {
  570. struct shim_process * proc = (void *) (base + GET_CP_FUNC_ENTRY());
  571. CP_REBASE(proc->self);
  572. CP_REBASE(proc->parent);
  573. CP_REBASE(proc->ns);
  574. if (proc->self) {
  575. proc->self->vmid = cur_process.vmid;
  576. get_ipc_info(proc->self);
  577. }
  578. if (proc->parent)
  579. get_ipc_info(proc->parent);
  580. for (int i = 0 ; i < TOTAL_NS ; i++)
  581. if (proc->ns[i])
  582. get_ipc_info(proc->ns[i]);
  583. proc->vmid = cur_process.vmid;
  584. memcpy(&cur_process, proc, sizeof(struct shim_process));
  585. create_lock(cur_process.lock);
  586. DEBUG_RS("vmid=%u,uri=%s,parent=%u(%s)", proc->vmid,
  587. proc->self ? qstrgetstr(&proc->self->uri) : "",
  588. proc->parent ? proc->parent->vmid : 0,
  589. proc->parent ? qstrgetstr(&proc->parent->uri) : "");
  590. }
  591. END_RS_FUNC(process)