signalfd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_code == BUS_MCEERR_AR ||
  101. kinfo->si_code == BUS_MCEERR_AO)
  102. err |= __put_user((short) kinfo->si_addr_lsb,
  103. &uinfo->ssi_addr_lsb);
  104. #endif
  105. break;
  106. case __SI_CHLD:
  107. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  108. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  109. err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
  110. err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
  111. err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
  112. break;
  113. case __SI_RT: /* This is not generated by the kernel as of now. */
  114. case __SI_MESGQ: /* But this is */
  115. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  116. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  117. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  118. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  119. break;
  120. default:
  121. /*
  122. * This case catches also the signals queued by sigqueue().
  123. */
  124. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  125. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  126. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  127. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  128. break;
  129. }
  130. return err ? -EFAULT: sizeof(*uinfo);
  131. }
  132. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
  133. int nonblock)
  134. {
  135. ssize_t ret;
  136. DECLARE_WAITQUEUE(wait, current);
  137. spin_lock_irq(&current->sighand->siglock);
  138. ret = dequeue_signal(current, &ctx->sigmask, info);
  139. switch (ret) {
  140. case 0:
  141. if (!nonblock)
  142. break;
  143. ret = -EAGAIN;
  144. default:
  145. spin_unlock_irq(&current->sighand->siglock);
  146. return ret;
  147. }
  148. add_wait_queue(&current->sighand->signalfd_wqh, &wait);
  149. for (;;) {
  150. set_current_state(TASK_INTERRUPTIBLE);
  151. ret = dequeue_signal(current, &ctx->sigmask, info);
  152. if (ret != 0)
  153. break;
  154. if (signal_pending(current)) {
  155. ret = -ERESTARTSYS;
  156. break;
  157. }
  158. spin_unlock_irq(&current->sighand->siglock);
  159. schedule();
  160. spin_lock_irq(&current->sighand->siglock);
  161. }
  162. spin_unlock_irq(&current->sighand->siglock);
  163. remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
  164. __set_current_state(TASK_RUNNING);
  165. return ret;
  166. }
  167. /*
  168. * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
  169. * error code. The "count" parameter must be at least the size of a
  170. * "struct signalfd_siginfo".
  171. */
  172. static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
  173. loff_t *ppos)
  174. {
  175. struct signalfd_ctx *ctx = file->private_data;
  176. struct signalfd_siginfo __user *siginfo;
  177. int nonblock = file->f_flags & O_NONBLOCK;
  178. ssize_t ret, total = 0;
  179. siginfo_t info;
  180. count /= sizeof(struct signalfd_siginfo);
  181. if (!count)
  182. return -EINVAL;
  183. siginfo = (struct signalfd_siginfo __user *) buf;
  184. do {
  185. ret = signalfd_dequeue(ctx, &info, nonblock);
  186. if (unlikely(ret <= 0))
  187. break;
  188. ret = signalfd_copyinfo(siginfo, &info);
  189. if (ret < 0)
  190. break;
  191. siginfo++;
  192. total += ret;
  193. nonblock = 1;
  194. } while (--count);
  195. return total ? total: ret;
  196. }
  197. static const struct file_operations signalfd_fops = {
  198. .release = signalfd_release,
  199. .poll = signalfd_poll,
  200. .read = signalfd_read,
  201. .llseek = noop_llseek,
  202. };
  203. SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
  204. size_t, sizemask, int, flags)
  205. {
  206. sigset_t sigmask;
  207. struct signalfd_ctx *ctx;
  208. /* Check the SFD_* constants for consistency. */
  209. BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
  210. BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
  211. if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
  212. return -EINVAL;
  213. if (sizemask != sizeof(sigset_t) ||
  214. copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
  215. return -EINVAL;
  216. sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  217. signotset(&sigmask);
  218. if (ufd == -1) {
  219. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  220. if (!ctx)
  221. return -ENOMEM;
  222. ctx->sigmask = sigmask;
  223. /*
  224. * When we call this, the initialization must be complete, since
  225. * anon_inode_getfd() will install the fd.
  226. */
  227. ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
  228. O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
  229. if (ufd < 0)
  230. kfree(ctx);
  231. } else {
  232. int fput_needed;
  233. struct file *file = fget_light(ufd, &fput_needed);
  234. if (!file)
  235. return -EBADF;
  236. ctx = file->private_data;
  237. if (file->f_op != &signalfd_fops) {
  238. fput_light(file, fput_needed);
  239. return -EINVAL;
  240. }
  241. spin_lock_irq(&current->sighand->siglock);
  242. ctx->sigmask = sigmask;
  243. spin_unlock_irq(&current->sighand->siglock);
  244. wake_up(&current->sighand->signalfd_wqh);
  245. fput_light(file, fput_needed);
  246. }
  247. return ufd;
  248. }
  249. SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
  250. size_t, sizemask)
  251. {
  252. return sys_signalfd4(ufd, user_mask, sizemask, 0);
  253. }