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/dnotify.h>
13 #include <linux/smp_lock.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/security.h>
17 #include <linux/ptrace.h>
18 #include <linux/signal.h>
19 #include <linux/rcupdate.h>
22 #include <asm/siginfo.h>
23 #include <asm/uaccess.h>
25 void fastcall
set_close_on_exec(unsigned int fd
, int flag
)
27 struct files_struct
*files
= current
->files
;
29 spin_lock(&files
->file_lock
);
30 fdt
= files_fdtable(files
);
32 FD_SET(fd
, fdt
->close_on_exec
);
34 FD_CLR(fd
, fdt
->close_on_exec
);
35 spin_unlock(&files
->file_lock
);
38 static inline int get_close_on_exec(unsigned int fd
)
40 struct files_struct
*files
= current
->files
;
44 fdt
= files_fdtable(files
);
45 res
= FD_ISSET(fd
, fdt
->close_on_exec
);
51 * locate_fd finds a free file descriptor in the open_fds fdset,
52 * expanding the fd arrays if necessary. Must be called with the
53 * file_lock held for write.
56 static int locate_fd(struct files_struct
*files
,
57 struct file
*file
, unsigned int orig_start
)
65 if (orig_start
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
69 fdt
= files_fdtable(files
);
71 * Someone might have closed fd's in the range
72 * orig_start..fdt->next_fd
75 if (start
< fdt
->next_fd
)
79 if (start
< fdt
->max_fdset
) {
80 newfd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
,
81 fdt
->max_fdset
, start
);
85 if (newfd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
88 error
= expand_files(files
, newfd
);
93 * If we needed to expand the fs array we
94 * might have blocked - try again.
100 * We reacquired files_lock, so we are safe as long as
101 * we reacquire the fdtable pointer and use it while holding
102 * the lock, no one can free it during that time.
104 fdt
= files_fdtable(files
);
105 if (start
<= fdt
->next_fd
)
106 fdt
->next_fd
= newfd
+ 1;
114 static int dupfd(struct file
*file
, unsigned int start
)
116 struct files_struct
* files
= current
->files
;
120 spin_lock(&files
->file_lock
);
121 fd
= locate_fd(files
, file
, start
);
123 /* locate_fd() may have expanded fdtable, load the ptr */
124 fdt
= files_fdtable(files
);
125 FD_SET(fd
, fdt
->open_fds
);
126 FD_CLR(fd
, fdt
->close_on_exec
);
127 spin_unlock(&files
->file_lock
);
128 fd_install(fd
, file
);
130 spin_unlock(&files
->file_lock
);
137 asmlinkage
long sys_dup2(unsigned int oldfd
, unsigned int newfd
)
140 struct file
* file
, *tofree
;
141 struct files_struct
* files
= current
->files
;
144 spin_lock(&files
->file_lock
);
145 if (!(file
= fcheck(oldfd
)))
151 if (newfd
>= current
->signal
->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 fdt
= files_fdtable(files
);
169 tofree
= fdt
->fd
[newfd
];
170 if (!tofree
&& FD_ISSET(newfd
, fdt
->open_fds
))
173 rcu_assign_pointer(fdt
->fd
[newfd
], file
);
174 FD_SET(newfd
, fdt
->open_fds
);
175 FD_CLR(newfd
, fdt
->close_on_exec
);
176 spin_unlock(&files
->file_lock
);
179 filp_close(tofree
, files
);
184 spin_unlock(&files
->file_lock
);
188 spin_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 | O_NOATIME)
205 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
207 struct inode
* inode
= filp
->f_dentry
->d_inode
;
210 /* O_APPEND cannot be cleared if the file is marked as append-only */
211 if (!(arg
& O_APPEND
) && IS_APPEND(inode
))
214 /* O_NOATIME can only be set by the owner or superuser */
215 if ((arg
& O_NOATIME
) && !(filp
->f_flags
& O_NOATIME
))
216 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
219 /* required for strict SunOS emulation */
220 if (O_NONBLOCK
!= O_NDELAY
)
224 if (arg
& O_DIRECT
) {
225 if (!filp
->f_mapping
|| !filp
->f_mapping
->a_ops
||
226 !filp
->f_mapping
->a_ops
->direct_IO
)
230 if (filp
->f_op
&& filp
->f_op
->check_flags
)
231 error
= filp
->f_op
->check_flags(arg
);
236 if ((arg
^ filp
->f_flags
) & FASYNC
) {
237 if (filp
->f_op
&& filp
->f_op
->fasync
) {
238 error
= filp
->f_op
->fasync(fd
, filp
, (arg
& FASYNC
) != 0);
244 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
250 static void f_modown(struct file
*filp
, unsigned long pid
,
251 uid_t uid
, uid_t euid
, int force
)
253 write_lock_irq(&filp
->f_owner
.lock
);
254 if (force
|| !filp
->f_owner
.pid
) {
255 filp
->f_owner
.pid
= pid
;
256 filp
->f_owner
.uid
= uid
;
257 filp
->f_owner
.euid
= euid
;
259 write_unlock_irq(&filp
->f_owner
.lock
);
262 int f_setown(struct file
*filp
, unsigned long arg
, int force
)
266 err
= security_file_set_fowner(filp
);
270 f_modown(filp
, arg
, current
->uid
, current
->euid
, force
);
274 EXPORT_SYMBOL(f_setown
);
276 void f_delown(struct file
*filp
)
278 f_modown(filp
, 0, 0, 0, 1);
281 static long do_fcntl(int fd
, unsigned int cmd
, unsigned long arg
,
289 err
= dupfd(filp
, arg
);
292 err
= get_close_on_exec(fd
) ? FD_CLOEXEC
: 0;
296 set_close_on_exec(fd
, arg
& FD_CLOEXEC
);
302 err
= setfl(fd
, filp
, arg
);
305 err
= fcntl_getlk(filp
, (struct flock __user
*) arg
);
309 err
= fcntl_setlk(fd
, filp
, cmd
, (struct flock __user
*) arg
);
313 * XXX If f_owner is a process group, the
314 * negative return value will get converted
315 * into an error. Oops. If we keep the
316 * current syscall conventions, the only way
317 * to fix this will be in libc.
319 err
= filp
->f_owner
.pid
;
320 force_successful_syscall_return();
323 err
= f_setown(filp
, arg
, 1);
326 err
= filp
->f_owner
.signum
;
329 /* arg == 0 restores default behaviour. */
330 if (!valid_signal(arg
)) {
334 filp
->f_owner
.signum
= arg
;
337 err
= fcntl_getlease(filp
);
340 err
= fcntl_setlease(fd
, filp
, arg
);
343 err
= fcntl_dirnotify(fd
, filp
, arg
);
351 asmlinkage
long sys_fcntl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
360 err
= security_file_fcntl(filp
, cmd
, arg
);
366 err
= do_fcntl(fd
, cmd
, arg
, filp
);
373 #if BITS_PER_LONG == 32
374 asmlinkage
long sys_fcntl64(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
384 err
= security_file_fcntl(filp
, cmd
, arg
);
393 err
= fcntl_getlk64(filp
, (struct flock64 __user
*) arg
);
397 err
= fcntl_setlk64(fd
, filp
, cmd
,
398 (struct flock64 __user
*) arg
);
401 err
= do_fcntl(fd
, cmd
, arg
, filp
);
410 /* Table to convert sigio signal codes into poll band bitmaps */
412 static long band_table
[NSIGPOLL
] = {
413 POLLIN
| POLLRDNORM
, /* POLL_IN */
414 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
415 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
416 POLLERR
, /* POLL_ERR */
417 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
418 POLLHUP
| POLLERR
/* POLL_HUP */
421 static inline int sigio_perm(struct task_struct
*p
,
422 struct fown_struct
*fown
, int sig
)
424 return (((fown
->euid
== 0) ||
425 (fown
->euid
== p
->suid
) || (fown
->euid
== p
->uid
) ||
426 (fown
->uid
== p
->suid
) || (fown
->uid
== p
->uid
)) &&
427 !security_file_send_sigiotask(p
, fown
, sig
));
430 static void send_sigio_to_task(struct task_struct
*p
,
431 struct fown_struct
*fown
,
435 if (!sigio_perm(p
, fown
, fown
->signum
))
438 switch (fown
->signum
) {
441 /* Queue a rt signal with the appropriate fd as its
442 value. We use SI_SIGIO as the source, not
443 SI_KERNEL, since kernel signals always get
444 delivered even if we can't queue. Failure to
445 queue in this case _should_ be reported; we fall
446 back to SIGIO in that case. --sct */
447 si
.si_signo
= fown
->signum
;
450 /* Make sure we are called with one of the POLL_*
451 reasons, otherwise we could leak kernel stack into
453 if ((reason
& __SI_MASK
) != __SI_POLL
)
455 if (reason
- POLL_IN
>= NSIGPOLL
)
458 si
.si_band
= band_table
[reason
- POLL_IN
];
460 if (!send_group_sig_info(fown
->signum
, &si
, p
))
462 /* fall-through: fall back on the old plain SIGIO signal */
464 send_group_sig_info(SIGIO
, SEND_SIG_PRIV
, p
);
468 void send_sigio(struct fown_struct
*fown
, int fd
, int band
)
470 struct task_struct
*p
;
473 read_lock(&fown
->lock
);
476 goto out_unlock_fown
;
478 read_lock(&tasklist_lock
);
480 p
= find_task_by_pid(pid
);
482 send_sigio_to_task(p
, fown
, fd
, band
);
485 do_each_task_pid(-pid
, PIDTYPE_PGID
, p
) {
486 send_sigio_to_task(p
, fown
, fd
, band
);
487 } while_each_task_pid(-pid
, PIDTYPE_PGID
, p
);
489 read_unlock(&tasklist_lock
);
491 read_unlock(&fown
->lock
);
494 static void send_sigurg_to_task(struct task_struct
*p
,
495 struct fown_struct
*fown
)
497 if (sigio_perm(p
, fown
, SIGURG
))
498 send_group_sig_info(SIGURG
, SEND_SIG_PRIV
, p
);
501 int send_sigurg(struct fown_struct
*fown
)
503 struct task_struct
*p
;
506 read_lock(&fown
->lock
);
509 goto out_unlock_fown
;
513 read_lock(&tasklist_lock
);
515 p
= find_task_by_pid(pid
);
517 send_sigurg_to_task(p
, fown
);
520 do_each_task_pid(-pid
, PIDTYPE_PGID
, p
) {
521 send_sigurg_to_task(p
, fown
);
522 } while_each_task_pid(-pid
, PIDTYPE_PGID
, p
);
524 read_unlock(&tasklist_lock
);
526 read_unlock(&fown
->lock
);
530 static DEFINE_RWLOCK(fasync_lock
);
531 static kmem_cache_t
*fasync_cache
;
534 * fasync_helper() is used by some character device drivers (mainly mice)
535 * to set up the fasync queue. It returns negative on error, 0 if it did
536 * no changes and positive if it added/deleted the entry.
538 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
540 struct fasync_struct
*fa
, **fp
;
541 struct fasync_struct
*new = NULL
;
545 new = kmem_cache_alloc(fasync_cache
, SLAB_KERNEL
);
549 write_lock_irq(&fasync_lock
);
550 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
551 if (fa
->fa_file
== filp
) {
554 kmem_cache_free(fasync_cache
, new);
557 kmem_cache_free(fasync_cache
, fa
);
565 new->magic
= FASYNC_MAGIC
;
568 new->fa_next
= *fapp
;
573 write_unlock_irq(&fasync_lock
);
577 EXPORT_SYMBOL(fasync_helper
);
579 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
582 struct fown_struct
* fown
;
583 if (fa
->magic
!= FASYNC_MAGIC
) {
584 printk(KERN_ERR
"kill_fasync: bad magic number in "
588 fown
= &fa
->fa_file
->f_owner
;
589 /* Don't send SIGURG to processes which have not set a
590 queued signum: SIGURG has its own default signalling
592 if (!(sig
== SIGURG
&& fown
->signum
== 0))
593 send_sigio(fown
, fa
->fa_fd
, band
);
598 EXPORT_SYMBOL(__kill_fasync
);
600 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
602 /* First a quick test without locking: usually
606 read_lock(&fasync_lock
);
607 /* reread *fp after obtaining the lock */
608 __kill_fasync(*fp
, sig
, band
);
609 read_unlock(&fasync_lock
);
612 EXPORT_SYMBOL(kill_fasync
);
614 static int __init
fasync_init(void)
616 fasync_cache
= kmem_cache_create("fasync_cache",
617 sizeof(struct fasync_struct
), 0, SLAB_PANIC
, NULL
, NULL
);
621 module_init(fasync_init
)