shim_msgget.c 26 KB

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