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