Back to home page

OSCL-LXR

 
 

    


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