4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/file.h>
9 #include <linux/smp_lock.h>
10 #include <linux/slab.h>
13 #include <asm/siginfo.h>
14 #include <asm/uaccess.h>
16 extern int sock_fcntl (struct file
*, unsigned int cmd
, unsigned long arg
);
18 /* Expand files. Return <0 on error; 0 nothing done; 1 files expanded,
19 * we may have blocked.
21 * Should be called with the files->file_lock spinlock held for write.
23 static int expand_files(struct files_struct
*files
, int nr
)
27 printk (KERN_ERR __FUNCTION__
" %d: nr = %d\n", current
->pid
, nr
);
30 if (nr
>= files
->max_fdset
) {
32 if ((err
= expand_fdset(files
, nr
)))
35 if (nr
>= files
->max_fds
) {
37 if ((err
= expand_fd_array(files
, nr
)))
44 printk (KERN_ERR __FUNCTION__
" %d: return %d\n", current
->pid
, err
);
50 * locate_fd finds a free file descriptor in the open_fds fdset,
51 * expanding the fd arrays if necessary. The files write lock will be
52 * held on exit to ensure that the fd can be entered atomically.
55 static int locate_fd(struct files_struct
*files
,
56 struct file
*file
, int orig_start
)
62 write_lock(&files
->file_lock
);
66 * Someone might have closed fd's in the range
67 * orig_start..files->next_fd
70 if (start
< files
->next_fd
)
71 start
= files
->next_fd
;
74 if (start
< files
->max_fdset
) {
75 newfd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
76 files
->max_fdset
, start
);
80 if (newfd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
83 error
= expand_files(files
, newfd
);
88 * If we needed to expand the fs array we
89 * might have blocked - try again.
94 if (start
<= files
->next_fd
)
95 files
->next_fd
= newfd
+ 1;
103 static inline void allocate_fd(struct files_struct
*files
,
104 struct file
*file
, int fd
)
106 FD_SET(fd
, files
->open_fds
);
107 FD_CLR(fd
, files
->close_on_exec
);
108 write_unlock(&files
->file_lock
);
109 fd_install(fd
, file
);
112 static int dupfd(struct file
*file
, int start
)
114 struct files_struct
* files
= current
->files
;
117 ret
= locate_fd(files
, file
, start
);
120 allocate_fd(files
, file
, ret
);
124 write_unlock(&files
->file_lock
);
129 asmlinkage
long sys_dup2(unsigned int oldfd
, unsigned int newfd
)
132 struct file
* file
, *tofree
;
133 struct files_struct
* files
= current
->files
;
135 write_lock(&files
->file_lock
);
136 if (!(file
= fcheck(oldfd
)))
142 if (newfd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
144 get_file(file
); /* We are now finished with oldfd */
146 err
= expand_files(files
, newfd
);
150 /* To avoid races with open() and dup(), we will mark the fd as
151 * in-use in the open-file bitmap throughout the entire dup2()
152 * process. This is quite safe: do_close() uses the fd array
153 * entry, not the bitmap, to decide what work needs to be
155 /* Doesn't work. open() might be there first. --AV */
157 /* Yes. It's a race. In user space. Nothing sane to do */
159 tofree
= files
->fd
[newfd
];
160 if (!tofree
&& FD_ISSET(newfd
, files
->open_fds
))
163 files
->fd
[newfd
] = file
;
164 FD_SET(newfd
, files
->open_fds
);
165 FD_CLR(newfd
, files
->close_on_exec
);
166 write_unlock(&files
->file_lock
);
169 filp_close(tofree
, files
);
174 write_unlock(&files
->file_lock
);
178 write_unlock(&files
->file_lock
);
183 asmlinkage
long sys_dup(unsigned int fildes
)
186 struct file
* file
= fget(fildes
);
189 ret
= dupfd(file
, 0);
193 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC)
195 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
197 struct inode
* inode
= filp
->f_dentry
->d_inode
;
200 * In the case of an append-only file, O_APPEND
203 if (!(arg
& O_APPEND
) && IS_APPEND(inode
))
206 /* Did FASYNC state change? */
207 if ((arg
^ filp
->f_flags
) & FASYNC
) {
208 if (filp
->f_op
&& filp
->f_op
->fasync
)
209 filp
->f_op
->fasync(fd
, filp
, (arg
& FASYNC
) != 0);
212 /* required for strict SunOS emulation */
213 if (O_NONBLOCK
!= O_NDELAY
)
217 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
221 static long do_fcntl(unsigned int fd
, unsigned int cmd
,
222 unsigned long arg
, struct file
* filp
)
231 err
= dupfd(filp
, arg
);
235 err
= get_close_on_exec(fd
);
238 set_close_on_exec(fd
, arg
&1);
244 err
= setfl(fd
, filp
, arg
);
247 err
= fcntl_getlk(fd
, (struct flock
*) arg
);
250 err
= fcntl_setlk(fd
, cmd
, (struct flock
*) arg
);
253 err
= fcntl_setlk(fd
, cmd
, (struct flock
*) arg
);
257 * XXX If f_owner is a process group, the
258 * negative return value will get converted
259 * into an error. Oops. If we keep the
260 * current syscall conventions, the only way
261 * to fix this will be in libc.
263 err
= filp
->f_owner
.pid
;
266 filp
->f_owner
.pid
= arg
;
267 filp
->f_owner
.uid
= current
->uid
;
268 filp
->f_owner
.euid
= current
->euid
;
269 if (S_ISSOCK (filp
->f_dentry
->d_inode
->i_mode
))
270 err
= sock_fcntl (filp
, F_SETOWN
, arg
);
273 err
= filp
->f_owner
.signum
;
276 /* arg == 0 restores default behaviour. */
277 if (arg
< 0 || arg
> _NSIG
) {
282 filp
->f_owner
.signum
= arg
;
285 /* sockets need a few special fcntls. */
287 if (S_ISSOCK (filp
->f_dentry
->d_inode
->i_mode
))
288 err
= sock_fcntl (filp
, cmd
, arg
);
295 asmlinkage
long sys_fcntl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
305 err
= do_fcntl(fd
, cmd
, arg
, filp
);
313 #if BITS_PER_LONG == 32
314 asmlinkage
long sys_fcntl64(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
327 err
= fcntl_getlk64(fd
, (struct flock64
*) arg
);
330 err
= fcntl_setlk64(fd
, cmd
, (struct flock64
*) arg
);
333 err
= fcntl_setlk64(fd
, cmd
, (struct flock64
*) arg
);
336 err
= do_fcntl(fd
, cmd
, arg
, filp
);
346 /* Table to convert sigio signal codes into poll band bitmaps */
348 static long band_table
[NSIGPOLL
] = {
349 POLLIN
| POLLRDNORM
, /* POLL_IN */
350 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
351 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
352 POLLERR
, /* POLL_ERR */
353 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
354 POLLHUP
| POLLERR
/* POLL_HUP */
357 static void send_sigio_to_task(struct task_struct
*p
,
358 struct fown_struct
*fown
,
359 struct fasync_struct
*fa
,
362 if ((fown
->euid
!= 0) &&
363 (fown
->euid
^ p
->suid
) && (fown
->euid
^ p
->uid
) &&
364 (fown
->uid
^ p
->suid
) && (fown
->uid
^ p
->uid
))
366 switch (fown
->signum
) {
369 /* Queue a rt signal with the appropriate fd as its
370 value. We use SI_SIGIO as the source, not
371 SI_KERNEL, since kernel signals always get
372 delivered even if we can't queue. Failure to
373 queue in this case _should_ be reported; we fall
374 back to SIGIO in that case. --sct */
375 si
.si_signo
= fown
->signum
;
378 /* Make sure we are called with one of the POLL_*
379 reasons, otherwise we could leak kernel stack into
381 if ((reason
& __SI_MASK
) != __SI_POLL
)
383 if (reason
- POLL_IN
> NSIGPOLL
)
386 si
.si_band
= band_table
[reason
- POLL_IN
];
387 si
.si_fd
= fa
->fa_fd
;
388 if (!send_sig_info(fown
->signum
, &si
, p
))
390 /* fall-through: fall back on the old plain SIGIO signal */
392 send_sig(SIGIO
, p
, 1);
396 static void send_sigio(struct fown_struct
*fown
, struct fasync_struct
*fa
,
399 struct task_struct
* p
;
402 read_lock(&tasklist_lock
);
403 if ( (pid
> 0) && (p
= find_task_by_pid(pid
)) ) {
404 send_sigio_to_task(p
, fown
, fa
, band
);
413 send_sigio_to_task(p
, fown
, fa
, band
);
416 read_unlock(&tasklist_lock
);
420 * fasync_helper() is used by some character device drivers (mainly mice)
421 * to set up the fasync queue. It returns negative on error, 0 if it did
422 * no changes and positive if it added/deleted the entry.
424 static rwlock_t fasync_lock
= RW_LOCK_UNLOCKED
;
425 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
427 struct fasync_struct
*fa
, **fp
;
428 struct fasync_struct
*new = NULL
;
432 new = kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
436 write_lock_irq(&fasync_lock
);
437 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
438 if (fa
->fa_file
== filp
) {
452 new->magic
= FASYNC_MAGIC
;
455 new->fa_next
= *fapp
;
460 write_unlock_irq(&fasync_lock
);
464 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
467 struct fown_struct
* fown
;
468 if (fa
->magic
!= FASYNC_MAGIC
) {
469 printk("kill_fasync: bad magic number in "
473 fown
= &fa
->fa_file
->f_owner
;
474 /* Don't send SIGURG to processes which have not set a
475 queued signum: SIGURG has its own default signalling
477 if (fown
->pid
&& !(sig
== SIGURG
&& fown
->signum
== 0))
478 send_sigio(fown
, fa
, band
);
483 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
485 read_lock(&fasync_lock
);
486 __kill_fasync(*fp
, sig
, band
);
487 read_unlock(&fasync_lock
);