shim_ipc_pid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_ipc_pid.c
  17. *
  18. * This file contains functions and callbacks to handle IPC of PID namespace.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_fs.h>
  23. #include <shim_ipc.h>
  24. #include <shim_checkpoint.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #define PID_RANGE_SIZE 32
  29. #define PID_LEASE_TIME 1000
  30. #define NS pid
  31. #define NS_CAP PID
  32. #include "shim_ipc_nsimpl.h"
  33. static int thread_add_subrange (struct shim_thread * thread, void * arg,
  34. bool * unlocked)
  35. {
  36. if (!thread->in_vm)
  37. return 0;
  38. struct shim_ipc_info * info = (struct shim_ipc_info *) arg;
  39. add_pid_subrange(thread->tid, info->vmid,
  40. qstrgetstr(&info->uri), &thread->tid_lease);
  41. return 0;
  42. }
  43. int init_ns_pid (void)
  44. {
  45. struct shim_ipc_info * info;
  46. int ret = 0;
  47. init_namespace();
  48. if ((ret = create_ipc_location(&info)) < 0)
  49. return ret;
  50. walk_thread_list(&thread_add_subrange, info, false);
  51. return 0;
  52. }
  53. int broadcast_signal (IDTYPE sender, int signum)
  54. {
  55. BEGIN_PROFILE_INTERVAL();
  56. int ret;
  57. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  58. IPC_PID_KILL,
  59. sizeof(struct shim_ipc_pid_kill), 0);
  60. struct shim_ipc_pid_kill * msgin =
  61. (struct shim_ipc_pid_kill *) &msg->msg;
  62. msgin->sender = sender;
  63. msgin->id = 0;
  64. msgin->type = KILL_ALL;
  65. msgin->signum = signum;
  66. debug("ipc send to %u: IPC_PID_KILL(%u, %d, %u, %d)\n", 0,
  67. sender, KILL_ALL, 0, signum);
  68. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRCLD|IPC_PORT_DIRPRT);
  69. SAVE_PROFILE_INTERVAL(ipc_pid_kill_send);
  70. return ret;
  71. }
  72. int ipc_pid_kill_send (IDTYPE sender, IDTYPE id, enum kill_type type,
  73. int signum)
  74. {
  75. BEGIN_PROFILE_INTERVAL();
  76. IDTYPE dest;
  77. struct shim_ipc_port * port = NULL;
  78. int ret;
  79. if ((ret = connect_owner(id, &port, &dest)) < 0)
  80. goto out;
  81. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  82. IPC_PID_KILL,
  83. sizeof(struct shim_ipc_pid_kill),
  84. dest);
  85. struct shim_ipc_pid_kill * msgin =
  86. (struct shim_ipc_pid_kill *) &msg->msg.msg;
  87. msgin->sender = sender;
  88. msgin->id = id;
  89. msgin->type = type;
  90. msgin->signum = signum;
  91. debug("ipc send to %u: IPC_PID_KILL(%u, %d, %u, %d)\n", dest,
  92. sender, type, id, signum);
  93. ret = do_ipc_duplex(msg, port, NULL, NULL);
  94. put_ipc_port(port);
  95. out:
  96. SAVE_PROFILE_INTERVAL(ipc_pid_kill_send);
  97. return ret;
  98. }
  99. DEFINE_PROFILE_INTERVAL(ipc_pid_kill_send, ipc);
  100. DEFINE_PROFILE_INTERVAL(ipc_pid_kill_callback, ipc);
  101. int ipc_pid_kill_callback (IPC_CALLBACK_ARGS)
  102. {
  103. BEGIN_PROFILE_INTERVAL();
  104. struct shim_ipc_pid_kill * msgin =
  105. (struct shim_ipc_pid_kill *) msg->msg;
  106. debug("ipc callback form %u: IPC_PID_KILL(%u, %u, %d)\n",
  107. msg->src, msgin->sender, msgin->id, msgin->signum);
  108. int ret = 0;
  109. switch (msgin->type) {
  110. case KILL_THREAD:
  111. ret = do_kill_thread(msgin->sender, 0, msgin->id, msgin->signum,
  112. true);
  113. break;
  114. case KILL_PROCESS:
  115. ret = do_kill_proc(msgin->sender, msgin->id, msgin->signum, true);
  116. break;
  117. case KILL_PGROUP:
  118. ret = do_kill_pgroup(msgin->sender, msgin->id, msgin->signum,
  119. true);
  120. break;
  121. case KILL_ALL:
  122. broadcast_ipc(msg, &port, 1, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  123. kill_all_threads(NULL, msgin->sender, msgin->signum);
  124. break;
  125. }
  126. assert(ret != -ESRCH);
  127. SAVE_PROFILE_INTERVAL(ipc_pid_kill_callback);
  128. return ret < 0 ? ret : RESPONSE_CALLBACK;
  129. }
  130. DEFINE_PROFILE_INTERVAL(ipc_pid_getstatus_send, ipc);
  131. DEFINE_PROFILE_INTERVAL(ipc_pid_getstatus_callback, ipc);
  132. int ipc_pid_getstatus_send (struct shim_ipc_port * port, IDTYPE dest,
  133. int npids, IDTYPE * pids,
  134. struct pid_status ** status)
  135. {
  136. BEGIN_PROFILE_INTERVAL();
  137. int ret;
  138. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  139. IPC_PID_GETSTATUS,
  140. sizeof(struct shim_ipc_pid_getstatus) +
  141. sizeof(IDTYPE) * npids,
  142. dest);
  143. struct shim_ipc_pid_getstatus * msgin =
  144. (struct shim_ipc_pid_getstatus *) &msg->msg.msg;
  145. msgin->npids = npids;
  146. memcpy(msgin->pids, pids, sizeof(IDTYPE) * npids);
  147. debug("ipc send to %u: IPC_PID_GETSTATUS(%d, [%u, ...])\n", dest,
  148. npids, pids[0]);
  149. ret = do_ipc_duplex(msg, port, NULL, status);
  150. SAVE_PROFILE_INTERVAL(ipc_pid_getstatus_send);
  151. return ret;
  152. }
  153. int ipc_pid_getstatus_callback (IPC_CALLBACK_ARGS)
  154. {
  155. BEGIN_PROFILE_INTERVAL();
  156. struct shim_ipc_pid_getstatus * msgin =
  157. (struct shim_ipc_pid_getstatus *) msg->msg;
  158. int ret = 0;
  159. debug("ipc callback form %u: IPC_PID_GETSTATUS(%d, [%u, ...])\n",
  160. msg->src, msgin->npids, msgin->pids[0]);
  161. struct thread_status {
  162. int npids;
  163. IDTYPE * pids;
  164. int nstatus;
  165. struct pid_status * status;
  166. };
  167. int check_thread (struct shim_thread * thread, void * arg,
  168. bool * unlocked)
  169. {
  170. struct thread_status * status = (struct thread_status *) arg;
  171. for (int i = 0 ; i < status->npids ; i++)
  172. if (status->pids[i] == thread->tid &&
  173. thread->in_vm && thread->is_alive) {
  174. status->status[status->nstatus].pid = thread->tid;
  175. status->status[status->nstatus].tgid = thread->tgid;
  176. status->status[status->nstatus].pgid = thread->pgid;
  177. status->nstatus++;
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. struct thread_status status;
  183. status.npids = msgin->npids;
  184. status.pids = msgin->pids;
  185. status.nstatus = 0;
  186. status.status = __alloca(sizeof(struct pid_status) * msgin->npids);
  187. ret = walk_thread_list(&check_thread, &status, false);
  188. if (ret < 0 && ret != -ESRCH)
  189. goto out;
  190. ret = ipc_pid_retstatus_send(port, msg->src, status.nstatus, status.status,
  191. msg->seq);
  192. out:
  193. SAVE_PROFILE_INTERVAL(ipc_pid_getstatus_callback);
  194. return ret;
  195. }
  196. DEFINE_PROFILE_INTERVAL(ipc_pid_retstatus_send, ipc);
  197. DEFINE_PROFILE_INTERVAL(ipc_pid_retstatus_callback, ipc);
  198. int ipc_pid_retstatus_send (struct shim_ipc_port * port, IDTYPE dest,
  199. int nstatus, struct pid_status * status,
  200. unsigned long seq)
  201. {
  202. BEGIN_PROFILE_INTERVAL();
  203. int ret;
  204. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  205. IPC_PID_RETSTATUS,
  206. sizeof(struct shim_ipc_pid_retstatus) +
  207. sizeof(struct pid_status) * nstatus,
  208. dest);
  209. struct shim_ipc_pid_retstatus * msgin =
  210. (struct shim_ipc_pid_retstatus *) &msg->msg;
  211. msgin->nstatus = nstatus;
  212. memcpy(msgin->status, status, sizeof(struct pid_status) * nstatus);
  213. msg->seq = seq;
  214. if (nstatus)
  215. debug("ipc send to %u: IPC_PID_RETSTATUS(%d, [%u, ...])\n", dest,
  216. nstatus, status[0].pid);
  217. else
  218. debug("ipc send to %u: IPC_PID_RETSTATUS(0, [])\n", dest);
  219. ret = send_ipc_message(msg, port);
  220. SAVE_PROFILE_INTERVAL(ipc_pid_retstatus_send);
  221. return ret;
  222. }
  223. int ipc_pid_retstatus_callback (IPC_CALLBACK_ARGS)
  224. {
  225. BEGIN_PROFILE_INTERVAL();
  226. struct shim_ipc_pid_retstatus * msgin =
  227. (struct shim_ipc_pid_retstatus *) msg->msg;
  228. if (msgin->nstatus)
  229. debug("ipc callback form %u: IPC_PID_RETSTATUS(%d, [%u, ...])\n",
  230. msg->src, msgin->nstatus, msgin->status[0].pid);
  231. else
  232. debug("ipc callback form %u: IPC_PID_RETSTATUS(0, [])\n", msg->src);
  233. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  234. if (obj) {
  235. struct pid_status ** status = (struct pid_status **) obj->private;
  236. if (status) {
  237. *status = remalloc(msgin->status, sizeof(struct pid_status) *
  238. msgin->nstatus);
  239. obj->retval = msgin->nstatus;
  240. }
  241. if (obj->thread)
  242. thread_wakeup(obj->thread);
  243. }
  244. SAVE_PROFILE_INTERVAL(ipc_pid_retstatus_callback);
  245. return 0;
  246. }
  247. int get_all_pid_status (struct pid_status ** status)
  248. {
  249. /* run queryall unconditionally */
  250. ipc_pid_queryall_send();
  251. int bufsize = RANGE_SIZE;
  252. struct pid_status * status_buf = malloc(bufsize);
  253. int nstatus = 0;
  254. if (!bufsize)
  255. return -ENOMEM;
  256. struct list_head * list = &offered_ranges;
  257. struct range * r;
  258. int ret;
  259. lock(range_map_lock);
  260. retry:
  261. list_for_each_entry (r, list, list) {
  262. struct subrange * s = NULL;
  263. struct shim_ipc_info * p;
  264. int off, idx;
  265. IDTYPE base;
  266. IDTYPE pids[RANGE_SIZE];
  267. struct pid_status * range_status;
  268. next_range:
  269. idx = -1;
  270. off = r->offset;
  271. base = off * RANGE_SIZE + 1;
  272. next_sub:
  273. if (idx == -1) {
  274. p = r->owner;
  275. } else {
  276. if (idx >= RANGE_SIZE)
  277. continue;
  278. if (!r->subranges)
  279. continue;
  280. s = r->subranges->map[idx];
  281. if (!s) {
  282. idx++;
  283. goto next_sub;
  284. }
  285. p = s->owner;
  286. }
  287. if (p->vmid == cur_process.vmid) {
  288. idx++;
  289. goto next_sub;
  290. }
  291. if (!p->port) {
  292. int type = IPC_PORT_PIDOWN|IPC_PORT_LISTEN;
  293. IDTYPE owner = p->vmid;
  294. char * uri = qstrtostr(&p->uri, true);
  295. struct shim_ipc_port * port = NULL;
  296. unlock(range_map_lock);
  297. PAL_HANDLE pal_handle = DkStreamOpen(uri, 0, 0, 0, 0);
  298. if (pal_handle)
  299. add_ipc_port_by_id(owner, pal_handle, type, NULL, &port);
  300. lock(range_map_lock);
  301. list_for_each_entry(r, list, list)
  302. if (r->offset >= off)
  303. break;
  304. if (&r->list == list)
  305. break;
  306. if (r->offset > off)
  307. goto next_range;
  308. if (!port)
  309. continue;
  310. if (idx == -1) {
  311. } else {
  312. if (!r->subranges)
  313. continue;
  314. s = r->subranges->map[idx];
  315. if (!s) {
  316. idx++;
  317. goto next_sub;
  318. }
  319. p = s->owner;
  320. }
  321. if (p->port)
  322. put_ipc_port(p->port);
  323. p->port = port;
  324. }
  325. if (idx == -1) {
  326. for (int i = 0 ; i < RANGE_SIZE ; i++)
  327. pids[i] = base + i;
  328. } else {
  329. pids[0] = base + idx;
  330. }
  331. ret = ipc_pid_getstatus_send(p->port, p->vmid,
  332. idx == -1 ? RANGE_SIZE : 1, pids,
  333. &range_status);
  334. if (ret > 0) {
  335. if (nstatus + ret > bufsize) {
  336. int newsize = bufsize * 2;
  337. while (nstatus + ret > newsize)
  338. newsize *= 2;
  339. struct pid_status * new_buf = malloc(newsize);
  340. if (!new_buf) {
  341. unlock(range_map_lock);
  342. free(range_status);
  343. free(status_buf);
  344. return -ENOMEM;
  345. }
  346. memcpy(new_buf, status_buf,
  347. sizeof(struct pid_status) * nstatus);
  348. free(status_buf);
  349. status_buf = new_buf;
  350. bufsize = newsize;
  351. }
  352. memcpy(status_buf + nstatus, range_status,
  353. sizeof(struct pid_status) * ret);
  354. free(range_status);
  355. nstatus += ret;
  356. }
  357. idx++;
  358. goto next_sub;
  359. }
  360. if (list == &offered_ranges) {
  361. list = &owned_ranges;
  362. goto retry;
  363. }
  364. unlock(range_map_lock);
  365. if (!nstatus) {
  366. free(status_buf);
  367. return 0;
  368. }
  369. *status = status_buf;
  370. return nstatus;
  371. }
  372. DEFINE_PROFILE_INTERVAL(ipc_pid_getmeta_send, ipc);
  373. DEFINE_PROFILE_INTERVAL(ipc_pid_getmeta_callback, ipc);
  374. static const char * pid_meta_code_str[4] = { "CRED", "EXEC", "CWD", "ROOT", };
  375. int ipc_pid_getmeta_send (IDTYPE pid, enum pid_meta_code code,
  376. void ** data)
  377. {
  378. BEGIN_PROFILE_INTERVAL();
  379. IDTYPE dest;
  380. struct shim_ipc_port * port = NULL;
  381. int ret;
  382. if ((ret = connect_owner(pid, &port, &dest)) < 0)
  383. goto out;
  384. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  385. IPC_PID_GETMETA,
  386. sizeof(struct shim_ipc_pid_getmeta),
  387. dest);
  388. struct shim_ipc_pid_getmeta * msgin =
  389. (struct shim_ipc_pid_getmeta *) &msg->msg.msg;
  390. msgin->pid = pid;
  391. msgin->code = code;
  392. debug("ipc send to %u: IPC_PID_GETMETA(%u, %s)\n", dest,
  393. pid, pid_meta_code_str[code]);
  394. ret = do_ipc_duplex(msg, port, NULL, data);
  395. put_ipc_port(port);
  396. out:
  397. SAVE_PROFILE_INTERVAL(ipc_pid_getmeta_send);
  398. return ret;
  399. }
  400. int ipc_pid_getmeta_callback (IPC_CALLBACK_ARGS)
  401. {
  402. BEGIN_PROFILE_INTERVAL();
  403. struct shim_ipc_pid_getmeta * msgin =
  404. (struct shim_ipc_pid_getmeta *) msg->msg;
  405. int ret = 0;
  406. debug("ipc callback form %u: IPC_PID_GETMETA(%u, %s)\n", msg->src,
  407. msgin->pid, pid_meta_code_str[msgin->code]);
  408. struct shim_thread * thread = lookup_thread(msgin->pid);
  409. void * data = NULL;
  410. int datasize = 0;
  411. if (!thread) {
  412. ret = -ESRCH;
  413. goto out;
  414. }
  415. lock(thread->lock);
  416. switch (msgin->code) {
  417. case PID_META_CRED:
  418. datasize = sizeof(IDTYPE) * 2;
  419. data = __alloca(datasize);
  420. ((IDTYPE *) data)[0] = thread->uid;
  421. ((IDTYPE *) data)[1] = thread->gid;
  422. break;
  423. case PID_META_EXEC:
  424. if (!thread->exec || !thread->exec->dentry) {
  425. ret = -ENOENT;
  426. break;
  427. }
  428. data = dentry_get_path(thread->exec->dentry, true, &datasize);
  429. break;
  430. case PID_META_CWD:
  431. if (!thread->cwd) {
  432. ret = -ENOENT;
  433. break;
  434. }
  435. data = dentry_get_path(thread->cwd, true, &datasize);
  436. break;
  437. case PID_META_ROOT:
  438. if (!thread->root) {
  439. ret = -ENOENT;
  440. break;
  441. }
  442. data = dentry_get_path(thread->root, true, &datasize);
  443. break;
  444. default:
  445. ret = -EINVAL;
  446. break;
  447. }
  448. unlock(thread->lock);
  449. put_thread(thread);
  450. if (ret < 0)
  451. goto out;
  452. ret = ipc_pid_retmeta_send(port, msg->src, msgin->pid, msgin->code,
  453. data, datasize, msg->seq);
  454. out:
  455. SAVE_PROFILE_INTERVAL(ipc_pid_getmeta_callback);
  456. return ret;
  457. }
  458. DEFINE_PROFILE_INTERVAL(ipc_pid_retmeta_send, ipc);
  459. DEFINE_PROFILE_INTERVAL(ipc_pid_retmeta_callback, ipc);
  460. int ipc_pid_retmeta_send (struct shim_ipc_port * port, IDTYPE dest,
  461. IDTYPE pid, enum pid_meta_code code,
  462. const void * data, int datasize,
  463. unsigned long seq)
  464. {
  465. BEGIN_PROFILE_INTERVAL();
  466. int ret;
  467. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  468. IPC_PID_RETMETA,
  469. sizeof(struct shim_ipc_pid_retmeta) +
  470. datasize, dest);
  471. struct shim_ipc_pid_retmeta * msgin =
  472. (struct shim_ipc_pid_retmeta *) &msg->msg;
  473. msgin->pid = pid;
  474. msgin->code = code;
  475. msgin->datasize = datasize;
  476. memcpy(msgin->data, data, datasize);
  477. msg->seq = seq;
  478. debug("ipc send to %u: IPC_PID_RETMETA(%d, %s, %d)\n", dest,
  479. pid, pid_meta_code_str[code], datasize);
  480. ret = send_ipc_message(msg, port);
  481. SAVE_PROFILE_INTERVAL(ipc_pid_retmeta_send);
  482. return ret;
  483. }
  484. int ipc_pid_retmeta_callback (IPC_CALLBACK_ARGS)
  485. {
  486. BEGIN_PROFILE_INTERVAL();
  487. struct shim_ipc_pid_retmeta * msgin =
  488. (struct shim_ipc_pid_retmeta *) msg->msg;
  489. debug("ipc callback form %u: IPC_PID_RETMETA(%u, %s, %d)\n", msg->src,
  490. msgin->pid, pid_meta_code_str[msgin->code], msgin->datasize);
  491. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  492. if (obj) {
  493. void ** data = (void **) obj->private;
  494. if (data)
  495. *data = msgin->datasize ?
  496. remalloc(msgin->data, msgin->datasize) : NULL;
  497. obj->retval = msgin->datasize;
  498. if (obj->thread)
  499. thread_wakeup(obj->thread);
  500. }
  501. SAVE_PROFILE_INTERVAL(ipc_pid_retmeta_callback);
  502. return 0;
  503. }
  504. int get_pid_port (IDTYPE pid, IDTYPE * dest, struct shim_ipc_port ** port)
  505. {
  506. IDTYPE owner;
  507. int ret;
  508. if ((ret = connect_owner(pid, port, &owner)) < 0)
  509. return ret;
  510. if (dest)
  511. *dest = owner;
  512. return 0;
  513. }
  514. DEFINE_PROFILE_INTERVAL(ipc_pid_nop_send, ipc);
  515. DEFINE_PROFILE_INTERVAL(ipc_pid_nop_callback, ipc);
  516. int ipc_pid_nop_send (struct shim_ipc_port * port, IDTYPE dest, int count,
  517. const void * buf, int len)
  518. {
  519. BEGIN_PROFILE_INTERVAL();
  520. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  521. IPC_PID_NOP,
  522. sizeof(struct shim_ipc_pid_nop) +
  523. len, dest);
  524. struct shim_ipc_pid_nop * msgin =
  525. (struct shim_ipc_pid_nop *) &msg->msg.msg;
  526. msgin->count = count * 2;
  527. memcpy(msgin->payload, buf, len);
  528. debug("ipc send to %u: IPC_PID_NOP(%d)\n", dest, count * 2);
  529. SAVE_PROFILE_INTERVAL(ipc_pid_nop_send);
  530. return do_ipc_duplex(msg, port, NULL, NULL);
  531. }
  532. int ipc_pid_nop_callback (IPC_CALLBACK_ARGS)
  533. {
  534. BEGIN_PROFILE_INTERVAL();
  535. struct shim_ipc_pid_nop * msgin =
  536. (struct shim_ipc_pid_nop *) &msg->msg;
  537. debug("ipc callback form %u: IPC_PID_NOP(%d)\n", msg->src,
  538. msgin->count);
  539. if (!(--msgin->count)) {
  540. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  541. if (obj && obj->thread)
  542. thread_wakeup(obj->thread);
  543. SAVE_PROFILE_INTERVAL(ipc_pid_nop_callback);
  544. return 0;
  545. }
  546. SAVE_PROFILE_INTERVAL(ipc_pid_nop_callback);
  547. debug("ipc send to %u: IPC_PID_NOP(%d)\n", msg->src,
  548. msgin->count);
  549. int ret = send_ipc_message(msg, port);
  550. SAVE_PROFILE_INTERVAL(ipc_pid_nop_send);
  551. return ret;
  552. }
  553. DEFINE_PROFILE_INTERVAL(ipc_pid_sendrpc_send, ipc);
  554. DEFINE_PROFILE_INTERVAL(ipc_pid_sendrpc_callback, ipc);
  555. int ipc_pid_sendrpc_send (IDTYPE pid, IDTYPE sender, const void * buf,
  556. int len)
  557. {
  558. BEGIN_PROFILE_INTERVAL();
  559. int ret = 0;
  560. IDTYPE dest;
  561. struct shim_ipc_port * port = NULL;
  562. if ((ret = get_pid_port(pid, &dest, &port)) < 0)
  563. return ret;
  564. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  565. IPC_PID_SENDRPC,
  566. sizeof(struct shim_ipc_pid_sendrpc) +
  567. len, dest);
  568. struct shim_ipc_pid_sendrpc * msgin =
  569. (struct shim_ipc_pid_sendrpc *) &msg->msg;
  570. debug("ipc send to %u: IPC_PID_SENDPRC(%d)\n", dest, len);
  571. msgin->sender = sender;
  572. msgin->len = len;
  573. memcpy(msgin->payload, buf, len);
  574. ret = send_ipc_message(msg, port);
  575. put_ipc_port(port);
  576. SAVE_PROFILE_INTERVAL(ipc_pid_sendrpc_send);
  577. return ret;
  578. }
  579. struct rpcmsg {
  580. struct list_head list;
  581. IDTYPE sender;
  582. int len;
  583. char payload[];
  584. };
  585. struct rpcreq {
  586. struct list_head list;
  587. struct shim_thread * thread;
  588. IDTYPE sender;
  589. int len;
  590. void * buffer;
  591. };
  592. static LIST_HEAD(rpc_msgs);
  593. static LIST_HEAD(rpc_reqs);
  594. static LOCKTYPE rpc_queue_lock;
  595. int get_rpc_msg (IDTYPE * sender, void * buf, int len)
  596. {
  597. create_lock_runtime(&rpc_queue_lock);
  598. lock(rpc_queue_lock);
  599. if (!list_empty(&rpc_msgs)) {
  600. struct rpcmsg * m = list_first_entry(&rpc_msgs, struct rpcmsg, list);
  601. list_del(&m->list);
  602. if (m->len < len)
  603. len = m->len;
  604. if (sender)
  605. *sender = m->sender;
  606. memcpy(buf, m->payload, len);
  607. unlock(rpc_queue_lock);
  608. return len;
  609. }
  610. struct rpcreq * r = malloc(sizeof(struct rpcreq));
  611. if (!r) {
  612. unlock(rpc_queue_lock);
  613. return -ENOMEM;
  614. }
  615. INIT_LIST_HEAD(&r->list);
  616. r->sender = 0;
  617. r->len = len;
  618. r->buffer = buf;
  619. thread_setwait(&r->thread, NULL);
  620. list_add_tail(&r->list, &rpc_reqs);
  621. unlock(rpc_queue_lock);
  622. thread_sleep();
  623. put_thread(r->thread);
  624. if (sender)
  625. *sender = r->sender;
  626. return r->len;
  627. }
  628. int ipc_pid_sendrpc_callback (IPC_CALLBACK_ARGS)
  629. {
  630. BEGIN_PROFILE_INTERVAL();
  631. int ret = 0;
  632. struct shim_ipc_pid_sendrpc * msgin =
  633. (struct shim_ipc_pid_sendrpc *) msg->msg;
  634. debug("ipc callback from %u: IPC_PID_SENDPRC(%u, %d)\n", msg->src,
  635. msgin->sender, msgin->len);
  636. create_lock_runtime(&rpc_queue_lock);
  637. lock(rpc_queue_lock);
  638. if (!list_empty(&rpc_reqs)) {
  639. struct rpcreq * r = list_first_entry(&rpc_reqs, struct rpcreq, list);
  640. list_del(&r->list);
  641. if (msgin->len < r->len)
  642. r->len = msgin->len;
  643. r->sender = msgin->sender;
  644. memcpy(r->buffer, msgin->payload, r->len);
  645. thread_wakeup(r->thread);
  646. goto out;
  647. }
  648. struct rpcmsg * m = malloc(sizeof(struct rpcmsg) + msgin->len);
  649. if (!m) {
  650. ret = -ENOMEM;
  651. goto out;
  652. }
  653. INIT_LIST_HEAD(&m->list);
  654. m->sender = msgin->sender;
  655. m->len = msgin->len;
  656. memcpy(m->payload, msgin->payload, msgin->len);
  657. list_add_tail(&m->list, &rpc_msgs);
  658. out:
  659. unlock(rpc_queue_lock);
  660. SAVE_PROFILE_INTERVAL(ipc_pid_sendrpc_callback);
  661. return ret;
  662. }