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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/* -*- c++ -*- */
/*
* Copyright 2003,2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "vmcircbuf_sysv_shm.h"
#include <stdexcept>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef HAVE_SYS_IPC_H
#include <sys/ipc.h>
#endif
#ifdef HAVE_SYS_SHM_H
#include <sys/shm.h>
#endif
#include <errno.h>
#include <stdio.h>
#include "pagesize.h"
namespace gr {
vmcircbuf_sysv_shm::vmcircbuf_sysv_shm(int size)
: gr::vmcircbuf(size)
{
gr::thread::scoped_lock guard(s_vm_mutex);
#if !defined(HAVE_SYS_SHM_H)
fprintf(stderr, "gr::vmcircbuf_sysv_shm: sysv shared memory is not available\n");
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
#else
int pagesize = gr::pagesize();
if(size <= 0 || (size % pagesize) != 0) {
fprintf(stderr, "gr::vmcircbuf_sysv_shm: invalid size = %d\n", size);
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
int shmid_guard = -1;
int shmid1 = -1;
int shmid2 = -1;
// We use this as a guard page. We'll map it read-only on both ends of the buffer.
// Ideally we'd map it no access, but I don't think that's possible with SysV
if((shmid_guard = shmget(IPC_PRIVATE, pagesize, IPC_CREAT | 0400)) == -1) {
perror("gr::vmcircbuf_sysv_shm: shmget (0)");
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
if((shmid2 = shmget(IPC_PRIVATE, 2 * size + 2 * pagesize, IPC_CREAT | 0700)) == -1) {
perror("gr::vmcircbuf_sysv_shm: shmget(1)");
shmctl(shmid_guard, IPC_RMID, 0);
throw std::runtime_error ("gr::vmcircbuf_sysv_shm");
}
if((shmid1 = shmget(IPC_PRIVATE, size, IPC_CREAT | 0700)) == -1) {
perror("gr::vmcircbuf_sysv_shm: shmget (2)");
shmctl(shmid_guard, IPC_RMID, 0);
shmctl(shmid2, IPC_RMID, 0);
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
void *first_copy = shmat (shmid2, 0, 0);
if(first_copy == (void *) -1) {
perror("gr::vmcircbuf_sysv_shm: shmat(1)");
shmctl(shmid_guard, IPC_RMID, 0);
shmctl(shmid2, IPC_RMID, 0);
shmctl(shmid1, IPC_RMID, 0);
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
shmctl(shmid2, IPC_RMID, 0);
// There may be a race between our detach and attach.
//
// If the system allocates all shared memory segments at the same
// virtual addresses in all processes and if the system allocates
// some other segment to first_copy or first_copoy + size between
// our detach and attach, the attaches below could fail [I've never
// seen it fail for this reason].
shmdt(first_copy);
// first read-only guard page
if(shmat(shmid_guard, first_copy, SHM_RDONLY) == (void *) -1) {
perror("gr::vmcircbuf_sysv_shm: shmat(2)");
shmctl(shmid_guard, IPC_RMID, 0);
shmctl(shmid1, IPC_RMID, 0);
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
// first copy
if(shmat (shmid1, (char*)first_copy + pagesize, 0) == (void *) -1) {
perror("gr::vmcircbuf_sysv_shm: shmat (3)");
shmctl(shmid_guard, IPC_RMID, 0);
shmctl(shmid1, IPC_RMID, 0);
shmdt(first_copy);
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
// second copy
if(shmat (shmid1, (char*)first_copy + pagesize + size, 0) == (void *) -1) {
perror("gr::vmcircbuf_sysv_shm: shmat (4)");
shmctl(shmid_guard, IPC_RMID, 0);
shmctl(shmid1, IPC_RMID, 0);
shmdt((char *)first_copy + pagesize);
throw std::runtime_error("gr::vmcircbuf_sysv_shm");
}
// second read-only guard page
if(shmat(shmid_guard, (char*)first_copy + pagesize + 2 * size, SHM_RDONLY) == (void *) -1) {
perror("gr::vmcircbuf_sysv_shm: shmat(5)");
shmctl(shmid_guard, IPC_RMID, 0);
shmctl(shmid1, IPC_RMID, 0);
shmdt(first_copy);
shmdt((char *)first_copy + pagesize);
shmdt((char *)first_copy + pagesize + size);
throw std::runtime_error ("gr::vmcircbuf_sysv_shm");
}
shmctl(shmid1, IPC_RMID, 0);
shmctl(shmid_guard, IPC_RMID, 0);
// Now remember the important stuff
d_base = (char*)first_copy + pagesize;
d_size = size;
#endif
}
vmcircbuf_sysv_shm::~vmcircbuf_sysv_shm()
{
gr::thread::scoped_lock guard(s_vm_mutex);
#if defined(HAVE_SYS_SHM_H)
if(shmdt(d_base - gr::pagesize()) == -1
|| shmdt(d_base) == -1
|| shmdt(d_base + d_size) == -1
|| shmdt(d_base + 2 * d_size) == -1){
perror("gr::vmcircbuf_sysv_shm: shmdt(2)");
}
#endif
}
// ----------------------------------------------------------------
// The factory interface
// ----------------------------------------------------------------
gr::vmcircbuf_factory *vmcircbuf_sysv_shm_factory::s_the_factory = 0;
gr::vmcircbuf_factory *
vmcircbuf_sysv_shm_factory::singleton()
{
if(s_the_factory)
return s_the_factory;
s_the_factory = new gr::vmcircbuf_sysv_shm_factory();
return s_the_factory;
}
int
vmcircbuf_sysv_shm_factory::granularity()
{
return gr::pagesize();
}
gr::vmcircbuf *
vmcircbuf_sysv_shm_factory::make(int size)
{
try {
return new vmcircbuf_sysv_shm(size);
}
catch (...) {
return 0;
}
}
} /* namespace gr */
|