This source file includes following definitions.
- shmem_ds_reset
- module_init
- module_finalize
- ds_copy
- segment_create
- segment_attach
- segment_detach
- segment_unlink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include "opal_config.h"
25
26 #include <errno.h>
27 #ifdef HAVE_FCNTL_H
28 #include <fcntl.h>
29 #endif
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
38 #endif
39 #ifdef HAVE_SYS_IPC_H
40 #include <sys/ipc.h>
41 #endif
42 #if HAVE_SYS_SHM_H
43 #include <sys/shm.h>
44 #endif
45 #if HAVE_SYS_STAT_H
46 #include <sys/stat.h>
47 #endif
48 #include <string.h>
49 #ifdef HAVE_NETDB_H
50 #include <netdb.h>
51 #endif
52
53 #include "opal/constants.h"
54 #include "opal_stdint.h"
55 #include "opal/util/output.h"
56 #include "opal/util/path.h"
57 #include "opal/util/show_help.h"
58 #include "opal/mca/shmem/shmem.h"
59 #include "opal/mca/shmem/base/base.h"
60
61 #include "shmem_sysv.h"
62
63
64
65
66
67 static int
68 module_init(void);
69
70 static int
71 segment_create(opal_shmem_ds_t *ds_buf,
72 const char *file_name,
73 size_t size);
74
75 static int
76 ds_copy(const opal_shmem_ds_t *from,
77 opal_shmem_ds_t *to);
78
79 static void *
80 segment_attach(opal_shmem_ds_t *ds_buf);
81
82 static int
83 segment_detach(opal_shmem_ds_t *ds_buf);
84
85 static int
86 segment_unlink(opal_shmem_ds_t *ds_buf);
87
88 static int
89 module_finalize(void);
90
91
92 opal_shmem_sysv_module_t opal_shmem_sysv_module = {
93 .super = {
94 .module_init = module_init,
95 .segment_create = segment_create,
96 .ds_copy = ds_copy,
97 .segment_attach = segment_attach,
98 .segment_detach = segment_detach,
99 .unlink = segment_unlink,
100 .module_finalize = module_finalize
101 }
102 };
103
104
105
106
107
108
109
110
111
112 static inline void
113 shmem_ds_reset(opal_shmem_ds_t *ds_buf)
114 {
115
116 OPAL_OUTPUT_VERBOSE(
117 (70, opal_shmem_base_framework.framework_output,
118 "%s: %s: shmem_ds_resetting\n",
119 mca_shmem_sysv_component.super.base_version.mca_type_name,
120 mca_shmem_sysv_component.super.base_version.mca_component_name)
121 );
122
123 ds_buf->seg_cpid = 0;
124 OPAL_SHMEM_DS_RESET_FLAGS(ds_buf);
125 ds_buf->seg_id = OPAL_SHMEM_DS_ID_INVALID;
126 ds_buf->seg_size = 0;
127 memset(ds_buf->seg_name, '\0', OPAL_PATH_MAX);
128 ds_buf->seg_base_addr = (unsigned char *)-1;
129 }
130
131
132 static int
133 module_init(void)
134 {
135
136 return OPAL_SUCCESS;
137 }
138
139
140 static int
141 module_finalize(void)
142 {
143
144 return OPAL_SUCCESS;
145 }
146
147
148 static int
149 ds_copy(const opal_shmem_ds_t *from,
150 opal_shmem_ds_t *to)
151 {
152 memcpy(to, from, sizeof(opal_shmem_ds_t));
153
154 OPAL_OUTPUT_VERBOSE(
155 (70, opal_shmem_base_framework.framework_output,
156 "%s: %s: ds_copy complete "
157 "from: (id: %d, size: %lu, "
158 "name: %s flags: 0x%02x) "
159 "to: (id: %d, size: %lu, "
160 "name: %s flags: 0x%02x)\n",
161 mca_shmem_sysv_component.super.base_version.mca_type_name,
162 mca_shmem_sysv_component.super.base_version.mca_component_name,
163 from->seg_id, (unsigned long)from->seg_size, from->seg_name,
164 from->flags, to->seg_id, (unsigned long)to->seg_size, to->seg_name,
165 to->flags)
166 );
167
168 return OPAL_SUCCESS;
169 }
170
171
172 static int
173 segment_create(opal_shmem_ds_t *ds_buf,
174 const char *file_name,
175 size_t size)
176 {
177 int rc = OPAL_SUCCESS;
178 pid_t my_pid = getpid();
179
180
181
182 size_t real_size = size + sizeof(opal_shmem_seg_hdr_t);
183 opal_shmem_seg_hdr_t *seg_hdrp = MAP_FAILED;
184
185
186 shmem_ds_reset(ds_buf);
187
188
189
190
191
192
193
194
195 if (-1 == (ds_buf->seg_id = shmget(IPC_PRIVATE, real_size,
196 IPC_CREAT | IPC_EXCL | S_IRWXU))) {
197 int err = errno;
198 char hn[OPAL_MAXHOSTNAMELEN];
199 gethostname(hn, sizeof(hn));
200 opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
201 "shmget(2)", "", strerror(err), err);
202 rc = OPAL_ERROR;
203 goto out;
204 }
205
206 else if ((void *)-1 == (seg_hdrp = shmat(ds_buf->seg_id, NULL, 0))) {
207 int err = errno;
208 char hn[OPAL_MAXHOSTNAMELEN];
209 gethostname(hn, sizeof(hn));
210 opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
211 "shmat(2)", "", strerror(err), err);
212 shmctl(ds_buf->seg_id, IPC_RMID, NULL);
213 rc = OPAL_ERROR;
214 goto out;
215 }
216
217
218
219
220 else if (0 != shmctl(ds_buf->seg_id, IPC_RMID, NULL)) {
221 int err = errno;
222 char hn[OPAL_MAXHOSTNAMELEN];
223 gethostname(hn, sizeof(hn));
224 opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
225 "shmctl(2)", "", strerror(err), err);
226 rc = OPAL_ERROR;
227 goto out;
228 }
229
230 else {
231
232 opal_atomic_rmb();
233
234
235 opal_atomic_lock_init(&seg_hdrp->lock, OPAL_ATOMIC_LOCK_UNLOCKED);
236
237 seg_hdrp->cpid = my_pid;
238
239 opal_atomic_wmb();
240
241
242 ds_buf->seg_cpid = my_pid;
243 ds_buf->seg_size = real_size;
244 ds_buf->seg_base_addr = (unsigned char *)seg_hdrp;
245
246
247
248
249
250
251
252 OPAL_SHMEM_DS_SET_VALID(ds_buf);
253
254 OPAL_OUTPUT_VERBOSE(
255 (70, opal_shmem_base_framework.framework_output,
256 "%s: %s: create successful "
257 "(id: %d, size: %lu, name: %s)\n",
258 mca_shmem_sysv_component.super.base_version.mca_type_name,
259 mca_shmem_sysv_component.super.base_version.mca_component_name,
260 ds_buf->seg_id, (unsigned long)ds_buf->seg_size, ds_buf->seg_name)
261 );
262 }
263
264 out:
265
266
267
268 if (OPAL_SUCCESS != rc) {
269
270 if ((void *)-1 != seg_hdrp) {
271 shmdt((char*)seg_hdrp);
272 }
273 shmctl(ds_buf->seg_id, IPC_RMID, NULL);
274
275
276 shmem_ds_reset(ds_buf);
277 }
278 return rc;
279 }
280
281
282
283
284
285 static void *
286 segment_attach(opal_shmem_ds_t *ds_buf)
287 {
288 pid_t my_pid = getpid();
289
290 if (my_pid != ds_buf->seg_cpid) {
291 if ((void *)-1 == (ds_buf->seg_base_addr = shmat(ds_buf->seg_id, NULL,
292 0))) {
293 int err = errno;
294 char hn[OPAL_MAXHOSTNAMELEN];
295 gethostname(hn, sizeof(hn));
296 opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
297 "shmat(2)", "", strerror(err), err);
298 shmctl(ds_buf->seg_id, IPC_RMID, NULL);
299 return NULL;
300 }
301 }
302
303
304
305
306 OPAL_OUTPUT_VERBOSE(
307 (70, opal_shmem_base_framework.framework_output,
308 "%s: %s: attach successful "
309 "(id: %d, size: %lu, name: %s)\n",
310 mca_shmem_sysv_component.super.base_version.mca_type_name,
311 mca_shmem_sysv_component.super.base_version.mca_component_name,
312 ds_buf->seg_id, (unsigned long)ds_buf->seg_size, ds_buf->seg_name)
313 );
314
315
316 return (ds_buf->seg_base_addr + sizeof(opal_shmem_seg_hdr_t));
317 }
318
319
320 static int
321 segment_detach(opal_shmem_ds_t *ds_buf)
322 {
323 int rc = OPAL_SUCCESS;
324
325 OPAL_OUTPUT_VERBOSE(
326 (70, opal_shmem_base_framework.framework_output,
327 "%s: %s: detaching "
328 "(id: %d, size: %lu, name: %s)\n",
329 mca_shmem_sysv_component.super.base_version.mca_type_name,
330 mca_shmem_sysv_component.super.base_version.mca_component_name,
331 ds_buf->seg_id, (unsigned long)ds_buf->seg_size, ds_buf->seg_name)
332 );
333
334 if (0 != shmdt((char*)ds_buf->seg_base_addr)) {
335 int err = errno;
336 char hn[OPAL_MAXHOSTNAMELEN];
337 gethostname(hn, sizeof(hn));
338 opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
339 "shmdt(2)", "", strerror(err), err);
340 rc = OPAL_ERROR;
341 }
342
343
344
345
346 shmem_ds_reset(ds_buf);
347 return rc;
348 }
349
350
351 static int
352 segment_unlink(opal_shmem_ds_t *ds_buf)
353 {
354
355
356 OPAL_OUTPUT_VERBOSE(
357 (70, opal_shmem_base_framework.framework_output,
358 "%s: %s: unlinking "
359 "(id: %d, size: %lu, name: %s)\n",
360 mca_shmem_sysv_component.super.base_version.mca_type_name,
361 mca_shmem_sysv_component.super.base_version.mca_component_name,
362 ds_buf->seg_id, (unsigned long)ds_buf->seg_size, ds_buf->seg_name)
363 );
364
365
366
367
368
369 ds_buf->seg_id = OPAL_SHMEM_DS_ID_INVALID;
370
371 OPAL_SHMEM_DS_INVALIDATE(ds_buf);
372 return OPAL_SUCCESS;
373 }
374