Back to home page

LXR

 
 

    


0001 /*
0002  *  fs/signalfd.c
0003  *
0004  *  Copyright (C) 2003  Linus Torvalds
0005  *
0006  *  Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
0007  *      Changed ->read() to return a siginfo strcture instead of signal number.
0008  *      Fixed locking in ->poll().
0009  *      Added sighand-detach notification.
0010  *      Added fd re-use in sys_signalfd() syscall.
0011  *      Now using anonymous inode source.
0012  *      Thanks to Oleg Nesterov for useful code review and suggestions.
0013  *      More comments and suggestions from Arnd Bergmann.
0014  *  Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
0015  *      Retrieve multiple signals with one read() call
0016  *  Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
0017  *      Attach to the sighand only during read() and poll().
0018  */
0019 
0020 #include <linux/file.h>
0021 #include <linux/poll.h>
0022 #include <linux/init.h>
0023 #include <linux/fs.h>
0024 #include <linux/sched.h>
0025 #include <linux/slab.h>
0026 #include <linux/kernel.h>
0027 #include <linux/signal.h>
0028 #include <linux/list.h>
0029 #include <linux/anon_inodes.h>
0030 #include <linux/signalfd.h>
0031 #include <linux/syscalls.h>
0032 #include <linux/proc_fs.h>
0033 #include <linux/compat.h>
0034 
0035 void signalfd_cleanup(struct sighand_struct *sighand)
0036 {
0037     wait_queue_head_t *wqh = &sighand->signalfd_wqh;
0038     /*
0039      * The lockless check can race with remove_wait_queue() in progress,
0040      * but in this case its caller should run under rcu_read_lock() and
0041      * sighand_cachep is SLAB_DESTROY_BY_RCU, we can safely return.
0042      */
0043     if (likely(!waitqueue_active(wqh)))
0044         return;
0045 
0046     /* wait_queue_t->func(POLLFREE) should do remove_wait_queue() */
0047     wake_up_poll(wqh, POLLHUP | POLLFREE);
0048 }
0049 
0050 struct signalfd_ctx {
0051     sigset_t sigmask;
0052 };
0053 
0054 static int signalfd_release(struct inode *inode, struct file *file)
0055 {
0056     kfree(file->private_data);
0057     return 0;
0058 }
0059 
0060 static unsigned int signalfd_poll(struct file *file, poll_table *wait)
0061 {
0062     struct signalfd_ctx *ctx = file->private_data;
0063     unsigned int events = 0;
0064 
0065     poll_wait(file, &current->sighand->signalfd_wqh, wait);
0066 
0067     spin_lock_irq(&current->sighand->siglock);
0068     if (next_signal(&current->pending, &ctx->sigmask) ||
0069         next_signal(&current->signal->shared_pending,
0070             &ctx->sigmask))
0071         events |= POLLIN;
0072     spin_unlock_irq(&current->sighand->siglock);
0073 
0074     return events;
0075 }
0076 
0077 /*
0078  * Copied from copy_siginfo_to_user() in kernel/signal.c
0079  */
0080 static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
0081                  siginfo_t const *kinfo)
0082 {
0083     long err;
0084 
0085     BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
0086 
0087     /*
0088      * Unused members should be zero ...
0089      */
0090     err = __clear_user(uinfo, sizeof(*uinfo));
0091 
0092     /*
0093      * If you change siginfo_t structure, please be sure
0094      * this code is fixed accordingly.
0095      */
0096     err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
0097     err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
0098     err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code);
0099     switch (kinfo->si_code & __SI_MASK) {
0100     case __SI_KILL:
0101         err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
0102         err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
0103         break;
0104     case __SI_TIMER:
0105          err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
0106          err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
0107          err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
0108          err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
0109         break;
0110     case __SI_POLL:
0111         err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
0112         err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
0113         break;
0114     case __SI_FAULT:
0115         err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
0116 #ifdef __ARCH_SI_TRAPNO
0117         err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
0118 #endif
0119 #ifdef BUS_MCEERR_AO
0120         /* 
0121          * Other callers might not initialize the si_lsb field,
0122          * so check explicitly for the right codes here.
0123          */
0124         if (kinfo->si_signo == SIGBUS &&
0125             (kinfo->si_code == BUS_MCEERR_AR ||
0126              kinfo->si_code == BUS_MCEERR_AO))
0127             err |= __put_user((short) kinfo->si_addr_lsb,
0128                       &uinfo->ssi_addr_lsb);
0129 #endif
0130         break;
0131     case __SI_CHLD:
0132         err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
0133         err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
0134         err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
0135         err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
0136         err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
0137         break;
0138     case __SI_RT: /* This is not generated by the kernel as of now. */
0139     case __SI_MESGQ: /* But this is */
0140         err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
0141         err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
0142         err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
0143         err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
0144         break;
0145     default:
0146         /*
0147          * This case catches also the signals queued by sigqueue().
0148          */
0149         err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
0150         err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
0151         err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
0152         err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
0153         break;
0154     }
0155 
0156     return err ? -EFAULT: sizeof(*uinfo);
0157 }
0158 
0159 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
0160                 int nonblock)
0161 {
0162     ssize_t ret;
0163     DECLARE_WAITQUEUE(wait, current);
0164 
0165     spin_lock_irq(&current->sighand->siglock);
0166     ret = dequeue_signal(current, &ctx->sigmask, info);
0167     switch (ret) {
0168     case 0:
0169         if (!nonblock)
0170             break;
0171         ret = -EAGAIN;
0172     default:
0173         spin_unlock_irq(&current->sighand->siglock);
0174         return ret;
0175     }
0176 
0177     add_wait_queue(&current->sighand->signalfd_wqh, &wait);
0178     for (;;) {
0179         set_current_state(TASK_INTERRUPTIBLE);
0180         ret = dequeue_signal(current, &ctx->sigmask, info);
0181         if (ret != 0)
0182             break;
0183         if (signal_pending(current)) {
0184             ret = -ERESTARTSYS;
0185             break;
0186         }
0187         spin_unlock_irq(&current->sighand->siglock);
0188         schedule();
0189         spin_lock_irq(&current->sighand->siglock);
0190     }
0191     spin_unlock_irq(&current->sighand->siglock);
0192 
0193     remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
0194     __set_current_state(TASK_RUNNING);
0195 
0196     return ret;
0197 }
0198 
0199 /*
0200  * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
0201  * error code. The "count" parameter must be at least the size of a
0202  * "struct signalfd_siginfo".
0203  */
0204 static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
0205                  loff_t *ppos)
0206 {
0207     struct signalfd_ctx *ctx = file->private_data;
0208     struct signalfd_siginfo __user *siginfo;
0209     int nonblock = file->f_flags & O_NONBLOCK;
0210     ssize_t ret, total = 0;
0211     siginfo_t info;
0212 
0213     count /= sizeof(struct signalfd_siginfo);
0214     if (!count)
0215         return -EINVAL;
0216 
0217     siginfo = (struct signalfd_siginfo __user *) buf;
0218     do {
0219         ret = signalfd_dequeue(ctx, &info, nonblock);
0220         if (unlikely(ret <= 0))
0221             break;
0222         ret = signalfd_copyinfo(siginfo, &info);
0223         if (ret < 0)
0224             break;
0225         siginfo++;
0226         total += ret;
0227         nonblock = 1;
0228     } while (--count);
0229 
0230     return total ? total: ret;
0231 }
0232 
0233 #ifdef CONFIG_PROC_FS
0234 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
0235 {
0236     struct signalfd_ctx *ctx = f->private_data;
0237     sigset_t sigmask;
0238 
0239     sigmask = ctx->sigmask;
0240     signotset(&sigmask);
0241     render_sigset_t(m, "sigmask:\t", &sigmask);
0242 }
0243 #endif
0244 
0245 static const struct file_operations signalfd_fops = {
0246 #ifdef CONFIG_PROC_FS
0247     .show_fdinfo    = signalfd_show_fdinfo,
0248 #endif
0249     .release    = signalfd_release,
0250     .poll       = signalfd_poll,
0251     .read       = signalfd_read,
0252     .llseek     = noop_llseek,
0253 };
0254 
0255 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
0256         size_t, sizemask, int, flags)
0257 {
0258     sigset_t sigmask;
0259     struct signalfd_ctx *ctx;
0260 
0261     /* Check the SFD_* constants for consistency.  */
0262     BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
0263     BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
0264 
0265     if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
0266         return -EINVAL;
0267 
0268     if (sizemask != sizeof(sigset_t) ||
0269         copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
0270         return -EINVAL;
0271     sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
0272     signotset(&sigmask);
0273 
0274     if (ufd == -1) {
0275         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
0276         if (!ctx)
0277             return -ENOMEM;
0278 
0279         ctx->sigmask = sigmask;
0280 
0281         /*
0282          * When we call this, the initialization must be complete, since
0283          * anon_inode_getfd() will install the fd.
0284          */
0285         ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
0286                        O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
0287         if (ufd < 0)
0288             kfree(ctx);
0289     } else {
0290         struct fd f = fdget(ufd);
0291         if (!f.file)
0292             return -EBADF;
0293         ctx = f.file->private_data;
0294         if (f.file->f_op != &signalfd_fops) {
0295             fdput(f);
0296             return -EINVAL;
0297         }
0298         spin_lock_irq(&current->sighand->siglock);
0299         ctx->sigmask = sigmask;
0300         spin_unlock_irq(&current->sighand->siglock);
0301 
0302         wake_up(&current->sighand->signalfd_wqh);
0303         fdput(f);
0304     }
0305 
0306     return ufd;
0307 }
0308 
0309 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
0310         size_t, sizemask)
0311 {
0312     return sys_signalfd4(ufd, user_mask, sizemask, 0);
0313 }
0314 
0315 #ifdef CONFIG_COMPAT
0316 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
0317              const compat_sigset_t __user *,sigmask,
0318              compat_size_t, sigsetsize,
0319              int, flags)
0320 {
0321     compat_sigset_t ss32;
0322     sigset_t tmp;
0323     sigset_t __user *ksigmask;
0324 
0325     if (sigsetsize != sizeof(compat_sigset_t))
0326         return -EINVAL;
0327     if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
0328         return -EFAULT;
0329     sigset_from_compat(&tmp, &ss32);
0330     ksigmask = compat_alloc_user_space(sizeof(sigset_t));
0331     if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
0332         return -EFAULT;
0333 
0334     return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
0335 }
0336 
0337 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
0338              const compat_sigset_t __user *,sigmask,
0339              compat_size_t, sigsetsize)
0340 {
0341     return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
0342 }
0343 #endif