signalfd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. wait_queue_head_t *wqh = &sighand->signalfd_wqh;
  34. /*
  35. * The lockless check can race with remove_wait_queue() in progress,
  36. * but in this case its caller should run under rcu_read_lock() and
  37. * sighand_cachep is SLAB_DESTROY_BY_RCU, we can safely return.
  38. */
  39. if (likely(!waitqueue_active(wqh)))
  40. return;
  41. /* wait_queue_t->func(POLLFREE) should do remove_wait_queue() */
  42. wake_up_poll(wqh, POLLHUP | POLLFREE);
  43. }
  44. struct signalfd_ctx {
  45. sigset_t sigmask;
  46. };
  47. static int signalfd_release(struct inode *inode, struct file *file)
  48. {
  49. kfree(file->private_data);
  50. return 0;
  51. }
  52. static unsigned int signalfd_poll(struct file *file, poll_table *wait)
  53. {
  54. struct signalfd_ctx *ctx = file->private_data;
  55. unsigned int events = 0;
  56. poll_wait(file, &current->sighand->signalfd_wqh, wait);
  57. spin_lock_irq(&current->sighand->siglock);
  58. if (next_signal(&current->pending, &ctx->sigmask) ||
  59. next_signal(&current->signal->shared_pending,
  60. &ctx->sigmask))
  61. events |= POLLIN;
  62. spin_unlock_irq(&current->sighand->siglock);
  63. return events;
  64. }
  65. /*
  66. * Copied from copy_siginfo_to_user() in kernel/signal.c
  67. */
  68. static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
  69. siginfo_t const *kinfo)
  70. {
  71. long err;
  72. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  73. /*
  74. * Unused members should be zero ...
  75. */
  76. err = __clear_user(uinfo, sizeof(*uinfo));
  77. /*
  78. * If you change siginfo_t structure, please be sure
  79. * this code is fixed accordingly.
  80. */
  81. err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
  82. err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
  83. err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code);
  84. switch (kinfo->si_code & __SI_MASK) {
  85. case __SI_KILL:
  86. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  87. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  88. break;
  89. case __SI_TIMER:
  90. err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
  91. err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
  92. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  93. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  94. break;
  95. case __SI_POLL:
  96. err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
  97. err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
  98. break;
  99. case __SI_FAULT:
  100. err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
  101. #ifdef __ARCH_SI_TRAPNO
  102. err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
  103. #endif
  104. #ifdef BUS_MCEERR_AO
  105. /*
  106. * Other callers might not initialize the si_lsb field,
  107. * so check explicitly for the right codes here.
  108. */
  109. if (kinfo->si_code == BUS_MCEERR_AR ||
  110. kinfo->si_code == BUS_MCEERR_AO)
  111. err |= __put_user((short) kinfo->si_addr_lsb,
  112. &uinfo->ssi_addr_lsb);
  113. #endif
  114. break;
  115. case __SI_CHLD:
  116. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  117. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  118. err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
  119. err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
  120. err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
  121. break;
  122. case __SI_RT: /* This is not generated by the kernel as of now. */
  123. case __SI_MESGQ: /* But this is */
  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. default:
  130. /*
  131. * This case catches also the signals queued by sigqueue().
  132. */
  133. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  134. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  135. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  136. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  137. break;
  138. }
  139. return err ? -EFAULT: sizeof(*uinfo);
  140. }
  141. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, 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. 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(current, &ctx->sigmask, info);
  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(struct file *file, char __user *buf, size_t count,
  182. loff_t *ppos)
  183. {
  184. struct signalfd_ctx *ctx = file->private_data;
  185. struct signalfd_siginfo __user *siginfo;
  186. int nonblock = file->f_flags & O_NONBLOCK;
  187. ssize_t ret, total = 0;
  188. siginfo_t info;
  189. count /= sizeof(struct signalfd_siginfo);
  190. if (!count)
  191. return -EINVAL;
  192. siginfo = (struct signalfd_siginfo __user *) buf;
  193. do {
  194. ret = signalfd_dequeue(ctx, &info, nonblock);
  195. if (unlikely(ret <= 0))
  196. break;
  197. ret = signalfd_copyinfo(siginfo, &info);
  198. if (ret < 0)
  199. break;
  200. siginfo++;
  201. total += ret;
  202. nonblock = 1;
  203. } while (--count);
  204. return total ? total: ret;
  205. }
  206. static const struct file_operations signalfd_fops = {
  207. .release = signalfd_release,
  208. .poll = signalfd_poll,
  209. .read = signalfd_read,
  210. .llseek = noop_llseek,
  211. };
  212. SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
  213. size_t, sizemask, int, flags)
  214. {
  215. sigset_t sigmask;
  216. struct signalfd_ctx *ctx;
  217. /* Check the SFD_* constants for consistency. */
  218. BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
  219. BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
  220. if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
  221. return -EINVAL;
  222. if (sizemask != sizeof(sigset_t) ||
  223. copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
  224. return -EINVAL;
  225. sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  226. signotset(&sigmask);
  227. if (ufd == -1) {
  228. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  229. if (!ctx)
  230. return -ENOMEM;
  231. ctx->sigmask = sigmask;
  232. /*
  233. * When we call this, the initialization must be complete, since
  234. * anon_inode_getfd() will install the fd.
  235. */
  236. ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
  237. O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
  238. if (ufd < 0)
  239. kfree(ctx);
  240. } else {
  241. struct file *file = fget(ufd);
  242. if (!file)
  243. return -EBADF;
  244. ctx = file->private_data;
  245. if (file->f_op != &signalfd_fops) {
  246. fput(file);
  247. return -EINVAL;
  248. }
  249. spin_lock_irq(&current->sighand->siglock);
  250. ctx->sigmask = sigmask;
  251. spin_unlock_irq(&current->sighand->siglock);
  252. wake_up(&current->sighand->signalfd_wqh);
  253. fput(file);
  254. }
  255. return ufd;
  256. }
  257. SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
  258. size_t, sizemask)
  259. {
  260. return sys_signalfd4(ufd, user_mask, sizemask, 0);
  261. }