signalfd.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 (next_signal(&lk.tsk->pending, &ctx->sigmask) > 0 ||
  121. next_signal(&lk.tsk->signal->shared_pending,
  122. &ctx->sigmask) > 0)
  123. events |= POLLIN;
  124. signalfd_unlock(&lk);
  125. } else
  126. events |= POLLIN;
  127. return events;
  128. }
  129. /*
  130. * Copied from copy_siginfo_to_user() in kernel/signal.c
  131. */
  132. static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
  133. siginfo_t const *kinfo)
  134. {
  135. long err;
  136. BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
  137. /*
  138. * Unused memebers should be zero ...
  139. */
  140. err = __clear_user(uinfo, sizeof(*uinfo));
  141. /*
  142. * If you change siginfo_t structure, please be sure
  143. * this code is fixed accordingly.
  144. */
  145. err |= __put_user(kinfo->si_signo, &uinfo->signo);
  146. err |= __put_user(kinfo->si_errno, &uinfo->err);
  147. err |= __put_user((short)kinfo->si_code, &uinfo->code);
  148. switch (kinfo->si_code & __SI_MASK) {
  149. case __SI_KILL:
  150. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  151. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  152. break;
  153. case __SI_TIMER:
  154. err |= __put_user(kinfo->si_tid, &uinfo->tid);
  155. err |= __put_user(kinfo->si_overrun, &uinfo->overrun);
  156. err |= __put_user((long)kinfo->si_ptr, &uinfo->svptr);
  157. break;
  158. case __SI_POLL:
  159. err |= __put_user(kinfo->si_band, &uinfo->band);
  160. err |= __put_user(kinfo->si_fd, &uinfo->fd);
  161. break;
  162. case __SI_FAULT:
  163. err |= __put_user((long)kinfo->si_addr, &uinfo->addr);
  164. #ifdef __ARCH_SI_TRAPNO
  165. err |= __put_user(kinfo->si_trapno, &uinfo->trapno);
  166. #endif
  167. break;
  168. case __SI_CHLD:
  169. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  170. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  171. err |= __put_user(kinfo->si_status, &uinfo->status);
  172. err |= __put_user(kinfo->si_utime, &uinfo->utime);
  173. err |= __put_user(kinfo->si_stime, &uinfo->stime);
  174. break;
  175. case __SI_RT: /* This is not generated by the kernel as of now. */
  176. case __SI_MESGQ: /* But this is */
  177. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  178. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  179. err |= __put_user((long)kinfo->si_ptr, &uinfo->svptr);
  180. break;
  181. default: /* this is just in case for now ... */
  182. err |= __put_user(kinfo->si_pid, &uinfo->pid);
  183. err |= __put_user(kinfo->si_uid, &uinfo->uid);
  184. break;
  185. }
  186. return err ? -EFAULT: sizeof(*uinfo);
  187. }
  188. static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
  189. int nonblock)
  190. {
  191. ssize_t ret;
  192. struct signalfd_lockctx lk;
  193. DECLARE_WAITQUEUE(wait, current);
  194. if (!signalfd_lock(ctx, &lk))
  195. return 0;
  196. ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
  197. switch (ret) {
  198. case 0:
  199. if (!nonblock)
  200. break;
  201. ret = -EAGAIN;
  202. default:
  203. signalfd_unlock(&lk);
  204. return ret;
  205. }
  206. add_wait_queue(&ctx->wqh, &wait);
  207. for (;;) {
  208. set_current_state(TASK_INTERRUPTIBLE);
  209. ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
  210. signalfd_unlock(&lk);
  211. if (ret != 0)
  212. break;
  213. if (signal_pending(current)) {
  214. ret = -ERESTARTSYS;
  215. break;
  216. }
  217. schedule();
  218. ret = signalfd_lock(ctx, &lk);
  219. if (unlikely(!ret)) {
  220. /*
  221. * Let the caller read zero byte, ala socket
  222. * recv() when the peer disconnect. This test
  223. * must be done before doing a dequeue_signal(),
  224. * because if the sighand has been orphaned,
  225. * the dequeue_signal() call is going to crash
  226. * because ->sighand will be long gone.
  227. */
  228. break;
  229. }
  230. }
  231. remove_wait_queue(&ctx->wqh, &wait);
  232. __set_current_state(TASK_RUNNING);
  233. return ret;
  234. }
  235. /*
  236. * Returns either the size of a "struct signalfd_siginfo", or zero if the
  237. * sighand we are attached to, has been orphaned. The "count" parameter
  238. * must be at least the size of a "struct signalfd_siginfo".
  239. */
  240. static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
  241. loff_t *ppos)
  242. {
  243. struct signalfd_ctx *ctx = file->private_data;
  244. struct signalfd_siginfo __user *siginfo;
  245. int nonblock = file->f_flags & O_NONBLOCK;
  246. ssize_t ret, total = 0;
  247. siginfo_t info;
  248. count /= sizeof(struct signalfd_siginfo);
  249. if (!count)
  250. return -EINVAL;
  251. siginfo = (struct signalfd_siginfo __user *) buf;
  252. do {
  253. ret = signalfd_dequeue(ctx, &info, nonblock);
  254. if (unlikely(ret <= 0))
  255. break;
  256. ret = signalfd_copyinfo(siginfo, &info);
  257. if (ret < 0)
  258. break;
  259. siginfo++;
  260. total += ret;
  261. nonblock = 1;
  262. } while (--count);
  263. return total ? total : ret;
  264. }
  265. static const struct file_operations signalfd_fops = {
  266. .release = signalfd_release,
  267. .poll = signalfd_poll,
  268. .read = signalfd_read,
  269. };
  270. /*
  271. * Create a file descriptor that is associated with our signal
  272. * state. We can pass it around to others if we want to, but
  273. * it will always be _our_ signal state.
  274. */
  275. asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
  276. {
  277. int error;
  278. sigset_t sigmask;
  279. struct signalfd_ctx *ctx;
  280. struct sighand_struct *sighand;
  281. struct file *file;
  282. struct inode *inode;
  283. struct signalfd_lockctx lk;
  284. if (sizemask != sizeof(sigset_t) ||
  285. copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
  286. return error = -EINVAL;
  287. sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  288. signotset(&sigmask);
  289. if (ufd == -1) {
  290. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  291. if (!ctx)
  292. return -ENOMEM;
  293. init_waitqueue_head(&ctx->wqh);
  294. ctx->sigmask = sigmask;
  295. ctx->tsk = current;
  296. sighand = current->sighand;
  297. /*
  298. * Add this fd to the list of signal listeners.
  299. */
  300. spin_lock_irq(&sighand->siglock);
  301. list_add_tail(&ctx->lnk, &sighand->signalfd_list);
  302. spin_unlock_irq(&sighand->siglock);
  303. /*
  304. * When we call this, the initialization must be complete, since
  305. * anon_inode_getfd() will install the fd.
  306. */
  307. error = anon_inode_getfd(&ufd, &inode, &file, "[signalfd]",
  308. &signalfd_fops, ctx);
  309. if (error)
  310. goto err_fdalloc;
  311. } else {
  312. file = fget(ufd);
  313. if (!file)
  314. return -EBADF;
  315. ctx = file->private_data;
  316. if (file->f_op != &signalfd_fops) {
  317. fput(file);
  318. return -EINVAL;
  319. }
  320. /*
  321. * We need to be prepared of the fact that the sighand this fd
  322. * is attached to, has been detched. In that case signalfd_lock()
  323. * will return 0, and we'll just skip setting the new mask.
  324. */
  325. if (signalfd_lock(ctx, &lk)) {
  326. ctx->sigmask = sigmask;
  327. signalfd_unlock(&lk);
  328. }
  329. wake_up(&ctx->wqh);
  330. fput(file);
  331. }
  332. return ufd;
  333. err_fdalloc:
  334. signalfd_cleanup(ctx);
  335. return error;
  336. }