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