shim_sigaction.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. #include <linux/signal.h>
  31. int shim_do_sigaction (int signum, const struct __kernel_sigaction * act,
  32. struct __kernel_sigaction * oldact)
  33. {
  34. /* SIGKILL and SIGSTOP cannot be caught or ignored */
  35. if (signum == SIGKILL || signum == SIGSTOP ||
  36. signum <= 0 || signum > NUM_SIGS)
  37. return -EINVAL;
  38. struct shim_thread * cur = get_cur_thread();
  39. int err = 0;
  40. assert(!act || (void *) act->k_sa_handler != (void *) 0x11);
  41. struct shim_signal_handle * sighdl = &cur->signal_handles[signum - 1];
  42. lock(cur->lock);
  43. if (oldact) {
  44. if (sighdl->action) {
  45. memcpy(oldact, sighdl->action, sizeof(struct __kernel_sigaction));
  46. } else {
  47. memset(oldact, 0, sizeof(struct __kernel_sigaction));
  48. oldact->k_sa_handler = SIG_DFL;
  49. }
  50. }
  51. if (act) {
  52. if (!(sighdl->action))
  53. sighdl->action = malloc(sizeof(struct __kernel_sigaction));
  54. if (!(sighdl->action)) {
  55. err = -ENOMEM;
  56. goto out;
  57. }
  58. memcpy(sighdl->action, act, sizeof(struct __kernel_sigaction));
  59. }
  60. err = 0;
  61. out:
  62. unlock(cur->lock);
  63. return err;
  64. }
  65. int shim_do_sigreturn (int __unused)
  66. {
  67. /* do nothing */
  68. return 0;
  69. }
  70. int shim_do_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset)
  71. {
  72. __sigset_t * old, tmp, set_tmp;
  73. if (how != SIG_BLOCK && how != SIG_UNBLOCK &&
  74. how != SIG_SETMASK)
  75. return -EINVAL;
  76. struct shim_thread * cur = get_cur_thread();
  77. int err = 0;
  78. lock(cur->lock);
  79. old = get_sig_mask(cur);
  80. if (oldset) {
  81. memcpy(&tmp, old, sizeof(__sigset_t));
  82. old = &tmp;
  83. }
  84. /* if set is NULL, then the signal mask is unchanged, but the current
  85. value of the signal mask is nevertheless returned in oldset */
  86. if (!set)
  87. goto out;
  88. memcpy(&set_tmp, old, sizeof(__sigset_t));
  89. switch (how) {
  90. case SIG_BLOCK:
  91. __sigorset(&set_tmp, &set_tmp, set);
  92. break;
  93. case SIG_UNBLOCK:
  94. __signotset(&set_tmp, &set_tmp, set);
  95. break;
  96. case SIG_SETMASK:
  97. memcpy(&set_tmp, set, sizeof(__sigset_t));
  98. break;
  99. }
  100. set_sig_mask(cur, &set_tmp);
  101. out:
  102. unlock(cur->lock);
  103. if (!err && oldset)
  104. memcpy(oldset, old, sizeof(__sigset_t));
  105. return err;
  106. }
  107. int shim_do_sigaltstack (const stack_t * ss, stack_t * oss)
  108. {
  109. if (ss && (ss->ss_flags & ~SS_DISABLE))
  110. return -EINVAL;
  111. struct shim_thread * cur = get_cur_thread();
  112. int err = 0;
  113. lock(cur->lock);
  114. if (oss)
  115. *oss = cur->signal_altstack;
  116. if (ss)
  117. cur->signal_altstack = *ss;
  118. unlock(cur->lock);
  119. return 0;
  120. }
  121. int shim_do_sigsuspend (const __sigset_t * mask)
  122. {
  123. __sigset_t * old, tmp;
  124. struct shim_thread * cur = get_cur_thread();
  125. int err = 0;
  126. lock(cur->lock);
  127. old = get_sig_mask(cur);
  128. memcpy(&tmp, old, sizeof(__sigset_t));
  129. old = &tmp;
  130. set_sig_mask(cur, mask);
  131. cur->suspend_on_signal = true;
  132. thread_setwait(NULL, NULL);
  133. thread_sleep(NO_TIMEOUT);
  134. out:
  135. unlock(cur->lock);
  136. set_sig_mask(cur, old);
  137. return err;
  138. }
  139. struct walk_arg {
  140. struct shim_thread * current;
  141. IDTYPE sender;
  142. IDTYPE id;
  143. int sig;
  144. bool use_ipc;
  145. };
  146. static inline void __append_signal (struct shim_thread * thread, int sig,
  147. IDTYPE sender)
  148. {
  149. debug("Thread %d killed by signal %d\n", thread->tid, sig);
  150. siginfo_t info;
  151. memset(&info, 0, sizeof(siginfo_t));
  152. info.si_signo = sig;
  153. info.si_pid = sender;
  154. append_signal(thread, sig, &info, true);
  155. }
  156. static int __kill_proc (struct shim_thread * thread, void * arg,
  157. bool * unlocked)
  158. {
  159. struct walk_arg * warg = (struct walk_arg *) arg;
  160. int srched = 0;
  161. if (!warg->use_ipc && !thread->in_vm)
  162. return 0;
  163. if (thread->tgid != warg->id)
  164. return 0;
  165. if (warg->current == thread)
  166. return 1;
  167. /* DEP: Let's do a racy read of is_alive and in_vm.
  168. * If either of these are zero it is a stable condition,
  169. * and we can elide the lock acquire (which helps perf).
  170. */
  171. if (!thread->is_alive)
  172. goto out;
  173. if (!thread->in_vm) {
  174. unlock(thread_list_lock);
  175. *unlocked = true;
  176. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  177. warg->sig)) ? 1 : 0;
  178. } else {
  179. lock(thread->lock);
  180. if (!thread->is_alive)
  181. goto out_locked;
  182. if (thread->in_vm) {
  183. if (warg->sig > 0)
  184. __append_signal(thread, warg->sig, warg->sender);
  185. srched = 1;
  186. } else {
  187. /* This double-check case is probably unnecessary, but keep it for now */
  188. unlock(thread->lock);
  189. unlock(thread_list_lock);
  190. *unlocked = true;
  191. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  192. warg->sig)) ? 1 : 0;
  193. }
  194. }
  195. out_locked:
  196. unlock(thread->lock);
  197. out:
  198. return srched;
  199. }
  200. static int __kill_proc_simple (struct shim_simple_thread * sthread,
  201. void * arg, bool * unlocked)
  202. {
  203. struct walk_arg * warg = (struct walk_arg *) arg;
  204. int srched = 0;
  205. if (sthread->tgid != warg->id)
  206. return 0;
  207. lock(sthread->lock);
  208. if (sthread->is_alive) {
  209. unlock(sthread->lock);
  210. unlock(thread_list_lock);
  211. *unlocked = true;
  212. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PROCESS,
  213. warg->sig)) ? 1 : 0;
  214. }
  215. unlock(sthread->lock);
  216. return srched;
  217. }
  218. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc)
  219. {
  220. struct shim_thread * cur = get_cur_thread();
  221. if (!tgid) {
  222. /* DEP: cur->tgid never changes. No lock needed */
  223. tgid = cur->tgid;
  224. }
  225. struct walk_arg arg;
  226. arg.current = cur;
  227. arg.sender = sender;
  228. arg.id = tgid;
  229. arg.sig = sig;
  230. arg.use_ipc = use_ipc;
  231. bool srched = false;
  232. if (!walk_thread_list(__kill_proc, &arg, false))
  233. srched = true;
  234. if (!use_ipc || srched)
  235. goto out;
  236. if (!walk_simple_thread_list(__kill_proc_simple, &arg, false))
  237. srched = true;
  238. if (!srched && !ipc_pid_kill_send(sender, tgid, KILL_PROCESS, sig))
  239. srched = true;
  240. out:
  241. return srched ? 0 : -ESRCH;
  242. }
  243. static int __kill_pgroup (struct shim_thread * thread, void * arg,
  244. bool * unlocked)
  245. {
  246. struct walk_arg * warg = (struct walk_arg *) arg;
  247. int srched = 0;
  248. if (!warg->use_ipc && !thread->in_vm)
  249. return 0;
  250. if (thread->pgid != warg->id)
  251. return 0;
  252. if (warg->current == thread)
  253. return 1;
  254. lock(thread->lock);
  255. if (!thread->is_alive)
  256. goto out;
  257. if (thread->in_vm) {
  258. if (warg->sig > 0)
  259. __append_signal(thread, warg->sig, warg->sender);
  260. srched = 1;
  261. } else {
  262. unlock(thread->lock);
  263. unlock(thread_list_lock);
  264. *unlocked = true;
  265. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  266. warg->sig)) ? 1 : 0;
  267. }
  268. out:
  269. unlock(thread->lock);
  270. return srched;
  271. }
  272. static int __kill_pgroup_simple (struct shim_simple_thread * sthread,
  273. void * arg, bool * unlocked)
  274. {
  275. struct walk_arg * warg = (struct walk_arg *) arg;
  276. int srched = 0;
  277. if (sthread->pgid != warg->id)
  278. return 0;
  279. lock(sthread->lock);
  280. if (sthread->is_alive) {
  281. unlock(sthread->lock);
  282. unlock(thread_list_lock);
  283. *unlocked = true;
  284. return (!ipc_pid_kill_send(warg->sender, warg->id, KILL_PGROUP,
  285. warg->sig)) ? 1 : 0;
  286. }
  287. unlock(sthread->lock);
  288. return srched;
  289. }
  290. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc)
  291. {
  292. struct shim_thread * cur = get_cur_thread();
  293. if (!pgid) {
  294. pgid = cur->pgid;
  295. }
  296. struct walk_arg arg;
  297. arg.current = cur;
  298. arg.sender = sender;
  299. arg.id = pgid;
  300. arg.sig = sig;
  301. arg.use_ipc = use_ipc;
  302. bool srched = false;
  303. if (!walk_thread_list(__kill_pgroup, &arg, false))
  304. srched = true;
  305. if (!use_ipc || srched)
  306. goto out;
  307. if (!walk_simple_thread_list(__kill_pgroup_simple, &arg, false))
  308. srched = true;
  309. if (!srched && !ipc_pid_kill_send(sender, pgid, KILL_PGROUP, sig))
  310. srched = true;
  311. out:
  312. return srched ? 0 : -ESRCH;
  313. }
  314. static int __kill_all_threads (struct shim_thread * thread, void * arg,
  315. bool * unlocked)
  316. {
  317. int srched = 0;
  318. struct walk_arg * warg = (struct walk_arg *) arg;
  319. if (thread->tgid != thread->tid)
  320. return 0;
  321. if (warg->current == thread)
  322. return 1;
  323. lock(thread->lock);
  324. if (thread->in_vm) {
  325. __append_signal(thread, warg->sig, warg->sender);
  326. srched = 1;
  327. }
  328. unlock(thread->lock);
  329. return srched;
  330. }
  331. int broadcast_signal (IDTYPE sender, int sig);
  332. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig)
  333. {
  334. struct walk_arg arg;
  335. arg.current = cur;
  336. arg.sender = sender;
  337. arg.id = 0;
  338. arg.sig = sig;
  339. arg.use_ipc = false;
  340. walk_thread_list(__kill_all_threads, &arg, false);
  341. return 0;
  342. }
  343. int shim_do_kill (pid_t pid, int sig)
  344. {
  345. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  346. if (sig < 0 || sig > NUM_SIGS)
  347. return -EINVAL;
  348. struct shim_thread * cur = get_cur_thread();
  349. int ret = 0;
  350. bool send_to_self = false;
  351. /* If pid equals 0, then sig is sent to every process in the process group
  352. of the calling process. */
  353. if (pid == 0) {
  354. ret = do_kill_pgroup(cur->tgid, 0, sig, true);
  355. send_to_self = true;
  356. }
  357. /* If pid equals -1, then sig is sent to every process for which the
  358. calling process has permission to send */
  359. else if (pid == -1) {
  360. broadcast_signal(cur->tid, sig);
  361. kill_all_threads(cur, cur->tid, sig);
  362. send_to_self = true;
  363. }
  364. /* If pid is positive, then signal sig is sent to the process with the ID
  365. specified by pid. */
  366. else if (pid > 0) {
  367. ret = do_kill_proc(cur->tid, pid, sig, true);
  368. send_to_self = (pid == cur->pgid);
  369. }
  370. /* If pid is less than -1, then sig is sent to every process in the
  371. process group whose id is -pid */
  372. else {
  373. ret = do_kill_pgroup(cur->tid, -pid, sig, true);
  374. send_to_self = (-pid == cur->pgid);
  375. }
  376. if (send_to_self) {
  377. if (ret == -ESRCH)
  378. ret = 0;
  379. if (sig) {
  380. siginfo_t info;
  381. memset(&info, 0, sizeof(siginfo_t));
  382. info.si_signo = sig;
  383. info.si_pid = cur->tid;
  384. deliver_signal(&info, NULL);
  385. }
  386. }
  387. return ret < 0 ? ret : 0;
  388. }
  389. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  390. bool use_ipc)
  391. {
  392. if (sig < 0 || sig > NUM_SIGS)
  393. return -EINVAL;
  394. struct shim_thread * thread = lookup_thread(tid);
  395. int ret = 0;
  396. if (thread) {
  397. lock(thread->lock);
  398. if (thread->in_vm) {
  399. if (!tgid || thread->tgid == tgid)
  400. __append_signal(thread, sig, sender);
  401. else
  402. ret = -ESRCH;
  403. } else {
  404. unlock(thread->lock);
  405. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  406. }
  407. unlock(thread->lock);
  408. return ret;
  409. }
  410. if (!use_ipc)
  411. return -ESRCH;
  412. return ipc_pid_kill_send(sender, tid, KILL_THREAD, sig);
  413. }
  414. int shim_do_tkill (pid_t tid, int sig)
  415. {
  416. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  417. if (tid <= 0)
  418. return -EINVAL;
  419. struct shim_thread * cur = get_cur_thread();
  420. if (tid == cur->tid) {
  421. if (sig) {
  422. siginfo_t info;
  423. memset(&info, 0, sizeof(siginfo_t));
  424. info.si_signo = sig;
  425. info.si_pid = cur->tid;
  426. deliver_signal(&info, NULL);
  427. }
  428. return 0;
  429. }
  430. return do_kill_thread(cur->tgid, 0, tid, sig, true);
  431. }
  432. int shim_do_tgkill (pid_t tgid, pid_t tid, int sig)
  433. {
  434. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  435. if (tgid < -1 || tgid == 0 || tid <= 0)
  436. return -EINVAL;
  437. if (tgid == -1)
  438. tgid = 0;
  439. struct shim_thread * cur = get_cur_thread();
  440. if (tid == cur->tid) {
  441. if (sig) {
  442. siginfo_t info;
  443. memset(&info, 0, sizeof(siginfo_t));
  444. info.si_signo = sig;
  445. info.si_pid = cur->tid;
  446. deliver_signal(&info, NULL);
  447. }
  448. return 0;
  449. }
  450. return do_kill_thread(cur->tgid, tgid, tid, sig, true);
  451. }