1 #include <sys/sem.h>
2 #include <stdarg.h>
3 #include <endian.h>
4 #include <unsupported_api.h>
5 #include "syscall.h"
6 #include "ipc.h"
7
8 #if __BYTE_ORDER != __BIG_ENDIAN
9 #undef SYSCALL_IPC_BROKEN_MODE
10 #endif
11
12 union semun {
13 int val;
14 struct semid_ds *buf;
15 unsigned short *array;
16 };
17
semctl(int id,int num,int cmd,...)18 int semctl(int id, int num, int cmd, ...)
19 {
20 UNSUPPORTED_API_VOID(LITEOS_A);
21 union semun arg = {0};
22 va_list ap;
23 switch (cmd & ~IPC_TIME64) {
24 case SETVAL: case GETALL: case SETALL: case IPC_SET:
25 case IPC_INFO: case SEM_INFO:
26 case IPC_STAT & ~IPC_TIME64:
27 case SEM_STAT & ~IPC_TIME64:
28 case SEM_STAT_ANY & ~IPC_TIME64:
29 va_start(ap, cmd);
30 arg = va_arg(ap, union semun);
31 va_end(ap);
32 }
33 #if IPC_TIME64
34 struct semid_ds out, *orig;
35 if (cmd&IPC_TIME64) {
36 out = (struct semid_ds){0};
37 orig = arg.buf;
38 arg.buf = &out;
39 }
40 #endif
41 #ifdef SYSCALL_IPC_BROKEN_MODE
42 struct semid_ds tmp;
43 if (cmd == IPC_SET) {
44 tmp = *arg.buf;
45 tmp.sem_perm.mode *= 0x10000U;
46 arg.buf = &tmp;
47 }
48 #endif
49 #ifndef SYS_ipc
50 int r = __syscall(SYS_semctl, id, num, IPC_CMD(cmd), arg.buf);
51 #else
52 int r = __syscall(SYS_ipc, IPCOP_semctl, id, num, IPC_CMD(cmd), &arg.buf);
53 #endif
54 #ifdef SYSCALL_IPC_BROKEN_MODE
55 if (r >= 0) switch (cmd | IPC_TIME64) {
56 case IPC_STAT:
57 case SEM_STAT:
58 case SEM_STAT_ANY:
59 arg.buf->sem_perm.mode >>= 16;
60 }
61 #endif
62 #if IPC_TIME64
63 if (r >= 0 && (cmd&IPC_TIME64)) {
64 arg.buf = orig;
65 *arg.buf = out;
66 IPC_HILO(arg.buf, sem_otime);
67 IPC_HILO(arg.buf, sem_ctime);
68 }
69 #endif
70 return __syscall_ret(r);
71 }
72