shim_sigaction.c 13 KB

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