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>
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 * locate_fd finds a free file descriptor in the open_fds fdset,
46 * expanding the fd arrays if necessary. Must be called with the
47 * file_lock held for write.
50 static int locate_fd(struct files_struct
*files
,
51 struct file
*file
, unsigned int orig_start
)
58 if (orig_start
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
63 * Someone might have closed fd's in the range
64 * orig_start..files->next_fd
67 if (start
< files
->next_fd
)
68 start
= files
->next_fd
;
71 if (start
< files
->max_fdset
) {
72 newfd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
73 files
->max_fdset
, start
);
77 if (newfd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
80 error
= expand_files(files
, newfd
);
85 * If we needed to expand the fs array we
86 * might have blocked - try again.
91 if (start
<= files
->next_fd
)
92 files
->next_fd
= newfd
+ 1;
100 static int dupfd(struct file
*file
, unsigned int start
)
102 struct files_struct
* files
= current
->files
;
105 spin_lock(&files
->file_lock
);
106 fd
= locate_fd(files
, file
, start
);
108 FD_SET(fd
, files
->open_fds
);
109 FD_CLR(fd
, files
->close_on_exec
);
110 spin_unlock(&files
->file_lock
);
111 fd_install(fd
, file
);
113 spin_unlock(&files
->file_lock
);
120 asmlinkage
long sys_dup2(unsigned int oldfd
, unsigned int newfd
)
123 struct file
* file
, *tofree
;
124 struct files_struct
* files
= current
->files
;
126 spin_lock(&files
->file_lock
);
127 if (!(file
= fcheck(oldfd
)))
133 if (newfd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
135 get_file(file
); /* We are now finished with oldfd */
137 err
= expand_files(files
, newfd
);
141 /* To avoid races with open() and dup(), we will mark the fd as
142 * in-use in the open-file bitmap throughout the entire dup2()
143 * process. This is quite safe: do_close() uses the fd array
144 * entry, not the bitmap, to decide what work needs to be
146 /* Doesn't work. open() might be there first. --AV */
148 /* Yes. It's a race. In user space. Nothing sane to do */
150 tofree
= files
->fd
[newfd
];
151 if (!tofree
&& FD_ISSET(newfd
, files
->open_fds
))
154 files
->fd
[newfd
] = file
;
155 FD_SET(newfd
, files
->open_fds
);
156 FD_CLR(newfd
, files
->close_on_exec
);
157 spin_unlock(&files
->file_lock
);
160 filp_close(tofree
, files
);
165 spin_unlock(&files
->file_lock
);
169 spin_unlock(&files
->file_lock
);
174 asmlinkage
long sys_dup(unsigned int fildes
)
177 struct file
* file
= fget(fildes
);
180 ret
= dupfd(file
, 0);
184 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
186 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
188 struct inode
* inode
= filp
->f_dentry
->d_inode
;
191 /* O_APPEND cannot be cleared if the file is marked as append-only */
192 if (!(arg
& O_APPEND
) && IS_APPEND(inode
))
195 /* O_NOATIME can only be set by the owner or superuser */
196 if ((arg
& O_NOATIME
) && !(filp
->f_flags
& O_NOATIME
))
197 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
200 /* required for strict SunOS emulation */
201 if (O_NONBLOCK
!= O_NDELAY
)
205 if (arg
& O_DIRECT
) {
206 if (!filp
->f_mapping
|| !filp
->f_mapping
->a_ops
||
207 !filp
->f_mapping
->a_ops
->direct_IO
)
211 if (filp
->f_op
&& filp
->f_op
->check_flags
)
212 error
= filp
->f_op
->check_flags(arg
);
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 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
231 static void f_modown(struct file
*filp
, unsigned long pid
,
232 uid_t uid
, uid_t euid
, int force
)
234 write_lock_irq(&filp
->f_owner
.lock
);
235 if (force
|| !filp
->f_owner
.pid
) {
236 filp
->f_owner
.pid
= pid
;
237 filp
->f_owner
.uid
= uid
;
238 filp
->f_owner
.euid
= euid
;
240 write_unlock_irq(&filp
->f_owner
.lock
);
243 int f_setown(struct file
*filp
, unsigned long arg
, int force
)
247 err
= security_file_set_fowner(filp
);
251 f_modown(filp
, arg
, current
->uid
, current
->euid
, force
);
255 EXPORT_SYMBOL(f_setown
);
257 void f_delown(struct file
*filp
)
259 f_modown(filp
, 0, 0, 0, 1);
262 static long do_fcntl(int fd
, unsigned int cmd
, unsigned long arg
,
270 err
= dupfd(filp
, arg
);
273 err
= get_close_on_exec(fd
) ? FD_CLOEXEC
: 0;
277 set_close_on_exec(fd
, arg
& FD_CLOEXEC
);
283 err
= setfl(fd
, filp
, arg
);
286 err
= fcntl_getlk(filp
, (struct flock __user
*) arg
);
290 err
= fcntl_setlk(filp
, cmd
, (struct flock __user
*) arg
);
294 * XXX If f_owner is a process group, the
295 * negative return value will get converted
296 * into an error. Oops. If we keep the
297 * current syscall conventions, the only way
298 * to fix this will be in libc.
300 err
= filp
->f_owner
.pid
;
301 force_successful_syscall_return();
304 err
= f_setown(filp
, arg
, 1);
307 err
= filp
->f_owner
.signum
;
310 /* arg == 0 restores default behaviour. */
311 if (arg
< 0 || arg
> _NSIG
) {
315 filp
->f_owner
.signum
= arg
;
318 err
= fcntl_getlease(filp
);
321 err
= fcntl_setlease(fd
, filp
, arg
);
324 err
= fcntl_dirnotify(fd
, filp
, arg
);
332 asmlinkage
long sys_fcntl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
341 err
= security_file_fcntl(filp
, cmd
, 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
)
365 err
= security_file_fcntl(filp
, cmd
, arg
);
374 err
= fcntl_getlk64(filp
, (struct flock64 __user
*) arg
);
378 err
= fcntl_setlk64(filp
, cmd
, (struct flock64 __user
*) arg
);
381 err
= do_fcntl(fd
, cmd
, arg
, filp
);
390 /* Table to convert sigio signal codes into poll band bitmaps */
392 static long band_table
[NSIGPOLL
] = {
393 POLLIN
| POLLRDNORM
, /* POLL_IN */
394 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
395 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
396 POLLERR
, /* POLL_ERR */
397 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
398 POLLHUP
| POLLERR
/* POLL_HUP */
401 static inline int sigio_perm(struct task_struct
*p
,
402 struct fown_struct
*fown
, int sig
)
404 return (((fown
->euid
== 0) ||
405 (fown
->euid
== p
->suid
) || (fown
->euid
== p
->uid
) ||
406 (fown
->uid
== p
->suid
) || (fown
->uid
== p
->uid
)) &&
407 !security_file_send_sigiotask(p
, fown
, sig
));
410 static void send_sigio_to_task(struct task_struct
*p
,
411 struct fown_struct
*fown
,
415 if (!sigio_perm(p
, fown
, fown
->signum
))
418 switch (fown
->signum
) {
421 /* Queue a rt signal with the appropriate fd as its
422 value. We use SI_SIGIO as the source, not
423 SI_KERNEL, since kernel signals always get
424 delivered even if we can't queue. Failure to
425 queue in this case _should_ be reported; we fall
426 back to SIGIO in that case. --sct */
427 si
.si_signo
= fown
->signum
;
430 /* Make sure we are called with one of the POLL_*
431 reasons, otherwise we could leak kernel stack into
433 if ((reason
& __SI_MASK
) != __SI_POLL
)
435 if (reason
- POLL_IN
>= NSIGPOLL
)
438 si
.si_band
= band_table
[reason
- POLL_IN
];
440 if (!send_group_sig_info(fown
->signum
, &si
, p
))
442 /* fall-through: fall back on the old plain SIGIO signal */
444 send_group_sig_info(SIGIO
, SEND_SIG_PRIV
, p
);
448 void send_sigio(struct fown_struct
*fown
, int fd
, int band
)
450 struct task_struct
*p
;
453 read_lock(&fown
->lock
);
456 goto out_unlock_fown
;
458 read_lock(&tasklist_lock
);
460 p
= find_task_by_pid(pid
);
462 send_sigio_to_task(p
, fown
, fd
, band
);
465 do_each_task_pid(-pid
, PIDTYPE_PGID
, p
) {
466 send_sigio_to_task(p
, fown
, fd
, band
);
467 } while_each_task_pid(-pid
, PIDTYPE_PGID
, p
);
469 read_unlock(&tasklist_lock
);
471 read_unlock(&fown
->lock
);
474 static void send_sigurg_to_task(struct task_struct
*p
,
475 struct fown_struct
*fown
)
477 if (sigio_perm(p
, fown
, SIGURG
))
478 send_group_sig_info(SIGURG
, SEND_SIG_PRIV
, p
);
481 int send_sigurg(struct fown_struct
*fown
)
483 struct task_struct
*p
;
486 read_lock(&fown
->lock
);
489 goto out_unlock_fown
;
493 read_lock(&tasklist_lock
);
495 p
= find_task_by_pid(pid
);
497 send_sigurg_to_task(p
, fown
);
500 do_each_task_pid(-pid
, PIDTYPE_PGID
, p
) {
501 send_sigurg_to_task(p
, fown
);
502 } while_each_task_pid(-pid
, PIDTYPE_PGID
, p
);
504 read_unlock(&tasklist_lock
);
506 read_unlock(&fown
->lock
);
510 static DEFINE_RWLOCK(fasync_lock
);
511 static kmem_cache_t
*fasync_cache
;
514 * fasync_helper() is used by some character device drivers (mainly mice)
515 * to set up the fasync queue. It returns negative on error, 0 if it did
516 * no changes and positive if it added/deleted the entry.
518 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
520 struct fasync_struct
*fa
, **fp
;
521 struct fasync_struct
*new = NULL
;
525 new = kmem_cache_alloc(fasync_cache
, SLAB_KERNEL
);
529 write_lock_irq(&fasync_lock
);
530 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
531 if (fa
->fa_file
== filp
) {
534 kmem_cache_free(fasync_cache
, new);
537 kmem_cache_free(fasync_cache
, fa
);
545 new->magic
= FASYNC_MAGIC
;
548 new->fa_next
= *fapp
;
553 write_unlock_irq(&fasync_lock
);
557 EXPORT_SYMBOL(fasync_helper
);
559 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
562 struct fown_struct
* fown
;
563 if (fa
->magic
!= FASYNC_MAGIC
) {
564 printk(KERN_ERR
"kill_fasync: bad magic number in "
568 fown
= &fa
->fa_file
->f_owner
;
569 /* Don't send SIGURG to processes which have not set a
570 queued signum: SIGURG has its own default signalling
572 if (!(sig
== SIGURG
&& fown
->signum
== 0))
573 send_sigio(fown
, fa
->fa_fd
, band
);
578 EXPORT_SYMBOL(__kill_fasync
);
580 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
582 /* First a quick test without locking: usually
586 read_lock(&fasync_lock
);
587 /* reread *fp after obtaining the lock */
588 __kill_fasync(*fp
, sig
, band
);
589 read_unlock(&fasync_lock
);
592 EXPORT_SYMBOL(kill_fasync
);
594 static int __init
fasync_init(void)
596 fasync_cache
= kmem_cache_create("fasync_cache",
597 sizeof(struct fasync_struct
), 0, SLAB_PANIC
, NULL
, NULL
);
601 module_init(fasync_init
)