blob: 7ae2a574cb25a64902128f53832b317202dbee8f [file] [log] [blame]
Davide Libenzifba2afa2007-05-10 22:23:13 -07001/*
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.
Davide Libenzib8fceee2007-09-20 12:40:16 -070014 * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
Davi Arnautb3762bf2007-05-23 13:58:04 -070015 * Retrieve multiple signals with one read() call
Davide Libenzib8fceee2007-09-20 12:40:16 -070016 * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
17 * Attach to the sighand only during read() and poll().
Davide Libenzifba2afa2007-05-10 22:23:13 -070018 */
19
20#include <linux/file.h>
21#include <linux/poll.h>
22#include <linux/init.h>
23#include <linux/fs.h>
24#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070026#include <linux/kernel.h>
27#include <linux/signal.h>
28#include <linux/list.h>
29#include <linux/anon_inodes.h>
30#include <linux/signalfd.h>
Adrian Bunk7ec37df2008-02-06 01:36:48 -080031#include <linux/syscalls.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070032
Oleg Nesterovd80e7312012-02-24 20:07:11 +010033void signalfd_cleanup(struct sighand_struct *sighand)
34{
35 wait_queue_head_t *wqh = &sighand->signalfd_wqh;
Oleg Nesterov971316f2012-02-24 20:07:29 +010036 /*
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 */
Oleg Nesterovd80e7312012-02-24 20:07:11 +010041 if (likely(!waitqueue_active(wqh)))
42 return;
43
44 /* wait_queue_t->func(POLLFREE) should do remove_wait_queue() */
45 wake_up_poll(wqh, POLLHUP | POLLFREE);
46}
47
Davide Libenzifba2afa2007-05-10 22:23:13 -070048struct signalfd_ctx {
Davide Libenzifba2afa2007-05-10 22:23:13 -070049 sigset_t sigmask;
Davide Libenzifba2afa2007-05-10 22:23:13 -070050};
51
Davide Libenzifba2afa2007-05-10 22:23:13 -070052static int signalfd_release(struct inode *inode, struct file *file)
53{
Davide Libenzib8fceee2007-09-20 12:40:16 -070054 kfree(file->private_data);
Davide Libenzifba2afa2007-05-10 22:23:13 -070055 return 0;
56}
57
58static unsigned int signalfd_poll(struct file *file, poll_table *wait)
59{
60 struct signalfd_ctx *ctx = file->private_data;
61 unsigned int events = 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -070062
Davide Libenzib8fceee2007-09-20 12:40:16 -070063 poll_wait(file, &current->sighand->signalfd_wqh, wait);
Davide Libenzifba2afa2007-05-10 22:23:13 -070064
Davide Libenzib8fceee2007-09-20 12:40:16 -070065 spin_lock_irq(&current->sighand->siglock);
66 if (next_signal(&current->pending, &ctx->sigmask) ||
67 next_signal(&current->signal->shared_pending,
68 &ctx->sigmask))
Davide Libenzifba2afa2007-05-10 22:23:13 -070069 events |= POLLIN;
Davide Libenzib8fceee2007-09-20 12:40:16 -070070 spin_unlock_irq(&current->sighand->siglock);
Davide Libenzifba2afa2007-05-10 22:23:13 -070071
72 return events;
73}
74
75/*
76 * Copied from copy_siginfo_to_user() in kernel/signal.c
77 */
78static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
79 siginfo_t const *kinfo)
80{
81 long err;
82
83 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
84
85 /*
Robert P. J. Day14e4a0f2008-02-03 15:12:15 +020086 * Unused members should be zero ...
Davide Libenzifba2afa2007-05-10 22:23:13 -070087 */
88 err = __clear_user(uinfo, sizeof(*uinfo));
89
90 /*
91 * If you change siginfo_t structure, please be sure
92 * this code is fixed accordingly.
93 */
Davide Libenzi96358de2007-10-16 23:30:23 -070094 err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
95 err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
96 err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code);
Davide Libenzifba2afa2007-05-10 22:23:13 -070097 switch (kinfo->si_code & __SI_MASK) {
98 case __SI_KILL:
Davide Libenzi96358de2007-10-16 23:30:23 -070099 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
100 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700101 break;
102 case __SI_TIMER:
Davide Libenzi96358de2007-10-16 23:30:23 -0700103 err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
104 err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
105 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
Nathan Lyncha2a20c42010-08-10 18:03:08 -0700106 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700107 break;
108 case __SI_POLL:
Davide Libenzi96358de2007-10-16 23:30:23 -0700109 err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
110 err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700111 break;
112 case __SI_FAULT:
Davide Libenzi96358de2007-10-16 23:30:23 -0700113 err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700114#ifdef __ARCH_SI_TRAPNO
Davide Libenzi96358de2007-10-16 23:30:23 -0700115 err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700116#endif
Hidetoshi Setob8aeec32010-10-07 15:31:31 +0900117#ifdef BUS_MCEERR_AO
118 /*
119 * Other callers might not initialize the si_lsb field,
120 * so check explicitly for the right codes here.
121 */
122 if (kinfo->si_code == BUS_MCEERR_AR ||
123 kinfo->si_code == BUS_MCEERR_AO)
124 err |= __put_user((short) kinfo->si_addr_lsb,
125 &uinfo->ssi_addr_lsb);
126#endif
Davide Libenzifba2afa2007-05-10 22:23:13 -0700127 break;
128 case __SI_CHLD:
Davide Libenzi96358de2007-10-16 23:30:23 -0700129 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
130 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
131 err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
132 err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
133 err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700134 break;
135 case __SI_RT: /* This is not generated by the kernel as of now. */
136 case __SI_MESGQ: /* But this is */
Davide Libenzi96358de2007-10-16 23:30:23 -0700137 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
138 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
139 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
Nathan Lyncha2a20c42010-08-10 18:03:08 -0700140 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700141 break;
Davide Libenzi0859ab52008-04-10 21:29:29 -0700142 default:
143 /*
144 * This case catches also the signals queued by sigqueue().
145 */
Davide Libenzi96358de2007-10-16 23:30:23 -0700146 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
147 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
Davide Libenzi0859ab52008-04-10 21:29:29 -0700148 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
149 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700150 break;
151 }
152
153 return err ? -EFAULT: sizeof(*uinfo);
154}
155
Davi Arnautb3762bf2007-05-23 13:58:04 -0700156static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
157 int nonblock)
158{
159 ssize_t ret;
Davi Arnautb3762bf2007-05-23 13:58:04 -0700160 DECLARE_WAITQUEUE(wait, current);
161
Davide Libenzib8fceee2007-09-20 12:40:16 -0700162 spin_lock_irq(&current->sighand->siglock);
163 ret = dequeue_signal(current, &ctx->sigmask, info);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700164 switch (ret) {
165 case 0:
166 if (!nonblock)
167 break;
168 ret = -EAGAIN;
169 default:
Davide Libenzib8fceee2007-09-20 12:40:16 -0700170 spin_unlock_irq(&current->sighand->siglock);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700171 return ret;
172 }
173
Davide Libenzib8fceee2007-09-20 12:40:16 -0700174 add_wait_queue(&current->sighand->signalfd_wqh, &wait);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700175 for (;;) {
176 set_current_state(TASK_INTERRUPTIBLE);
Davide Libenzib8fceee2007-09-20 12:40:16 -0700177 ret = dequeue_signal(current, &ctx->sigmask, info);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700178 if (ret != 0)
179 break;
180 if (signal_pending(current)) {
181 ret = -ERESTARTSYS;
182 break;
183 }
Davide Libenzib8fceee2007-09-20 12:40:16 -0700184 spin_unlock_irq(&current->sighand->siglock);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700185 schedule();
Davide Libenzib8fceee2007-09-20 12:40:16 -0700186 spin_lock_irq(&current->sighand->siglock);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700187 }
Davide Libenzib8fceee2007-09-20 12:40:16 -0700188 spin_unlock_irq(&current->sighand->siglock);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700189
Davide Libenzib8fceee2007-09-20 12:40:16 -0700190 remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
Davi Arnautb3762bf2007-05-23 13:58:04 -0700191 __set_current_state(TASK_RUNNING);
192
193 return ret;
194}
195
Davide Libenzifba2afa2007-05-10 22:23:13 -0700196/*
Davide Libenzib8fceee2007-09-20 12:40:16 -0700197 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
198 * error code. The "count" parameter must be at least the size of a
199 * "struct signalfd_siginfo".
Davide Libenzifba2afa2007-05-10 22:23:13 -0700200 */
201static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
202 loff_t *ppos)
203{
204 struct signalfd_ctx *ctx = file->private_data;
Davi Arnautb3762bf2007-05-23 13:58:04 -0700205 struct signalfd_siginfo __user *siginfo;
206 int nonblock = file->f_flags & O_NONBLOCK;
207 ssize_t ret, total = 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700208 siginfo_t info;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700209
Davi Arnautb3762bf2007-05-23 13:58:04 -0700210 count /= sizeof(struct signalfd_siginfo);
211 if (!count)
Davide Libenzifba2afa2007-05-10 22:23:13 -0700212 return -EINVAL;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700213
Davi Arnautb3762bf2007-05-23 13:58:04 -0700214 siginfo = (struct signalfd_siginfo __user *) buf;
Davi Arnautb3762bf2007-05-23 13:58:04 -0700215 do {
216 ret = signalfd_dequeue(ctx, &info, nonblock);
217 if (unlikely(ret <= 0))
218 break;
219 ret = signalfd_copyinfo(siginfo, &info);
220 if (ret < 0)
221 break;
222 siginfo++;
223 total += ret;
224 nonblock = 1;
225 } while (--count);
226
Davide Libenzib8fceee2007-09-20 12:40:16 -0700227 return total ? total: ret;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700228}
229
230static const struct file_operations signalfd_fops = {
231 .release = signalfd_release,
232 .poll = signalfd_poll,
233 .read = signalfd_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200234 .llseek = noop_llseek,
Davide Libenzifba2afa2007-05-10 22:23:13 -0700235};
236
Heiko Carstens836f92a2009-01-14 14:14:33 +0100237SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
238 size_t, sizemask, int, flags)
Davide Libenzifba2afa2007-05-10 22:23:13 -0700239{
Davide Libenzifba2afa2007-05-10 22:23:13 -0700240 sigset_t sigmask;
241 struct signalfd_ctx *ctx;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700242
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700243 /* Check the SFD_* constants for consistency. */
244 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
245 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
246
Ulrich Drepper5fb5e042008-07-23 21:29:37 -0700247 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
Ulrich Drepper9deb27b2008-07-23 21:29:24 -0700248 return -EINVAL;
249
Davide Libenzifba2afa2007-05-10 22:23:13 -0700250 if (sizemask != sizeof(sigset_t) ||
251 copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
Ulrich Drepperf50cada2007-07-26 10:40:55 -0700252 return -EINVAL;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700253 sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
254 signotset(&sigmask);
255
256 if (ufd == -1) {
257 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
258 if (!ctx)
259 return -ENOMEM;
260
Davide Libenzifba2afa2007-05-10 22:23:13 -0700261 ctx->sigmask = sigmask;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700262
263 /*
264 * When we call this, the initialization must be complete, since
265 * anon_inode_getfd() will install the fd.
266 */
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -0700267 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
Roland Dreier628ff7c2009-12-18 09:41:24 -0800268 O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
Al Viro2030a422008-02-23 06:46:49 -0500269 if (ufd < 0)
270 kfree(ctx);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700271 } else {
Al Viro2030a422008-02-23 06:46:49 -0500272 struct file *file = fget(ufd);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700273 if (!file)
274 return -EBADF;
275 ctx = file->private_data;
276 if (file->f_op != &signalfd_fops) {
277 fput(file);
278 return -EINVAL;
279 }
Davide Libenzib8fceee2007-09-20 12:40:16 -0700280 spin_lock_irq(&current->sighand->siglock);
281 ctx->sigmask = sigmask;
282 spin_unlock_irq(&current->sighand->siglock);
283
284 wake_up(&current->sighand->signalfd_wqh);
Davide Libenzifba2afa2007-05-10 22:23:13 -0700285 fput(file);
286 }
287
288 return ufd;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700289}
Ulrich Drepper9deb27b2008-07-23 21:29:24 -0700290
Heiko Carstens836f92a2009-01-14 14:14:33 +0100291SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
292 size_t, sizemask)
Ulrich Drepper9deb27b2008-07-23 21:29:24 -0700293{
294 return sys_signalfd4(ufd, user_mask, sizemask, 0);
295}