xref: /minix3/minix/lib/libc/sys/sem.c (revision 4d272e5a970c4e7d6700cabd52e337e37404b70e)
1433d6423SLionel Sambuc #define _SYSTEM	1
2433d6423SLionel Sambuc #define _MINIX_SYSTEM	1
3433d6423SLionel Sambuc 
4433d6423SLionel Sambuc #include <sys/cdefs.h>
5433d6423SLionel Sambuc #include <lib.h>
6433d6423SLionel Sambuc #include "namespace.h"
7433d6423SLionel Sambuc 
8433d6423SLionel Sambuc #include <lib.h>
9433d6423SLionel Sambuc #include <minix/rs.h>
10433d6423SLionel Sambuc 
11433d6423SLionel Sambuc #include <sys/types.h>
12433d6423SLionel Sambuc #include <sys/ipc.h>
13433d6423SLionel Sambuc #include <sys/sem.h>
14433d6423SLionel Sambuc #include <stdlib.h>
15433d6423SLionel Sambuc #include <stdarg.h>
16433d6423SLionel Sambuc #include <errno.h>
17433d6423SLionel Sambuc #include <string.h>
18433d6423SLionel Sambuc 
get_ipc_endpt(endpoint_t * pt)19433d6423SLionel Sambuc static int get_ipc_endpt(endpoint_t *pt)
20433d6423SLionel Sambuc {
21433d6423SLionel Sambuc 	return minix_rs_lookup("ipc", pt);
22433d6423SLionel Sambuc }
23433d6423SLionel Sambuc 
24433d6423SLionel Sambuc /* Get semaphore.  */
semget(key_t key,int nsems,int semflag)25433d6423SLionel Sambuc int semget(key_t key, int nsems, int semflag)
26433d6423SLionel Sambuc {
27433d6423SLionel Sambuc 	message m;
28433d6423SLionel Sambuc 	endpoint_t ipc_pt;
29433d6423SLionel Sambuc 	int r;
30433d6423SLionel Sambuc 
31433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
32433d6423SLionel Sambuc 		errno = ENOSYS;
33433d6423SLionel Sambuc 		return -1;
34433d6423SLionel Sambuc 	}
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
37433d6423SLionel Sambuc 	m.m_lc_ipc_semget.key = key;
38433d6423SLionel Sambuc 	m.m_lc_ipc_semget.nr = nsems;
39433d6423SLionel Sambuc 	m.m_lc_ipc_semget.flag = semflag;
40433d6423SLionel Sambuc 
41433d6423SLionel Sambuc 	r = _syscall(ipc_pt, IPC_SEMGET, &m);
42433d6423SLionel Sambuc 	if (r != OK)
43433d6423SLionel Sambuc 		return r;
44433d6423SLionel Sambuc 
45433d6423SLionel Sambuc 	return m.m_lc_ipc_semget.retid;
46433d6423SLionel Sambuc }
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc /* Semaphore control operation.  */
semctl(int semid,int semnum,int cmd,...)49433d6423SLionel Sambuc int semctl(int semid, int semnum, int cmd, ...)
50433d6423SLionel Sambuc {
51433d6423SLionel Sambuc 	message m;
52433d6423SLionel Sambuc 	endpoint_t ipc_pt;
53433d6423SLionel Sambuc 	va_list ap;
54433d6423SLionel Sambuc 	int r;
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
57433d6423SLionel Sambuc 		errno = ENOSYS;
58433d6423SLionel Sambuc 		return -1;
59433d6423SLionel Sambuc 	}
60433d6423SLionel Sambuc 
61433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
62433d6423SLionel Sambuc 	m.m_lc_ipc_semctl.id = semid;
63433d6423SLionel Sambuc 	m.m_lc_ipc_semctl.num = semnum;
64433d6423SLionel Sambuc 	m.m_lc_ipc_semctl.cmd = cmd;
65433d6423SLionel Sambuc 	va_start(ap, cmd);
66*4d272e5aSDavid van Moolenbroek 	switch (cmd) {
67*4d272e5aSDavid van Moolenbroek 	case SETVAL:
68*4d272e5aSDavid van Moolenbroek 		m.m_lc_ipc_semctl.opt = (vir_bytes)va_arg(ap, int);
69*4d272e5aSDavid van Moolenbroek 		break;
70*4d272e5aSDavid van Moolenbroek 	case IPC_STAT:
71*4d272e5aSDavid van Moolenbroek 	case IPC_SET:
72*4d272e5aSDavid van Moolenbroek 	case IPC_INFO:
73*4d272e5aSDavid van Moolenbroek 	case SEM_INFO:
74*4d272e5aSDavid van Moolenbroek 	case SEM_STAT:
75*4d272e5aSDavid van Moolenbroek 	case GETALL:
76*4d272e5aSDavid van Moolenbroek 	case SETALL:
77*4d272e5aSDavid van Moolenbroek 		m.m_lc_ipc_semctl.opt = (vir_bytes)va_arg(ap, void *);
78*4d272e5aSDavid van Moolenbroek 		break;
79*4d272e5aSDavid van Moolenbroek 	default:
80*4d272e5aSDavid van Moolenbroek 		m.m_lc_ipc_semctl.opt = 0;
81*4d272e5aSDavid van Moolenbroek 		break;
82*4d272e5aSDavid van Moolenbroek 	}
83433d6423SLionel Sambuc 	va_end(ap);
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc 	r = _syscall(ipc_pt, IPC_SEMCTL, &m);
86433d6423SLionel Sambuc 	if ((r != -1) && (cmd == GETNCNT || cmd == GETZCNT || cmd == GETPID ||
87433d6423SLionel Sambuc 		cmd == GETVAL || cmd == IPC_INFO || cmd == SEM_INFO ||
88433d6423SLionel Sambuc 		cmd == SEM_STAT))
89433d6423SLionel Sambuc 		return m.m_lc_ipc_semctl.ret;
90433d6423SLionel Sambuc 	return r;
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc 
93433d6423SLionel Sambuc /* Operate on semaphore.  */
semop(int semid,struct sembuf * sops,size_t nsops)94433d6423SLionel Sambuc int semop(int semid, struct sembuf *sops, size_t nsops)
95433d6423SLionel Sambuc {
96433d6423SLionel Sambuc 	message m;
97433d6423SLionel Sambuc 	endpoint_t ipc_pt;
98433d6423SLionel Sambuc 
99433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
100433d6423SLionel Sambuc 		errno = ENOSYS;
101433d6423SLionel Sambuc 		return -1;
102433d6423SLionel Sambuc 	}
103433d6423SLionel Sambuc 
104433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
105433d6423SLionel Sambuc 	m.m_lc_ipc_semop.id = semid;
106433d6423SLionel Sambuc 	m.m_lc_ipc_semop.ops = sops;
107433d6423SLionel Sambuc 	m.m_lc_ipc_semop.size = nsops;
108433d6423SLionel Sambuc 
109433d6423SLionel Sambuc 	return _syscall(ipc_pt, IPC_SEMOP, &m);
110433d6423SLionel Sambuc }
111433d6423SLionel Sambuc 
112