sys_pipe(): fix file descriptor leaks
[safe/jmp/linux-2.6] / arch / cris / kernel / sys_cris.c
1 /* $Id: sys_cris.c,v 1.6 2004/03/11 11:38:40 starvik Exp $
2  *
3  * linux/arch/cris/kernel/sys_cris.c
4  *
5  * This file contains various random system calls that
6  * have a non-standard calling sequence on some platforms.
7  * Since we don't have to do any backwards compatibility, our
8  * versions are done in the most "normal" way possible.
9  *
10  */
11
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/syscalls.h>
15 #include <linux/mm.h>
16 #include <linux/fs.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/sem.h>
20 #include <linux/msg.h>
21 #include <linux/shm.h>
22 #include <linux/stat.h>
23 #include <linux/mman.h>
24 #include <linux/file.h>
25 #include <linux/ipc.h>
26
27 #include <asm/uaccess.h>
28 #include <asm/segment.h>
29
30 /*
31  * sys_pipe() is the normal C calling standard for creating
32  * a pipe. It's not the way Unix traditionally does this, though.
33  */
34 asmlinkage int sys_pipe(unsigned long __user * fildes)
35 {
36         int fd[2];
37         int error;
38
39         lock_kernel();
40         error = do_pipe(fd);
41         unlock_kernel();
42         if (!error) {
43                 if (copy_to_user(fildes, fd, 2*sizeof(int))) {
44                         sys_close(fd[0]);
45                         sys_close(fd[1]);
46                         error = -EFAULT;
47                 }
48         }
49         return error;
50 }
51
52 /* common code for old and new mmaps */
53 static inline long
54 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
55         unsigned long flags, unsigned long fd, unsigned long pgoff)
56 {
57         int error = -EBADF;
58         struct file * file = NULL;
59
60         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
61         if (!(flags & MAP_ANONYMOUS)) {
62                 file = fget(fd);
63                 if (!file)
64                         goto out;
65         }
66
67         down_write(&current->mm->mmap_sem);
68         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
69         up_write(&current->mm->mmap_sem);
70
71         if (file)
72                 fput(file);
73 out:
74         return error;
75 }
76
77 asmlinkage unsigned long old_mmap(unsigned long __user *args)
78 {        
79         unsigned long buffer[6];
80         int err = -EFAULT;
81
82         if (copy_from_user(&buffer, args, sizeof(buffer)))
83                 goto out;
84
85         err = -EINVAL;
86         if (buffer[5] & ~PAGE_MASK) /* verify that offset is on page boundary */
87                 goto out;
88
89         err = do_mmap2(buffer[0], buffer[1], buffer[2], buffer[3],
90                        buffer[4], buffer[5] >> PAGE_SHIFT);
91 out:
92         return err;
93 }
94
95 asmlinkage long
96 sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
97           unsigned long flags, unsigned long fd, unsigned long pgoff)
98 {
99         return do_mmap2(addr, len, prot, flags, fd, pgoff);
100 }
101
102 /*
103  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
104  *
105  * This is really horribly ugly. (same as arch/i386)
106  */
107
108 asmlinkage int sys_ipc (uint call, int first, int second,
109                         int third, void __user *ptr, long fifth)
110 {
111         int version, ret;
112
113         version = call >> 16; /* hack for backward compatibility */
114         call &= 0xffff;
115
116         switch (call) {
117         case SEMOP:
118                 return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
119         case SEMTIMEDOP:
120                 return sys_semtimedop(first, (struct sembuf __user *)ptr, second,
121                                         (const struct timespec __user *)fifth);
122
123         case SEMGET:
124                 return sys_semget (first, second, third);
125         case SEMCTL: {
126                 union semun fourth;
127                 if (!ptr)
128                         return -EINVAL;
129                 if (get_user(fourth.__pad, (void * __user *) ptr))
130                         return -EFAULT;
131                 return sys_semctl (first, second, third, fourth);
132         }
133
134         case MSGSND:
135                 return sys_msgsnd (first, (struct msgbuf __user *) ptr, 
136                                    second, third);
137         case MSGRCV:
138                 switch (version) {
139                 case 0: {
140                         struct ipc_kludge tmp;
141                         if (!ptr)
142                                 return -EINVAL;
143                         
144                         if (copy_from_user(&tmp,
145                                            (struct ipc_kludge __user *) ptr, 
146                                            sizeof (tmp)))
147                                 return -EFAULT;
148                         return sys_msgrcv (first, tmp.msgp, second,
149                                            tmp.msgtyp, third);
150                 }
151                 default:
152                         return sys_msgrcv (first,
153                                            (struct msgbuf __user *) ptr,
154                                            second, fifth, third);
155                 }
156         case MSGGET:
157                 return sys_msgget ((key_t) first, second);
158         case MSGCTL:
159                 return sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
160
161         case SHMAT: {
162                 ulong raddr;
163                 ret = do_shmat (first, (char __user *) ptr, second, &raddr);
164                 if (ret)
165                         return ret;
166                 return put_user (raddr, (ulong __user *) third);
167         }
168         case SHMDT: 
169                 return sys_shmdt ((char __user *)ptr);
170         case SHMGET:
171                 return sys_shmget (first, second, third);
172         case SHMCTL:
173                 return sys_shmctl (first, second,
174                                    (struct shmid_ds __user *) ptr);
175         default:
176                 return -ENOSYS;
177         }
178 }