shim_ipc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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 <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_LIST_HEAD(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. /* Links to shim_ipc_info->hlist */
  145. DEFINE_LISTP(shim_ipc_info);
  146. static LISTP_TYPE(shim_ipc_info) client_table [CLIENT_HASH_NUM];
  147. struct shim_ipc_info *
  148. lookup_and_alloc_client (IDTYPE vmid, const char * uri)
  149. {
  150. struct shim_ipc_info * p;
  151. LISTP_TYPE(shim_ipc_info) *head = client_table + CLIENT_HASH(vmid);
  152. size_t len = strlen(uri);
  153. assert(vmid);
  154. lock(ipc_info_lock);
  155. listp_for_each_entry(p, head, hlist)
  156. if (p->vmid == vmid && !qstrcmpstr(&p->uri, uri, len)) {
  157. get_ipc_info(p);
  158. unlock(ipc_info_lock);
  159. return p;
  160. }
  161. unlock(ipc_info_lock);
  162. lock(ipc_info_lock);
  163. p = __get_new_ipc_info(vmid, uri, len);
  164. if (p) {
  165. listp_add(p, head, hlist);
  166. get_ipc_info(p);
  167. }
  168. unlock(ipc_info_lock);
  169. return p;
  170. }
  171. void put_client (struct shim_ipc_info * info)
  172. {
  173. lock(ipc_info_lock);
  174. /* Look up the hash */
  175. LISTP_TYPE(shim_ipc_info) *head = client_table + CLIENT_HASH(info->vmid);
  176. __put_ipc_info(info);
  177. if (REF_GET(info->ref_count) == 1) {
  178. listp_del_init(info, head, hlist);
  179. __put_ipc_info(info);
  180. }
  181. unlock(ipc_info_lock);
  182. }
  183. struct shim_ipc_info * discover_client (struct shim_ipc_port * port,
  184. IDTYPE vmid)
  185. {
  186. struct shim_ipc_info * p;
  187. LISTP_TYPE(shim_ipc_info) * head = client_table + CLIENT_HASH(vmid);
  188. assert(vmid);
  189. lock(ipc_info_lock);
  190. listp_for_each_entry(p, head, hlist)
  191. if (p->vmid == vmid && !qstrempty(&p->uri)) {
  192. __get_ipc_info(p);
  193. unlock(ipc_info_lock);
  194. return p;
  195. }
  196. unlock(ipc_info_lock);
  197. return NULL;
  198. if (!ipc_finduri_send(port, vmid, &p))
  199. return p;
  200. return NULL;
  201. }
  202. struct shim_process * create_new_process (bool inherit_parent)
  203. {
  204. struct shim_process * new_process = calloc(1, sizeof(struct shim_process));
  205. if (!new_process)
  206. return NULL;
  207. new_process->parent = get_new_ipc_info(cur_process.vmid, NULL, 0);
  208. if (!inherit_parent)
  209. return new_process;
  210. lock(cur_process.lock);
  211. if (cur_process.self)
  212. qstrcopy(&new_process->parent->uri, &cur_process.self->uri);
  213. for (int i = 0 ; i < TOTAL_NS ; i++)
  214. if (cur_process.ns[i])
  215. new_process->ns[i] =
  216. get_new_ipc_info(cur_process.ns[i]->vmid,
  217. qstrgetstr(&cur_process.ns[i]->uri),
  218. cur_process.ns[i]->uri.len);
  219. unlock(cur_process.lock);
  220. return new_process;
  221. }
  222. void destroy_process (struct shim_process * proc)
  223. {
  224. if (proc->self)
  225. put_ipc_info(proc->self);
  226. if (proc->parent)
  227. put_ipc_info(proc->parent);
  228. for (int i = 0 ; i < TOTAL_NS ; i++)
  229. if (proc->ns[i])
  230. put_ipc_info(proc->ns[i]);
  231. free(proc);
  232. }
  233. int __init_ipc_msg (struct shim_ipc_msg * msg, int code, int size, IDTYPE dest)
  234. {
  235. msg->code = code;
  236. msg->size = IPC_MSG_SIZE(size);
  237. msg->src = cur_process.vmid;
  238. msg->dst = dest;
  239. msg->seq = 0;
  240. return 0;
  241. }
  242. struct shim_ipc_msg * create_ipc_msg (int code, int size, IDTYPE dest)
  243. {
  244. struct shim_ipc_msg * msg = malloc(IPC_MSG_SIZE(size));
  245. if (msg && __init_ipc_msg(msg, code, size, dest)) {
  246. free(msg);
  247. msg = NULL;
  248. }
  249. return msg;
  250. }
  251. int __init_ipc_msg_duplex (struct shim_ipc_msg_obj * msg, int code, int size,
  252. IDTYPE dest)
  253. {
  254. __init_ipc_msg(&msg->msg, code, size, dest);
  255. msg->thread = NULL;
  256. INIT_LIST_HEAD(msg, list);
  257. msg->retval = 0;
  258. msg->private = NULL;
  259. return 0;
  260. }
  261. struct shim_ipc_msg_obj *
  262. create_ipc_msg_duplex (int code, int size, IDTYPE dest)
  263. {
  264. struct shim_ipc_msg_obj * msg = malloc(IPC_MSGOBJ_SIZE(size));
  265. if (msg && __init_ipc_msg_duplex(msg, code, size, dest)) {
  266. free(msg);
  267. msg = NULL;
  268. }
  269. return msg;
  270. }
  271. int __init_ipc_resp_msg (struct shim_ipc_msg * resp, int ret,
  272. unsigned long seq)
  273. {
  274. struct shim_ipc_resp * resp_in = (struct shim_ipc_resp *) resp->msg;
  275. resp->seq = seq;
  276. resp_in->retval = ret;
  277. return 0;
  278. }
  279. struct shim_ipc_msg *
  280. create_ipc_resp_msg (int ret, IDTYPE dest, unsigned long seq)
  281. {
  282. struct shim_ipc_msg * resp =
  283. create_ipc_msg(IPC_RESP, sizeof(struct shim_ipc_resp), dest);
  284. if (resp && __init_ipc_resp_msg(resp, ret, seq)) {
  285. free(resp);
  286. resp = NULL;
  287. }
  288. return resp;
  289. }
  290. int send_ipc_message (struct shim_ipc_msg * msg, struct shim_ipc_port * port)
  291. {
  292. assert(msg->size >= IPC_MSG_MINIMAL_SIZE);
  293. msg->src = cur_process.vmid;
  294. int ret = DkStreamWrite(port->pal_handle, 0, msg->size, msg, NULL);
  295. if (ret == 0 && PAL_NATIVE_ERRNO) {
  296. debug("port %p (handle %p) is removed at sending\n", port,
  297. port->pal_handle);
  298. del_ipc_port_fini(port, -ECHILD);
  299. return -PAL_ERRNO;
  300. }
  301. return 0;
  302. }
  303. int close_ipc_message_duplex (struct shim_ipc_msg_obj * msg,
  304. struct shim_ipc_port * port)
  305. {
  306. if (port && !list_empty(msg, list)) {
  307. lock(port->msgs_lock);
  308. listp_del_init(msg, &port->msgs, list);
  309. unlock(port->msgs_lock);
  310. }
  311. if (msg->thread) {
  312. put_thread(msg->thread);
  313. msg->thread = NULL;
  314. }
  315. return 0;
  316. }
  317. static struct atomic_int ipc_seq_counter;
  318. int send_ipc_message_duplex (struct shim_ipc_msg_obj * msg,
  319. struct shim_ipc_port * port, bool save,
  320. void * private_data)
  321. {
  322. atomic_inc(&ipc_seq_counter);
  323. msg->msg.seq = atomic_read(&ipc_seq_counter);
  324. if (save) {
  325. lock(port->msgs_lock);
  326. msg->private = private_data;
  327. listp_add_tail(msg, &port->msgs, list);
  328. unlock(port->msgs_lock);
  329. }
  330. int ret = send_ipc_message(&msg->msg, port);
  331. if (ret < 0) {
  332. if (save)
  333. close_ipc_message_duplex(msg, port);
  334. return ret;
  335. }
  336. return 0;
  337. }
  338. struct shim_ipc_msg_obj * find_ipc_msg_duplex (struct shim_ipc_port * port,
  339. unsigned long seq)
  340. {
  341. struct shim_ipc_msg_obj * tmp, * found = NULL;
  342. lock(port->msgs_lock);
  343. listp_for_each_entry(tmp, &port->msgs, list)
  344. if (tmp->msg.seq == seq) {
  345. found = tmp;
  346. listp_del_init(tmp, &port->msgs, list);
  347. break;
  348. }
  349. unlock(port->msgs_lock);
  350. return found;
  351. }
  352. /* for convenience */
  353. int do_ipc_duplex (struct shim_ipc_msg_obj * msg,
  354. struct shim_ipc_port * port, unsigned long * seq,
  355. void * private_data)
  356. {
  357. int ret = 0;
  358. struct shim_thread * thread = get_cur_thread();
  359. assert(thread);
  360. if (!msg->thread)
  361. thread_setwait(&msg->thread, thread);
  362. ret = send_ipc_message_duplex(msg, port, true, private_data);
  363. if (seq)
  364. *seq = (ret < 0) ? 0 : msg->msg.seq;
  365. if (ret < 0)
  366. goto out;
  367. debug("wait for response (seq = %lu)\n", msg->msg.seq);
  368. thread_sleep(NO_TIMEOUT);
  369. ret = msg->retval;
  370. out:
  371. close_ipc_message_duplex(msg, port);
  372. return ret;
  373. }
  374. struct shim_ipc_info * create_ipc_port (IDTYPE vmid, bool listen)
  375. {
  376. struct shim_ipc_info * proc = get_new_ipc_info(vmid, NULL, 0);
  377. if (!proc)
  378. return NULL;
  379. char uri[PIPE_URI_SIZE];
  380. if (create_pipe(NULL, uri, PIPE_URI_SIZE, &proc->pal_handle,
  381. &proc->uri) < 0) {
  382. put_ipc_info(proc);
  383. return NULL;
  384. }
  385. if (listen)
  386. add_ipc_port_by_id(0, proc->pal_handle, IPC_PORT_SERVER,
  387. NULL, &proc->port);
  388. return proc;
  389. }
  390. int create_ipc_location (struct shim_ipc_info ** info)
  391. {
  392. lock(cur_process.lock);
  393. int ret = -EACCES;
  394. if (cur_process.self)
  395. goto success;
  396. cur_process.self = create_ipc_port(cur_process.vmid, true);
  397. if (!cur_process.self)
  398. goto out;
  399. success:
  400. get_ipc_info(cur_process.self);
  401. *info = cur_process.self;
  402. ret = 0;
  403. out:
  404. unlock(cur_process.lock);
  405. return ret;
  406. }
  407. DEFINE_PROFILE_INTERVAL(ipc_finduri_send, ipc);
  408. DEFINE_PROFILE_INTERVAL(ipc_finduri_callback, ipc);
  409. int ipc_finduri_send (struct shim_ipc_port * port, IDTYPE dest,
  410. struct shim_ipc_info ** info)
  411. {
  412. BEGIN_PROFILE_INTERVAL();
  413. int ret;
  414. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  415. IPC_FINDURI, 0, dest);
  416. debug("ipc send to %u: IPC_FINDURI\n", dest);
  417. ret = do_ipc_duplex(msg, port, NULL, info);
  418. SAVE_PROFILE_INTERVAL(ipc_finduri_send);
  419. return ret;
  420. }
  421. int ipc_finduri_callback (IPC_CALLBACK_ARGS)
  422. {
  423. BEGIN_PROFILE_INTERVAL();
  424. int ret = 0;
  425. debug("ipc callback from %u: IPC_FINDURI\n", msg->src);
  426. struct shim_ipc_info * info;
  427. if ((ret = create_ipc_location(&info)) < 0)
  428. goto out;
  429. ret = ipc_telluri_send(port, msg->src, info);
  430. out:
  431. SAVE_PROFILE_INTERVAL(ipc_finduri_callback);
  432. return ret;
  433. }
  434. DEFINE_PROFILE_INTERVAL(ipc_telluri_send, ipc);
  435. DEFINE_PROFILE_INTERVAL(ipc_telluri_callback, ipc);
  436. int ipc_telluri_send (struct shim_ipc_port * port, IDTYPE dest,
  437. struct shim_ipc_info * info)
  438. {
  439. BEGIN_PROFILE_INTERVAL();
  440. int ret;
  441. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  442. IPC_TELLURI,
  443. info->uri.len, dest);
  444. struct shim_ipc_telluri * msgin =
  445. (struct shim_ipc_telluri *) &msg->msg;
  446. if (qstrempty(&info->uri)) {
  447. ret = -ENOENT;
  448. return ret;
  449. }
  450. memcpy(msgin->uri, qstrgetstr(&info->uri), info->uri.len + 1);
  451. debug("ipc send to %u: IPC_TELLURI(%s)\n", dest,
  452. qstrgetstr(&info->uri));
  453. ret = send_ipc_message(msg, port);
  454. SAVE_PROFILE_INTERVAL(ipc_telluri_send);
  455. return ret;
  456. }
  457. int ipc_telluri_callback (IPC_CALLBACK_ARGS)
  458. {
  459. BEGIN_PROFILE_INTERVAL();
  460. int ret = 0;
  461. struct shim_ipc_telluri * msgin =
  462. (struct shim_ipc_telluri *) &msg->msg;
  463. debug("ipc callback from %u: IPC_TELLURI(%s)\n", msg->src, msgin->uri);
  464. struct shim_ipc_info * info =
  465. lookup_and_alloc_client(msg->src, msgin->uri);
  466. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  467. if (obj) {
  468. if (info) {
  469. if (obj->private)
  470. *(struct shim_ipc_info **) obj->private = info;
  471. obj->retval = 0;
  472. } else {
  473. obj->retval = -ENOMEM;
  474. }
  475. if (obj->thread)
  476. thread_wakeup(obj->thread);
  477. }
  478. SAVE_PROFILE_INTERVAL(ipc_telluri_callback);
  479. return ret;
  480. }
  481. DEFINE_PROFILE_INTERVAL(ipc_checkpoint_send, ipc);
  482. DEFINE_PROFILE_INTERVAL(ipc_checkpoint_callback, ipc);
  483. int ipc_checkpoint_send (const char * cpdir, IDTYPE cpsession)
  484. {
  485. BEGIN_PROFILE_INTERVAL();
  486. int ret;
  487. int len = strlen(cpdir);
  488. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  489. IPC_CHECKPOINT,
  490. sizeof(struct shim_ipc_checkpoint)
  491. + len, 0);
  492. struct shim_ipc_checkpoint * msgin =
  493. (struct shim_ipc_checkpoint *) &msg->msg;
  494. msgin->cpsession = cpsession;
  495. memcpy(&msgin->cpdir, cpdir, len + 1);
  496. debug("ipc broadcast to all: IPC_CHECKPOINT(%u, %s)\n",
  497. cpsession, cpdir);
  498. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRCLD|IPC_PORT_DIRPRT);
  499. SAVE_PROFILE_INTERVAL(ipc_checkpoint_send);
  500. return ret;
  501. }
  502. int ipc_checkpoint_callback (IPC_CALLBACK_ARGS)
  503. {
  504. BEGIN_PROFILE_INTERVAL();
  505. int ret = 0;
  506. struct shim_ipc_checkpoint * msgin =
  507. (struct shim_ipc_checkpoint *) msg->msg;
  508. debug("ipc callback form %u: IPC_CHECKPOINT(%u, %s)\n", msg->src,
  509. msgin->cpsession, msgin->cpdir);
  510. ret = create_checkpoint(msgin->cpdir, &msgin->cpsession);
  511. if (ret < 0)
  512. goto out;
  513. kill_all_threads(NULL, msgin->cpsession, SIGCP);
  514. broadcast_ipc(msg, &port, 1, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  515. out:
  516. SAVE_PROFILE_INTERVAL(ipc_checkpoint_callback);
  517. return ret;
  518. }
  519. BEGIN_CP_FUNC(ipc_info)
  520. {
  521. assert(size == sizeof(struct shim_ipc_info));
  522. struct shim_ipc_info * port = (struct shim_ipc_info *) obj;
  523. struct shim_ipc_info * new_port = NULL;
  524. ptr_t off = GET_FROM_CP_MAP(obj);
  525. if (!off) {
  526. off = ADD_CP_OFFSET(sizeof(struct shim_ipc_info));
  527. new_port = (struct shim_ipc_info *) (base + off);
  528. memcpy(new_port, port, sizeof(struct shim_ipc_info));
  529. REF_SET(new_port->ref_count, 0);
  530. DO_CP_IN_MEMBER(qstr, new_port, uri);
  531. if (port->pal_handle &&
  532. port->pal_handle != IPC_FORCE_RECONNECT) {
  533. struct shim_palhdl_entry * entry;
  534. DO_CP(palhdl, port->pal_handle, &entry);
  535. entry->uri = &new_port->uri;
  536. entry->phandle = &new_port->pal_handle;
  537. }
  538. } else {
  539. new_port = (struct shim_ipc_info *) (base + off);
  540. }
  541. if (new_port && objp)
  542. *objp = (void *) new_port;
  543. }
  544. END_CP_FUNC_NO_RS(ipc_info)
  545. BEGIN_CP_FUNC(process)
  546. {
  547. assert(size == sizeof(struct shim_process));
  548. struct shim_process * proc = (struct shim_process *) obj;
  549. struct shim_process * new_proc = NULL;
  550. ptr_t off = GET_FROM_CP_MAP(obj);
  551. if (!off) {
  552. off = ADD_CP_OFFSET(sizeof(struct shim_process));
  553. ADD_TO_CP_MAP(obj, off);
  554. new_proc = (struct shim_process *) (base + off);
  555. memcpy(new_proc, proc, sizeof(struct shim_process));
  556. if (proc->self)
  557. DO_CP_MEMBER(ipc_info, proc, new_proc, self);
  558. if (proc->parent)
  559. DO_CP_MEMBER(ipc_info, proc, new_proc, parent);
  560. for (int i = 0 ; i < TOTAL_NS ; i++)
  561. if (proc->ns[i])
  562. DO_CP_MEMBER(ipc_info, proc, new_proc, ns[i]);
  563. ADD_CP_FUNC_ENTRY(off);
  564. } else {
  565. new_proc = (struct shim_process *) (base + off);
  566. }
  567. if (objp)
  568. *objp = (void *) new_proc;
  569. }
  570. END_CP_FUNC(process)
  571. BEGIN_RS_FUNC(process)
  572. {
  573. struct shim_process * proc = (void *) (base + GET_CP_FUNC_ENTRY());
  574. CP_REBASE(proc->self);
  575. CP_REBASE(proc->parent);
  576. CP_REBASE(proc->ns);
  577. if (proc->self) {
  578. proc->self->vmid = cur_process.vmid;
  579. get_ipc_info(proc->self);
  580. }
  581. if (proc->parent)
  582. get_ipc_info(proc->parent);
  583. for (int i = 0 ; i < TOTAL_NS ; i++)
  584. if (proc->ns[i])
  585. get_ipc_info(proc->ns[i]);
  586. proc->vmid = cur_process.vmid;
  587. memcpy(&cur_process, proc, sizeof(struct shim_process));
  588. create_lock(cur_process.lock);
  589. DEBUG_RS("vmid=%u,uri=%s,parent=%u(%s)", proc->vmid,
  590. proc->self ? qstrgetstr(&proc->self->uri) : "",
  591. proc->parent ? proc->parent->vmid : 0,
  592. proc->parent ? qstrgetstr(&proc->parent->uri) : "");
  593. }
  594. END_RS_FUNC(process)