signalfd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. */
  15. #include <linux/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/init.h>
  18. #include <linux/fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/signal.h>
  22. #include <linux/list.h>
  23. #include <linux/anon_inodes.h>
  24. #include <linux/signalfd.h>
  25. struct signalfd_ctx {
  26. struct list_head lnk;
  27. wait_queue_head_t wqh;
  28. sigset_t sigmask;
  29. struct task_struct *tsk;
  30. };
  31. struct signalfd_lockctx {
  32. struct task_struct *tsk;
  33. unsigned long flags;
  34. };
  35. /*
  36. * Tries to acquire the sighand lock. We do not increment the sighand
  37. * use count, and we do not even pin the task struct, so we need to
  38. * do it inside an RCU read lock, and we must be prepared for the
  39. * ctx->tsk going to NULL (in signalfd_deliver()), and for the sighand
  40. * being detached. We return 0 if the sighand has been detached, or
  41. * 1 if we were able to pin the sighand lock.
  42. */
  43. static int signalfd_lock(struct signalfd_ctx *ctx, struct signalfd_lockctx *lk)
  44. {
  45. struct sighand_struct *sighand = NULL;
  46. rcu_read_lock();
  47. lk->tsk = rcu_dereference(ctx->tsk);
  48. if (likely(lk->tsk != NULL))
  49. sighand = lock_task_sighand(lk->tsk, &lk->flags);
  50. rcu_read_unlock();
  51. if (sighand && !ctx->tsk) {
  52. unlock_task_sighand(lk->tsk, &lk->flags);
  53. sighand = NULL;
  54. }
  55. return sighand != NULL;
  56. }
  57. static void signalfd_unlock(struct signalfd_lockctx *lk)
  58. {
  59. unlock_task_sighand(lk->tsk, &lk->flags);
  60. }
  61. /*
  62. * This must be called with the sighand lock held.
  63. */
  64. void signalfd_deliver(struct task_struct *tsk, int sig)
  65. {
  66. struct sighand_struct *sighand = tsk->sighand;
  67. struct signalfd_ctx *ctx, *tmp;
  68. BUG_ON(!sig);
  69. list_for_each_entry_safe(ctx, tmp, &sighand->signalfd_list, lnk) {
  70. /*
  71. * We use a negative signal value as a way to broadcast that the
  72. * sighand has been orphaned, so that we can notify all the
  73. * listeners about this. Remember the ctx->sigmask is inverted,
  74. * so if the user is interested in a signal, that corresponding
  75. * bit will be zero.
  76. */
  77. if (sig < 0) {
  78. if (ctx->tsk == tsk) {
  79. ctx->tsk = NULL;
  80. list_del_init(&ctx->lnk);
  81. wake_up(&ctx->wqh);
  82. }
  83. } else {
  84. if (!sigismember(&ctx->sigmask, sig))
  85. wake_up(&ctx->wqh);
  86. }
  87. }
  88. }
  89. static void signalfd_cleanup(struct signalfd_ctx *ctx)
  90. {
  91. struct signalfd_lockctx lk;
  92. /*
  93. * This is tricky. If the sighand is gone, we do not need to remove
  94. * context from the list, the list itself won't be there anymore.
  95. */
  96. if (signalfd_lock(ctx, &lk)) {
  97. list_del(&ctx->lnk);
  98. signalfd_unlock(&lk);
  99. }
  100. kfree(ctx);
  101. }
  102. static int signalfd_release(struct inode *inode, struct file *file)
  103. {
  104. signalfd_cleanup(file->private_data);
  105. return 0;
  106. }
  107. static unsigned int signalfd_poll(struct file *file, poll_table *wait)
  108. {
  109. struct signalfd_ctx *ctx = file->private_data;
  110. unsigned int events = 0;
  111. struct signalfd_lockctx lk;
  112. poll_wait(file, &ctx->wqh, wait);
  113. /*
  114. * Let the caller get a POLLIN in this case, ala socket recv() when
  115. * the peer disconnects.
  116. */
  117. if (signalfd_lock(ctx, &lk)) {
  118. if (next_signal(&lk.tsk->pending, &ctx->sigmask) > 0 ||
  119. next_signal(&lk.tsk->signal->shared_pending,
  120. &ctx->sigmask) > 0)
  121. events |= POLLIN;
  122. signalfd_unlock(&lk);
  123. } else
  124. events |= POLLIN;
  125. return events;
  126. }
  127. /*
  128. * Copied from copy_siginfo_to_user() in kernel/signal.c
  129. */
  130. static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
  131. siginfo_t const *kinfo)
  132. {
  133. long err;
  134. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  135. /*
  136. * Unused memebers should be zero ...
  137. */
  138. err = __clear_user(uinfo, sizeof(*uinfo));
  139. /*
  140. * If you change siginfo_t structure, please be sure
  141. * this code is fixed accordingly.
  142. */
  143. err |= __put_user(kinfo->si_signo, &uinfo->signo);
  144. err |= __put_user(kinfo->si_errno, &uinfo->err);
  145. err |= __put_user((short)kinfo->si_code, &uinfo->code);
  146. switch (kinfo->si_code & __SI_MASK) {
  147. case __SI_KILL:
  148. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  149. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  150. break;
  151. case __SI_TIMER:
  152. err |= __put_user(kinfo->si_tid, &uinfo->tid);
  153. err |= __put_user(kinfo->si_overrun, &uinfo->overrun);
  154. err |= __put_user((long)kinfo->si_ptr, &uinfo->svptr);
  155. break;
  156. case __SI_POLL:
  157. err |= __put_user(kinfo->si_band, &uinfo->band);
  158. err |= __put_user(kinfo->si_fd, &uinfo->fd);
  159. break;
  160. case __SI_FAULT:
  161. err |= __put_user((long)kinfo->si_addr, &uinfo->addr);
  162. #ifdef __ARCH_SI_TRAPNO
  163. err |= __put_user(kinfo->si_trapno, &uinfo->trapno);
  164. #endif
  165. break;
  166. case __SI_CHLD:
  167. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  168. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  169. err |= __put_user(kinfo->si_status, &uinfo->status);
  170. err |= __put_user(kinfo->si_utime, &uinfo->utime);
  171. err |= __put_user(kinfo->si_stime, &uinfo->stime);
  172. break;
  173. case __SI_RT: /* This is not generated by the kernel as of now. */
  174. case __SI_MESGQ: /* But this is */
  175. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  176. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  177. err |= __put_user((long)kinfo->si_ptr, &uinfo->svptr);
  178. break;
  179. default: /* this is just in case for now ... */
  180. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  181. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  182. break;
  183. }
  184. return err ? -EFAULT: sizeof(*uinfo);
  185. }
  186. /*
  187. * Returns either the size of a "struct signalfd_siginfo", or zero if the
  188. * sighand we are attached to, has been orphaned. The "count" parameter
  189. * must be at least the size of a "struct signalfd_siginfo".
  190. */
  191. static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
  192. loff_t *ppos)
  193. {
  194. struct signalfd_ctx *ctx = file->private_data;
  195. ssize_t res = 0;
  196. int locked, signo;
  197. siginfo_t info;
  198. struct signalfd_lockctx lk;
  199. DECLARE_WAITQUEUE(wait, current);
  200. if (count < sizeof(struct signalfd_siginfo))
  201. return -EINVAL;
  202. locked = signalfd_lock(ctx, &lk);
  203. if (!locked)
  204. return 0;
  205. res = -EAGAIN;
  206. signo = dequeue_signal(lk.tsk, &ctx->sigmask, &info);
  207. if (signo == 0 && !(file->f_flags & O_NONBLOCK)) {
  208. add_wait_queue(&ctx->wqh, &wait);
  209. for (;;) {
  210. set_current_state(TASK_INTERRUPTIBLE);
  211. signo = dequeue_signal(lk.tsk, &ctx->sigmask, &info);
  212. if (signo != 0)
  213. break;
  214. if (signal_pending(current)) {
  215. res = -ERESTARTSYS;
  216. break;
  217. }
  218. signalfd_unlock(&lk);
  219. schedule();
  220. locked = signalfd_lock(ctx, &lk);
  221. if (unlikely(!locked)) {
  222. /*
  223. * Let the caller read zero byte, ala socket
  224. * recv() when the peer disconnect. This test
  225. * must be done before doing a dequeue_signal(),
  226. * because if the sighand has been orphaned,
  227. * the dequeue_signal() call is going to crash.
  228. */
  229. res = 0;
  230. break;
  231. }
  232. }
  233. remove_wait_queue(&ctx->wqh, &wait);
  234. __set_current_state(TASK_RUNNING);
  235. }
  236. if (likely(locked))
  237. signalfd_unlock(&lk);
  238. if (likely(signo))
  239. res = signalfd_copyinfo((struct signalfd_siginfo __user *) buf,
  240. &info);
  241. return res;
  242. }
  243. static const struct file_operations signalfd_fops = {
  244. .release = signalfd_release,
  245. .poll = signalfd_poll,
  246. .read = signalfd_read,
  247. };
  248. /*
  249. * Create a file descriptor that is associated with our signal
  250. * state. We can pass it around to others if we want to, but
  251. * it will always be _our_ signal state.
  252. */
  253. asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
  254. {
  255. int error;
  256. sigset_t sigmask;
  257. struct signalfd_ctx *ctx;
  258. struct sighand_struct *sighand;
  259. struct file *file;
  260. struct inode *inode;
  261. struct signalfd_lockctx lk;
  262. if (sizemask != sizeof(sigset_t) ||
  263. copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
  264. return error = -EINVAL;
  265. sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  266. signotset(&sigmask);
  267. if (ufd == -1) {
  268. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  269. if (!ctx)
  270. return -ENOMEM;
  271. init_waitqueue_head(&ctx->wqh);
  272. ctx->sigmask = sigmask;
  273. ctx->tsk = current;
  274. sighand = current->sighand;
  275. /*
  276. * Add this fd to the list of signal listeners.
  277. */
  278. spin_lock_irq(&sighand->siglock);
  279. list_add_tail(&ctx->lnk, &sighand->signalfd_list);
  280. spin_unlock_irq(&sighand->siglock);
  281. /*
  282. * When we call this, the initialization must be complete, since
  283. * anon_inode_getfd() will install the fd.
  284. */
  285. error = anon_inode_getfd(&ufd, &inode, &file, "[signalfd]",
  286. &signalfd_fops, ctx);
  287. if (error)
  288. goto err_fdalloc;
  289. } else {
  290. file = fget(ufd);
  291. if (!file)
  292. return -EBADF;
  293. ctx = file->private_data;
  294. if (file->f_op != &signalfd_fops) {
  295. fput(file);
  296. return -EINVAL;
  297. }
  298. /*
  299. * We need to be prepared of the fact that the sighand this fd
  300. * is attached to, has been detched. In that case signalfd_lock()
  301. * will return 0, and we'll just skip setting the new mask.
  302. */
  303. if (signalfd_lock(ctx, &lk)) {
  304. ctx->sigmask = sigmask;
  305. signalfd_unlock(&lk);
  306. }
  307. wake_up(&ctx->wqh);
  308. fput(file);
  309. }
  310. return ufd;
  311. err_fdalloc:
  312. signalfd_cleanup(ctx);
  313. return error;
  314. }