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