1 #include <sys/sem.h>
2 #include <stdarg.h>
3 #include <endian.h>
4 #include "syscall.h"
5 #include "ipc.h"
6 
7 #if __BYTE_ORDER != __BIG_ENDIAN
8 #undef SYSCALL_IPC_BROKEN_MODE
9 #endif
10 
11 union semun {
12 	int val;
13 	struct semid_ds *buf;
14 	unsigned short *array;
15 };
16 
semctl(int id,int num,int cmd,...)17 int semctl(int id, int num, int cmd, ...)
18 {
19 	union semun arg = {0};
20 	va_list ap;
21 	switch (cmd) {
22 	case SETVAL: case GETALL: case SETALL: case IPC_STAT: case IPC_SET:
23 	case IPC_INFO: case SEM_INFO: case SEM_STAT:
24 		va_start(ap, cmd);
25 		arg = va_arg(ap, union semun);
26 		va_end(ap);
27 	}
28 #ifdef SYSCALL_IPC_BROKEN_MODE
29 	struct semid_ds tmp;
30 	if (cmd == IPC_SET) {
31 		tmp = *arg.buf;
32 		tmp.sem_perm.mode *= 0x10000U;
33 		arg.buf = &tmp;
34 	}
35 #endif
36 #ifndef SYS_ipc
37 	int r = __syscall(SYS_semctl, id, num, cmd | IPC_64, arg.buf);
38 #else
39 	int r = __syscall(SYS_ipc, IPCOP_semctl, id, num, cmd | IPC_64, &arg.buf);
40 #endif
41 #ifdef SYSCALL_IPC_BROKEN_MODE
42 	if (r >= 0) switch (cmd) {
43 	case IPC_STAT:
44 	case SEM_STAT:
45 	case SEM_STAT_ANY:
46 		arg.buf->sem_perm.mode >>= 16;
47 	}
48 #endif
49 	return __syscall_ret(r);
50 }
51