shim_ipc.c 19 KB

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