shim_msgget.c 27 KB

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