shim_ipc_pid.c 23 KB

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