shim_ipc.c 19 KB

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