Blame view
fs/signalfd.c
8.06 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 |
|
d80e731ec
|
33 34 35 |
void signalfd_cleanup(struct sighand_struct *sighand) { wait_queue_head_t *wqh = &sighand->signalfd_wqh; |
971316f05
|
36 37 38 39 40 |
/* * The lockless check can race with remove_wait_queue() in progress, * but in this case its caller should run under rcu_read_lock() and * sighand_cachep is SLAB_DESTROY_BY_RCU, we can safely return. */ |
d80e731ec
|
41 42 43 44 45 46 |
if (likely(!waitqueue_active(wqh))) return; /* wait_queue_t->func(POLLFREE) should do remove_wait_queue() */ wake_up_poll(wqh, POLLHUP | POLLFREE); } |
fba2afaae
|
47 |
struct signalfd_ctx { |
fba2afaae
|
48 |
sigset_t sigmask; |
fba2afaae
|
49 |
}; |
fba2afaae
|
50 51 |
static int signalfd_release(struct inode *inode, struct file *file) { |
b8fceee17
|
52 |
kfree(file->private_data); |
fba2afaae
|
53 54 55 56 57 58 59 |
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
|
60 |
|
b8fceee17
|
61 |
poll_wait(file, ¤t->sighand->signalfd_wqh, wait); |
fba2afaae
|
62 |
|
b8fceee17
|
63 64 65 66 |
spin_lock_irq(¤t->sighand->siglock); if (next_signal(¤t->pending, &ctx->sigmask) || next_signal(¤t->signal->shared_pending, &ctx->sigmask)) |
fba2afaae
|
67 |
events |= POLLIN; |
b8fceee17
|
68 |
spin_unlock_irq(¤t->sighand->siglock); |
fba2afaae
|
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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
|
84 |
* Unused members should be zero ... |
fba2afaae
|
85 86 87 88 89 90 91 |
*/ err = __clear_user(uinfo, sizeof(*uinfo)); /* * If you change siginfo_t structure, please be sure * this code is fixed accordingly. */ |
96358de6b
|
92 93 94 |
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
|
95 96 |
switch (kinfo->si_code & __SI_MASK) { case __SI_KILL: |
96358de6b
|
97 98 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); |
fba2afaae
|
99 100 |
break; case __SI_TIMER: |
96358de6b
|
101 102 103 |
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
|
104 |
err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae
|
105 106 |
break; case __SI_POLL: |
96358de6b
|
107 108 |
err |= __put_user(kinfo->si_band, &uinfo->ssi_band); err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd); |
fba2afaae
|
109 110 |
break; case __SI_FAULT: |
96358de6b
|
111 |
err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr); |
fba2afaae
|
112 |
#ifdef __ARCH_SI_TRAPNO |
96358de6b
|
113 |
err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno); |
fba2afaae
|
114 |
#endif |
b8aeec341
|
115 116 117 118 119 120 121 122 123 124 |
#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
|
125 126 |
break; case __SI_CHLD: |
96358de6b
|
127 128 129 130 131 |
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
|
132 133 134 |
break; case __SI_RT: /* This is not generated by the kernel as of now. */ case __SI_MESGQ: /* But this is */ |
96358de6b
|
135 136 137 |
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
|
138 |
err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae
|
139 |
break; |
0859ab59a
|
140 141 142 143 |
default: /* * This case catches also the signals queued by sigqueue(). */ |
96358de6b
|
144 145 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); |
0859ab59a
|
146 147 |
err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr); err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae
|
148 149 150 151 152 |
break; } return err ? -EFAULT: sizeof(*uinfo); } |
b3762bfc8
|
153 154 155 156 |
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info, int nonblock) { ssize_t ret; |
b3762bfc8
|
157 |
DECLARE_WAITQUEUE(wait, current); |
b8fceee17
|
158 159 |
spin_lock_irq(¤t->sighand->siglock); ret = dequeue_signal(current, &ctx->sigmask, info); |
b3762bfc8
|
160 161 162 163 164 165 |
switch (ret) { case 0: if (!nonblock) break; ret = -EAGAIN; default: |
b8fceee17
|
166 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8
|
167 168 |
return ret; } |
b8fceee17
|
169 |
add_wait_queue(¤t->sighand->signalfd_wqh, &wait); |
b3762bfc8
|
170 171 |
for (;;) { set_current_state(TASK_INTERRUPTIBLE); |
b8fceee17
|
172 |
ret = dequeue_signal(current, &ctx->sigmask, info); |
b3762bfc8
|
173 174 175 176 177 178 |
if (ret != 0) break; if (signal_pending(current)) { ret = -ERESTARTSYS; break; } |
b8fceee17
|
179 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8
|
180 |
schedule(); |
b8fceee17
|
181 |
spin_lock_irq(¤t->sighand->siglock); |
b3762bfc8
|
182 |
} |
b8fceee17
|
183 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8
|
184 |
|
b8fceee17
|
185 |
remove_wait_queue(¤t->sighand->signalfd_wqh, &wait); |
b3762bfc8
|
186 187 188 189 |
__set_current_state(TASK_RUNNING); return ret; } |
fba2afaae
|
190 |
/* |
b8fceee17
|
191 192 193 |
* 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
|
194 195 196 197 198 |
*/ 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
|
199 200 201 |
struct signalfd_siginfo __user *siginfo; int nonblock = file->f_flags & O_NONBLOCK; ssize_t ret, total = 0; |
fba2afaae
|
202 |
siginfo_t info; |
fba2afaae
|
203 |
|
b3762bfc8
|
204 205 |
count /= sizeof(struct signalfd_siginfo); if (!count) |
fba2afaae
|
206 |
return -EINVAL; |
fba2afaae
|
207 |
|
b3762bfc8
|
208 |
siginfo = (struct signalfd_siginfo __user *) buf; |
b3762bfc8
|
209 210 211 212 213 214 215 216 217 218 219 |
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
|
220 |
return total ? total: ret; |
fba2afaae
|
221 222 223 224 225 226 |
} static const struct file_operations signalfd_fops = { .release = signalfd_release, .poll = signalfd_poll, .read = signalfd_read, |
6038f373a
|
227 |
.llseek = noop_llseek, |
fba2afaae
|
228 |
}; |
836f92adf
|
229 230 |
SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, size_t, sizemask, int, flags) |
fba2afaae
|
231 |
{ |
fba2afaae
|
232 233 |
sigset_t sigmask; struct signalfd_ctx *ctx; |
fba2afaae
|
234 |
|
e38b36f32
|
235 236 237 |
/* Check the SFD_* constants for consistency. */ BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC); BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK); |
5fb5e0492
|
238 |
if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK)) |
9deb27bae
|
239 |
return -EINVAL; |
fba2afaae
|
240 241 |
if (sizemask != sizeof(sigset_t) || copy_from_user(&sigmask, user_mask, sizeof(sigmask))) |
f50cadaa8
|
242 |
return -EINVAL; |
fba2afaae
|
243 244 245 246 247 248 249 |
sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP)); signotset(&sigmask); if (ufd == -1) { ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; |
fba2afaae
|
250 |
ctx->sigmask = sigmask; |
fba2afaae
|
251 252 253 254 255 |
/* * When we call this, the initialization must be complete, since * anon_inode_getfd() will install the fd. */ |
7d9dbca34
|
256 |
ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx, |
628ff7c1d
|
257 |
O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK))); |
2030a42ce
|
258 259 |
if (ufd < 0) kfree(ctx); |
fba2afaae
|
260 |
} else { |
20ba5d736
|
261 262 |
int fput_needed; struct file *file = fget_light(ufd, &fput_needed); |
fba2afaae
|
263 264 265 266 |
if (!file) return -EBADF; ctx = file->private_data; if (file->f_op != &signalfd_fops) { |
20ba5d736
|
267 |
fput_light(file, fput_needed); |
fba2afaae
|
268 269 |
return -EINVAL; } |
b8fceee17
|
270 271 272 273 274 |
spin_lock_irq(¤t->sighand->siglock); ctx->sigmask = sigmask; spin_unlock_irq(¤t->sighand->siglock); wake_up(¤t->sighand->signalfd_wqh); |
20ba5d736
|
275 |
fput_light(file, fput_needed); |
fba2afaae
|
276 277 278 |
} return ufd; |
fba2afaae
|
279 |
} |
9deb27bae
|
280 |
|
836f92adf
|
281 282 |
SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask, size_t, sizemask) |
9deb27bae
|
283 284 285 |
{ return sys_signalfd4(ufd, user_mask, sizemask, 0); } |