Blame view
fs/signalfd.c
7.05 KB
fba2afaae signal/timer/even... |
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 signalfd simplifi... |
14 |
* Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br> |
b3762bfc8 signalfd: retriev... |
15 |
* Retrieve multiple signals with one read() call |
b8fceee17 signalfd simplifi... |
16 17 |
* Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org> * Attach to the sighand only during read() and poll(). |
fba2afaae signal/timer/even... |
18 19 20 21 22 23 24 25 26 27 28 29 |
*/ #include <linux/file.h> #include <linux/poll.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/signal.h> #include <linux/list.h> #include <linux/anon_inodes.h> #include <linux/signalfd.h> |
7ec37dfd4 fs/signalfd.c sho... |
30 |
#include <linux/syscalls.h> |
fba2afaae signal/timer/even... |
31 32 |
struct signalfd_ctx { |
fba2afaae signal/timer/even... |
33 |
sigset_t sigmask; |
fba2afaae signal/timer/even... |
34 |
}; |
fba2afaae signal/timer/even... |
35 36 |
static int signalfd_release(struct inode *inode, struct file *file) { |
b8fceee17 signalfd simplifi... |
37 |
kfree(file->private_data); |
fba2afaae signal/timer/even... |
38 39 40 41 42 43 44 |
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 signal/timer/even... |
45 |
|
b8fceee17 signalfd simplifi... |
46 |
poll_wait(file, ¤t->sighand->signalfd_wqh, wait); |
fba2afaae signal/timer/even... |
47 |
|
b8fceee17 signalfd simplifi... |
48 49 50 51 |
spin_lock_irq(¤t->sighand->siglock); if (next_signal(¤t->pending, &ctx->sigmask) || next_signal(¤t->signal->shared_pending, &ctx->sigmask)) |
fba2afaae signal/timer/even... |
52 |
events |= POLLIN; |
b8fceee17 signalfd simplifi... |
53 |
spin_unlock_irq(¤t->sighand->siglock); |
fba2afaae signal/timer/even... |
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
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 Fix a small numbe... |
69 |
* Unused members should be zero ... |
fba2afaae signal/timer/even... |
70 71 72 73 74 75 76 |
*/ err = __clear_user(uinfo, sizeof(*uinfo)); /* * If you change siginfo_t structure, please be sure * this code is fixed accordingly. */ |
96358de6b rename signalfd_s... |
77 78 79 |
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 signal/timer/even... |
80 81 |
switch (kinfo->si_code & __SI_MASK) { case __SI_KILL: |
96358de6b rename signalfd_s... |
82 83 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); |
fba2afaae signal/timer/even... |
84 85 |
break; case __SI_TIMER: |
96358de6b rename signalfd_s... |
86 87 88 |
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); |
fba2afaae signal/timer/even... |
89 90 |
break; case __SI_POLL: |
96358de6b rename signalfd_s... |
91 92 |
err |= __put_user(kinfo->si_band, &uinfo->ssi_band); err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd); |
fba2afaae signal/timer/even... |
93 94 |
break; case __SI_FAULT: |
96358de6b rename signalfd_s... |
95 |
err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr); |
fba2afaae signal/timer/even... |
96 |
#ifdef __ARCH_SI_TRAPNO |
96358de6b rename signalfd_s... |
97 |
err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno); |
fba2afaae signal/timer/even... |
98 99 100 |
#endif break; case __SI_CHLD: |
96358de6b rename signalfd_s... |
101 102 103 104 105 |
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 signal/timer/even... |
106 107 108 |
break; case __SI_RT: /* This is not generated by the kernel as of now. */ case __SI_MESGQ: /* But this is */ |
96358de6b rename signalfd_s... |
109 110 111 |
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); |
fba2afaae signal/timer/even... |
112 |
break; |
0859ab59a signalfd: fix for... |
113 114 115 116 |
default: /* * This case catches also the signals queued by sigqueue(). */ |
96358de6b rename signalfd_s... |
117 118 |
err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid); err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid); |
0859ab59a signalfd: fix for... |
119 120 |
err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr); err |= __put_user(kinfo->si_int, &uinfo->ssi_int); |
fba2afaae signal/timer/even... |
121 122 123 124 125 |
break; } return err ? -EFAULT: sizeof(*uinfo); } |
b3762bfc8 signalfd: retriev... |
126 127 128 129 |
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info, int nonblock) { ssize_t ret; |
b3762bfc8 signalfd: retriev... |
130 |
DECLARE_WAITQUEUE(wait, current); |
b8fceee17 signalfd simplifi... |
131 132 |
spin_lock_irq(¤t->sighand->siglock); ret = dequeue_signal(current, &ctx->sigmask, info); |
b3762bfc8 signalfd: retriev... |
133 134 135 136 137 138 |
switch (ret) { case 0: if (!nonblock) break; ret = -EAGAIN; default: |
b8fceee17 signalfd simplifi... |
139 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8 signalfd: retriev... |
140 141 |
return ret; } |
b8fceee17 signalfd simplifi... |
142 |
add_wait_queue(¤t->sighand->signalfd_wqh, &wait); |
b3762bfc8 signalfd: retriev... |
143 144 |
for (;;) { set_current_state(TASK_INTERRUPTIBLE); |
b8fceee17 signalfd simplifi... |
145 |
ret = dequeue_signal(current, &ctx->sigmask, info); |
b3762bfc8 signalfd: retriev... |
146 147 148 149 150 151 |
if (ret != 0) break; if (signal_pending(current)) { ret = -ERESTARTSYS; break; } |
b8fceee17 signalfd simplifi... |
152 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8 signalfd: retriev... |
153 |
schedule(); |
b8fceee17 signalfd simplifi... |
154 |
spin_lock_irq(¤t->sighand->siglock); |
b3762bfc8 signalfd: retriev... |
155 |
} |
b8fceee17 signalfd simplifi... |
156 |
spin_unlock_irq(¤t->sighand->siglock); |
b3762bfc8 signalfd: retriev... |
157 |
|
b8fceee17 signalfd simplifi... |
158 |
remove_wait_queue(¤t->sighand->signalfd_wqh, &wait); |
b3762bfc8 signalfd: retriev... |
159 160 161 162 |
__set_current_state(TASK_RUNNING); return ret; } |
fba2afaae signal/timer/even... |
163 |
/* |
b8fceee17 signalfd simplifi... |
164 165 166 |
* 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 signal/timer/even... |
167 168 169 170 171 |
*/ 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 signalfd: retriev... |
172 173 174 |
struct signalfd_siginfo __user *siginfo; int nonblock = file->f_flags & O_NONBLOCK; ssize_t ret, total = 0; |
fba2afaae signal/timer/even... |
175 |
siginfo_t info; |
fba2afaae signal/timer/even... |
176 |
|
b3762bfc8 signalfd: retriev... |
177 178 |
count /= sizeof(struct signalfd_siginfo); if (!count) |
fba2afaae signal/timer/even... |
179 |
return -EINVAL; |
fba2afaae signal/timer/even... |
180 |
|
b3762bfc8 signalfd: retriev... |
181 |
siginfo = (struct signalfd_siginfo __user *) buf; |
b3762bfc8 signalfd: retriev... |
182 183 184 185 186 187 188 189 190 191 192 |
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 signalfd simplifi... |
193 |
return total ? total: ret; |
fba2afaae signal/timer/even... |
194 195 196 197 198 199 200 |
} static const struct file_operations signalfd_fops = { .release = signalfd_release, .poll = signalfd_poll, .read = signalfd_read, }; |
836f92adf [CVE-2009-0029] S... |
201 202 |
SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, size_t, sizemask, int, flags) |
fba2afaae signal/timer/even... |
203 |
{ |
fba2afaae signal/timer/even... |
204 205 |
sigset_t sigmask; struct signalfd_ctx *ctx; |
fba2afaae signal/timer/even... |
206 |
|
e38b36f32 flag parameters: ... |
207 208 209 |
/* Check the SFD_* constants for consistency. */ BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC); BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK); |
5fb5e0492 flag parameters: ... |
210 |
if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK)) |
9deb27bae flag parameters: ... |
211 |
return -EINVAL; |
fba2afaae signal/timer/even... |
212 213 |
if (sizemask != sizeof(sigset_t) || copy_from_user(&sigmask, user_mask, sizeof(sigmask))) |
f50cadaa8 tiny signalfd cle... |
214 |
return -EINVAL; |
fba2afaae signal/timer/even... |
215 216 217 218 219 220 221 |
sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP)); signotset(&sigmask); if (ufd == -1) { ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; |
fba2afaae signal/timer/even... |
222 |
ctx->sigmask = sigmask; |
fba2afaae signal/timer/even... |
223 224 225 226 227 |
/* * When we call this, the initialization must be complete, since * anon_inode_getfd() will install the fd. */ |
7d9dbca34 flag parameters: ... |
228 |
ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx, |
5fb5e0492 flag parameters: ... |
229 |
flags & (O_CLOEXEC | O_NONBLOCK)); |
2030a42ce [PATCH] sanitize ... |
230 231 |
if (ufd < 0) kfree(ctx); |
fba2afaae signal/timer/even... |
232 |
} else { |
2030a42ce [PATCH] sanitize ... |
233 |
struct file *file = fget(ufd); |
fba2afaae signal/timer/even... |
234 235 236 237 238 239 240 |
if (!file) return -EBADF; ctx = file->private_data; if (file->f_op != &signalfd_fops) { fput(file); return -EINVAL; } |
b8fceee17 signalfd simplifi... |
241 242 243 244 245 |
spin_lock_irq(¤t->sighand->siglock); ctx->sigmask = sigmask; spin_unlock_irq(¤t->sighand->siglock); wake_up(¤t->sighand->signalfd_wqh); |
fba2afaae signal/timer/even... |
246 247 248 249 |
fput(file); } return ufd; |
fba2afaae signal/timer/even... |
250 |
} |
9deb27bae flag parameters: ... |
251 |
|
836f92adf [CVE-2009-0029] S... |
252 253 |
SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask, size_t, sizemask) |
9deb27bae flag parameters: ... |
254 255 256 |
{ return sys_signalfd4(ufd, user_mask, sizemask, 0); } |