Blame view
fs/signalfd.c
7.52 KB
fba2afaae
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* * fs/signalfd.c * * Copyright (C) 2003 Linus Torvalds * * Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org> * Changed ->read() to return a siginfo strcture instead of signal number. * Fixed locking in ->poll(). * Added sighand-detach notification. * Added fd re-use in sys_signalfd() syscall. * Now using anonymous inode source. * Thanks to Oleg Nesterov for useful code review and suggestions. * More comments and suggestions from Arnd Bergmann. |
b8fceee17
|
14 |
* Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br> |
b3762bfc8
|
15 |
* Retrieve multiple signals with one read() call |
b8fceee17
|
16 17 |
* Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org> * Attach to the sighand only during read() and poll(). |
fba2afaae
|
18 19 20 21 22 23 24 |
*/ #include <linux/file.h> #include <linux/poll.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/sched.h> |
5a0e3ad6a
|
25 |
#include <linux/slab.h> |
fba2afaae
|
26 27 28 29 30 |
#include <linux/kernel.h> #include <linux/signal.h> #include <linux/list.h> #include <linux/anon_inodes.h> #include <linux/signalfd.h> |
7ec37dfd4
|
31 |
#include <linux/syscalls.h> |
fba2afaae
|
32 33 |
struct signalfd_ctx { |
fba2afaae
|
34 |
sigset_t sigmask; |
fba2afaae
|
35 |
}; |
fba2afaae
|
36 37 |
static int signalfd_release(struct inode *inode, struct file *file) { |
b8fceee17
|
38 |
kfree(file->private_data); |
fba2afaae
|
39 40 41 42 43 44 45 |
return 0; } static unsigned int signalfd_poll(struct file *file, poll_table *wait) { struct signalfd_ctx *ctx = file->private_data; unsigned int events = 0; |
fba2afaae
|
46 |
|
b8fceee17
|
47 |
poll_wait(file, ¤t->sighand->signalfd_wqh, wait); |
fba2afaae
|
48 |
|
b8fceee17
|
49 50 51 52 |
spin_lock_irq(¤t->sighand->siglock); if (next_signal(¤t->pending, &ctx->sigmask) || next_signal(¤t->signal->shared_pending, &ctx->sigmask)) |
fba2afaae
|
53 |
events |= POLLIN; |
b8fceee17
|
54 |
spin_unlock_irq(¤t->sighand->siglock); |
fba2afaae
|
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
return events; } /* * Copied from copy_siginfo_to_user() in kernel/signal.c */ static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo, siginfo_t const *kinfo) { long err; BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128); /* |
14e4a0f2b
|
70 |
* Unused members should be zero ... |
fba2afaae
|
71 72 73 74 75 76 77 |
*/ err = __clear_user(uinfo, sizeof(*uinfo)); /* * If you change siginfo_t structure, please be sure * this code is fixed accordingly. */ |
96358de6b
|
78 79 80 |
err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo); err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno); err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code); |
fba2afaae
|
81 82 |
switch (kinfo->si_code & __SI_MASK) { case __SI_KILL: |
96358de6b
|
83 84 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); |
fba2afaae
|
85 86 |
break; case __SI_TIMER: |
96358de6b
|
87 88 89 |
err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid); err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun); err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr); |
a2a20c412
|
90 |
err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae
|
91 92 |
break; case __SI_POLL: |
96358de6b
|
93 94 |
err |= __put_user(kinfo->si_band, &uinfo->ssi_band); err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd); |
fba2afaae
|
95 96 |
break; case __SI_FAULT: |
96358de6b
|
97 |
err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr); |
fba2afaae
|
98 |
#ifdef __ARCH_SI_TRAPNO |
96358de6b
|
99 |
err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno); |
fba2afaae
|
100 |
#endif |
b8aeec341
|
101 102 103 104 105 106 107 108 109 110 |
#ifdef BUS_MCEERR_AO /* * Other callers might not initialize the si_lsb field, * so check explicitly for the right codes here. */ if (kinfo->si_code == BUS_MCEERR_AR || kinfo->si_code == BUS_MCEERR_AO) err |= __put_user((short) kinfo->si_addr_lsb, &uinfo->ssi_addr_lsb); #endif |
fba2afaae
|
111 112 |
break; case __SI_CHLD: |
96358de6b
|
113 114 115 116 117 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); err |= __put_user(kinfo->si_status, &uinfo->ssi_status); err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime); err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime); |
fba2afaae
|
118 119 120 |
break; case __SI_RT: /* This is not generated by the kernel as of now. */ case __SI_MESGQ: /* But this is */ |
96358de6b
|
121 122 123 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr); |
a2a20c412
|
124 |
err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae
|
125 |
break; |
0859ab59a
|
126 127 128 129 |
default: /* * This case catches also the signals queued by sigqueue(). */ |
96358de6b
|
130 131 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); |
0859ab59a
|
132 133 |
err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr); err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae
|
134 135 136 137 138 |
break; } return err ? -EFAULT: sizeof(*uinfo); } |
b3762bfc8
|
139 140 141 142 |
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info, int nonblock) { ssize_t ret; |
b3762bfc8
|
143 |
DECLARE_WAITQUEUE(wait, current); |
b8fceee17
|
144 145 |
spin_lock_irq(¤t->sighand->siglock); ret = dequeue_signal(current, &ctx->sigmask, info); |
b3762bfc8
|
146 147 148 149 150 151 |
switch (ret) { case 0: if (!nonblock) break; ret = -EAGAIN; default: |
b8fceee17
|
152 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8
|
153 154 |
return ret; } |
b8fceee17
|
155 |
add_wait_queue(¤t->sighand->signalfd_wqh, &wait); |
b3762bfc8
|
156 157 |
for (;;) { set_current_state(TASK_INTERRUPTIBLE); |
b8fceee17
|
158 |
ret = dequeue_signal(current, &ctx->sigmask, info); |
b3762bfc8
|
159 160 161 162 163 164 |
if (ret != 0) break; if (signal_pending(current)) { ret = -ERESTARTSYS; break; } |
b8fceee17
|
165 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8
|
166 |
schedule(); |
b8fceee17
|
167 |
spin_lock_irq(¤t->sighand->siglock); |
b3762bfc8
|
168 |
} |
b8fceee17
|
169 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8
|
170 |
|
b8fceee17
|
171 |
remove_wait_queue(¤t->sighand->signalfd_wqh, &wait); |
b3762bfc8
|
172 173 174 175 |
__set_current_state(TASK_RUNNING); return ret; } |
fba2afaae
|
176 |
/* |
b8fceee17
|
177 178 179 |
* Returns a multiple of the size of a "struct signalfd_siginfo", or a negative * error code. The "count" parameter must be at least the size of a * "struct signalfd_siginfo". |
fba2afaae
|
180 181 182 183 184 |
*/ static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct signalfd_ctx *ctx = file->private_data; |
b3762bfc8
|
185 186 187 |
struct signalfd_siginfo __user *siginfo; int nonblock = file->f_flags & O_NONBLOCK; ssize_t ret, total = 0; |
fba2afaae
|
188 |
siginfo_t info; |
fba2afaae
|
189 |
|
b3762bfc8
|
190 191 |
count /= sizeof(struct signalfd_siginfo); if (!count) |
fba2afaae
|
192 |
return -EINVAL; |
fba2afaae
|
193 |
|
b3762bfc8
|
194 |
siginfo = (struct signalfd_siginfo __user *) buf; |
b3762bfc8
|
195 196 197 198 199 200 201 202 203 204 205 |
do { ret = signalfd_dequeue(ctx, &info, nonblock); if (unlikely(ret <= 0)) break; ret = signalfd_copyinfo(siginfo, &info); if (ret < 0) break; siginfo++; total += ret; nonblock = 1; } while (--count); |
b8fceee17
|
206 |
return total ? total: ret; |
fba2afaae
|
207 208 209 210 211 212 |
} static const struct file_operations signalfd_fops = { .release = signalfd_release, .poll = signalfd_poll, .read = signalfd_read, |
6038f373a
|
213 |
.llseek = noop_llseek, |
fba2afaae
|
214 |
}; |
836f92adf
|
215 216 |
SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, size_t, sizemask, int, flags) |
fba2afaae
|
217 |
{ |
fba2afaae
|
218 219 |
sigset_t sigmask; struct signalfd_ctx *ctx; |
fba2afaae
|
220 |
|
e38b36f32
|
221 222 223 |
/* Check the SFD_* constants for consistency. */ BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC); BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK); |
5fb5e0492
|
224 |
if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK)) |
9deb27bae
|
225 |
return -EINVAL; |
fba2afaae
|
226 227 |
if (sizemask != sizeof(sigset_t) || copy_from_user(&sigmask, user_mask, sizeof(sigmask))) |
f50cadaa8
|
228 |
return -EINVAL; |
fba2afaae
|
229 230 231 232 233 234 235 |
sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP)); signotset(&sigmask); if (ufd == -1) { ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; |
fba2afaae
|
236 |
ctx->sigmask = sigmask; |
fba2afaae
|
237 238 239 240 241 |
/* * When we call this, the initialization must be complete, since * anon_inode_getfd() will install the fd. */ |
7d9dbca34
|
242 |
ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx, |
628ff7c1d
|
243 |
O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK))); |
2030a42ce
|
244 245 |
if (ufd < 0) kfree(ctx); |
fba2afaae
|
246 |
} else { |
2030a42ce
|
247 |
struct file *file = fget(ufd); |
fba2afaae
|
248 249 250 251 252 253 254 |
if (!file) return -EBADF; ctx = file->private_data; if (file->f_op != &signalfd_fops) { fput(file); return -EINVAL; } |
b8fceee17
|
255 256 257 258 259 |
spin_lock_irq(¤t->sighand->siglock); ctx->sigmask = sigmask; spin_unlock_irq(¤t->sighand->siglock); wake_up(¤t->sighand->signalfd_wqh); |
fba2afaae
|
260 261 262 263 |
fput(file); } return ufd; |
fba2afaae
|
264 |
} |
9deb27bae
|
265 |
|
836f92adf
|
266 267 |
SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask, size_t, sizemask) |
9deb27bae
|
268 269 270 |
{ return sys_signalfd4(ufd, user_mask, sizemask, 0); } |