4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/init.h>
9 #include <linux/file.h>
10 #include <linux/dnotify.h>
11 #include <linux/smp_lock.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/security.h>
17 #include <asm/siginfo.h>
18 #include <asm/uaccess.h>
20 extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
21 extern int fcntl_getlease(struct file *filp);
23 void 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, 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)
112 error = expand_files(files, newfd);
117 * If we needed to expand the fs array we
118 * might have blocked - try again.
123 if (start <= files->next_fd)
124 files->next_fd = newfd + 1;
132 static int dupfd(struct file *file, int start)
134 struct files_struct * files = current->files;
137 spin_lock(&files->file_lock);
138 fd = locate_fd(files, file, start);
140 FD_SET(fd, files->open_fds);
141 FD_CLR(fd, files->close_on_exec);
142 spin_unlock(&files->file_lock);
143 fd_install(fd, file);
145 spin_unlock(&files->file_lock);
152 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
155 struct file * file, *tofree;
156 struct files_struct * files = current->files;
158 spin_lock(&files->file_lock);
159 if (!(file = fcheck(oldfd)))
165 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
167 get_file(file); /* We are now finished with oldfd */
169 err = expand_files(files, newfd);
173 /* To avoid races with open() and dup(), we will mark the fd as
174 * in-use in the open-file bitmap throughout the entire dup2()
175 * process. This is quite safe: do_close() uses the fd array
176 * entry, not the bitmap, to decide what work needs to be
178 /* Doesn't work. open() might be there first. --AV */
180 /* Yes. It's a race. In user space. Nothing sane to do */
182 tofree = files->fd[newfd];
183 if (!tofree && FD_ISSET(newfd, files->open_fds))
186 files->fd[newfd] = file;
187 FD_SET(newfd, files->open_fds);
188 FD_CLR(newfd, files->close_on_exec);
189 spin_unlock(&files->file_lock);
192 filp_close(tofree, files);
197 spin_unlock(&files->file_lock);
201 spin_unlock(&files->file_lock);
206 asmlinkage long sys_dup(unsigned int fildes)
209 struct file * file = fget(fildes);
212 ret = dupfd(file, 0);
216 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT)
218 static int setfl(int fd, struct file * filp, unsigned long arg)
220 struct inode * inode = filp->f_dentry->d_inode;
223 /* O_APPEND cannot be cleared if the file is marked as append-only */
224 if (!(arg & O_APPEND) && IS_APPEND(inode))
227 /* required for strict SunOS emulation */
228 if (O_NONBLOCK != O_NDELAY)
232 if (arg & O_DIRECT) {
233 if (!inode->i_mapping || !inode->i_mapping->a_ops ||
234 !inode->i_mapping->a_ops->direct_IO)
239 if ((arg ^ filp->f_flags) & FASYNC) {
240 if (filp->f_op && filp->f_op->fasync) {
241 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
247 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
253 static void f_modown(struct file *filp, unsigned long pid,
254 uid_t uid, uid_t euid, int force)
256 write_lock_irq(&filp->f_owner.lock);
257 if (force || !filp->f_owner.pid) {
258 filp->f_owner.pid = pid;
259 filp->f_owner.uid = uid;
260 filp->f_owner.euid = euid;
262 write_unlock_irq(&filp->f_owner.lock);
265 int f_setown(struct file *filp, unsigned long arg, int force)
269 err = security_file_set_fowner(filp);
273 f_modown(filp, arg, current->uid, current->euid, force);
277 void f_delown(struct file *filp)
279 f_modown(filp, 0, 0, 0, 1);
282 static long do_fcntl(unsigned int fd, unsigned int cmd,
283 unsigned long arg, struct file * filp)
291 err = dupfd(filp, arg);
295 err = get_close_on_exec(fd);
299 set_close_on_exec(fd, arg&1);
305 err = setfl(fd, filp, arg);
308 err = fcntl_getlk(filp, (struct flock *) arg);
312 err = fcntl_setlk(filp, cmd, (struct flock *) arg);
316 * XXX If f_owner is a process group, the
317 * negative return value will get converted
318 * into an error. Oops. If we keep the
319 * current syscall conventions, the only way
320 * to fix this will be in libc.
322 err = filp->f_owner.pid;
323 force_successful_syscall_return();
326 err = f_setown(filp, arg, 1);
329 err = filp->f_owner.signum;
332 /* arg == 0 restores default behaviour. */
333 if (arg < 0 || arg > _NSIG) {
337 filp->f_owner.signum = arg;
340 err = fcntl_getlease(filp);
343 err = fcntl_setlease(fd, filp, arg);
346 err = fcntl_dirnotify(fd, filp, arg);
355 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
364 err = security_file_fcntl(filp, cmd, arg);
370 err = do_fcntl(fd, cmd, arg, filp);
377 #if BITS_PER_LONG == 32
378 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
388 err = security_file_fcntl(filp, cmd, arg);
397 err = fcntl_getlk64(filp, (struct flock64 *) arg);
401 err = fcntl_setlk64(filp, cmd, (struct flock64 *) arg);
404 err = do_fcntl(fd, cmd, arg, filp);
413 /* Table to convert sigio signal codes into poll band bitmaps */
415 static long band_table[NSIGPOLL] = {
416 POLLIN | POLLRDNORM, /* POLL_IN */
417 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
418 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
419 POLLERR, /* POLL_ERR */
420 POLLPRI | POLLRDBAND, /* POLL_PRI */
421 POLLHUP | POLLERR /* POLL_HUP */
424 static inline int sigio_perm(struct task_struct *p,
425 struct fown_struct *fown)
427 return ((fown->euid == 0) ||
428 (fown->euid == p->suid) || (fown->euid == p->uid) ||
429 (fown->uid == p->suid) || (fown->uid == p->uid));
432 static void send_sigio_to_task(struct task_struct *p,
433 struct fown_struct *fown,
437 if (!sigio_perm(p, fown))
440 if (security_file_send_sigiotask(p, fown, fd, reason))
443 switch (fown->signum) {
446 /* Queue a rt signal with the appropriate fd as its
447 value. We use SI_SIGIO as the source, not
448 SI_KERNEL, since kernel signals always get
449 delivered even if we can't queue. Failure to
450 queue in this case _should_ be reported; we fall
451 back to SIGIO in that case. --sct */
452 si.si_signo = fown->signum;
455 /* Make sure we are called with one of the POLL_*
456 reasons, otherwise we could leak kernel stack into
458 if ((reason & __SI_MASK) != __SI_POLL)
460 if (reason - POLL_IN >= NSIGPOLL)
463 si.si_band = band_table[reason - POLL_IN];
465 if (!send_sig_info(fown->signum, &si, p))
467 /* fall-through: fall back on the old plain SIGIO signal */
469 send_group_sig_info(SIGIO, SEND_SIG_PRIV, p);
473 void send_sigio(struct fown_struct *fown, int fd, int band)
475 struct task_struct *p;
478 read_lock(&fown->lock);
481 goto out_unlock_fown;
483 read_lock(&tasklist_lock);
485 p = find_task_by_pid(pid);
487 send_sigio_to_task(p, fown, fd, band);
492 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
493 send_sigio_to_task(p, fown, fd, band);
496 read_unlock(&tasklist_lock);
498 read_unlock(&fown->lock);
501 static void send_sigurg_to_task(struct task_struct *p,
502 struct fown_struct *fown)
504 if (sigio_perm(p, fown))
505 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
508 int send_sigurg(struct fown_struct *fown)
510 struct task_struct *p;
513 read_lock(&fown->lock);
516 goto out_unlock_fown;
520 read_lock(&tasklist_lock);
522 p = find_task_by_pid(pid);
524 send_sigurg_to_task(p, fown);
529 for_each_task_pid(-pid, PIDTYPE_PGID, p, l, pidptr) {
530 send_sigurg_to_task(p, fown);
533 read_unlock(&tasklist_lock);
535 read_unlock(&fown->lock);
539 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
540 static kmem_cache_t *fasync_cache;
543 * fasync_helper() is used by some character device drivers (mainly mice)
544 * to set up the fasync queue. It returns negative on error, 0 if it did
545 * no changes and positive if it added/deleted the entry.
547 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
549 struct fasync_struct *fa, **fp;
550 struct fasync_struct *new = NULL;
554 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
558 write_lock_irq(&fasync_lock);
559 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
560 if (fa->fa_file == filp) {
563 kmem_cache_free(fasync_cache, new);
566 kmem_cache_free(fasync_cache, fa);
574 new->magic = FASYNC_MAGIC;
577 new->fa_next = *fapp;
582 write_unlock_irq(&fasync_lock);
586 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
589 struct fown_struct * fown;
590 if (fa->magic != FASYNC_MAGIC) {
591 printk(KERN_ERR "kill_fasync: bad magic number in "
595 fown = &fa->fa_file->f_owner;
596 /* Don't send SIGURG to processes which have not set a
597 queued signum: SIGURG has its own default signalling
599 if (!(sig == SIGURG && fown->signum == 0))
600 send_sigio(fown, fa->fa_fd, band);
605 void kill_fasync(struct fasync_struct **fp, int sig, int band)
607 read_lock(&fasync_lock);
608 __kill_fasync(*fp, sig, band);
609 read_unlock(&fasync_lock);
612 static int __init fasync_init(void)
614 fasync_cache = kmem_cache_create("fasync_cache",
615 sizeof(struct fasync_struct), 0, 0, NULL, NULL);
617 panic("cannot create fasync slab cache");
621 module_init(fasync_init)
623 EXPORT_SYMBOL(f_setown);
624 EXPORT_SYMBOL(f_delown);