xref: /minix3/minix/lib/libc/sys/sem.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #define __USE_MISC
2*433d6423SLionel Sambuc #define _SYSTEM	1
3*433d6423SLionel Sambuc #define _MINIX_SYSTEM	1
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc #include <sys/cdefs.h>
6*433d6423SLionel Sambuc #include <lib.h>
7*433d6423SLionel Sambuc #include "namespace.h"
8*433d6423SLionel Sambuc 
9*433d6423SLionel Sambuc #include <lib.h>
10*433d6423SLionel Sambuc #include <minix/rs.h>
11*433d6423SLionel Sambuc 
12*433d6423SLionel Sambuc #include <sys/types.h>
13*433d6423SLionel Sambuc #include <sys/ipc.h>
14*433d6423SLionel Sambuc #include <sys/sem.h>
15*433d6423SLionel Sambuc #include <stdlib.h>
16*433d6423SLionel Sambuc #include <stdarg.h>
17*433d6423SLionel Sambuc #include <errno.h>
18*433d6423SLionel Sambuc #include <string.h>
19*433d6423SLionel Sambuc 
20*433d6423SLionel Sambuc static int get_ipc_endpt(endpoint_t *pt)
21*433d6423SLionel Sambuc {
22*433d6423SLionel Sambuc 	return minix_rs_lookup("ipc", pt);
23*433d6423SLionel Sambuc }
24*433d6423SLionel Sambuc 
25*433d6423SLionel Sambuc /* Get semaphore.  */
26*433d6423SLionel Sambuc int semget(key_t key, int nsems, int semflag)
27*433d6423SLionel Sambuc {
28*433d6423SLionel Sambuc 	message m;
29*433d6423SLionel Sambuc 	endpoint_t ipc_pt;
30*433d6423SLionel Sambuc 	int r;
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
33*433d6423SLionel Sambuc 		errno = ENOSYS;
34*433d6423SLionel Sambuc 		return -1;
35*433d6423SLionel Sambuc 	}
36*433d6423SLionel Sambuc 
37*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
38*433d6423SLionel Sambuc 	m.m_lc_ipc_semget.key = key;
39*433d6423SLionel Sambuc 	m.m_lc_ipc_semget.nr = nsems;
40*433d6423SLionel Sambuc 	m.m_lc_ipc_semget.flag = semflag;
41*433d6423SLionel Sambuc 
42*433d6423SLionel Sambuc 	r = _syscall(ipc_pt, IPC_SEMGET, &m);
43*433d6423SLionel Sambuc 	if (r != OK)
44*433d6423SLionel Sambuc 		return r;
45*433d6423SLionel Sambuc 
46*433d6423SLionel Sambuc 	return m.m_lc_ipc_semget.retid;
47*433d6423SLionel Sambuc }
48*433d6423SLionel Sambuc 
49*433d6423SLionel Sambuc /* Semaphore control operation.  */
50*433d6423SLionel Sambuc int semctl(int semid, int semnum, int cmd, ...)
51*433d6423SLionel Sambuc {
52*433d6423SLionel Sambuc 	message m;
53*433d6423SLionel Sambuc 	endpoint_t ipc_pt;
54*433d6423SLionel Sambuc 	va_list ap;
55*433d6423SLionel Sambuc 	int r;
56*433d6423SLionel Sambuc 
57*433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
58*433d6423SLionel Sambuc 		errno = ENOSYS;
59*433d6423SLionel Sambuc 		return -1;
60*433d6423SLionel Sambuc 	}
61*433d6423SLionel Sambuc 
62*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
63*433d6423SLionel Sambuc 	m.m_lc_ipc_semctl.id = semid;
64*433d6423SLionel Sambuc 	m.m_lc_ipc_semctl.num = semnum;
65*433d6423SLionel Sambuc 	m.m_lc_ipc_semctl.cmd = cmd;
66*433d6423SLionel Sambuc 	va_start(ap, cmd);
67*433d6423SLionel Sambuc 	if (cmd == IPC_STAT || cmd == IPC_SET || cmd == IPC_INFO ||
68*433d6423SLionel Sambuc 		cmd == SEM_INFO || cmd == SEM_STAT || cmd == GETALL ||
69*433d6423SLionel Sambuc 		cmd == SETALL || cmd == SETVAL)
70*433d6423SLionel Sambuc 		m.m_lc_ipc_semctl.opt = (long) va_arg(ap, long);
71*433d6423SLionel Sambuc 	va_end(ap);
72*433d6423SLionel Sambuc 
73*433d6423SLionel Sambuc 	r = _syscall(ipc_pt, IPC_SEMCTL, &m);
74*433d6423SLionel Sambuc 	if ((r != -1) && (cmd == GETNCNT || cmd == GETZCNT || cmd == GETPID ||
75*433d6423SLionel Sambuc 		cmd == GETVAL || cmd == IPC_INFO || cmd == SEM_INFO ||
76*433d6423SLionel Sambuc 		cmd == SEM_STAT))
77*433d6423SLionel Sambuc 		return m.m_lc_ipc_semctl.ret;
78*433d6423SLionel Sambuc 	return r;
79*433d6423SLionel Sambuc }
80*433d6423SLionel Sambuc 
81*433d6423SLionel Sambuc /* Operate on semaphore.  */
82*433d6423SLionel Sambuc int semop(int semid, struct sembuf *sops, size_t nsops)
83*433d6423SLionel Sambuc {
84*433d6423SLionel Sambuc 	message m;
85*433d6423SLionel Sambuc 	endpoint_t ipc_pt;
86*433d6423SLionel Sambuc 
87*433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
88*433d6423SLionel Sambuc 		errno = ENOSYS;
89*433d6423SLionel Sambuc 		return -1;
90*433d6423SLionel Sambuc 	}
91*433d6423SLionel Sambuc 
92*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
93*433d6423SLionel Sambuc 	m.m_lc_ipc_semop.id = semid;
94*433d6423SLionel Sambuc 	m.m_lc_ipc_semop.ops = sops;
95*433d6423SLionel Sambuc 	m.m_lc_ipc_semop.size = nsops;
96*433d6423SLionel Sambuc 
97*433d6423SLionel Sambuc 	return _syscall(ipc_pt, IPC_SEMOP, &m);
98*433d6423SLionel Sambuc }
99*433d6423SLionel Sambuc 
100