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>
14 #include <linux/ptrace.h>
17 #include <asm/siginfo.h>
18 #include <asm/uaccess.h>
20 extern int sock_fcntl (struct file *, unsigned int cmd, unsigned long arg);
21 extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
22 extern int fcntl_getlease(struct file *filp);
24 /* Expand files. Return <0 on error; 0 nothing done; 1 files expanded,
25 * we may have blocked.
27 * Should be called with the files->file_lock spinlock held for write.
29 static int expand_files(struct files_struct *files, int nr)
33 printk (KERN_ERR __FUNCTION__ " %d: nr = %d\n", current->pid, nr);
36 if (nr >= files->max_fdset) {
38 if ((err = expand_fdset(files, nr)))
41 if (nr >= files->max_fds) {
43 if ((err = expand_fd_array(files, nr)))
50 printk (KERN_ERR __FUNCTION__ " %d: return %d\n", current->pid, err);
56 * locate_fd finds a free file descriptor in the open_fds fdset,
57 * expanding the fd arrays if necessary. The files write lock will be
58 * held on exit to ensure that the fd can be entered atomically.
61 static int locate_fd(struct files_struct *files,
62 struct file *file, int orig_start)
68 write_lock(&files->file_lock);
71 if (orig_start >= current->rlim[RLIMIT_NOFILE].rlim_cur)
76 * Someone might have closed fd's in the range
77 * orig_start..files->next_fd
80 if (start < files->next_fd)
81 start = files->next_fd;
84 if (start < files->max_fdset) {
85 newfd = find_next_zero_bit(files->open_fds->fds_bits,
86 files->max_fdset, start);
90 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
93 error = expand_files(files, newfd);
98 * If we needed to expand the fs array we
99 * might have blocked - try again.
104 if (start <= files->next_fd)
105 files->next_fd = newfd + 1;
113 static inline void allocate_fd(struct files_struct *files,
114 struct file *file, int fd)
116 FD_SET(fd, files->open_fds);
117 FD_CLR(fd, files->close_on_exec);
118 write_unlock(&files->file_lock);
119 fd_install(fd, file);
122 static int dupfd(struct file *file, int start)
124 struct files_struct * files = current->files;
127 ret = locate_fd(files, file, start);
130 allocate_fd(files, file, ret);
134 write_unlock(&files->file_lock);
139 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
142 struct file * file, *tofree;
143 struct files_struct * files = current->files;
145 write_lock(&files->file_lock);
146 if (!(file = fcheck(oldfd)))
152 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
154 get_file(file); /* We are now finished with oldfd */
156 err = expand_files(files, newfd);
160 /* To avoid races with open() and dup(), we will mark the fd as
161 * in-use in the open-file bitmap throughout the entire dup2()
162 * process. This is quite safe: do_close() uses the fd array
163 * entry, not the bitmap, to decide what work needs to be
165 /* Doesn't work. open() might be there first. --AV */
167 /* Yes. It's a race. In user space. Nothing sane to do */
169 tofree = files->fd[newfd];
170 if (!tofree && FD_ISSET(newfd, files->open_fds))
173 files->fd[newfd] = file;
174 FD_SET(newfd, files->open_fds);
175 FD_CLR(newfd, files->close_on_exec);
176 write_unlock(&files->file_lock);
179 filp_close(tofree, files);
184 write_unlock(&files->file_lock);
188 write_unlock(&files->file_lock);
193 asmlinkage long sys_dup(unsigned int fildes)
196 struct file * file = fget(fildes);
199 ret = dupfd(file, 0);
203 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT)
205 static int setfl(int fd, struct file * filp, unsigned long arg)
207 struct inode * inode = filp->f_dentry->d_inode;
211 * In the case of an append-only file, O_APPEND
214 if (!(arg & O_APPEND) && IS_APPEND(inode))
217 /* Did FASYNC state change? */
218 if ((arg ^ filp->f_flags) & FASYNC) {
219 if (filp->f_op && filp->f_op->fasync) {
220 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
226 if (arg & O_DIRECT) {
228 * alloc_kiovec() can sleep and we are only serialized by
229 * the big kernel lock here, so abuse the i_sem to serialize
230 * this case too. We of course wouldn't need to go deep down
231 * to the inode layer, we could stay at the file layer, but
232 * we don't want to pay for the memory of a semaphore in each
233 * file structure too and we use the inode semaphore that we just
239 error = alloc_kiovec(1, &filp->f_iobuf);
245 /* required for strict SunOS emulation */
246 if (O_NONBLOCK != O_NDELAY)
250 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
254 static long do_fcntl(unsigned int fd, unsigned int cmd,
255 unsigned long arg, struct file * filp)
263 err = dupfd(filp, arg);
267 err = get_close_on_exec(fd);
271 set_close_on_exec(fd, arg&1);
278 err = setfl(fd, filp, arg);
282 err = fcntl_getlk(fd, (struct flock *) arg);
286 err = fcntl_setlk(fd, cmd, (struct flock *) arg);
290 * XXX If f_owner is a process group, the
291 * negative return value will get converted
292 * into an error. Oops. If we keep the
293 * current syscall conventions, the only way
294 * to fix this will be in libc.
296 err = filp->f_owner.pid;
297 force_successful_syscall_return();
301 filp->f_owner.pid = arg;
302 filp->f_owner.uid = current->uid;
303 filp->f_owner.euid = current->euid;
305 if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
306 err = sock_fcntl (filp, F_SETOWN, arg);
310 err = filp->f_owner.signum;
313 /* arg == 0 restores default behaviour. */
314 if (arg < 0 || arg > _NSIG) {
318 filp->f_owner.signum = arg;
321 err = fcntl_getlease(filp);
324 err = fcntl_setlease(fd, filp, arg);
327 err = fcntl_dirnotify(fd, filp, arg);
330 /* sockets need a few special fcntls. */
332 if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
333 err = sock_fcntl (filp, cmd, arg);
340 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
349 err = do_fcntl(fd, cmd, arg, filp);
356 #if BITS_PER_LONG == 32
357 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
369 err = fcntl_getlk64(fd, (struct flock64 *) arg);
372 err = fcntl_setlk64(fd, cmd, (struct flock64 *) arg);
375 err = fcntl_setlk64(fd, cmd, (struct flock64 *) arg);
378 err = do_fcntl(fd, cmd, arg, filp);
387 /* Table to convert sigio signal codes into poll band bitmaps */
389 static long band_table[NSIGPOLL] = {
390 POLLIN | POLLRDNORM, /* POLL_IN */
391 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
392 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
393 POLLERR, /* POLL_ERR */
394 POLLPRI | POLLRDBAND, /* POLL_PRI */
395 POLLHUP | POLLERR /* POLL_HUP */
398 static void send_sigio_to_task(struct task_struct *p,
399 struct fown_struct *fown,
403 if ((fown->euid != 0) &&
404 (fown->euid ^ p->suid) && (fown->euid ^ p->uid) &&
405 (fown->uid ^ p->suid) && (fown->uid ^ p->uid))
407 switch (fown->signum) {
410 /* Queue a rt signal with the appropriate fd as its
411 value. We use SI_SIGIO as the source, not
412 SI_KERNEL, since kernel signals always get
413 delivered even if we can't queue. Failure to
414 queue in this case _should_ be reported; we fall
415 back to SIGIO in that case. --sct */
416 si.si_signo = fown->signum;
419 /* Make sure we are called with one of the POLL_*
420 reasons, otherwise we could leak kernel stack into
422 if ((reason & __SI_MASK) != __SI_POLL)
424 if (reason - POLL_IN >= NSIGPOLL)
427 si.si_band = band_table[reason - POLL_IN];
429 if (!send_sig_info(fown->signum, &si, p))
431 /* fall-through: fall back on the old plain SIGIO signal */
433 send_sig(SIGIO, p, 1);
437 void send_sigio(struct fown_struct *fown, int fd, int band)
439 struct task_struct * p;
442 read_lock(&tasklist_lock);
443 if ( (pid > 0) && (p = find_task_by_pid(pid)) ) {
444 send_sigio_to_task(p, fown, fd, band);
453 send_sigio_to_task(p, fown, fd, band);
456 read_unlock(&tasklist_lock);
459 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
460 static kmem_cache_t *fasync_cache;
463 * fasync_helper() is used by some character device drivers (mainly mice)
464 * to set up the fasync queue. It returns negative on error, 0 if it did
465 * no changes and positive if it added/deleted the entry.
467 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
469 struct fasync_struct *fa, **fp;
470 struct fasync_struct *new = NULL;
474 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
478 write_lock_irq(&fasync_lock);
479 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
480 if (fa->fa_file == filp) {
483 kmem_cache_free(fasync_cache, new);
486 kmem_cache_free(fasync_cache, fa);
494 new->magic = FASYNC_MAGIC;
497 new->fa_next = *fapp;
502 write_unlock_irq(&fasync_lock);
506 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
509 struct fown_struct * fown;
510 if (fa->magic != FASYNC_MAGIC) {
511 printk(KERN_ERR "kill_fasync: bad magic number in "
515 fown = &fa->fa_file->f_owner;
516 /* Don't send SIGURG to processes which have not set a
517 queued signum: SIGURG has its own default signalling
519 if (fown->pid && !(sig == SIGURG && fown->signum == 0))
520 send_sigio(fown, fa->fa_fd, band);
525 void kill_fasync(struct fasync_struct **fp, int sig, int band)
527 read_lock(&fasync_lock);
528 __kill_fasync(*fp, sig, band);
529 read_unlock(&fasync_lock);
532 static int __init fasync_init(void)
534 fasync_cache = kmem_cache_create("fasync_cache",
535 sizeof(struct fasync_struct), 0, 0, NULL, NULL);
537 panic("cannot create fasync slab cache");
541 module_init(fasync_init)