signalfd.c 8.8 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. wake_up_pollfree(&sighand->signalfd_wqh);
  37. }
  38. struct signalfd_ctx {
  39. sigset_t sigmask;
  40. };
  41. static int signalfd_release(struct inode *inode, struct file *file)
  42. {
  43. kfree(file->private_data);
  44. return 0;
  45. }
  46. static __poll_t signalfd_poll(struct file *file, poll_table *wait)
  47. {
  48. struct signalfd_ctx *ctx = file->private_data;
  49. __poll_t events = 0;
  50. poll_wait(file, &current->sighand->signalfd_wqh, wait);
  51. spin_lock_irq(&current->sighand->siglock);
  52. if (next_signal(&current->pending, &ctx->sigmask) ||
  53. next_signal(&current->signal->shared_pending,
  54. &ctx->sigmask))
  55. events |= EPOLLIN;
  56. spin_unlock_irq(&current->sighand->siglock);
  57. return events;
  58. }
  59. /*
  60. * Copied from copy_siginfo_to_user() in kernel/signal.c
  61. */
  62. static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo)
  63. {
  64. struct signalfd_siginfo new;
  65. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  66. /*
  67. * Unused members should be zero ...
  68. */
  69. memset(&new, 0, sizeof(new));
  70. /*
  71. * If you change siginfo_t structure, please be sure
  72. * this code is fixed accordingly.
  73. */
  74. new.ssi_signo = kinfo->si_signo;
  75. new.ssi_errno = kinfo->si_errno;
  76. new.ssi_code = kinfo->si_code;
  77. switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
  78. case SIL_KILL:
  79. new.ssi_pid = kinfo->si_pid;
  80. new.ssi_uid = kinfo->si_uid;
  81. break;
  82. case SIL_TIMER:
  83. new.ssi_tid = kinfo->si_tid;
  84. new.ssi_overrun = kinfo->si_overrun;
  85. new.ssi_ptr = (long) kinfo->si_ptr;
  86. new.ssi_int = kinfo->si_int;
  87. break;
  88. case SIL_POLL:
  89. new.ssi_band = kinfo->si_band;
  90. new.ssi_fd = kinfo->si_fd;
  91. break;
  92. case SIL_FAULT_BNDERR:
  93. case SIL_FAULT_PKUERR:
  94. case SIL_FAULT_PERF_EVENT:
  95. /*
  96. * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR,
  97. * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
  98. * generated by faults that deliver them synchronously to
  99. * userspace. In case someone injects one of these signals
  100. * and signalfd catches it treat it as SIL_FAULT.
  101. */
  102. case SIL_FAULT:
  103. new.ssi_addr = (long) kinfo->si_addr;
  104. break;
  105. case SIL_FAULT_TRAPNO:
  106. new.ssi_addr = (long) kinfo->si_addr;
  107. new.ssi_trapno = kinfo->si_trapno;
  108. break;
  109. case SIL_FAULT_MCEERR:
  110. new.ssi_addr = (long) kinfo->si_addr;
  111. new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
  112. break;
  113. case SIL_CHLD:
  114. new.ssi_pid = kinfo->si_pid;
  115. new.ssi_uid = kinfo->si_uid;
  116. new.ssi_status = kinfo->si_status;
  117. new.ssi_utime = kinfo->si_utime;
  118. new.ssi_stime = kinfo->si_stime;
  119. break;
  120. case SIL_RT:
  121. /*
  122. * This case catches also the signals queued by sigqueue().
  123. */
  124. new.ssi_pid = kinfo->si_pid;
  125. new.ssi_uid = kinfo->si_uid;
  126. new.ssi_ptr = (long) kinfo->si_ptr;
  127. new.ssi_int = kinfo->si_int;
  128. break;
  129. case SIL_SYS:
  130. new.ssi_call_addr = (long) kinfo->si_call_addr;
  131. new.ssi_syscall = kinfo->si_syscall;
  132. new.ssi_arch = kinfo->si_arch;
  133. break;
  134. }
  135. if (!copy_to_iter_full(&new, sizeof(struct signalfd_siginfo), to))
  136. return -EFAULT;
  137. return sizeof(struct signalfd_siginfo);
  138. }
  139. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
  140. int nonblock)
  141. {
  142. enum pid_type type;
  143. ssize_t ret;
  144. DECLARE_WAITQUEUE(wait, current);
  145. spin_lock_irq(&current->sighand->siglock);
  146. ret = dequeue_signal(&ctx->sigmask, info, &type);
  147. switch (ret) {
  148. case 0:
  149. if (!nonblock)
  150. break;
  151. ret = -EAGAIN;
  152. fallthrough;
  153. default:
  154. spin_unlock_irq(&current->sighand->siglock);
  155. return ret;
  156. }
  157. add_wait_queue(&current->sighand->signalfd_wqh, &wait);
  158. for (;;) {
  159. set_current_state(TASK_INTERRUPTIBLE);
  160. ret = dequeue_signal(&ctx->sigmask, info, &type);
  161. if (ret != 0)
  162. break;
  163. if (signal_pending(current)) {
  164. ret = -ERESTARTSYS;
  165. break;
  166. }
  167. spin_unlock_irq(&current->sighand->siglock);
  168. schedule();
  169. spin_lock_irq(&current->sighand->siglock);
  170. }
  171. spin_unlock_irq(&current->sighand->siglock);
  172. remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
  173. __set_current_state(TASK_RUNNING);
  174. return ret;
  175. }
  176. /*
  177. * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
  178. * error code. The "count" parameter must be at least the size of a
  179. * "struct signalfd_siginfo".
  180. */
  181. static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
  182. {
  183. struct file *file = iocb->ki_filp;
  184. struct signalfd_ctx *ctx = file->private_data;
  185. size_t count = iov_iter_count(to);
  186. ssize_t ret, total = 0;
  187. kernel_siginfo_t info;
  188. bool nonblock;
  189. count /= sizeof(struct signalfd_siginfo);
  190. if (!count)
  191. return -EINVAL;
  192. nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT;
  193. do {
  194. ret = signalfd_dequeue(ctx, &info, nonblock);
  195. if (unlikely(ret <= 0))
  196. break;
  197. ret = signalfd_copyinfo(to, &info);
  198. if (ret < 0)
  199. break;
  200. total += ret;
  201. nonblock = 1;
  202. } while (--count);
  203. return total ? total: ret;
  204. }
  205. #ifdef CONFIG_PROC_FS
  206. static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
  207. {
  208. struct signalfd_ctx *ctx = f->private_data;
  209. sigset_t sigmask;
  210. sigmask = ctx->sigmask;
  211. signotset(&sigmask);
  212. render_sigset_t(m, "sigmask:\t", &sigmask);
  213. }
  214. #endif
  215. static const struct file_operations signalfd_fops = {
  216. #ifdef CONFIG_PROC_FS
  217. .show_fdinfo = signalfd_show_fdinfo,
  218. #endif
  219. .release = signalfd_release,
  220. .poll = signalfd_poll,
  221. .read_iter = signalfd_read_iter,
  222. .llseek = noop_llseek,
  223. };
  224. static int do_signalfd4(int ufd, sigset_t *mask, int flags)
  225. {
  226. struct signalfd_ctx *ctx;
  227. /* Check the SFD_* constants for consistency. */
  228. BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
  229. BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
  230. if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
  231. return -EINVAL;
  232. sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  233. signotset(mask);
  234. if (ufd == -1) {
  235. struct file *file;
  236. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  237. if (!ctx)
  238. return -ENOMEM;
  239. ctx->sigmask = *mask;
  240. ufd = get_unused_fd_flags(flags & O_CLOEXEC);
  241. if (ufd < 0) {
  242. kfree(ctx);
  243. return ufd;
  244. }
  245. file = anon_inode_getfile("[signalfd]", &signalfd_fops, ctx,
  246. O_RDWR | (flags & O_NONBLOCK));
  247. if (IS_ERR(file)) {
  248. put_unused_fd(ufd);
  249. kfree(ctx);
  250. return PTR_ERR(file);
  251. }
  252. file->f_mode |= FMODE_NOWAIT;
  253. fd_install(ufd, file);
  254. } else {
  255. struct fd f = fdget(ufd);
  256. if (!fd_file(f))
  257. return -EBADF;
  258. ctx = fd_file(f)->private_data;
  259. if (fd_file(f)->f_op != &signalfd_fops) {
  260. fdput(f);
  261. return -EINVAL;
  262. }
  263. spin_lock_irq(&current->sighand->siglock);
  264. ctx->sigmask = *mask;
  265. spin_unlock_irq(&current->sighand->siglock);
  266. wake_up(&current->sighand->signalfd_wqh);
  267. fdput(f);
  268. }
  269. return ufd;
  270. }
  271. SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
  272. size_t, sizemask, int, flags)
  273. {
  274. sigset_t mask;
  275. if (sizemask != sizeof(sigset_t))
  276. return -EINVAL;
  277. if (copy_from_user(&mask, user_mask, sizeof(mask)))
  278. return -EFAULT;
  279. return do_signalfd4(ufd, &mask, flags);
  280. }
  281. SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
  282. size_t, sizemask)
  283. {
  284. sigset_t mask;
  285. if (sizemask != sizeof(sigset_t))
  286. return -EINVAL;
  287. if (copy_from_user(&mask, user_mask, sizeof(mask)))
  288. return -EFAULT;
  289. return do_signalfd4(ufd, &mask, 0);
  290. }
  291. #ifdef CONFIG_COMPAT
  292. static long do_compat_signalfd4(int ufd,
  293. const compat_sigset_t __user *user_mask,
  294. compat_size_t sigsetsize, int flags)
  295. {
  296. sigset_t mask;
  297. if (sigsetsize != sizeof(compat_sigset_t))
  298. return -EINVAL;
  299. if (get_compat_sigset(&mask, user_mask))
  300. return -EFAULT;
  301. return do_signalfd4(ufd, &mask, flags);
  302. }
  303. COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
  304. const compat_sigset_t __user *, user_mask,
  305. compat_size_t, sigsetsize,
  306. int, flags)
  307. {
  308. return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
  309. }
  310. COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
  311. const compat_sigset_t __user *, user_mask,
  312. compat_size_t, sigsetsize)
  313. {
  314. return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
  315. }
  316. #endif