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/iobuf.h>
16 #include <asm/siginfo.h>
17 #include <asm/uaccess.h>
19 extern int sock_fcntl (struct file *, unsigned int cmd, unsigned long arg);
20 extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
21 extern int fcntl_getlease(struct file *filp);
23 /* Expand files. Return <0 on error; 0 nothing done; 1 files expanded,
24 * we may have blocked.
26 * Should be called with the files->file_lock spinlock held for write.
28 static int expand_files(struct files_struct *files, int nr)
32 printk (KERN_ERR __FUNCTION__ " %d: nr = %d\n", current->pid, nr);
35 if (nr >= files->max_fdset) {
37 if ((err = expand_fdset(files, nr)))
40 if (nr >= files->max_fds) {
42 if ((err = expand_fd_array(files, nr)))
49 printk (KERN_ERR __FUNCTION__ " %d: return %d\n", current->pid, err);
55 * locate_fd finds a free file descriptor in the open_fds fdset,
56 * expanding the fd arrays if necessary. The files write lock will be
57 * held on exit to ensure that the fd can be entered atomically.
60 static int locate_fd(struct files_struct *files,
61 struct file *file, int orig_start)
67 write_lock(&files->file_lock);
70 if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
75 * Someone might have closed fd's in the range
76 * orig_start..files->next_fd
79 if (start < files->next_fd)
80 start = files->next_fd;
83 if (start < files->max_fdset) {
84 newfd = find_next_zero_bit(files->open_fds->fds_bits,
85 files->max_fdset, start);
89 if (newfd >= current->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;
112 static inline void allocate_fd(struct files_struct *files,
113 struct file *file, int fd)
115 FD_SET(fd, files->open_fds);
116 FD_CLR(fd, files->close_on_exec);
117 write_unlock(&files->file_lock);
118 fd_install(fd, file);
121 static int dupfd(struct file *file, int start)
123 struct files_struct * files = current->files;
126 ret = locate_fd(files, file, start);
129 allocate_fd(files, file, ret);
133 write_unlock(&files->file_lock);
138 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
141 struct file * file, *tofree;
142 struct files_struct * files = current->files;
144 write_lock(&files->file_lock);
145 if (!(file = fcheck(oldfd)))
151 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
153 get_file(file); /* We are now finished with oldfd */
155 err = expand_files(files, newfd);
159 /* To avoid races with open() and dup(), we will mark the fd as
160 * in-use in the open-file bitmap throughout the entire dup2()
161 * process. This is quite safe: do_close() uses the fd array
162 * entry, not the bitmap, to decide what work needs to be
164 /* Doesn't work. open() might be there first. --AV */
166 /* Yes. It's a race. In user space. Nothing sane to do */
168 tofree = files->fd[newfd];
169 if (!tofree && FD_ISSET(newfd, files->open_fds))
172 files->fd[newfd] = file;
173 FD_SET(newfd, files->open_fds);
174 FD_CLR(newfd, files->close_on_exec);
175 write_unlock(&files->file_lock);
178 filp_close(tofree, files);
183 write_unlock(&files->file_lock);
187 write_unlock(&files->file_lock);
192 asmlinkage long sys_dup(unsigned int fildes)
195 struct file * file = fget(fildes);
198 ret = dupfd(file, 0);
202 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT)
204 static int setfl(int fd, struct file * filp, unsigned long arg)
206 struct inode * inode = filp->f_dentry->d_inode;
210 * In the case of an append-only file, O_APPEND
213 if (!(arg & O_APPEND) && IS_APPEND(inode))
216 /* Did FASYNC state change? */
217 if ((arg ^ filp->f_flags) & FASYNC) {
218 if (filp->f_op && filp->f_op->fasync) {
219 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
225 if (arg & O_DIRECT) {
227 * alloc_kiovec() can sleep and we are only serialized by
228 * the big kernel lock here, so abuse the i_sem to serialize
229 * this case too. We of course wouldn't need to go deep down
230 * to the inode layer, we could stay at the file layer, but
231 * we don't want to pay for the memory of a semaphore in each
232 * file structure too and we use the inode semaphore that we just
238 error = alloc_kiovec(1, &filp->f_iobuf);
244 /* required for strict SunOS emulation */
245 if (O_NONBLOCK != O_NDELAY)
249 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
253 static long do_fcntl(unsigned int fd, unsigned int cmd,
254 unsigned long arg, struct file * filp)
262 err = dupfd(filp, arg);
266 err = get_close_on_exec(fd);
270 set_close_on_exec(fd, arg&1);
277 err = setfl(fd, filp, arg);
281 err = fcntl_getlk(fd, (struct flock *) arg);
285 err = fcntl_setlk(fd, cmd, (struct flock *) arg);
289 * XXX If f_owner is a process group, the
290 * negative return value will get converted
291 * into an error. Oops. If we keep the
292 * current syscall conventions, the only way
293 * to fix this will be in libc.
295 err = filp->f_owner.pid;
299 filp->f_owner.pid = arg;
300 filp->f_owner.uid = current->uid;
301 filp->f_owner.euid = current->euid;
303 if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
304 err = sock_fcntl (filp, F_SETOWN, arg);
308 err = filp->f_owner.signum;
311 /* arg == 0 restores default behaviour. */
312 if (arg < 0 || arg > _NSIG) {
316 filp->f_owner.signum = arg;
319 err = fcntl_getlease(filp);
322 err = fcntl_setlease(fd, filp, arg);
325 err = fcntl_dirnotify(fd, filp, arg);
328 /* sockets need a few special fcntls. */
330 if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
331 err = sock_fcntl (filp, cmd, arg);
338 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
347 err = do_fcntl(fd, cmd, arg, filp);
354 #if BITS_PER_LONG == 32
355 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
367 err = fcntl_getlk64(fd, (struct flock64 *) arg);
370 err = fcntl_setlk64(fd, cmd, (struct flock64 *) arg);
373 err = fcntl_setlk64(fd, cmd, (struct flock64 *) arg);
376 err = do_fcntl(fd, cmd, arg, filp);
385 /* Table to convert sigio signal codes into poll band bitmaps */
387 static long band_table[NSIGPOLL] = {
388 POLLIN | POLLRDNORM, /* POLL_IN */
389 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
390 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
391 POLLERR, /* POLL_ERR */
392 POLLPRI | POLLRDBAND, /* POLL_PRI */
393 POLLHUP | POLLERR /* POLL_HUP */
396 static void send_sigio_to_task(struct task_struct *p,
397 struct fown_struct *fown,
401 if ((fown->euid != 0) &&
402 (fown->euid ^ p->suid) && (fown->euid ^ p->uid) &&
403 (fown->uid ^ p->suid) && (fown->uid ^ p->uid))
405 switch (fown->signum) {
408 /* Queue a rt signal with the appropriate fd as its
409 value. We use SI_SIGIO as the source, not
410 SI_KERNEL, since kernel signals always get
411 delivered even if we can't queue. Failure to
412 queue in this case _should_ be reported; we fall
413 back to SIGIO in that case. --sct */
414 si.si_signo = fown->signum;
417 /* Make sure we are called with one of the POLL_*
418 reasons, otherwise we could leak kernel stack into
420 if ((reason & __SI_MASK) != __SI_POLL)
422 if (reason - POLL_IN >= NSIGPOLL)
425 si.si_band = band_table[reason - POLL_IN];
427 if (!send_sig_info(fown->signum, &si, p))
429 /* fall-through: fall back on the old plain SIGIO signal */
431 send_sig(SIGIO, p, 1);
435 void send_sigio(struct fown_struct *fown, int fd, int band)
437 struct task_struct * p;
440 read_lock(&tasklist_lock);
441 if ( (pid > 0) && (p = find_task_by_pid(pid)) ) {
442 send_sigio_to_task(p, fown, fd, band);
451 send_sigio_to_task(p, fown, fd, band);
454 read_unlock(&tasklist_lock);
457 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
458 static kmem_cache_t *fasync_cache;
461 * fasync_helper() is used by some character device drivers (mainly mice)
462 * to set up the fasync queue. It returns negative on error, 0 if it did
463 * no changes and positive if it added/deleted the entry.
465 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
467 struct fasync_struct *fa, **fp;
468 struct fasync_struct *new = NULL;
472 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
476 write_lock_irq(&fasync_lock);
477 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
478 if (fa->fa_file == filp) {
481 kmem_cache_free(fasync_cache, new);
484 kmem_cache_free(fasync_cache, fa);
492 new->magic = FASYNC_MAGIC;
495 new->fa_next = *fapp;
500 write_unlock_irq(&fasync_lock);
504 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
507 struct fown_struct * fown;
508 if (fa->magic != FASYNC_MAGIC) {
509 printk(KERN_ERR "kill_fasync: bad magic number in "
513 fown = &fa->fa_file->f_owner;
514 /* Don't send SIGURG to processes which have not set a
515 queued signum: SIGURG has its own default signalling
517 if (fown->pid && !(sig == SIGURG && fown->signum == 0))
518 send_sigio(fown, fa->fa_fd, band);
523 void kill_fasync(struct fasync_struct **fp, int sig, int band)
525 read_lock(&fasync_lock);
526 __kill_fasync(*fp, sig, band);
527 read_unlock(&fasync_lock);
530 static int __init fasync_init(void)
532 fasync_cache = kmem_cache_create("fasync_cache",
533 sizeof(struct fasync_struct), 0, 0, NULL, NULL);
535 panic("cannot create fasync slab cache");
539 module_init(fasync_init)