Repository navigation
Expand file tree
/
Copy pathshm.c
More file actions
110 lines (90 loc) · 1.92 KB
/
Copy pathshm.c
File metadata and controls
110 lines (90 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#define _SYSTEM 1
#define _MINIX 1
#include <minix/callnr.h>
#include <minix/com.h>
#include <minix/config.h>
#include <minix/ipc.h>
#include <minix/endpoint.h>
#include <minix/sysutil.h>
#include <minix/const.h>
#include <minix/type.h>
#include <minix/rs.h>
#include <lib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
PRIVATE int get_ipc_endpt(endpoint_t *pt)
{
return minix_rs_lookup("ipc", pt);
}
/* Shared memory control operation. */
PUBLIC int shmctl(int shmid, int cmd, struct shmid_ds *buf)
{
message m;
endpoint_t ipc_pt;
int r;
if (get_ipc_endpt(&ipc_pt) != OK) {
errno = ENOSYS;
return -1;
}
m.SHMCTL_ID = shmid;
m.SHMCTL_CMD = cmd;
m.SHMCTL_BUF = (long) buf;
r = _syscall(ipc_pt, IPC_SHMCTL, &m);
if ((cmd == IPC_INFO || cmd == SHM_INFO || cmd == SHM_STAT)
&& (r == OK))
return m.SHMCTL_RET;
return r;
}
/* Get shared memory segment. */
PUBLIC int shmget(key_t key, size_t size, int shmflg)
{
message m;
endpoint_t ipc_pt;
int r;
if (get_ipc_endpt(&ipc_pt) != OK) {
errno = ENOSYS;
return -1;
}
m.SHMGET_KEY = key;
m.SHMGET_SIZE = size;
m.SHMGET_FLAG = shmflg;
r = _syscall(ipc_pt, IPC_SHMGET, &m);
if (r != OK)
return r;
return m.SHMGET_RETID;
}
/* Attach shared memory segment. */
PUBLIC void *shmat(int shmid, const void *shmaddr, int shmflg)
{
message m;
endpoint_t ipc_pt;
int r;
if (get_ipc_endpt(&ipc_pt) != OK) {
errno = ENOSYS;
return NULL;
}
m.SHMAT_ID = shmid;
m.SHMAT_ADDR = (long) shmaddr;
m.SHMAT_FLAG = shmflg;
r = _syscall(ipc_pt, IPC_SHMAT, &m);
if (r != OK)
return (void *) -1;
return (void *) m.SHMAT_RETADDR;
}
/* Deattach shared memory segment. */
PUBLIC int shmdt(const void *shmaddr)
{
message m;
endpoint_t ipc_pt;
if (get_ipc_endpt(&ipc_pt) != OK) {
errno = ENOSYS;
return -1;
}
m.SHMDT_ADDR = (long) shmaddr;
return _syscall(ipc_pt, IPC_SHMDT, &m);
}