signalfd.c 9.6 KB

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