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