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