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