shim_ipc_pid.c 23 KB

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