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>
21 #include <asm/siginfo.h>
22 #include <asm/uaccess.h>
24 void fastcall
set_close_on_exec(unsigned int fd
, int flag
)
26 struct files_struct
*files
= current
->files
;
27 spin_lock(&files
->file_lock
);
29 FD_SET(fd
, files
->close_on_exec
);
31 FD_CLR(fd
, files
->close_on_exec
);
32 spin_unlock(&files
->file_lock
);
35 static inline int get_close_on_exec(unsigned int fd
)
37 struct files_struct
*files
= current
->files
;
39 spin_lock(&files
->file_lock
);
40 res
= FD_ISSET(fd
, files
->close_on_exec
);
41 spin_unlock(&files
->file_lock
);
46 * locate_fd finds a free file descriptor in the open_fds fdset,
47 * expanding the fd arrays if necessary. Must be called with the
48 * file_lock held for write.
51 static int locate_fd(struct files_struct
*files
,
52 struct file
*file
, unsigned int orig_start
)
59 if (orig_start
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
64 * Someone might have closed fd's in the range
65 * orig_start..files->next_fd
68 if (start
< files
->next_fd
)
69 start
= files
->next_fd
;
72 if (start
< files
->max_fdset
) {
73 newfd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
74 files
->max_fdset
, start
);
78 if (newfd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
81 error
= expand_files(files
, newfd
);
86 * If we needed to expand the fs array we
87 * might have blocked - try again.
92 if (start
<= files
->next_fd
)
93 files
->next_fd
= newfd
+ 1;
101 static int dupfd(struct file
*file
, unsigned int start
)
103 struct files_struct
* files
= current
->files
;
106 spin_lock(&files
->file_lock
);
107 fd
= locate_fd(files
, file
, start
);
109 FD_SET(fd
, files
->open_fds
);
110 FD_CLR(fd
, files
->close_on_exec
);
111 spin_unlock(&files
->file_lock
);
112 fd_install(fd
, file
);
114 spin_unlock(&files
->file_lock
);
121 asmlinkage
long sys_dup2(unsigned int oldfd
, unsigned int newfd
)
124 struct file
* file
, *tofree
;
125 struct files_struct
* files
= current
->files
;
127 spin_lock(&files
->file_lock
);
128 if (!(file
= fcheck(oldfd
)))
134 if (newfd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
136 get_file(file
); /* We are now finished with oldfd */
138 err
= expand_files(files
, newfd
);
142 /* To avoid races with open() and dup(), we will mark the fd as
143 * in-use in the open-file bitmap throughout the entire dup2()
144 * process. This is quite safe: do_close() uses the fd array
145 * entry, not the bitmap, to decide what work needs to be
147 /* Doesn't work. open() might be there first. --AV */
149 /* Yes. It's a race. In user space. Nothing sane to do */
151 tofree
= files
->fd
[newfd
];
152 if (!tofree
&& FD_ISSET(newfd
, files
->open_fds
))
155 files
->fd
[newfd
] = file
;
156 FD_SET(newfd
, files
->open_fds
);
157 FD_CLR(newfd
, files
->close_on_exec
);
158 spin_unlock(&files
->file_lock
);
161 filp_close(tofree
, files
);
166 spin_unlock(&files
->file_lock
);
170 spin_unlock(&files
->file_lock
);
175 asmlinkage
long sys_dup(unsigned int fildes
)
178 struct file
* file
= fget(fildes
);
181 ret
= dupfd(file
, 0);
185 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
187 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
189 struct inode
* inode
= filp
->f_dentry
->d_inode
;
192 /* O_APPEND cannot be cleared if the file is marked as append-only */
193 if (!(arg
& O_APPEND
) && IS_APPEND(inode
))
196 /* O_NOATIME can only be set by the owner or superuser */
197 if ((arg
& O_NOATIME
) && !(filp
->f_flags
& O_NOATIME
))
198 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
201 /* required for strict SunOS emulation */
202 if (O_NONBLOCK
!= O_NDELAY
)
206 if (arg
& O_DIRECT
) {
207 if (!filp
->f_mapping
|| !filp
->f_mapping
->a_ops
||
208 !filp
->f_mapping
->a_ops
->direct_IO
)
212 if (filp
->f_op
&& filp
->f_op
->check_flags
)
213 error
= filp
->f_op
->check_flags(arg
);
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 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
232 static void f_modown(struct file
*filp
, unsigned long pid
,
233 uid_t uid
, uid_t euid
, int force
)
235 write_lock_irq(&filp
->f_owner
.lock
);
236 if (force
|| !filp
->f_owner
.pid
) {
237 filp
->f_owner
.pid
= pid
;
238 filp
->f_owner
.uid
= uid
;
239 filp
->f_owner
.euid
= euid
;
241 write_unlock_irq(&filp
->f_owner
.lock
);
244 int f_setown(struct file
*filp
, unsigned long arg
, int force
)
248 err
= security_file_set_fowner(filp
);
252 f_modown(filp
, arg
, current
->uid
, current
->euid
, force
);
256 EXPORT_SYMBOL(f_setown
);
258 void f_delown(struct file
*filp
)
260 f_modown(filp
, 0, 0, 0, 1);
263 static long do_fcntl(int fd
, unsigned int cmd
, unsigned long arg
,
271 err
= dupfd(filp
, arg
);
274 err
= get_close_on_exec(fd
) ? FD_CLOEXEC
: 0;
278 set_close_on_exec(fd
, arg
& FD_CLOEXEC
);
284 err
= setfl(fd
, filp
, arg
);
287 err
= fcntl_getlk(filp
, (struct flock __user
*) arg
);
291 err
= fcntl_setlk(filp
, cmd
, (struct flock __user
*) arg
);
295 * XXX If f_owner is a process group, the
296 * negative return value will get converted
297 * into an error. Oops. If we keep the
298 * current syscall conventions, the only way
299 * to fix this will be in libc.
301 err
= filp
->f_owner
.pid
;
302 force_successful_syscall_return();
305 err
= f_setown(filp
, arg
, 1);
308 err
= filp
->f_owner
.signum
;
311 /* arg == 0 restores default behaviour. */
312 if (!valid_signal(arg
)) {
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
);
333 asmlinkage
long sys_fcntl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
342 err
= security_file_fcntl(filp
, cmd
, arg
);
348 err
= do_fcntl(fd
, cmd
, arg
, filp
);
355 #if BITS_PER_LONG == 32
356 asmlinkage
long sys_fcntl64(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
366 err
= security_file_fcntl(filp
, cmd
, arg
);
375 err
= fcntl_getlk64(filp
, (struct flock64 __user
*) arg
);
379 err
= fcntl_setlk64(filp
, cmd
, (struct flock64 __user
*) arg
);
382 err
= do_fcntl(fd
, cmd
, arg
, filp
);
391 /* Table to convert sigio signal codes into poll band bitmaps */
393 static long band_table
[NSIGPOLL
] = {
394 POLLIN
| POLLRDNORM
, /* POLL_IN */
395 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
396 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
397 POLLERR
, /* POLL_ERR */
398 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
399 POLLHUP
| POLLERR
/* POLL_HUP */
402 static inline int sigio_perm(struct task_struct
*p
,
403 struct fown_struct
*fown
, int sig
)
405 return (((fown
->euid
== 0) ||
406 (fown
->euid
== p
->suid
) || (fown
->euid
== p
->uid
) ||
407 (fown
->uid
== p
->suid
) || (fown
->uid
== p
->uid
)) &&
408 !security_file_send_sigiotask(p
, fown
, sig
));
411 static void send_sigio_to_task(struct task_struct
*p
,
412 struct fown_struct
*fown
,
416 if (!sigio_perm(p
, fown
, fown
->signum
))
419 switch (fown
->signum
) {
422 /* Queue a rt signal with the appropriate fd as its
423 value. We use SI_SIGIO as the source, not
424 SI_KERNEL, since kernel signals always get
425 delivered even if we can't queue. Failure to
426 queue in this case _should_ be reported; we fall
427 back to SIGIO in that case. --sct */
428 si
.si_signo
= fown
->signum
;
431 /* Make sure we are called with one of the POLL_*
432 reasons, otherwise we could leak kernel stack into
434 if ((reason
& __SI_MASK
) != __SI_POLL
)
436 if (reason
- POLL_IN
>= NSIGPOLL
)
439 si
.si_band
= band_table
[reason
- POLL_IN
];
441 if (!send_group_sig_info(fown
->signum
, &si
, p
))
443 /* fall-through: fall back on the old plain SIGIO signal */
445 send_group_sig_info(SIGIO
, SEND_SIG_PRIV
, p
);
449 void send_sigio(struct fown_struct
*fown
, int fd
, int band
)
451 struct task_struct
*p
;
454 read_lock(&fown
->lock
);
457 goto out_unlock_fown
;
459 read_lock(&tasklist_lock
);
461 p
= find_task_by_pid(pid
);
463 send_sigio_to_task(p
, fown
, fd
, band
);
466 do_each_task_pid(-pid
, PIDTYPE_PGID
, p
) {
467 send_sigio_to_task(p
, fown
, fd
, band
);
468 } while_each_task_pid(-pid
, PIDTYPE_PGID
, p
);
470 read_unlock(&tasklist_lock
);
472 read_unlock(&fown
->lock
);
475 static void send_sigurg_to_task(struct task_struct
*p
,
476 struct fown_struct
*fown
)
478 if (sigio_perm(p
, fown
, SIGURG
))
479 send_group_sig_info(SIGURG
, SEND_SIG_PRIV
, p
);
482 int send_sigurg(struct fown_struct
*fown
)
484 struct task_struct
*p
;
487 read_lock(&fown
->lock
);
490 goto out_unlock_fown
;
494 read_lock(&tasklist_lock
);
496 p
= find_task_by_pid(pid
);
498 send_sigurg_to_task(p
, fown
);
501 do_each_task_pid(-pid
, PIDTYPE_PGID
, p
) {
502 send_sigurg_to_task(p
, fown
);
503 } while_each_task_pid(-pid
, PIDTYPE_PGID
, p
);
505 read_unlock(&tasklist_lock
);
507 read_unlock(&fown
->lock
);
511 static DEFINE_RWLOCK(fasync_lock
);
512 static kmem_cache_t
*fasync_cache
;
515 * fasync_helper() is used by some character device drivers (mainly mice)
516 * to set up the fasync queue. It returns negative on error, 0 if it did
517 * no changes and positive if it added/deleted the entry.
519 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
521 struct fasync_struct
*fa
, **fp
;
522 struct fasync_struct
*new = NULL
;
526 new = kmem_cache_alloc(fasync_cache
, SLAB_KERNEL
);
530 write_lock_irq(&fasync_lock
);
531 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
532 if (fa
->fa_file
== filp
) {
535 kmem_cache_free(fasync_cache
, new);
538 kmem_cache_free(fasync_cache
, fa
);
546 new->magic
= FASYNC_MAGIC
;
549 new->fa_next
= *fapp
;
554 write_unlock_irq(&fasync_lock
);
558 EXPORT_SYMBOL(fasync_helper
);
560 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
563 struct fown_struct
* fown
;
564 if (fa
->magic
!= FASYNC_MAGIC
) {
565 printk(KERN_ERR
"kill_fasync: bad magic number in "
569 fown
= &fa
->fa_file
->f_owner
;
570 /* Don't send SIGURG to processes which have not set a
571 queued signum: SIGURG has its own default signalling
573 if (!(sig
== SIGURG
&& fown
->signum
== 0))
574 send_sigio(fown
, fa
->fa_fd
, band
);
579 EXPORT_SYMBOL(__kill_fasync
);
581 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
583 /* First a quick test without locking: usually
587 read_lock(&fasync_lock
);
588 /* reread *fp after obtaining the lock */
589 __kill_fasync(*fp
, sig
, band
);
590 read_unlock(&fasync_lock
);
593 EXPORT_SYMBOL(kill_fasync
);
595 static int __init
fasync_init(void)
597 fasync_cache
= kmem_cache_create("fasync_cache",
598 sizeof(struct fasync_struct
), 0, SLAB_PANIC
, NULL
, NULL
);
602 module_init(fasync_init
)
This page took 0.045759 seconds and 5 git commands to generate.