• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 & ~IPC_TIME64) {
22 	case SETVAL: case GETALL: case SETALL: case IPC_SET:
23 	case IPC_INFO: case SEM_INFO:
24 	case IPC_STAT & ~IPC_TIME64:
25 	case SEM_STAT & ~IPC_TIME64:
26 	case SEM_STAT_ANY & ~IPC_TIME64:
27 		va_start(ap, cmd);
28 		arg = va_arg(ap, union semun);
29 		va_end(ap);
30 	}
31 #ifdef SYSCALL_IPC_BROKEN_MODE
32 	struct semid_ds tmp;
33 	if (cmd == IPC_SET) {
34 		tmp = *arg.buf;
35 		tmp.sem_perm.mode *= 0x10000U;
36 		arg.buf = &tmp;
37 	}
38 #endif
39 #ifndef SYS_ipc
40 	int r = __syscall(SYS_semctl, id, num, IPC_CMD(cmd), arg.buf);
41 #else
42 	int r = __syscall(SYS_ipc, IPCOP_semctl, id, num, IPC_CMD(cmd), &arg.buf);
43 #endif
44 #ifdef SYSCALL_IPC_BROKEN_MODE
45 	if (r >= 0) switch (cmd | IPC_TIME64) {
46 	case IPC_STAT:
47 	case SEM_STAT:
48 	case SEM_STAT_ANY:
49 		arg.buf->sem_perm.mode >>= 16;
50 	}
51 #endif
52 #if IPC_TIME64
53 	if (r >= 0 && (cmd&IPC_TIME64)) {
54 		IPC_HILO(arg.buf, sem_otime);
55 		IPC_HILO(arg.buf, sem_ctime);
56 	}
57 #endif
58 	return __syscall_ret(r);
59 }
60