4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/syscalls.h>
8 #include <linux/init.h>
11 #include <linux/file.h>
12 #include <linux/fdtable.h>
13 #include <linux/capability.h>
14 #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>
20 #include <linux/signal.h>
21 #include <linux/rcupdate.h>
22 #include <linux/pid_namespace.h>
25 #include <asm/siginfo.h>
26 #include <asm/uaccess.h>
28 void set_close_on_exec(unsigned int fd, int flag)
30 struct files_struct *files = current->files;
32 spin_lock(&files->file_lock);
33 fdt = files_fdtable(files);
35 FD_SET(fd, fdt->close_on_exec);
37 FD_CLR(fd, fdt->close_on_exec);
38 spin_unlock(&files->file_lock);
41 static int get_close_on_exec(unsigned int fd)
43 struct files_struct *files = current->files;
47 fdt = files_fdtable(files);
48 res = FD_ISSET(fd, fdt->close_on_exec);
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.
59 static int locate_fd(unsigned int orig_start, int cloexec)
61 struct files_struct *files = current->files;
67 spin_lock(&files->file_lock);
70 if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
74 fdt = files_fdtable(files);
76 * Someone might have closed fd's in the range
77 * orig_start..fdt->next_fd
80 if (start < files->next_fd)
81 start = files->next_fd;
84 if (start < fdt->max_fds)
85 newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
89 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
92 error = expand_files(files, newfd);
97 * If we needed to expand the fs array we
98 * might have blocked - try again.
103 if (start <= files->next_fd)
104 files->next_fd = newfd + 1;
106 FD_SET(newfd, fdt->open_fds);
108 FD_SET(newfd, fdt->close_on_exec);
110 FD_CLR(newfd, fdt->close_on_exec);
114 spin_unlock(&files->file_lock);
118 static int dupfd(struct file *file, unsigned int start, int cloexec)
120 int fd = locate_fd(start, cloexec);
122 fd_install(fd, file);
129 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
132 struct file * file, *tofree;
133 struct files_struct * files = current->files;
136 spin_lock(&files->file_lock);
137 if (!(file = fcheck(oldfd)))
143 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
145 get_file(file); /* We are now finished with oldfd */
147 err = expand_files(files, newfd);
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
156 /* Doesn't work. open() might be there first. --AV */
158 /* Yes. It's a race. In user space. Nothing sane to do */
160 fdt = files_fdtable(files);
161 tofree = fdt->fd[newfd];
162 if (!tofree && FD_ISSET(newfd, fdt->open_fds))
165 rcu_assign_pointer(fdt->fd[newfd], file);
166 FD_SET(newfd, fdt->open_fds);
167 FD_CLR(newfd, fdt->close_on_exec);
168 spin_unlock(&files->file_lock);
171 filp_close(tofree, files);
176 spin_unlock(&files->file_lock);
180 spin_unlock(&files->file_lock);
185 asmlinkage long sys_dup(unsigned int fildes)
188 struct file * file = fget(fildes);
191 ret = dupfd(file, 0, 0);
195 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
197 static int setfl(int fd, struct file * filp, unsigned long arg)
199 struct inode * inode = filp->f_path.dentry->d_inode;
203 * O_APPEND cannot be cleared if the file is marked as append-only
204 * and the file is open for write.
206 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
209 /* O_NOATIME can only be set by the owner or superuser */
210 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
211 if (!is_owner_or_cap(inode))
214 /* required for strict SunOS emulation */
215 if (O_NONBLOCK != O_NDELAY)
219 if (arg & O_DIRECT) {
220 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
221 !filp->f_mapping->a_ops->direct_IO)
225 if (filp->f_op && filp->f_op->check_flags)
226 error = filp->f_op->check_flags(arg);
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);
239 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
245 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
246 uid_t uid, uid_t euid, int force)
248 write_lock_irq(&filp->f_owner.lock);
249 if (force || !filp->f_owner.pid) {
250 put_pid(filp->f_owner.pid);
251 filp->f_owner.pid = get_pid(pid);
252 filp->f_owner.pid_type = type;
253 filp->f_owner.uid = uid;
254 filp->f_owner.euid = euid;
256 write_unlock_irq(&filp->f_owner.lock);
259 int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
264 err = security_file_set_fowner(filp);
268 f_modown(filp, pid, type, current->uid, current->euid, force);
271 EXPORT_SYMBOL(__f_setown);
273 int f_setown(struct file *filp, unsigned long arg, int force)
285 pid = find_vpid(who);
286 result = __f_setown(filp, pid, type, force);
290 EXPORT_SYMBOL(f_setown);
292 void f_delown(struct file *filp)
294 f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
297 pid_t f_getown(struct file *filp)
300 read_lock(&filp->f_owner.lock);
301 pid = pid_vnr(filp->f_owner.pid);
302 if (filp->f_owner.pid_type == PIDTYPE_PGID)
304 read_unlock(&filp->f_owner.lock);
308 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
315 case F_DUPFD_CLOEXEC:
317 err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
320 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
324 set_close_on_exec(fd, arg & FD_CLOEXEC);
330 err = setfl(fd, filp, arg);
333 err = fcntl_getlk(filp, (struct flock __user *) arg);
337 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
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.
347 err = f_getown(filp);
348 force_successful_syscall_return();
351 err = f_setown(filp, arg, 1);
354 err = filp->f_owner.signum;
357 /* arg == 0 restores default behaviour. */
358 if (!valid_signal(arg)) {
362 filp->f_owner.signum = arg;
365 err = fcntl_getlease(filp);
368 err = fcntl_setlease(fd, filp, arg);
371 err = fcntl_dirnotify(fd, filp, arg);
379 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
388 err = security_file_fcntl(filp, cmd, arg);
394 err = do_fcntl(fd, cmd, arg, filp);
401 #if BITS_PER_LONG == 32
402 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
412 err = security_file_fcntl(filp, cmd, arg);
421 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
425 err = fcntl_setlk64(fd, filp, cmd,
426 (struct flock64 __user *) arg);
429 err = do_fcntl(fd, cmd, arg, filp);
438 /* Table to convert sigio signal codes into poll band bitmaps */
440 static const long band_table[NSIGPOLL] = {
441 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 */
449 static inline int sigio_perm(struct task_struct *p,
450 struct fown_struct *fown, int sig)
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));
458 static void send_sigio_to_task(struct task_struct *p,
459 struct fown_struct *fown,
463 if (!sigio_perm(p, fown, fown->signum))
466 switch (fown->signum) {
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;
478 /* Make sure we are called with one of the POLL_*
479 reasons, otherwise we could leak kernel stack into
481 BUG_ON((reason & __SI_MASK) != __SI_POLL);
482 if (reason - POLL_IN >= NSIGPOLL)
485 si.si_band = band_table[reason - POLL_IN];
487 if (!group_send_sig_info(fown->signum, &si, p))
489 /* fall-through: fall back on the old plain SIGIO signal */
491 group_send_sig_info(SIGIO, SEND_SIG_PRIV, p);
495 void send_sigio(struct fown_struct *fown, int fd, int band)
497 struct task_struct *p;
501 read_lock(&fown->lock);
502 type = fown->pid_type;
505 goto out_unlock_fown;
507 read_lock(&tasklist_lock);
508 do_each_pid_task(pid, type, p) {
509 send_sigio_to_task(p, fown, fd, band);
510 } while_each_pid_task(pid, type, p);
511 read_unlock(&tasklist_lock);
513 read_unlock(&fown->lock);
516 static void send_sigurg_to_task(struct task_struct *p,
517 struct fown_struct *fown)
519 if (sigio_perm(p, fown, SIGURG))
520 group_send_sig_info(SIGURG, SEND_SIG_PRIV, p);
523 int send_sigurg(struct fown_struct *fown)
525 struct task_struct *p;
530 read_lock(&fown->lock);
531 type = fown->pid_type;
534 goto out_unlock_fown;
538 read_lock(&tasklist_lock);
539 do_each_pid_task(pid, type, p) {
540 send_sigurg_to_task(p, fown);
541 } while_each_pid_task(pid, type, p);
542 read_unlock(&tasklist_lock);
544 read_unlock(&fown->lock);
548 static DEFINE_RWLOCK(fasync_lock);
549 static struct kmem_cache *fasync_cache __read_mostly;
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.
556 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
558 struct fasync_struct *fa, **fp;
559 struct fasync_struct *new = NULL;
563 new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
567 write_lock_irq(&fasync_lock);
568 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
569 if (fa->fa_file == filp) {
572 kmem_cache_free(fasync_cache, new);
575 kmem_cache_free(fasync_cache, fa);
583 new->magic = FASYNC_MAGIC;
586 new->fa_next = *fapp;
591 write_unlock_irq(&fasync_lock);
595 EXPORT_SYMBOL(fasync_helper);
597 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
600 struct fown_struct * fown;
601 if (fa->magic != FASYNC_MAGIC) {
602 printk(KERN_ERR "kill_fasync: bad magic number in "
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
610 if (!(sig == SIGURG && fown->signum == 0))
611 send_sigio(fown, fa->fa_fd, band);
616 EXPORT_SYMBOL(__kill_fasync);
618 void kill_fasync(struct fasync_struct **fp, int sig, int band)
620 /* First a quick test without locking: usually
624 read_lock(&fasync_lock);
625 /* reread *fp after obtaining the lock */
626 __kill_fasync(*fp, sig, band);
627 read_unlock(&fasync_lock);
630 EXPORT_SYMBOL(kill_fasync);
632 static int __init fasync_init(void)
634 fasync_cache = kmem_cache_create("fasync_cache",
635 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
639 module_init(fasync_init)