signalfd.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include <linux/proc_fs.h>
  32. #include <linux/compat.h>
  33. void signalfd_cleanup(struct sighand_struct *sighand)
  34. {
  35. wait_queue_head_t *wqh = &sighand->signalfd_wqh;
  36. /*
  37. * The lockless check can race with remove_wait_queue() in progress,
  38. * but in this case its caller should run under rcu_read_lock() and
  39. * sighand_cachep is SLAB_DESTROY_BY_RCU, we can safely return.
  40. */
  41. if (likely(!waitqueue_active(wqh)))
  42. return;
  43. /* wait_queue_t->func(POLLFREE) should do remove_wait_queue() */
  44. wake_up_poll(wqh, POLLHUP | POLLFREE);
  45. }
  46. struct signalfd_ctx {
  47. sigset_t sigmask;
  48. };
  49. static int signalfd_release(struct inode *inode, struct file *file)
  50. {
  51. kfree(file->private_data);
  52. return 0;
  53. }
  54. static unsigned int signalfd_poll(struct file *file, poll_table *wait)
  55. {
  56. struct signalfd_ctx *ctx = file->private_data;
  57. unsigned int events = 0;
  58. poll_wait(file, &current->sighand->signalfd_wqh, wait);
  59. spin_lock_irq(&current->sighand->siglock);
  60. if (next_signal(&current->pending, &ctx->sigmask) ||
  61. next_signal(&current->signal->shared_pending,
  62. &ctx->sigmask))
  63. events |= POLLIN;
  64. spin_unlock_irq(&current->sighand->siglock);
  65. return events;
  66. }
  67. /*
  68. * Copied from copy_siginfo_to_user() in kernel/signal.c
  69. */
  70. static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
  71. siginfo_t const *kinfo)
  72. {
  73. long err;
  74. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  75. /*
  76. * Unused members should be zero ...
  77. */
  78. err = __clear_user(uinfo, sizeof(*uinfo));
  79. /*
  80. * If you change siginfo_t structure, please be sure
  81. * this code is fixed accordingly.
  82. */
  83. err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
  84. err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
  85. err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code);
  86. switch (kinfo->si_code & __SI_MASK) {
  87. case __SI_KILL:
  88. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  89. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  90. break;
  91. case __SI_TIMER:
  92. err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
  93. err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
  94. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  95. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  96. break;
  97. case __SI_POLL:
  98. err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
  99. err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
  100. break;
  101. case __SI_FAULT:
  102. err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
  103. #ifdef __ARCH_SI_TRAPNO
  104. err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
  105. #endif
  106. #ifdef BUS_MCEERR_AO
  107. /*
  108. * Other callers might not initialize the si_lsb field,
  109. * so check explicitly for the right codes here.
  110. */
  111. if (kinfo->si_signo == SIGBUS &&
  112. (kinfo->si_code == BUS_MCEERR_AR ||
  113. kinfo->si_code == BUS_MCEERR_AO))
  114. err |= __put_user((short) kinfo->si_addr_lsb,
  115. &uinfo->ssi_addr_lsb);
  116. #endif
  117. break;
  118. case __SI_CHLD:
  119. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  120. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  121. err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
  122. err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
  123. err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
  124. break;
  125. case __SI_RT: /* This is not generated by the kernel as of now. */
  126. case __SI_MESGQ: /* But this is */
  127. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  128. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  129. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  130. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  131. break;
  132. default:
  133. /*
  134. * This case catches also the signals queued by sigqueue().
  135. */
  136. err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
  137. err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
  138. err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
  139. err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
  140. break;
  141. }
  142. return err ? -EFAULT: sizeof(*uinfo);
  143. }
  144. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
  145. int nonblock)
  146. {
  147. ssize_t ret;
  148. DECLARE_WAITQUEUE(wait, current);
  149. spin_lock_irq(&current->sighand->siglock);
  150. ret = dequeue_signal(current, &ctx->sigmask, info);
  151. switch (ret) {
  152. case 0:
  153. if (!nonblock)
  154. break;
  155. ret = -EAGAIN;
  156. default:
  157. spin_unlock_irq(&current->sighand->siglock);
  158. return ret;
  159. }
  160. add_wait_queue(&current->sighand->signalfd_wqh, &wait);
  161. for (;;) {
  162. set_current_state(TASK_INTERRUPTIBLE);
  163. ret = dequeue_signal(current, &ctx->sigmask, info);
  164. if (ret != 0)
  165. break;
  166. if (signal_pending(current)) {
  167. ret = -ERESTARTSYS;
  168. break;
  169. }
  170. spin_unlock_irq(&current->sighand->siglock);
  171. schedule();
  172. spin_lock_irq(&current->sighand->siglock);
  173. }
  174. spin_unlock_irq(&current->sighand->siglock);
  175. remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
  176. __set_current_state(TASK_RUNNING);
  177. return ret;
  178. }
  179. /*
  180. * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
  181. * error code. The "count" parameter must be at least the size of a
  182. * "struct signalfd_siginfo".
  183. */
  184. static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
  185. loff_t *ppos)
  186. {
  187. struct signalfd_ctx *ctx = file->private_data;
  188. struct signalfd_siginfo __user *siginfo;
  189. int nonblock = file->f_flags & O_NONBLOCK;
  190. ssize_t ret, total = 0;
  191. siginfo_t info;
  192. count /= sizeof(struct signalfd_siginfo);
  193. if (!count)
  194. return -EINVAL;
  195. siginfo = (struct signalfd_siginfo __user *) buf;
  196. do {
  197. ret = signalfd_dequeue(ctx, &info, nonblock);
  198. if (unlikely(ret <= 0))
  199. break;
  200. ret = signalfd_copyinfo(siginfo, &info);
  201. if (ret < 0)
  202. break;
  203. siginfo++;
  204. total += ret;
  205. nonblock = 1;
  206. } while (--count);
  207. return total ? total: ret;
  208. }
  209. #ifdef CONFIG_PROC_FS
  210. static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
  211. {
  212. struct signalfd_ctx *ctx = f->private_data;
  213. sigset_t sigmask;
  214. sigmask = ctx->sigmask;
  215. signotset(&sigmask);
  216. render_sigset_t(m, "sigmask:\t", &sigmask);
  217. }
  218. #endif
  219. static const struct file_operations signalfd_fops = {
  220. #ifdef CONFIG_PROC_FS
  221. .show_fdinfo = signalfd_show_fdinfo,
  222. #endif
  223. .release = signalfd_release,
  224. .poll = signalfd_poll,
  225. .read = signalfd_read,
  226. .llseek = noop_llseek,
  227. };
  228. SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
  229. size_t, sizemask, int, flags)
  230. {
  231. sigset_t sigmask;
  232. struct signalfd_ctx *ctx;
  233. /* Check the SFD_* constants for consistency. */
  234. BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
  235. BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
  236. if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
  237. return -EINVAL;
  238. if (sizemask != sizeof(sigset_t) ||
  239. copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
  240. return -EINVAL;
  241. sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  242. signotset(&sigmask);
  243. if (ufd == -1) {
  244. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  245. if (!ctx)
  246. return -ENOMEM;
  247. ctx->sigmask = sigmask;
  248. /*
  249. * When we call this, the initialization must be complete, since
  250. * anon_inode_getfd() will install the fd.
  251. */
  252. ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
  253. O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
  254. if (ufd < 0)
  255. kfree(ctx);
  256. } else {
  257. struct fd f = fdget(ufd);
  258. if (!f.file)
  259. return -EBADF;
  260. ctx = f.file->private_data;
  261. if (f.file->f_op != &signalfd_fops) {
  262. fdput(f);
  263. return -EINVAL;
  264. }
  265. spin_lock_irq(&current->sighand->siglock);
  266. ctx->sigmask = sigmask;
  267. spin_unlock_irq(&current->sighand->siglock);
  268. wake_up(&current->sighand->signalfd_wqh);
  269. fdput(f);
  270. }
  271. return ufd;
  272. }
  273. SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
  274. size_t, sizemask)
  275. {
  276. return sys_signalfd4(ufd, user_mask, sizemask, 0);
  277. }
  278. #ifdef CONFIG_COMPAT
  279. COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
  280. const compat_sigset_t __user *,sigmask,
  281. compat_size_t, sigsetsize,
  282. int, flags)
  283. {
  284. compat_sigset_t ss32;
  285. sigset_t tmp;
  286. sigset_t __user *ksigmask;
  287. if (sigsetsize != sizeof(compat_sigset_t))
  288. return -EINVAL;
  289. if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
  290. return -EFAULT;
  291. sigset_from_compat(&tmp, &ss32);
  292. ksigmask = compat_alloc_user_space(sizeof(sigset_t));
  293. if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
  294. return -EFAULT;
  295. return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
  296. }
  297. COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
  298. const compat_sigset_t __user *,sigmask,
  299. compat_size_t, sigsetsize)
  300. {
  301. return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
  302. }
  303. #endif