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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
Copyright (C) 2003 Paul Davis
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id$
*/
#include <config.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <jack/shm.h>
#include <jack/internal.h>
static jack_shm_registry_t* jack_shm_registry;
static void
jack_shm_lock_registry ()
{
/* XXX magic with semaphores here */
}
static void
jack_shm_unlock_registry ()
{
/* XXX magic with semaphores here */
}
jack_shm_registry_t *
jack_get_free_shm_info ()
{
jack_shm_registry_t* si = NULL;
int i;
jack_shm_lock_registry ();
for (i = 0; i < MAX_SHM_ID; ++i) {
if (jack_shm_registry[i].size == 0) {
break;
}
}
if (i < MAX_SHM_ID) {
si = &jack_shm_registry[i];
}
jack_shm_unlock_registry ();
return si;
}
void
jack_release_shm_info (jack_shm_registry_index_t index)
{
if (jack_shm_registry[index].allocator == getpid()) {
jack_shm_lock_registry ();
jack_shm_registry[index].size = 0;
jack_shm_registry[index].allocator = 0;
jack_shm_unlock_registry ();
}
}
void
jack_cleanup_shm (void)
{
int i;
int destroy;
jack_shm_info_t copy;
jack_initialize_shm ();
jack_shm_lock_registry ();
for (i = 0; i < MAX_SHM_ID; i++) {
jack_shm_registry_t* r;
r = &jack_shm_registry[i];
copy.index = r->index;
destroy = FALSE;
if (r->allocator == getpid()) {
/* allocated by this process, so unattach
and destroy.
*/
jack_release_shm (©);
destroy = TRUE;
} else {
if (kill (r->allocator, 0)) {
if (errno == ESRCH) {
/* allocator no longer exists, so destroy */
destroy = TRUE;
}
}
}
if (destroy) {
jack_destroy_shm (©);
r->size = 0;
r->allocator = 0;
}
}
jack_shm_unlock_registry ();
}
#ifdef USE_POSIX_SHM
int
jack_initialize_shm (void)
{
int shm_fd;
jack_shmsize_t size;
int new_registry = FALSE;
int ret = -1;
int perm;
if (jack_shm_registry != NULL) {
return 0;
}
/* grab a chunk of memory to store shm ids in. this is
to allow clean up of all segments whenever JACK
starts (or stops).
*/
size = sizeof (jack_shm_registry_t) * MAX_SHM_ID;
jack_shm_lock_registry ();
perm = O_RDWR;
/* try without O_CREAT to see if it already exists */
if ((shm_fd = shm_open ("/jack-shm-registry", perm, 0666)) < 0) {
if (errno == ENOENT) {
perm = O_RDWR|O_CREAT;
/* it doesn't exist, so create it */
if ((shm_fd = shm_open ("/jack-shm-registry", perm, 0666)) < 0) {
jack_error ("cannot create shm registry segment (%s)",
strerror (errno));
goto out;
}
new_registry = TRUE;
} else {
jack_error ("cannot open existing shm registry segment (%s)",
strerror (errno));
goto out;
}
}
if (perm & O_CREAT) {
if (ftruncate (shm_fd, size) < 0) {
jack_error ("cannot set size of engine shm registry 1"
"(%s)", strerror (errno));
goto out;
}
}
if ((jack_shm_registry = mmap (0, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
jack_error ("cannot mmap shm registry segment (%s)",
strerror (errno));
goto out;
}
if (new_registry) {
int i;
memset (jack_shm_registry, 0, size);
for (i = 0; i < MAX_SHM_ID; ++i) {
jack_shm_registry[i].index = i;
}
fprintf (stderr, "JACK compiled with POSIX SHM support\n");
}
ret = 0;
out:
close (shm_fd);
jack_shm_unlock_registry ();
return ret;
}
void
jack_destroy_shm (jack_shm_info_t* si)
{
shm_unlink (jack_shm_registry[si->index].id);
jack_release_shm_info (si->index);
}
void
jack_release_shm (jack_shm_info_t* si)
{
if (si->attached_at != MAP_FAILED) {
munmap (si->attached_at, jack_shm_registry[si->index].size);
}
}
int
jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
{
jack_shm_registry_t* registry;
int shm_fd;
int perm = O_RDWR|O_CREAT;
if ((registry = jack_get_free_shm_info ()) == NULL) {
return -1;
}
if ((shm_fd = shm_open (shm_name, O_RDWR|O_CREAT, 0666)) < 0) {
jack_error ("cannot create shm segment %s (%s)", shm_name,
strerror (errno));
return -1;
}
if (perm & O_CREAT) {
if (ftruncate (shm_fd, size) < 0) {
jack_error ("cannot set size of engine shm registry 0"
"(%s)", strerror (errno));
return -1;
}
}
close (shm_fd);
registry->size = size;
snprintf (registry->id, sizeof (registry->id), "%s", shm_name);
registry->allocator = getpid();
si->index = registry->index;
si->attached_at = MAP_FAILED; /* segment not attached */
return 0;
}
int
jack_attach_shm (jack_shm_info_t* si)
{
int shm_fd;
jack_shm_registry_t *registry = &jack_shm_registry[si->index];
if ((shm_fd = shm_open (registry->id,
O_RDWR, 0666)) < 0) {
jack_error ("cannot open shm segment %s (%s)", registry->id,
strerror (errno));
return -1;
}
if ((si->attached_at = mmap (0, registry->size, PROT_READ|PROT_WRITE,
MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
jack_error ("cannot mmap shm segment %s (%s)",
registry->id,
strerror (errno));
close (shm_fd);
return -1;
}
close (shm_fd);
return 0;
}
int
jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
{
int shm_fd;
jack_shm_registry_t *registry = &jack_shm_registry[si->index];
if ((shm_fd = shm_open (registry->id, O_RDWR, 0666)) < 0) {
jack_error ("cannot create shm segment %s (%s)", registry->id,
strerror (errno));
return -1;
}
munmap (si->attached_at, registry->size);
if (ftruncate (shm_fd, size) < 0) {
jack_error ("cannot set size of shm segment %s "
"(%s)", registry->id, strerror (errno));
return -1;
}
if ((si->attached_at = mmap (0, size, PROT_READ|PROT_WRITE,
MAP_SHARED, shm_fd, 0))
== MAP_FAILED) {
jack_error ("cannot mmap shm segment %s (%s)", registry->id,
strerror (errno));
close (shm_fd);
return -1;
}
close (shm_fd);
return 0;
}
#else /* USE_POSIX_SHM */
#define JACK_SHM_REGISTRY_KEY 0x282929
int
jack_initialize_shm (void)
{
int shmflags;
int shmid;
key_t key;
jack_shmsize_t size;
int new_registry = FALSE;
int ret = -1;
if (jack_shm_registry != NULL) {
return 0;
}
/* grab a chunk of memory to store shm ids in. this is
to allow our parent to clean up all such ids when
if we exit. otherwise, they can get lost in crash
or debugger driven exits.
*/
shmflags = 0666;
key = JACK_SHM_REGISTRY_KEY;
size = sizeof (jack_shm_registry_t) * MAX_SHM_ID;
jack_shm_lock_registry ();
/* try without IPC_CREAT to check if it already exists */
if ((shmid = shmget (key, size, shmflags)) < 0) {
if (errno == ENOENT) {
if ((shmid = shmget (key, size, shmflags|IPC_CREAT)) < 0) {
jack_error ("cannot create shm registry segment (%s)",
strerror (errno));
goto out;
}
new_registry = TRUE;
} else {
jack_error ("cannot use existing shm registry segment (%s)",
strerror (errno));
goto out;
}
}
if ((jack_shm_registry = shmat (shmid, 0, 0)) < 0) {
jack_error ("cannot attach shm registry segment (%s)",
strerror (errno));
goto out;
}
if (new_registry) {
int i;
memset (jack_shm_registry, 0, size);
for (i = 0; i < MAX_SHM_ID; ++i) {
jack_shm_registry[i].index = i;
}
fprintf (stderr, "JACK compiled with System V SHM support\n");
}
ret = 0;
out:
jack_shm_unlock_registry ();
return ret;
}
void
jack_destroy_shm (jack_shm_info_t* si)
{
shmctl (jack_shm_registry[si->index].id, IPC_RMID, NULL);
jack_release_shm_info (si->index);
}
void
jack_release_shm (jack_shm_info_t* si)
{
if (si->attached_at != MAP_FAILED) {
shmdt (si->attached_at);
}
}
int
jack_shmalloc (const char* name_not_used, jack_shmsize_t size, jack_shm_info_t* si)
{
int shmflags;
int shmid;
jack_shm_registry_t* registry;
if ((registry = jack_get_free_shm_info ()) == NULL) {
return -1;
}
shmflags = 0666 | IPC_CREAT | IPC_EXCL;
if ((shmid = shmget (IPC_PRIVATE, size, shmflags)) < 0) {
jack_error ("cannot create shm segment %s (%s)",
name_not_used, strerror (errno));
return -1;
}
registry->size = size;
registry->id = shmid;
registry->allocator = getpid();
si->index = registry->index;
si->attached_at = MAP_FAILED; /* segment not attached */
return 0;
}
int
jack_attach_shm (jack_shm_info_t* si)
{
if ((si->attached_at = shmat (jack_shm_registry[si->index].id, 0, 0)) < 0) {
jack_error ("cannot attach shm segment (%s)",
strerror (errno));
jack_release_shm_info (si->index);
return -1;
}
return 0;
}
int
jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
{
/* There is no way to resize a System V shm segment. So, we
* delete it and allocate a new one. This is tricky, because
* the old segment will not disappear until all the clients
* have released it. We can only do what we can from here.
*/
jack_release_shm (si);
jack_destroy_shm (si);
if (jack_shmalloc ("not used", size, si)) {
return -1;
}
return jack_attach_shm (si);
}
#endif /* !USE_POSIX_SHM */
|