-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshm_posix.h
137 lines (120 loc) · 3.23 KB
/
shm_posix.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
typedef struct {
char name[ NAME_MAX ];
void* addr;
size_t len;
} ipc_shm_handle;
static void* ipc_shm_addr( ipc_shm_handle* h ) {
return h->addr;
}
static size_t ipc_shm_size( ipc_shm_handle* h ) {
return h->len;
}
static void ipc_shm_error( char* buf, size_t len, int code ) {
if( len > 0 && strerror_r( code, buf, len ) != (int)0 ) {
strncpy( buf, "unknown error", len-1 );
buf[ len-1 ] = '\0';
}
}
/* the format of shm names is restricted (at least if you want to be
* portable), so this functions checks the input and makes such a
* portable name. */
static int ipc_shm_make_name_( char const* s, ipc_shm_handle* h ) {
size_t ilen = strlen( s );
if( ilen == 0 || ilen+2 > sizeof( h->name ) )
return IPC_ERR( ENAMETOOLONG );
if( strchr( s, '/' ) != NULL )
return IPC_ERR( EINVAL );
h->name[ 0 ] = '/';
memcpy( h->name+1, s, ilen+1 );
return 0;
}
static int ipc_shm_create( ipc_shm_handle* h, char const* name,
size_t req ) {
int fd, rv, flags = O_RDWR|O_CREAT|O_EXCL;
rv = ipc_shm_make_name_( name, h );
if( rv != 0 )
return IPC_ERR( rv );
if( req == 0 )
return IPC_ERR( EINVAL );
/* open(create) shm object */
#ifdef O_CLOEXEC
flags |= O_CLOEXEC;
#endif
fd = shm_open( h->name, flags, S_IRUSR|S_IWUSR );
if( fd < 0 )
return IPC_ERR( errno );
/* resize to requested size */
if( ftruncate( fd, (off_t)req ) < 0 ) {
int saved_errno = errno;
close( fd );
shm_unlink( h->name );
return IPC_ERR( saved_errno );
}
h->len = req;
/* create mmap */
h->addr = mmap( NULL, req, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
if( h->addr == MAP_FAILED ) {
int saved_errno = errno;
close( fd );
shm_unlink( h->name );
return IPC_ERR( saved_errno );
}
close( fd ); /* we don't need it anymore! */
return 0;
}
static int ipc_shm_attach( ipc_shm_handle* h, char const* name ) {
int fd, rv, flags = O_RDWR;
struct stat buf;
rv = ipc_shm_make_name_( name, h );
if( rv != 0 )
return IPC_ERR( rv );
/* open shared memory object */
#ifdef O_CLOEXEC
flags |= O_CLOEXEC;
#endif
fd = shm_open( h->name, flags, S_IRUSR|S_IWUSR );
if( fd < 0 )
return IPC_ERR( errno );
/* figure out its size */
if( fstat( fd, &buf ) < 0 ) {
int saved_errno = errno;
close( fd );
return IPC_ERR( saved_errno );
}
if( buf.st_size > (size_t)-1 ) {
close( fd );
return IPC_ERR( EFBIG );
}
h->len = buf.st_size;
/* create mmap */
h->addr = mmap( NULL, h->len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
if( h->addr == MAP_FAILED ) {
int saved_errno = errno;
close( fd );
return IPC_ERR( saved_errno );
}
close( fd ); /* we don't need it anymore! */
return 0;
}
static int ipc_shm_detach( ipc_shm_handle* h ) {
if( munmap( h->addr, h->len ) < 0 )
return IPC_ERR( errno );
return 0;
}
static int ipc_shm_remove( ipc_shm_handle* h ) {
if( munmap( h->addr, h->len ) < 0 ) {
int saved_errno = errno;
shm_unlink( h->name ); /* try to delete it anyway */
return IPC_ERR( saved_errno );
}
shm_unlink( h->name );
return 0;
}