shim_ipc_pid.c 22 KB

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