Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / fcntl.c
blobc1708066bf55d4a65e91a6d06391a01163caec67
1 /*
2 * linux/fs/fcntl.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/syscalls.h>
8 #include <linux/init.h>
9 #include <linux/mm.h>
10 #include <linux/fs.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>
19 #include <asm/poll.h>
20 #include <asm/siginfo.h>
21 #include <asm/uaccess.h>
23 void fastcall set_close_on_exec(unsigned int fd, int flag)
25 struct files_struct *files = current->files;
26 spin_lock(&files->file_lock);
27 if (flag)
28 FD_SET(fd, files->close_on_exec);
29 else
30 FD_CLR(fd, files->close_on_exec);
31 spin_unlock(&files->file_lock);
34 static inline int get_close_on_exec(unsigned int fd)
36 struct files_struct *files = current->files;
37 int res;
38 spin_lock(&files->file_lock);
39 res = FD_ISSET(fd, files->close_on_exec);
40 spin_unlock(&files->file_lock);
41 return res;
45 * locate_fd finds a free file descriptor in the open_fds fdset,
46 * expanding the fd arrays if necessary. Must be called with the
47 * file_lock held for write.
50 static int locate_fd(struct files_struct *files,
51 struct file *file, unsigned int orig_start)
53 unsigned int newfd;
54 unsigned int start;
55 int error;
57 error = -EINVAL;
58 if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
59 goto out;
61 repeat:
63 * Someone might have closed fd's in the range
64 * orig_start..files->next_fd
66 start = orig_start;
67 if (start < files->next_fd)
68 start = files->next_fd;
70 newfd = start;
71 if (start < files->max_fdset) {
72 newfd = find_next_zero_bit(files->open_fds->fds_bits,
73 files->max_fdset, start);
76 error = -EMFILE;
77 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
78 goto out;
80 error = expand_files(files, newfd);
81 if (error < 0)
82 goto out;
85 * If we needed to expand the fs array we
86 * might have blocked - try again.
88 if (error)
89 goto repeat;
91 if (start <= files->next_fd)
92 files->next_fd = newfd + 1;
94 error = newfd;
96 out:
97 return error;
100 static int dupfd(struct file *file, unsigned int start)
102 struct files_struct * files = current->files;
103 int fd;
105 spin_lock(&files->file_lock);
106 fd = locate_fd(files, file, start);
107 if (fd >= 0) {
108 FD_SET(fd, files->open_fds);
109 FD_CLR(fd, files->close_on_exec);
110 spin_unlock(&files->file_lock);
111 fd_install(fd, file);
112 } else {
113 spin_unlock(&files->file_lock);
114 fput(file);
117 return fd;
120 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
122 int err = -EBADF;
123 struct file * file, *tofree;
124 struct files_struct * files = current->files;
126 spin_lock(&files->file_lock);
127 if (!(file = fcheck(oldfd)))
128 goto out_unlock;
129 err = newfd;
130 if (newfd == oldfd)
131 goto out_unlock;
132 err = -EBADF;
133 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
134 goto out_unlock;
135 get_file(file); /* We are now finished with oldfd */
137 err = expand_files(files, newfd);
138 if (err < 0)
139 goto out_fput;
141 /* To avoid races with open() and dup(), we will mark the fd as
142 * in-use in the open-file bitmap throughout the entire dup2()
143 * process. This is quite safe: do_close() uses the fd array
144 * entry, not the bitmap, to decide what work needs to be
145 * done. --sct */
146 /* Doesn't work. open() might be there first. --AV */
148 /* Yes. It's a race. In user space. Nothing sane to do */
149 err = -EBUSY;
150 tofree = files->fd[newfd];
151 if (!tofree && FD_ISSET(newfd, files->open_fds))
152 goto out_fput;
154 files->fd[newfd] = file;
155 FD_SET(newfd, files->open_fds);
156 FD_CLR(newfd, files->close_on_exec);
157 spin_unlock(&files->file_lock);
159 if (tofree)
160 filp_close(tofree, files);
161 err = newfd;
162 out:
163 return err;
164 out_unlock:
165 spin_unlock(&files->file_lock);
166 goto out;
168 out_fput:
169 spin_unlock(&files->file_lock);
170 fput(file);
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 | O_DIRECT | O_NOATIME)
186 static int setfl(int fd, struct file * filp, unsigned long arg)
188 struct inode * inode = filp->f_dentry->d_inode;
189 int error = 0;
191 /* O_APPEND cannot be cleared if the file is marked as append-only */
192 if (!(arg & O_APPEND) && IS_APPEND(inode))
193 return -EPERM;
195 /* O_NOATIME can only be set by the owner or superuser */
196 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
197 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
198 return -EPERM;
200 /* required for strict SunOS emulation */
201 if (O_NONBLOCK != O_NDELAY)
202 if (arg & O_NDELAY)
203 arg |= O_NONBLOCK;
205 if (arg & O_DIRECT) {
206 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
207 !filp->f_mapping->a_ops->direct_IO)
208 return -EINVAL;
211 if (filp->f_op && filp->f_op->check_flags)
212 error = filp->f_op->check_flags(arg);
213 if (error)
214 return error;
216 lock_kernel();
217 if ((arg ^ filp->f_flags) & FASYNC) {
218 if (filp->f_op && filp->f_op->fasync) {
219 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
220 if (error < 0)
221 goto out;
225 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
226 out:
227 unlock_kernel();
228 return error;
231 static void f_modown(struct file *filp, unsigned long pid,
232 uid_t uid, uid_t euid, int force)
234 write_lock_irq(&filp->f_owner.lock);
235 if (force || !filp->f_owner.pid) {
236 filp->f_owner.pid = pid;
237 filp->f_owner.uid = uid;
238 filp->f_owner.euid = euid;
240 write_unlock_irq(&filp->f_owner.lock);
243 int f_setown(struct file *filp, unsigned long arg, int force)
245 int err;
247 err = security_file_set_fowner(filp);
248 if (err)
249 return err;
251 f_modown(filp, arg, current->uid, current->euid, force);
252 return 0;
255 EXPORT_SYMBOL(f_setown);
257 void f_delown(struct file *filp)
259 f_modown(filp, 0, 0, 0, 1);
262 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
263 struct file *filp)
265 long err = -EINVAL;
267 switch (cmd) {
268 case F_DUPFD:
269 get_file(filp);
270 err = dupfd(filp, arg);
271 break;
272 case F_GETFD:
273 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
274 break;
275 case F_SETFD:
276 err = 0;
277 set_close_on_exec(fd, arg & FD_CLOEXEC);
278 break;
279 case F_GETFL:
280 err = filp->f_flags;
281 break;
282 case F_SETFL:
283 err = setfl(fd, filp, arg);
284 break;
285 case F_GETLK:
286 err = fcntl_getlk(filp, (struct flock __user *) arg);
287 break;
288 case F_SETLK:
289 case F_SETLKW:
290 err = fcntl_setlk(filp, cmd, (struct flock __user *) arg);
291 break;
292 case F_GETOWN:
294 * XXX If f_owner is a process group, the
295 * negative return value will get converted
296 * into an error. Oops. If we keep the
297 * current syscall conventions, the only way
298 * to fix this will be in libc.
300 err = filp->f_owner.pid;
301 force_successful_syscall_return();
302 break;
303 case F_SETOWN:
304 err = f_setown(filp, arg, 1);
305 break;
306 case F_GETSIG:
307 err = filp->f_owner.signum;
308 break;
309 case F_SETSIG:
310 /* arg == 0 restores default behaviour. */
311 if (arg < 0 || arg > _NSIG) {
312 break;
314 err = 0;
315 filp->f_owner.signum = arg;
316 break;
317 case F_GETLEASE:
318 err = fcntl_getlease(filp);
319 break;
320 case F_SETLEASE:
321 err = fcntl_setlease(fd, filp, arg);
322 break;
323 case F_NOTIFY:
324 err = fcntl_dirnotify(fd, filp, arg);
325 break;
326 default:
327 break;
329 return err;
332 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
334 struct file *filp;
335 long err = -EBADF;
337 filp = fget(fd);
338 if (!filp)
339 goto out;
341 err = security_file_fcntl(filp, cmd, arg);
342 if (err) {
343 fput(filp);
344 return err;
347 err = do_fcntl(fd, cmd, arg, filp);
349 fput(filp);
350 out:
351 return err;
354 #if BITS_PER_LONG == 32
355 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
357 struct file * filp;
358 long err;
360 err = -EBADF;
361 filp = fget(fd);
362 if (!filp)
363 goto out;
365 err = security_file_fcntl(filp, cmd, arg);
366 if (err) {
367 fput(filp);
368 return err;
370 err = -EBADF;
372 switch (cmd) {
373 case F_GETLK64:
374 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
375 break;
376 case F_SETLK64:
377 case F_SETLKW64:
378 err = fcntl_setlk64(filp, cmd, (struct flock64 __user *) arg);
379 break;
380 default:
381 err = do_fcntl(fd, cmd, arg, filp);
382 break;
384 fput(filp);
385 out:
386 return err;
388 #endif
390 /* Table to convert sigio signal codes into poll band bitmaps */
392 static long band_table[NSIGPOLL] = {
393 POLLIN | POLLRDNORM, /* POLL_IN */
394 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
395 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
396 POLLERR, /* POLL_ERR */
397 POLLPRI | POLLRDBAND, /* POLL_PRI */
398 POLLHUP | POLLERR /* POLL_HUP */
401 static inline int sigio_perm(struct task_struct *p,
402 struct fown_struct *fown, int sig)
404 return (((fown->euid == 0) ||
405 (fown->euid == p->suid) || (fown->euid == p->uid) ||
406 (fown->uid == p->suid) || (fown->uid == p->uid)) &&
407 !security_file_send_sigiotask(p, fown, sig));
410 static void send_sigio_to_task(struct task_struct *p,
411 struct fown_struct *fown,
412 int fd,
413 int reason)
415 if (!sigio_perm(p, fown, fown->signum))
416 return;
418 switch (fown->signum) {
419 siginfo_t si;
420 default:
421 /* Queue a rt signal with the appropriate fd as its
422 value. We use SI_SIGIO as the source, not
423 SI_KERNEL, since kernel signals always get
424 delivered even if we can't queue. Failure to
425 queue in this case _should_ be reported; we fall
426 back to SIGIO in that case. --sct */
427 si.si_signo = fown->signum;
428 si.si_errno = 0;
429 si.si_code = reason;
430 /* Make sure we are called with one of the POLL_*
431 reasons, otherwise we could leak kernel stack into
432 userspace. */
433 if ((reason & __SI_MASK) != __SI_POLL)
434 BUG();
435 if (reason - POLL_IN >= NSIGPOLL)
436 si.si_band = ~0L;
437 else
438 si.si_band = band_table[reason - POLL_IN];
439 si.si_fd = fd;
440 if (!send_sig_info(fown->signum, &si, p))
441 break;
442 /* fall-through: fall back on the old plain SIGIO signal */
443 case 0:
444 send_group_sig_info(SIGIO, SEND_SIG_PRIV, p);
448 void send_sigio(struct fown_struct *fown, int fd, int band)
450 struct task_struct *p;
451 int pid;
453 read_lock(&fown->lock);
454 pid = fown->pid;
455 if (!pid)
456 goto out_unlock_fown;
458 read_lock(&tasklist_lock);
459 if (pid > 0) {
460 p = find_task_by_pid(pid);
461 if (p) {
462 send_sigio_to_task(p, fown, fd, band);
464 } else {
465 do_each_task_pid(-pid, PIDTYPE_PGID, p) {
466 send_sigio_to_task(p, fown, fd, band);
467 } while_each_task_pid(-pid, PIDTYPE_PGID, p);
469 read_unlock(&tasklist_lock);
470 out_unlock_fown:
471 read_unlock(&fown->lock);
474 static void send_sigurg_to_task(struct task_struct *p,
475 struct fown_struct *fown)
477 if (sigio_perm(p, fown, SIGURG))
478 send_group_sig_info(SIGURG, SEND_SIG_PRIV, p);
481 int send_sigurg(struct fown_struct *fown)
483 struct task_struct *p;
484 int pid, ret = 0;
486 read_lock(&fown->lock);
487 pid = fown->pid;
488 if (!pid)
489 goto out_unlock_fown;
491 ret = 1;
493 read_lock(&tasklist_lock);
494 if (pid > 0) {
495 p = find_task_by_pid(pid);
496 if (p) {
497 send_sigurg_to_task(p, fown);
499 } else {
500 do_each_task_pid(-pid, PIDTYPE_PGID, p) {
501 send_sigurg_to_task(p, fown);
502 } while_each_task_pid(-pid, PIDTYPE_PGID, p);
504 read_unlock(&tasklist_lock);
505 out_unlock_fown:
506 read_unlock(&fown->lock);
507 return ret;
510 static DEFINE_RWLOCK(fasync_lock);
511 static kmem_cache_t *fasync_cache;
514 * fasync_helper() is used by some character device drivers (mainly mice)
515 * to set up the fasync queue. It returns negative on error, 0 if it did
516 * no changes and positive if it added/deleted the entry.
518 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
520 struct fasync_struct *fa, **fp;
521 struct fasync_struct *new = NULL;
522 int result = 0;
524 if (on) {
525 new = kmem_cache_alloc(fasync_cache, SLAB_KERNEL);
526 if (!new)
527 return -ENOMEM;
529 write_lock_irq(&fasync_lock);
530 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
531 if (fa->fa_file == filp) {
532 if(on) {
533 fa->fa_fd = fd;
534 kmem_cache_free(fasync_cache, new);
535 } else {
536 *fp = fa->fa_next;
537 kmem_cache_free(fasync_cache, fa);
538 result = 1;
540 goto out;
544 if (on) {
545 new->magic = FASYNC_MAGIC;
546 new->fa_file = filp;
547 new->fa_fd = fd;
548 new->fa_next = *fapp;
549 *fapp = new;
550 result = 1;
552 out:
553 write_unlock_irq(&fasync_lock);
554 return result;
557 EXPORT_SYMBOL(fasync_helper);
559 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
561 while (fa) {
562 struct fown_struct * fown;
563 if (fa->magic != FASYNC_MAGIC) {
564 printk(KERN_ERR "kill_fasync: bad magic number in "
565 "fasync_struct!\n");
566 return;
568 fown = &fa->fa_file->f_owner;
569 /* Don't send SIGURG to processes which have not set a
570 queued signum: SIGURG has its own default signalling
571 mechanism. */
572 if (!(sig == SIGURG && fown->signum == 0))
573 send_sigio(fown, fa->fa_fd, band);
574 fa = fa->fa_next;
578 EXPORT_SYMBOL(__kill_fasync);
580 void kill_fasync(struct fasync_struct **fp, int sig, int band)
582 /* First a quick test without locking: usually
583 * the list is empty.
585 if (*fp) {
586 read_lock(&fasync_lock);
587 /* reread *fp after obtaining the lock */
588 __kill_fasync(*fp, sig, band);
589 read_unlock(&fasync_lock);
592 EXPORT_SYMBOL(kill_fasync);
594 static int __init fasync_init(void)
596 fasync_cache = kmem_cache_create("fasync_cache",
597 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL, NULL);
598 return 0;
601 module_init(fasync_init)