signalfd.c 9.4 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.
D
Davide Libenzi 已提交
15
 *  Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
16
 *      Retrieve multiple signals with one read() call
D
Davide Libenzi 已提交
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)
{
D
Davide Libenzi 已提交
57
	kfree(file->private_data);
58 59 60
	return 0;
}

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

D
Davide Libenzi 已提交
66
	poll_wait(file, &current->sighand->signalfd_wqh, wait);
67

D
Davide Libenzi 已提交
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;
D
Davide Libenzi 已提交
73
	spin_unlock_irq(&current->sighand->siglock);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

	return events;
}

/*
 * Copied from copy_siginfo_to_user() in kernel/signal.c
 */
static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
			     siginfo_t const *kinfo)
{
	long err;

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

	/*
89
	 * Unused members should be zero ...
90 91 92 93 94 95 96
	 */
	err = __clear_user(uinfo, sizeof(*uinfo));

	/*
	 * If you change siginfo_t structure, please be sure
	 * this code is fixed accordingly.
	 */
D
Davide Libenzi 已提交
97 98
	err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
	err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
99 100 101
	err |= __put_user(kinfo->si_code, &uinfo->ssi_code);
	switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
	case SIL_KILL:
D
Davide Libenzi 已提交
102 103
		err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
		err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
104
		break;
105
	case SIL_TIMER:
D
Davide Libenzi 已提交
106 107 108
		 err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
		 err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
		 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
109
		 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
110
		break;
111
	case SIL_POLL:
D
Davide Libenzi 已提交
112 113
		err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
		err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
114
		break;
115
	case SIL_FAULT:
D
Davide Libenzi 已提交
116
		err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
117
#ifdef __ARCH_SI_TRAPNO
D
Davide Libenzi 已提交
118
		err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
119 120
#endif
#ifdef BUS_MCEERR_AO
121
		/*
122 123 124
		 * Other callers might not initialize the si_lsb field,
		 * so check explicitly for the right codes here.
		 */
125
		if (kinfo->si_signo == SIGBUS &&
126 127 128 129 130 131 132 133 134 135 136
		     kinfo->si_code == BUS_MCEERR_AO)
			err |= __put_user((short) kinfo->si_addr_lsb,
					  &uinfo->ssi_addr_lsb);
#endif
#ifdef BUS_MCEERR_AR
		/*
		 * Other callers might not initialize the si_lsb field,
		 * so check explicitly for the right codes here.
		 */
		if (kinfo->si_signo == SIGBUS &&
		    kinfo->si_code == BUS_MCEERR_AR)
137 138
			err |= __put_user((short) kinfo->si_addr_lsb,
					  &uinfo->ssi_addr_lsb);
139 140
#endif
		break;
141
	case SIL_CHLD:
D
Davide Libenzi 已提交
142 143 144 145 146
		err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
		err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
		err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
		err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
		err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
147
		break;
148
	case SIL_RT:
149 150 151 152
	default:
		/*
		 * This case catches also the signals queued by sigqueue().
		 */
D
Davide Libenzi 已提交
153 154
		err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
		err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
155 156
		err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
		err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
157 158 159 160 161 162
		break;
	}

	return err ? -EFAULT: sizeof(*uinfo);
}

163 164 165 166 167 168
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
				int nonblock)
{
	ssize_t ret;
	DECLARE_WAITQUEUE(wait, current);

D
Davide Libenzi 已提交
169 170
	spin_lock_irq(&current->sighand->siglock);
	ret = dequeue_signal(current, &ctx->sigmask, info);
171 172 173 174 175 176
	switch (ret) {
	case 0:
		if (!nonblock)
			break;
		ret = -EAGAIN;
	default:
D
Davide Libenzi 已提交
177
		spin_unlock_irq(&current->sighand->siglock);
178 179 180
		return ret;
	}

D
Davide Libenzi 已提交
181
	add_wait_queue(&current->sighand->signalfd_wqh, &wait);
182 183
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
D
Davide Libenzi 已提交
184
		ret = dequeue_signal(current, &ctx->sigmask, info);
185 186 187 188 189 190
		if (ret != 0)
			break;
		if (signal_pending(current)) {
			ret = -ERESTARTSYS;
			break;
		}
D
Davide Libenzi 已提交
191
		spin_unlock_irq(&current->sighand->siglock);
192
		schedule();
D
Davide Libenzi 已提交
193
		spin_lock_irq(&current->sighand->siglock);
194
	}
D
Davide Libenzi 已提交
195
	spin_unlock_irq(&current->sighand->siglock);
196

D
Davide Libenzi 已提交
197
	remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
198 199 200 201 202
	__set_current_state(TASK_RUNNING);

	return ret;
}

203
/*
D
Davide Libenzi 已提交
204 205 206
 * 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".
207 208 209 210 211
 */
static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
			     loff_t *ppos)
{
	struct signalfd_ctx *ctx = file->private_data;
212 213 214
	struct signalfd_siginfo __user *siginfo;
	int nonblock = file->f_flags & O_NONBLOCK;
	ssize_t ret, total = 0;
215 216
	siginfo_t info;

217 218
	count /= sizeof(struct signalfd_siginfo);
	if (!count)
219 220
		return -EINVAL;

221 222 223 224 225 226 227 228 229 230 231 232 233
	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);

D
Davide Libenzi 已提交
234
	return total ? total: ret;
235 236
}

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

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

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

259 260
static int do_signalfd4(int ufd, sigset_t __user *user_mask, size_t sizemask,
			int flags)
261 262 263 264
{
	sigset_t sigmask;
	struct signalfd_ctx *ctx;

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

269
	if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
U
Ulrich Drepper 已提交
270 271
		return -EINVAL;

272 273
	if (sizemask != sizeof(sigset_t) ||
	    copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
U
Ulrich Drepper 已提交
274
		return -EINVAL;
275 276 277 278 279 280 281 282 283 284 285 286 287 288
	sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
	signotset(&sigmask);

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

		ctx->sigmask = sigmask;

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

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

	return ufd;
}
U
Ulrich Drepper 已提交
312

313 314 315 316 317 318
SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
		size_t, sizemask, int, flags)
{
	return do_signalfd4(ufd, user_mask, sizemask, flags);
}

319 320
SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
		size_t, sizemask)
U
Ulrich Drepper 已提交
321
{
322
	return do_signalfd4(ufd, user_mask, sizemask, 0);
U
Ulrich Drepper 已提交
323
}
324 325

#ifdef CONFIG_COMPAT
326 327 328
static long do_compat_signalfd4(int ufd,
			const compat_sigset_t __user *sigmask,
			compat_size_t sigsetsize, int flags)
329 330 331 332 333 334
{
	sigset_t tmp;
	sigset_t __user *ksigmask;

	if (sigsetsize != sizeof(compat_sigset_t))
		return -EINVAL;
A
Al Viro 已提交
335
	if (get_compat_sigset(&tmp, sigmask))
336 337 338 339 340
		return -EFAULT;
	ksigmask = compat_alloc_user_space(sizeof(sigset_t));
	if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
		return -EFAULT;

341
	return do_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
342 343
}

344 345 346 347 348 349 350 351
COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
		     const compat_sigset_t __user *, sigmask,
		     compat_size_t, sigsetsize,
		     int, flags)
{
	return do_compat_signalfd4(ufd, sigmask, sigsetsize, flags);
}

352 353 354 355
COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
		     const compat_sigset_t __user *,sigmask,
		     compat_size_t, sigsetsize)
{
356
	return do_compat_signalfd4(ufd, sigmask, sigsetsize, 0);
357 358
}
#endif