shim_ipc.c 20 KB

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