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>
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>
31 wait_queue_head_t wqh
;
33 struct task_struct
*tsk
;
36 struct signalfd_lockctx
{
37 struct task_struct
*tsk
;
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
;
54 lk
->tsk
= rcu_dereference(ctx
->tsk
);
55 if (likely(lk
->tsk
!= NULL
))
56 sighand
= lock_task_sighand(lk
->tsk
, &lk
->flags
);
63 unlock_task_sighand(lk
->tsk
, &lk
->flags
);
67 if (lk
->tsk
->tgid
== current
->tgid
)
73 static void signalfd_unlock(struct signalfd_lockctx
*lk
)
75 unlock_task_sighand(lk
->tsk
, &lk
->flags
);
79 * This must be called with the sighand lock held.
81 void signalfd_deliver(struct task_struct
*tsk
, int sig
)
83 struct sighand_struct
*sighand
= tsk
->sighand
;
84 struct signalfd_ctx
*ctx
, *tmp
;
87 list_for_each_entry_safe(ctx
, tmp
, &sighand
->signalfd_list
, lnk
) {
89 * We use a negative signal value as a way to broadcast that the
90 * sighand has been orphaned, so that we can notify all the
91 * listeners about this. Remember the ctx->sigmask is inverted,
92 * so if the user is interested in a signal, that corresponding
96 if (ctx
->tsk
== tsk
) {
98 list_del_init(&ctx
->lnk
);
102 if (!sigismember(&ctx
->sigmask
, sig
))
108 static void signalfd_cleanup(struct signalfd_ctx
*ctx
)
110 struct signalfd_lockctx lk
;
113 * This is tricky. If the sighand is gone, we do not need to remove
114 * context from the list, the list itself won't be there anymore.
116 if (signalfd_lock(ctx
, &lk
)) {
118 signalfd_unlock(&lk
);
123 static int signalfd_release(struct inode
*inode
, struct file
*file
)
125 signalfd_cleanup(file
->private_data
);
129 static unsigned int signalfd_poll(struct file
*file
, poll_table
*wait
)
131 struct signalfd_ctx
*ctx
= file
->private_data
;
132 unsigned int events
= 0;
133 struct signalfd_lockctx lk
;
135 poll_wait(file
, &ctx
->wqh
, wait
);
138 * Let the caller get a POLLIN in this case, ala socket recv() when
139 * the peer disconnects.
141 if (signalfd_lock(ctx
, &lk
)) {
142 if ((lk
.tsk
== current
&&
143 next_signal(&lk
.tsk
->pending
, &ctx
->sigmask
) > 0) ||
144 next_signal(&lk
.tsk
->signal
->shared_pending
,
147 signalfd_unlock(&lk
);
155 * Copied from copy_siginfo_to_user() in kernel/signal.c
157 static int signalfd_copyinfo(struct signalfd_siginfo __user
*uinfo
,
158 siginfo_t
const *kinfo
)
162 BUILD_BUG_ON(sizeof(struct signalfd_siginfo
) != 128);
165 * Unused memebers should be zero ...
167 err
= __clear_user(uinfo
, sizeof(*uinfo
));
170 * If you change siginfo_t structure, please be sure
171 * this code is fixed accordingly.
173 err
|= __put_user(kinfo
->si_signo
, &uinfo
->signo
);
174 err
|= __put_user(kinfo
->si_errno
, &uinfo
->err
);
175 err
|= __put_user((short)kinfo
->si_code
, &uinfo
->code
);
176 switch (kinfo
->si_code
& __SI_MASK
) {
178 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
179 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
182 err
|= __put_user(kinfo
->si_tid
, &uinfo
->tid
);
183 err
|= __put_user(kinfo
->si_overrun
, &uinfo
->overrun
);
184 err
|= __put_user((long)kinfo
->si_ptr
, &uinfo
->svptr
);
187 err
|= __put_user(kinfo
->si_band
, &uinfo
->band
);
188 err
|= __put_user(kinfo
->si_fd
, &uinfo
->fd
);
191 err
|= __put_user((long)kinfo
->si_addr
, &uinfo
->addr
);
192 #ifdef __ARCH_SI_TRAPNO
193 err
|= __put_user(kinfo
->si_trapno
, &uinfo
->trapno
);
197 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
198 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
199 err
|= __put_user(kinfo
->si_status
, &uinfo
->status
);
200 err
|= __put_user(kinfo
->si_utime
, &uinfo
->utime
);
201 err
|= __put_user(kinfo
->si_stime
, &uinfo
->stime
);
203 case __SI_RT
: /* This is not generated by the kernel as of now. */
204 case __SI_MESGQ
: /* But this is */
205 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
206 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
207 err
|= __put_user((long)kinfo
->si_ptr
, &uinfo
->svptr
);
209 default: /* this is just in case for now ... */
210 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
211 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
215 return err
? -EFAULT
: sizeof(*uinfo
);
218 static ssize_t
signalfd_dequeue(struct signalfd_ctx
*ctx
, siginfo_t
*info
,
222 struct signalfd_lockctx lk
;
223 DECLARE_WAITQUEUE(wait
, current
);
225 if (!signalfd_lock(ctx
, &lk
))
228 ret
= dequeue_signal(lk
.tsk
, &ctx
->sigmask
, info
);
235 signalfd_unlock(&lk
);
239 add_wait_queue(&ctx
->wqh
, &wait
);
241 set_current_state(TASK_INTERRUPTIBLE
);
242 ret
= dequeue_signal(lk
.tsk
, &ctx
->sigmask
, info
);
243 signalfd_unlock(&lk
);
246 if (signal_pending(current
)) {
251 ret
= signalfd_lock(ctx
, &lk
);
252 if (unlikely(!ret
)) {
254 * Let the caller read zero byte, ala socket
255 * recv() when the peer disconnect. This test
256 * must be done before doing a dequeue_signal(),
257 * because if the sighand has been orphaned,
258 * the dequeue_signal() call is going to crash
259 * because ->sighand will be long gone.
265 remove_wait_queue(&ctx
->wqh
, &wait
);
266 __set_current_state(TASK_RUNNING
);
272 * Returns either the size of a "struct signalfd_siginfo", or zero if the
273 * sighand we are attached to, has been orphaned. The "count" parameter
274 * must be at least the size of a "struct signalfd_siginfo".
276 static ssize_t
signalfd_read(struct file
*file
, char __user
*buf
, size_t count
,
279 struct signalfd_ctx
*ctx
= file
->private_data
;
280 struct signalfd_siginfo __user
*siginfo
;
281 int nonblock
= file
->f_flags
& O_NONBLOCK
;
282 ssize_t ret
, total
= 0;
285 count
/= sizeof(struct signalfd_siginfo
);
289 siginfo
= (struct signalfd_siginfo __user
*) buf
;
292 ret
= signalfd_dequeue(ctx
, &info
, nonblock
);
293 if (unlikely(ret
<= 0))
295 ret
= signalfd_copyinfo(siginfo
, &info
);
303 return total
? total
: ret
;
306 static const struct file_operations signalfd_fops
= {
307 .release
= signalfd_release
,
308 .poll
= signalfd_poll
,
309 .read
= signalfd_read
,
313 * Create a file descriptor that is associated with our signal
314 * state. We can pass it around to others if we want to, but
315 * it will always be _our_ signal state.
317 asmlinkage
long sys_signalfd(int ufd
, sigset_t __user
*user_mask
, size_t sizemask
)
321 struct signalfd_ctx
*ctx
;
322 struct sighand_struct
*sighand
;
325 struct signalfd_lockctx lk
;
327 if (sizemask
!= sizeof(sigset_t
) ||
328 copy_from_user(&sigmask
, user_mask
, sizeof(sigmask
)))
330 sigdelsetmask(&sigmask
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
334 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
338 init_waitqueue_head(&ctx
->wqh
);
339 ctx
->sigmask
= sigmask
;
340 ctx
->tsk
= current
->group_leader
;
342 sighand
= current
->sighand
;
344 * Add this fd to the list of signal listeners.
346 spin_lock_irq(&sighand
->siglock
);
347 list_add_tail(&ctx
->lnk
, &sighand
->signalfd_list
);
348 spin_unlock_irq(&sighand
->siglock
);
351 * When we call this, the initialization must be complete, since
352 * anon_inode_getfd() will install the fd.
354 error
= anon_inode_getfd(&ufd
, &inode
, &file
, "[signalfd]",
355 &signalfd_fops
, ctx
);
362 ctx
= file
->private_data
;
363 if (file
->f_op
!= &signalfd_fops
) {
368 * We need to be prepared of the fact that the sighand this fd
369 * is attached to, has been detched. In that case signalfd_lock()
370 * will return 0, and we'll just skip setting the new mask.
372 if (signalfd_lock(ctx
, &lk
)) {
373 ctx
->sigmask
= sigmask
;
374 signalfd_unlock(&lk
);
383 signalfd_cleanup(ctx
);