[DCCP]: Fix build warning when debugging is disabled.
[linux-2.6.22.y-op.git] / fs / signalfd.c
blobf1da89203a9aeea3108fe9270169c501df821129
1 /*
2 * fs/signalfd.c
4 * Copyright (C) 2003 Linus Torvalds
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
18 #include <linux/file.h>
19 #include <linux/poll.h>
20 #include <linux/init.h>
21 #include <linux/fs.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/signal.h>
25 #include <linux/list.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/signalfd.h>
29 struct signalfd_ctx {
30 struct list_head lnk;
31 wait_queue_head_t wqh;
32 sigset_t sigmask;
33 struct task_struct *tsk;
36 struct signalfd_lockctx {
37 struct task_struct *tsk;
38 unsigned long flags;
42 * Tries to acquire the sighand lock. We do not increment the sighand
43 * use count, and we do not even pin the task struct, so we need to
44 * do it inside an RCU read lock, and we must be prepared for the
45 * ctx->tsk going to NULL (in signalfd_deliver()), and for the sighand
46 * being detached. We return 0 if the sighand has been detached, or
47 * 1 if we were able to pin the sighand lock.
49 static int signalfd_lock(struct signalfd_ctx *ctx, struct signalfd_lockctx *lk)
51 struct sighand_struct *sighand = NULL;
53 rcu_read_lock();
54 lk->tsk = rcu_dereference(ctx->tsk);
55 if (likely(lk->tsk != NULL))
56 sighand = lock_task_sighand(lk->tsk, &lk->flags);
57 rcu_read_unlock();
59 if (sighand && !ctx->tsk) {
60 unlock_task_sighand(lk->tsk, &lk->flags);
61 sighand = NULL;
64 return sighand != NULL;
67 static void signalfd_unlock(struct signalfd_lockctx *lk)
69 unlock_task_sighand(lk->tsk, &lk->flags);
73 * This must be called with the sighand lock held.
75 void signalfd_deliver(struct task_struct *tsk, int sig)
77 struct sighand_struct *sighand = tsk->sighand;
78 struct signalfd_ctx *ctx, *tmp;
80 BUG_ON(!sig);
81 list_for_each_entry_safe(ctx, tmp, &sighand->signalfd_list, lnk) {
83 * We use a negative signal value as a way to broadcast that the
84 * sighand has been orphaned, so that we can notify all the
85 * listeners about this. Remember the ctx->sigmask is inverted,
86 * so if the user is interested in a signal, that corresponding
87 * bit will be zero.
89 if (sig < 0) {
90 if (ctx->tsk == tsk) {
91 ctx->tsk = NULL;
92 list_del_init(&ctx->lnk);
93 wake_up(&ctx->wqh);
95 } else {
96 if (!sigismember(&ctx->sigmask, sig))
97 wake_up(&ctx->wqh);
102 static void signalfd_cleanup(struct signalfd_ctx *ctx)
104 struct signalfd_lockctx lk;
107 * This is tricky. If the sighand is gone, we do not need to remove
108 * context from the list, the list itself won't be there anymore.
110 if (signalfd_lock(ctx, &lk)) {
111 list_del(&ctx->lnk);
112 signalfd_unlock(&lk);
114 kfree(ctx);
117 static int signalfd_release(struct inode *inode, struct file *file)
119 signalfd_cleanup(file->private_data);
120 return 0;
123 static unsigned int signalfd_poll(struct file *file, poll_table *wait)
125 struct signalfd_ctx *ctx = file->private_data;
126 unsigned int events = 0;
127 struct signalfd_lockctx lk;
129 poll_wait(file, &ctx->wqh, wait);
132 * Let the caller get a POLLIN in this case, ala socket recv() when
133 * the peer disconnects.
135 if (signalfd_lock(ctx, &lk)) {
136 if (next_signal(&lk.tsk->pending, &ctx->sigmask) > 0 ||
137 next_signal(&lk.tsk->signal->shared_pending,
138 &ctx->sigmask) > 0)
139 events |= POLLIN;
140 signalfd_unlock(&lk);
141 } else
142 events |= POLLIN;
144 return events;
148 * Copied from copy_siginfo_to_user() in kernel/signal.c
150 static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
151 siginfo_t const *kinfo)
153 long err;
155 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
158 * Unused memebers should be zero ...
160 err = __clear_user(uinfo, sizeof(*uinfo));
163 * If you change siginfo_t structure, please be sure
164 * this code is fixed accordingly.
166 err |= __put_user(kinfo->si_signo, &uinfo->signo);
167 err |= __put_user(kinfo->si_errno, &uinfo->err);
168 err |= __put_user((short)kinfo->si_code, &uinfo->code);
169 switch (kinfo->si_code & __SI_MASK) {
170 case __SI_KILL:
171 err |= __put_user(kinfo->si_pid, &uinfo->pid);
172 err |= __put_user(kinfo->si_uid, &uinfo->uid);
173 break;
174 case __SI_TIMER:
175 err |= __put_user(kinfo->si_tid, &uinfo->tid);
176 err |= __put_user(kinfo->si_overrun, &uinfo->overrun);
177 err |= __put_user((long)kinfo->si_ptr, &uinfo->svptr);
178 break;
179 case __SI_POLL:
180 err |= __put_user(kinfo->si_band, &uinfo->band);
181 err |= __put_user(kinfo->si_fd, &uinfo->fd);
182 break;
183 case __SI_FAULT:
184 err |= __put_user((long)kinfo->si_addr, &uinfo->addr);
185 #ifdef __ARCH_SI_TRAPNO
186 err |= __put_user(kinfo->si_trapno, &uinfo->trapno);
187 #endif
188 break;
189 case __SI_CHLD:
190 err |= __put_user(kinfo->si_pid, &uinfo->pid);
191 err |= __put_user(kinfo->si_uid, &uinfo->uid);
192 err |= __put_user(kinfo->si_status, &uinfo->status);
193 err |= __put_user(kinfo->si_utime, &uinfo->utime);
194 err |= __put_user(kinfo->si_stime, &uinfo->stime);
195 break;
196 case __SI_RT: /* This is not generated by the kernel as of now. */
197 case __SI_MESGQ: /* But this is */
198 err |= __put_user(kinfo->si_pid, &uinfo->pid);
199 err |= __put_user(kinfo->si_uid, &uinfo->uid);
200 err |= __put_user((long)kinfo->si_ptr, &uinfo->svptr);
201 break;
202 default: /* this is just in case for now ... */
203 err |= __put_user(kinfo->si_pid, &uinfo->pid);
204 err |= __put_user(kinfo->si_uid, &uinfo->uid);
205 break;
208 return err ? -EFAULT: sizeof(*uinfo);
211 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
212 int nonblock)
214 ssize_t ret;
215 struct signalfd_lockctx lk;
216 DECLARE_WAITQUEUE(wait, current);
218 if (!signalfd_lock(ctx, &lk))
219 return 0;
221 ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
222 switch (ret) {
223 case 0:
224 if (!nonblock)
225 break;
226 ret = -EAGAIN;
227 default:
228 signalfd_unlock(&lk);
229 return ret;
232 add_wait_queue(&ctx->wqh, &wait);
233 for (;;) {
234 set_current_state(TASK_INTERRUPTIBLE);
235 ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
236 signalfd_unlock(&lk);
237 if (ret != 0)
238 break;
239 if (signal_pending(current)) {
240 ret = -ERESTARTSYS;
241 break;
243 schedule();
244 ret = signalfd_lock(ctx, &lk);
245 if (unlikely(!ret)) {
247 * Let the caller read zero byte, ala socket
248 * recv() when the peer disconnect. This test
249 * must be done before doing a dequeue_signal(),
250 * because if the sighand has been orphaned,
251 * the dequeue_signal() call is going to crash
252 * because ->sighand will be long gone.
254 break;
258 remove_wait_queue(&ctx->wqh, &wait);
259 __set_current_state(TASK_RUNNING);
261 return ret;
265 * Returns either the size of a "struct signalfd_siginfo", or zero if the
266 * sighand we are attached to, has been orphaned. The "count" parameter
267 * must be at least the size of a "struct signalfd_siginfo".
269 static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
270 loff_t *ppos)
272 struct signalfd_ctx *ctx = file->private_data;
273 struct signalfd_siginfo __user *siginfo;
274 int nonblock = file->f_flags & O_NONBLOCK;
275 ssize_t ret, total = 0;
276 siginfo_t info;
278 count /= sizeof(struct signalfd_siginfo);
279 if (!count)
280 return -EINVAL;
282 siginfo = (struct signalfd_siginfo __user *) buf;
284 do {
285 ret = signalfd_dequeue(ctx, &info, nonblock);
286 if (unlikely(ret <= 0))
287 break;
288 ret = signalfd_copyinfo(siginfo, &info);
289 if (ret < 0)
290 break;
291 siginfo++;
292 total += ret;
293 nonblock = 1;
294 } while (--count);
296 return total ? total : ret;
299 static const struct file_operations signalfd_fops = {
300 .release = signalfd_release,
301 .poll = signalfd_poll,
302 .read = signalfd_read,
306 * Create a file descriptor that is associated with our signal
307 * state. We can pass it around to others if we want to, but
308 * it will always be _our_ signal state.
310 asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
312 int error;
313 sigset_t sigmask;
314 struct signalfd_ctx *ctx;
315 struct sighand_struct *sighand;
316 struct file *file;
317 struct inode *inode;
318 struct signalfd_lockctx lk;
320 if (sizemask != sizeof(sigset_t) ||
321 copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
322 return error = -EINVAL;
323 sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
324 signotset(&sigmask);
326 if (ufd == -1) {
327 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
328 if (!ctx)
329 return -ENOMEM;
331 init_waitqueue_head(&ctx->wqh);
332 ctx->sigmask = sigmask;
333 ctx->tsk = current;
335 sighand = current->sighand;
337 * Add this fd to the list of signal listeners.
339 spin_lock_irq(&sighand->siglock);
340 list_add_tail(&ctx->lnk, &sighand->signalfd_list);
341 spin_unlock_irq(&sighand->siglock);
344 * When we call this, the initialization must be complete, since
345 * anon_inode_getfd() will install the fd.
347 error = anon_inode_getfd(&ufd, &inode, &file, "[signalfd]",
348 &signalfd_fops, ctx);
349 if (error)
350 goto err_fdalloc;
351 } else {
352 file = fget(ufd);
353 if (!file)
354 return -EBADF;
355 ctx = file->private_data;
356 if (file->f_op != &signalfd_fops) {
357 fput(file);
358 return -EINVAL;
361 * We need to be prepared of the fact that the sighand this fd
362 * is attached to, has been detched. In that case signalfd_lock()
363 * will return 0, and we'll just skip setting the new mask.
365 if (signalfd_lock(ctx, &lk)) {
366 ctx->sigmask = sigmask;
367 signalfd_unlock(&lk);
369 wake_up(&ctx->wqh);
370 fput(file);
373 return ufd;
375 err_fdalloc:
376 signalfd_cleanup(ctx);
377 return error;