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/module.h>
14 #include <linux/security.h>
15 #include <linux/ptrace.h>
18 #include <asm/siginfo.h>
19 #include <asm/uaccess.h>
21 extern int fcntl_setlease(unsigned int fd
, struct file
*filp
, long arg
);
22 extern int fcntl_getlease(struct file
*filp
);
24 void 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 /* Expand files. Return <0 on error; 0 nothing done; 1 files expanded,
47 * we may have blocked.
49 * Should be called with the files->file_lock spinlock held for write.
51 static int expand_files(struct files_struct
*files
, int nr
)
55 printk (KERN_ERR
"%s %d: nr = %d\n", __FUNCTION__
, current
->pid
, nr
);
58 if (nr
>= files
->max_fdset
) {
60 if ((err
= expand_fdset(files
, nr
)))
63 if (nr
>= files
->max_fds
) {
65 if ((err
= expand_fd_array(files
, nr
)))
72 printk (KERN_ERR
"%s %d: return %d\n", __FUNCTION__
, current
->pid
, err
);
78 * locate_fd finds a free file descriptor in the open_fds fdset,
79 * expanding the fd arrays if necessary. Must be called with the
80 * file_lock held for write.
83 static int locate_fd(struct files_struct
*files
,
84 struct file
*file
, unsigned int orig_start
)
91 if (orig_start
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
96 * Someone might have closed fd's in the range
97 * orig_start..files->next_fd
100 if (start
< files
->next_fd
)
101 start
= files
->next_fd
;
104 if (start
< files
->max_fdset
) {
105 newfd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
106 files
->max_fdset
, start
);
110 if (newfd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
113 error
= expand_files(files
, newfd
);
118 * If we needed to expand the fs array we
119 * might have blocked - try again.
124 if (start
<= files
->next_fd
)
125 files
->next_fd
= newfd
+ 1;
133 static int dupfd(struct file
*file
, unsigned int start
)
135 struct files_struct
* files
= current
->files
;
138 spin_lock(&files
->file_lock
);
139 fd
= locate_fd(files
, file
, start
);
141 FD_SET(fd
, files
->open_fds
);
142 FD_CLR(fd
, files
->close_on_exec
);
143 spin_unlock(&files
->file_lock
);
144 fd_install(fd
, file
);
146 spin_unlock(&files
->file_lock
);
153 asmlinkage
long sys_dup2(unsigned int oldfd
, unsigned int newfd
)
156 struct file
* file
, *tofree
;
157 struct files_struct
* files
= current
->files
;
159 spin_lock(&files
->file_lock
);
160 if (!(file
= fcheck(oldfd
)))
166 if (newfd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
168 get_file(file
); /* We are now finished with oldfd */
170 err
= expand_files(files
, newfd
);
174 /* To avoid races with open() and dup(), we will mark the fd as
175 * in-use in the open-file bitmap throughout the entire dup2()
176 * process. This is quite safe: do_close() uses the fd array
177 * entry, not the bitmap, to decide what work needs to be
179 /* Doesn't work. open() might be there first. --AV */
181 /* Yes. It's a race. In user space. Nothing sane to do */
183 tofree
= files
->fd
[newfd
];
184 if (!tofree
&& FD_ISSET(newfd
, files
->open_fds
))
187 files
->fd
[newfd
] = file
;
188 FD_SET(newfd
, files
->open_fds
);
189 FD_CLR(newfd
, files
->close_on_exec
);
190 spin_unlock(&files
->file_lock
);
193 filp_close(tofree
, files
);
198 spin_unlock(&files
->file_lock
);
202 spin_unlock(&files
->file_lock
);
207 asmlinkage
long sys_dup(unsigned int fildes
)
210 struct file
* file
= fget(fildes
);
213 ret
= dupfd(file
, 0);
217 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT)
219 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
221 struct inode
* inode
= filp
->f_dentry
->d_inode
;
224 /* O_APPEND cannot be cleared if the file is marked as append-only */
225 if (!(arg
& O_APPEND
) && IS_APPEND(inode
))
228 /* required for strict SunOS emulation */
229 if (O_NONBLOCK
!= O_NDELAY
)
233 if (arg
& O_DIRECT
) {
234 if (!inode
->i_mapping
|| !inode
->i_mapping
->a_ops
||
235 !inode
->i_mapping
->a_ops
->direct_IO
)
240 if ((arg
^ filp
->f_flags
) & FASYNC
) {
241 if (filp
->f_op
&& filp
->f_op
->fasync
) {
242 error
= filp
->f_op
->fasync(fd
, filp
, (arg
& FASYNC
) != 0);
248 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
254 static void f_modown(struct file
*filp
, unsigned long pid
,
255 uid_t uid
, uid_t euid
, int force
)
257 write_lock_irq(&filp
->f_owner
.lock
);
258 if (force
|| !filp
->f_owner
.pid
) {
259 filp
->f_owner
.pid
= pid
;
260 filp
->f_owner
.uid
= uid
;
261 filp
->f_owner
.euid
= euid
;
263 write_unlock_irq(&filp
->f_owner
.lock
);
266 int f_setown(struct file
*filp
, unsigned long arg
, int force
)
270 err
= security_file_set_fowner(filp
);
274 f_modown(filp
, arg
, current
->uid
, current
->euid
, force
);
278 void f_delown(struct file
*filp
)
280 f_modown(filp
, 0, 0, 0, 1);
283 static long do_fcntl(unsigned int fd
, unsigned int cmd
,
284 unsigned long arg
, struct file
* filp
)
291 err
= dupfd(filp
, arg
);
294 err
= get_close_on_exec(fd
);
298 set_close_on_exec(fd
, arg
&1);
304 err
= setfl(fd
, filp
, arg
);
307 err
= fcntl_getlk(filp
, (struct flock __user
*) arg
);
311 err
= fcntl_setlk(filp
, cmd
, (struct flock __user
*) arg
);
315 * XXX If f_owner is a process group, the
316 * negative return value will get converted
317 * into an error. Oops. If we keep the
318 * current syscall conventions, the only way
319 * to fix this will be in libc.
321 err
= filp
->f_owner
.pid
;
322 force_successful_syscall_return();
325 err
= f_setown(filp
, arg
, 1);
328 err
= filp
->f_owner
.signum
;
331 /* arg == 0 restores default behaviour. */
332 if (arg
< 0 || arg
> _NSIG
) {
336 filp
->f_owner
.signum
= arg
;
339 err
= fcntl_getlease(filp
);
342 err
= fcntl_setlease(fd
, filp
, arg
);
345 err
= fcntl_dirnotify(fd
, filp
, arg
);
354 asmlinkage
long sys_fcntl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
363 err
= security_file_fcntl(filp
, cmd
, arg
);
369 err
= do_fcntl(fd
, cmd
, arg
, filp
);
376 #if BITS_PER_LONG == 32
377 asmlinkage
long sys_fcntl64(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
387 err
= security_file_fcntl(filp
, cmd
, arg
);
396 err
= fcntl_getlk64(filp
, (struct flock64 __user
*) arg
);
400 err
= fcntl_setlk64(filp
, cmd
, (struct flock64 __user
*) arg
);
403 err
= do_fcntl(fd
, cmd
, arg
, filp
);
412 /* Table to convert sigio signal codes into poll band bitmaps */
414 static long band_table
[NSIGPOLL
] = {
415 POLLIN
| POLLRDNORM
, /* POLL_IN */
416 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
417 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
418 POLLERR
, /* POLL_ERR */
419 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
420 POLLHUP
| POLLERR
/* POLL_HUP */
423 static inline int sigio_perm(struct task_struct
*p
,
424 struct fown_struct
*fown
)
426 return ((fown
->euid
== 0) ||
427 (fown
->euid
== p
->suid
) || (fown
->euid
== p
->uid
) ||
428 (fown
->uid
== p
->suid
) || (fown
->uid
== p
->uid
));
431 static void send_sigio_to_task(struct task_struct
*p
,
432 struct fown_struct
*fown
,
436 if (!sigio_perm(p
, fown
))
439 if (security_file_send_sigiotask(p
, fown
, fd
, reason
))
442 switch (fown
->signum
) {
445 /* Queue a rt signal with the appropriate fd as its
446 value. We use SI_SIGIO as the source, not
447 SI_KERNEL, since kernel signals always get
448 delivered even if we can't queue. Failure to
449 queue in this case _should_ be reported; we fall
450 back to SIGIO in that case. --sct */
451 si
.si_signo
= fown
->signum
;
454 /* Make sure we are called with one of the POLL_*
455 reasons, otherwise we could leak kernel stack into
457 if ((reason
& __SI_MASK
) != __SI_POLL
)
459 if (reason
- POLL_IN
>= NSIGPOLL
)
462 si
.si_band
= band_table
[reason
- POLL_IN
];
464 if (!send_sig_info(fown
->signum
, &si
, p
))
466 /* fall-through: fall back on the old plain SIGIO signal */
468 send_group_sig_info(SIGIO
, SEND_SIG_PRIV
, p
);
472 void send_sigio(struct fown_struct
*fown
, int fd
, int band
)
474 struct task_struct
*p
;
477 read_lock(&fown
->lock
);
480 goto out_unlock_fown
;
482 read_lock(&tasklist_lock
);
484 p
= find_task_by_pid(pid
);
486 send_sigio_to_task(p
, fown
, fd
, band
);
491 for_each_task_pid(-pid
, PIDTYPE_PGID
, p
, l
, pidptr
) {
492 send_sigio_to_task(p
, fown
, fd
, band
);
495 read_unlock(&tasklist_lock
);
497 read_unlock(&fown
->lock
);
500 static void send_sigurg_to_task(struct task_struct
*p
,
501 struct fown_struct
*fown
)
503 if (sigio_perm(p
, fown
))
504 send_group_sig_info(SIGURG
, SEND_SIG_PRIV
, p
);
507 int send_sigurg(struct fown_struct
*fown
)
509 struct task_struct
*p
;
512 read_lock(&fown
->lock
);
515 goto out_unlock_fown
;
519 read_lock(&tasklist_lock
);
521 p
= find_task_by_pid(pid
);
523 send_sigurg_to_task(p
, fown
);
528 for_each_task_pid(-pid
, PIDTYPE_PGID
, p
, l
, pidptr
) {
529 send_sigurg_to_task(p
, fown
);
532 read_unlock(&tasklist_lock
);
534 read_unlock(&fown
->lock
);
538 static rwlock_t fasync_lock
= RW_LOCK_UNLOCKED
;
539 static kmem_cache_t
*fasync_cache
;
542 * fasync_helper() is used by some character device drivers (mainly mice)
543 * to set up the fasync queue. It returns negative on error, 0 if it did
544 * no changes and positive if it added/deleted the entry.
546 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
548 struct fasync_struct
*fa
, **fp
;
549 struct fasync_struct
*new = NULL
;
553 new = kmem_cache_alloc(fasync_cache
, SLAB_KERNEL
);
557 write_lock_irq(&fasync_lock
);
558 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
559 if (fa
->fa_file
== filp
) {
562 kmem_cache_free(fasync_cache
, new);
565 kmem_cache_free(fasync_cache
, fa
);
573 new->magic
= FASYNC_MAGIC
;
576 new->fa_next
= *fapp
;
581 write_unlock_irq(&fasync_lock
);
585 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
588 struct fown_struct
* fown
;
589 if (fa
->magic
!= FASYNC_MAGIC
) {
590 printk(KERN_ERR
"kill_fasync: bad magic number in "
594 fown
= &fa
->fa_file
->f_owner
;
595 /* Don't send SIGURG to processes which have not set a
596 queued signum: SIGURG has its own default signalling
598 if (!(sig
== SIGURG
&& fown
->signum
== 0))
599 send_sigio(fown
, fa
->fa_fd
, band
);
604 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
606 read_lock(&fasync_lock
);
607 __kill_fasync(*fp
, sig
, band
);
608 read_unlock(&fasync_lock
);
611 static int __init
fasync_init(void)
613 fasync_cache
= kmem_cache_create("fasync_cache",
614 sizeof(struct fasync_struct
), 0, 0, NULL
, NULL
);
616 panic("cannot create fasync slab cache");
620 module_init(fasync_init
)
622 EXPORT_SYMBOL(f_setown
);
623 EXPORT_SYMBOL(f_delown
);