blob: bfd776509a7271e324c5559397542c36c4564a34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/fcntl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <linux/syscalls.h>
8#include <linux/init.h>
9#include <linux/mm.h>
10#include <linux/fs.h>
11#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040012#include <linux/fdtable.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/dnotify.h>
15#include <linux/smp_lock.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/security.h>
19#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070021#include <linux/rcupdate.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070022#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/poll.h>
25#include <asm/siginfo.h>
26#include <asm/uaccess.h>
27
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -080028void set_close_on_exec(unsigned int fd, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070031 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070033 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (flag)
Dipankar Sarmabadf1662005-09-09 13:04:10 -070035 FD_SET(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 else
Dipankar Sarmabadf1662005-09-09 13:04:10 -070037 FD_CLR(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 spin_unlock(&files->file_lock);
39}
40
Arjan van de Ven858119e2006-01-14 13:20:43 -080041static int get_close_on_exec(unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070044 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 int res;
Dipankar Sarmab8359962005-09-09 13:04:14 -070046 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -070047 fdt = files_fdtable(files);
48 res = FD_ISSET(fd, fdt->close_on_exec);
Dipankar Sarmab8359962005-09-09 13:04:14 -070049 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return res;
51}
52
53/*
54 * locate_fd finds a free file descriptor in the open_fds fdset,
55 * expanding the fd arrays if necessary. Must be called with the
56 * file_lock held for write.
57 */
58
Al Virof8f95702008-04-23 20:38:10 -040059static int locate_fd(unsigned int orig_start, int cloexec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Al Virof8f95702008-04-23 20:38:10 -040061 struct files_struct *files = current->files;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned int newfd;
63 unsigned int start;
64 int error;
Dipankar Sarmabadf1662005-09-09 13:04:10 -070065 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Al Virof8f95702008-04-23 20:38:10 -040067 spin_lock(&files->file_lock);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 error = -EINVAL;
70 if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
71 goto out;
72
73repeat:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070074 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /*
76 * Someone might have closed fd's in the range
Dipankar Sarmabadf1662005-09-09 13:04:10 -070077 * orig_start..fdt->next_fd
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
79 start = orig_start;
Eric Dumazet0c9e63f2006-03-23 03:00:12 -080080 if (start < files->next_fd)
81 start = files->next_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 newfd = start;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -080084 if (start < fdt->max_fds)
Dipankar Sarmabadf1662005-09-09 13:04:10 -070085 newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
Vadim Lobanovbbea9f62006-12-10 02:21:12 -080086 fdt->max_fds, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 error = -EMFILE;
89 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
90 goto out;
91
92 error = expand_files(files, newfd);
93 if (error < 0)
94 goto out;
95
96 /*
97 * If we needed to expand the fs array we
98 * might have blocked - try again.
99 */
100 if (error)
101 goto repeat;
102
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800103 if (start <= files->next_fd)
104 files->next_fd = newfd + 1;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700105
Al Virof8f95702008-04-23 20:38:10 -0400106 FD_SET(newfd, fdt->open_fds);
107 if (cloexec)
108 FD_SET(newfd, fdt->close_on_exec);
109 else
110 FD_CLR(newfd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 error = newfd;
Al Virof8f95702008-04-23 20:38:10 -0400112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113out:
Al Virof8f95702008-04-23 20:38:10 -0400114 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return error;
116}
117
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700118static int dupfd(struct file *file, unsigned int start, int cloexec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Al Virof8f95702008-04-23 20:38:10 -0400120 int fd = locate_fd(start, cloexec);
121 if (fd >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 fd_install(fd, file);
Al Virof8f95702008-04-23 20:38:10 -0400123 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 fput(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 return fd;
127}
128
129asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
130{
131 int err = -EBADF;
132 struct file * file, *tofree;
133 struct files_struct * files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700134 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 spin_lock(&files->file_lock);
137 if (!(file = fcheck(oldfd)))
138 goto out_unlock;
139 err = newfd;
140 if (newfd == oldfd)
141 goto out_unlock;
142 err = -EBADF;
143 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
144 goto out_unlock;
145 get_file(file); /* We are now finished with oldfd */
146
147 err = expand_files(files, newfd);
148 if (err < 0)
149 goto out_fput;
150
151 /* To avoid races with open() and dup(), we will mark the fd as
152 * in-use in the open-file bitmap throughout the entire dup2()
153 * process. This is quite safe: do_close() uses the fd array
154 * entry, not the bitmap, to decide what work needs to be
155 * done. --sct */
156 /* Doesn't work. open() might be there first. --AV */
157
158 /* Yes. It's a race. In user space. Nothing sane to do */
159 err = -EBUSY;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700160 fdt = files_fdtable(files);
161 tofree = fdt->fd[newfd];
162 if (!tofree && FD_ISSET(newfd, fdt->open_fds))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto out_fput;
164
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700165 rcu_assign_pointer(fdt->fd[newfd], file);
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700166 FD_SET(newfd, fdt->open_fds);
167 FD_CLR(newfd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 spin_unlock(&files->file_lock);
169
170 if (tofree)
171 filp_close(tofree, files);
172 err = newfd;
173out:
174 return err;
175out_unlock:
176 spin_unlock(&files->file_lock);
177 goto out;
178
179out_fput:
180 spin_unlock(&files->file_lock);
181 fput(file);
182 goto out;
183}
184
185asmlinkage long sys_dup(unsigned int fildes)
186{
187 int ret = -EBADF;
188 struct file * file = fget(fildes);
189
190 if (file)
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700191 ret = dupfd(file, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ret;
193}
194
195#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
196
197static int setfl(int fd, struct file * filp, unsigned long arg)
198{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800199 struct inode * inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int error = 0;
201
dean gaudet7d95c8f2006-02-03 03:04:30 -0800202 /*
203 * O_APPEND cannot be cleared if the file is marked as append-only
204 * and the file is open for write.
205 */
206 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EPERM;
208
209 /* O_NOATIME can only be set by the owner or superuser */
210 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
Satyam Sharma3bd858a2007-07-17 15:00:08 +0530211 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -EPERM;
213
214 /* required for strict SunOS emulation */
215 if (O_NONBLOCK != O_NDELAY)
216 if (arg & O_NDELAY)
217 arg |= O_NONBLOCK;
218
219 if (arg & O_DIRECT) {
220 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
221 !filp->f_mapping->a_ops->direct_IO)
222 return -EINVAL;
223 }
224
225 if (filp->f_op && filp->f_op->check_flags)
226 error = filp->f_op->check_flags(arg);
227 if (error)
228 return error;
229
230 lock_kernel();
231 if ((arg ^ filp->f_flags) & FASYNC) {
232 if (filp->f_op && filp->f_op->fasync) {
233 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
234 if (error < 0)
235 goto out;
236 }
237 }
238
239 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
240 out:
241 unlock_kernel();
242 return error;
243}
244
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700245static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 uid_t uid, uid_t euid, int force)
247{
248 write_lock_irq(&filp->f_owner.lock);
249 if (force || !filp->f_owner.pid) {
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700250 put_pid(filp->f_owner.pid);
251 filp->f_owner.pid = get_pid(pid);
252 filp->f_owner.pid_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 filp->f_owner.uid = uid;
254 filp->f_owner.euid = euid;
255 }
256 write_unlock_irq(&filp->f_owner.lock);
257}
258
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700259int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
260 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int err;
263
264 err = security_file_set_fowner(filp);
265 if (err)
266 return err;
267
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700268 f_modown(filp, pid, type, current->uid, current->euid, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270}
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700271EXPORT_SYMBOL(__f_setown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700273int f_setown(struct file *filp, unsigned long arg, int force)
274{
275 enum pid_type type;
276 struct pid *pid;
277 int who = arg;
278 int result;
279 type = PIDTYPE_PID;
280 if (who < 0) {
281 type = PIDTYPE_PGID;
282 who = -who;
283 }
284 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700285 pid = find_vpid(who);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700286 result = __f_setown(filp, pid, type, force);
287 rcu_read_unlock();
288 return result;
289}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290EXPORT_SYMBOL(f_setown);
291
292void f_delown(struct file *filp)
293{
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700294 f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
295}
296
297pid_t f_getown(struct file *filp)
298{
299 pid_t pid;
Eric W. Biederman43fa1adb2006-10-02 02:17:27 -0700300 read_lock(&filp->f_owner.lock);
Pavel Emelyanov6c5f3e72008-02-08 04:19:20 -0800301 pid = pid_vnr(filp->f_owner.pid);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700302 if (filp->f_owner.pid_type == PIDTYPE_PGID)
303 pid = -pid;
Eric W. Biederman43fa1adb2006-10-02 02:17:27 -0700304 read_unlock(&filp->f_owner.lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700305 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
309 struct file *filp)
310{
311 long err = -EINVAL;
312
313 switch (cmd) {
314 case F_DUPFD:
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700315 case F_DUPFD_CLOEXEC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 get_file(filp);
Ulrich Drepper22d2b352007-10-16 23:30:26 -0700317 err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 case F_GETFD:
320 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
321 break;
322 case F_SETFD:
323 err = 0;
324 set_close_on_exec(fd, arg & FD_CLOEXEC);
325 break;
326 case F_GETFL:
327 err = filp->f_flags;
328 break;
329 case F_SETFL:
330 err = setfl(fd, filp, arg);
331 break;
332 case F_GETLK:
333 err = fcntl_getlk(filp, (struct flock __user *) arg);
334 break;
335 case F_SETLK:
336 case F_SETLKW:
Peter Staubachc2936212005-07-27 11:45:09 -0700337 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
339 case F_GETOWN:
340 /*
341 * XXX If f_owner is a process group, the
342 * negative return value will get converted
343 * into an error. Oops. If we keep the
344 * current syscall conventions, the only way
345 * to fix this will be in libc.
346 */
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700347 err = f_getown(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 force_successful_syscall_return();
349 break;
350 case F_SETOWN:
351 err = f_setown(filp, arg, 1);
352 break;
353 case F_GETSIG:
354 err = filp->f_owner.signum;
355 break;
356 case F_SETSIG:
357 /* arg == 0 restores default behaviour. */
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700358 if (!valid_signal(arg)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360 }
361 err = 0;
362 filp->f_owner.signum = arg;
363 break;
364 case F_GETLEASE:
365 err = fcntl_getlease(filp);
366 break;
367 case F_SETLEASE:
368 err = fcntl_setlease(fd, filp, arg);
369 break;
370 case F_NOTIFY:
371 err = fcntl_dirnotify(fd, filp, arg);
372 break;
373 default:
374 break;
375 }
376 return err;
377}
378
379asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
380{
381 struct file *filp;
382 long err = -EBADF;
383
384 filp = fget(fd);
385 if (!filp)
386 goto out;
387
388 err = security_file_fcntl(filp, cmd, arg);
389 if (err) {
390 fput(filp);
391 return err;
392 }
393
394 err = do_fcntl(fd, cmd, arg, filp);
395
396 fput(filp);
397out:
398 return err;
399}
400
401#if BITS_PER_LONG == 32
402asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
403{
404 struct file * filp;
405 long err;
406
407 err = -EBADF;
408 filp = fget(fd);
409 if (!filp)
410 goto out;
411
412 err = security_file_fcntl(filp, cmd, arg);
413 if (err) {
414 fput(filp);
415 return err;
416 }
417 err = -EBADF;
418
419 switch (cmd) {
420 case F_GETLK64:
421 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
422 break;
423 case F_SETLK64:
424 case F_SETLKW64:
Peter Staubachc2936212005-07-27 11:45:09 -0700425 err = fcntl_setlk64(fd, filp, cmd,
426 (struct flock64 __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 default:
429 err = do_fcntl(fd, cmd, arg, filp);
430 break;
431 }
432 fput(filp);
433out:
434 return err;
435}
436#endif
437
438/* Table to convert sigio signal codes into poll band bitmaps */
439
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800440static const long band_table[NSIGPOLL] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 POLLIN | POLLRDNORM, /* POLL_IN */
442 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
443 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
444 POLLERR, /* POLL_ERR */
445 POLLPRI | POLLRDBAND, /* POLL_PRI */
446 POLLHUP | POLLERR /* POLL_HUP */
447};
448
449static inline int sigio_perm(struct task_struct *p,
450 struct fown_struct *fown, int sig)
451{
452 return (((fown->euid == 0) ||
453 (fown->euid == p->suid) || (fown->euid == p->uid) ||
454 (fown->uid == p->suid) || (fown->uid == p->uid)) &&
455 !security_file_send_sigiotask(p, fown, sig));
456}
457
458static void send_sigio_to_task(struct task_struct *p,
459 struct fown_struct *fown,
460 int fd,
461 int reason)
462{
463 if (!sigio_perm(p, fown, fown->signum))
464 return;
465
466 switch (fown->signum) {
467 siginfo_t si;
468 default:
469 /* Queue a rt signal with the appropriate fd as its
470 value. We use SI_SIGIO as the source, not
471 SI_KERNEL, since kernel signals always get
472 delivered even if we can't queue. Failure to
473 queue in this case _should_ be reported; we fall
474 back to SIGIO in that case. --sct */
475 si.si_signo = fown->signum;
476 si.si_errno = 0;
477 si.si_code = reason;
478 /* Make sure we are called with one of the POLL_*
479 reasons, otherwise we could leak kernel stack into
480 userspace. */
Eric Sesterhennf6298aa2006-04-02 13:37:19 +0200481 BUG_ON((reason & __SI_MASK) != __SI_POLL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (reason - POLL_IN >= NSIGPOLL)
483 si.si_band = ~0L;
484 else
485 si.si_band = band_table[reason - POLL_IN];
486 si.si_fd = fd;
Oleg Nesterov850d6fb2006-01-08 01:03:29 -0800487 if (!group_send_sig_info(fown->signum, &si, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 /* fall-through: fall back on the old plain SIGIO signal */
490 case 0:
Oleg Nesterov850d6fb2006-01-08 01:03:29 -0800491 group_send_sig_info(SIGIO, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493}
494
495void send_sigio(struct fown_struct *fown, int fd, int band)
496{
497 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700498 enum pid_type type;
499 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 read_lock(&fown->lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700502 type = fown->pid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 pid = fown->pid;
504 if (!pid)
505 goto out_unlock_fown;
506
507 read_lock(&tasklist_lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700508 do_each_pid_task(pid, type, p) {
509 send_sigio_to_task(p, fown, fd, band);
510 } while_each_pid_task(pid, type, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 read_unlock(&tasklist_lock);
512 out_unlock_fown:
513 read_unlock(&fown->lock);
514}
515
516static void send_sigurg_to_task(struct task_struct *p,
517 struct fown_struct *fown)
518{
519 if (sigio_perm(p, fown, SIGURG))
Oleg Nesterov850d6fb2006-01-08 01:03:29 -0800520 group_send_sig_info(SIGURG, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523int send_sigurg(struct fown_struct *fown)
524{
525 struct task_struct *p;
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700526 enum pid_type type;
527 struct pid *pid;
528 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 read_lock(&fown->lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700531 type = fown->pid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 pid = fown->pid;
533 if (!pid)
534 goto out_unlock_fown;
535
536 ret = 1;
537
538 read_lock(&tasklist_lock);
Eric W. Biederman609d7fa2006-10-02 02:17:15 -0700539 do_each_pid_task(pid, type, p) {
540 send_sigurg_to_task(p, fown);
541 } while_each_pid_task(pid, type, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 read_unlock(&tasklist_lock);
543 out_unlock_fown:
544 read_unlock(&fown->lock);
545 return ret;
546}
547
548static DEFINE_RWLOCK(fasync_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800549static struct kmem_cache *fasync_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551/*
552 * fasync_helper() is used by some character device drivers (mainly mice)
553 * to set up the fasync queue. It returns negative on error, 0 if it did
554 * no changes and positive if it added/deleted the entry.
555 */
556int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
557{
558 struct fasync_struct *fa, **fp;
559 struct fasync_struct *new = NULL;
560 int result = 0;
561
562 if (on) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800563 new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!new)
565 return -ENOMEM;
566 }
567 write_lock_irq(&fasync_lock);
568 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
569 if (fa->fa_file == filp) {
570 if(on) {
571 fa->fa_fd = fd;
572 kmem_cache_free(fasync_cache, new);
573 } else {
574 *fp = fa->fa_next;
575 kmem_cache_free(fasync_cache, fa);
576 result = 1;
577 }
578 goto out;
579 }
580 }
581
582 if (on) {
583 new->magic = FASYNC_MAGIC;
584 new->fa_file = filp;
585 new->fa_fd = fd;
586 new->fa_next = *fapp;
587 *fapp = new;
588 result = 1;
589 }
590out:
591 write_unlock_irq(&fasync_lock);
592 return result;
593}
594
595EXPORT_SYMBOL(fasync_helper);
596
597void __kill_fasync(struct fasync_struct *fa, int sig, int band)
598{
599 while (fa) {
600 struct fown_struct * fown;
601 if (fa->magic != FASYNC_MAGIC) {
602 printk(KERN_ERR "kill_fasync: bad magic number in "
603 "fasync_struct!\n");
604 return;
605 }
606 fown = &fa->fa_file->f_owner;
607 /* Don't send SIGURG to processes which have not set a
608 queued signum: SIGURG has its own default signalling
609 mechanism. */
610 if (!(sig == SIGURG && fown->signum == 0))
611 send_sigio(fown, fa->fa_fd, band);
612 fa = fa->fa_next;
613 }
614}
615
616EXPORT_SYMBOL(__kill_fasync);
617
618void kill_fasync(struct fasync_struct **fp, int sig, int band)
619{
620 /* First a quick test without locking: usually
621 * the list is empty.
622 */
623 if (*fp) {
624 read_lock(&fasync_lock);
625 /* reread *fp after obtaining the lock */
626 __kill_fasync(*fp, sig, band);
627 read_unlock(&fasync_lock);
628 }
629}
630EXPORT_SYMBOL(kill_fasync);
631
632static int __init fasync_init(void)
633{
634 fasync_cache = kmem_cache_create("fasync_cache",
Paul Mundt20c2df82007-07-20 10:11:58 +0900635 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
637}
638
639module_init(fasync_init)