shim_sigaction.c 14 KB

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