shim_ipc_pid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 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 = malloc_copy(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. LISTP_TYPE(range) * list = &offered_ranges;
  257. struct range * r;
  258. int ret;
  259. lock(range_map_lock);
  260. retry:
  261. listp_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. listp_for_each_entry(r, list, list)
  302. if (r->offset >= off)
  303. break;
  304. /* DEP 5/15/17: I believe this is checking if the list is empty */
  305. //if (&r->list == list)
  306. if (listp_empty(list))
  307. break;
  308. if (r->offset > off)
  309. goto next_range;
  310. if (!port)
  311. continue;
  312. if (idx == -1) {
  313. } else {
  314. if (!r->subranges)
  315. continue;
  316. s = r->subranges->map[idx];
  317. if (!s) {
  318. idx++;
  319. goto next_sub;
  320. }
  321. p = s->owner;
  322. }
  323. if (p->port)
  324. put_ipc_port(p->port);
  325. p->port = port;
  326. }
  327. if (idx == -1) {
  328. for (int i = 0 ; i < RANGE_SIZE ; i++)
  329. pids[i] = base + i;
  330. } else {
  331. pids[0] = base + idx;
  332. }
  333. ret = ipc_pid_getstatus_send(p->port, p->vmid,
  334. idx == -1 ? RANGE_SIZE : 1, pids,
  335. &range_status);
  336. if (ret > 0) {
  337. if (nstatus + ret > bufsize) {
  338. int newsize = bufsize * 2;
  339. while (nstatus + ret > newsize)
  340. newsize *= 2;
  341. struct pid_status * new_buf = malloc(newsize);
  342. if (!new_buf) {
  343. unlock(range_map_lock);
  344. free(range_status);
  345. free(status_buf);
  346. return -ENOMEM;
  347. }
  348. memcpy(new_buf, status_buf,
  349. sizeof(struct pid_status) * nstatus);
  350. free(status_buf);
  351. status_buf = new_buf;
  352. bufsize = newsize;
  353. }
  354. memcpy(status_buf + nstatus, range_status,
  355. sizeof(struct pid_status) * ret);
  356. free(range_status);
  357. nstatus += ret;
  358. }
  359. idx++;
  360. goto next_sub;
  361. }
  362. if (list == &offered_ranges) {
  363. list = &owned_ranges;
  364. goto retry;
  365. }
  366. unlock(range_map_lock);
  367. if (!nstatus) {
  368. free(status_buf);
  369. return 0;
  370. }
  371. *status = status_buf;
  372. return nstatus;
  373. }
  374. DEFINE_PROFILE_INTERVAL(ipc_pid_getmeta_send, ipc);
  375. DEFINE_PROFILE_INTERVAL(ipc_pid_getmeta_callback, ipc);
  376. static const char * pid_meta_code_str[4] = { "CRED", "EXEC", "CWD", "ROOT", };
  377. int ipc_pid_getmeta_send (IDTYPE pid, enum pid_meta_code code,
  378. void ** data)
  379. {
  380. BEGIN_PROFILE_INTERVAL();
  381. IDTYPE dest;
  382. struct shim_ipc_port * port = NULL;
  383. int ret;
  384. if ((ret = connect_owner(pid, &port, &dest)) < 0)
  385. goto out;
  386. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  387. IPC_PID_GETMETA,
  388. sizeof(struct shim_ipc_pid_getmeta),
  389. dest);
  390. struct shim_ipc_pid_getmeta * msgin =
  391. (struct shim_ipc_pid_getmeta *) &msg->msg.msg;
  392. msgin->pid = pid;
  393. msgin->code = code;
  394. debug("ipc send to %u: IPC_PID_GETMETA(%u, %s)\n", dest,
  395. pid, pid_meta_code_str[code]);
  396. ret = do_ipc_duplex(msg, port, NULL, data);
  397. put_ipc_port(port);
  398. out:
  399. SAVE_PROFILE_INTERVAL(ipc_pid_getmeta_send);
  400. return ret;
  401. }
  402. int ipc_pid_getmeta_callback (IPC_CALLBACK_ARGS)
  403. {
  404. BEGIN_PROFILE_INTERVAL();
  405. struct shim_ipc_pid_getmeta * msgin =
  406. (struct shim_ipc_pid_getmeta *) msg->msg;
  407. int ret = 0;
  408. debug("ipc callback form %u: IPC_PID_GETMETA(%u, %s)\n", msg->src,
  409. msgin->pid, pid_meta_code_str[msgin->code]);
  410. struct shim_thread * thread = lookup_thread(msgin->pid);
  411. void * data = NULL;
  412. int datasize = 0;
  413. if (!thread) {
  414. ret = -ESRCH;
  415. goto out;
  416. }
  417. lock(thread->lock);
  418. switch (msgin->code) {
  419. case PID_META_CRED:
  420. datasize = sizeof(IDTYPE) * 2;
  421. data = __alloca(datasize);
  422. ((IDTYPE *) data)[0] = thread->uid;
  423. ((IDTYPE *) data)[1] = thread->gid;
  424. break;
  425. case PID_META_EXEC:
  426. if (!thread->exec || !thread->exec->dentry) {
  427. ret = -ENOENT;
  428. break;
  429. }
  430. data = dentry_get_path(thread->exec->dentry, true, &datasize);
  431. break;
  432. case PID_META_CWD:
  433. if (!thread->cwd) {
  434. ret = -ENOENT;
  435. break;
  436. }
  437. data = dentry_get_path(thread->cwd, true, &datasize);
  438. break;
  439. case PID_META_ROOT:
  440. if (!thread->root) {
  441. ret = -ENOENT;
  442. break;
  443. }
  444. data = dentry_get_path(thread->root, true, &datasize);
  445. break;
  446. default:
  447. ret = -EINVAL;
  448. break;
  449. }
  450. unlock(thread->lock);
  451. put_thread(thread);
  452. if (ret < 0)
  453. goto out;
  454. ret = ipc_pid_retmeta_send(port, msg->src, msgin->pid, msgin->code,
  455. data, datasize, msg->seq);
  456. out:
  457. SAVE_PROFILE_INTERVAL(ipc_pid_getmeta_callback);
  458. return ret;
  459. }
  460. DEFINE_PROFILE_INTERVAL(ipc_pid_retmeta_send, ipc);
  461. DEFINE_PROFILE_INTERVAL(ipc_pid_retmeta_callback, ipc);
  462. int ipc_pid_retmeta_send (struct shim_ipc_port * port, IDTYPE dest,
  463. IDTYPE pid, enum pid_meta_code code,
  464. const void * data, int datasize,
  465. unsigned long seq)
  466. {
  467. BEGIN_PROFILE_INTERVAL();
  468. int ret;
  469. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  470. IPC_PID_RETMETA,
  471. sizeof(struct shim_ipc_pid_retmeta) +
  472. datasize, dest);
  473. struct shim_ipc_pid_retmeta * msgin =
  474. (struct shim_ipc_pid_retmeta *) &msg->msg;
  475. msgin->pid = pid;
  476. msgin->code = code;
  477. msgin->datasize = datasize;
  478. memcpy(msgin->data, data, datasize);
  479. msg->seq = seq;
  480. debug("ipc send to %u: IPC_PID_RETMETA(%d, %s, %d)\n", dest,
  481. pid, pid_meta_code_str[code], datasize);
  482. ret = send_ipc_message(msg, port);
  483. SAVE_PROFILE_INTERVAL(ipc_pid_retmeta_send);
  484. return ret;
  485. }
  486. int ipc_pid_retmeta_callback (IPC_CALLBACK_ARGS)
  487. {
  488. BEGIN_PROFILE_INTERVAL();
  489. struct shim_ipc_pid_retmeta * msgin =
  490. (struct shim_ipc_pid_retmeta *) msg->msg;
  491. debug("ipc callback form %u: IPC_PID_RETMETA(%u, %s, %d)\n", msg->src,
  492. msgin->pid, pid_meta_code_str[msgin->code], msgin->datasize);
  493. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  494. if (obj) {
  495. void ** data = (void **) obj->private;
  496. if (data)
  497. *data = msgin->datasize ?
  498. malloc_copy(msgin->data, msgin->datasize) : NULL;
  499. obj->retval = msgin->datasize;
  500. if (obj->thread)
  501. thread_wakeup(obj->thread);
  502. }
  503. SAVE_PROFILE_INTERVAL(ipc_pid_retmeta_callback);
  504. return 0;
  505. }
  506. int get_pid_port (IDTYPE pid, IDTYPE * dest, struct shim_ipc_port ** port)
  507. {
  508. IDTYPE owner;
  509. int ret;
  510. if ((ret = connect_owner(pid, port, &owner)) < 0)
  511. return ret;
  512. if (dest)
  513. *dest = owner;
  514. return 0;
  515. }
  516. DEFINE_PROFILE_INTERVAL(ipc_pid_nop_send, ipc);
  517. DEFINE_PROFILE_INTERVAL(ipc_pid_nop_callback, ipc);
  518. int ipc_pid_nop_send (struct shim_ipc_port * port, IDTYPE dest, int count,
  519. const void * buf, int len)
  520. {
  521. BEGIN_PROFILE_INTERVAL();
  522. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  523. IPC_PID_NOP,
  524. sizeof(struct shim_ipc_pid_nop) +
  525. len, dest);
  526. struct shim_ipc_pid_nop * msgin =
  527. (struct shim_ipc_pid_nop *) &msg->msg.msg;
  528. msgin->count = count * 2;
  529. memcpy(msgin->payload, buf, len);
  530. debug("ipc send to %u: IPC_PID_NOP(%d)\n", dest, count * 2);
  531. SAVE_PROFILE_INTERVAL(ipc_pid_nop_send);
  532. return do_ipc_duplex(msg, port, NULL, NULL);
  533. }
  534. int ipc_pid_nop_callback (IPC_CALLBACK_ARGS)
  535. {
  536. BEGIN_PROFILE_INTERVAL();
  537. struct shim_ipc_pid_nop * msgin =
  538. (struct shim_ipc_pid_nop *) &msg->msg;
  539. debug("ipc callback form %u: IPC_PID_NOP(%d)\n", msg->src,
  540. msgin->count);
  541. if (!(--msgin->count)) {
  542. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  543. if (obj && obj->thread)
  544. thread_wakeup(obj->thread);
  545. SAVE_PROFILE_INTERVAL(ipc_pid_nop_callback);
  546. return 0;
  547. }
  548. SAVE_PROFILE_INTERVAL(ipc_pid_nop_callback);
  549. debug("ipc send to %u: IPC_PID_NOP(%d)\n", msg->src,
  550. msgin->count);
  551. int ret = send_ipc_message(msg, port);
  552. SAVE_PROFILE_INTERVAL(ipc_pid_nop_send);
  553. return ret;
  554. }
  555. DEFINE_PROFILE_INTERVAL(ipc_pid_sendrpc_send, ipc);
  556. DEFINE_PROFILE_INTERVAL(ipc_pid_sendrpc_callback, ipc);
  557. int ipc_pid_sendrpc_send (IDTYPE pid, IDTYPE sender, const void * buf,
  558. int len)
  559. {
  560. BEGIN_PROFILE_INTERVAL();
  561. int ret = 0;
  562. IDTYPE dest;
  563. struct shim_ipc_port * port = NULL;
  564. if ((ret = get_pid_port(pid, &dest, &port)) < 0)
  565. return ret;
  566. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  567. IPC_PID_SENDRPC,
  568. sizeof(struct shim_ipc_pid_sendrpc) +
  569. len, dest);
  570. struct shim_ipc_pid_sendrpc * msgin =
  571. (struct shim_ipc_pid_sendrpc *) &msg->msg;
  572. debug("ipc send to %u: IPC_PID_SENDPRC(%d)\n", dest, len);
  573. msgin->sender = sender;
  574. msgin->len = len;
  575. memcpy(msgin->payload, buf, len);
  576. ret = send_ipc_message(msg, port);
  577. put_ipc_port(port);
  578. SAVE_PROFILE_INTERVAL(ipc_pid_sendrpc_send);
  579. return ret;
  580. }
  581. DEFINE_LIST(rpcmsg);
  582. struct rpcmsg {
  583. LIST_TYPE(rpcmsg) list;
  584. IDTYPE sender;
  585. int len;
  586. char payload[];
  587. };
  588. DEFINE_LIST(rpcreq);
  589. struct rpcreq {
  590. LIST_TYPE(rpcreq) list;
  591. struct shim_thread * thread;
  592. IDTYPE sender;
  593. int len;
  594. void * buffer;
  595. };
  596. DEFINE_LISTP(rpcmsg);
  597. DEFINE_LISTP(rpcreq);
  598. static LISTP_TYPE(rpcmsg) rpc_msgs;
  599. static LISTP_TYPE(rpcreq) rpc_reqs;
  600. static LOCKTYPE rpc_queue_lock;
  601. int get_rpc_msg (IDTYPE * sender, void * buf, int len)
  602. {
  603. create_lock_runtime(&rpc_queue_lock);
  604. lock(rpc_queue_lock);
  605. if (!listp_empty(&rpc_msgs)) {
  606. struct rpcmsg * m = listp_first_entry(&rpc_msgs, struct rpcmsg, list);
  607. listp_del(m, &rpc_msgs, list);
  608. if (m->len < len)
  609. len = m->len;
  610. if (sender)
  611. *sender = m->sender;
  612. memcpy(buf, m->payload, len);
  613. unlock(rpc_queue_lock);
  614. return len;
  615. }
  616. struct rpcreq * r = malloc(sizeof(struct rpcreq));
  617. if (!r) {
  618. unlock(rpc_queue_lock);
  619. return -ENOMEM;
  620. }
  621. INIT_LIST_HEAD(r, list);
  622. r->sender = 0;
  623. r->len = len;
  624. r->buffer = buf;
  625. thread_setwait(&r->thread, NULL);
  626. listp_add_tail(r, &rpc_reqs, list);
  627. unlock(rpc_queue_lock);
  628. thread_sleep(NO_TIMEOUT);
  629. put_thread(r->thread);
  630. if (sender)
  631. *sender = r->sender;
  632. return r->len;
  633. }
  634. int ipc_pid_sendrpc_callback (IPC_CALLBACK_ARGS)
  635. {
  636. BEGIN_PROFILE_INTERVAL();
  637. int ret = 0;
  638. struct shim_ipc_pid_sendrpc * msgin =
  639. (struct shim_ipc_pid_sendrpc *) msg->msg;
  640. debug("ipc callback from %u: IPC_PID_SENDPRC(%u, %d)\n", msg->src,
  641. msgin->sender, msgin->len);
  642. create_lock_runtime(&rpc_queue_lock);
  643. lock(rpc_queue_lock);
  644. if (!listp_empty(&rpc_reqs)) {
  645. struct rpcreq * r = listp_first_entry(&rpc_reqs, struct rpcreq, list);
  646. listp_del(r, &rpc_reqs, list);
  647. if (msgin->len < r->len)
  648. r->len = msgin->len;
  649. r->sender = msgin->sender;
  650. memcpy(r->buffer, msgin->payload, r->len);
  651. thread_wakeup(r->thread);
  652. goto out;
  653. }
  654. struct rpcmsg * m = malloc(sizeof(struct rpcmsg) + msgin->len);
  655. if (!m) {
  656. ret = -ENOMEM;
  657. goto out;
  658. }
  659. INIT_LIST_HEAD(m, list);
  660. m->sender = msgin->sender;
  661. m->len = msgin->len;
  662. memcpy(m->payload, msgin->payload, msgin->len);
  663. listp_add_tail(m, &rpc_msgs, list);
  664. out:
  665. unlock(rpc_queue_lock);
  666. SAVE_PROFILE_INTERVAL(ipc_pid_sendrpc_callback);
  667. return ret;
  668. }