LCOV - code coverage report
Current view: top level - fs - signalfd.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 83 164 50.6 %
Date: 2023-07-31 20:08:34 Functions: 9 19 47.4 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  *  fs/signalfd.c
       4             :  *
       5             :  *  Copyright (C) 2003  Linus Torvalds
       6             :  *
       7             :  *  Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
       8             :  *      Changed ->read() to return a siginfo strcture instead of signal number.
       9             :  *      Fixed locking in ->poll().
      10             :  *      Added sighand-detach notification.
      11             :  *      Added fd re-use in sys_signalfd() syscall.
      12             :  *      Now using anonymous inode source.
      13             :  *      Thanks to Oleg Nesterov for useful code review and suggestions.
      14             :  *      More comments and suggestions from Arnd Bergmann.
      15             :  *  Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
      16             :  *      Retrieve multiple signals with one read() call
      17             :  *  Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
      18             :  *      Attach to the sighand only during read() and poll().
      19             :  */
      20             : 
      21             : #include <linux/file.h>
      22             : #include <linux/poll.h>
      23             : #include <linux/init.h>
      24             : #include <linux/fs.h>
      25             : #include <linux/sched.h>
      26             : #include <linux/slab.h>
      27             : #include <linux/kernel.h>
      28             : #include <linux/signal.h>
      29             : #include <linux/list.h>
      30             : #include <linux/anon_inodes.h>
      31             : #include <linux/signalfd.h>
      32             : #include <linux/syscalls.h>
      33             : #include <linux/proc_fs.h>
      34             : #include <linux/compat.h>
      35             : 
      36    52533271 : void signalfd_cleanup(struct sighand_struct *sighand)
      37             : {
      38    52533271 :         wake_up_pollfree(&sighand->signalfd_wqh);
      39    52533271 : }
      40             : 
      41             : struct signalfd_ctx {
      42             :         sigset_t sigmask;
      43             : };
      44             : 
      45       84997 : static int signalfd_release(struct inode *inode, struct file *file)
      46             : {
      47       84997 :         kfree(file->private_data);
      48       85855 :         return 0;
      49             : }
      50             : 
      51      464042 : static __poll_t signalfd_poll(struct file *file, poll_table *wait)
      52             : {
      53      464042 :         struct signalfd_ctx *ctx = file->private_data;
      54      464042 :         __poll_t events = 0;
      55             : 
      56      464042 :         poll_wait(file, &current->sighand->signalfd_wqh, wait);
      57             : 
      58      464063 :         spin_lock_irq(&current->sighand->siglock);
      59      928536 :         if (next_signal(&current->pending, &ctx->sigmask) ||
      60      464034 :             next_signal(&current->signal->shared_pending,
      61             :                         &ctx->sigmask))
      62             :                 events |= EPOLLIN;
      63      464291 :         spin_unlock_irq(&current->sighand->siglock);
      64             : 
      65      464117 :         return events;
      66             : }
      67             : 
      68             : /*
      69             :  * Copied from copy_siginfo_to_user() in kernel/signal.c
      70             :  */
      71      217971 : static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
      72             :                              kernel_siginfo_t const *kinfo)
      73             : {
      74      217971 :         struct signalfd_siginfo new;
      75             : 
      76      217971 :         BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
      77             : 
      78             :         /*
      79             :          * Unused members should be zero ...
      80             :          */
      81      217971 :         memset(&new, 0, sizeof(new));
      82             : 
      83             :         /*
      84             :          * If you change siginfo_t structure, please be sure
      85             :          * this code is fixed accordingly.
      86             :          */
      87      217971 :         new.ssi_signo = kinfo->si_signo;
      88      217971 :         new.ssi_errno = kinfo->si_errno;
      89      217971 :         new.ssi_code  = kinfo->si_code;
      90      217971 :         switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
      91       84831 :         case SIL_KILL:
      92       84831 :                 new.ssi_pid = kinfo->si_pid;
      93       84831 :                 new.ssi_uid = kinfo->si_uid;
      94       84831 :                 break;
      95           0 :         case SIL_TIMER:
      96           0 :                 new.ssi_tid = kinfo->si_tid;
      97           0 :                 new.ssi_overrun = kinfo->si_overrun;
      98           0 :                 new.ssi_ptr = (long) kinfo->si_ptr;
      99           0 :                 new.ssi_int = kinfo->si_int;
     100           0 :                 break;
     101           0 :         case SIL_POLL:
     102           0 :                 new.ssi_band = kinfo->si_band;
     103           0 :                 new.ssi_fd   = kinfo->si_fd;
     104           0 :                 break;
     105           0 :         case SIL_FAULT_BNDERR:
     106             :         case SIL_FAULT_PKUERR:
     107             :         case SIL_FAULT_PERF_EVENT:
     108             :                 /*
     109             :                  * Fall through to the SIL_FAULT case.  SIL_FAULT_BNDERR,
     110             :                  * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
     111             :                  * generated by faults that deliver them synchronously to
     112             :                  * userspace.  In case someone injects one of these signals
     113             :                  * and signalfd catches it treat it as SIL_FAULT.
     114             :                  */
     115             :         case SIL_FAULT:
     116           0 :                 new.ssi_addr = (long) kinfo->si_addr;
     117           0 :                 break;
     118           0 :         case SIL_FAULT_TRAPNO:
     119           0 :                 new.ssi_addr = (long) kinfo->si_addr;
     120           0 :                 new.ssi_trapno = kinfo->si_trapno;
     121           0 :                 break;
     122           0 :         case SIL_FAULT_MCEERR:
     123           0 :                 new.ssi_addr = (long) kinfo->si_addr;
     124           0 :                 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
     125           0 :                 break;
     126      132471 :         case SIL_CHLD:
     127      132471 :                 new.ssi_pid    = kinfo->si_pid;
     128      132471 :                 new.ssi_uid    = kinfo->si_uid;
     129      132471 :                 new.ssi_status = kinfo->si_status;
     130      132471 :                 new.ssi_utime  = kinfo->si_utime;
     131      132471 :                 new.ssi_stime  = kinfo->si_stime;
     132      132471 :                 break;
     133           0 :         case SIL_RT:
     134             :                 /*
     135             :                  * This case catches also the signals queued by sigqueue().
     136             :                  */
     137           0 :                 new.ssi_pid = kinfo->si_pid;
     138           0 :                 new.ssi_uid = kinfo->si_uid;
     139           0 :                 new.ssi_ptr = (long) kinfo->si_ptr;
     140           0 :                 new.ssi_int = kinfo->si_int;
     141           0 :                 break;
     142           0 :         case SIL_SYS:
     143           0 :                 new.ssi_call_addr = (long) kinfo->si_call_addr;
     144           0 :                 new.ssi_syscall   = kinfo->si_syscall;
     145           0 :                 new.ssi_arch      = kinfo->si_arch;
     146           0 :                 break;
     147             :         }
     148             : 
     149      217302 :         if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
     150           0 :                 return -EFAULT;
     151             : 
     152             :         return sizeof(*uinfo);
     153             : }
     154             : 
     155      217812 : static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
     156             :                                 int nonblock)
     157             : {
     158      217812 :         enum pid_type type;
     159      217812 :         ssize_t ret;
     160      217812 :         DECLARE_WAITQUEUE(wait, current);
     161             : 
     162      217812 :         spin_lock_irq(&current->sighand->siglock);
     163      218263 :         ret = dequeue_signal(current, &ctx->sigmask, info, &type);
     164      218180 :         switch (ret) {
     165           0 :         case 0:
     166           0 :                 if (!nonblock)
     167             :                         break;
     168             :                 ret = -EAGAIN;
     169      218180 :                 fallthrough;
     170      218180 :         default:
     171      218180 :                 spin_unlock_irq(&current->sighand->siglock);
     172      218180 :                 return ret;
     173             :         }
     174             : 
     175           0 :         add_wait_queue(&current->sighand->signalfd_wqh, &wait);
     176           0 :         for (;;) {
     177           0 :                 set_current_state(TASK_INTERRUPTIBLE);
     178           0 :                 ret = dequeue_signal(current, &ctx->sigmask, info, &type);
     179           0 :                 if (ret != 0)
     180             :                         break;
     181           0 :                 if (signal_pending(current)) {
     182             :                         ret = -ERESTARTSYS;
     183             :                         break;
     184             :                 }
     185           0 :                 spin_unlock_irq(&current->sighand->siglock);
     186           0 :                 schedule();
     187           0 :                 spin_lock_irq(&current->sighand->siglock);
     188             :         }
     189           0 :         spin_unlock_irq(&current->sighand->siglock);
     190             : 
     191           0 :         remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
     192           0 :         __set_current_state(TASK_RUNNING);
     193             : 
     194           0 :         return ret;
     195             : }
     196             : 
     197             : /*
     198             :  * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
     199             :  * error code. The "count" parameter must be at least the size of a
     200             :  * "struct signalfd_siginfo".
     201             :  */
     202      218075 : static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
     203             :                              loff_t *ppos)
     204             : {
     205      218075 :         struct signalfd_ctx *ctx = file->private_data;
     206      218075 :         struct signalfd_siginfo __user *siginfo;
     207      218075 :         int nonblock = file->f_flags & O_NONBLOCK;
     208      218075 :         ssize_t ret, total = 0;
     209      218075 :         kernel_siginfo_t info;
     210             : 
     211      218075 :         count /= sizeof(struct signalfd_siginfo);
     212      218075 :         if (!count)
     213             :                 return -EINVAL;
     214             : 
     215             :         siginfo = (struct signalfd_siginfo __user *) buf;
     216      218217 :         do {
     217      218217 :                 ret = signalfd_dequeue(ctx, &info, nonblock);
     218      218015 :                 if (unlikely(ret <= 0))
     219             :                         break;
     220      218084 :                 ret = signalfd_copyinfo(siginfo, &info);
     221      217823 :                 if (ret < 0)
     222             :                         break;
     223      217823 :                 siginfo++;
     224      217823 :                 total += ret;
     225      217823 :                 nonblock = 1;
     226      217823 :         } while (--count);
     227             : 
     228      217612 :         return total ? total: ret;
     229             : }
     230             : 
     231             : #ifdef CONFIG_PROC_FS
     232           0 : static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
     233             : {
     234           0 :         struct signalfd_ctx *ctx = f->private_data;
     235           0 :         sigset_t sigmask;
     236             : 
     237           0 :         sigmask = ctx->sigmask;
     238           0 :         signotset(&sigmask);
     239           0 :         render_sigset_t(m, "sigmask:\t", &sigmask);
     240           0 : }
     241             : #endif
     242             : 
     243             : static const struct file_operations signalfd_fops = {
     244             : #ifdef CONFIG_PROC_FS
     245             :         .show_fdinfo    = signalfd_show_fdinfo,
     246             : #endif
     247             :         .release        = signalfd_release,
     248             :         .poll           = signalfd_poll,
     249             :         .read           = signalfd_read,
     250             :         .llseek         = noop_llseek,
     251             : };
     252             : 
     253       87466 : static int do_signalfd4(int ufd, sigset_t *mask, int flags)
     254             : {
     255       87466 :         struct signalfd_ctx *ctx;
     256             : 
     257             :         /* Check the SFD_* constants for consistency.  */
     258       87466 :         BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
     259       87466 :         BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
     260             : 
     261       87466 :         if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
     262             :                 return -EINVAL;
     263             : 
     264       87466 :         sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
     265       87466 :         signotset(mask);
     266             : 
     267       87466 :         if (ufd == -1) {
     268       87466 :                 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
     269       87562 :                 if (!ctx)
     270             :                         return -ENOMEM;
     271             : 
     272       87562 :                 ctx->sigmask = *mask;
     273             : 
     274             :                 /*
     275             :                  * When we call this, the initialization must be complete, since
     276             :                  * anon_inode_getfd() will install the fd.
     277             :                  */
     278       87562 :                 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
     279       87562 :                                        O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
     280       87569 :                 if (ufd < 0)
     281           0 :                         kfree(ctx);
     282             :         } else {
     283           0 :                 struct fd f = fdget(ufd);
     284           0 :                 if (!f.file)
     285             :                         return -EBADF;
     286           0 :                 ctx = f.file->private_data;
     287           0 :                 if (f.file->f_op != &signalfd_fops) {
     288           0 :                         fdput(f);
     289           0 :                         return -EINVAL;
     290             :                 }
     291           0 :                 spin_lock_irq(&current->sighand->siglock);
     292           0 :                 ctx->sigmask = *mask;
     293           0 :                 spin_unlock_irq(&current->sighand->siglock);
     294             : 
     295           0 :                 wake_up(&current->sighand->signalfd_wqh);
     296           0 :                 fdput(f);
     297             :         }
     298             : 
     299             :         return ufd;
     300             : }
     301             : 
     302      174997 : SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
     303             :                 size_t, sizemask, int, flags)
     304             : {
     305       87481 :         sigset_t mask;
     306             : 
     307       87481 :         if (sizemask != sizeof(sigset_t))
     308             :                 return -EINVAL;
     309       87481 :         if (copy_from_user(&mask, user_mask, sizeof(mask)))
     310             :                 return -EFAULT;
     311       87459 :         return do_signalfd4(ufd, &mask, flags);
     312             : }
     313             : 
     314           0 : SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
     315             :                 size_t, sizemask)
     316             : {
     317           0 :         sigset_t mask;
     318             : 
     319           0 :         if (sizemask != sizeof(sigset_t))
     320             :                 return -EINVAL;
     321           0 :         if (copy_from_user(&mask, user_mask, sizeof(mask)))
     322             :                 return -EFAULT;
     323           0 :         return do_signalfd4(ufd, &mask, 0);
     324             : }
     325             : 
     326             : #ifdef CONFIG_COMPAT
     327           0 : static long do_compat_signalfd4(int ufd,
     328             :                         const compat_sigset_t __user *user_mask,
     329             :                         compat_size_t sigsetsize, int flags)
     330             : {
     331           0 :         sigset_t mask;
     332             : 
     333           0 :         if (sigsetsize != sizeof(compat_sigset_t))
     334             :                 return -EINVAL;
     335           0 :         if (get_compat_sigset(&mask, user_mask))
     336             :                 return -EFAULT;
     337           0 :         return do_signalfd4(ufd, &mask, flags);
     338             : }
     339             : 
     340           0 : COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
     341             :                      const compat_sigset_t __user *, user_mask,
     342             :                      compat_size_t, sigsetsize,
     343             :                      int, flags)
     344             : {
     345           0 :         return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
     346             : }
     347             : 
     348           0 : COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
     349             :                      const compat_sigset_t __user *, user_mask,
     350             :                      compat_size_t, sigsetsize)
     351             : {
     352           0 :         return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
     353             : }
     354             : #endif

Generated by: LCOV version 1.14