signalfd.c 9.6 KB

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