shim_ipc_pid.c 23 KB

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