signalfd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * fs/signalfd.c
  3. *
  4. * Copyright (C) 2003 Linus Torvalds
  5. *
  6. * Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
  7. * Changed ->read() to return a siginfo strcture instead of signal number.
  8. * Fixed locking in ->poll().
  9. * Added sighand-detach notification.
  10. * Added fd re-use in sys_signalfd() syscall.
  11. * Now using anonymous inode source.
  12. * Thanks to Oleg Nesterov for useful code review and suggestions.
  13. * More comments and suggestions from Arnd Bergmann.
  14. * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
  15. * Retrieve multiple signals with one read() call
  16. * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
  17. * Attach to the sighand only during read() and poll().
  18. */
  19. #include <linux/file.h>
  20. #include <linux/poll.h>
  21. #include <linux/init.h>
  22. #include <linux/fs.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/kernel.h>
  26. #include <linux/signal.h>
  27. #include <linux/list.h>
  28. #include <linux/anon_inodes.h>
  29. #include <linux/signalfd.h>
  30. #include <linux/syscalls.h>
  31. void signalfd_cleanup(struct sighand_struct *sighand)
  32. {
  33. wake_up_pollfree(&sighand->signalfd_wqh);
  34. }
  35. struct signalfd_ctx {
  36. sigset_t sigmask;
  37. };
  38. static int signalfd_release(struct inode *inode, struct file *file)
  39. {
  40. kfree(file->private_data);
  41. return 0;
  42. }
  43. static unsigned int signalfd_poll(struct file *file, poll_table *wait)
  44. {
  45. struct signalfd_ctx *ctx = file->private_data;
  46. unsigned int events = 0;
  47. poll_wait(file, &current->sighand->signalfd_wqh, wait);
  48. spin_lock_irq(&current->sighand->siglock);
  49. if (next_signal(&current->pending, &ctx->sigmask) ||
  50. next_signal(&current->signal->shared_pending,
  51. &ctx->sigmask))
  52. events |= POLLIN;
  53. spin_unlock_irq(&current->sighand->siglock);
  54. return events;
  55. }
  56. /*
  57. * Copied from copy_siginfo_to_user() in kernel/signal.c
  58. */
  59. static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
  60. siginfo_t const *kinfo)
  61. {
  62. long err;
  63. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  64. /*
  65. * Unused members should be zero ...
  66. */
  67. err = __clear_user(uinfo, sizeof(*uinfo));
  68. /*
  69. * If you change siginfo_t structure, please be sure
  70. * this code is fixed accordingly.
  71. */
  72. err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
  73. err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
  74. err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code);
  75. switch (kinfo->si_code & __SI_MASK) {
  76. case __SI_KILL:
  77. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  78. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  79. break;
  80. case __SI_TIMER:
  81. err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
  82. err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
  83. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  84. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  85. break;
  86. case __SI_POLL:
  87. err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
  88. err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
  89. break;
  90. case __SI_FAULT:
  91. err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
  92. #ifdef __ARCH_SI_TRAPNO
  93. err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
  94. #endif
  95. #ifdef BUS_MCEERR_AO
  96. /*
  97. * Other callers might not initialize the si_lsb field,
  98. * so check explicitly for the right codes here.
  99. */
  100. if (kinfo->si_signo == SIGBUS &&
  101. (kinfo->si_code == BUS_MCEERR_AR ||
  102. kinfo->si_code == BUS_MCEERR_AO))
  103. err |= __put_user((short) kinfo->si_addr_lsb,
  104. &uinfo->ssi_addr_lsb);
  105. #endif
  106. break;
  107. case __SI_CHLD:
  108. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  109. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  110. err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
  111. err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
  112. err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
  113. break;
  114. case __SI_RT: /* This is not generated by the kernel as of now. */
  115. case __SI_MESGQ: /* But this is */
  116. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  117. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  118. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  119. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  120. break;
  121. default:
  122. /*
  123. * This case catches also the signals queued by sigqueue().
  124. */
  125. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  126. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  127. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  128. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  129. break;
  130. }
  131. return err ? -EFAULT: sizeof(*uinfo);
  132. }
  133. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
  134. int nonblock)
  135. {
  136. ssize_t ret;
  137. DECLARE_WAITQUEUE(wait, current);
  138. spin_lock_irq(&current->sighand->siglock);
  139. ret = dequeue_signal(current, &ctx->sigmask, info);
  140. switch (ret) {
  141. case 0:
  142. if (!nonblock)
  143. break;
  144. ret = -EAGAIN;
  145. default:
  146. spin_unlock_irq(&current->sighand->siglock);
  147. return ret;
  148. }
  149. add_wait_queue(&current->sighand->signalfd_wqh, &wait);
  150. for (;;) {
  151. set_current_state(TASK_INTERRUPTIBLE);
  152. ret = dequeue_signal(current, &ctx->sigmask, info);
  153. if (ret != 0)
  154. break;
  155. if (signal_pending(current)) {
  156. ret = -ERESTARTSYS;
  157. break;
  158. }
  159. spin_unlock_irq(&current->sighand->siglock);
  160. schedule();
  161. spin_lock_irq(&current->sighand->siglock);
  162. }
  163. spin_unlock_irq(&current->sighand->siglock);
  164. remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
  165. __set_current_state(TASK_RUNNING);
  166. return ret;
  167. }
  168. /*
  169. * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
  170. * error code. The "count" parameter must be at least the size of a
  171. * "struct signalfd_siginfo".
  172. */
  173. static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
  174. loff_t *ppos)
  175. {
  176. struct signalfd_ctx *ctx = file->private_data;
  177. struct signalfd_siginfo __user *siginfo;
  178. int nonblock = file->f_flags & O_NONBLOCK;
  179. ssize_t ret, total = 0;
  180. siginfo_t info;
  181. count /= sizeof(struct signalfd_siginfo);
  182. if (!count)
  183. return -EINVAL;
  184. siginfo = (struct signalfd_siginfo __user *) buf;
  185. do {
  186. ret = signalfd_dequeue(ctx, &info, nonblock);
  187. if (unlikely(ret <= 0))
  188. break;
  189. ret = signalfd_copyinfo(siginfo, &info);
  190. if (ret < 0)
  191. break;
  192. siginfo++;
  193. total += ret;
  194. nonblock = 1;
  195. } while (--count);
  196. return total ? total: ret;
  197. }
  198. static const struct file_operations signalfd_fops = {
  199. .release = signalfd_release,
  200. .poll = signalfd_poll,
  201. .read = signalfd_read,
  202. .llseek = noop_llseek,
  203. };
  204. SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
  205. size_t, sizemask, int, flags)
  206. {
  207. sigset_t sigmask;
  208. struct signalfd_ctx *ctx;
  209. /* Check the SFD_* constants for consistency. */
  210. BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
  211. BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
  212. if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
  213. return -EINVAL;
  214. if (sizemask != sizeof(sigset_t) ||
  215. copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
  216. return -EINVAL;
  217. sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  218. signotset(&sigmask);
  219. if (ufd == -1) {
  220. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  221. if (!ctx)
  222. return -ENOMEM;
  223. ctx->sigmask = sigmask;
  224. /*
  225. * When we call this, the initialization must be complete, since
  226. * anon_inode_getfd() will install the fd.
  227. */
  228. ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
  229. O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
  230. if (ufd < 0)
  231. kfree(ctx);
  232. } else {
  233. int fput_needed;
  234. struct file *file = fget_light(ufd, &fput_needed);
  235. if (!file)
  236. return -EBADF;
  237. ctx = file->private_data;
  238. if (file->f_op != &signalfd_fops) {
  239. fput_light(file, fput_needed);
  240. return -EINVAL;
  241. }
  242. spin_lock_irq(&current->sighand->siglock);
  243. ctx->sigmask = sigmask;
  244. spin_unlock_irq(&current->sighand->siglock);
  245. wake_up(&current->sighand->signalfd_wqh);
  246. fput_light(file, fput_needed);
  247. }
  248. return ufd;
  249. }
  250. SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
  251. size_t, sizemask)
  252. {
  253. return sys_signalfd4(ufd, user_mask, sizemask, 0);
  254. }