shim_ipc_pid.c 23 KB

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