take close-on-exec logics to fs/file.c, clean it up a bit
[linux-2.6-block.git] / fs / fcntl.c
CommitLineData
1da177e4
LT
1/*
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>
9f3acc31 12#include <linux/fdtable.h>
16f7e0fe 13#include <linux/capability.h>
1da177e4 14#include <linux/dnotify.h>
1da177e4
LT
15#include <linux/slab.h>
16#include <linux/module.h>
35f3d14d 17#include <linux/pipe_fs_i.h>
1da177e4
LT
18#include <linux/security.h>
19#include <linux/ptrace.h>
7ed20e1a 20#include <linux/signal.h>
ab2af1f5 21#include <linux/rcupdate.h>
b488893a 22#include <linux/pid_namespace.h>
1d151c33 23#include <linux/user_namespace.h>
1da177e4
LT
24
25#include <asm/poll.h>
26#include <asm/siginfo.h>
27#include <asm/uaccess.h>
28
fc9b52cd 29void set_close_on_exec(unsigned int fd, int flag)
1da177e4
LT
30{
31 struct files_struct *files = current->files;
badf1662 32 struct fdtable *fdt;
1da177e4 33 spin_lock(&files->file_lock);
badf1662 34 fdt = files_fdtable(files);
1da177e4 35 if (flag)
1dce27c5 36 __set_close_on_exec(fd, fdt);
1da177e4 37 else
1dce27c5 38 __clear_close_on_exec(fd, fdt);
1da177e4
LT
39 spin_unlock(&files->file_lock);
40}
41
1dce27c5 42static bool get_close_on_exec(unsigned int fd)
1da177e4
LT
43{
44 struct files_struct *files = current->files;
badf1662 45 struct fdtable *fdt;
1dce27c5 46 bool res;
b835996f 47 rcu_read_lock();
badf1662 48 fdt = files_fdtable(files);
1dce27c5 49 res = close_on_exec(fd, fdt);
b835996f 50 rcu_read_unlock();
1da177e4
LT
51 return res;
52}
53
a26eab24 54SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
1da177e4
LT
55{
56 int err = -EBADF;
57 struct file * file, *tofree;
58 struct files_struct * files = current->files;
badf1662 59 struct fdtable *fdt;
1da177e4 60
336dd1f7
UD
61 if ((flags & ~O_CLOEXEC) != 0)
62 return -EINVAL;
63
6c5d0512
AV
64 if (unlikely(oldfd == newfd))
65 return -EINVAL;
f33ff992
AV
66
67 if (newfd >= rlimit(RLIMIT_NOFILE))
68 return -EMFILE;
6c5d0512 69
1da177e4 70 spin_lock(&files->file_lock);
1da177e4 71 err = expand_files(files, newfd);
1b7e190b
AV
72 file = fcheck(oldfd);
73 if (unlikely(!file))
74 goto Ebadf;
4e1e018e
AV
75 if (unlikely(err < 0)) {
76 if (err == -EMFILE)
1b7e190b
AV
77 goto Ebadf;
78 goto out_unlock;
4e1e018e 79 }
1b7e190b
AV
80 /*
81 * We need to detect attempts to do dup2() over allocated but still
82 * not finished descriptor. NB: OpenBSD avoids that at the price of
83 * extra work in their equivalent of fget() - they insert struct
84 * file immediately after grabbing descriptor, mark it larval if
85 * more work (e.g. actual opening) is needed and make sure that
86 * fget() treats larval files as absent. Potentially interesting,
87 * but while extra work in fget() is trivial, locking implications
88 * and amount of surgery on open()-related paths in VFS are not.
89 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
90 * deadlocks in rather amusing ways, AFAICS. All of that is out of
91 * scope of POSIX or SUS, since neither considers shared descriptor
92 * tables and this condition does not arise without those.
93 */
1da177e4 94 err = -EBUSY;
badf1662
DS
95 fdt = files_fdtable(files);
96 tofree = fdt->fd[newfd];
1dce27c5 97 if (!tofree && fd_is_open(newfd, fdt))
1b7e190b
AV
98 goto out_unlock;
99 get_file(file);
ab2af1f5 100 rcu_assign_pointer(fdt->fd[newfd], file);
1dce27c5 101 __set_open_fd(newfd, fdt);
336dd1f7 102 if (flags & O_CLOEXEC)
1dce27c5 103 __set_close_on_exec(newfd, fdt);
336dd1f7 104 else
1dce27c5 105 __clear_close_on_exec(newfd, fdt);
1da177e4
LT
106 spin_unlock(&files->file_lock);
107
108 if (tofree)
109 filp_close(tofree, files);
1da177e4 110
1b7e190b
AV
111 return newfd;
112
113Ebadf:
114 err = -EBADF;
115out_unlock:
1da177e4 116 spin_unlock(&files->file_lock);
1b7e190b 117 return err;
1da177e4 118}
336dd1f7 119
a26eab24 120SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
336dd1f7 121{
6c5d0512
AV
122 if (unlikely(newfd == oldfd)) { /* corner case */
123 struct files_struct *files = current->files;
2b79bc4f
JM
124 int retval = oldfd;
125
6c5d0512
AV
126 rcu_read_lock();
127 if (!fcheck_files(files, oldfd))
2b79bc4f 128 retval = -EBADF;
6c5d0512 129 rcu_read_unlock();
2b79bc4f 130 return retval;
6c5d0512 131 }
336dd1f7
UD
132 return sys_dup3(oldfd, newfd, 0);
133}
1da177e4 134
a26eab24 135SYSCALL_DEFINE1(dup, unsigned int, fildes)
1da177e4
LT
136{
137 int ret = -EBADF;
1abf0c71 138 struct file *file = fget_raw(fildes);
1027abe8
AV
139
140 if (file) {
141 ret = get_unused_fd();
142 if (ret >= 0)
143 fd_install(ret, file);
144 else
145 fput(file);
146 }
1da177e4
LT
147 return ret;
148}
149
76398425 150#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
1da177e4
LT
151
152static int setfl(int fd, struct file * filp, unsigned long arg)
153{
0f7fc9e4 154 struct inode * inode = filp->f_path.dentry->d_inode;
1da177e4
LT
155 int error = 0;
156
7d95c8f2 157 /*
158 * O_APPEND cannot be cleared if the file is marked as append-only
159 * and the file is open for write.
160 */
161 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
1da177e4
LT
162 return -EPERM;
163
164 /* O_NOATIME can only be set by the owner or superuser */
165 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
2e149670 166 if (!inode_owner_or_capable(inode))
1da177e4
LT
167 return -EPERM;
168
169 /* required for strict SunOS emulation */
170 if (O_NONBLOCK != O_NDELAY)
171 if (arg & O_NDELAY)
172 arg |= O_NONBLOCK;
173
174 if (arg & O_DIRECT) {
175 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
176 !filp->f_mapping->a_ops->direct_IO)
177 return -EINVAL;
178 }
179
180 if (filp->f_op && filp->f_op->check_flags)
181 error = filp->f_op->check_flags(arg);
182 if (error)
183 return error;
184
218d11a8 185 /*
76398425 186 * ->fasync() is responsible for setting the FASYNC bit.
218d11a8 187 */
76398425
JC
188 if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op &&
189 filp->f_op->fasync) {
190 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
191 if (error < 0)
192 goto out;
60aa4924
JC
193 if (error > 0)
194 error = 0;
1da177e4 195 }
db1dd4d3 196 spin_lock(&filp->f_lock);
1da177e4 197 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
db1dd4d3 198 spin_unlock(&filp->f_lock);
76398425 199
1da177e4 200 out:
1da177e4
LT
201 return error;
202}
203
609d7fa9 204static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
2f38d70f 205 int force)
1da177e4 206{
80e1e823 207 write_lock_irq(&filp->f_owner.lock);
1da177e4 208 if (force || !filp->f_owner.pid) {
609d7fa9
EB
209 put_pid(filp->f_owner.pid);
210 filp->f_owner.pid = get_pid(pid);
211 filp->f_owner.pid_type = type;
2f38d70f
ON
212
213 if (pid) {
214 const struct cred *cred = current_cred();
215 filp->f_owner.uid = cred->uid;
216 filp->f_owner.euid = cred->euid;
217 }
1da177e4 218 }
80e1e823 219 write_unlock_irq(&filp->f_owner.lock);
1da177e4
LT
220}
221
609d7fa9
EB
222int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
223 int force)
1da177e4
LT
224{
225 int err;
2f38d70f 226
1da177e4
LT
227 err = security_file_set_fowner(filp);
228 if (err)
229 return err;
230
2f38d70f 231 f_modown(filp, pid, type, force);
1da177e4
LT
232 return 0;
233}
609d7fa9 234EXPORT_SYMBOL(__f_setown);
1da177e4 235
609d7fa9
EB
236int f_setown(struct file *filp, unsigned long arg, int force)
237{
238 enum pid_type type;
239 struct pid *pid;
240 int who = arg;
241 int result;
242 type = PIDTYPE_PID;
243 if (who < 0) {
244 type = PIDTYPE_PGID;
245 who = -who;
246 }
247 rcu_read_lock();
b488893a 248 pid = find_vpid(who);
609d7fa9
EB
249 result = __f_setown(filp, pid, type, force);
250 rcu_read_unlock();
251 return result;
252}
1da177e4
LT
253EXPORT_SYMBOL(f_setown);
254
255void f_delown(struct file *filp)
256{
2f38d70f 257 f_modown(filp, NULL, PIDTYPE_PID, 1);
609d7fa9
EB
258}
259
260pid_t f_getown(struct file *filp)
261{
262 pid_t pid;
43fa1adb 263 read_lock(&filp->f_owner.lock);
6c5f3e7b 264 pid = pid_vnr(filp->f_owner.pid);
609d7fa9
EB
265 if (filp->f_owner.pid_type == PIDTYPE_PGID)
266 pid = -pid;
43fa1adb 267 read_unlock(&filp->f_owner.lock);
609d7fa9 268 return pid;
1da177e4
LT
269}
270
ba0a6c9f
PZ
271static int f_setown_ex(struct file *filp, unsigned long arg)
272{
273 struct f_owner_ex * __user owner_p = (void * __user)arg;
274 struct f_owner_ex owner;
275 struct pid *pid;
276 int type;
277 int ret;
278
279 ret = copy_from_user(&owner, owner_p, sizeof(owner));
280 if (ret)
5b54470d 281 return -EFAULT;
ba0a6c9f
PZ
282
283 switch (owner.type) {
284 case F_OWNER_TID:
285 type = PIDTYPE_MAX;
286 break;
287
288 case F_OWNER_PID:
289 type = PIDTYPE_PID;
290 break;
291
978b4053 292 case F_OWNER_PGRP:
ba0a6c9f
PZ
293 type = PIDTYPE_PGID;
294 break;
295
296 default:
297 return -EINVAL;
298 }
299
300 rcu_read_lock();
301 pid = find_vpid(owner.pid);
302 if (owner.pid && !pid)
303 ret = -ESRCH;
304 else
305 ret = __f_setown(filp, pid, type, 1);
306 rcu_read_unlock();
307
308 return ret;
309}
310
311static int f_getown_ex(struct file *filp, unsigned long arg)
312{
313 struct f_owner_ex * __user owner_p = (void * __user)arg;
314 struct f_owner_ex owner;
315 int ret = 0;
316
317 read_lock(&filp->f_owner.lock);
318 owner.pid = pid_vnr(filp->f_owner.pid);
319 switch (filp->f_owner.pid_type) {
320 case PIDTYPE_MAX:
321 owner.type = F_OWNER_TID;
322 break;
323
324 case PIDTYPE_PID:
325 owner.type = F_OWNER_PID;
326 break;
327
328 case PIDTYPE_PGID:
978b4053 329 owner.type = F_OWNER_PGRP;
ba0a6c9f
PZ
330 break;
331
332 default:
333 WARN_ON(1);
334 ret = -EINVAL;
335 break;
336 }
337 read_unlock(&filp->f_owner.lock);
338
5b54470d 339 if (!ret) {
ba0a6c9f 340 ret = copy_to_user(owner_p, &owner, sizeof(owner));
5b54470d
DC
341 if (ret)
342 ret = -EFAULT;
343 }
ba0a6c9f
PZ
344 return ret;
345}
346
1d151c33
CG
347#ifdef CONFIG_CHECKPOINT_RESTORE
348static int f_getowner_uids(struct file *filp, unsigned long arg)
349{
350 struct user_namespace *user_ns = current_user_ns();
351 uid_t * __user dst = (void * __user)arg;
352 uid_t src[2];
353 int err;
354
355 read_lock(&filp->f_owner.lock);
356 src[0] = from_kuid(user_ns, filp->f_owner.uid);
357 src[1] = from_kuid(user_ns, filp->f_owner.euid);
358 read_unlock(&filp->f_owner.lock);
359
360 err = put_user(src[0], &dst[0]);
361 err |= put_user(src[1], &dst[1]);
362
363 return err;
364}
365#else
366static int f_getowner_uids(struct file *filp, unsigned long arg)
367{
368 return -EINVAL;
369}
370#endif
371
1da177e4
LT
372static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
373 struct file *filp)
374{
375 long err = -EINVAL;
376
377 switch (cmd) {
378 case F_DUPFD:
22d2b35b 379 case F_DUPFD_CLOEXEC:
d554ed89 380 if (arg >= rlimit(RLIMIT_NOFILE))
4e1e018e 381 break;
1027abe8
AV
382 err = alloc_fd(arg, cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0);
383 if (err >= 0) {
384 get_file(filp);
385 fd_install(err, filp);
386 }
1da177e4
LT
387 break;
388 case F_GETFD:
389 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
390 break;
391 case F_SETFD:
392 err = 0;
393 set_close_on_exec(fd, arg & FD_CLOEXEC);
394 break;
395 case F_GETFL:
396 err = filp->f_flags;
397 break;
398 case F_SETFL:
399 err = setfl(fd, filp, arg);
400 break;
401 case F_GETLK:
402 err = fcntl_getlk(filp, (struct flock __user *) arg);
403 break;
404 case F_SETLK:
405 case F_SETLKW:
c293621b 406 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
1da177e4
LT
407 break;
408 case F_GETOWN:
409 /*
410 * XXX If f_owner is a process group, the
411 * negative return value will get converted
412 * into an error. Oops. If we keep the
413 * current syscall conventions, the only way
414 * to fix this will be in libc.
415 */
609d7fa9 416 err = f_getown(filp);
1da177e4
LT
417 force_successful_syscall_return();
418 break;
419 case F_SETOWN:
420 err = f_setown(filp, arg, 1);
421 break;
ba0a6c9f
PZ
422 case F_GETOWN_EX:
423 err = f_getown_ex(filp, arg);
424 break;
425 case F_SETOWN_EX:
426 err = f_setown_ex(filp, arg);
427 break;
1d151c33
CG
428 case F_GETOWNER_UIDS:
429 err = f_getowner_uids(filp, arg);
430 break;
1da177e4
LT
431 case F_GETSIG:
432 err = filp->f_owner.signum;
433 break;
434 case F_SETSIG:
435 /* arg == 0 restores default behaviour. */
7ed20e1a 436 if (!valid_signal(arg)) {
1da177e4
LT
437 break;
438 }
439 err = 0;
440 filp->f_owner.signum = arg;
441 break;
442 case F_GETLEASE:
443 err = fcntl_getlease(filp);
444 break;
445 case F_SETLEASE:
446 err = fcntl_setlease(fd, filp, arg);
447 break;
448 case F_NOTIFY:
449 err = fcntl_dirnotify(fd, filp, arg);
450 break;
35f3d14d
JA
451 case F_SETPIPE_SZ:
452 case F_GETPIPE_SZ:
453 err = pipe_fcntl(filp, cmd, arg);
454 break;
1da177e4
LT
455 default:
456 break;
457 }
458 return err;
459}
460
1abf0c71
AV
461static int check_fcntl_cmd(unsigned cmd)
462{
463 switch (cmd) {
464 case F_DUPFD:
465 case F_DUPFD_CLOEXEC:
466 case F_GETFD:
467 case F_SETFD:
468 case F_GETFL:
469 return 1;
470 }
471 return 0;
472}
473
a26eab24 474SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
1da177e4
LT
475{
476 struct file *filp;
545ec2c7 477 int fput_needed;
1da177e4
LT
478 long err = -EBADF;
479
545ec2c7 480 filp = fget_raw_light(fd, &fput_needed);
1da177e4
LT
481 if (!filp)
482 goto out;
483
1abf0c71 484 if (unlikely(filp->f_mode & FMODE_PATH)) {
545ec2c7
AV
485 if (!check_fcntl_cmd(cmd))
486 goto out1;
1abf0c71
AV
487 }
488
1da177e4 489 err = security_file_fcntl(filp, cmd, arg);
545ec2c7
AV
490 if (!err)
491 err = do_fcntl(fd, cmd, arg, filp);
1da177e4 492
545ec2c7
AV
493out1:
494 fput_light(filp, fput_needed);
1da177e4
LT
495out:
496 return err;
497}
498
499#if BITS_PER_LONG == 32
a26eab24
HC
500SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
501 unsigned long, arg)
1da177e4
LT
502{
503 struct file * filp;
545ec2c7
AV
504 long err = -EBADF;
505 int fput_needed;
1da177e4 506
545ec2c7 507 filp = fget_raw_light(fd, &fput_needed);
1da177e4
LT
508 if (!filp)
509 goto out;
510
1abf0c71 511 if (unlikely(filp->f_mode & FMODE_PATH)) {
545ec2c7
AV
512 if (!check_fcntl_cmd(cmd))
513 goto out1;
1abf0c71
AV
514 }
515
1da177e4 516 err = security_file_fcntl(filp, cmd, arg);
545ec2c7
AV
517 if (err)
518 goto out1;
1da177e4
LT
519
520 switch (cmd) {
521 case F_GETLK64:
522 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
523 break;
524 case F_SETLK64:
525 case F_SETLKW64:
c293621b
PS
526 err = fcntl_setlk64(fd, filp, cmd,
527 (struct flock64 __user *) arg);
1da177e4
LT
528 break;
529 default:
530 err = do_fcntl(fd, cmd, arg, filp);
531 break;
532 }
545ec2c7
AV
533out1:
534 fput_light(filp, fput_needed);
1da177e4
LT
535out:
536 return err;
537}
538#endif
539
540/* Table to convert sigio signal codes into poll band bitmaps */
541
fa3536cc 542static const long band_table[NSIGPOLL] = {
1da177e4
LT
543 POLLIN | POLLRDNORM, /* POLL_IN */
544 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
545 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
546 POLLERR, /* POLL_ERR */
547 POLLPRI | POLLRDBAND, /* POLL_PRI */
548 POLLHUP | POLLERR /* POLL_HUP */
549};
550
551static inline int sigio_perm(struct task_struct *p,
552 struct fown_struct *fown, int sig)
553{
c69e8d9c
DH
554 const struct cred *cred;
555 int ret;
556
557 rcu_read_lock();
558 cred = __task_cred(p);
8e96e3b7
EB
559 ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) ||
560 uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) ||
561 uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) &&
c69e8d9c
DH
562 !security_file_send_sigiotask(p, fown, sig));
563 rcu_read_unlock();
564 return ret;
1da177e4
LT
565}
566
567static void send_sigio_to_task(struct task_struct *p,
8eeee4e2 568 struct fown_struct *fown,
ba0a6c9f 569 int fd, int reason, int group)
1da177e4 570{
8eeee4e2
ON
571 /*
572 * F_SETSIG can change ->signum lockless in parallel, make
573 * sure we read it once and use the same value throughout.
574 */
575 int signum = ACCESS_ONCE(fown->signum);
576
577 if (!sigio_perm(p, fown, signum))
1da177e4
LT
578 return;
579
8eeee4e2 580 switch (signum) {
1da177e4
LT
581 siginfo_t si;
582 default:
583 /* Queue a rt signal with the appropriate fd as its
584 value. We use SI_SIGIO as the source, not
585 SI_KERNEL, since kernel signals always get
586 delivered even if we can't queue. Failure to
587 queue in this case _should_ be reported; we fall
588 back to SIGIO in that case. --sct */
8eeee4e2 589 si.si_signo = signum;
1da177e4
LT
590 si.si_errno = 0;
591 si.si_code = reason;
592 /* Make sure we are called with one of the POLL_*
593 reasons, otherwise we could leak kernel stack into
594 userspace. */
f6298aab 595 BUG_ON((reason & __SI_MASK) != __SI_POLL);
1da177e4
LT
596 if (reason - POLL_IN >= NSIGPOLL)
597 si.si_band = ~0L;
598 else
599 si.si_band = band_table[reason - POLL_IN];
600 si.si_fd = fd;
ba0a6c9f 601 if (!do_send_sig_info(signum, &si, p, group))
1da177e4
LT
602 break;
603 /* fall-through: fall back on the old plain SIGIO signal */
604 case 0:
ba0a6c9f 605 do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, group);
1da177e4
LT
606 }
607}
608
609void send_sigio(struct fown_struct *fown, int fd, int band)
610{
611 struct task_struct *p;
609d7fa9
EB
612 enum pid_type type;
613 struct pid *pid;
ba0a6c9f 614 int group = 1;
1da177e4
LT
615
616 read_lock(&fown->lock);
ba0a6c9f 617
609d7fa9 618 type = fown->pid_type;
ba0a6c9f
PZ
619 if (type == PIDTYPE_MAX) {
620 group = 0;
621 type = PIDTYPE_PID;
622 }
623
1da177e4
LT
624 pid = fown->pid;
625 if (!pid)
626 goto out_unlock_fown;
627
628 read_lock(&tasklist_lock);
609d7fa9 629 do_each_pid_task(pid, type, p) {
ba0a6c9f 630 send_sigio_to_task(p, fown, fd, band, group);
609d7fa9 631 } while_each_pid_task(pid, type, p);
1da177e4
LT
632 read_unlock(&tasklist_lock);
633 out_unlock_fown:
634 read_unlock(&fown->lock);
635}
636
637static void send_sigurg_to_task(struct task_struct *p,
ba0a6c9f 638 struct fown_struct *fown, int group)
1da177e4
LT
639{
640 if (sigio_perm(p, fown, SIGURG))
ba0a6c9f 641 do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, group);
1da177e4
LT
642}
643
644int send_sigurg(struct fown_struct *fown)
645{
646 struct task_struct *p;
609d7fa9
EB
647 enum pid_type type;
648 struct pid *pid;
ba0a6c9f 649 int group = 1;
609d7fa9 650 int ret = 0;
1da177e4
LT
651
652 read_lock(&fown->lock);
ba0a6c9f 653
609d7fa9 654 type = fown->pid_type;
ba0a6c9f
PZ
655 if (type == PIDTYPE_MAX) {
656 group = 0;
657 type = PIDTYPE_PID;
658 }
659
1da177e4
LT
660 pid = fown->pid;
661 if (!pid)
662 goto out_unlock_fown;
663
664 ret = 1;
665
666 read_lock(&tasklist_lock);
609d7fa9 667 do_each_pid_task(pid, type, p) {
ba0a6c9f 668 send_sigurg_to_task(p, fown, group);
609d7fa9 669 } while_each_pid_task(pid, type, p);
1da177e4
LT
670 read_unlock(&tasklist_lock);
671 out_unlock_fown:
672 read_unlock(&fown->lock);
673 return ret;
674}
675
989a2979 676static DEFINE_SPINLOCK(fasync_lock);
e18b890b 677static struct kmem_cache *fasync_cache __read_mostly;
1da177e4 678
989a2979
ED
679static void fasync_free_rcu(struct rcu_head *head)
680{
681 kmem_cache_free(fasync_cache,
682 container_of(head, struct fasync_struct, fa_rcu));
683}
684
1da177e4 685/*
53281b6d
LT
686 * Remove a fasync entry. If successfully removed, return
687 * positive and clear the FASYNC flag. If no entry exists,
688 * do nothing and return 0.
689 *
690 * NOTE! It is very important that the FASYNC flag always
691 * match the state "is the filp on a fasync list".
692 *
1da177e4 693 */
f7347ce4 694int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp)
1da177e4
LT
695{
696 struct fasync_struct *fa, **fp;
1da177e4
LT
697 int result = 0;
698
53281b6d 699 spin_lock(&filp->f_lock);
989a2979 700 spin_lock(&fasync_lock);
53281b6d
LT
701 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
702 if (fa->fa_file != filp)
703 continue;
989a2979
ED
704
705 spin_lock_irq(&fa->fa_lock);
706 fa->fa_file = NULL;
707 spin_unlock_irq(&fa->fa_lock);
708
53281b6d 709 *fp = fa->fa_next;
989a2979 710 call_rcu(&fa->fa_rcu, fasync_free_rcu);
53281b6d
LT
711 filp->f_flags &= ~FASYNC;
712 result = 1;
713 break;
1da177e4 714 }
989a2979 715 spin_unlock(&fasync_lock);
53281b6d
LT
716 spin_unlock(&filp->f_lock);
717 return result;
718}
719
f7347ce4
LT
720struct fasync_struct *fasync_alloc(void)
721{
722 return kmem_cache_alloc(fasync_cache, GFP_KERNEL);
723}
724
53281b6d 725/*
f7347ce4
LT
726 * NOTE! This can be used only for unused fasync entries:
727 * entries that actually got inserted on the fasync list
728 * need to be released by rcu - see fasync_remove_entry.
53281b6d 729 */
f7347ce4 730void fasync_free(struct fasync_struct *new)
53281b6d 731{
f7347ce4
LT
732 kmem_cache_free(fasync_cache, new);
733}
53281b6d 734
f7347ce4
LT
735/*
736 * Insert a new entry into the fasync list. Return the pointer to the
737 * old one if we didn't use the new one.
55f335a8
LT
738 *
739 * NOTE! It is very important that the FASYNC flag always
740 * match the state "is the filp on a fasync list".
f7347ce4
LT
741 */
742struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new)
743{
744 struct fasync_struct *fa, **fp;
4a6a4499 745
4a6a4499 746 spin_lock(&filp->f_lock);
989a2979 747 spin_lock(&fasync_lock);
1da177e4 748 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
53281b6d
LT
749 if (fa->fa_file != filp)
750 continue;
989a2979
ED
751
752 spin_lock_irq(&fa->fa_lock);
53281b6d 753 fa->fa_fd = fd;
989a2979 754 spin_unlock_irq(&fa->fa_lock);
53281b6d 755 goto out;
1da177e4
LT
756 }
757
989a2979 758 spin_lock_init(&new->fa_lock);
53281b6d
LT
759 new->magic = FASYNC_MAGIC;
760 new->fa_file = filp;
761 new->fa_fd = fd;
762 new->fa_next = *fapp;
989a2979 763 rcu_assign_pointer(*fapp, new);
53281b6d
LT
764 filp->f_flags |= FASYNC;
765
1da177e4 766out:
989a2979 767 spin_unlock(&fasync_lock);
4a6a4499 768 spin_unlock(&filp->f_lock);
f7347ce4
LT
769 return fa;
770}
771
772/*
773 * Add a fasync entry. Return negative on error, positive if
774 * added, and zero if did nothing but change an existing one.
f7347ce4
LT
775 */
776static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp)
777{
778 struct fasync_struct *new;
779
780 new = fasync_alloc();
781 if (!new)
782 return -ENOMEM;
783
784 /*
785 * fasync_insert_entry() returns the old (update) entry if
786 * it existed.
787 *
788 * So free the (unused) new entry and return 0 to let the
789 * caller know that we didn't add any new fasync entries.
790 */
791 if (fasync_insert_entry(fd, filp, fapp, new)) {
792 fasync_free(new);
793 return 0;
794 }
795
796 return 1;
1da177e4
LT
797}
798
53281b6d
LT
799/*
800 * fasync_helper() is used by almost all character device drivers
801 * to set up the fasync queue, and for regular files by the file
802 * lease code. It returns negative on error, 0 if it did no changes
803 * and positive if it added/deleted the entry.
804 */
805int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
806{
807 if (!on)
808 return fasync_remove_entry(filp, fapp);
809 return fasync_add_entry(fd, filp, fapp);
810}
811
1da177e4
LT
812EXPORT_SYMBOL(fasync_helper);
813
989a2979
ED
814/*
815 * rcu_read_lock() is held
816 */
817static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band)
1da177e4
LT
818{
819 while (fa) {
989a2979 820 struct fown_struct *fown;
f4985dc7
AM
821 unsigned long flags;
822
1da177e4
LT
823 if (fa->magic != FASYNC_MAGIC) {
824 printk(KERN_ERR "kill_fasync: bad magic number in "
825 "fasync_struct!\n");
826 return;
827 }
f4985dc7 828 spin_lock_irqsave(&fa->fa_lock, flags);
989a2979
ED
829 if (fa->fa_file) {
830 fown = &fa->fa_file->f_owner;
831 /* Don't send SIGURG to processes which have not set a
832 queued signum: SIGURG has its own default signalling
833 mechanism. */
834 if (!(sig == SIGURG && fown->signum == 0))
835 send_sigio(fown, fa->fa_fd, band);
836 }
f4985dc7 837 spin_unlock_irqrestore(&fa->fa_lock, flags);
989a2979 838 fa = rcu_dereference(fa->fa_next);
1da177e4
LT
839 }
840}
841
1da177e4
LT
842void kill_fasync(struct fasync_struct **fp, int sig, int band)
843{
844 /* First a quick test without locking: usually
845 * the list is empty.
846 */
847 if (*fp) {
989a2979
ED
848 rcu_read_lock();
849 kill_fasync_rcu(rcu_dereference(*fp), sig, band);
850 rcu_read_unlock();
1da177e4
LT
851 }
852}
853EXPORT_SYMBOL(kill_fasync);
854
454eedb8 855static int __init fcntl_init(void)
1da177e4 856{
3ab04d5c
JB
857 /*
858 * Please add new bits here to ensure allocation uniqueness.
859 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
860 * is defined as O_NONBLOCK on some platforms and not on others.
861 */
1abf0c71 862 BUILD_BUG_ON(19 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
454eedb8
WF
863 O_RDONLY | O_WRONLY | O_RDWR |
864 O_CREAT | O_EXCL | O_NOCTTY |
3ab04d5c 865 O_TRUNC | O_APPEND | /* O_NONBLOCK | */
454eedb8
WF
866 __O_SYNC | O_DSYNC | FASYNC |
867 O_DIRECT | O_LARGEFILE | O_DIRECTORY |
868 O_NOFOLLOW | O_NOATIME | O_CLOEXEC |
1abf0c71 869 __FMODE_EXEC | O_PATH
454eedb8
WF
870 ));
871
1da177e4 872 fasync_cache = kmem_cache_create("fasync_cache",
20c2df83 873 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
1da177e4
LT
874 return 0;
875}
876
454eedb8 877module_init(fcntl_init)