4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/init.h>
10 #include <linux/file.h>
11 #include <linux/dnotify.h>
12 #include <linux/smp_lock.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/security.h>
16 #include <linux/ptrace.h>
17 #include <linux/vs_limit.h>
20 #include <asm/siginfo.h>
21 #include <asm/uaccess.h>
23 void fastcall set_close_on_exec(unsigned int fd, int flag)
25 struct files_struct *files = current->files;
26 spin_lock(&files->file_lock);
28 FD_SET(fd, files->close_on_exec);
30 FD_CLR(fd, files->close_on_exec);
31 spin_unlock(&files->file_lock);
34 static inline int get_close_on_exec(unsigned int fd)
36 struct files_struct *files = current->files;
38 spin_lock(&files->file_lock);
39 res = FD_ISSET(fd, files->close_on_exec);
40 spin_unlock(&files->file_lock);
45 /* Expand files. Return <0 on error; 0 nothing done; 1 files expanded,
46 * we may have blocked.
48 * Should be called with the files->file_lock spinlock held for write.
50 static int expand_files(struct files_struct *files, int nr)
54 printk (KERN_ERR "%s %d: nr = %d\n", __FUNCTION__, current->pid, nr);
57 if (nr >= files->max_fdset) {
59 if ((err = expand_fdset(files, nr)))
62 if (nr >= files->max_fds) {
64 if ((err = expand_fd_array(files, nr)))
71 printk (KERN_ERR "%s %d: return %d\n", __FUNCTION__, current->pid, err);
77 * locate_fd finds a free file descriptor in the open_fds fdset,
78 * expanding the fd arrays if necessary. Must be called with the
79 * file_lock held for write.
82 static int locate_fd(struct files_struct *files,
83 struct file *file, unsigned int orig_start)
90 if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
95 * Someone might have closed fd's in the range
96 * orig_start..files->next_fd
99 if (start < files->next_fd)
100 start = files->next_fd;
103 if (start < files->max_fdset) {
104 newfd = find_next_zero_bit(files->open_fds->fds_bits,
105 files->max_fdset, start);
109 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
111 if (!vx_files_avail(1))
114 error = expand_files(files, newfd);
119 * If we needed to expand the fs array we
120 * might have blocked - try again.
125 if (start <= files->next_fd)
126 files->next_fd = newfd + 1;
134 static int dupfd(struct file *file, unsigned int start)
136 struct files_struct * files = current->files;
139 spin_lock(&files->file_lock);
140 fd = locate_fd(files, file, start);
142 FD_SET(fd, files->open_fds);
143 FD_CLR(fd, files->close_on_exec);
144 spin_unlock(&files->file_lock);
146 fd_install(fd, file);
148 spin_unlock(&files->file_lock);
155 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
158 struct file * file, *tofree;
159 struct files_struct * files = current->files;
161 spin_lock(&files->file_lock);
162 if (!(file = fcheck(oldfd)))
168 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
170 get_file(file); /* We are now finished with oldfd */
172 err = expand_files(files, newfd);
176 /* To avoid races with open() and dup(), we will mark the fd as
177 * in-use in the open-file bitmap throughout the entire dup2()
178 * process. This is quite safe: do_close() uses the fd array
179 * entry, not the bitmap, to decide what work needs to be
181 /* Doesn't work. open() might be there first. --AV */
183 /* Yes. It's a race. In user space. Nothing sane to do */
185 tofree = files->fd[newfd];
186 if (!tofree && FD_ISSET(newfd, files->open_fds))
189 files->fd[newfd] = file;
190 FD_SET(newfd, files->open_fds);
191 FD_CLR(newfd, files->close_on_exec);
192 spin_unlock(&files->file_lock);
193 vx_openfd_inc(newfd);
196 filp_close(tofree, files);
201 spin_unlock(&files->file_lock);
205 spin_unlock(&files->file_lock);
210 asmlinkage long sys_dup(unsigned int fildes)
213 struct file * file = fget(fildes);
216 ret = dupfd(file, 0);
220 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
222 static int setfl(int fd, struct file * filp, unsigned long arg)
224 struct inode * inode = filp->f_dentry->d_inode;
227 /* O_APPEND cannot be cleared if the file is marked as append-only */
228 if (!(arg & O_APPEND) && IS_APPEND(inode))
231 /* O_NOATIME can only be set by the owner or superuser */
232 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
233 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
236 /* required for strict SunOS emulation */
237 if (O_NONBLOCK != O_NDELAY)
241 if (arg & O_DIRECT) {
242 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
243 !filp->f_mapping->a_ops->direct_IO)
248 if ((arg ^ filp->f_flags) & FASYNC) {
249 if (filp->f_op && filp->f_op->fasync) {
250 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
256 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
262 static void f_modown(struct file *filp, unsigned long pid,
263 uid_t uid, uid_t euid, int force)
265 write_lock_irq(&filp->f_owner.lock);
266 if (force || !filp->f_owner.pid) {
267 filp->f_owner.pid = pid;
268 filp->f_owner.uid = uid;
269 filp->f_owner.euid = euid;
271 write_unlock_irq(&filp->f_owner.lock);
274 int f_setown(struct file *filp, unsigned long arg, int force)
278 err = security_file_set_fowner(filp);
282 f_modown(filp, arg, current->uid, current->euid, force);
286 EXPORT_SYMBOL(f_setown);
288 void f_delown(struct file *filp)
290 f_modown(filp, 0, 0, 0, 1);
293 EXPORT_SYMBOL(f_delown);
295 long generic_file_fcntl(int fd, unsigned int cmd,
296 unsigned long arg, struct file *filp)
303 err = dupfd(filp, arg);
306 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
310 set_close_on_exec(fd, arg & FD_CLOEXEC);
316 err = setfl(fd, filp, arg);
319 err = fcntl_getlk(filp, (struct flock __user *) arg);
323 err = fcntl_setlk(filp, cmd, (struct flock __user *) arg);
327 * XXX If f_owner is a process group, the
328 * negative return value will get converted
329 * into an error. Oops. If we keep the
330 * current syscall conventions, the only way
331 * to fix this will be in libc.
333 err = filp->f_owner.pid;
334 force_successful_syscall_return();
337 err = f_setown(filp, arg, 1);
340 err = filp->f_owner.signum;
343 /* arg == 0 restores default behaviour. */
344 if (arg < 0 || arg > _NSIG) {
348 filp->f_owner.signum = arg;
351 err = fcntl_getlease(filp);
354 err = fcntl_setlease(fd, filp, arg);
357 err = fcntl_dirnotify(fd, filp, arg);
364 EXPORT_SYMBOL(generic_file_fcntl);
366 static long do_fcntl(int fd, unsigned int cmd,
367 unsigned long arg, struct file *filp)
369 if (filp->f_op && filp->f_op->fcntl)
370 return filp->f_op->fcntl(fd, cmd, arg, filp);
371 return generic_file_fcntl(fd, cmd, arg, filp);
374 asmlinkage long sys_fcntl(int fd, unsigned int cmd, unsigned long arg)
383 err = security_file_fcntl(filp, cmd, arg);
389 err = do_fcntl(fd, cmd, arg, filp);
396 #if BITS_PER_LONG == 32
397 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
407 err = security_file_fcntl(filp, cmd, arg);
416 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
420 err = fcntl_setlk64(filp, cmd, (struct flock64 __user *) arg);
423 err = do_fcntl(fd, cmd, arg, filp);
432 /* Table to convert sigio signal codes into poll band bitmaps */
434 static long band_table[NSIGPOLL] = {
435 POLLIN | POLLRDNORM, /* POLL_IN */
436 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
437 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
438 POLLERR, /* POLL_ERR */
439 POLLPRI | POLLRDBAND, /* POLL_PRI */
440 POLLHUP | POLLERR /* POLL_HUP */
443 static inline int sigio_perm(struct task_struct *p,
444 struct fown_struct *fown)
446 return ((fown->euid == 0) ||
447 (fown->euid == p->suid) || (fown->euid == p->uid) ||
448 (fown->uid == p->suid) || (fown->uid == p->uid));
451 static void send_sigio_to_task(struct task_struct *p,
452 struct fown_struct *fown,
456 if (!sigio_perm(p, fown))
459 if (security_file_send_sigiotask(p, fown, fd, reason))
462 switch (fown->signum) {
465 /* Queue a rt signal with the appropriate fd as its
466 value. We use SI_SIGIO as the source, not
467 SI_KERNEL, since kernel signals always get
468 delivered even if we can't queue. Failure to
469 queue in this case _should_ be reported; we fall
470 back to SIGIO in that case. --sct */
471 si.si_signo = fown->signum;
474 /* Make sure we are called with one of the POLL_*
475 reasons, otherwise we could leak kernel stack into
477 if ((reason & __SI_MASK) != __SI_POLL)
479 if (reason - POLL_IN >= NSIGPOLL)
482 si.si_band = band_table[reason - POLL_IN];
484 if (!send_sig_info(fown->signum, &si, p))
486 /* fall-through: fall back on the old plain SIGIO signal */
488 send_group_sig_info(SIGIO, SEND_SIG_PRIV, p);
492 void send_sigio(struct fown_struct *fown, int fd, int band)
494 struct task_struct *p;
497 read_lock(&fown->lock);
500 goto out_unlock_fown;
502 read_lock(&tasklist_lock);
504 p = find_task_by_pid(pid);
506 send_sigio_to_task(p, fown, fd, band);
511 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
512 send_sigio_to_task(p, fown, fd, band);
515 read_unlock(&tasklist_lock);
517 read_unlock(&fown->lock);
520 static void send_sigurg_to_task(struct task_struct *p,
521 struct fown_struct *fown)
523 if (sigio_perm(p, fown))
524 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
527 int send_sigurg(struct fown_struct *fown)
529 struct task_struct *p;
532 read_lock(&fown->lock);
535 goto out_unlock_fown;
539 read_lock(&tasklist_lock);
541 p = find_task_by_pid(pid);
543 send_sigurg_to_task(p, fown);
548 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
549 send_sigurg_to_task(p, fown);
552 read_unlock(&tasklist_lock);
554 read_unlock(&fown->lock);
558 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
559 static kmem_cache_t *fasync_cache;
562 * fasync_helper() is used by some character device drivers (mainly mice)
563 * to set up the fasync queue. It returns negative on error, 0 if it did
564 * no changes and positive if it added/deleted the entry.
566 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
568 struct fasync_struct *fa, **fp;
569 struct fasync_struct *new = NULL;
573 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
577 write_lock_irq(&fasync_lock);
578 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
579 if (fa->fa_file == filp) {
582 kmem_cache_free(fasync_cache, new);
585 kmem_cache_free(fasync_cache, fa);
593 new->magic = FASYNC_MAGIC;
596 new->fa_next = *fapp;
601 write_unlock_irq(&fasync_lock);
605 EXPORT_SYMBOL(fasync_helper);
607 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
610 struct fown_struct * fown;
611 if (fa->magic != FASYNC_MAGIC) {
612 printk(KERN_ERR "kill_fasync: bad magic number in "
616 fown = &fa->fa_file->f_owner;
617 /* Don't send SIGURG to processes which have not set a
618 queued signum: SIGURG has its own default signalling
620 if (!(sig == SIGURG && fown->signum == 0))
621 send_sigio(fown, fa->fa_fd, band);
626 EXPORT_SYMBOL(__kill_fasync);
628 void kill_fasync(struct fasync_struct **fp, int sig, int band)
630 /* First a quick test without locking: usually
634 read_lock(&fasync_lock);
635 /* reread *fp after obtaining the lock */
636 __kill_fasync(*fp, sig, band);
637 read_unlock(&fasync_lock);
640 EXPORT_SYMBOL(kill_fasync);
642 static int __init fasync_init(void)
644 fasync_cache = kmem_cache_create("fasync_cache",
645 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL, NULL);
649 module_init(fasync_init)