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
);
19 * locate_fd finds a free file descriptor in the open_fds fdset,
20 * expanding the fd arrays if necessary. The files write lock will be
21 * held on exit to ensure that the fd can be entered atomically.
24 static inline int locate_fd(struct files_struct
*files
,
25 struct file
*file
, int start
)
30 write_lock(&files
->file_lock
);
34 if (start
< files
->next_fd
)
35 start
= files
->next_fd
;
36 if (start
>= files
->max_fdset
) {
38 error
= expand_files(files
, start
);
44 newfd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
45 files
->max_fdset
, start
);
48 if (newfd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
50 if (newfd
>= files
->max_fdset
)
53 error
= expand_files(files
, newfd
);
56 if (error
) /* If we might have blocked, try again. */
59 if (start
<= files
->next_fd
)
60 files
->next_fd
= newfd
+ 1;
68 static inline void allocate_fd(struct files_struct
*files
,
69 struct file
*file
, int fd
)
71 FD_SET(fd
, files
->open_fds
);
72 FD_CLR(fd
, files
->close_on_exec
);
73 write_unlock(&files
->file_lock
);
77 static int dupfd(struct file
*file
, int start
)
79 struct files_struct
* files
= current
->files
;
82 ret
= locate_fd(files
, file
, start
);
85 allocate_fd(files
, file
, ret
);
89 write_unlock(&files
->file_lock
);
94 asmlinkage
long sys_dup2(unsigned int oldfd
, unsigned int newfd
)
98 struct files_struct
* files
= current
->files
;
100 write_lock(¤t
->files
->file_lock
);
101 if (!(file
= fcheck(oldfd
)))
107 if (newfd
>= NR_OPEN
)
108 goto out_unlock
; /* following POSIX.1 6.2.1 */
109 get_file(file
); /* We are now finished with oldfd */
111 err
= expand_files(files
, newfd
);
113 write_unlock(&files
->file_lock
);
118 /* To avoid races with open() and dup(), we will mark the fd as
119 * in-use in the open-file bitmap throughout the entire dup2()
120 * process. This is quite safe: do_close() uses the fd array
121 * entry, not the bitmap, to decide what work needs to be
123 FD_SET(newfd
, files
->open_fds
);
124 write_unlock(&files
->file_lock
);
128 write_lock(&files
->file_lock
);
129 allocate_fd(files
, file
, newfd
);
135 write_unlock(¤t
->files
->file_lock
);
139 asmlinkage
long sys_dup(unsigned int fildes
)
142 struct file
* file
= fget(fildes
);
145 ret
= dupfd(file
, 0);
149 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC)
151 static int setfl(int fd
, struct file
* filp
, unsigned long arg
)
153 struct inode
* inode
= filp
->f_dentry
->d_inode
;
156 * In the case of an append-only file, O_APPEND
159 if (!(arg
& O_APPEND
) && IS_APPEND(inode
))
162 /* Did FASYNC state change? */
163 if ((arg
^ filp
->f_flags
) & FASYNC
) {
164 if (filp
->f_op
&& filp
->f_op
->fasync
)
165 filp
->f_op
->fasync(fd
, filp
, (arg
& FASYNC
) != 0);
168 /* required for strict SunOS emulation */
169 if (O_NONBLOCK
!= O_NDELAY
)
173 filp
->f_flags
= (arg
& SETFL_MASK
) | (filp
->f_flags
& ~SETFL_MASK
);
177 asmlinkage
long sys_fcntl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
192 err
= dupfd(filp
, arg
);
196 err
= FD_ISSET(fd
, current
->files
->close_on_exec
);
200 FD_SET(fd
, current
->files
->close_on_exec
);
202 FD_CLR(fd
, current
->files
->close_on_exec
);
208 err
= setfl(fd
, filp
, arg
);
211 err
= fcntl_getlk(fd
, (struct flock
*) arg
);
214 err
= fcntl_setlk(fd
, cmd
, (struct flock
*) arg
);
217 err
= fcntl_setlk(fd
, cmd
, (struct flock
*) arg
);
221 * XXX If f_owner is a process group, the
222 * negative return value will get converted
223 * into an error. Oops. If we keep the
224 * current syscall conventions, the only way
225 * to fix this will be in libc.
227 err
= filp
->f_owner
.pid
;
230 filp
->f_owner
.pid
= arg
;
231 filp
->f_owner
.uid
= current
->uid
;
232 filp
->f_owner
.euid
= current
->euid
;
233 if (S_ISSOCK (filp
->f_dentry
->d_inode
->i_mode
))
234 err
= sock_fcntl (filp
, F_SETOWN
, arg
);
237 err
= filp
->f_owner
.signum
;
240 /* arg == 0 restores default behaviour. */
241 if (arg
< 0 || arg
> _NSIG
) {
246 filp
->f_owner
.signum
= arg
;
249 /* sockets need a few special fcntls. */
251 if (S_ISSOCK (filp
->f_dentry
->d_inode
->i_mode
))
252 err
= sock_fcntl (filp
, cmd
, arg
);
261 /* Table to convert sigio signal codes into poll band bitmaps */
263 static long band_table
[NSIGPOLL
] = {
264 POLLIN
| POLLRDNORM
, /* POLL_IN */
265 POLLOUT
| POLLWRNORM
| POLLWRBAND
, /* POLL_OUT */
266 POLLIN
| POLLRDNORM
| POLLMSG
, /* POLL_MSG */
267 POLLERR
, /* POLL_ERR */
268 POLLPRI
| POLLRDBAND
, /* POLL_PRI */
269 POLLHUP
| POLLERR
/* POLL_HUP */
272 static void send_sigio_to_task(struct task_struct
*p
,
273 struct fown_struct
*fown
,
274 struct fasync_struct
*fa
,
277 if ((fown
->euid
!= 0) &&
278 (fown
->euid
^ p
->suid
) && (fown
->euid
^ p
->uid
) &&
279 (fown
->uid
^ p
->suid
) && (fown
->uid
^ p
->uid
))
281 switch (fown
->signum
) {
284 /* Queue a rt signal with the appropriate fd as its
285 value. We use SI_SIGIO as the source, not
286 SI_KERNEL, since kernel signals always get
287 delivered even if we can't queue. Failure to
288 queue in this case _should_ be reported; we fall
289 back to SIGIO in that case. --sct */
290 si
.si_signo
= fown
->signum
;
293 /* Make sure we are called with one of the POLL_*
294 reasons, otherwise we could leak kernel stack into
296 if ((reason
& __SI_MASK
) != __SI_POLL
)
298 if (reason
- POLL_IN
> NSIGPOLL
)
301 si
.si_band
= band_table
[reason
- POLL_IN
];
302 si
.si_fd
= fa
->fa_fd
;
303 if (!send_sig_info(fown
->signum
, &si
, p
))
305 /* fall-through: fall back on the old plain SIGIO signal */
307 send_sig(SIGIO
, p
, 1);
311 static void send_sigio(struct fown_struct
*fown
, struct fasync_struct
*fa
,
314 struct task_struct
* p
;
317 read_lock(&tasklist_lock
);
318 if ( (pid
> 0) && (p
= find_task_by_pid(pid
)) ) {
319 send_sigio_to_task(p
, fown
, fa
, band
);
328 send_sigio_to_task(p
, fown
, fa
, band
);
331 read_unlock(&tasklist_lock
);
335 * fasync_helper() is used by some character device drivers (mainly mice)
336 * to set up the fasync queue. It returns negative on error, 0 if it did
337 * no changes and positive if it added/deleted the entry.
339 static rwlock_t fasync_lock
= RW_LOCK_UNLOCKED
;
340 int fasync_helper(int fd
, struct file
* filp
, int on
, struct fasync_struct
**fapp
)
342 struct fasync_struct
*fa
, **fp
;
343 struct fasync_struct
*new = NULL
;
347 new = kmalloc(sizeof(struct fasync_struct
), GFP_KERNEL
);
351 write_lock_irq(&fasync_lock
);
352 for (fp
= fapp
; (fa
= *fp
) != NULL
; fp
= &fa
->fa_next
) {
353 if (fa
->fa_file
== filp
) {
367 new->magic
= FASYNC_MAGIC
;
370 new->fa_next
= *fapp
;
375 write_unlock_irq(&fasync_lock
);
379 void __kill_fasync(struct fasync_struct
*fa
, int sig
, int band
)
382 struct fown_struct
* fown
;
383 if (fa
->magic
!= FASYNC_MAGIC
) {
384 printk("kill_fasync: bad magic number in "
388 fown
= &fa
->fa_file
->f_owner
;
389 /* Don't send SIGURG to processes which have not set a
390 queued signum: SIGURG has its own default signalling
392 if (fown
->pid
&& !(sig
== SIGURG
&& fown
->signum
== 0))
393 send_sigio(fown
, fa
, band
);
398 void kill_fasync(struct fasync_struct
**fp
, int sig
, int band
)
400 read_lock(&fasync_lock
);
401 __kill_fasync(*fp
, sig
, band
);
402 read_unlock(&fasync_lock
);