shim_msgget.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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_msgget.c
  15. *
  16. * Implementation of system call "msgget", "msgsnd", "msgrcv" and "msgctl".
  17. */
  18. #include <errno.h>
  19. #include <list.h>
  20. #include <pal.h>
  21. #include <pal_error.h>
  22. #include <shim_handle.h>
  23. #include <shim_internal.h>
  24. #include <shim_ipc.h>
  25. #include <shim_profile.h>
  26. #include <shim_sysv.h>
  27. #include <shim_unistd.h>
  28. #include <shim_utils.h>
  29. #define MSGQ_HASH_LEN 8
  30. #define MSGQ_HASH_NUM (1 << MSGQ_HASH_LEN)
  31. #define MSGQ_HASH_MASK (MSGQ_HASH_NUM - 1)
  32. #define MSGQ_HASH(idx) ((idx) & MSGQ_HASH_MASK)
  33. /* The msgq_list links shim_msg_handle objects by the list field.
  34. * The msgq_key_hlist links them by key_hlist, and qid_hlist by qid_hlist */
  35. DEFINE_LISTP(shim_msg_handle);
  36. static LISTP_TYPE(shim_msg_handle) msgq_list;
  37. static LISTP_TYPE(shim_msg_handle) msgq_key_hlist[MSGQ_HASH_NUM];
  38. static LISTP_TYPE(shim_msg_handle) msgq_qid_hlist[MSGQ_HASH_NUM];
  39. static struct shim_lock msgq_list_lock;
  40. static int __load_msg_persist(struct shim_msg_handle* msgq, bool readmsg);
  41. static int __store_msg_persist(struct shim_msg_handle* msgq);
  42. DEFINE_PROFILE_CATEGORY(sysv_msg, );
  43. #define MSG_TO_HANDLE(msghdl) container_of((msghdl), struct shim_handle, info.msg)
  44. static int __add_msg_handle(unsigned long key, IDTYPE msqid, bool owned,
  45. struct shim_msg_handle** msghdl) {
  46. assert(locked(&msgq_list_lock));
  47. LISTP_TYPE(shim_msg_handle)* key_head =
  48. (key != IPC_PRIVATE) ? &msgq_key_hlist[MSGQ_HASH(key)] : NULL;
  49. LISTP_TYPE(shim_msg_handle)* qid_head = msqid ? &msgq_qid_hlist[MSGQ_HASH(msqid)] : NULL;
  50. struct shim_msg_handle* tmp;
  51. if (key_head)
  52. LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist) {
  53. if (tmp->msqkey == key) {
  54. if (tmp->msqid == msqid) {
  55. if (msghdl)
  56. *msghdl = tmp;
  57. return 0;
  58. }
  59. return -EEXIST;
  60. }
  61. }
  62. if (qid_head)
  63. LISTP_FOR_EACH_ENTRY(tmp, qid_head, qid_hlist) {
  64. if (tmp->msqid == msqid) {
  65. if (key)
  66. tmp->msqkey = key;
  67. if (msghdl)
  68. *msghdl = tmp;
  69. return 0;
  70. }
  71. }
  72. struct shim_handle* hdl = get_new_handle();
  73. if (!hdl)
  74. return -ENOMEM;
  75. struct shim_msg_handle* msgq = &hdl->info.msg;
  76. hdl->type = TYPE_MSG;
  77. msgq->msqkey = key;
  78. msgq->msqid = msqid;
  79. msgq->owned = owned;
  80. msgq->deleted = false;
  81. msgq->currentsize = 0;
  82. msgq->event = DkSynchronizationEventCreate(PAL_FALSE);
  83. msgq->queue = malloc(MSG_QOBJ_SIZE * DEFAULT_MSG_QUEUE_SIZE);
  84. msgq->queuesize = DEFAULT_MSG_QUEUE_SIZE;
  85. msgq->queueused = 0;
  86. msgq->freed = NULL;
  87. msgq->ntypes = 0;
  88. msgq->maxtypes = INIT_MSG_TYPE_SIZE;
  89. msgq->types = malloc(sizeof(struct msg_type) * INIT_MSG_TYPE_SIZE);
  90. INIT_LIST_HEAD(msgq, list);
  91. get_handle(hdl);
  92. LISTP_ADD_TAIL(msgq, &msgq_list, list);
  93. INIT_LIST_HEAD(msgq, key_hlist);
  94. if (key_head) {
  95. get_handle(hdl);
  96. LISTP_ADD(msgq, key_head, key_hlist);
  97. }
  98. INIT_LIST_HEAD(msgq, qid_hlist);
  99. if (qid_head) {
  100. get_handle(hdl);
  101. LISTP_ADD(msgq, qid_head, qid_hlist);
  102. }
  103. if (!msghdl) {
  104. put_handle(hdl);
  105. return 0;
  106. }
  107. *msghdl = msgq;
  108. return 0;
  109. }
  110. int add_msg_handle(unsigned long key, IDTYPE id, bool owned) {
  111. lock(&msgq_list_lock);
  112. int ret = __add_msg_handle(key, id, owned, NULL);
  113. unlock(&msgq_list_lock);
  114. return ret;
  115. }
  116. struct shim_msg_handle* get_msg_handle_by_key(unsigned long key) {
  117. LISTP_TYPE(shim_msg_handle)* key_head = &msgq_key_hlist[MSGQ_HASH(key)];
  118. struct shim_msg_handle* tmp;
  119. struct shim_msg_handle* found = NULL;
  120. lock(&msgq_list_lock);
  121. LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist) {
  122. if (tmp->msqkey == key) {
  123. found = tmp;
  124. break;
  125. }
  126. }
  127. if (found)
  128. get_handle(MSG_TO_HANDLE(found));
  129. unlock(&msgq_list_lock);
  130. return found;
  131. }
  132. struct shim_msg_handle* get_msg_handle_by_id(IDTYPE msqid) {
  133. LISTP_TYPE(shim_msg_handle)* qid_head = &msgq_qid_hlist[MSGQ_HASH(msqid)];
  134. struct shim_msg_handle* tmp;
  135. struct shim_msg_handle* found = NULL;
  136. lock(&msgq_list_lock);
  137. LISTP_FOR_EACH_ENTRY(tmp, qid_head, qid_hlist) {
  138. if (tmp->msqid == msqid) {
  139. found = tmp;
  140. break;
  141. }
  142. }
  143. if (found)
  144. get_handle(MSG_TO_HANDLE(found));
  145. unlock(&msgq_list_lock);
  146. return found;
  147. }
  148. void put_msg_handle(struct shim_msg_handle* msgq) {
  149. put_handle(MSG_TO_HANDLE(msgq));
  150. }
  151. static void* __get_msg_qobj(struct shim_msg_handle* msgq) {
  152. struct msg_qobj* obj = NULL;
  153. if (msgq->freed) {
  154. obj = msgq->freed;
  155. msgq->freed = obj->next;
  156. obj->next = NULL;
  157. return obj;
  158. }
  159. if (msgq->queueused < msgq->queuesize) {
  160. obj = &msgq->queue[msgq->queueused];
  161. msgq->queueused++;
  162. obj->next = NULL;
  163. return obj;
  164. }
  165. return NULL;
  166. }
  167. static void __free_msg_qobj(struct shim_msg_handle* msgq, void* obj) {
  168. ((struct msg_qobj*)obj)->next = msgq->freed;
  169. msgq->freed = obj;
  170. }
  171. static void __free_msg_linked_qobjs(struct shim_msg_handle* msgq, void* obj) {
  172. struct msg_qobj* qobj = obj;
  173. while (qobj) {
  174. struct msg_qobj* next = qobj->next;
  175. __free_msg_qobj(msgq, qobj);
  176. qobj = next;
  177. }
  178. }
  179. static int __del_msg_handle(struct shim_msg_handle* msgq) {
  180. struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
  181. assert(locked(&hdl->lock));
  182. if (msgq->deleted)
  183. return -EIDRM;
  184. msgq->deleted = true;
  185. free(msgq->queue);
  186. msgq->queuesize = 0;
  187. msgq->queueused = 0;
  188. free(msgq->types);
  189. msgq->ntypes = 0;
  190. lock(&msgq_list_lock);
  191. LISTP_DEL_INIT(msgq, &msgq_list, list);
  192. put_handle(hdl);
  193. if (!LIST_EMPTY(msgq, key_hlist)) {
  194. // DEP: Yuck, re-find the head; maybe we can do better...
  195. LISTP_TYPE(shim_msg_handle)* key_head = &msgq_key_hlist[MSGQ_HASH(msgq->msqkey)];
  196. LISTP_DEL_INIT(msgq, key_head, key_hlist);
  197. put_handle(hdl);
  198. }
  199. if (!LIST_EMPTY(msgq, qid_hlist)) {
  200. // DEP: Yuck, re-find the head; maybe we can do better...
  201. LISTP_TYPE(shim_msg_handle)* qid_head = &msgq_qid_hlist[MSGQ_HASH(msgq->msqid)];
  202. LISTP_DEL_INIT(msgq, qid_head, qid_hlist);
  203. put_handle(hdl);
  204. }
  205. unlock(&msgq_list_lock);
  206. return 0;
  207. }
  208. int del_msg_handle(struct shim_msg_handle* msgq) {
  209. struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
  210. lock(&hdl->lock);
  211. int ret = __del_msg_handle(msgq);
  212. unlock(&hdl->lock);
  213. return ret;
  214. }
  215. static void __try_create_lock(void) {
  216. create_lock_runtime(&msgq_list_lock);
  217. }
  218. int shim_do_msgget(key_t key, int msgflg) {
  219. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  220. IDTYPE msgid = 0;
  221. int ret;
  222. __try_create_lock();
  223. if (key != IPC_PRIVATE) {
  224. struct shim_msg_handle* msgq = get_msg_handle_by_key(key);
  225. if (msgq) {
  226. msgid = msgq->msqid;
  227. put_msg_handle(msgq);
  228. return (msgflg & IPC_EXCL) ? -EEXIST : (int)msgid;
  229. }
  230. }
  231. struct sysv_key k;
  232. k.key = key;
  233. k.type = SYSV_MSGQ;
  234. if (msgflg & IPC_CREAT) {
  235. do {
  236. msgid = allocate_sysv(0, 0);
  237. if (!msgid)
  238. ipc_sysv_lease_send(NULL);
  239. } while (!msgid);
  240. if (key != IPC_PRIVATE) {
  241. if ((ret = ipc_sysv_tellkey_send(NULL, 0, &k, msgid, 0)) < 0) {
  242. release_sysv(msgid);
  243. return ret;
  244. }
  245. }
  246. add_msg_handle(key, msgid, true);
  247. } else {
  248. /* query the manager with the key to find the
  249. corresponding sysvkey */
  250. if ((ret = ipc_sysv_findkey_send(&k)) < 0)
  251. return ret;
  252. msgid = ret;
  253. if ((ret = ipc_sysv_query_send(msgid)) < 0)
  254. return ret;
  255. add_msg_handle(key, msgid, false);
  256. }
  257. return msgid;
  258. }
  259. static int connect_msg_handle(int msqid, struct shim_msg_handle** msgqp) {
  260. struct shim_msg_handle* msgq = get_msg_handle_by_id(msqid);
  261. int ret;
  262. if (!msgq) {
  263. if ((ret = ipc_sysv_query_send(msqid)) < 0)
  264. return ret;
  265. if (!msgq) {
  266. lock(&msgq_list_lock);
  267. ret = __add_msg_handle(IPC_PRIVATE, msqid, false, &msgq);
  268. unlock(&msgq_list_lock);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. }
  273. if (msgq->deleted)
  274. return -EIDRM;
  275. *msgqp = msgq;
  276. return 0;
  277. }
  278. int recover_msg_ownership(struct shim_msg_handle* msgq) {
  279. struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
  280. lock(&hdl->lock);
  281. assert(!msgq->owned);
  282. int ret = __load_msg_persist(msgq, true);
  283. if (ret < 0) {
  284. ret = (ret == -ENOENT) ? -EIDRM : ret;
  285. goto out;
  286. }
  287. msgq->owned = true;
  288. DkEventSet(msgq->event);
  289. out:
  290. unlock(&hdl->lock);
  291. return 0;
  292. }
  293. int shim_do_msgsnd(int msqid, const void* msgp, size_t msgsz, int msgflg) {
  294. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  295. // Issue #755 - https://github.com/oscarlab/graphene/issues/755
  296. __UNUSED(msgflg);
  297. int ret;
  298. if (msgsz > MSGMAX)
  299. return -EINVAL;
  300. if (!msgp)
  301. return -EFAULT;
  302. struct __kernel_msgbuf* msgbuf = (struct __kernel_msgbuf*)msgp;
  303. if (msgbuf->mtype < 0)
  304. return -EINVAL;
  305. struct shim_msg_handle* msgq;
  306. __try_create_lock();
  307. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  308. return ret;
  309. ret = add_sysv_msg(msgq, msgbuf->mtype, msgsz, msgbuf->mtext, NULL);
  310. put_msg_handle(msgq);
  311. return ret;
  312. }
  313. int shim_do_msgrcv(int msqid, void* msgp, size_t msgsz, long msgtype, int msgflg) {
  314. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  315. // Issue #755 - https://github.com/oscarlab/graphene/issues/755
  316. __UNUSED(msgflg);
  317. int ret;
  318. if (msgsz > MSGMAX)
  319. return -EINVAL;
  320. if (!msgp)
  321. return -EFAULT;
  322. struct __kernel_msgbuf* msgbuf = (struct __kernel_msgbuf*)msgp;
  323. struct shim_msg_handle* msgq;
  324. __try_create_lock();
  325. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  326. return ret;
  327. ret = get_sysv_msg(msgq, msgtype, msgsz, msgbuf->mtext, msgflg, NULL);
  328. put_msg_handle(msgq);
  329. return ret;
  330. }
  331. int shim_do_msgctl(int msqid, int cmd, struct msqid_ds* buf) {
  332. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  333. // Issue #756 - https://github.com/oscarlab/graphene/issues/756
  334. __UNUSED(buf);
  335. struct shim_msg_handle* msgq;
  336. int ret;
  337. __try_create_lock();
  338. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  339. return ret;
  340. switch (cmd) {
  341. case IPC_RMID:
  342. if (!msgq->owned) {
  343. ret = ipc_sysv_delres_send(NULL, 0, msgq->msqid, SYSV_MSGQ);
  344. if (ret < 0)
  345. break;
  346. }
  347. __del_msg_handle(msgq);
  348. break;
  349. default:
  350. ret = -ENOSYS;
  351. break;
  352. }
  353. put_msg_handle(msgq);
  354. return ret;
  355. }
  356. static struct msg_type* __add_msg_type(int type, struct msg_type** ptypes, int* pntypes,
  357. int* pmaxtypes) {
  358. struct msg_type* types = *ptypes;
  359. int ntypes = *pntypes;
  360. int maxtypes = *pmaxtypes;
  361. struct msg_type* mtype;
  362. for (mtype = types; mtype < &types[ntypes] && mtype->type <= type; mtype++)
  363. if (mtype->type == type)
  364. return mtype;
  365. int off = mtype - types;
  366. struct msg_type* new_types = types;
  367. if (ntypes == maxtypes)
  368. new_types = malloc(sizeof(struct msg_type) * maxtypes * 2);
  369. if (mtype < &types[ntypes])
  370. memmove(new_types + off + 1, mtype, sizeof(struct msg_type) * (ntypes - off));
  371. if (new_types != types) {
  372. memcpy(new_types, types, sizeof(struct msg_type) * off);
  373. free(types);
  374. mtype = new_types + off;
  375. *ptypes = new_types;
  376. *pmaxtypes = maxtypes * 2;
  377. }
  378. mtype->type = type;
  379. mtype->msgs = NULL;
  380. mtype->msg_tail = NULL;
  381. mtype->reqs = NULL;
  382. mtype->req_tail = NULL;
  383. (*pntypes)++;
  384. return mtype;
  385. }
  386. static int __load_msg_qobjs(struct shim_msg_handle* msgq, struct msg_type* mtype,
  387. struct msg_item* msg, void* data) {
  388. size_t copysize = MSG_ITEM_DATA_SIZE(msg->size);
  389. memcpy(data, msg->data, copysize);
  390. mtype->msgs = msg->next;
  391. __free_msg_qobj(msgq, msg);
  392. while (copysize < msg->size) {
  393. assert(mtype->msgs);
  394. struct msg_ext_item* ext = (struct msg_ext_item*)mtype->msgs;
  395. size_t sz = MSG_EXT_ITEM_DATA_SIZE(msg->size - copysize);
  396. memcpy(data + copysize, ext->data, sz);
  397. copysize += sz;
  398. mtype->msgs = ext->next;
  399. __free_msg_qobj(msgq, ext);
  400. }
  401. if (!mtype->msgs)
  402. mtype->msg_tail = NULL;
  403. msgq->nmsgs--;
  404. msgq->currentsize -= msg->size;
  405. return 0;
  406. }
  407. static int __store_msg_qobjs(struct shim_msg_handle* msgq, struct msg_type* mtype, size_t size,
  408. const void* data) {
  409. struct msg_item* newmsg = __get_msg_qobj(msgq);
  410. if (!newmsg)
  411. return -EAGAIN;
  412. struct msg_item* old_tail = mtype->msg_tail;
  413. newmsg->next = NULL;
  414. newmsg->size = size;
  415. size_t copysize = MSG_ITEM_DATA_SIZE(size);
  416. memcpy(newmsg->data, data, copysize);
  417. if (mtype->msg_tail) {
  418. mtype->msg_tail->next = newmsg;
  419. mtype->msg_tail = newmsg;
  420. } else {
  421. assert(!mtype->msgs);
  422. mtype->msgs = mtype->msg_tail = newmsg;
  423. }
  424. while (copysize < size) {
  425. struct msg_ext_item* ext = __get_msg_qobj(msgq);
  426. if (!ext)
  427. goto eagain;
  428. size_t sz = MSG_EXT_ITEM_DATA_SIZE(size - copysize);
  429. memcpy(ext->data, data + copysize, sz);
  430. ext->next = NULL;
  431. mtype->msg_tail->next = ext;
  432. mtype->msg_tail = (struct msg_item*)ext;
  433. copysize += sz;
  434. }
  435. msgq->nmsgs++;
  436. msgq->currentsize += size;
  437. return 0;
  438. eagain:
  439. __free_msg_linked_qobjs(msgq, newmsg);
  440. if (mtype->msgs == newmsg)
  441. mtype->msgs = NULL;
  442. mtype->msg_tail = old_tail;
  443. return -EAGAIN;
  444. }
  445. #if MIGRATE_SYSV_MSG == 1
  446. static int msg_balance_migrate(struct shim_handle* hdl, struct sysv_client* client);
  447. static struct sysv_balance_policy msg_policy = {
  448. .score_decay = MSG_SCORE_DECAY,
  449. .score_max = MSG_SCORE_MAX,
  450. .balance_threshold = MSG_BALANCE_THRESHOLD,
  451. .migrate = &msg_balance_migrate,
  452. };
  453. #endif
  454. DEFINE_PROFILE_INTERVAL(add_sysv_msg, sysv_msg);
  455. int add_sysv_msg(struct shim_msg_handle* msgq, long type, size_t size, const void* data,
  456. struct sysv_client* src) {
  457. BEGIN_PROFILE_INTERVAL();
  458. struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
  459. int ret = 0;
  460. lock(&hdl->lock);
  461. if (msgq->deleted) {
  462. ret = -EIDRM;
  463. goto out_locked;
  464. }
  465. if (!msgq->owned) {
  466. unlock(&hdl->lock);
  467. ret = ipc_sysv_msgsnd_send(src->port, src->vmid, msgq->msqid, type, data, size, src->seq);
  468. goto out;
  469. }
  470. struct msg_type* mtype = __add_msg_type(type, &msgq->types, &msgq->ntypes, &msgq->maxtypes);
  471. if ((ret = __store_msg_qobjs(msgq, mtype, size, data)) < 0)
  472. goto out_locked;
  473. #if MIGRATE_SYSV_MSG == 1
  474. if (msgq->owned)
  475. __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS, src, MSG_SND_SCORE);
  476. #endif
  477. DkEventSet(msgq->event);
  478. ret = 0;
  479. out_locked:
  480. unlock(&hdl->lock);
  481. out:
  482. SAVE_PROFILE_INTERVAL(add_sysv_msg);
  483. return ret;
  484. }
  485. static struct msg_type* __find_msg_type(int type, struct msg_type* types, int ntypes) {
  486. for (struct msg_type* mtype = types; mtype < &types[ntypes] && mtype->type <= type; mtype++)
  487. if (mtype->type == type)
  488. return mtype;
  489. return NULL;
  490. }
  491. static int __add_msg_req(struct shim_msg_handle* msgq, struct msg_type* mtype, int size, int flags,
  492. struct sysv_client* src) {
  493. if (msgq->deleted)
  494. return -EIDRM;
  495. struct msg_req* req = __get_msg_qobj(msgq);
  496. if (!req)
  497. return -ENOMEM;
  498. get_ipc_port(src->port);
  499. req->next = NULL;
  500. req->size = size;
  501. req->flags = flags;
  502. req->dest = *src;
  503. if (mtype->req_tail) {
  504. mtype->req_tail->next = req;
  505. mtype->req_tail = req;
  506. } else {
  507. assert(!mtype->reqs);
  508. mtype->reqs = mtype->req_tail = req;
  509. }
  510. return 0;
  511. }
  512. DEFINE_PROFILE_INTERVAL(get_sysv_msg, sysv_msg);
  513. int get_sysv_msg(struct shim_msg_handle* msgq, long type, size_t size, void* data, int flags,
  514. struct sysv_client* src) {
  515. BEGIN_PROFILE_INTERVAL();
  516. int ret = 0;
  517. struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
  518. struct msg_item* msg = NULL;
  519. struct msg_type* alltypes = NULL;
  520. struct msg_type* mtype = NULL;
  521. lock(&hdl->lock);
  522. if (msgq->deleted) {
  523. ret = -EIDRM;
  524. goto out_locked;
  525. }
  526. #if MIGRATE_SYSV_MSG == 1
  527. if (msgq->owned) {
  528. __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS, src, MSG_RCV_SCORE);
  529. if (!msgq->owned && src) {
  530. struct shim_ipc_info* owner = msgq->owner;
  531. assert(owner);
  532. ret = ipc_sysv_movres_send(src, owner->vmid, qstrgetstr(&owner->uri), msgq->lease,
  533. msgq->msqid, SYSV_MSGQ);
  534. goto out_locked;
  535. }
  536. }
  537. #endif
  538. if (!msgq->owned) {
  539. if (src) {
  540. struct shim_ipc_info* owner = msgq->owner;
  541. ret = owner ? ipc_sysv_movres_send(src, owner->vmid, qstrgetstr(&owner->uri),
  542. msgq->lease, msgq->msqid, SYSV_MSGQ)
  543. : -ECONNREFUSED;
  544. goto out_locked;
  545. }
  546. unowned:
  547. unlock(&hdl->lock);
  548. ret = ipc_sysv_msgrcv_send(msgq->msqid, type, flags, data, size);
  549. if (ret != -EAGAIN && ret != -ECONNREFUSED)
  550. goto out;
  551. lock(&hdl->lock);
  552. if (!msgq->owned)
  553. goto out_locked;
  554. }
  555. while (1) {
  556. if (alltypes != msgq->types || !mtype || mtype->type != type) {
  557. alltypes = msgq->types;
  558. mtype = __find_msg_type(type, alltypes, msgq->ntypes);
  559. }
  560. if (mtype && mtype->msgs) {
  561. msg = mtype->msgs;
  562. if (msg->size > size && !(flags & MSG_NOERROR)) {
  563. ret = -E2BIG;
  564. goto out;
  565. }
  566. break;
  567. }
  568. if (flags & IPC_NOWAIT || src)
  569. break;
  570. unlock(&hdl->lock);
  571. object_wait_with_retry(msgq->event);
  572. lock(&hdl->lock);
  573. if (!msgq->owned)
  574. goto unowned;
  575. }
  576. if (!msg) {
  577. ret =
  578. (!(flags & IPC_NOWAIT) && src) ? __add_msg_req(msgq, mtype, size, flags, src) : -ENOMSG;
  579. goto out_locked;
  580. }
  581. if ((ret = __load_msg_qobjs(msgq, mtype, msg, data)) < 0)
  582. goto out_locked;
  583. ret = msg->size;
  584. out_locked:
  585. unlock(&hdl->lock);
  586. out:
  587. SAVE_PROFILE_INTERVAL(get_sysv_msg);
  588. return ret;
  589. }
  590. static int __store_msg_persist(struct shim_msg_handle* msgq) {
  591. int ret = 0;
  592. if (msgq->deleted)
  593. goto out;
  594. debug("store msgq %d to persistent store\n", msgq->msqid);
  595. char fileuri[20];
  596. snprintf(fileuri, 20, URI_PREFIX_FILE "msgq.%08x", msgq->msqid);
  597. PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDWR, 0600, PAL_CREATE_TRY, 0);
  598. if (!file) {
  599. ret = -PAL_ERRNO;
  600. goto out;
  601. }
  602. int expected_size = sizeof(struct msg_handle_backup) + sizeof(struct msg_backup) * msgq->nmsgs +
  603. msgq->currentsize;
  604. if (DkStreamSetLength(file, expected_size))
  605. goto err_file;
  606. void* mem =
  607. (void*)DkStreamMap(file, NULL, PAL_PROT_READ | PAL_PROT_WRITE, 0,
  608. ALLOC_ALIGN_UP(expected_size));
  609. if (!mem) {
  610. ret = -EFAULT;
  611. goto err_file;
  612. }
  613. struct msg_handle_backup* mback = mem;
  614. mem += sizeof(struct msg_handle_backup);
  615. mback->perm = msgq->perm;
  616. mback->nmsgs = msgq->nmsgs;
  617. mback->currentsize = msgq->currentsize;
  618. struct msg_type* mtype;
  619. for (mtype = msgq->types; mtype < &msgq->types[msgq->ntypes]; mtype++) {
  620. while (mtype->msgs) {
  621. struct msg_backup* msg = mem;
  622. mem += sizeof(struct msg_backup) + mtype->msgs->size;
  623. msg->type = mtype->type;
  624. msg->size = mtype->msgs->size;
  625. __load_msg_qobjs(msgq, mtype, mtype->msgs, msg->data);
  626. }
  627. mtype->msgs = mtype->msg_tail = NULL;
  628. }
  629. DkStreamUnmap(mem, ALLOC_ALIGN_UP(expected_size));
  630. if (msgq->owned)
  631. for (mtype = msgq->types; mtype < &msgq->types[msgq->ntypes]; mtype++) {
  632. struct msg_req* req = mtype->reqs;
  633. mtype->reqs = mtype->req_tail = NULL;
  634. while (req) {
  635. struct sysv_client* c = &req->dest;
  636. struct msg_req* next = req->next;
  637. send_response_ipc_message(c->port, c->vmid, -EIDRM, c->seq);
  638. put_ipc_port(c->port);
  639. __free_msg_qobj(msgq, req);
  640. req = next;
  641. }
  642. }
  643. msgq->owned = false;
  644. ret = 0;
  645. goto out;
  646. err_file:
  647. DkStreamDelete(file, 0);
  648. DkObjectClose(file);
  649. out:
  650. // To wake up any receiver waiting on local message which must
  651. // now be requested from new owner.
  652. DkEventSet(msgq->event);
  653. return ret;
  654. }
  655. static int __load_msg_persist(struct shim_msg_handle* msgq, bool readmsg) {
  656. int ret = 0;
  657. char fileuri[20];
  658. snprintf(fileuri, 20, URI_PREFIX_FILE "msgq.%08x", msgq->msqid);
  659. PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDONLY, 0, 0, 0);
  660. if (!file)
  661. return -EIDRM;
  662. struct msg_handle_backup mback;
  663. size_t bytes = DkStreamRead(file, 0, sizeof(struct msg_handle_backup), &mback, NULL, 0);
  664. if (bytes == PAL_STREAM_ERROR) {
  665. ret = -PAL_ERRNO;
  666. goto out;
  667. }
  668. if (bytes < sizeof(struct msg_handle_backup)) {
  669. ret = -EFAULT;
  670. goto out;
  671. }
  672. msgq->perm = mback.perm;
  673. if (!readmsg || !mback.nmsgs)
  674. goto done;
  675. int expected_size = sizeof(struct msg_handle_backup) + sizeof(struct msg_backup) * mback.nmsgs +
  676. mback.currentsize;
  677. void* mem = (void*)DkStreamMap(file, NULL, PAL_PROT_READ, 0, ALLOC_ALIGN_UP(expected_size));
  678. if (!mem) {
  679. ret = -PAL_ERRNO;
  680. goto out;
  681. }
  682. mem += sizeof(struct msg_handle_backup);
  683. struct msg_type* mtype = NULL;
  684. for (int i = 0; i < mback.nmsgs; i++) {
  685. struct msg_backup* m = mem;
  686. mem += sizeof(struct msg_backup) + m->size;
  687. debug("load msg: type=%ld, size=%d\n", m->type, m->size);
  688. if (!mtype || mtype->type != m->type)
  689. mtype = __add_msg_type(m->type, &msgq->types, &msgq->ntypes, &msgq->maxtypes);
  690. if ((ret = __store_msg_qobjs(msgq, mtype, m->size, m->data)) < 0)
  691. goto out;
  692. };
  693. DkStreamUnmap(mem, ALLOC_ALIGN_UP(expected_size));
  694. done:
  695. DkStreamDelete(file, 0);
  696. ret = 0;
  697. goto out;
  698. out:
  699. DkObjectClose(file);
  700. return ret;
  701. }
  702. int store_all_msg_persist(void) {
  703. struct shim_msg_handle* msgq;
  704. struct shim_msg_handle* n;
  705. lock(&msgq_list_lock);
  706. LISTP_FOR_EACH_ENTRY_SAFE(msgq, n, &msgq_list, list) {
  707. if (msgq->owned) {
  708. struct shim_handle* hdl = container_of(msgq, struct shim_handle, info.msg);
  709. lock(&hdl->lock);
  710. __store_msg_persist(msgq);
  711. unlock(&hdl->lock);
  712. }
  713. }
  714. unlock(&msgq_list_lock);
  715. return 0;
  716. }
  717. int shim_do_msgpersist(int msqid, int cmd) {
  718. struct shim_msg_handle* msgq;
  719. struct shim_handle* hdl;
  720. int ret = -EINVAL;
  721. switch (cmd) {
  722. case MSGPERSIST_STORE:
  723. msgq = get_msg_handle_by_id(msqid);
  724. if (!msgq)
  725. return -EINVAL;
  726. hdl = container_of(msgq, struct shim_handle, info.msg);
  727. lock(&hdl->lock);
  728. ret = __store_msg_persist(msgq);
  729. unlock(&hdl->lock);
  730. put_msg_handle(msgq);
  731. break;
  732. case MSGPERSIST_LOAD:
  733. lock(&msgq_list_lock);
  734. ret = __add_msg_handle(0, msqid, false, &msgq);
  735. if (!ret)
  736. ret = __load_msg_persist(msgq, true);
  737. unlock(&msgq_list_lock);
  738. put_msg_handle(msgq);
  739. break;
  740. }
  741. return ret;
  742. }
  743. #if MIGRATE_SYSV_MSG == 1
  744. static int msg_balance_migrate(struct shim_handle* hdl, struct sysv_client* src) {
  745. struct shim_msg_handle* msgq = &hdl->info.msg;
  746. int ret = 0;
  747. debug("trigger msg queue balancing, migrate to process %u\n", src->vmid);
  748. if ((ret = __store_msg_persist(msgq)) < 0)
  749. return 0;
  750. struct shim_ipc_info* info = lookup_ipc_info(src->vmid);
  751. if (!info)
  752. goto failed;
  753. ipc_sysv_sublease_send(src->vmid, msgq->msqid, qstrgetstr(&info->uri), &msgq->lease);
  754. ret = ipc_sysv_msgmov_send(src->port, src->vmid, msgq->msqid, msgq->lease, msgq->scores,
  755. MAX_SYSV_CLIENTS);
  756. if (ret < 0)
  757. goto failed_info;
  758. msgq->owner = info;
  759. for (struct msg_type* mtype = msgq->types; mtype < &msgq->types[msgq->ntypes]; mtype++) {
  760. struct msg_req* req = mtype->reqs;
  761. mtype->reqs = mtype->req_tail = NULL;
  762. while (req) {
  763. struct msg_req* next = req->next;
  764. ipc_sysv_movres_send(&req->dest, info->vmid, qstrgetstr(&info->uri), msgq->lease,
  765. msgq->msqid, SYSV_MSGQ);
  766. put_ipc_port(req->dest.port);
  767. __free_msg_qobj(msgq, req);
  768. req = next;
  769. }
  770. }
  771. ret = 0;
  772. DkEventSet(msgq->event);
  773. goto out;
  774. failed_info:
  775. put_ipc_info(info);
  776. failed:
  777. ret = __load_msg_persist(msgq, true);
  778. out:
  779. return ret;
  780. }
  781. #endif