shim_ipc_pid.c 24 KB

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