shim_sigaction.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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);
  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. /* only signal thread leader */
  131. if (thread->tgid != warg->id || thread->tid != thread->tgid)
  132. return 0;
  133. if (warg->current == thread)
  134. return 1;
  135. /* DEP: Let's do a racy read of is_alive and in_vm.
  136. * If either of these are zero it is a stable condition,
  137. * and we can elide the lock acquire (which helps perf).
  138. */
  139. if (!thread->is_alive)
  140. goto out;
  141. if (!thread->in_vm) {
  142. unlock(thread_list_lock);
  143. *unlocked = true;
  144. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  145. warg->sig)) ? 1 : 0;
  146. } else {
  147. lock(thread->lock);
  148. if (!thread->is_alive)
  149. goto out_locked;
  150. if (thread->in_vm) {
  151. if (warg->sig > 0)
  152. __append_signal(thread, warg->sig, warg->sender);
  153. srched = 1;
  154. } else {
  155. /* This double-check case is probably unnecessary, but keep it for now */
  156. unlock(thread->lock);
  157. unlock(thread_list_lock);
  158. *unlocked = true;
  159. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  160. warg->sig)) ? 1 : 0;
  161. }
  162. }
  163. out_locked:
  164. unlock(thread->lock);
  165. out:
  166. return srched;
  167. }
  168. static int __kill_proc_simple (struct shim_simple_thread * sthread,
  169. void * arg, bool * unlocked)
  170. {
  171. struct walk_arg * warg = (struct walk_arg *) arg;
  172. int srched = 0;
  173. if (sthread->tgid != warg->id || sthread->tgid != sthread->tid)
  174. return 0;
  175. lock(sthread->lock);
  176. if (sthread->is_alive) {
  177. unlock(sthread->lock);
  178. unlock(thread_list_lock);
  179. *unlocked = true;
  180. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  181. warg->sig)) ? 1 : 0;
  182. }
  183. unlock(sthread->lock);
  184. return srched;
  185. }
  186. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc)
  187. {
  188. struct shim_thread * cur = get_cur_thread();
  189. if (!tgid) {
  190. /* DEP: cur->tgid never changes. No lock needed */
  191. tgid = cur->tgid;
  192. }
  193. struct walk_arg arg;
  194. arg.current = cur;
  195. arg.sender = sender;
  196. arg.id = tgid;
  197. arg.sig = sig;
  198. arg.use_ipc = use_ipc;
  199. bool srched = false;
  200. if (!walk_thread_list(__kill_proc, &arg, false))
  201. srched = true;
  202. if (!use_ipc || srched)
  203. goto out;
  204. if (!walk_simple_thread_list(__kill_proc_simple, &arg, false))
  205. srched = true;
  206. if (!srched && !ipc_pid_kill_send(sender, tgid, KILL_PROCESS, sig))
  207. srched = true;
  208. out:
  209. return srched ? 0 : -ESRCH;
  210. }
  211. static int __kill_pgroup (struct shim_thread * thread, void * arg,
  212. bool * unlocked)
  213. {
  214. struct walk_arg * warg = (struct walk_arg *) arg;
  215. int srched = 0;
  216. if (!warg->use_ipc && !thread->in_vm)
  217. return 0;
  218. /* only signal thread leader */
  219. if (thread->pgid != warg->id || thread->tgid != thread->tid)
  220. return 0;
  221. if (warg->current == thread)
  222. return 1;
  223. lock(thread->lock);
  224. if (!thread->is_alive)
  225. goto out;
  226. if (thread->in_vm) {
  227. if (warg->sig > 0)
  228. __append_signal(thread, warg->sig, warg->sender);
  229. srched = 1;
  230. } else {
  231. unlock(thread->lock);
  232. unlock(thread_list_lock);
  233. *unlocked = true;
  234. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  235. warg->sig)) ? 1 : 0;
  236. }
  237. out:
  238. unlock(thread->lock);
  239. return srched;
  240. }
  241. static int __kill_pgroup_simple (struct shim_simple_thread * sthread,
  242. void * arg, bool * unlocked)
  243. {
  244. struct walk_arg * warg = (struct walk_arg *) arg;
  245. int srched = 0;
  246. if (sthread->pgid != warg->id || sthread->tgid != sthread->tid)
  247. return 0;
  248. lock(sthread->lock);
  249. if (sthread->is_alive) {
  250. unlock(sthread->lock);
  251. unlock(thread_list_lock);
  252. *unlocked = true;
  253. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  254. warg->sig)) ? 1 : 0;
  255. }
  256. unlock(sthread->lock);
  257. return srched;
  258. }
  259. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc)
  260. {
  261. struct shim_thread * cur = get_cur_thread();
  262. if (!pgid) {
  263. pgid = cur->pgid;
  264. }
  265. struct walk_arg arg;
  266. arg.current = cur;
  267. arg.sender = sender;
  268. arg.id = pgid;
  269. arg.sig = sig;
  270. arg.use_ipc = use_ipc;
  271. bool srched = false;
  272. if (!walk_thread_list(__kill_pgroup, &arg, false))
  273. srched = true;
  274. if (!use_ipc || srched)
  275. goto out;
  276. if (!walk_simple_thread_list(__kill_pgroup_simple, &arg, false))
  277. srched = true;
  278. if (!srched && !ipc_pid_kill_send(sender, pgid, KILL_PGROUP, sig))
  279. srched = true;
  280. out:
  281. return srched ? 0 : -ESRCH;
  282. }
  283. static int __kill_all_threads (struct shim_thread * thread, void * arg,
  284. bool * unlocked)
  285. {
  286. int srched = 0;
  287. struct walk_arg * warg = (struct walk_arg *) arg;
  288. if (thread->tgid != thread->tid)
  289. return 0;
  290. if (warg->current == thread)
  291. return 1;
  292. lock(thread->lock);
  293. if (thread->in_vm) {
  294. __append_signal(thread, warg->sig, warg->sender);
  295. srched = 1;
  296. }
  297. unlock(thread->lock);
  298. return srched;
  299. }
  300. int broadcast_signal (IDTYPE sender, int sig);
  301. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig)
  302. {
  303. struct walk_arg arg;
  304. arg.current = cur;
  305. arg.sender = sender;
  306. arg.id = 0;
  307. arg.sig = sig;
  308. arg.use_ipc = false;
  309. walk_thread_list(__kill_all_threads, &arg, false);
  310. return 0;
  311. }
  312. int shim_do_kill (pid_t pid, int sig)
  313. {
  314. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  315. if (sig < 0 || sig > NUM_SIGS)
  316. return -EINVAL;
  317. struct shim_thread * cur = get_cur_thread();
  318. int ret = 0;
  319. bool send_to_self = false;
  320. /* If pid equals 0, then sig is sent to every process in the process group
  321. of the calling process. */
  322. if (pid == 0) {
  323. ret = do_kill_pgroup(cur->tgid, 0, sig, true);
  324. send_to_self = true;
  325. }
  326. /* If pid equals -1, then sig is sent to every process for which the
  327. calling process has permission to send */
  328. else if (pid == -1) {
  329. broadcast_signal(cur->tid, sig);
  330. kill_all_threads(cur, cur->tid, sig);
  331. send_to_self = true;
  332. }
  333. /* If pid is positive, then signal sig is sent to the process with the ID
  334. specified by pid. */
  335. else if (pid > 0) {
  336. ret = do_kill_proc(cur->tid, pid, sig, true);
  337. send_to_self = (pid == cur->pgid);
  338. }
  339. /* If pid is less than -1, then sig is sent to every process in the
  340. process group whose id is -pid */
  341. else {
  342. ret = do_kill_pgroup(cur->tid, -pid, sig, true);
  343. send_to_self = (-pid == cur->pgid);
  344. }
  345. if (send_to_self) {
  346. if (ret == -ESRCH)
  347. ret = 0;
  348. if (sig) {
  349. siginfo_t info;
  350. memset(&info, 0, sizeof(siginfo_t));
  351. info.si_signo = sig;
  352. info.si_pid = cur->tid;
  353. deliver_signal(&info, NULL);
  354. }
  355. }
  356. return ret < 0 ? ret : 0;
  357. }
  358. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  359. bool use_ipc)
  360. {
  361. if (sig < 0 || sig > NUM_SIGS)
  362. return -EINVAL;
  363. struct shim_thread * thread = lookup_thread(tid);
  364. int ret = 0;
  365. if (thread) {
  366. lock(thread->lock);
  367. if (thread->in_vm) {
  368. if (!tgid || thread->tgid == tgid)
  369. __append_signal(thread, sig, sender);
  370. else
  371. ret = -ESRCH;
  372. } else {
  373. unlock(thread->lock);
  374. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  375. }
  376. unlock(thread->lock);
  377. return ret;
  378. }
  379. if (!use_ipc)
  380. return -ESRCH;
  381. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  382. }
  383. int shim_do_tkill (pid_t tid, int sig)
  384. {
  385. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  386. if (tid <= 0)
  387. return -EINVAL;
  388. struct shim_thread * cur = get_cur_thread();
  389. if (tid == cur->tid) {
  390. if (sig) {
  391. siginfo_t info;
  392. memset(&info, 0, sizeof(siginfo_t));
  393. info.si_signo = sig;
  394. info.si_pid = cur->tid;
  395. deliver_signal(&info, NULL);
  396. }
  397. return 0;
  398. }
  399. return do_kill_thread(cur->tgid, 0, tid, sig, true);
  400. }
  401. int shim_do_tgkill (pid_t tgid, pid_t tid, int sig)
  402. {
  403. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  404. if (tgid < -1 || tgid == 0 || tid <= 0)
  405. return -EINVAL;
  406. if (tgid == -1)
  407. tgid = 0;
  408. struct shim_thread * cur = get_cur_thread();
  409. if (tid == cur->tid) {
  410. if (sig) {
  411. siginfo_t info;
  412. memset(&info, 0, sizeof(siginfo_t));
  413. info.si_signo = sig;
  414. info.si_pid = cur->tid;
  415. deliver_signal(&info, NULL);
  416. }
  417. return 0;
  418. }
  419. return do_kill_thread(cur->tgid, tgid, tid, sig, true);
  420. }