shim_sigaction.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_sigaction.c
  17. *
  18. * Implementation of system call "sigaction", "sigreturn", "sigprocmask",
  19. * "kill", "tkill" and "tgkill".
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_utils.h>
  23. #include <shim_table.h>
  24. #include <shim_thread.h>
  25. #include <shim_ipc.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <errno.h>
  30. int shim_do_sigaction (int signum, const struct __kernel_sigaction * act,
  31. struct __kernel_sigaction * oldact)
  32. {
  33. /* SIGKILL and SIGSTOP cannot be caught or ignored */
  34. if (signum == SIGKILL || signum == SIGSTOP ||
  35. signum <= 0 || signum > NUM_SIGS)
  36. return -EINVAL;
  37. struct shim_thread * cur = get_cur_thread();
  38. int err = 0;
  39. assert(!act || (void *) act->k_sa_handler != (void *) 0x11);
  40. struct shim_signal_handle * sighdl = &cur->signal_handles[signum - 1];
  41. lock(cur->lock);
  42. if (oldact) {
  43. if (sighdl->action) {
  44. memcpy(oldact, sighdl->action, sizeof(struct __kernel_sigaction));
  45. } else {
  46. memset(oldact, 0, sizeof(struct __kernel_sigaction));
  47. oldact->k_sa_handler = SIG_DFL;
  48. }
  49. }
  50. if (act) {
  51. if (!(sighdl->action))
  52. sighdl->action = malloc(sizeof(struct __kernel_sigaction));
  53. if (!(sighdl->action)) {
  54. err = -ENOMEM;
  55. goto out;
  56. }
  57. memcpy(sighdl->action, act, sizeof(struct __kernel_sigaction));
  58. }
  59. err = 0;
  60. out:
  61. unlock(cur->lock);
  62. return err;
  63. }
  64. int shim_do_sigreturn (int __unused)
  65. {
  66. /* do nothing */
  67. return 0;
  68. }
  69. int shim_do_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset)
  70. {
  71. __sigset_t * old, tmp, set_tmp;
  72. if (how != SIG_BLOCK && how != SIG_UNBLOCK &&
  73. how != SIG_SETMASK)
  74. return -EINVAL;
  75. struct shim_thread * cur = get_cur_thread();
  76. int err = 0;
  77. lock(cur->lock);
  78. old = get_sig_mask(cur);
  79. if (oldset) {
  80. memcpy(&tmp, old, sizeof(__sigset_t));
  81. old = &tmp;
  82. }
  83. /* if set is NULL, then the signal mask is unchanged, but the current
  84. value of the signal mask is nevertheless returned in oldset */
  85. if (!set)
  86. goto out;
  87. memcpy(&set_tmp, old, sizeof(__sigset_t));
  88. switch (how) {
  89. case SIG_BLOCK:
  90. __sigorset(&set_tmp, &set_tmp, set);
  91. break;
  92. case SIG_UNBLOCK:
  93. __signotset(&set_tmp, &set_tmp, set);
  94. break;
  95. case SIG_SETMASK:
  96. memcpy(&set_tmp, set, sizeof(__sigset_t));
  97. break;
  98. }
  99. set_sig_mask(cur, &set_tmp);
  100. out:
  101. unlock(cur->lock);
  102. if (!err && oldset)
  103. memcpy(oldset, old, sizeof(__sigset_t));
  104. return err;
  105. }
  106. struct walk_arg {
  107. struct shim_thread * current;
  108. IDTYPE sender;
  109. IDTYPE id;
  110. int sig;
  111. bool use_ipc;
  112. };
  113. static inline void __append_signal (struct shim_thread * thread, int sig,
  114. IDTYPE sender)
  115. {
  116. debug("Thread %d killed by signal %d\n", thread->tid, sig);
  117. siginfo_t info;
  118. memset(&info, 0, sizeof(siginfo_t));
  119. info.si_signo = sig;
  120. info.si_pid = sender;
  121. append_signal(thread, sig, &info, true);
  122. }
  123. static int __kill_proc (struct shim_thread * thread, void * arg,
  124. bool * unlocked)
  125. {
  126. struct walk_arg * warg = (struct walk_arg *) arg;
  127. int srched = 0;
  128. if (!warg->use_ipc && !thread->in_vm)
  129. return 0;
  130. if (thread->tgid != warg->id)
  131. return 0;
  132. if (warg->current == thread)
  133. return 1;
  134. /* DEP: Let's do a racy read of is_alive and in_vm.
  135. * If either of these are zero it is a stable condition,
  136. * and we can elide the lock acquire (which helps perf).
  137. */
  138. if (!thread->is_alive)
  139. goto out;
  140. if (!thread->in_vm) {
  141. unlock(thread_list_lock);
  142. *unlocked = true;
  143. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  144. warg->sig)) ? 1 : 0;
  145. } else {
  146. lock(thread->lock);
  147. if (!thread->is_alive)
  148. goto out_locked;
  149. if (thread->in_vm) {
  150. if (warg->sig > 0)
  151. __append_signal(thread, warg->sig, warg->sender);
  152. srched = 1;
  153. } else {
  154. /* This double-check case is probably unnecessary, but keep it for now */
  155. unlock(thread->lock);
  156. unlock(thread_list_lock);
  157. *unlocked = true;
  158. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  159. warg->sig)) ? 1 : 0;
  160. }
  161. }
  162. out_locked:
  163. unlock(thread->lock);
  164. out:
  165. return srched;
  166. }
  167. static int __kill_proc_simple (struct shim_simple_thread * sthread,
  168. void * arg, bool * unlocked)
  169. {
  170. struct walk_arg * warg = (struct walk_arg *) arg;
  171. int srched = 0;
  172. if (sthread->tgid != warg->id)
  173. return 0;
  174. lock(sthread->lock);
  175. if (sthread->is_alive) {
  176. unlock(sthread->lock);
  177. unlock(thread_list_lock);
  178. *unlocked = true;
  179. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  180. warg->sig)) ? 1 : 0;
  181. }
  182. unlock(sthread->lock);
  183. return srched;
  184. }
  185. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc)
  186. {
  187. struct shim_thread * cur = get_cur_thread();
  188. if (!tgid) {
  189. /* DEP: cur->tgid never changes. No lock needed */
  190. tgid = cur->tgid;
  191. }
  192. struct walk_arg arg;
  193. arg.current = cur;
  194. arg.sender = sender;
  195. arg.id = tgid;
  196. arg.sig = sig;
  197. arg.use_ipc = use_ipc;
  198. bool srched = false;
  199. if (!walk_thread_list(__kill_proc, &arg, false))
  200. srched = true;
  201. if (!use_ipc || srched)
  202. goto out;
  203. if (!walk_simple_thread_list(__kill_proc_simple, &arg, false))
  204. srched = true;
  205. if (!srched && !ipc_pid_kill_send(sender, tgid, KILL_PROCESS, sig))
  206. srched = true;
  207. out:
  208. return srched ? 0 : -ESRCH;
  209. }
  210. static int __kill_pgroup (struct shim_thread * thread, void * arg,
  211. bool * unlocked)
  212. {
  213. struct walk_arg * warg = (struct walk_arg *) arg;
  214. int srched = 0;
  215. if (!warg->use_ipc && !thread->in_vm)
  216. return 0;
  217. if (thread->pgid != warg->id)
  218. return 0;
  219. if (warg->current == thread)
  220. return 1;
  221. lock(thread->lock);
  222. if (!thread->is_alive)
  223. goto out;
  224. if (thread->in_vm) {
  225. if (warg->sig > 0)
  226. __append_signal(thread, warg->sig, warg->sender);
  227. srched = 1;
  228. } else {
  229. unlock(thread->lock);
  230. unlock(thread_list_lock);
  231. *unlocked = true;
  232. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  233. warg->sig)) ? 1 : 0;
  234. }
  235. out:
  236. unlock(thread->lock);
  237. return srched;
  238. }
  239. static int __kill_pgroup_simple (struct shim_simple_thread * sthread,
  240. void * arg, bool * unlocked)
  241. {
  242. struct walk_arg * warg = (struct walk_arg *) arg;
  243. int srched = 0;
  244. if (sthread->pgid != warg->id)
  245. return 0;
  246. lock(sthread->lock);
  247. if (sthread->is_alive) {
  248. unlock(sthread->lock);
  249. unlock(thread_list_lock);
  250. *unlocked = true;
  251. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  252. warg->sig)) ? 1 : 0;
  253. }
  254. unlock(sthread->lock);
  255. return srched;
  256. }
  257. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc)
  258. {
  259. struct shim_thread * cur = get_cur_thread();
  260. if (!pgid) {
  261. pgid = cur->pgid;
  262. }
  263. struct walk_arg arg;
  264. arg.current = cur;
  265. arg.sender = sender;
  266. arg.id = pgid;
  267. arg.sig = sig;
  268. arg.use_ipc = use_ipc;
  269. bool srched = false;
  270. if (!walk_thread_list(__kill_pgroup, &arg, false))
  271. srched = true;
  272. if (!use_ipc || srched)
  273. goto out;
  274. if (!walk_simple_thread_list(__kill_pgroup_simple, &arg, false))
  275. srched = true;
  276. if (!srched && !ipc_pid_kill_send(sender, pgid, KILL_PGROUP, sig))
  277. srched = true;
  278. out:
  279. return srched ? 0 : -ESRCH;
  280. }
  281. static int __kill_all_threads (struct shim_thread * thread, void * arg,
  282. bool * unlocked)
  283. {
  284. int srched = 0;
  285. struct walk_arg * warg = (struct walk_arg *) arg;
  286. if (thread->tgid != thread->tid)
  287. return 0;
  288. if (warg->current == thread)
  289. return 1;
  290. lock(thread->lock);
  291. if (thread->in_vm) {
  292. __append_signal(thread, warg->sig, warg->sender);
  293. srched = 1;
  294. }
  295. unlock(thread->lock);
  296. return srched;
  297. }
  298. int broadcast_signal (IDTYPE sender, int sig);
  299. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig)
  300. {
  301. struct walk_arg arg;
  302. arg.current = cur;
  303. arg.sender = sender;
  304. arg.id = 0;
  305. arg.sig = sig;
  306. arg.use_ipc = false;
  307. walk_thread_list(__kill_all_threads, &arg, false);
  308. return 0;
  309. }
  310. int shim_do_kill (pid_t pid, int sig)
  311. {
  312. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  313. if (sig < 0 || sig > NUM_SIGS)
  314. return -EINVAL;
  315. struct shim_thread * cur = get_cur_thread();
  316. int ret = 0;
  317. bool send_to_self = false;
  318. /* If pid equals 0, then sig is sent to every process in the process group
  319. of the calling process. */
  320. if (pid == 0) {
  321. ret = do_kill_pgroup(cur->tgid, 0, sig, true);
  322. send_to_self = true;
  323. }
  324. /* If pid equals -1, then sig is sent to every process for which the
  325. calling process has permission to send */
  326. else if (pid == -1) {
  327. broadcast_signal(cur->tid, sig);
  328. kill_all_threads(cur, cur->tid, sig);
  329. send_to_self = true;
  330. }
  331. /* If pid is positive, then signal sig is sent to the process with the ID
  332. specified by pid. */
  333. else if (pid > 0) {
  334. ret = do_kill_proc(cur->tid, pid, sig, true);
  335. send_to_self = (pid == cur->pgid);
  336. }
  337. /* If pid is less than -1, then sig is sent to every process in the
  338. process group whose id is -pid */
  339. else {
  340. ret = do_kill_pgroup(cur->tid, -pid, sig, true);
  341. send_to_self = (-pid == cur->pgid);
  342. }
  343. if (send_to_self) {
  344. if (ret == -ESRCH)
  345. ret = 0;
  346. if (sig) {
  347. siginfo_t info;
  348. memset(&info, 0, sizeof(siginfo_t));
  349. info.si_signo = sig;
  350. info.si_pid = cur->tid;
  351. deliver_signal(&info, NULL);
  352. }
  353. }
  354. return ret < 0 ? ret : 0;
  355. }
  356. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  357. bool use_ipc)
  358. {
  359. if (sig < 0 || sig > NUM_SIGS)
  360. return -EINVAL;
  361. struct shim_thread * thread = lookup_thread(tid);
  362. int ret = 0;
  363. if (thread) {
  364. lock(thread->lock);
  365. if (thread->in_vm) {
  366. if (!tgid || thread->tgid == tgid)
  367. __append_signal(thread, sig, sender);
  368. else
  369. ret = -ESRCH;
  370. } else {
  371. unlock(thread->lock);
  372. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  373. }
  374. unlock(thread->lock);
  375. return ret;
  376. }
  377. if (!use_ipc)
  378. return -ESRCH;
  379. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  380. }
  381. int shim_do_tkill (pid_t tid, int sig)
  382. {
  383. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  384. if (tid <= 0)
  385. return -EINVAL;
  386. struct shim_thread * cur = get_cur_thread();
  387. if (tid == cur->tid) {
  388. if (sig) {
  389. siginfo_t info;
  390. memset(&info, 0, sizeof(siginfo_t));
  391. info.si_signo = sig;
  392. info.si_pid = cur->tid;
  393. deliver_signal(&info, NULL);
  394. }
  395. return 0;
  396. }
  397. return do_kill_thread(cur->tgid, 0, tid, sig, true);
  398. }
  399. int shim_do_tgkill (pid_t tgid, pid_t tid, int sig)
  400. {
  401. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  402. if (tgid < -1 || tgid == 0 || tid <= 0)
  403. return -EINVAL;
  404. if (tgid == -1)
  405. tgid = 0;
  406. struct shim_thread * cur = get_cur_thread();
  407. if (tid == cur->tid) {
  408. if (sig) {
  409. siginfo_t info;
  410. memset(&info, 0, sizeof(siginfo_t));
  411. info.si_signo = sig;
  412. info.si_pid = cur->tid;
  413. deliver_signal(&info, NULL);
  414. }
  415. return 0;
  416. }
  417. return do_kill_thread(cur->tgid, tgid, tid, sig, true);
  418. }