shim_msgget.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_msgget.c
  17. *
  18. * Implementation of system call "msgget", "msgsnd", "msgrcv" and "msgctl".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_handle.h>
  22. #include <shim_utils.h>
  23. #include <shim_ipc.h>
  24. #include <shim_sysv.h>
  25. #include <shim_unistd.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <list.h>
  30. #include <errno.h>
  31. #define MSGQ_HASH_LEN 8
  32. #define MSGQ_HASH_NUM (1 << MSGQ_HASH_LEN)
  33. #define MSGQ_HASH_MASK (MSGQ_HASH_NUM - 1)
  34. #define MSGQ_HASH(idx) ((idx) & MSGQ_HASH_MASK)
  35. /* The msgq_list links shim_msg_handle objects by the list field.
  36. * The msgq_key_hlist links them by key_hlist, and qid_hlist by qid_hlist */
  37. DEFINE_LISTP(shim_msg_handle);
  38. static LISTP_TYPE(shim_msg_handle) msgq_list;
  39. static LISTP_TYPE(shim_msg_handle) msgq_key_hlist [MSGQ_HASH_NUM];
  40. static LISTP_TYPE(shim_msg_handle) msgq_qid_hlist [MSGQ_HASH_NUM];
  41. static LOCKTYPE msgq_list_lock;
  42. static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg);
  43. static int __store_msg_persist(struct shim_msg_handle * msgq);
  44. DEFINE_PROFILE_CATAGORY(sysv_msg, );
  45. #define MSG_TO_HANDLE(msghdl) \
  46. container_of((msghdl), struct shim_handle, info.msg)
  47. static int __add_msg_handle (unsigned long key, IDTYPE msqid, bool owned,
  48. struct shim_msg_handle ** msghdl)
  49. {
  50. LISTP_TYPE(shim_msg_handle) * key_head = (key != IPC_PRIVATE) ?
  51. &msgq_key_hlist[MSGQ_HASH(key)] :
  52. NULL;
  53. LISTP_TYPE(shim_msg_handle) * qid_head = msqid ?
  54. &msgq_qid_hlist[MSGQ_HASH(msqid)] :
  55. NULL;
  56. struct shim_msg_handle * tmp;
  57. if (key_head)
  58. listp_for_each_entry(tmp, key_head, key_hlist)
  59. if (tmp->msqkey == key) {
  60. if (tmp->msqid == msqid) {
  61. if (msghdl)
  62. *msghdl = tmp;
  63. return 0;
  64. }
  65. return -EEXIST;
  66. }
  67. if (qid_head)
  68. listp_for_each_entry(tmp, qid_head, qid_hlist)
  69. if (tmp->msqid == msqid) {
  70. if (key)
  71. tmp->msqkey = key;
  72. if (msghdl)
  73. *msghdl = tmp;
  74. return 0;
  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. {
  116. lock(msgq_list_lock);
  117. int ret = __add_msg_handle(key, id, owned, NULL);
  118. unlock(msgq_list_lock);
  119. return ret;
  120. }
  121. struct shim_msg_handle * get_msg_handle_by_key (unsigned long key)
  122. {
  123. LISTP_TYPE(shim_msg_handle) * key_head = &msgq_key_hlist[MSGQ_HASH(key)];
  124. struct shim_msg_handle * tmp, * found = NULL;
  125. lock(msgq_list_lock);
  126. listp_for_each_entry(tmp, key_head, key_hlist)
  127. if (tmp->msqkey == key) {
  128. found = tmp;
  129. break;
  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. {
  138. LISTP_TYPE(shim_msg_handle) * qid_head = &msgq_qid_hlist[MSGQ_HASH(msqid)];
  139. struct shim_msg_handle * tmp, * 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. if (found)
  147. get_handle(MSG_TO_HANDLE(found));
  148. unlock(msgq_list_lock);
  149. return found;
  150. }
  151. void put_msg_handle (struct shim_msg_handle * msgq)
  152. {
  153. put_handle(MSG_TO_HANDLE(msgq));
  154. }
  155. static void * __get_msg_qobj (struct shim_msg_handle * msgq)
  156. {
  157. struct msg_qobj * obj = NULL;
  158. if (msgq->freed) {
  159. obj = msgq->freed;
  160. msgq->freed = obj->next;
  161. obj->next = NULL;
  162. return obj;
  163. }
  164. if (msgq->queueused < msgq->queuesize) {
  165. obj = &msgq->queue[msgq->queueused];
  166. msgq->queueused++;
  167. obj->next = NULL;
  168. return obj;
  169. }
  170. return NULL;
  171. }
  172. static void __free_msg_qobj (struct shim_msg_handle * msgq, void * obj)
  173. {
  174. ((struct msg_qobj *) obj)->next = msgq->freed;
  175. msgq->freed = obj;
  176. }
  177. static void __free_msg_linked_qobjs (struct shim_msg_handle * msgq, void * obj)
  178. {
  179. struct msg_qobj * qobj = obj;
  180. while (qobj) {
  181. struct msg_qobj * next = qobj->next;
  182. __free_msg_qobj(msgq, qobj);
  183. qobj = next;
  184. }
  185. }
  186. static int __del_msg_handle (struct shim_msg_handle * msgq)
  187. {
  188. if (msgq->deleted)
  189. return -EIDRM;
  190. msgq->deleted = true;
  191. free(msgq->queue);
  192. msgq->queuesize = 0;
  193. msgq->queueused = 0;
  194. free(msgq->types);
  195. msgq->ntypes = 0;
  196. struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
  197. lock(msgq_list_lock);
  198. listp_del_init(msgq, &msgq_list, list);
  199. put_handle(hdl);
  200. if (!list_empty(msgq, key_hlist)) {
  201. // DEP: Yuck, re-find the head; maybe we can do better...
  202. LISTP_TYPE(shim_msg_handle) * key_head = &msgq_key_hlist[MSGQ_HASH(msgq->msqkey)];
  203. listp_del_init(msgq, key_head, key_hlist);
  204. put_handle(hdl);
  205. }
  206. if (!list_empty(msgq, qid_hlist)) {
  207. // DEP: Yuck, re-find the head; maybe we can do better...
  208. LISTP_TYPE(shim_msg_handle) * qid_head = &msgq_qid_hlist[MSGQ_HASH(msgq->msqid)];
  209. listp_del_init(msgq, qid_head, qid_hlist);
  210. put_handle(hdl);
  211. }
  212. unlock(msgq_list_lock);
  213. return 0;
  214. }
  215. int del_msg_handle (struct shim_msg_handle * msgq)
  216. {
  217. struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
  218. lock(hdl->lock);
  219. int ret = __del_msg_handle(msgq);
  220. unlock(hdl->lock);
  221. return ret;
  222. }
  223. static void __try_create_lock (void)
  224. {
  225. create_lock_runtime(&msgq_list_lock);
  226. }
  227. int shim_do_msgget (key_t key, int msgflg)
  228. {
  229. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  230. IDTYPE msgid = 0;
  231. int ret;
  232. __try_create_lock();
  233. if (key != IPC_PRIVATE) {
  234. struct shim_msg_handle * msgq = get_msg_handle_by_key(key);
  235. if (msgq) {
  236. msgid = msgq->msqid;
  237. put_msg_handle(msgq);
  238. return (msgflg & IPC_EXCL) ? -EEXIST : msgid;
  239. }
  240. }
  241. struct sysv_key k;
  242. k.key = key;
  243. k.type = SYSV_MSGQ;
  244. if (msgflg & IPC_CREAT) {
  245. do {
  246. msgid = allocate_sysv(0, 0);
  247. if (!msgid)
  248. ipc_sysv_lease_send(NULL);
  249. } while (!msgid);
  250. if (key != IPC_PRIVATE) {
  251. if ((ret = ipc_sysv_tellkey_send(NULL, 0, &k, msgid, 0)) < 0) {
  252. release_sysv(msgid);
  253. return ret;
  254. }
  255. }
  256. add_msg_handle(key, msgid, true);
  257. } else {
  258. /* query the manager with the key to find the
  259. corresponding sysvkey */
  260. if ((ret = ipc_sysv_findkey_send(&k)) < 0)
  261. return ret;
  262. msgid = ret;
  263. if ((ret = ipc_sysv_query_send(msgid)) < 0)
  264. return ret;
  265. add_msg_handle(key, msgid, false);
  266. }
  267. return msgid;
  268. }
  269. static int connect_msg_handle (int msqid, struct shim_msg_handle ** msgqp)
  270. {
  271. struct shim_msg_handle * msgq = get_msg_handle_by_id(msqid);
  272. int ret;
  273. if (!msgq) {
  274. if ((ret = ipc_sysv_query_send(msqid)) < 0)
  275. return ret;
  276. if (!msgq) {
  277. lock(msgq_list_lock);
  278. ret = __add_msg_handle(IPC_PRIVATE, msqid, false, &msgq);
  279. unlock(msgq_list_lock);
  280. if (ret < 0)
  281. return ret;
  282. }
  283. }
  284. if (msgq->deleted)
  285. return -EIDRM;
  286. *msgqp = msgq;
  287. return 0;
  288. }
  289. int recover_msg_ownership (struct shim_msg_handle * msgq)
  290. {
  291. struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
  292. lock(hdl->lock);
  293. assert(!msgq->owned);
  294. int ret = __load_msg_persist(msgq, true);
  295. if (ret < 0) {
  296. ret = (ret == -ENOENT) ? -EIDRM : ret;
  297. goto out;
  298. }
  299. msgq->owned = true;
  300. DkEventSet(msgq->event);
  301. out:
  302. unlock(hdl->lock);
  303. return 0;
  304. }
  305. int shim_do_msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg)
  306. {
  307. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  308. int ret;
  309. if ((msgsz < 0) || (msgsz > MSGMAX))
  310. return -EINVAL;
  311. if (!msgp)
  312. return -EFAULT;
  313. struct __kernel_msgbuf * msgbuf = (struct __kernel_msgbuf *) msgp;
  314. if (msgbuf->mtype < 0)
  315. return -EINVAL;
  316. struct shim_msg_handle * msgq;
  317. __try_create_lock();
  318. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  319. return ret;
  320. ret = add_sysv_msg(msgq, msgbuf->mtype, msgsz, msgbuf->mtext, NULL);
  321. put_msg_handle(msgq);
  322. return ret;
  323. }
  324. int shim_do_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtype,
  325. int msgflg)
  326. {
  327. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  328. int ret;
  329. if (msgsz < 0)
  330. return -EINVAL;
  331. if (!msgp)
  332. return -EFAULT;
  333. struct __kernel_msgbuf * msgbuf = (struct __kernel_msgbuf *) msgp;
  334. struct shim_msg_handle * msgq;
  335. __try_create_lock();
  336. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  337. return ret;
  338. ret = get_sysv_msg(msgq, msgtype, msgsz, msgbuf->mtext, msgflg, NULL);
  339. put_msg_handle(msgq);
  340. return ret;
  341. }
  342. int shim_do_msgctl (int msqid, int cmd, struct msqid_ds * buf)
  343. {
  344. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  345. struct shim_msg_handle * msgq;
  346. int ret;
  347. __try_create_lock();
  348. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  349. return ret;
  350. switch (cmd) {
  351. case IPC_RMID:
  352. if (!msgq->owned) {
  353. ret = ipc_sysv_delres_send(NULL, 0, msgq->msqid, SYSV_MSGQ);
  354. if (ret < 0)
  355. break;
  356. }
  357. __del_msg_handle(msgq);
  358. break;
  359. default:
  360. ret = -ENOSYS;
  361. break;
  362. }
  363. put_msg_handle(msgq);
  364. return ret;
  365. }
  366. static struct msg_type *
  367. __add_msg_type (int type, struct msg_type ** ptypes, int * pntypes,
  368. int * pmaxtypes)
  369. {
  370. struct msg_type * types = *ptypes;
  371. int ntypes = *pntypes;
  372. int maxtypes = *pmaxtypes;
  373. struct msg_type * mtype;
  374. for (mtype = types ;
  375. mtype < &types[ntypes] && mtype->type <= type ; mtype++)
  376. if (mtype->type == type)
  377. return mtype;
  378. int off = mtype - types;
  379. struct msg_type * new_types = types;
  380. if (ntypes == maxtypes)
  381. new_types = malloc(sizeof(struct msg_type) * maxtypes * 2);
  382. if (mtype < &types[ntypes])
  383. memmove(new_types + off + 1, mtype,
  384. sizeof(struct msg_type) * (ntypes - off));
  385. if (new_types != types) {
  386. memcpy(new_types, types, sizeof(struct msg_type) * off);
  387. free(types);
  388. mtype = new_types + off;
  389. *ptypes = new_types;
  390. *pmaxtypes = maxtypes * 2;
  391. }
  392. mtype->type = type;
  393. mtype->msgs = NULL;
  394. mtype->msg_tail = NULL;
  395. mtype->reqs = NULL;
  396. mtype->req_tail = NULL;
  397. (*pntypes)++;
  398. return mtype;
  399. }
  400. static int __load_msg_qobjs (struct shim_msg_handle * msgq,
  401. struct msg_type * mtype,
  402. struct msg_item * msg, void * data)
  403. {
  404. int copysize = MSG_ITEM_DATA_SIZE(msg->size);
  405. memcpy(data, msg->data, copysize);
  406. mtype->msgs = msg->next;
  407. __free_msg_qobj(msgq, msg);
  408. while (copysize < msg->size) {
  409. assert(mtype->msgs);
  410. struct msg_ext_item * ext = (struct msg_ext_item *) mtype->msgs;
  411. int sz = MSG_EXT_ITEM_DATA_SIZE(msg->size - copysize);
  412. memcpy(data + copysize, ext->data, sz);
  413. copysize += sz;
  414. mtype->msgs = ext->next;
  415. __free_msg_qobj(msgq, ext);
  416. }
  417. if (!mtype->msgs)
  418. mtype->msg_tail = NULL;
  419. msgq->nmsgs--;
  420. msgq->currentsize -= msg->size;
  421. return 0;
  422. }
  423. static int __store_msg_qobjs (struct shim_msg_handle * msgq,
  424. struct msg_type * mtype,
  425. int size, const void * data)
  426. {
  427. struct msg_item * newmsg = __get_msg_qobj(msgq);
  428. if (!newmsg)
  429. return -EAGAIN;
  430. struct msg_item * old_tail = mtype->msg_tail;
  431. newmsg->next = NULL;
  432. newmsg->size = size;
  433. int copysize = MSG_ITEM_DATA_SIZE(size);
  434. memcpy(newmsg->data, data, copysize);
  435. if (mtype->msg_tail) {
  436. mtype->msg_tail->next = newmsg;
  437. mtype->msg_tail = newmsg;
  438. } else {
  439. assert(!mtype->msgs);
  440. mtype->msgs = mtype->msg_tail = newmsg;
  441. }
  442. while (copysize < size) {
  443. struct msg_ext_item * ext = __get_msg_qobj(msgq);
  444. if (!ext)
  445. goto eagain;
  446. int sz = MSG_EXT_ITEM_DATA_SIZE(size - copysize);
  447. memcpy(ext->data, data + copysize, sz);
  448. ext->next = NULL;
  449. mtype->msg_tail->next = ext;
  450. mtype->msg_tail = (struct msg_item *) ext;
  451. copysize += sz;
  452. }
  453. msgq->nmsgs++;
  454. msgq->currentsize += size;
  455. return 0;
  456. eagain:
  457. __free_msg_linked_qobjs(msgq, newmsg);
  458. if (mtype->msgs == newmsg)
  459. mtype->msgs = NULL;
  460. mtype->msg_tail = old_tail;
  461. return -EAGAIN;
  462. }
  463. static int msg_balance_migrate (struct shim_handle * hdl,
  464. struct sysv_client * client);
  465. static struct sysv_balance_policy msg_policy = {
  466. .score_decay = MSG_SCORE_DECAY,
  467. .score_max = MSG_SCORE_MAX,
  468. .balance_threshold = MSG_BALANCE_THRESHOLD,
  469. .migrate = &msg_balance_migrate,
  470. };
  471. DEFINE_PROFILE_INTERVAL(add_sysv_msg, sysv_msg);
  472. int add_sysv_msg (struct shim_msg_handle * msgq,
  473. long type, int 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 (msgq->owned)
  496. __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS,
  497. src, MSG_SND_SCORE);
  498. DkEventSet(msgq->event);
  499. ret = 0;
  500. out_locked:
  501. unlock(hdl->lock);
  502. out:
  503. SAVE_PROFILE_INTERVAL(add_sysv_msg);
  504. return ret;
  505. }
  506. static struct msg_type *
  507. __find_msg_type (int type, struct msg_type * types, int ntypes)
  508. {
  509. for (struct msg_type * mtype = types ;
  510. mtype < &types[ntypes] && mtype->type <= type; mtype++)
  511. if (mtype->type == type)
  512. return mtype;
  513. return NULL;
  514. }
  515. static int __add_msg_req (struct shim_msg_handle * msgq,
  516. struct msg_type * mtype,
  517. int size, int flags, struct sysv_client * src)
  518. {
  519. if (msgq->deleted)
  520. return -EIDRM;
  521. struct msg_req * req = __get_msg_qobj(msgq);
  522. if (!req)
  523. return -ENOMEM;
  524. get_ipc_port(src->port);
  525. req->next = NULL;
  526. req->size = size;
  527. req->flags = flags;
  528. req->dest = *src;
  529. if (mtype->req_tail) {
  530. mtype->req_tail->next = req;
  531. mtype->req_tail = req;
  532. } else {
  533. assert(!mtype->reqs);
  534. mtype->reqs = mtype->req_tail = req;
  535. }
  536. return 0;
  537. }
  538. DEFINE_PROFILE_INTERVAL(get_sysv_msg, sysv_msg);
  539. int get_sysv_msg (struct shim_msg_handle * msgq,
  540. long type, int size, void * data, int flags,
  541. struct sysv_client * src)
  542. {
  543. BEGIN_PROFILE_INTERVAL();
  544. int ret = 0;
  545. struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
  546. struct msg_item * msg = NULL;
  547. struct msg_type * alltypes = NULL, * mtype = NULL;
  548. lock(hdl->lock);
  549. if (msgq->deleted) {
  550. ret = -EIDRM;
  551. goto out_locked;
  552. }
  553. if (msgq->owned) {
  554. __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS,
  555. src, MSG_RCV_SCORE);
  556. if (!msgq->owned && src) {
  557. struct shim_ipc_info * owner = msgq->owner;
  558. assert(owner);
  559. ret = ipc_sysv_movres_send(src, owner->vmid,
  560. qstrgetstr(&owner->uri), msgq->lease,
  561. msgq->msqid, SYSV_MSGQ);
  562. goto out_locked;
  563. }
  564. }
  565. if (!msgq->owned) {
  566. IDTYPE msqid = msgq->msqid;
  567. if (src) {
  568. struct shim_ipc_info * owner = msgq->owner;
  569. ret = owner ?
  570. ipc_sysv_movres_send(src, owner->vmid,
  571. qstrgetstr(&owner->uri), msgq->lease,
  572. msgq->msqid, SYSV_MSGQ) :
  573. -ECONNREFUSED;
  574. goto out_locked;
  575. }
  576. unowned:
  577. unlock(hdl->lock);
  578. ret = ipc_sysv_msgrcv_send(msqid, type, flags, data, size);
  579. if (ret != -EAGAIN &&
  580. ret != -ECONNREFUSED)
  581. goto out;
  582. lock(hdl->lock);
  583. if (!msgq->owned)
  584. goto out_locked;
  585. }
  586. while (1) {
  587. if (alltypes != msgq->types || !mtype || mtype->type != type) {
  588. alltypes = msgq->types;
  589. mtype = __find_msg_type(type, alltypes, msgq->ntypes);
  590. }
  591. if (mtype && mtype->msgs) {
  592. msg = mtype->msgs;
  593. if (msg->size > size && !(flags & MSG_NOERROR)) {
  594. ret = -E2BIG;
  595. goto out;
  596. }
  597. break;
  598. }
  599. if (flags & IPC_NOWAIT || src)
  600. break;
  601. unlock(hdl->lock);
  602. while (!DkObjectsWaitAny(1, &msgq->event, NO_TIMEOUT));
  603. lock(hdl->lock);
  604. if (!msgq->owned)
  605. goto unowned;
  606. }
  607. if (!msg) {
  608. ret = (!(flags & IPC_NOWAIT) && src) ?
  609. __add_msg_req(msgq, mtype, size, flags, src) : -ENOMSG;
  610. goto out_locked;
  611. }
  612. if ((ret = __load_msg_qobjs(msgq, mtype, msg, data)) < 0)
  613. goto out_locked;;
  614. ret = msg->size;
  615. out_locked:
  616. unlock(hdl->lock);
  617. out:
  618. SAVE_PROFILE_INTERVAL(get_sysv_msg);
  619. return ret;
  620. }
  621. static int __store_msg_persist (struct shim_msg_handle * msgq)
  622. {
  623. int ret = 0;
  624. if (msgq->deleted)
  625. goto out;
  626. debug("store msgq %d to persistent store\n", msgq->msqid);
  627. char fileuri[20];
  628. snprintf(fileuri, 20, "file:msgq.%08x", msgq->msqid);
  629. PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDWR, 0600,
  630. PAL_CREAT_TRY, 0);
  631. if (!file) {
  632. ret = -PAL_ERRNO;
  633. goto out;
  634. }
  635. int expected_size = sizeof(struct msg_handle_backup) +
  636. sizeof(struct msg_backup) * msgq->nmsgs +
  637. msgq->currentsize;
  638. if (DkStreamSetLength(file, expected_size) != expected_size)
  639. goto err_file;
  640. void * mem = (void *) DkStreamMap(file, NULL,
  641. PAL_PROT_READ|PAL_PROT_WRITE,
  642. 0, ALIGN_UP(expected_size));
  643. if (!mem) {
  644. ret = -EFAULT;
  645. goto err_file;
  646. }
  647. struct msg_handle_backup * mback = mem;
  648. mem += sizeof(struct msg_handle_backup);
  649. mback->perm = msgq->perm;
  650. mback->nmsgs = msgq->nmsgs;
  651. mback->currentsize = msgq->currentsize;
  652. struct msg_type * mtype;
  653. for (mtype = msgq->types ; mtype < &msgq->types[msgq->ntypes] ;
  654. mtype++) {
  655. while (mtype->msgs) {
  656. struct msg_backup * msg = mem;
  657. mem += sizeof(struct msg_backup) + mtype->msgs->size;
  658. msg->type = mtype->type;
  659. msg->size = mtype->msgs->size;
  660. __load_msg_qobjs(msgq, mtype, mtype->msgs, msg->data);
  661. }
  662. mtype->msgs = mtype->msg_tail = NULL;
  663. }
  664. DkStreamUnmap(mem, ALIGN_UP(expected_size));
  665. if (msgq->owned)
  666. for (mtype = msgq->types ; mtype < &msgq->types[msgq->ntypes] ;
  667. mtype++) {
  668. struct msg_req * req = mtype->reqs;
  669. mtype->reqs = mtype->req_tail = NULL;
  670. while (req) {
  671. struct sysv_client * c = &req->dest;
  672. struct msg_req * next = req->next;
  673. __response_ipc_message(c->port, c->vmid, -EIDRM, c->seq);
  674. put_ipc_port(c->port);
  675. __free_msg_qobj(msgq, req);
  676. req = next;
  677. }
  678. }
  679. msgq->owned = false;
  680. ret = 0;
  681. goto out;
  682. err_file:
  683. DkStreamDelete(file, 0);
  684. DkObjectClose(file);
  685. out:
  686. // To wake up any receiver waiting on local message which must
  687. // now be requested from new owner.
  688. DkEventSet(msgq->event);
  689. return ret;
  690. }
  691. static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg)
  692. {
  693. int ret = 0;
  694. char fileuri[20];
  695. snprintf(fileuri, 20, "file:msgq.%08x", msgq->msqid);
  696. PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDONLY, 0, 0, 0);
  697. if (!file)
  698. return -EIDRM;
  699. struct msg_handle_backup mback;
  700. int bytes = DkStreamRead(file, 0, sizeof(struct msg_handle_backup),
  701. &mback, NULL, 0);
  702. if (bytes < sizeof(struct msg_handle_backup)) {
  703. ret = bytes ? -EFAULT : -PAL_ERRNO;
  704. goto out;
  705. }
  706. msgq->perm = mback.perm;
  707. if (!readmsg || !mback.nmsgs)
  708. goto done;
  709. int expected_size = sizeof(struct msg_handle_backup) +
  710. sizeof(struct msg_backup) * mback.nmsgs +
  711. mback.currentsize;
  712. void * mem = (void *) DkStreamMap(file, NULL, PAL_PROT_READ, 0,
  713. ALIGN_UP(expected_size));
  714. if (!mem) {
  715. ret = -PAL_ERRNO;
  716. goto out;
  717. }
  718. mem += sizeof(struct msg_handle_backup);
  719. struct msg_type * mtype = NULL;
  720. for (int i = 0 ; i < mback.nmsgs ; i++) {
  721. struct msg_backup * m = mem;
  722. mem += sizeof(struct msg_backup) + m->size;
  723. debug("load msg: type=%d, size=%d\n", m->type, m->size);
  724. if (!mtype || mtype->type != m->type)
  725. mtype = __add_msg_type(m->type, &msgq->types, &msgq->ntypes,
  726. &msgq->maxtypes);
  727. if ((ret = __store_msg_qobjs(msgq, mtype, m->size, m->data)) < 0)
  728. goto out;
  729. };
  730. DkStreamUnmap(mem, ALIGN_UP(expected_size));
  731. done:
  732. DkStreamDelete(file, 0);
  733. ret = 0;
  734. goto out;
  735. out:
  736. DkObjectClose(file);
  737. return ret;
  738. }
  739. int store_all_msg_persist (void)
  740. {
  741. struct shim_msg_handle * msgq, *n;
  742. lock(msgq_list_lock);
  743. listp_for_each_entry_safe(msgq, n, &msgq_list, list)
  744. if (msgq->owned) {
  745. struct shim_handle * hdl = container_of(msgq, struct shim_handle,
  746. info.msg);
  747. lock(hdl->lock);
  748. __store_msg_persist(msgq);
  749. unlock(hdl->lock);
  750. }
  751. unlock(msgq_list_lock);
  752. return 0;
  753. }
  754. int shim_do_msgpersist (int msqid, int cmd)
  755. {
  756. struct shim_msg_handle * msgq;
  757. struct shim_handle * hdl;
  758. int ret = -EINVAL;
  759. switch (cmd) {
  760. case MSGPERSIST_STORE:
  761. msgq = get_msg_handle_by_id(msqid);
  762. if (!msgq)
  763. return -EINVAL;
  764. hdl = container_of(msgq, struct shim_handle, info.msg);
  765. lock(hdl->lock);
  766. ret = __store_msg_persist(msgq);
  767. unlock(hdl->lock);
  768. put_msg_handle(msgq);
  769. break;
  770. case MSGPERSIST_LOAD:
  771. lock(msgq_list_lock);
  772. ret = __add_msg_handle(0, msqid, false, &msgq);
  773. if (!ret)
  774. ret = __load_msg_persist(msgq, true);
  775. unlock(msgq_list_lock);
  776. put_msg_handle(msgq);
  777. break;
  778. }
  779. return ret;
  780. }
  781. static int msg_balance_migrate (struct shim_handle * hdl,
  782. struct sysv_client * src)
  783. {
  784. struct shim_msg_handle * msgq = &hdl->info.msg;
  785. int ret = 0;
  786. debug("trigger msg queue balancing, migrate to process %u\n", src->vmid);
  787. if ((ret = __store_msg_persist(msgq)) < 0)
  788. return 0;
  789. struct shim_ipc_info * info = discover_client(src->port, src->vmid);
  790. if (!info)
  791. goto failed;
  792. ipc_sysv_sublease_send(src->vmid, msgq->msqid,
  793. qstrgetstr(&info->uri),
  794. &msgq->lease);
  795. ret = ipc_sysv_msgmov_send(src->port, src->vmid, msgq->msqid, msgq->lease,
  796. msgq->scores, MAX_SYSV_CLIENTS);
  797. if (ret < 0)
  798. goto failed_info;
  799. msgq->owner = info;
  800. for (struct msg_type * mtype = msgq->types ;
  801. mtype < &msgq->types[msgq->ntypes] ; mtype++) {
  802. struct msg_req * req = mtype->reqs;
  803. mtype->reqs = mtype->req_tail = NULL;
  804. while (req) {
  805. struct msg_req * next = req->next;
  806. ipc_sysv_movres_send(&req->dest, info->vmid, qstrgetstr(&info->uri),
  807. msgq->lease, msgq->msqid, SYSV_MSGQ);
  808. put_ipc_port(req->dest.port);
  809. __free_msg_qobj(msgq, req);
  810. req = next;
  811. }
  812. }
  813. ret = 0;
  814. DkEventSet(msgq->event);
  815. goto out;
  816. failed_info:
  817. put_ipc_info(info);
  818. failed:
  819. ret = __load_msg_persist(msgq, true);
  820. out:
  821. return ret;
  822. }