shim_msgget.c 27 KB

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