Import 2.4.0-test5pre5
[davej-history.git] / fs / fcntl.c
blob65982187328b93cb5cbd758995b186cfa2477601
1 /*
2 * linux/fs/fcntl.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/mm.h>
8 #include <linux/file.h>
9 #include <linux/smp_lock.h>
10 #include <linux/slab.h>
12 #include <asm/poll.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)
25 int err, expand = 0;
26 #ifdef FDSET_DEBUG
27 printk (KERN_ERR __FUNCTION__ " %d: nr = %d\n", current->pid, nr);
28 #endif
30 if (nr >= files->max_fdset) {
31 expand = 1;
32 if ((err = expand_fdset(files, nr)))
33 goto out;
35 if (nr >= files->max_fds) {
36 expand = 1;
37 if ((err = expand_fd_array(files, nr)))
38 goto out;
40 err = expand;
41 out:
42 #ifdef FDSET_DEBUG
43 if (err)
44 printk (KERN_ERR __FUNCTION__ " %d: return %d\n", current->pid, err);
45 #endif
46 return 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)
58 unsigned int newfd;
59 int error;
60 int start;
62 write_lock(&files->file_lock);
64 repeat:
66 * Someone might have closed fd's in the range
67 * orig_start..files->next_fd
69 start = orig_start;
70 if (start < files->next_fd)
71 start = files->next_fd;
73 newfd = start;
74 if (start < files->max_fdset) {
75 newfd = find_next_zero_bit(files->open_fds->fds_bits,
76 files->max_fdset, start);
79 error = -EMFILE;
80 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
81 goto out;
83 error = expand_files(files, newfd);
84 if (error < 0)
85 goto out;
88 * If we needed to expand the fs array we
89 * might have blocked - try again.
91 if (error)
92 goto repeat;
94 if (start <= files->next_fd)
95 files->next_fd = newfd + 1;
97 error = newfd;
99 out:
100 return error;
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;
115 int ret;
117 ret = locate_fd(files, file, start);
118 if (ret < 0)
119 goto out_putf;
120 allocate_fd(files, file, ret);
121 return ret;
123 out_putf:
124 write_unlock(&files->file_lock);
125 fput(file);
126 return ret;
129 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
131 int err = -EBADF;
132 struct file * file;
133 struct files_struct * files = current->files;
135 write_lock(&current->files->file_lock);
136 if (!(file = fcheck(oldfd)))
137 goto out_unlock;
138 err = newfd;
139 if (newfd == oldfd)
140 goto out_unlock;
141 err = -EBADF;
142 if (newfd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
143 goto out_unlock;
144 get_file(file); /* We are now finished with oldfd */
146 err = expand_files(files, newfd);
147 if (err < 0) {
148 write_unlock(&files->file_lock);
149 fput(file);
150 goto out;
153 /* To avoid races with open() and dup(), we will mark the fd as
154 * in-use in the open-file bitmap throughout the entire dup2()
155 * process. This is quite safe: do_close() uses the fd array
156 * entry, not the bitmap, to decide what work needs to be
157 * done. --sct */
158 FD_SET(newfd, files->open_fds);
159 write_unlock(&files->file_lock);
161 do_close(newfd, 0);
163 write_lock(&files->file_lock);
164 allocate_fd(files, file, newfd);
165 err = newfd;
167 out:
168 return err;
169 out_unlock:
170 write_unlock(&current->files->file_lock);
171 goto out;
174 asmlinkage long sys_dup(unsigned int fildes)
176 int ret = -EBADF;
177 struct file * file = fget(fildes);
179 if (file)
180 ret = dupfd(file, 0);
181 return ret;
184 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC)
186 static int setfl(int fd, struct file * filp, unsigned long arg)
188 struct inode * inode = filp->f_dentry->d_inode;
191 * In the case of an append-only file, O_APPEND
192 * cannot be cleared
194 if (!(arg & O_APPEND) && IS_APPEND(inode))
195 return -EPERM;
197 /* Did FASYNC state change? */
198 if ((arg ^ filp->f_flags) & FASYNC) {
199 if (filp->f_op && filp->f_op->fasync)
200 filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
203 /* required for strict SunOS emulation */
204 if (O_NONBLOCK != O_NDELAY)
205 if (arg & O_NDELAY)
206 arg |= O_NONBLOCK;
208 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
209 return 0;
212 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
214 struct file * filp;
215 long err = -EBADF;
217 filp = fget(fd);
218 if (!filp)
219 goto out;
220 err = 0;
221 lock_kernel();
222 switch (cmd) {
223 case F_DUPFD:
224 err = -EINVAL;
225 if (arg < NR_OPEN) {
226 get_file(filp);
227 err = dupfd(filp, arg);
229 break;
230 case F_GETFD:
231 err = FD_ISSET(fd, current->files->close_on_exec);
232 break;
233 case F_SETFD:
234 if (arg&1)
235 FD_SET(fd, current->files->close_on_exec);
236 else
237 FD_CLR(fd, current->files->close_on_exec);
238 break;
239 case F_GETFL:
240 err = filp->f_flags;
241 break;
242 case F_SETFL:
243 err = setfl(fd, filp, arg);
244 break;
245 case F_GETLK:
246 err = fcntl_getlk(fd, (struct flock *) arg);
247 break;
248 case F_SETLK:
249 err = fcntl_setlk(fd, cmd, (struct flock *) arg);
250 break;
251 case F_SETLKW:
252 err = fcntl_setlk(fd, cmd, (struct flock *) arg);
253 break;
254 case F_GETOWN:
256 * XXX If f_owner is a process group, the
257 * negative return value will get converted
258 * into an error. Oops. If we keep the
259 * current syscall conventions, the only way
260 * to fix this will be in libc.
262 err = filp->f_owner.pid;
263 break;
264 case F_SETOWN:
265 filp->f_owner.pid = arg;
266 filp->f_owner.uid = current->uid;
267 filp->f_owner.euid = current->euid;
268 if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
269 err = sock_fcntl (filp, F_SETOWN, arg);
270 break;
271 case F_GETSIG:
272 err = filp->f_owner.signum;
273 break;
274 case F_SETSIG:
275 /* arg == 0 restores default behaviour. */
276 if (arg < 0 || arg > _NSIG) {
277 err = -EINVAL;
278 break;
280 err = 0;
281 filp->f_owner.signum = arg;
282 break;
283 default:
284 /* sockets need a few special fcntls. */
285 err = -EINVAL;
286 if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
287 err = sock_fcntl (filp, cmd, arg);
288 break;
290 unlock_kernel();
291 fput(filp);
292 out:
293 return err;
296 /* Table to convert sigio signal codes into poll band bitmaps */
298 static long band_table[NSIGPOLL] = {
299 POLLIN | POLLRDNORM, /* POLL_IN */
300 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
301 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
302 POLLERR, /* POLL_ERR */
303 POLLPRI | POLLRDBAND, /* POLL_PRI */
304 POLLHUP | POLLERR /* POLL_HUP */
307 static void send_sigio_to_task(struct task_struct *p,
308 struct fown_struct *fown,
309 struct fasync_struct *fa,
310 int reason)
312 if ((fown->euid != 0) &&
313 (fown->euid ^ p->suid) && (fown->euid ^ p->uid) &&
314 (fown->uid ^ p->suid) && (fown->uid ^ p->uid))
315 return;
316 switch (fown->signum) {
317 siginfo_t si;
318 default:
319 /* Queue a rt signal with the appropriate fd as its
320 value. We use SI_SIGIO as the source, not
321 SI_KERNEL, since kernel signals always get
322 delivered even if we can't queue. Failure to
323 queue in this case _should_ be reported; we fall
324 back to SIGIO in that case. --sct */
325 si.si_signo = fown->signum;
326 si.si_errno = 0;
327 si.si_code = reason;
328 /* Make sure we are called with one of the POLL_*
329 reasons, otherwise we could leak kernel stack into
330 userspace. */
331 if ((reason & __SI_MASK) != __SI_POLL)
332 BUG();
333 if (reason - POLL_IN > NSIGPOLL)
334 si.si_band = ~0L;
335 else
336 si.si_band = band_table[reason - POLL_IN];
337 si.si_fd = fa->fa_fd;
338 if (!send_sig_info(fown->signum, &si, p))
339 break;
340 /* fall-through: fall back on the old plain SIGIO signal */
341 case 0:
342 send_sig(SIGIO, p, 1);
346 static void send_sigio(struct fown_struct *fown, struct fasync_struct *fa,
347 int band)
349 struct task_struct * p;
350 int pid = fown->pid;
352 read_lock(&tasklist_lock);
353 if ( (pid > 0) && (p = find_task_by_pid(pid)) ) {
354 send_sigio_to_task(p, fown, fa, band);
355 goto out;
357 for_each_task(p) {
358 int match = p->pid;
359 if (pid < 0)
360 match = -p->pgrp;
361 if (pid != match)
362 continue;
363 send_sigio_to_task(p, fown, fa, band);
365 out:
366 read_unlock(&tasklist_lock);
370 * fasync_helper() is used by some character device drivers (mainly mice)
371 * to set up the fasync queue. It returns negative on error, 0 if it did
372 * no changes and positive if it added/deleted the entry.
374 static rwlock_t fasync_lock = RW_LOCK_UNLOCKED;
375 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
377 struct fasync_struct *fa, **fp;
378 struct fasync_struct *new = NULL;
379 int result = 0;
381 if (on) {
382 new = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
383 if (!new)
384 return -ENOMEM;
386 write_lock_irq(&fasync_lock);
387 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
388 if (fa->fa_file == filp) {
389 if(on) {
390 fa->fa_fd = fd;
391 kfree(new);
392 } else {
393 *fp = fa->fa_next;
394 kfree(fa);
395 result = 1;
397 goto out;
401 if (on) {
402 new->magic = FASYNC_MAGIC;
403 new->fa_file = filp;
404 new->fa_fd = fd;
405 new->fa_next = *fapp;
406 *fapp = new;
407 result = 1;
409 out:
410 write_unlock_irq(&fasync_lock);
411 return result;
414 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
416 while (fa) {
417 struct fown_struct * fown;
418 if (fa->magic != FASYNC_MAGIC) {
419 printk("kill_fasync: bad magic number in "
420 "fasync_struct!\n");
421 return;
423 fown = &fa->fa_file->f_owner;
424 /* Don't send SIGURG to processes which have not set a
425 queued signum: SIGURG has its own default signalling
426 mechanism. */
427 if (fown->pid && !(sig == SIGURG && fown->signum == 0))
428 send_sigio(fown, fa, band);
429 fa = fa->fa_next;
433 void kill_fasync(struct fasync_struct **fp, int sig, int band)
435 read_lock(&fasync_lock);
436 __kill_fasync(*fp, sig, band);
437 read_unlock(&fasync_lock);