Move _RET_IP_ and _THIS_IP_ to include/linux/kernel.h
[linux-2.6/mini2440.git] / fs / fcntl.c
blobbfd776509a7271e324c5559397542c36c4564a34
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/fdtable.h>
13 #include <linux/capability.h>
14 #include <linux/dnotify.h>
15 #include <linux/smp_lock.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/security.h>
19 #include <linux/ptrace.h>
20 #include <linux/signal.h>
21 #include <linux/rcupdate.h>
22 #include <linux/pid_namespace.h>
24 #include <asm/poll.h>
25 #include <asm/siginfo.h>
26 #include <asm/uaccess.h>
28 void set_close_on_exec(unsigned int fd, int flag)
30 struct files_struct *files = current->files;
31 struct fdtable *fdt;
32 spin_lock(&files->file_lock);
33 fdt = files_fdtable(files);
34 if (flag)
35 FD_SET(fd, fdt->close_on_exec);
36 else
37 FD_CLR(fd, fdt->close_on_exec);
38 spin_unlock(&files->file_lock);
41 static int get_close_on_exec(unsigned int fd)
43 struct files_struct *files = current->files;
44 struct fdtable *fdt;
45 int res;
46 rcu_read_lock();
47 fdt = files_fdtable(files);
48 res = FD_ISSET(fd, fdt->close_on_exec);
49 rcu_read_unlock();
50 return res;
54 * locate_fd finds a free file descriptor in the open_fds fdset,
55 * expanding the fd arrays if necessary. Must be called with the
56 * file_lock held for write.
59 static int locate_fd(unsigned int orig_start, int cloexec)
61 struct files_struct *files = current->files;
62 unsigned int newfd;
63 unsigned int start;
64 int error;
65 struct fdtable *fdt;
67 spin_lock(&files->file_lock);
69 error = -EINVAL;
70 if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
71 goto out;
73 repeat:
74 fdt = files_fdtable(files);
76 * Someone might have closed fd's in the range
77 * orig_start..fdt->next_fd
79 start = orig_start;
80 if (start < files->next_fd)
81 start = files->next_fd;
83 newfd = start;
84 if (start < fdt->max_fds)
85 newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
86 fdt->max_fds, start);
88 error = -EMFILE;
89 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
90 goto out;
92 error = expand_files(files, newfd);
93 if (error < 0)
94 goto out;
97 * If we needed to expand the fs array we
98 * might have blocked - try again.
100 if (error)
101 goto repeat;
103 if (start <= files->next_fd)
104 files->next_fd = newfd + 1;
106 FD_SET(newfd, fdt->open_fds);
107 if (cloexec)
108 FD_SET(newfd, fdt->close_on_exec);
109 else
110 FD_CLR(newfd, fdt->close_on_exec);
111 error = newfd;
113 out:
114 spin_unlock(&files->file_lock);
115 return error;
118 static int dupfd(struct file *file, unsigned int start, int cloexec)
120 int fd = locate_fd(start, cloexec);
121 if (fd >= 0)
122 fd_install(fd, file);
123 else
124 fput(file);
126 return fd;
129 asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
131 int err = -EBADF;
132 struct file * file, *tofree;
133 struct files_struct * files = current->files;
134 struct fdtable *fdt;
136 spin_lock(&files->file_lock);
137 if (!(file = fcheck(oldfd)))
138 goto out_unlock;
139 err = newfd;
140 if (newfd == oldfd)
141 goto out_unlock;
142 err = -EBADF;
143 if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
144 goto out_unlock;
145 get_file(file); /* We are now finished with oldfd */
147 err = expand_files(files, newfd);
148 if (err < 0)
149 goto out_fput;
151 /* To avoid races with open() and dup(), we will mark the fd as
152 * in-use in the open-file bitmap throughout the entire dup2()
153 * process. This is quite safe: do_close() uses the fd array
154 * entry, not the bitmap, to decide what work needs to be
155 * done. --sct */
156 /* Doesn't work. open() might be there first. --AV */
158 /* Yes. It's a race. In user space. Nothing sane to do */
159 err = -EBUSY;
160 fdt = files_fdtable(files);
161 tofree = fdt->fd[newfd];
162 if (!tofree && FD_ISSET(newfd, fdt->open_fds))
163 goto out_fput;
165 rcu_assign_pointer(fdt->fd[newfd], file);
166 FD_SET(newfd, fdt->open_fds);
167 FD_CLR(newfd, fdt->close_on_exec);
168 spin_unlock(&files->file_lock);
170 if (tofree)
171 filp_close(tofree, files);
172 err = newfd;
173 out:
174 return err;
175 out_unlock:
176 spin_unlock(&files->file_lock);
177 goto out;
179 out_fput:
180 spin_unlock(&files->file_lock);
181 fput(file);
182 goto out;
185 asmlinkage long sys_dup(unsigned int fildes)
187 int ret = -EBADF;
188 struct file * file = fget(fildes);
190 if (file)
191 ret = dupfd(file, 0, 0);
192 return ret;
195 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME)
197 static int setfl(int fd, struct file * filp, unsigned long arg)
199 struct inode * inode = filp->f_path.dentry->d_inode;
200 int error = 0;
203 * O_APPEND cannot be cleared if the file is marked as append-only
204 * and the file is open for write.
206 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode))
207 return -EPERM;
209 /* O_NOATIME can only be set by the owner or superuser */
210 if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME))
211 if (!is_owner_or_cap(inode))
212 return -EPERM;
214 /* required for strict SunOS emulation */
215 if (O_NONBLOCK != O_NDELAY)
216 if (arg & O_NDELAY)
217 arg |= O_NONBLOCK;
219 if (arg & O_DIRECT) {
220 if (!filp->f_mapping || !filp->f_mapping->a_ops ||
221 !filp->f_mapping->a_ops->direct_IO)
222 return -EINVAL;
225 if (filp->f_op && filp->f_op->check_flags)
226 error = filp->f_op->check_flags(arg);
227 if (error)
228 return error;
230 lock_kernel();
231 if ((arg ^ filp->f_flags) & FASYNC) {
232 if (filp->f_op && filp->f_op->fasync) {
233 error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0);
234 if (error < 0)
235 goto out;
239 filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK);
240 out:
241 unlock_kernel();
242 return error;
245 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
246 uid_t uid, uid_t euid, int force)
248 write_lock_irq(&filp->f_owner.lock);
249 if (force || !filp->f_owner.pid) {
250 put_pid(filp->f_owner.pid);
251 filp->f_owner.pid = get_pid(pid);
252 filp->f_owner.pid_type = type;
253 filp->f_owner.uid = uid;
254 filp->f_owner.euid = euid;
256 write_unlock_irq(&filp->f_owner.lock);
259 int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
260 int force)
262 int err;
264 err = security_file_set_fowner(filp);
265 if (err)
266 return err;
268 f_modown(filp, pid, type, current->uid, current->euid, force);
269 return 0;
271 EXPORT_SYMBOL(__f_setown);
273 int f_setown(struct file *filp, unsigned long arg, int force)
275 enum pid_type type;
276 struct pid *pid;
277 int who = arg;
278 int result;
279 type = PIDTYPE_PID;
280 if (who < 0) {
281 type = PIDTYPE_PGID;
282 who = -who;
284 rcu_read_lock();
285 pid = find_vpid(who);
286 result = __f_setown(filp, pid, type, force);
287 rcu_read_unlock();
288 return result;
290 EXPORT_SYMBOL(f_setown);
292 void f_delown(struct file *filp)
294 f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
297 pid_t f_getown(struct file *filp)
299 pid_t pid;
300 read_lock(&filp->f_owner.lock);
301 pid = pid_vnr(filp->f_owner.pid);
302 if (filp->f_owner.pid_type == PIDTYPE_PGID)
303 pid = -pid;
304 read_unlock(&filp->f_owner.lock);
305 return pid;
308 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
309 struct file *filp)
311 long err = -EINVAL;
313 switch (cmd) {
314 case F_DUPFD:
315 case F_DUPFD_CLOEXEC:
316 get_file(filp);
317 err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
318 break;
319 case F_GETFD:
320 err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;
321 break;
322 case F_SETFD:
323 err = 0;
324 set_close_on_exec(fd, arg & FD_CLOEXEC);
325 break;
326 case F_GETFL:
327 err = filp->f_flags;
328 break;
329 case F_SETFL:
330 err = setfl(fd, filp, arg);
331 break;
332 case F_GETLK:
333 err = fcntl_getlk(filp, (struct flock __user *) arg);
334 break;
335 case F_SETLK:
336 case F_SETLKW:
337 err = fcntl_setlk(fd, filp, cmd, (struct flock __user *) arg);
338 break;
339 case F_GETOWN:
341 * XXX If f_owner is a process group, the
342 * negative return value will get converted
343 * into an error. Oops. If we keep the
344 * current syscall conventions, the only way
345 * to fix this will be in libc.
347 err = f_getown(filp);
348 force_successful_syscall_return();
349 break;
350 case F_SETOWN:
351 err = f_setown(filp, arg, 1);
352 break;
353 case F_GETSIG:
354 err = filp->f_owner.signum;
355 break;
356 case F_SETSIG:
357 /* arg == 0 restores default behaviour. */
358 if (!valid_signal(arg)) {
359 break;
361 err = 0;
362 filp->f_owner.signum = arg;
363 break;
364 case F_GETLEASE:
365 err = fcntl_getlease(filp);
366 break;
367 case F_SETLEASE:
368 err = fcntl_setlease(fd, filp, arg);
369 break;
370 case F_NOTIFY:
371 err = fcntl_dirnotify(fd, filp, arg);
372 break;
373 default:
374 break;
376 return err;
379 asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
381 struct file *filp;
382 long err = -EBADF;
384 filp = fget(fd);
385 if (!filp)
386 goto out;
388 err = security_file_fcntl(filp, cmd, arg);
389 if (err) {
390 fput(filp);
391 return err;
394 err = do_fcntl(fd, cmd, arg, filp);
396 fput(filp);
397 out:
398 return err;
401 #if BITS_PER_LONG == 32
402 asmlinkage long sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
404 struct file * filp;
405 long err;
407 err = -EBADF;
408 filp = fget(fd);
409 if (!filp)
410 goto out;
412 err = security_file_fcntl(filp, cmd, arg);
413 if (err) {
414 fput(filp);
415 return err;
417 err = -EBADF;
419 switch (cmd) {
420 case F_GETLK64:
421 err = fcntl_getlk64(filp, (struct flock64 __user *) arg);
422 break;
423 case F_SETLK64:
424 case F_SETLKW64:
425 err = fcntl_setlk64(fd, filp, cmd,
426 (struct flock64 __user *) arg);
427 break;
428 default:
429 err = do_fcntl(fd, cmd, arg, filp);
430 break;
432 fput(filp);
433 out:
434 return err;
436 #endif
438 /* Table to convert sigio signal codes into poll band bitmaps */
440 static const long band_table[NSIGPOLL] = {
441 POLLIN | POLLRDNORM, /* POLL_IN */
442 POLLOUT | POLLWRNORM | POLLWRBAND, /* POLL_OUT */
443 POLLIN | POLLRDNORM | POLLMSG, /* POLL_MSG */
444 POLLERR, /* POLL_ERR */
445 POLLPRI | POLLRDBAND, /* POLL_PRI */
446 POLLHUP | POLLERR /* POLL_HUP */
449 static inline int sigio_perm(struct task_struct *p,
450 struct fown_struct *fown, int sig)
452 return (((fown->euid == 0) ||
453 (fown->euid == p->suid) || (fown->euid == p->uid) ||
454 (fown->uid == p->suid) || (fown->uid == p->uid)) &&
455 !security_file_send_sigiotask(p, fown, sig));
458 static void send_sigio_to_task(struct task_struct *p,
459 struct fown_struct *fown,
460 int fd,
461 int reason)
463 if (!sigio_perm(p, fown, fown->signum))
464 return;
466 switch (fown->signum) {
467 siginfo_t si;
468 default:
469 /* Queue a rt signal with the appropriate fd as its
470 value. We use SI_SIGIO as the source, not
471 SI_KERNEL, since kernel signals always get
472 delivered even if we can't queue. Failure to
473 queue in this case _should_ be reported; we fall
474 back to SIGIO in that case. --sct */
475 si.si_signo = fown->signum;
476 si.si_errno = 0;
477 si.si_code = reason;
478 /* Make sure we are called with one of the POLL_*
479 reasons, otherwise we could leak kernel stack into
480 userspace. */
481 BUG_ON((reason & __SI_MASK) != __SI_POLL);
482 if (reason - POLL_IN >= NSIGPOLL)
483 si.si_band = ~0L;
484 else
485 si.si_band = band_table[reason - POLL_IN];
486 si.si_fd = fd;
487 if (!group_send_sig_info(fown->signum, &si, p))
488 break;
489 /* fall-through: fall back on the old plain SIGIO signal */
490 case 0:
491 group_send_sig_info(SIGIO, SEND_SIG_PRIV, p);
495 void send_sigio(struct fown_struct *fown, int fd, int band)
497 struct task_struct *p;
498 enum pid_type type;
499 struct pid *pid;
501 read_lock(&fown->lock);
502 type = fown->pid_type;
503 pid = fown->pid;
504 if (!pid)
505 goto out_unlock_fown;
507 read_lock(&tasklist_lock);
508 do_each_pid_task(pid, type, p) {
509 send_sigio_to_task(p, fown, fd, band);
510 } while_each_pid_task(pid, type, p);
511 read_unlock(&tasklist_lock);
512 out_unlock_fown:
513 read_unlock(&fown->lock);
516 static void send_sigurg_to_task(struct task_struct *p,
517 struct fown_struct *fown)
519 if (sigio_perm(p, fown, SIGURG))
520 group_send_sig_info(SIGURG, SEND_SIG_PRIV, p);
523 int send_sigurg(struct fown_struct *fown)
525 struct task_struct *p;
526 enum pid_type type;
527 struct pid *pid;
528 int ret = 0;
530 read_lock(&fown->lock);
531 type = fown->pid_type;
532 pid = fown->pid;
533 if (!pid)
534 goto out_unlock_fown;
536 ret = 1;
538 read_lock(&tasklist_lock);
539 do_each_pid_task(pid, type, p) {
540 send_sigurg_to_task(p, fown);
541 } while_each_pid_task(pid, type, p);
542 read_unlock(&tasklist_lock);
543 out_unlock_fown:
544 read_unlock(&fown->lock);
545 return ret;
548 static DEFINE_RWLOCK(fasync_lock);
549 static struct kmem_cache *fasync_cache __read_mostly;
552 * fasync_helper() is used by some character device drivers (mainly mice)
553 * to set up the fasync queue. It returns negative on error, 0 if it did
554 * no changes and positive if it added/deleted the entry.
556 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
558 struct fasync_struct *fa, **fp;
559 struct fasync_struct *new = NULL;
560 int result = 0;
562 if (on) {
563 new = kmem_cache_alloc(fasync_cache, GFP_KERNEL);
564 if (!new)
565 return -ENOMEM;
567 write_lock_irq(&fasync_lock);
568 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
569 if (fa->fa_file == filp) {
570 if(on) {
571 fa->fa_fd = fd;
572 kmem_cache_free(fasync_cache, new);
573 } else {
574 *fp = fa->fa_next;
575 kmem_cache_free(fasync_cache, fa);
576 result = 1;
578 goto out;
582 if (on) {
583 new->magic = FASYNC_MAGIC;
584 new->fa_file = filp;
585 new->fa_fd = fd;
586 new->fa_next = *fapp;
587 *fapp = new;
588 result = 1;
590 out:
591 write_unlock_irq(&fasync_lock);
592 return result;
595 EXPORT_SYMBOL(fasync_helper);
597 void __kill_fasync(struct fasync_struct *fa, int sig, int band)
599 while (fa) {
600 struct fown_struct * fown;
601 if (fa->magic != FASYNC_MAGIC) {
602 printk(KERN_ERR "kill_fasync: bad magic number in "
603 "fasync_struct!\n");
604 return;
606 fown = &fa->fa_file->f_owner;
607 /* Don't send SIGURG to processes which have not set a
608 queued signum: SIGURG has its own default signalling
609 mechanism. */
610 if (!(sig == SIGURG && fown->signum == 0))
611 send_sigio(fown, fa->fa_fd, band);
612 fa = fa->fa_next;
616 EXPORT_SYMBOL(__kill_fasync);
618 void kill_fasync(struct fasync_struct **fp, int sig, int band)
620 /* First a quick test without locking: usually
621 * the list is empty.
623 if (*fp) {
624 read_lock(&fasync_lock);
625 /* reread *fp after obtaining the lock */
626 __kill_fasync(*fp, sig, band);
627 read_unlock(&fasync_lock);
630 EXPORT_SYMBOL(kill_fasync);
632 static int __init fasync_init(void)
634 fasync_cache = kmem_cache_create("fasync_cache",
635 sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
636 return 0;
639 module_init(fasync_init)