shim_msgget.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. // Issue #755 - https://github.com/oscarlab/graphene/issues/755
  307. __UNUSED(msgflg);
  308. int ret;
  309. if (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. // Issue #755 - https://github.com/oscarlab/graphene/issues/755
  329. __UNUSED(msgflg);
  330. int ret;
  331. if (msgsz > MSGMAX)
  332. return -EINVAL;
  333. if (!msgp)
  334. return -EFAULT;
  335. struct __kernel_msgbuf * msgbuf = (struct __kernel_msgbuf *) msgp;
  336. struct shim_msg_handle * msgq;
  337. __try_create_lock();
  338. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  339. return ret;
  340. ret = get_sysv_msg(msgq, msgtype, msgsz, msgbuf->mtext, msgflg, NULL);
  341. put_msg_handle(msgq);
  342. return ret;
  343. }
  344. int shim_do_msgctl (int msqid, int cmd, struct msqid_ds * buf)
  345. {
  346. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  347. // Issue #756 - https://github.com/oscarlab/graphene/issues/756
  348. __UNUSED(buf);
  349. struct shim_msg_handle * msgq;
  350. int ret;
  351. __try_create_lock();
  352. if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
  353. return ret;
  354. switch (cmd) {
  355. case IPC_RMID:
  356. if (!msgq->owned) {
  357. ret = ipc_sysv_delres_send(NULL, 0, msgq->msqid, SYSV_MSGQ);
  358. if (ret < 0)
  359. break;
  360. }
  361. __del_msg_handle(msgq);
  362. break;
  363. default:
  364. ret = -ENOSYS;
  365. break;
  366. }
  367. put_msg_handle(msgq);
  368. return ret;
  369. }
  370. static struct msg_type *
  371. __add_msg_type (int type, struct msg_type ** ptypes, int * pntypes,
  372. int * pmaxtypes)
  373. {
  374. struct msg_type * types = *ptypes;
  375. int ntypes = *pntypes;
  376. int maxtypes = *pmaxtypes;
  377. struct msg_type * mtype;
  378. for (mtype = types ;
  379. mtype < &types[ntypes] && mtype->type <= type ; mtype++)
  380. if (mtype->type == type)
  381. return mtype;
  382. int off = mtype - types;
  383. struct msg_type * new_types = types;
  384. if (ntypes == maxtypes)
  385. new_types = malloc(sizeof(struct msg_type) * maxtypes * 2);
  386. if (mtype < &types[ntypes])
  387. memmove(new_types + off + 1, mtype,
  388. sizeof(struct msg_type) * (ntypes - off));
  389. if (new_types != types) {
  390. memcpy(new_types, types, sizeof(struct msg_type) * off);
  391. free(types);
  392. mtype = new_types + off;
  393. *ptypes = new_types;
  394. *pmaxtypes = maxtypes * 2;
  395. }
  396. mtype->type = type;
  397. mtype->msgs = NULL;
  398. mtype->msg_tail = NULL;
  399. mtype->reqs = NULL;
  400. mtype->req_tail = NULL;
  401. (*pntypes)++;
  402. return mtype;
  403. }
  404. static int __load_msg_qobjs (struct shim_msg_handle * msgq,
  405. struct msg_type * mtype,
  406. struct msg_item * msg, void * data)
  407. {
  408. size_t copysize = MSG_ITEM_DATA_SIZE(msg->size);
  409. memcpy(data, msg->data, copysize);
  410. mtype->msgs = msg->next;
  411. __free_msg_qobj(msgq, msg);
  412. while (copysize < msg->size) {
  413. assert(mtype->msgs);
  414. struct msg_ext_item * ext = (struct msg_ext_item *) mtype->msgs;
  415. size_t sz = MSG_EXT_ITEM_DATA_SIZE(msg->size - copysize);
  416. memcpy(data + copysize, ext->data, sz);
  417. copysize += sz;
  418. mtype->msgs = ext->next;
  419. __free_msg_qobj(msgq, ext);
  420. }
  421. if (!mtype->msgs)
  422. mtype->msg_tail = NULL;
  423. msgq->nmsgs--;
  424. msgq->currentsize -= msg->size;
  425. return 0;
  426. }
  427. static int __store_msg_qobjs (struct shim_msg_handle * msgq,
  428. struct msg_type * mtype,
  429. size_t size, const void * data)
  430. {
  431. struct msg_item * newmsg = __get_msg_qobj(msgq);
  432. if (!newmsg)
  433. return -EAGAIN;
  434. struct msg_item * old_tail = mtype->msg_tail;
  435. newmsg->next = NULL;
  436. newmsg->size = size;
  437. size_t copysize = MSG_ITEM_DATA_SIZE(size);
  438. memcpy(newmsg->data, data, copysize);
  439. if (mtype->msg_tail) {
  440. mtype->msg_tail->next = newmsg;
  441. mtype->msg_tail = newmsg;
  442. } else {
  443. assert(!mtype->msgs);
  444. mtype->msgs = mtype->msg_tail = newmsg;
  445. }
  446. while (copysize < size) {
  447. struct msg_ext_item * ext = __get_msg_qobj(msgq);
  448. if (!ext)
  449. goto eagain;
  450. size_t sz = MSG_EXT_ITEM_DATA_SIZE(size - copysize);
  451. memcpy(ext->data, data + copysize, sz);
  452. ext->next = NULL;
  453. mtype->msg_tail->next = ext;
  454. mtype->msg_tail = (struct msg_item *) ext;
  455. copysize += sz;
  456. }
  457. msgq->nmsgs++;
  458. msgq->currentsize += size;
  459. return 0;
  460. eagain:
  461. __free_msg_linked_qobjs(msgq, newmsg);
  462. if (mtype->msgs == newmsg)
  463. mtype->msgs = NULL;
  464. mtype->msg_tail = old_tail;
  465. return -EAGAIN;
  466. }
  467. #if MIGRATE_SYSV_MSG == 1
  468. static int msg_balance_migrate (struct shim_handle * hdl,
  469. struct sysv_client * client);
  470. static struct sysv_balance_policy msg_policy = {
  471. .score_decay = MSG_SCORE_DECAY,
  472. .score_max = MSG_SCORE_MAX,
  473. .balance_threshold = MSG_BALANCE_THRESHOLD,
  474. .migrate = &msg_balance_migrate,
  475. };
  476. #endif
  477. DEFINE_PROFILE_INTERVAL(add_sysv_msg, sysv_msg);
  478. int add_sysv_msg (struct shim_msg_handle * msgq,
  479. long type, size_t size, const void * data,
  480. struct sysv_client * src)
  481. {
  482. BEGIN_PROFILE_INTERVAL();
  483. struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
  484. int ret = 0;
  485. lock(&hdl->lock);
  486. if (msgq->deleted) {
  487. ret = -EIDRM;
  488. goto out_locked;
  489. }
  490. if (!msgq->owned) {
  491. unlock(&hdl->lock);
  492. ret = ipc_sysv_msgsnd_send(src->port, src->vmid, msgq->msqid,
  493. type, data, size, src->seq);
  494. goto out;
  495. }
  496. struct msg_type * mtype = __add_msg_type(type, &msgq->types,
  497. &msgq->ntypes,
  498. &msgq->maxtypes);
  499. if ((ret = __store_msg_qobjs(msgq, mtype, size, data)) < 0)
  500. goto out_locked;
  501. #if MIGRATE_SYSV_MSG == 1
  502. if (msgq->owned)
  503. __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS,
  504. src, MSG_SND_SCORE);
  505. #endif
  506. DkEventSet(msgq->event);
  507. ret = 0;
  508. out_locked:
  509. unlock(&hdl->lock);
  510. out:
  511. SAVE_PROFILE_INTERVAL(add_sysv_msg);
  512. return ret;
  513. }
  514. static struct msg_type *
  515. __find_msg_type (int type, struct msg_type * types, int ntypes)
  516. {
  517. for (struct msg_type * mtype = types ;
  518. mtype < &types[ntypes] && mtype->type <= type; mtype++)
  519. if (mtype->type == type)
  520. return mtype;
  521. return NULL;
  522. }
  523. static int __add_msg_req (struct shim_msg_handle * msgq,
  524. struct msg_type * mtype,
  525. int size, int flags, struct sysv_client * src)
  526. {
  527. if (msgq->deleted)
  528. return -EIDRM;
  529. struct msg_req * req = __get_msg_qobj(msgq);
  530. if (!req)
  531. return -ENOMEM;
  532. get_ipc_port(src->port);
  533. req->next = NULL;
  534. req->size = size;
  535. req->flags = flags;
  536. req->dest = *src;
  537. if (mtype->req_tail) {
  538. mtype->req_tail->next = req;
  539. mtype->req_tail = req;
  540. } else {
  541. assert(!mtype->reqs);
  542. mtype->reqs = mtype->req_tail = req;
  543. }
  544. return 0;
  545. }
  546. DEFINE_PROFILE_INTERVAL(get_sysv_msg, sysv_msg);
  547. int get_sysv_msg (struct shim_msg_handle * msgq,
  548. long type, size_t size, void * data, int flags,
  549. struct sysv_client * src)
  550. {
  551. BEGIN_PROFILE_INTERVAL();
  552. int ret = 0;
  553. struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
  554. struct msg_item * msg = NULL;
  555. struct msg_type * alltypes = NULL, * mtype = NULL;
  556. lock(&hdl->lock);
  557. if (msgq->deleted) {
  558. ret = -EIDRM;
  559. goto out_locked;
  560. }
  561. #if MIGRATE_SYSV_MSG == 1
  562. if (msgq->owned) {
  563. __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS,
  564. src, MSG_RCV_SCORE);
  565. if (!msgq->owned && src) {
  566. struct shim_ipc_info * owner = msgq->owner;
  567. assert(owner);
  568. ret = ipc_sysv_movres_send(src, owner->vmid,
  569. qstrgetstr(&owner->uri), msgq->lease,
  570. msgq->msqid, SYSV_MSGQ);
  571. goto out_locked;
  572. }
  573. }
  574. #endif
  575. if (!msgq->owned) {
  576. IDTYPE msqid = msgq->msqid;
  577. if (src) {
  578. struct shim_ipc_info * owner = msgq->owner;
  579. ret = owner ?
  580. ipc_sysv_movres_send(src, owner->vmid,
  581. qstrgetstr(&owner->uri), msgq->lease,
  582. msgq->msqid, SYSV_MSGQ) :
  583. -ECONNREFUSED;
  584. goto out_locked;
  585. }
  586. unowned:
  587. unlock(&hdl->lock);
  588. ret = ipc_sysv_msgrcv_send(msqid, type, flags, data, size);
  589. if (ret != -EAGAIN &&
  590. ret != -ECONNREFUSED)
  591. goto out;
  592. lock(&hdl->lock);
  593. if (!msgq->owned)
  594. goto out_locked;
  595. }
  596. while (1) {
  597. if (alltypes != msgq->types || !mtype || mtype->type != type) {
  598. alltypes = msgq->types;
  599. mtype = __find_msg_type(type, alltypes, msgq->ntypes);
  600. }
  601. if (mtype && mtype->msgs) {
  602. msg = mtype->msgs;
  603. if (msg->size > size && !(flags & MSG_NOERROR)) {
  604. ret = -E2BIG;
  605. goto out;
  606. }
  607. break;
  608. }
  609. if (flags & IPC_NOWAIT || src)
  610. break;
  611. unlock(&hdl->lock);
  612. while (!DkObjectsWaitAny(1, &msgq->event, NO_TIMEOUT));
  613. lock(&hdl->lock);
  614. if (!msgq->owned)
  615. goto unowned;
  616. }
  617. if (!msg) {
  618. ret = (!(flags & IPC_NOWAIT) && src) ?
  619. __add_msg_req(msgq, mtype, size, flags, src) : -ENOMSG;
  620. goto out_locked;
  621. }
  622. if ((ret = __load_msg_qobjs(msgq, mtype, msg, data)) < 0)
  623. goto out_locked;;
  624. ret = msg->size;
  625. out_locked:
  626. unlock(&hdl->lock);
  627. out:
  628. SAVE_PROFILE_INTERVAL(get_sysv_msg);
  629. return ret;
  630. }
  631. static int __store_msg_persist (struct shim_msg_handle * msgq)
  632. {
  633. int ret = 0;
  634. if (msgq->deleted)
  635. goto out;
  636. debug("store msgq %d to persistent store\n", msgq->msqid);
  637. char fileuri[20];
  638. snprintf(fileuri, 20, "file:msgq.%08x", msgq->msqid);
  639. PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDWR, 0600,
  640. PAL_CREATE_TRY, 0);
  641. if (!file) {
  642. ret = -PAL_ERRNO;
  643. goto out;
  644. }
  645. int expected_size = sizeof(struct msg_handle_backup) +
  646. sizeof(struct msg_backup) * msgq->nmsgs +
  647. msgq->currentsize;
  648. if (DkStreamSetLength(file, expected_size))
  649. goto err_file;
  650. void * mem = (void *) DkStreamMap(file, NULL,
  651. PAL_PROT_READ|PAL_PROT_WRITE,
  652. 0, ALIGN_UP(expected_size));
  653. if (!mem) {
  654. ret = -EFAULT;
  655. goto err_file;
  656. }
  657. struct msg_handle_backup * mback = mem;
  658. mem += sizeof(struct msg_handle_backup);
  659. mback->perm = msgq->perm;
  660. mback->nmsgs = msgq->nmsgs;
  661. mback->currentsize = msgq->currentsize;
  662. struct msg_type * mtype;
  663. for (mtype = msgq->types ; mtype < &msgq->types[msgq->ntypes] ;
  664. mtype++) {
  665. while (mtype->msgs) {
  666. struct msg_backup * msg = mem;
  667. mem += sizeof(struct msg_backup) + mtype->msgs->size;
  668. msg->type = mtype->type;
  669. msg->size = mtype->msgs->size;
  670. __load_msg_qobjs(msgq, mtype, mtype->msgs, msg->data);
  671. }
  672. mtype->msgs = mtype->msg_tail = NULL;
  673. }
  674. DkStreamUnmap(mem, ALIGN_UP(expected_size));
  675. if (msgq->owned)
  676. for (mtype = msgq->types ; mtype < &msgq->types[msgq->ntypes] ;
  677. mtype++) {
  678. struct msg_req * req = mtype->reqs;
  679. mtype->reqs = mtype->req_tail = NULL;
  680. while (req) {
  681. struct sysv_client * c = &req->dest;
  682. struct msg_req * next = req->next;
  683. send_response_ipc_message(c->port, c->vmid, -EIDRM, c->seq);
  684. put_ipc_port(c->port);
  685. __free_msg_qobj(msgq, req);
  686. req = next;
  687. }
  688. }
  689. msgq->owned = false;
  690. ret = 0;
  691. goto out;
  692. err_file:
  693. DkStreamDelete(file, 0);
  694. DkObjectClose(file);
  695. out:
  696. // To wake up any receiver waiting on local message which must
  697. // now be requested from new owner.
  698. DkEventSet(msgq->event);
  699. return ret;
  700. }
  701. static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg)
  702. {
  703. int ret = 0;
  704. char fileuri[20];
  705. snprintf(fileuri, 20, "file:msgq.%08x", msgq->msqid);
  706. PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDONLY, 0, 0, 0);
  707. if (!file)
  708. return -EIDRM;
  709. struct msg_handle_backup mback;
  710. size_t bytes = DkStreamRead(file, 0, sizeof(struct msg_handle_backup),
  711. &mback, NULL, 0);
  712. if (bytes < sizeof(struct msg_handle_backup)) {
  713. ret = bytes ? -EFAULT : -PAL_ERRNO;
  714. goto out;
  715. }
  716. msgq->perm = mback.perm;
  717. if (!readmsg || !mback.nmsgs)
  718. goto done;
  719. int expected_size = sizeof(struct msg_handle_backup) +
  720. sizeof(struct msg_backup) * mback.nmsgs +
  721. mback.currentsize;
  722. void * mem = (void *) DkStreamMap(file, NULL, PAL_PROT_READ, 0,
  723. ALIGN_UP(expected_size));
  724. if (!mem) {
  725. ret = -PAL_ERRNO;
  726. goto out;
  727. }
  728. mem += sizeof(struct msg_handle_backup);
  729. struct msg_type * mtype = NULL;
  730. for (int i = 0 ; i < mback.nmsgs ; i++) {
  731. struct msg_backup * m = mem;
  732. mem += sizeof(struct msg_backup) + m->size;
  733. debug("load msg: type=%ld, size=%d\n", m->type, m->size);
  734. if (!mtype || mtype->type != m->type)
  735. mtype = __add_msg_type(m->type, &msgq->types, &msgq->ntypes,
  736. &msgq->maxtypes);
  737. if ((ret = __store_msg_qobjs(msgq, mtype, m->size, m->data)) < 0)
  738. goto out;
  739. };
  740. DkStreamUnmap(mem, ALIGN_UP(expected_size));
  741. done:
  742. DkStreamDelete(file, 0);
  743. ret = 0;
  744. goto out;
  745. out:
  746. DkObjectClose(file);
  747. return ret;
  748. }
  749. int store_all_msg_persist (void)
  750. {
  751. struct shim_msg_handle * msgq, *n;
  752. lock(&msgq_list_lock);
  753. LISTP_FOR_EACH_ENTRY_SAFE(msgq, n, &msgq_list, list)
  754. if (msgq->owned) {
  755. struct shim_handle * hdl = container_of(msgq, struct shim_handle,
  756. info.msg);
  757. lock(&hdl->lock);
  758. __store_msg_persist(msgq);
  759. unlock(&hdl->lock);
  760. }
  761. unlock(&msgq_list_lock);
  762. return 0;
  763. }
  764. int shim_do_msgpersist (int msqid, int cmd)
  765. {
  766. struct shim_msg_handle * msgq;
  767. struct shim_handle * hdl;
  768. int ret = -EINVAL;
  769. switch (cmd) {
  770. case MSGPERSIST_STORE:
  771. msgq = get_msg_handle_by_id(msqid);
  772. if (!msgq)
  773. return -EINVAL;
  774. hdl = container_of(msgq, struct shim_handle, info.msg);
  775. lock(&hdl->lock);
  776. ret = __store_msg_persist(msgq);
  777. unlock(&hdl->lock);
  778. put_msg_handle(msgq);
  779. break;
  780. case MSGPERSIST_LOAD:
  781. lock(&msgq_list_lock);
  782. ret = __add_msg_handle(0, msqid, false, &msgq);
  783. if (!ret)
  784. ret = __load_msg_persist(msgq, true);
  785. unlock(&msgq_list_lock);
  786. put_msg_handle(msgq);
  787. break;
  788. }
  789. return ret;
  790. }
  791. #if MIGRATE_SYSV_MSG == 1
  792. static int msg_balance_migrate (struct shim_handle * hdl,
  793. struct sysv_client * src)
  794. {
  795. struct shim_msg_handle * msgq = &hdl->info.msg;
  796. int ret = 0;
  797. debug("trigger msg queue balancing, migrate to process %u\n", src->vmid);
  798. if ((ret = __store_msg_persist(msgq)) < 0)
  799. return 0;
  800. struct shim_ipc_info * info = lookup_ipc_info(src->vmid);
  801. if (!info)
  802. goto failed;
  803. ipc_sysv_sublease_send(src->vmid, msgq->msqid,
  804. qstrgetstr(&info->uri),
  805. &msgq->lease);
  806. ret = ipc_sysv_msgmov_send(src->port, src->vmid, msgq->msqid, msgq->lease,
  807. msgq->scores, MAX_SYSV_CLIENTS);
  808. if (ret < 0)
  809. goto failed_info;
  810. msgq->owner = info;
  811. for (struct msg_type * mtype = msgq->types ;
  812. mtype < &msgq->types[msgq->ntypes] ; mtype++) {
  813. struct msg_req * req = mtype->reqs;
  814. mtype->reqs = mtype->req_tail = NULL;
  815. while (req) {
  816. struct msg_req * next = req->next;
  817. ipc_sysv_movres_send(&req->dest, info->vmid, qstrgetstr(&info->uri),
  818. msgq->lease, msgq->msqid, SYSV_MSGQ);
  819. put_ipc_port(req->dest.port);
  820. __free_msg_qobj(msgq, req);
  821. req = next;
  822. }
  823. }
  824. ret = 0;
  825. DkEventSet(msgq->event);
  826. goto out;
  827. failed_info:
  828. put_ipc_info(info);
  829. failed:
  830. ret = __load_msg_persist(msgq, true);
  831. out:
  832. return ret;
  833. }
  834. #endif