shim_ipc_pid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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_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 from %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. SAVE_PROFILE_INTERVAL(ipc_pid_kill_callback);
  127. return ret < 0 ? ret : RESPONSE_CALLBACK;
  128. }
  129. DEFINE_PROFILE_INTERVAL(ipc_pid_getstatus_send, ipc);
  130. DEFINE_PROFILE_INTERVAL(ipc_pid_getstatus_callback, ipc);
  131. int ipc_pid_getstatus_send (struct shim_ipc_port * port, IDTYPE dest,
  132. int npids, IDTYPE * pids,
  133. struct pid_status ** status)
  134. {
  135. BEGIN_PROFILE_INTERVAL();
  136. int ret;
  137. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  138. IPC_PID_GETSTATUS,
  139. sizeof(struct shim_ipc_pid_getstatus) +
  140. sizeof(IDTYPE) * npids,
  141. dest);
  142. struct shim_ipc_pid_getstatus * msgin =
  143. (struct shim_ipc_pid_getstatus *) &msg->msg.msg;
  144. msgin->npids = npids;
  145. memcpy(msgin->pids, pids, sizeof(IDTYPE) * npids);
  146. debug("ipc send to %u: IPC_PID_GETSTATUS(%d, [%u, ...])\n", dest,
  147. npids, pids[0]);
  148. ret = do_ipc_duplex(msg, port, NULL, status);
  149. SAVE_PROFILE_INTERVAL(ipc_pid_getstatus_send);
  150. return ret;
  151. }
  152. int ipc_pid_getstatus_callback (IPC_CALLBACK_ARGS)
  153. {
  154. BEGIN_PROFILE_INTERVAL();
  155. struct shim_ipc_pid_getstatus * msgin =
  156. (struct shim_ipc_pid_getstatus *) msg->msg;
  157. int ret = 0;
  158. debug("ipc callback from %u: IPC_PID_GETSTATUS(%d, [%u, ...])\n",
  159. msg->src, msgin->npids, msgin->pids[0]);
  160. struct thread_status {
  161. int npids;
  162. IDTYPE * pids;
  163. int nstatus;
  164. struct pid_status * status;
  165. };
  166. int check_thread (struct shim_thread * thread, void * arg,
  167. bool * unlocked)
  168. {
  169. struct thread_status * status = (struct thread_status *) arg;
  170. for (int i = 0 ; i < status->npids ; i++)
  171. if (status->pids[i] == thread->tid &&
  172. thread->in_vm && thread->is_alive) {
  173. status->status[status->nstatus].pid = thread->tid;
  174. status->status[status->nstatus].tgid = thread->tgid;
  175. status->status[status->nstatus].pgid = thread->pgid;
  176. status->nstatus++;
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. struct thread_status status;
  182. status.npids = msgin->npids;
  183. status.pids = msgin->pids;
  184. status.nstatus = 0;
  185. status.status = __alloca(sizeof(struct pid_status) * msgin->npids);
  186. ret = walk_thread_list(&check_thread, &status, false);
  187. if (ret < 0 && ret != -ESRCH)
  188. goto out;
  189. ret = ipc_pid_retstatus_send(port, msg->src, status.nstatus, status.status,
  190. msg->seq);
  191. out:
  192. SAVE_PROFILE_INTERVAL(ipc_pid_getstatus_callback);
  193. return ret;
  194. }
  195. DEFINE_PROFILE_INTERVAL(ipc_pid_retstatus_send, ipc);
  196. DEFINE_PROFILE_INTERVAL(ipc_pid_retstatus_callback, ipc);
  197. int ipc_pid_retstatus_send (struct shim_ipc_port * port, IDTYPE dest,
  198. int nstatus, struct pid_status * status,
  199. unsigned long seq)
  200. {
  201. BEGIN_PROFILE_INTERVAL();
  202. int ret;
  203. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  204. IPC_PID_RETSTATUS,
  205. sizeof(struct shim_ipc_pid_retstatus) +
  206. sizeof(struct pid_status) * nstatus,
  207. dest);
  208. struct shim_ipc_pid_retstatus * msgin =
  209. (struct shim_ipc_pid_retstatus *) &msg->msg;
  210. msgin->nstatus = nstatus;
  211. memcpy(msgin->status, status, sizeof(struct pid_status) * nstatus);
  212. msg->seq = seq;
  213. if (nstatus)
  214. debug("ipc send to %u: IPC_PID_RETSTATUS(%d, [%u, ...])\n", dest,
  215. nstatus, status[0].pid);
  216. else
  217. debug("ipc send to %u: IPC_PID_RETSTATUS(0, [])\n", dest);
  218. ret = send_ipc_message(msg, port);
  219. SAVE_PROFILE_INTERVAL(ipc_pid_retstatus_send);
  220. return ret;
  221. }
  222. int ipc_pid_retstatus_callback (IPC_CALLBACK_ARGS)
  223. {
  224. BEGIN_PROFILE_INTERVAL();
  225. struct shim_ipc_pid_retstatus * msgin =
  226. (struct shim_ipc_pid_retstatus *) msg->msg;
  227. if (msgin->nstatus)
  228. debug("ipc callback from %u: IPC_PID_RETSTATUS(%d, [%u, ...])\n",
  229. msg->src, msgin->nstatus, msgin->status[0].pid);
  230. else
  231. debug("ipc callback from %u: IPC_PID_RETSTATUS(0, [])\n", msg->src);
  232. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  233. if (obj) {
  234. struct pid_status ** status = (struct pid_status **) obj->private;
  235. if (status) {
  236. *status = malloc_copy(msgin->status, sizeof(struct pid_status) *
  237. msgin->nstatus);
  238. obj->retval = msgin->nstatus;
  239. }
  240. if (obj->thread)
  241. thread_wakeup(obj->thread);
  242. }
  243. SAVE_PROFILE_INTERVAL(ipc_pid_retstatus_callback);
  244. return 0;
  245. }
  246. int get_all_pid_status (struct pid_status ** status)
  247. {
  248. /* run queryall unconditionally */
  249. ipc_pid_queryall_send();
  250. int bufsize = RANGE_SIZE;
  251. struct pid_status * status_buf = malloc(bufsize);
  252. int nstatus = 0;
  253. if (!bufsize)
  254. return -ENOMEM;
  255. LISTP_TYPE(range) * list = &offered_ranges;
  256. struct range * r;
  257. int ret;
  258. lock(range_map_lock);
  259. retry:
  260. listp_for_each_entry (r, list, list) {
  261. struct subrange * s = NULL;
  262. struct shim_ipc_info * p;
  263. int off, idx;
  264. IDTYPE base;
  265. IDTYPE pids[RANGE_SIZE];
  266. struct pid_status * range_status;
  267. next_range:
  268. idx = -1;
  269. off = r->offset;
  270. base = off * RANGE_SIZE + 1;
  271. next_sub:
  272. if (idx == -1) {
  273. p = r->owner;
  274. } else {
  275. if (idx >= RANGE_SIZE)
  276. continue;
  277. if (!r->subranges)
  278. continue;
  279. s = r->subranges->map[idx];
  280. if (!s) {
  281. idx++;
  282. goto next_sub;
  283. }
  284. p = s->owner;
  285. }
  286. if (p->vmid == cur_process.vmid) {
  287. idx++;
  288. goto next_sub;
  289. }
  290. if (!p->port) {
  291. int type = IPC_PORT_PIDOWN|IPC_PORT_LISTEN;
  292. IDTYPE owner = p->vmid;
  293. char * uri = qstrtostr(&p->uri, true);
  294. struct shim_ipc_port * port = NULL;
  295. unlock(range_map_lock);
  296. PAL_HANDLE pal_handle = DkStreamOpen(uri, 0, 0, 0, 0);
  297. if (pal_handle)
  298. add_ipc_port_by_id(owner, pal_handle, type, NULL, &port);
  299. lock(range_map_lock);
  300. listp_for_each_entry(r, list, list)
  301. if (r->offset >= off)
  302. break;
  303. /* DEP 5/15/17: I believe this is checking if the list is empty */
  304. //if (&r->list == list)
  305. if (listp_empty(list))
  306. break;
  307. if (r->offset > off)
  308. goto next_range;
  309. if (!port)
  310. continue;
  311. if (idx == -1) {
  312. } else {
  313. if (!r->subranges)
  314. continue;
  315. s = r->subranges->map[idx];
  316. if (!s) {
  317. idx++;
  318. goto next_sub;
  319. }
  320. p = s->owner;
  321. }
  322. if (p->port)
  323. put_ipc_port(p->port);
  324. p->port = port;
  325. }
  326. if (idx == -1) {
  327. for (int i = 0 ; i < RANGE_SIZE ; i++)
  328. pids[i] = base + i;
  329. } else {
  330. pids[0] = base + idx;
  331. }
  332. ret = ipc_pid_getstatus_send(p->port, p->vmid,
  333. idx == -1 ? RANGE_SIZE : 1, pids,
  334. &range_status);
  335. if (ret > 0) {
  336. if (nstatus + ret > bufsize) {
  337. int newsize = bufsize * 2;
  338. while (nstatus + ret > newsize)
  339. newsize *= 2;
  340. struct pid_status * new_buf = malloc(newsize);
  341. if (!new_buf) {
  342. unlock(range_map_lock);
  343. free(range_status);
  344. free(status_buf);
  345. return -ENOMEM;
  346. }
  347. memcpy(new_buf, status_buf,
  348. sizeof(struct pid_status) * nstatus);
  349. free(status_buf);
  350. status_buf = new_buf;
  351. bufsize = newsize;
  352. }
  353. memcpy(status_buf + nstatus, range_status,
  354. sizeof(struct pid_status) * ret);
  355. free(range_status);
  356. nstatus += ret;
  357. }
  358. idx++;
  359. goto next_sub;
  360. }
  361. if (list == &offered_ranges) {
  362. list = &owned_ranges;
  363. goto retry;
  364. }
  365. unlock(range_map_lock);
  366. if (!nstatus) {
  367. free(status_buf);
  368. return 0;
  369. }
  370. *status = status_buf;
  371. return nstatus;
  372. }
  373. DEFINE_PROFILE_INTERVAL(ipc_pid_getmeta_send, ipc);
  374. DEFINE_PROFILE_INTERVAL(ipc_pid_getmeta_callback, ipc);
  375. static const char * pid_meta_code_str[4] = { "CRED", "EXEC", "CWD", "ROOT", };
  376. int ipc_pid_getmeta_send (IDTYPE pid, enum pid_meta_code code,
  377. void ** data)
  378. {
  379. BEGIN_PROFILE_INTERVAL();
  380. IDTYPE dest;
  381. struct shim_ipc_port * port = NULL;
  382. int ret;
  383. if ((ret = connect_owner(pid, &port, &dest)) < 0)
  384. goto out;
  385. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  386. IPC_PID_GETMETA,
  387. sizeof(struct shim_ipc_pid_getmeta),
  388. dest);
  389. struct shim_ipc_pid_getmeta * msgin =
  390. (struct shim_ipc_pid_getmeta *) &msg->msg.msg;
  391. msgin->pid = pid;
  392. msgin->code = code;
  393. debug("ipc send to %u: IPC_PID_GETMETA(%u, %s)\n", dest,
  394. pid, pid_meta_code_str[code]);
  395. ret = do_ipc_duplex(msg, port, NULL, data);
  396. put_ipc_port(port);
  397. out:
  398. SAVE_PROFILE_INTERVAL(ipc_pid_getmeta_send);
  399. return ret;
  400. }
  401. int ipc_pid_getmeta_callback (IPC_CALLBACK_ARGS)
  402. {
  403. BEGIN_PROFILE_INTERVAL();
  404. struct shim_ipc_pid_getmeta * msgin =
  405. (struct shim_ipc_pid_getmeta *) msg->msg;
  406. int ret = 0;
  407. debug("ipc callback from %u: IPC_PID_GETMETA(%u, %s)\n", msg->src,
  408. msgin->pid, pid_meta_code_str[msgin->code]);
  409. struct shim_thread * thread = lookup_thread(msgin->pid);
  410. void * data = NULL;
  411. int datasize = 0;
  412. if (!thread) {
  413. ret = -ESRCH;
  414. goto out;
  415. }
  416. lock(thread->lock);
  417. switch (msgin->code) {
  418. case PID_META_CRED:
  419. datasize = sizeof(IDTYPE) * 2;
  420. data = __alloca(datasize);
  421. ((IDTYPE *) data)[0] = thread->uid;
  422. ((IDTYPE *) data)[1] = thread->gid;
  423. break;
  424. case PID_META_EXEC:
  425. if (!thread->exec || !thread->exec->dentry) {
  426. ret = -ENOENT;
  427. break;
  428. }
  429. data = dentry_get_path(thread->exec->dentry, true, &datasize);
  430. break;
  431. case PID_META_CWD:
  432. if (!thread->cwd) {
  433. ret = -ENOENT;
  434. break;
  435. }
  436. data = dentry_get_path(thread->cwd, true, &datasize);
  437. break;
  438. case PID_META_ROOT:
  439. if (!thread->root) {
  440. ret = -ENOENT;
  441. break;
  442. }
  443. data = dentry_get_path(thread->root, true, &datasize);
  444. break;
  445. default:
  446. ret = -EINVAL;
  447. break;
  448. }
  449. unlock(thread->lock);
  450. put_thread(thread);
  451. if (ret < 0)
  452. goto out;
  453. ret = ipc_pid_retmeta_send(port, msg->src, msgin->pid, msgin->code,
  454. data, datasize, msg->seq);
  455. out:
  456. SAVE_PROFILE_INTERVAL(ipc_pid_getmeta_callback);
  457. return ret;
  458. }
  459. DEFINE_PROFILE_INTERVAL(ipc_pid_retmeta_send, ipc);
  460. DEFINE_PROFILE_INTERVAL(ipc_pid_retmeta_callback, ipc);
  461. int ipc_pid_retmeta_send (struct shim_ipc_port * port, IDTYPE dest,
  462. IDTYPE pid, enum pid_meta_code code,
  463. const void * data, int datasize,
  464. unsigned long seq)
  465. {
  466. BEGIN_PROFILE_INTERVAL();
  467. int ret;
  468. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  469. IPC_PID_RETMETA,
  470. sizeof(struct shim_ipc_pid_retmeta) +
  471. datasize, dest);
  472. struct shim_ipc_pid_retmeta * msgin =
  473. (struct shim_ipc_pid_retmeta *) &msg->msg;
  474. msgin->pid = pid;
  475. msgin->code = code;
  476. msgin->datasize = datasize;
  477. memcpy(msgin->data, data, datasize);
  478. msg->seq = seq;
  479. debug("ipc send to %u: IPC_PID_RETMETA(%d, %s, %d)\n", dest,
  480. pid, pid_meta_code_str[code], datasize);
  481. ret = send_ipc_message(msg, port);
  482. SAVE_PROFILE_INTERVAL(ipc_pid_retmeta_send);
  483. return ret;
  484. }
  485. int ipc_pid_retmeta_callback (IPC_CALLBACK_ARGS)
  486. {
  487. BEGIN_PROFILE_INTERVAL();
  488. struct shim_ipc_pid_retmeta * msgin =
  489. (struct shim_ipc_pid_retmeta *) msg->msg;
  490. debug("ipc callback from %u: IPC_PID_RETMETA(%u, %s, %d)\n", msg->src,
  491. msgin->pid, pid_meta_code_str[msgin->code], msgin->datasize);
  492. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  493. if (obj) {
  494. void ** data = (void **) obj->private;
  495. if (data)
  496. *data = msgin->datasize ?
  497. malloc_copy(msgin->data, msgin->datasize) : NULL;
  498. obj->retval = msgin->datasize;
  499. if (obj->thread)
  500. thread_wakeup(obj->thread);
  501. }
  502. SAVE_PROFILE_INTERVAL(ipc_pid_retmeta_callback);
  503. return 0;
  504. }
  505. int get_pid_port (IDTYPE pid, IDTYPE * dest, struct shim_ipc_port ** port)
  506. {
  507. IDTYPE owner;
  508. int ret;
  509. if ((ret = connect_owner(pid, port, &owner)) < 0)
  510. return ret;
  511. if (dest)
  512. *dest = owner;
  513. return 0;
  514. }
  515. DEFINE_PROFILE_INTERVAL(ipc_pid_nop_send, ipc);
  516. DEFINE_PROFILE_INTERVAL(ipc_pid_nop_callback, ipc);
  517. int ipc_pid_nop_send (struct shim_ipc_port * port, IDTYPE dest, int count,
  518. const void * buf, int len)
  519. {
  520. BEGIN_PROFILE_INTERVAL();
  521. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  522. IPC_PID_NOP,
  523. sizeof(struct shim_ipc_pid_nop) +
  524. len, dest);
  525. struct shim_ipc_pid_nop * msgin =
  526. (struct shim_ipc_pid_nop *) &msg->msg.msg;
  527. msgin->count = count * 2;
  528. memcpy(msgin->payload, buf, len);
  529. debug("ipc send to %u: IPC_PID_NOP(%d)\n", dest, count * 2);
  530. SAVE_PROFILE_INTERVAL(ipc_pid_nop_send);
  531. return do_ipc_duplex(msg, port, NULL, NULL);
  532. }
  533. int ipc_pid_nop_callback (IPC_CALLBACK_ARGS)
  534. {
  535. BEGIN_PROFILE_INTERVAL();
  536. struct shim_ipc_pid_nop * msgin =
  537. (struct shim_ipc_pid_nop *) &msg->msg;
  538. debug("ipc callback from %u: IPC_PID_NOP(%d)\n", msg->src,
  539. msgin->count);
  540. if (!(--msgin->count)) {
  541. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  542. if (obj && obj->thread)
  543. thread_wakeup(obj->thread);
  544. SAVE_PROFILE_INTERVAL(ipc_pid_nop_callback);
  545. return 0;
  546. }
  547. SAVE_PROFILE_INTERVAL(ipc_pid_nop_callback);
  548. debug("ipc send to %u: IPC_PID_NOP(%d)\n", msg->src,
  549. msgin->count);
  550. int ret = send_ipc_message(msg, port);
  551. SAVE_PROFILE_INTERVAL(ipc_pid_nop_send);
  552. return ret;
  553. }
  554. DEFINE_PROFILE_INTERVAL(ipc_pid_sendrpc_send, ipc);
  555. DEFINE_PROFILE_INTERVAL(ipc_pid_sendrpc_callback, ipc);
  556. int ipc_pid_sendrpc_send (IDTYPE pid, IDTYPE sender, const void * buf,
  557. int len)
  558. {
  559. BEGIN_PROFILE_INTERVAL();
  560. int ret = 0;
  561. IDTYPE dest;
  562. struct shim_ipc_port * port = NULL;
  563. if ((ret = get_pid_port(pid, &dest, &port)) < 0)
  564. return ret;
  565. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  566. IPC_PID_SENDRPC,
  567. sizeof(struct shim_ipc_pid_sendrpc) +
  568. len, dest);
  569. struct shim_ipc_pid_sendrpc * msgin =
  570. (struct shim_ipc_pid_sendrpc *) &msg->msg;
  571. debug("ipc send to %u: IPC_PID_SENDPRC(%d)\n", dest, len);
  572. msgin->sender = sender;
  573. msgin->len = len;
  574. memcpy(msgin->payload, buf, len);
  575. ret = send_ipc_message(msg, port);
  576. put_ipc_port(port);
  577. SAVE_PROFILE_INTERVAL(ipc_pid_sendrpc_send);
  578. return ret;
  579. }
  580. DEFINE_LIST(rpcmsg);
  581. struct rpcmsg {
  582. LIST_TYPE(rpcmsg) list;
  583. IDTYPE sender;
  584. int len;
  585. char payload[];
  586. };
  587. DEFINE_LIST(rpcreq);
  588. struct rpcreq {
  589. LIST_TYPE(rpcreq) list;
  590. struct shim_thread * thread;
  591. IDTYPE sender;
  592. int len;
  593. void * buffer;
  594. };
  595. DEFINE_LISTP(rpcmsg);
  596. DEFINE_LISTP(rpcreq);
  597. static LISTP_TYPE(rpcmsg) rpc_msgs;
  598. static LISTP_TYPE(rpcreq) rpc_reqs;
  599. static LOCKTYPE rpc_queue_lock;
  600. int get_rpc_msg (IDTYPE * sender, void * buf, int len)
  601. {
  602. create_lock_runtime(&rpc_queue_lock);
  603. lock(rpc_queue_lock);
  604. if (!listp_empty(&rpc_msgs)) {
  605. struct rpcmsg * m = listp_first_entry(&rpc_msgs, struct rpcmsg, list);
  606. listp_del(m, &rpc_msgs, list);
  607. if (m->len < len)
  608. len = m->len;
  609. if (sender)
  610. *sender = m->sender;
  611. memcpy(buf, m->payload, len);
  612. unlock(rpc_queue_lock);
  613. return len;
  614. }
  615. struct rpcreq * r = malloc(sizeof(struct rpcreq));
  616. if (!r) {
  617. unlock(rpc_queue_lock);
  618. return -ENOMEM;
  619. }
  620. INIT_LIST_HEAD(r, list);
  621. r->sender = 0;
  622. r->len = len;
  623. r->buffer = buf;
  624. thread_setwait(&r->thread, NULL);
  625. listp_add_tail(r, &rpc_reqs, list);
  626. unlock(rpc_queue_lock);
  627. thread_sleep(NO_TIMEOUT);
  628. put_thread(r->thread);
  629. if (sender)
  630. *sender = r->sender;
  631. return r->len;
  632. }
  633. int ipc_pid_sendrpc_callback (IPC_CALLBACK_ARGS)
  634. {
  635. BEGIN_PROFILE_INTERVAL();
  636. int ret = 0;
  637. struct shim_ipc_pid_sendrpc * msgin =
  638. (struct shim_ipc_pid_sendrpc *) msg->msg;
  639. debug("ipc callback from %u: IPC_PID_SENDPRC(%u, %d)\n", msg->src,
  640. msgin->sender, msgin->len);
  641. create_lock_runtime(&rpc_queue_lock);
  642. lock(rpc_queue_lock);
  643. if (!listp_empty(&rpc_reqs)) {
  644. struct rpcreq * r = listp_first_entry(&rpc_reqs, struct rpcreq, list);
  645. listp_del(r, &rpc_reqs, list);
  646. if (msgin->len < r->len)
  647. r->len = msgin->len;
  648. r->sender = msgin->sender;
  649. memcpy(r->buffer, msgin->payload, r->len);
  650. thread_wakeup(r->thread);
  651. goto out;
  652. }
  653. struct rpcmsg * m = malloc(sizeof(struct rpcmsg) + msgin->len);
  654. if (!m) {
  655. ret = -ENOMEM;
  656. goto out;
  657. }
  658. INIT_LIST_HEAD(m, list);
  659. m->sender = msgin->sender;
  660. m->len = msgin->len;
  661. memcpy(m->payload, msgin->payload, msgin->len);
  662. listp_add_tail(m, &rpc_msgs, list);
  663. out:
  664. unlock(rpc_queue_lock);
  665. SAVE_PROFILE_INTERVAL(ipc_pid_sendrpc_callback);
  666. return ret;
  667. }