signalfd.c 8.7 KB

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