signalfd.c 8.98 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  fs/signalfd.c
 *
 *  Copyright (C) 2003  Linus Torvalds
 *
 *  Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
 *      Changed ->read() to return a siginfo strcture instead of signal number.
 *      Fixed locking in ->poll().
 *      Added sighand-detach notification.
 *      Added fd re-use in sys_signalfd() syscall.
 *      Now using anonymous inode source.
 *      Thanks to Oleg Nesterov for useful code review and suggestions.
 *      More comments and suggestions from Arnd Bergmann.
Davide Libenzi's avatar
Davide Libenzi committed
15
 *  Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
16
 *      Retrieve multiple signals with one read() call
Davide Libenzi's avatar
Davide Libenzi committed
17 18
 *  Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
 *      Attach to the sighand only during read() and poll().
19 20 21 22 23 24 25
 */

#include <linux/file.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
26
#include <linux/slab.h>
27 28 29 30 31
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/list.h>
#include <linux/anon_inodes.h>
#include <linux/signalfd.h>
32
#include <linux/syscalls.h>
33
#include <linux/proc_fs.h>
34
#include <linux/compat.h>
35

36 37 38
void signalfd_cleanup(struct sighand_struct *sighand)
{
	wait_queue_head_t *wqh = &sighand->signalfd_wqh;
39 40 41
	/*
	 * The lockless check can race with remove_wait_queue() in progress,
	 * but in this case its caller should run under rcu_read_lock() and
42
	 * sighand_cachep is SLAB_TYPESAFE_BY_RCU, we can safely return.
43
	 */
44 45 46
	if (likely(!waitqueue_active(wqh)))
		return;

47
	/* wait_queue_entry_t->func(POLLFREE) should do remove_wait_queue() */
48
	wake_up_poll(wqh, EPOLLHUP | POLLFREE);
49 50
}

51 52 53 54 55 56
struct signalfd_ctx {
	sigset_t sigmask;
};

static int signalfd_release(struct inode *inode, struct file *file)
{
Davide Libenzi's avatar
Davide Libenzi committed
57
	kfree(file->private_data);
58 59 60
	return 0;
}

61
static __poll_t signalfd_poll(struct file *file, poll_table *wait)
62 63
{
	struct signalfd_ctx *ctx = file->private_data;
64
	__poll_t events = 0;
65

Davide Libenzi's avatar
Davide Libenzi committed
66
	poll_wait(file, &current->sighand->signalfd_wqh, wait);
67

Davide Libenzi's avatar
Davide Libenzi committed
68 69 70 71
	spin_lock_irq(&current->sighand->siglock);
	if (next_signal(&current->pending, &ctx->sigmask) ||
	    next_signal(&current->signal->shared_pending,
			&ctx->sigmask))
72
		events |= EPOLLIN;
Davide Libenzi's avatar
Davide Libenzi committed
73
	spin_unlock_irq(&current->sighand->siglock);
74 75 76 77 78 79 80 81

	return events;
}

/*
 * Copied from copy_siginfo_to_user() in kernel/signal.c
 */
static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
82
			     kernel_siginfo_t const *kinfo)
83
{
84
	struct signalfd_siginfo new;
85 86 87 88

	BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);

	/*
89
	 * Unused members should be zero ...
90
	 */
91
	memset(&new, 0, sizeof(new));
92 93 94 95 96

	/*
	 * If you change siginfo_t structure, please be sure
	 * this code is fixed accordingly.
	 */
97 98 99
	new.ssi_signo = kinfo->si_signo;
	new.ssi_errno = kinfo->si_errno;
	new.ssi_code  = kinfo->si_code;
100 101
	switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
	case SIL_KILL:
102 103
		new.ssi_pid = kinfo->si_pid;
		new.ssi_uid = kinfo->si_uid;
104
		break;
105
	case SIL_TIMER:
106 107 108 109
		new.ssi_tid = kinfo->si_tid;
		new.ssi_overrun = kinfo->si_overrun;
		new.ssi_ptr = (long) kinfo->si_ptr;
		new.ssi_int = kinfo->si_int;
110
		break;
111
	case SIL_POLL:
112 113
		new.ssi_band = kinfo->si_band;
		new.ssi_fd   = kinfo->si_fd;
114
		break;
115 116 117 118 119 120 121 122 123
	case SIL_FAULT_BNDERR:
	case SIL_FAULT_PKUERR:
		/*
		 * Fall through to the SIL_FAULT case.  Both SIL_FAULT_BNDERR
		 * and SIL_FAULT_PKUERR are only generated by faults that
		 * deliver them synchronously to userspace.  In case someone
		 * injects one of these signals and signalfd catches it treat
		 * it as SIL_FAULT.
		 */
124
	case SIL_FAULT:
125
		new.ssi_addr = (long) kinfo->si_addr;
126
#ifdef __ARCH_SI_TRAPNO
127
		new.ssi_trapno = kinfo->si_trapno;
128
#endif
129 130 131 132 133 134 135
		break;
	case SIL_FAULT_MCEERR:
		new.ssi_addr = (long) kinfo->si_addr;
#ifdef __ARCH_SI_TRAPNO
		new.ssi_trapno = kinfo->si_trapno;
#endif
		new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
136
		break;
137
	case SIL_CHLD:
138 139 140 141 142
		new.ssi_pid    = kinfo->si_pid;
		new.ssi_uid    = kinfo->si_uid;
		new.ssi_status = kinfo->si_status;
		new.ssi_utime  = kinfo->si_utime;
		new.ssi_stime  = kinfo->si_stime;
143
		break;
144
	case SIL_RT:
145 146 147
		/*
		 * This case catches also the signals queued by sigqueue().
		 */
148 149 150 151
		new.ssi_pid = kinfo->si_pid;
		new.ssi_uid = kinfo->si_uid;
		new.ssi_ptr = (long) kinfo->si_ptr;
		new.ssi_int = kinfo->si_int;
152
		break;
153 154 155 156 157
	case SIL_SYS:
		new.ssi_call_addr = (long) kinfo->si_call_addr;
		new.ssi_syscall   = kinfo->si_syscall;
		new.ssi_arch      = kinfo->si_arch;
		break;
158 159
	}

160 161 162 163
	if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
		return -EFAULT;

	return sizeof(*uinfo);
164 165
}

166
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
167 168 169 170 171
				int nonblock)
{
	ssize_t ret;
	DECLARE_WAITQUEUE(wait, current);

Davide Libenzi's avatar
Davide Libenzi committed
172 173
	spin_lock_irq(&current->sighand->siglock);
	ret = dequeue_signal(current, &ctx->sigmask, info);
174 175 176 177 178 179
	switch (ret) {
	case 0:
		if (!nonblock)
			break;
		ret = -EAGAIN;
	default:
Davide Libenzi's avatar
Davide Libenzi committed
180
		spin_unlock_irq(&current->sighand->siglock);
181 182 183
		return ret;
	}

Davide Libenzi's avatar
Davide Libenzi committed
184
	add_wait_queue(&current->sighand->signalfd_wqh, &wait);
185 186
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
Davide Libenzi's avatar
Davide Libenzi committed
187
		ret = dequeue_signal(current, &ctx->sigmask, info);
188 189 190 191 192 193
		if (ret != 0)
			break;
		if (signal_pending(current)) {
			ret = -ERESTARTSYS;
			break;
		}
Davide Libenzi's avatar
Davide Libenzi committed
194
		spin_unlock_irq(&current->sighand->siglock);
195
		schedule();
Davide Libenzi's avatar
Davide Libenzi committed
196
		spin_lock_irq(&current->sighand->siglock);
197
	}
Davide Libenzi's avatar
Davide Libenzi committed
198
	spin_unlock_irq(&current->sighand->siglock);
199

Davide Libenzi's avatar
Davide Libenzi committed
200
	remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
201 202 203 204 205
	__set_current_state(TASK_RUNNING);

	return ret;
}

206
/*
Davide Libenzi's avatar
Davide Libenzi committed
207 208 209
 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
 * error code. The "count" parameter must be at least the size of a
 * "struct signalfd_siginfo".
210 211 212 213 214
 */
static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
			     loff_t *ppos)
{
	struct signalfd_ctx *ctx = file->private_data;
215 216 217
	struct signalfd_siginfo __user *siginfo;
	int nonblock = file->f_flags & O_NONBLOCK;
	ssize_t ret, total = 0;
218
	kernel_siginfo_t info;
219

220 221
	count /= sizeof(struct signalfd_siginfo);
	if (!count)
222 223
		return -EINVAL;

224 225 226 227 228 229 230 231 232 233 234 235 236
	siginfo = (struct signalfd_siginfo __user *) buf;
	do {
		ret = signalfd_dequeue(ctx, &info, nonblock);
		if (unlikely(ret <= 0))
			break;
		ret = signalfd_copyinfo(siginfo, &info);
		if (ret < 0)
			break;
		siginfo++;
		total += ret;
		nonblock = 1;
	} while (--count);

Davide Libenzi's avatar
Davide Libenzi committed
237
	return total ? total: ret;
238 239
}

240
#ifdef CONFIG_PROC_FS
241
static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
242 243 244 245 246 247 248 249 250 251
{
	struct signalfd_ctx *ctx = f->private_data;
	sigset_t sigmask;

	sigmask = ctx->sigmask;
	signotset(&sigmask);
	render_sigset_t(m, "sigmask:\t", &sigmask);
}
#endif

252
static const struct file_operations signalfd_fops = {
253 254 255
#ifdef CONFIG_PROC_FS
	.show_fdinfo	= signalfd_show_fdinfo,
#endif
256 257 258
	.release	= signalfd_release,
	.poll		= signalfd_poll,
	.read		= signalfd_read,
259
	.llseek		= noop_llseek,
260 261
};

262
static int do_signalfd4(int ufd, sigset_t *mask, int flags)
263 264 265
{
	struct signalfd_ctx *ctx;

266 267 268 269
	/* Check the SFD_* constants for consistency.  */
	BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
	BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);

270
	if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
271 272
		return -EINVAL;

273 274
	sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
	signotset(mask);
275 276 277 278 279 280

	if (ufd == -1) {
		ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
		if (!ctx)
			return -ENOMEM;

281
		ctx->sigmask = *mask;
282 283 284 285 286

		/*
		 * When we call this, the initialization must be complete, since
		 * anon_inode_getfd() will install the fd.
		 */
287
		ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
288
				       O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
289 290
		if (ufd < 0)
			kfree(ctx);
291
	} else {
292 293
		struct fd f = fdget(ufd);
		if (!f.file)
294
			return -EBADF;
295 296 297
		ctx = f.file->private_data;
		if (f.file->f_op != &signalfd_fops) {
			fdput(f);
298 299
			return -EINVAL;
		}
Davide Libenzi's avatar
Davide Libenzi committed
300
		spin_lock_irq(&current->sighand->siglock);
301
		ctx->sigmask = *mask;
Davide Libenzi's avatar
Davide Libenzi committed
302 303 304
		spin_unlock_irq(&current->sighand->siglock);

		wake_up(&current->sighand->signalfd_wqh);
305
		fdput(f);
306 307 308 309
	}

	return ufd;
}
310

311 312 313
SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
		size_t, sizemask, int, flags)
{
314 315 316 317 318 319
	sigset_t mask;

	if (sizemask != sizeof(sigset_t) ||
	    copy_from_user(&mask, user_mask, sizeof(mask)))
		return -EINVAL;
	return do_signalfd4(ufd, &mask, flags);
320 321
}

322 323
SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
		size_t, sizemask)
324
{
325 326 327 328 329 330
	sigset_t mask;

	if (sizemask != sizeof(sigset_t) ||
	    copy_from_user(&mask, user_mask, sizeof(mask)))
		return -EINVAL;
	return do_signalfd4(ufd, &mask, 0);
331
}
332 333

#ifdef CONFIG_COMPAT
334
static long do_compat_signalfd4(int ufd,
335
			const compat_sigset_t __user *user_mask,
336
			compat_size_t sigsetsize, int flags)
337
{
338
	sigset_t mask;
339 340 341

	if (sigsetsize != sizeof(compat_sigset_t))
		return -EINVAL;
342
	if (get_compat_sigset(&mask, user_mask))
343
		return -EFAULT;
344
	return do_signalfd4(ufd, &mask, flags);
345 346
}

347
COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
348
		     const compat_sigset_t __user *, user_mask,
349 350 351
		     compat_size_t, sigsetsize,
		     int, flags)
{
352
	return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
353 354
}

355
COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
356
		     const compat_sigset_t __user *, user_mask,
357 358
		     compat_size_t, sigsetsize)
{
359
	return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
360 361
}
#endif