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