opendir change: refinement
[minix.git] / lib / libc / sys-minix / sem.c
blobafb9bb828022cf20b1d4ed969ca7fa15b5f2fc3c
1 #define __USE_MISC
2 #define _SYSTEM 1
3 #define _MINIX 1
5 #include <sys/cdefs.h>
6 #include <lib.h>
7 #include "namespace.h"
9 #include <lib.h>
10 #include <minix/rs.h>
12 #include <sys/types.h>
13 #include <sys/ipc.h>
14 #include <sys/sem.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <errno.h>
19 static int get_ipc_endpt(endpoint_t *pt)
21 return minix_rs_lookup("ipc", pt);
24 /* Get semaphore. */
25 int semget(key_t key, int nsems, int semflag)
27 message m;
28 endpoint_t ipc_pt;
29 int r;
31 if (get_ipc_endpt(&ipc_pt) != OK) {
32 errno = ENOSYS;
33 return -1;
36 m.SEMGET_KEY = key;
37 m.SEMGET_NR = nsems;
38 m.SEMGET_FLAG = semflag;
40 r = _syscall(ipc_pt, IPC_SEMGET, &m);
41 if (r != OK)
42 return r;
44 return m.SEMGET_RETID;
47 /* Semaphore control operation. */
48 int semctl(int semid, int semnum, int cmd, ...)
50 message m;
51 endpoint_t ipc_pt;
52 va_list ap;
53 int r;
55 if (get_ipc_endpt(&ipc_pt) != OK) {
56 errno = ENOSYS;
57 return -1;
60 m.SEMCTL_ID = semid;
61 m.SEMCTL_NUM = semnum;
62 m.SEMCTL_CMD = cmd;
63 va_start(ap, cmd);
64 if (cmd == IPC_STAT || cmd == IPC_SET || cmd == IPC_INFO ||
65 cmd == SEM_INFO || cmd == SEM_STAT || cmd == GETALL ||
66 cmd == SETALL || cmd == SETVAL)
67 m.SEMCTL_OPT = (long) va_arg(ap, long);
68 va_end(ap);
70 r = _syscall(ipc_pt, IPC_SEMCTL, &m);
71 if ((r != -1) && (cmd == GETNCNT || cmd == GETZCNT || cmd == GETPID ||
72 cmd == GETVAL || cmd == IPC_INFO || cmd == SEM_INFO ||
73 cmd == SEM_STAT))
74 return m.SHMCTL_RET;
75 return r;
78 /* Operate on semaphore. */
79 int semop(int semid, struct sembuf *sops, size_t nsops)
81 message m;
82 endpoint_t ipc_pt;
84 if (get_ipc_endpt(&ipc_pt) != OK) {
85 errno = ENOSYS;
86 return -1;
89 m.SEMOP_ID = semid;
90 m.SEMOP_OPS = (long) sops;
91 m.SEMOP_SIZE = nsops;
93 return _syscall(ipc_pt, IPC_SEMOP, &m);