xref: /minix3/minix/lib/libc/sys/shmat.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #define _SYSTEM	1
2*433d6423SLionel Sambuc #define _MINIX_SYSTEM	1
3*433d6423SLionel Sambuc 
4*433d6423SLionel Sambuc #include <sys/cdefs.h>
5*433d6423SLionel Sambuc #include <lib.h>
6*433d6423SLionel Sambuc #include "namespace.h"
7*433d6423SLionel Sambuc 
8*433d6423SLionel Sambuc #include <minix/rs.h>
9*433d6423SLionel Sambuc 
10*433d6423SLionel Sambuc #include <lib.h>
11*433d6423SLionel Sambuc #include <sys/types.h>
12*433d6423SLionel Sambuc #include <sys/ipc.h>
13*433d6423SLionel Sambuc #include <sys/shm.h>
14*433d6423SLionel Sambuc #include <stdlib.h>
15*433d6423SLionel Sambuc #include <errno.h>
16*433d6423SLionel Sambuc #include <string.h>
17*433d6423SLionel Sambuc 
get_ipc_endpt(endpoint_t * pt)18*433d6423SLionel Sambuc static int get_ipc_endpt(endpoint_t *pt)
19*433d6423SLionel Sambuc {
20*433d6423SLionel Sambuc 	return minix_rs_lookup("ipc", pt);
21*433d6423SLionel Sambuc }
22*433d6423SLionel Sambuc 
23*433d6423SLionel Sambuc /* Attach shared memory segment. */
shmat(int shmid,const void * shmaddr,int shmflg)24*433d6423SLionel Sambuc void *shmat(int shmid, const void *shmaddr, int shmflg)
25*433d6423SLionel Sambuc {
26*433d6423SLionel Sambuc 	message m;
27*433d6423SLionel Sambuc 	endpoint_t ipc_pt;
28*433d6423SLionel Sambuc 	int r;
29*433d6423SLionel Sambuc 
30*433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
31*433d6423SLionel Sambuc 		errno = ENOSYS;
32*433d6423SLionel Sambuc 		return NULL;
33*433d6423SLionel Sambuc 	}
34*433d6423SLionel Sambuc 
35*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
36*433d6423SLionel Sambuc 	m.m_lc_ipc_shmat.id = shmid;
37*433d6423SLionel Sambuc 	m.m_lc_ipc_shmat.addr = shmaddr;
38*433d6423SLionel Sambuc 	m.m_lc_ipc_shmat.flag = shmflg;
39*433d6423SLionel Sambuc 
40*433d6423SLionel Sambuc 	r = _syscall(ipc_pt, IPC_SHMAT, &m);
41*433d6423SLionel Sambuc 	if (r != OK)
42*433d6423SLionel Sambuc 		return (void *) -1;
43*433d6423SLionel Sambuc 	return m.m_lc_ipc_shmat.retaddr;
44*433d6423SLionel Sambuc }
45*433d6423SLionel Sambuc 
46*433d6423SLionel Sambuc /* Deattach shared memory segment. */
shmdt(const void * shmaddr)47*433d6423SLionel Sambuc int shmdt(const void *shmaddr)
48*433d6423SLionel Sambuc {
49*433d6423SLionel Sambuc 	message m;
50*433d6423SLionel Sambuc 	endpoint_t ipc_pt;
51*433d6423SLionel Sambuc 
52*433d6423SLionel Sambuc 	if (get_ipc_endpt(&ipc_pt) != OK) {
53*433d6423SLionel Sambuc 		errno = ENOSYS;
54*433d6423SLionel Sambuc 		return -1;
55*433d6423SLionel Sambuc 	}
56*433d6423SLionel Sambuc 
57*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
58*433d6423SLionel Sambuc 	m.m_lc_ipc_shmdt.addr = shmaddr;
59*433d6423SLionel Sambuc 
60*433d6423SLionel Sambuc 	return _syscall(ipc_pt, IPC_SHMDT, &m);
61*433d6423SLionel Sambuc }
62*433d6423SLionel Sambuc 
63