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.
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/list.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/signalfd.h>
29 wait_queue_head_t wqh
;
31 struct task_struct
*tsk
;
34 struct signalfd_lockctx
{
35 struct task_struct
*tsk
;
40 * Tries to acquire the sighand lock. We do not increment the sighand
41 * use count, and we do not even pin the task struct, so we need to
42 * do it inside an RCU read lock, and we must be prepared for the
43 * ctx->tsk going to NULL (in signalfd_deliver()), and for the sighand
44 * being detached. We return 0 if the sighand has been detached, or
45 * 1 if we were able to pin the sighand lock.
47 static int signalfd_lock(struct signalfd_ctx
*ctx
, struct signalfd_lockctx
*lk
)
49 struct sighand_struct
*sighand
= NULL
;
52 lk
->tsk
= rcu_dereference(ctx
->tsk
);
53 if (likely(lk
->tsk
!= NULL
))
54 sighand
= lock_task_sighand(lk
->tsk
, &lk
->flags
);
57 if (sighand
&& !ctx
->tsk
) {
58 unlock_task_sighand(lk
->tsk
, &lk
->flags
);
62 return sighand
!= NULL
;
65 static void signalfd_unlock(struct signalfd_lockctx
*lk
)
67 unlock_task_sighand(lk
->tsk
, &lk
->flags
);
71 * This must be called with the sighand lock held.
73 void signalfd_deliver(struct task_struct
*tsk
, int sig
)
75 struct sighand_struct
*sighand
= tsk
->sighand
;
76 struct signalfd_ctx
*ctx
, *tmp
;
79 list_for_each_entry_safe(ctx
, tmp
, &sighand
->signalfd_list
, lnk
) {
81 * We use a negative signal value as a way to broadcast that the
82 * sighand has been orphaned, so that we can notify all the
83 * listeners about this. Remember the ctx->sigmask is inverted,
84 * so if the user is interested in a signal, that corresponding
88 if (ctx
->tsk
== tsk
) {
90 list_del_init(&ctx
->lnk
);
94 if (!sigismember(&ctx
->sigmask
, sig
))
100 static void signalfd_cleanup(struct signalfd_ctx
*ctx
)
102 struct signalfd_lockctx lk
;
105 * This is tricky. If the sighand is gone, we do not need to remove
106 * context from the list, the list itself won't be there anymore.
108 if (signalfd_lock(ctx
, &lk
)) {
110 signalfd_unlock(&lk
);
115 static int signalfd_release(struct inode
*inode
, struct file
*file
)
117 signalfd_cleanup(file
->private_data
);
121 static unsigned int signalfd_poll(struct file
*file
, poll_table
*wait
)
123 struct signalfd_ctx
*ctx
= file
->private_data
;
124 unsigned int events
= 0;
125 struct signalfd_lockctx lk
;
127 poll_wait(file
, &ctx
->wqh
, wait
);
130 * Let the caller get a POLLIN in this case, ala socket recv() when
131 * the peer disconnects.
133 if (signalfd_lock(ctx
, &lk
)) {
134 if (next_signal(&lk
.tsk
->pending
, &ctx
->sigmask
) > 0 ||
135 next_signal(&lk
.tsk
->signal
->shared_pending
,
138 signalfd_unlock(&lk
);
146 * Copied from copy_siginfo_to_user() in kernel/signal.c
148 static int signalfd_copyinfo(struct signalfd_siginfo __user
*uinfo
,
149 siginfo_t
const *kinfo
)
153 BUILD_BUG_ON(sizeof(struct signalfd_siginfo
) != 128);
156 * Unused memebers should be zero ...
158 err
= __clear_user(uinfo
, sizeof(*uinfo
));
161 * If you change siginfo_t structure, please be sure
162 * this code is fixed accordingly.
164 err
|= __put_user(kinfo
->si_signo
, &uinfo
->signo
);
165 err
|= __put_user(kinfo
->si_errno
, &uinfo
->err
);
166 err
|= __put_user((short)kinfo
->si_code
, &uinfo
->code
);
167 switch (kinfo
->si_code
& __SI_MASK
) {
169 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
170 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
173 err
|= __put_user(kinfo
->si_tid
, &uinfo
->tid
);
174 err
|= __put_user(kinfo
->si_overrun
, &uinfo
->overrun
);
175 err
|= __put_user((long)kinfo
->si_ptr
, &uinfo
->svptr
);
178 err
|= __put_user(kinfo
->si_band
, &uinfo
->band
);
179 err
|= __put_user(kinfo
->si_fd
, &uinfo
->fd
);
182 err
|= __put_user((long)kinfo
->si_addr
, &uinfo
->addr
);
183 #ifdef __ARCH_SI_TRAPNO
184 err
|= __put_user(kinfo
->si_trapno
, &uinfo
->trapno
);
188 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
189 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
190 err
|= __put_user(kinfo
->si_status
, &uinfo
->status
);
191 err
|= __put_user(kinfo
->si_utime
, &uinfo
->utime
);
192 err
|= __put_user(kinfo
->si_stime
, &uinfo
->stime
);
194 case __SI_RT
: /* This is not generated by the kernel as of now. */
195 case __SI_MESGQ
: /* But this is */
196 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
197 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
198 err
|= __put_user((long)kinfo
->si_ptr
, &uinfo
->svptr
);
200 default: /* this is just in case for now ... */
201 err
|= __put_user(kinfo
->si_pid
, &uinfo
->pid
);
202 err
|= __put_user(kinfo
->si_uid
, &uinfo
->uid
);
206 return err
? -EFAULT
: sizeof(*uinfo
);
210 * Returns either the size of a "struct signalfd_siginfo", or zero if the
211 * sighand we are attached to, has been orphaned. The "count" parameter
212 * must be at least the size of a "struct signalfd_siginfo".
214 static ssize_t
signalfd_read(struct file
*file
, char __user
*buf
, size_t count
,
217 struct signalfd_ctx
*ctx
= file
->private_data
;
221 struct signalfd_lockctx lk
;
222 DECLARE_WAITQUEUE(wait
, current
);
224 if (count
< sizeof(struct signalfd_siginfo
))
226 locked
= signalfd_lock(ctx
, &lk
);
230 signo
= dequeue_signal(lk
.tsk
, &ctx
->sigmask
, &info
);
231 if (signo
== 0 && !(file
->f_flags
& O_NONBLOCK
)) {
232 add_wait_queue(&ctx
->wqh
, &wait
);
234 set_current_state(TASK_INTERRUPTIBLE
);
235 signo
= dequeue_signal(lk
.tsk
, &ctx
->sigmask
, &info
);
238 if (signal_pending(current
)) {
242 signalfd_unlock(&lk
);
244 locked
= signalfd_lock(ctx
, &lk
);
245 if (unlikely(!locked
)) {
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.
257 remove_wait_queue(&ctx
->wqh
, &wait
);
258 __set_current_state(TASK_RUNNING
);
261 signalfd_unlock(&lk
);
263 res
= signalfd_copyinfo((struct signalfd_siginfo __user
*) buf
,
269 static const struct file_operations signalfd_fops
= {
270 .release
= signalfd_release
,
271 .poll
= signalfd_poll
,
272 .read
= signalfd_read
,
276 * Create a file descriptor that is associated with our signal
277 * state. We can pass it around to others if we want to, but
278 * it will always be _our_ signal state.
280 asmlinkage
long sys_signalfd(int ufd
, sigset_t __user
*user_mask
, size_t sizemask
)
284 struct signalfd_ctx
*ctx
;
285 struct sighand_struct
*sighand
;
288 struct signalfd_lockctx lk
;
290 if (sizemask
!= sizeof(sigset_t
) ||
291 copy_from_user(&sigmask
, user_mask
, sizeof(sigmask
)))
292 return error
= -EINVAL
;
293 sigdelsetmask(&sigmask
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
297 ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
);
301 init_waitqueue_head(&ctx
->wqh
);
302 ctx
->sigmask
= sigmask
;
305 sighand
= current
->sighand
;
307 * Add this fd to the list of signal listeners.
309 spin_lock_irq(&sighand
->siglock
);
310 list_add_tail(&ctx
->lnk
, &sighand
->signalfd_list
);
311 spin_unlock_irq(&sighand
->siglock
);
314 * When we call this, the initialization must be complete, since
315 * anon_inode_getfd() will install the fd.
317 error
= anon_inode_getfd(&ufd
, &inode
, &file
, "[signalfd]",
318 &signalfd_fops
, ctx
);
325 ctx
= file
->private_data
;
326 if (file
->f_op
!= &signalfd_fops
) {
331 * We need to be prepared of the fact that the sighand this fd
332 * is attached to, has been detched. In that case signalfd_lock()
333 * will return 0, and we'll just skip setting the new mask.
335 if (signalfd_lock(ctx
, &lk
)) {
336 ctx
->sigmask
= sigmask
;
337 signalfd_unlock(&lk
);
346 signalfd_cleanup(ctx
);