signalfd.c 7.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  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
14
 *  Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
15
 *      Retrieve multiple signals with one read() call
Davide Libenzi's avatar
Davide Libenzi committed
16 17
 *  Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
 *      Attach to the sighand only during read() and poll().
18 19 20 21 22 23 24 25 26 27 28 29
 */

#include <linux/file.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/list.h>
#include <linux/anon_inodes.h>
#include <linux/signalfd.h>
30
#include <linux/syscalls.h>
31 32 33 34 35 36 37

struct signalfd_ctx {
	sigset_t sigmask;
};

static int signalfd_release(struct inode *inode, struct file *file)
{
Davide Libenzi's avatar
Davide Libenzi committed
38
	kfree(file->private_data);
39 40 41 42 43 44 45 46
	return 0;
}

static unsigned int signalfd_poll(struct file *file, poll_table *wait)
{
	struct signalfd_ctx *ctx = file->private_data;
	unsigned int events = 0;

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

Davide Libenzi's avatar
Davide Libenzi committed
49 50 51 52
	spin_lock_irq(&current->sighand->siglock);
	if (next_signal(&current->pending, &ctx->sigmask) ||
	    next_signal(&current->signal->shared_pending,
			&ctx->sigmask))
53
		events |= POLLIN;
Davide Libenzi's avatar
Davide Libenzi committed
54
	spin_unlock_irq(&current->sighand->siglock);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

	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);

	/*
70
	 * Unused members should be zero ...
71 72 73 74 75 76 77
	 */
	err = __clear_user(uinfo, sizeof(*uinfo));

	/*
	 * If you change siginfo_t structure, please be sure
	 * this code is fixed accordingly.
	 */
78 79 80
	err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
	err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
	err |= __put_user((short) kinfo->si_code, &uinfo->ssi_code);
81 82
	switch (kinfo->si_code & __SI_MASK) {
	case __SI_KILL:
83 84
		err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
		err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
85 86
		break;
	case __SI_TIMER:
87 88 89
		 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);
90 91
		break;
	case __SI_POLL:
92 93
		err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
		err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
94 95
		break;
	case __SI_FAULT:
96
		err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
97
#ifdef __ARCH_SI_TRAPNO
98
		err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
99 100 101
#endif
		break;
	case __SI_CHLD:
102 103 104 105 106
		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);
107 108 109
		break;
	case __SI_RT: /* This is not generated by the kernel as of now. */
	case __SI_MESGQ: /* But this is */
110 111 112
		err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
		err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
		err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
113
		break;
114 115 116 117
	default:
		/*
		 * This case catches also the signals queued by sigqueue().
		 */
118 119
		err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
		err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
120 121
		err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
		err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
122 123 124 125 126 127
		break;
	}

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

128 129 130 131 132 133
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
				int nonblock)
{
	ssize_t ret;
	DECLARE_WAITQUEUE(wait, current);

Davide Libenzi's avatar
Davide Libenzi committed
134 135
	spin_lock_irq(&current->sighand->siglock);
	ret = dequeue_signal(current, &ctx->sigmask, info);
136 137 138 139 140 141
	switch (ret) {
	case 0:
		if (!nonblock)
			break;
		ret = -EAGAIN;
	default:
Davide Libenzi's avatar
Davide Libenzi committed
142
		spin_unlock_irq(&current->sighand->siglock);
143 144 145
		return ret;
	}

Davide Libenzi's avatar
Davide Libenzi committed
146
	add_wait_queue(&current->sighand->signalfd_wqh, &wait);
147 148
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
Davide Libenzi's avatar
Davide Libenzi committed
149
		ret = dequeue_signal(current, &ctx->sigmask, info);
150 151 152 153 154 155
		if (ret != 0)
			break;
		if (signal_pending(current)) {
			ret = -ERESTARTSYS;
			break;
		}
Davide Libenzi's avatar
Davide Libenzi committed
156
		spin_unlock_irq(&current->sighand->siglock);
157
		schedule();
Davide Libenzi's avatar
Davide Libenzi committed
158
		spin_lock_irq(&current->sighand->siglock);
159
	}
Davide Libenzi's avatar
Davide Libenzi committed
160
	spin_unlock_irq(&current->sighand->siglock);
161

Davide Libenzi's avatar
Davide Libenzi committed
162
	remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
163 164 165 166 167
	__set_current_state(TASK_RUNNING);

	return ret;
}

168
/*
Davide Libenzi's avatar
Davide Libenzi committed
169 170 171
 * 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".
172 173 174 175 176
 */
static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
			     loff_t *ppos)
{
	struct signalfd_ctx *ctx = file->private_data;
177 178 179
	struct signalfd_siginfo __user *siginfo;
	int nonblock = file->f_flags & O_NONBLOCK;
	ssize_t ret, total = 0;
180 181
	siginfo_t info;

182 183
	count /= sizeof(struct signalfd_siginfo);
	if (!count)
184 185
		return -EINVAL;

186 187 188 189 190 191 192 193 194 195 196 197 198
	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
199
	return total ? total: ret;
200 201 202 203 204 205 206 207
}

static const struct file_operations signalfd_fops = {
	.release	= signalfd_release,
	.poll		= signalfd_poll,
	.read		= signalfd_read,
};

208 209
asmlinkage long sys_signalfd4(int ufd, sigset_t __user *user_mask,
			      size_t sizemask, int flags)
210 211 212 213
{
	sigset_t sigmask;
	struct signalfd_ctx *ctx;

214 215 216 217
	/* Check the SFD_* constants for consistency.  */
	BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
	BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);

218
	if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
219 220
		return -EINVAL;

221 222
	if (sizemask != sizeof(sigset_t) ||
	    copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
Ulrich Drepper's avatar
Ulrich Drepper committed
223
		return -EINVAL;
224 225 226 227 228 229 230 231 232 233 234 235 236 237
	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.
		 */
238
		ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
239
				       flags & (O_CLOEXEC | O_NONBLOCK));
240 241
		if (ufd < 0)
			kfree(ctx);
242
	} else {
243
		struct file *file = fget(ufd);
244 245 246 247 248 249 250
		if (!file)
			return -EBADF;
		ctx = file->private_data;
		if (file->f_op != &signalfd_fops) {
			fput(file);
			return -EINVAL;
		}
Davide Libenzi's avatar
Davide Libenzi committed
251 252 253 254 255
		spin_lock_irq(&current->sighand->siglock);
		ctx->sigmask = sigmask;
		spin_unlock_irq(&current->sighand->siglock);

		wake_up(&current->sighand->signalfd_wqh);
256 257 258 259 260
		fput(file);
	}

	return ufd;
}
261 262 263 264 265 266

asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask,
			     size_t sizemask)
{
	return sys_signalfd4(ufd, user_mask, sizemask, 0);
}