syscall.c revision 6aa211e8
1/*
2 * sys_ipc() is the old de-multiplexer for the SysV IPC calls.
3 *
4 * This is really horribly ugly, and new architectures should just wire up
5 * the individual syscalls instead.
6 */
7#include <linux/unistd.h>
8#include <linux/syscalls.h>
9
10#ifdef __ARCH_WANT_SYS_IPC
11#include <linux/errno.h>
12#include <linux/ipc.h>
13#include <linux/shm.h>
14#include <linux/uaccess.h>
15
16SYSCALL_DEFINE6(ipc, unsigned int, call, int, first, unsigned long, second,
17		unsigned long, third, void __user *, ptr, long, fifth)
18{
19	int version, ret;
20
21	version = call >> 16; /* hack for backward compatibility */
22	call &= 0xffff;
23
24	switch (call) {
25	case SEMOP:
26		return sys_semtimedop(first, (struct sembuf __user *)ptr,
27				      second, NULL);
28	case SEMTIMEDOP:
29		return sys_semtimedop(first, (struct sembuf __user *)ptr,
30				      second,
31				      (const struct timespec __user *)fifth);
32
33	case SEMGET:
34		return sys_semget(first, second, third);
35	case SEMCTL: {
36		unsigned long arg;
37		if (!ptr)
38			return -EINVAL;
39		if (get_user(arg, (unsigned long __user *) ptr))
40			return -EFAULT;
41		return sys_semctl(first, second, third, arg);
42	}
43
44	case MSGSND:
45		return sys_msgsnd(first, (struct msgbuf __user *) ptr,
46				  second, third);
47	case MSGRCV:
48		switch (version) {
49		case 0: {
50			struct ipc_kludge tmp;
51			if (!ptr)
52				return -EINVAL;
53
54			if (copy_from_user(&tmp,
55					   (struct ipc_kludge __user *) ptr,
56					   sizeof(tmp)))
57				return -EFAULT;
58			return sys_msgrcv(first, tmp.msgp, second,
59					   tmp.msgtyp, third);
60		}
61		default:
62			return sys_msgrcv(first,
63					   (struct msgbuf __user *) ptr,
64					   second, fifth, third);
65		}
66	case MSGGET:
67		return sys_msgget((key_t) first, second);
68	case MSGCTL:
69		return sys_msgctl(first, second, (struct msqid_ds __user *)ptr);
70
71	case SHMAT:
72		switch (version) {
73		default: {
74			unsigned long raddr;
75			ret = do_shmat(first, (char __user *)ptr,
76				       second, &raddr, SHMLBA);
77			if (ret)
78				return ret;
79			return put_user(raddr, (unsigned long __user *) third);
80		}
81		case 1:
82			/*
83			 * This was the entry point for kernel-originating calls
84			 * from iBCS2 in 2.2 days.
85			 */
86			return -EINVAL;
87		}
88	case SHMDT:
89		return sys_shmdt((char __user *)ptr);
90	case SHMGET:
91		return sys_shmget(first, second, third);
92	case SHMCTL:
93		return sys_shmctl(first, second,
94				   (struct shmid_ds __user *) ptr);
95	default:
96		return -ENOSYS;
97	}
98}
99#endif
100
101#ifdef CONFIG_COMPAT
102#include <linux/compat.h>
103
104#ifndef COMPAT_SHMLBA
105#define COMPAT_SHMLBA	SHMLBA
106#endif
107
108struct compat_ipc_kludge {
109	compat_uptr_t msgp;
110	compat_long_t msgtyp;
111};
112
113#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
114COMPAT_SYSCALL_DEFINE6(ipc, u32, call, int, first, int, second,
115	u32, third, compat_uptr_t, ptr, u32, fifth)
116{
117	int version;
118	u32 pad;
119
120	version = call >> 16; /* hack for backward compatibility */
121	call &= 0xffff;
122
123	switch (call) {
124	case SEMOP:
125		/* struct sembuf is the same on 32 and 64bit :)) */
126		return sys_semtimedop(first, compat_ptr(ptr), second, NULL);
127	case SEMTIMEDOP:
128		return compat_sys_semtimedop(first, compat_ptr(ptr), second,
129						compat_ptr(fifth));
130	case SEMGET:
131		return sys_semget(first, second, third);
132	case SEMCTL:
133		if (!ptr)
134			return -EINVAL;
135		if (get_user(pad, (u32 __user *) compat_ptr(ptr)))
136			return -EFAULT;
137		return compat_sys_semctl(first, second, third, pad);
138
139	case MSGSND:
140		return compat_sys_msgsnd(first, ptr, second, third);
141
142	case MSGRCV: {
143		void __user *uptr = compat_ptr(ptr);
144
145		if (first < 0 || second < 0)
146			return -EINVAL;
147
148		if (!version) {
149			struct compat_ipc_kludge ipck;
150			if (!uptr)
151				return -EINVAL;
152			if (copy_from_user(&ipck, uptr, sizeof(ipck)))
153				return -EFAULT;
154			return compat_sys_msgrcv(first, ipck.msgp, second,
155						 ipck.msgtyp, third);
156		}
157		return compat_sys_msgrcv(first, ptr, second, fifth, third);
158	}
159	case MSGGET:
160		return sys_msgget(first, second);
161	case MSGCTL:
162		return compat_sys_msgctl(first, second, compat_ptr(ptr));
163
164	case SHMAT: {
165		int err;
166		unsigned long raddr;
167
168		if (version == 1)
169			return -EINVAL;
170		err = do_shmat(first, compat_ptr(ptr), second, &raddr,
171			       COMPAT_SHMLBA);
172		if (err < 0)
173			return err;
174		return put_user(raddr, (compat_ulong_t __user *)compat_ptr(third));
175	}
176	case SHMDT:
177		return sys_shmdt(compat_ptr(ptr));
178	case SHMGET:
179		return sys_shmget(first, (unsigned)second, third);
180	case SHMCTL:
181		return compat_sys_shmctl(first, second, compat_ptr(ptr));
182	}
183
184	return -ENOSYS;
185}
186#endif
187#endif
188