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