signalfd.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/signalfd.c
  4. *
  5. * Copyright (C) 2003 Linus Torvalds
  6. *
  7. * Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
  8. * Changed ->read() to return a siginfo strcture instead of signal number.
  9. * Fixed locking in ->poll().
  10. * Added sighand-detach notification.
  11. * Added fd re-use in sys_signalfd() syscall.
  12. * Now using anonymous inode source.
  13. * Thanks to Oleg Nesterov for useful code review and suggestions.
  14. * More comments and suggestions from Arnd Bergmann.
  15. * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
  16. * Retrieve multiple signals with one read() call
  17. * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
  18. * Attach to the sighand only during read() and poll().
  19. */
  20. #include <linux/file.h>
  21. #include <linux/poll.h>
  22. #include <linux/init.h>
  23. #include <linux/fs.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/kernel.h>
  27. #include <linux/signal.h>
  28. #include <linux/list.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/signalfd.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/compat.h>
  34. void signalfd_cleanup(struct sighand_struct *sighand)
  35. {
  36. wait_queue_head_t *wqh = &sighand->signalfd_wqh;
  37. /*
  38. * The lockless check can race with remove_wait_queue() in progress,
  39. * but in this case its caller should run under rcu_read_lock() and
  40. * sighand_cachep is SLAB_TYPESAFE_BY_RCU, we can safely return.
  41. */
  42. if (likely(!waitqueue_active(wqh)))
  43. return;
  44. /* wait_queue_entry_t->func(POLLFREE) should do remove_wait_queue() */
  45. wake_up_poll(wqh, EPOLLHUP | POLLFREE);
  46. }
  47. struct signalfd_ctx {
  48. sigset_t sigmask;
  49. };
  50. static int signalfd_release(struct inode *inode, struct file *file)
  51. {
  52. kfree(file->private_data);
  53. return 0;
  54. }
  55. static __poll_t signalfd_poll(struct file *file, poll_table *wait)
  56. {
  57. struct signalfd_ctx *ctx = file->private_data;
  58. __poll_t events = 0;
  59. poll_wait(file, &current->sighand->signalfd_wqh, wait);
  60. spin_lock_irq(&current->sighand->siglock);
  61. if (next_signal(&current->pending, &ctx->sigmask) ||
  62. next_signal(&current->signal->shared_pending,
  63. &ctx->sigmask))
  64. events |= EPOLLIN;
  65. spin_unlock_irq(&current->sighand->siglock);
  66. return events;
  67. }
  68. /*
  69. * Copied from copy_siginfo_to_user() in kernel/signal.c
  70. */
  71. static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
  72. siginfo_t const *kinfo)
  73. {
  74. struct signalfd_siginfo new;
  75. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  76. /*
  77. * Unused members should be zero ...
  78. */
  79. memset(&new, 0, sizeof(new));
  80. /*
  81. * If you change siginfo_t structure, please be sure
  82. * this code is fixed accordingly.
  83. */
  84. new.ssi_signo = kinfo->si_signo;
  85. new.ssi_errno = kinfo->si_errno;
  86. new.ssi_code = kinfo->si_code;
  87. switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
  88. case SIL_KILL:
  89. new.ssi_pid = kinfo->si_pid;
  90. new.ssi_uid = kinfo->si_uid;
  91. break;
  92. case SIL_TIMER:
  93. new.ssi_tid = kinfo->si_tid;
  94. new.ssi_overrun = kinfo->si_overrun;
  95. new.ssi_ptr = (long) kinfo->si_ptr;
  96. new.ssi_int = kinfo->si_int;
  97. break;
  98. case SIL_POLL:
  99. new.ssi_band = kinfo->si_band;
  100. new.ssi_fd = kinfo->si_fd;
  101. break;
  102. case SIL_FAULT_BNDERR:
  103. case SIL_FAULT_PKUERR:
  104. /*
  105. * Fall through to the SIL_FAULT case. Both SIL_FAULT_BNDERR
  106. * and SIL_FAULT_PKUERR are only generated by faults that
  107. * deliver them synchronously to userspace. In case someone
  108. * injects one of these signals and signalfd catches it treat
  109. * it as SIL_FAULT.
  110. */
  111. case SIL_FAULT:
  112. new.ssi_addr = (long) kinfo->si_addr;
  113. #ifdef __ARCH_SI_TRAPNO
  114. new.ssi_trapno = kinfo->si_trapno;
  115. #endif
  116. break;
  117. case SIL_FAULT_MCEERR:
  118. new.ssi_addr = (long) kinfo->si_addr;
  119. #ifdef __ARCH_SI_TRAPNO
  120. new.ssi_trapno = kinfo->si_trapno;
  121. #endif
  122. new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
  123. break;
  124. case SIL_CHLD:
  125. new.ssi_pid = kinfo->si_pid;
  126. new.ssi_uid = kinfo->si_uid;
  127. new.ssi_status = kinfo->si_status;
  128. new.ssi_utime = kinfo->si_utime;
  129. new.ssi_stime = kinfo->si_stime;
  130. break;
  131. case SIL_RT:
  132. /*
  133. * This case catches also the signals queued by sigqueue().
  134. */
  135. new.ssi_pid = kinfo->si_pid;
  136. new.ssi_uid = kinfo->si_uid;
  137. new.ssi_ptr = (long) kinfo->si_ptr;
  138. new.ssi_int = kinfo->si_int;
  139. break;
  140. case SIL_SYS:
  141. new.ssi_call_addr = (long) kinfo->si_call_addr;
  142. new.ssi_syscall = kinfo->si_syscall;
  143. new.ssi_arch = kinfo->si_arch;
  144. break;
  145. }
  146. if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
  147. return -EFAULT;
  148. return sizeof(*uinfo);
  149. }
  150. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
  151. int nonblock)
  152. {
  153. ssize_t ret;
  154. DECLARE_WAITQUEUE(wait, current);
  155. spin_lock_irq(&current->sighand->siglock);
  156. ret = dequeue_signal(current, &ctx->sigmask, info);
  157. switch (ret) {
  158. case 0:
  159. if (!nonblock)
  160. break;
  161. ret = -EAGAIN;
  162. default:
  163. spin_unlock_irq(&current->sighand->siglock);
  164. return ret;
  165. }
  166. add_wait_queue(&current->sighand->signalfd_wqh, &wait);
  167. for (;;) {
  168. set_current_state(TASK_INTERRUPTIBLE);
  169. ret = dequeue_signal(current, &ctx->sigmask, info);
  170. if (ret != 0)
  171. break;
  172. if (signal_pending(current)) {
  173. ret = -ERESTARTSYS;
  174. break;
  175. }
  176. spin_unlock_irq(&current->sighand->siglock);
  177. schedule();
  178. spin_lock_irq(&current->sighand->siglock);
  179. }
  180. spin_unlock_irq(&current->sighand->siglock);
  181. remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
  182. __set_current_state(TASK_RUNNING);
  183. return ret;
  184. }
  185. /*
  186. * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
  187. * error code. The "count" parameter must be at least the size of a
  188. * "struct signalfd_siginfo".
  189. */
  190. static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
  191. loff_t *ppos)
  192. {
  193. struct signalfd_ctx *ctx = file->private_data;
  194. struct signalfd_siginfo __user *siginfo;
  195. int nonblock = file->f_flags & O_NONBLOCK;
  196. ssize_t ret, total = 0;
  197. siginfo_t info;
  198. count /= sizeof(struct signalfd_siginfo);
  199. if (!count)
  200. return -EINVAL;
  201. siginfo = (struct signalfd_siginfo __user *) buf;
  202. do {
  203. ret = signalfd_dequeue(ctx, &info, nonblock);
  204. if (unlikely(ret <= 0))
  205. break;
  206. ret = signalfd_copyinfo(siginfo, &info);
  207. if (ret < 0)
  208. break;
  209. siginfo++;
  210. total += ret;
  211. nonblock = 1;
  212. } while (--count);
  213. return total ? total: ret;
  214. }
  215. #ifdef CONFIG_PROC_FS
  216. static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
  217. {
  218. struct signalfd_ctx *ctx = f->private_data;
  219. sigset_t sigmask;
  220. sigmask = ctx->sigmask;
  221. signotset(&sigmask);
  222. render_sigset_t(m, "sigmask:\t", &sigmask);
  223. }
  224. #endif
  225. static const struct file_operations signalfd_fops = {
  226. #ifdef CONFIG_PROC_FS
  227. .show_fdinfo = signalfd_show_fdinfo,
  228. #endif
  229. .release = signalfd_release,
  230. .poll = signalfd_poll,
  231. .read = signalfd_read,
  232. .llseek = noop_llseek,
  233. };
  234. static int do_signalfd4(int ufd, sigset_t *mask, int flags)
  235. {
  236. struct signalfd_ctx *ctx;
  237. /* Check the SFD_* constants for consistency. */
  238. BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
  239. BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
  240. if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
  241. return -EINVAL;
  242. sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  243. signotset(mask);
  244. if (ufd == -1) {
  245. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  246. if (!ctx)
  247. return -ENOMEM;
  248. ctx->sigmask = *mask;
  249. /*
  250. * When we call this, the initialization must be complete, since
  251. * anon_inode_getfd() will install the fd.
  252. */
  253. ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
  254. O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
  255. if (ufd < 0)
  256. kfree(ctx);
  257. } else {
  258. struct fd f = fdget(ufd);
  259. if (!f.file)
  260. return -EBADF;
  261. ctx = f.file->private_data;
  262. if (f.file->f_op != &signalfd_fops) {
  263. fdput(f);
  264. return -EINVAL;
  265. }
  266. spin_lock_irq(&current->sighand->siglock);
  267. ctx->sigmask = *mask;
  268. spin_unlock_irq(&current->sighand->siglock);
  269. wake_up(&current->sighand->signalfd_wqh);
  270. fdput(f);
  271. }
  272. return ufd;
  273. }
  274. SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
  275. size_t, sizemask, int, flags)
  276. {
  277. sigset_t mask;
  278. if (sizemask != sizeof(sigset_t) ||
  279. copy_from_user(&mask, user_mask, sizeof(mask)))
  280. return -EINVAL;
  281. return do_signalfd4(ufd, &mask, flags);
  282. }
  283. SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
  284. size_t, sizemask)
  285. {
  286. sigset_t mask;
  287. if (sizemask != sizeof(sigset_t) ||
  288. copy_from_user(&mask, user_mask, sizeof(mask)))
  289. return -EINVAL;
  290. return do_signalfd4(ufd, &mask, 0);
  291. }
  292. #ifdef CONFIG_COMPAT
  293. static long do_compat_signalfd4(int ufd,
  294. const compat_sigset_t __user *user_mask,
  295. compat_size_t sigsetsize, int flags)
  296. {
  297. sigset_t mask;
  298. if (sigsetsize != sizeof(compat_sigset_t))
  299. return -EINVAL;
  300. if (get_compat_sigset(&mask, user_mask))
  301. return -EFAULT;
  302. return do_signalfd4(ufd, &mask, flags);
  303. }
  304. COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
  305. const compat_sigset_t __user *, user_mask,
  306. compat_size_t, sigsetsize,
  307. int, flags)
  308. {
  309. return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
  310. }
  311. COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
  312. const compat_sigset_t __user *, user_mask,
  313. compat_size_t, sigsetsize)
  314. {
  315. return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
  316. }
  317. #endif