shm.c 29.5 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
/* This module provides a set of abstract shared memory interfaces
 * with support using both System V and POSIX shared memory
 * implementations.  The code is divided into three sections:
 *
 *	- common (interface-independent) code
 *	- POSIX implementation
 *	- System V implementation
 *
 * The implementation used is determined by whether USE_POSIX_SHM was
 * set in the ./configure step.
 */
12

sletz's avatar
sletz committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 Copyright (C) 2001-2003 Paul Davis
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public License
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 */

32
#include "JackConstants.h"
33

sletz's avatar
sletz committed
34
#ifdef WIN32
sletz's avatar
sletz committed
35
#include <process.h>
sletz's avatar
sletz committed
36
#include <stdio.h>
sletz's avatar
sletz committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#else

#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/stat.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <stdlib.h>

#endif

#include "shm.h"
#include "JackError.h"

sletz's avatar
sletz committed
59
static int GetUID()
60
61
62
63
64
65
66
67
68
{
#ifdef WIN32
    return  _getpid();
    //#error "No getuid function available"
#else
    return getuid();
#endif
}

sletz's avatar
sletz committed
69
static int GetPID()
sletz's avatar
sletz committed
70
71
72
73
74
75
76
{
#ifdef WIN32
    return  _getpid();
#else
    return getpid();
#endif
}
77

sletz's avatar
sletz committed
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
#ifdef USE_POSIX_SHM
static jack_shmtype_t jack_shmtype = shm_POSIX;
#elif WIN32
static jack_shmtype_t jack_shmtype = shm_WIN32;
#else
static jack_shmtype_t jack_shmtype = shm_SYSV;
#endif

/* interface-dependent forward declarations */
static int	jack_access_registry (jack_shm_info_t *ri);
static int	jack_create_registry (jack_shm_info_t *ri);
static void	jack_remove_shm (jack_shm_id_t *id);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * common interface-independent section
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* The JACK SHM registry is a chunk of memory for keeping track of the
 * shared memory used by each active JACK server.  This allows the
 * server to clean up shared memory when it exits.  To avoid memory
 * leakage due to kill -9, crashes or debugger-driven exits, this
 * cleanup is also done when a new instance of that server starts.
 */

/* per-process global data for the SHM interfaces */
static jack_shm_id_t   registry_id;	/* SHM id for the registry */

#ifdef WIN32
static jack_shm_info_t registry_info = {/* SHM info for the registry */
sletz's avatar
sletz committed
107
	JACK_SHM_NULL_INDEX,
sletz's avatar
sletz committed
108
	NULL
sletz's avatar
sletz committed
109
};
sletz's avatar
sletz committed
110
111
112
#else
static jack_shm_info_t registry_info = { /* SHM info for the registry */
	.index = JACK_SHM_NULL_INDEX,
sletz's avatar
sletz committed
113
	.ptr.attached_at = MAP_FAILED
sletz's avatar
sletz committed
114
115
116
117
118
119
120
121
122
};
#endif

/* pointers to registry header and array */
static jack_shm_header_t   *jack_shm_header = NULL;
static jack_shm_registry_t *jack_shm_registry = NULL;
static char jack_shm_server_prefix[JACK_SERVER_NAME_SIZE] = "";

/* jack_shm_lock_registry() serializes updates to the shared memory
sletz's avatar
sletz committed
123
 * segment JACK uses to keep track of the SHM segments allocated to
sletz's avatar
sletz committed
124
125
126
 * all its processes, including multiple servers.
 *
 * This is not a high-contention lock, but it does need to work across
sletz's avatar
sletz committed
127
128
 * multiple processes. High transaction rates and realtime safety are
 * not required. Any solution needs to at least be portable to POSIX
sletz's avatar
sletz committed
129
130
131
 * and POSIX-like systems.
 *
 * We must be particularly careful to ensure that the lock be released
sletz's avatar
sletz committed
132
 * if the owning process terminates abnormally. Otherwise, a segfault
sletz's avatar
sletz committed
133
134
135
136
137
138
139
140
141
142
143
 * or kill -9 at the wrong moment could prevent JACK from ever running
 * again on that machine until after a reboot.
 */

#define JACK_SEMAPHORE_KEY 0x282929
#ifndef USE_POSIX_SHM
#define JACK_SHM_REGISTRY_KEY JACK_SEMAPHORE_KEY
#endif

static int semid = -1;

sletz's avatar
sletz committed
144
#ifdef WIN32
sletz's avatar
sletz committed
145

sletz's avatar
sletz committed
146
147
static int
semaphore_init () {return 0;}
sletz's avatar
sletz committed
148

sletz's avatar
sletz committed
149
static  int
sletz's avatar
sletz committed
150
semaphore_add (int value) {return 0;}
sletz's avatar
sletz committed
151
152
153
154
155
156

#else
/* all semaphore errors are fatal -- issue message, but do not return */
static void
semaphore_error (char *msg)
{
sletz's avatar
sletz committed
157
	jack_error ("JACK semaphore error: %s (%s)",
sletz's avatar
sletz committed
158
159
160
		    msg, strerror (errno));
}

sletz's avatar
sletz committed
161
static int
sletz's avatar
sletz committed
162
163
164
165
166
167
semaphore_init ()
{
	key_t semkey = JACK_SEMAPHORE_KEY;
	struct sembuf sbuf;
	int create_flags = IPC_CREAT | IPC_EXCL
		| S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
sletz's avatar
sletz committed
168

sletz's avatar
sletz committed
169
170
171
172
173
174
175
176
177
178
179
	/* Get semaphore ID associated with this key. */
	if ((semid = semget(semkey, 0, 0)) == -1) {

		/* Semaphore does not exist - Create. */
		if ((semid = semget(semkey, 1, create_flags)) != -1) {

			/* Initialize the semaphore, allow one owner. */
			sbuf.sem_num = 0;
			sbuf.sem_op = 1;
			sbuf.sem_flg = 0;
			if (semop(semid, &sbuf, 1) == -1) {
sletz's avatar
sletz committed
180
181
                semaphore_error ("semop");
                return -1;
sletz's avatar
sletz committed
182
183
184
185
			}

		} else if (errno == EEXIST) {
			if ((semid = semget(semkey, 0, 0)) == -1) {
sletz's avatar
sletz committed
186
187
                semaphore_error ("semget");
                return -1;
sletz's avatar
sletz committed
188
189
190
			}

		} else {
sletz's avatar
sletz committed
191
192
            semaphore_error ("semget creation");
            return -1;
sletz's avatar
sletz committed
193
194
		}
	}
sletz's avatar
sletz committed
195
196

    return 0;
sletz's avatar
sletz committed
197
198
}

sletz's avatar
sletz committed
199
static inline int
sletz's avatar
sletz committed
200
201
202
203
204
205
206
semaphore_add (int value)
{
	struct sembuf sbuf;

	sbuf.sem_num = 0;
	sbuf.sem_op = value;
	sbuf.sem_flg = SEM_UNDO;
sletz's avatar
sletz committed
207

sletz's avatar
sletz committed
208
209
	if (semop(semid, &sbuf, 1) == -1) {
		semaphore_error ("semop");
sletz's avatar
sletz committed
210
        return -1;
sletz's avatar
sletz committed
211
	}
sletz's avatar
sletz committed
212
213

    return 0;
sletz's avatar
sletz committed
214
215
216
217
}

#endif

sletz's avatar
sletz committed
218
static int
sletz's avatar
sletz committed
219
220
jack_shm_lock_registry (void)
{
sletz's avatar
sletz committed
221
222
223
224
	if (semid == -1) {
        if (semaphore_init () < 0)
            return -1;
    }
sletz's avatar
sletz committed
225

sletz's avatar
sletz committed
226
	return semaphore_add (-1);
sletz's avatar
sletz committed
227
228
}

sletz's avatar
sletz committed
229
static void
sletz's avatar
sletz committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
jack_shm_unlock_registry (void)
{
	semaphore_add (1);
}

static void
jack_shm_init_registry ()
{
	/* registry must be locked */
	int i;

	memset (jack_shm_header, 0, JACK_SHM_REGISTRY_SIZE);

	jack_shm_header->magic = JACK_SHM_MAGIC;
sletz's avatar
sletz committed
244
	//jack_shm_header->protocol = JACK_PROTOCOL_VERSION;
sletz's avatar
sletz committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
	jack_shm_header->type = jack_shmtype;
	jack_shm_header->size = JACK_SHM_REGISTRY_SIZE;
	jack_shm_header->hdr_len = sizeof (jack_shm_header_t);
	jack_shm_header->entry_len = sizeof (jack_shm_registry_t);

	for (i = 0; i < MAX_SHM_ID; ++i) {
		jack_shm_registry[i].index = i;
	}
}

static int
jack_shm_validate_registry ()
{
	/* registry must be locked */

	if ((jack_shm_header->magic == JACK_SHM_MAGIC)
sletz's avatar
sletz committed
261
	    //&& (jack_shm_header->protocol == JACK_PROTOCOL_VERSION)
sletz's avatar
sletz committed
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
	    && (jack_shm_header->type == jack_shmtype)
	    && (jack_shm_header->size == JACK_SHM_REGISTRY_SIZE)
	    && (jack_shm_header->hdr_len == sizeof (jack_shm_header_t))
	    && (jack_shm_header->entry_len == sizeof (jack_shm_registry_t))) {

		return 0;		/* registry OK */
	}

	return -1;
}

/* set a unique per-user, per-server shm prefix string
 *
 * According to the POSIX standard:
 *
 *   "The name argument conforms to the construction rules for a
 *   pathname. If name begins with the slash character, then processes
 *   calling shm_open() with the same value of name refer to the same
 *   shared memory object, as long as that name has not been
 *   removed. If name does not begin with the slash character, the
 *   effect is implementation-defined. The interpretation of slash
 *   characters other than the leading slash character in name is
 *   implementation-defined."
 *
 * Since the Linux implementation does not allow slashes *within* the
 * name, in the interest of portability we use colons instead.
 */
static void
jack_set_server_prefix (const char *server_name)
{
	snprintf (jack_shm_server_prefix, sizeof (jack_shm_server_prefix),
sletz's avatar
sletz committed
293
		  "jack-%d:%s:", GetUID(), server_name);
sletz's avatar
sletz committed
294
295
296
297
298
299
300
}

/* gain server addressability to shared memory registration segment
 *
 * returns: 0 if successful
 */
static int
301
jack_server_initialize_shm (int new_registry)
sletz's avatar
sletz committed
302
303
304
305
306
307
{
	int rc;

	if (jack_shm_header)
		return 0;		/* already initialized */

sletz's avatar
sletz committed
308
309
310
311
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }
sletz's avatar
sletz committed
312
313

	rc = jack_access_registry (&registry_info);
sletz's avatar
sletz committed
314

315
316
317
318
	if (new_registry) {
		jack_remove_shm (&registry_id);
		rc = ENOENT;
	}
sletz's avatar
sletz committed
319
320

	switch (rc) {
sletz's avatar
Cleanup    
sletz committed
321
	case ENOENT:		/* registry does not exist */
sletz's avatar
sletz committed
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
		rc = jack_create_registry (&registry_info);
		break;
	case 0:				/* existing registry */
		if (jack_shm_validate_registry () == 0)
			break;
		/* else it was invalid, so fall through */
	case EINVAL:			/* bad registry */
		/* Apparently, this registry was created by an older
		 * JACK version.  Delete it so we can try again. */
		jack_release_shm (&registry_info);
		jack_remove_shm (&registry_id);
		if ((rc = jack_create_registry (&registry_info)) != 0) {
			jack_error ("incompatible shm registry (%s)",
				    strerror (errno));
#ifndef USE_POSIX_SHM
			jack_error ("to delete, use `ipcrm -M 0x%0.8x'",
				    JACK_SHM_REGISTRY_KEY);
#endif
		}
		break;
	default:			/* failure return code */
		break;
	}

	jack_shm_unlock_registry ();
	return rc;
}

/* gain client addressability to shared memory registration segment
 *
 * NOTE: this function is no longer used for server initialization,
 * instead it calls jack_register_server().
 *
 * returns: 0 if successful
 */
int
jack_initialize_shm (const char *server_name)
{
	int rc;

	if (jack_shm_header)
		return 0;		/* already initialized */

	jack_set_server_prefix (server_name);

sletz's avatar
sletz committed
367
368
369
370
371
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }

sletz's avatar
sletz committed
372
373
374
375
376
377
378
379
380
381
382
383
	if ((rc = jack_access_registry (&registry_info)) == 0) {
		if ((rc = jack_shm_validate_registry ()) != 0) {
			jack_error ("Incompatible shm registry, "
				    "are jackd and libjack in sync?");
		}
	}
	jack_shm_unlock_registry ();

	return rc;
}


sletz's avatar
sletz committed
384
char* jack_shm_addr (jack_shm_info_t* si)
sletz's avatar
sletz committed
385
{
sletz's avatar
sletz committed
386
	return (char*)si->ptr.attached_at;
sletz's avatar
sletz committed
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
}

void
jack_destroy_shm (jack_shm_info_t* si)
{
	/* must NOT have the registry locked */
	if (si->index == JACK_SHM_NULL_INDEX)
		return;			/* segment not allocated */

	jack_remove_shm (&jack_shm_registry[si->index].id);
	jack_release_shm_info (si->index);
}

jack_shm_registry_t *
jack_get_free_shm_info ()
{
	/* registry must be locked */
	jack_shm_registry_t* si = NULL;
	int i;

	for (i = 0; i < MAX_SHM_ID; ++i) {
		if (jack_shm_registry[i].size == 0) {
			break;
		}
	}
sletz's avatar
sletz committed
412

sletz's avatar
sletz committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
	if (i < MAX_SHM_ID) {
		si = &jack_shm_registry[i];
	}

	return si;
}

static void
jack_release_shm_entry (jack_shm_registry_index_t index)
{
	/* the registry must be locked */
	jack_shm_registry[index].size = 0;
	jack_shm_registry[index].allocator = 0;
	memset (&jack_shm_registry[index].id, 0,
		sizeof (jack_shm_registry[index].id));
}

sletz's avatar
sletz committed
430
int
sletz's avatar
sletz committed
431
432
433
jack_release_shm_info (jack_shm_registry_index_t index)
{
	/* must NOT have the registry locked */
sletz's avatar
sletz committed
434
	if (jack_shm_registry[index].allocator == GetPID()) {
sletz's avatar
sletz committed
435
436
437
438
		if (jack_shm_lock_registry () < 0) {
            jack_error ("jack_shm_lock_registry fails...");
            return -1;
        }
sletz's avatar
sletz committed
439
440
441
		jack_release_shm_entry (index);
		jack_shm_unlock_registry ();
	}
sletz's avatar
sletz committed
442
443

    return 0;
sletz's avatar
sletz committed
444
445
}

sletz's avatar
sletz committed
446
/* Claim server_name for this process.
sletz's avatar
sletz committed
447
448
449
450
451
452
 *
 * returns 0 if successful
 *	   EEXIST if server_name was already active for this user
 *	   ENOSPC if server registration limit reached
 *	   ENOMEM if unable to access shared memory registry
 */
sletz's avatar
sletz committed
453
int
454
jack_register_server (const char *server_name, int new_registry)
sletz's avatar
sletz committed
455
{
sletz's avatar
sletz committed
456
	int i, res = 0;
sletz's avatar
sletz committed
457
458
459

	jack_set_server_prefix (server_name);

460
	if (jack_server_initialize_shm (new_registry))
sletz's avatar
sletz committed
461
462
		return ENOMEM;

sletz's avatar
sletz committed
463
464
465
466
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }
sletz's avatar
sletz committed
467
468
469
470
471
472
473
474
475
476
477

	/* See if server_name already registered.  Since server names
	 * are per-user, we register the unique server prefix string.
	 */
	for (i = 0; i < MAX_SERVERS; i++) {

		if (strncmp (jack_shm_header->server[i].name,
			     jack_shm_server_prefix,
			     JACK_SERVER_NAME_SIZE) != 0)
			continue;	/* no match */

sletz's avatar
sletz committed
478
		if (jack_shm_header->server[i].pid == GetPID()){
sletz's avatar
sletz committed
479
480
481
			res = 0; /* it's me */
			goto unlock;
		}
sletz's avatar
sletz committed
482
483
484

		/* see if server still exists */
	#ifndef WIN32 // steph TO CHECK
sletz's avatar
sletz committed
485
486
487
		if (kill (jack_shm_header->server[i].pid, 0) == 0)  {
			res = EEXIST;	/* other server running */
			goto unlock;
sletz's avatar
sletz committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
		}
	#endif

		/* it's gone, reclaim this entry */
		memset (&jack_shm_header->server[i], 0,
			sizeof (jack_shm_server_t));
	}

	/* find a free entry */
	for (i = 0; i < MAX_SERVERS; i++) {
		if (jack_shm_header->server[i].pid == 0)
			break;
	}

sletz's avatar
sletz committed
502
503
504
505
506
	if (i >= MAX_SERVERS){
		res = ENOSPC;		/* out of space */
		goto unlock;
	}

sletz's avatar
sletz committed
507
	/* claim it */
sletz's avatar
sletz committed
508
	jack_shm_header->server[i].pid = GetPID();
sletz's avatar
sletz committed
509
510
511
	strncpy (jack_shm_header->server[i].name,
		 jack_shm_server_prefix,
		 JACK_SERVER_NAME_SIZE);
sletz's avatar
sletz committed
512

sletz's avatar
sletz committed
513
 unlock:
sletz's avatar
sletz committed
514
	jack_shm_unlock_registry ();
sletz's avatar
sletz committed
515
	return res;
sletz's avatar
sletz committed
516
517
518
}

/* release server_name registration */
sletz's avatar
sletz committed
519
int
sletz's avatar
sletz committed
520
521
522
jack_unregister_server (const char *server_name /* unused */)
{
	int i;
sletz's avatar
sletz committed
523
524
525
526
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }
sletz's avatar
sletz committed
527
528

	for (i = 0; i < MAX_SERVERS; i++) {
sletz's avatar
sletz committed
529
		if (jack_shm_header->server[i].pid == GetPID()) {
sletz's avatar
sletz committed
530
531
532
533
534
			memset (&jack_shm_header->server[i], 0,
				sizeof (jack_shm_server_t));
		}
	}

sletz's avatar
sletz committed
535
	jack_shm_unlock_registry ();
sletz's avatar
sletz committed
536
	return 0;
sletz's avatar
sletz committed
537
538
539
}

/* called for server startup and termination */
sletz's avatar
sletz committed
540
int
sletz's avatar
sletz committed
541
542
543
544
545
546
jack_cleanup_shm ()
{
	int i;
	int destroy;
	jack_shm_info_t copy;

sletz's avatar
sletz committed
547
548
549
550
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }
sletz's avatar
sletz committed
551

sletz's avatar
sletz committed
552
553
	for (i = 0; i < MAX_SHM_ID; i++) {
		jack_shm_registry_t* r;
sletz's avatar
sletz committed
554

sletz's avatar
sletz committed
555
556
557
558
559
560
561
562
563
		r = &jack_shm_registry[i];
		memcpy (&copy, r, sizeof (jack_shm_info_t));
		destroy = FALSE;

		/* ignore unused entries */
		if (r->allocator == 0)
			continue;

		/* is this my shm segment? */
sletz's avatar
sletz committed
564
		if (r->allocator == GetPID()) {
sletz's avatar
sletz committed
565

sletz's avatar
sletz committed
566
			/* allocated by this process, so unattach
sletz's avatar
sletz committed
567
568
569
570
571
572
573
574
			   and destroy. */
			jack_release_shm (&copy);
			destroy = TRUE;

		} else {

			/* see if allocator still exists */
		#ifdef WIN32 // steph
sletz's avatar
sletz committed
575
			jack_info("TODO: kill API not available !!");
sletz's avatar
sletz committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
		#else
			if (kill (r->allocator, 0)) {
				if (errno == ESRCH) {
					/* allocator no longer exists,
					 * so destroy */
					destroy = TRUE;
				}
			}
		#endif
		}

		if (destroy) {

			int index = copy.index;

			if ((index >= 0)  && (index < MAX_SHM_ID)) {
				jack_remove_shm (&jack_shm_registry[index].id);
				jack_release_shm_entry (index);
			}
			r->size = 0;
			r->allocator = 0;
		}
	}

	jack_shm_unlock_registry ();
	return TRUE;
}

/* resize a shared memory segment
 *
 * There is no way to resize a System V shm segment.  Resizing is
 * possible with POSIX shm, but not with the non-conformant Mac OS X
 * implementation.  Since POSIX shm is mainly used on that platform,
 * it's simpler to treat them both the same.
 *
 * So, we always resize by deleting and reallocating.  This is
 * tricky, because the old segment will not disappear until
 * all the clients have released it.  We only do what we can
 * from here.
 *
 * This is not done under a single lock.  I don't even want to think
 * about all the things that could possibly go wrong if multple
 * processes tried to resize the same segment concurrently.  That
 * probably doesn't happen.
 */
int
jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
{
	jack_shm_id_t id;

	/* The underlying type of `id' differs for SYSV and POSIX */
	memcpy (&id, &jack_shm_registry[si->index].id, sizeof (id));

	jack_release_shm (si);
	jack_destroy_shm (si);

	if (jack_shmalloc ((char *) id, size, si)) {
		return -1;
	}

	return jack_attach_shm (si);
}

#ifdef USE_POSIX_SHM

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * POSIX interface-dependent functions
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* gain addressability to existing SHM registry segment
 *
 * sets up global registry pointers, if successful
 *
 * returns: 0 if existing registry accessed successfully
 *          ENOENT if registry does not exist
 *          EINVAL if registry exists, but has the wrong size
 */
static int
jack_access_registry (jack_shm_info_t *ri)
{
	/* registry must be locked */
	int shm_fd;

	strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));

	/* try to open an existing segment */
	if ((shm_fd = shm_open (registry_id, O_RDWR, 0666)) < 0) {
		int rc = errno;
		if (errno != ENOENT) {
			jack_error ("Cannot open existing shm registry segment"
				    " (%s)", strerror (errno));
		}
		close (shm_fd);
		return rc;
	}

sletz's avatar
sletz committed
672
	if ((ri->ptr.attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
sletz's avatar
sletz committed
673
674
675
676
677
678
679
680
681
682
				     PROT_READ|PROT_WRITE,
				     MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
		jack_error ("Cannot mmap shm registry segment (%s)",
			    strerror (errno));
		close (shm_fd);
		return EINVAL;
	}

	/* set up global pointers */
	ri->index = JACK_SHM_REGISTRY_INDEX;
sletz's avatar
sletz committed
683
	jack_shm_header = ri->ptr.attached_at;
sletz's avatar
sletz committed
684
	jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
sletz's avatar
sletz committed
685

sletz's avatar
sletz committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
	close (shm_fd); // steph
	return 0;
}

/* create a new SHM registry segment
 *
 * sets up global registry pointers, if successful
 *
 * returns: 0 if registry created successfully
 *          nonzero error code if unable to allocate a new registry
 */
static int
jack_create_registry (jack_shm_info_t *ri)
{
	/* registry must be locked */
	int shm_fd;
sletz's avatar
sletz committed
702

sletz's avatar
sletz committed
703
704
705
706
707
708
709
710
	strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));

	if ((shm_fd = shm_open (registry_id, O_RDWR|O_CREAT, 0666)) < 0) {
		int rc = errno;
		jack_error ("Cannot create shm registry segment (%s)",
			    strerror (errno));
		return rc;
	}
sletz's avatar
sletz committed
711

sletz's avatar
sletz committed
712
713
    /* Previous shm_open result depends of the actual value of umask, force correct file permisssion here */
    if (fchmod(shm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
sletz's avatar
sletz committed
714
	    jack_log("Cannot chmod jack-shm-registry (%s) %d %d", strerror (errno));
sletz's avatar
sletz committed
715
    }
sletz's avatar
sletz committed
716
717
718
719
720
721
722
723
724
725
726
727

	/* Set the desired segment size.  NOTE: the non-conformant Mac
	 * OS X POSIX shm only allows ftruncate() on segment creation.
	 */
	if (ftruncate (shm_fd, JACK_SHM_REGISTRY_SIZE) < 0) {
		int rc = errno;
		jack_error ("Cannot set registry size (%s)", strerror (errno));
		jack_remove_shm (&registry_id);
		close (shm_fd);
		return rc;
	}

sletz's avatar
sletz committed
728
	if ((ri->ptr.attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
sletz's avatar
sletz committed
729
730
731
732
733
734
735
736
737
738
739
				     PROT_READ|PROT_WRITE,
				     MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
		jack_error ("Cannot mmap shm registry segment (%s)",
			    strerror (errno));
		jack_remove_shm (&registry_id);
		close (shm_fd);
		return EINVAL;
	}

	/* set up global pointers */
	ri->index = JACK_SHM_REGISTRY_INDEX;
sletz's avatar
sletz committed
740
	jack_shm_header = ri->ptr.attached_at;
sletz's avatar
sletz committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
	jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);

	/* initialize registry contents */
	jack_shm_init_registry ();
	close (shm_fd); // steph
	return 0;
}

static void
jack_remove_shm (jack_shm_id_t *id)
{
	/* registry may or may not be locked */
	shm_unlink ((char *) id);
}

void
jack_release_shm (jack_shm_info_t* si)
{
	/* registry may or may not be locked */
sletz's avatar
sletz committed
760
761
	if (si->ptr.attached_at != MAP_FAILED) {
		munmap (si->ptr.attached_at, jack_shm_registry[si->index].size);
sletz's avatar
sletz committed
762
763
764
765
766
767
768
769
770
771
772
773
	}
}

/* allocate a POSIX shared memory segment */
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 rc = -1;
	char name[SHM_NAME_MAX+1];

sletz's avatar
sletz committed
774
775
776
777
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }
sletz's avatar
sletz committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791

	if ((registry = jack_get_free_shm_info ()) == NULL) {
		jack_error ("shm registry full");
		goto unlock;
	}

	/* On Mac OS X, the maximum length of a shared memory segment
	 * name is SHM_NAME_MAX (instead of NAME_MAX or PATH_MAX as
	 * defined by the standard).  Unfortunately, Apple sets this
	 * value so small (about 31 bytes) that it is useless for
	 * actual names.  So, we construct a short name from the
	 * registry index for uniqueness and ignore the shm_name
	 * parameter.  Bah!
	 */
792
	snprintf (name, sizeof (name), "/jack-%d-%d", GetUID(), registry->index);
sletz's avatar
sletz committed
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815

	if (strlen (name) >= sizeof (registry->id)) {
		jack_error ("shm segment name too long %s", name);
		goto unlock;
	}

	if ((shm_fd = shm_open (name, O_RDWR|O_CREAT, 0666)) < 0) {
		jack_error ("Cannot create shm segment %s (%s)",
			    name, strerror (errno));
		goto unlock;
	}

	if (ftruncate (shm_fd, size) < 0) {
		jack_error ("Cannot set size of engine shm "
			    "registry 0 (%s)",
			    strerror (errno));
		close (shm_fd);
		goto unlock;
	}

	close (shm_fd);
	registry->size = size;
	strncpy (registry->id, name, sizeof (registry->id));
sletz's avatar
sletz committed
816
	registry->allocator = GetPID();
sletz's avatar
sletz committed
817
	si->index = registry->index;
sletz's avatar
sletz committed
818
	si->ptr.attached_at = MAP_FAILED;	/* not attached */
sletz's avatar
sletz committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
	rc = 0;				/* success */

 unlock:
	jack_shm_unlock_registry ();
	return rc;
}

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;
	}

sletz's avatar
sletz committed
839
	if ((si->ptr.attached_at = mmap (0, registry->size, PROT_READ|PROT_WRITE,
sletz's avatar
sletz committed
840
				     MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
sletz's avatar
sletz committed
841
		jack_error ("Cannot mmap shm segment %s (%s)",
sletz's avatar
sletz committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
			    registry->id,
			    strerror (errno));
		close (shm_fd);
		return -1;
	}

	close (shm_fd);
	return 0;
}

int
jack_attach_shm_read (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_RDONLY, 0666)) < 0) {
		jack_error ("Cannot open shm segment %s (%s)", registry->id,
			    strerror (errno));
		return -1;
	}

sletz's avatar
sletz committed
865
	if ((si->ptr.attached_at = mmap (0, registry->size, PROT_READ,
sletz's avatar
sletz committed
866
				     MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
sletz's avatar
sletz committed
867
		jack_error ("Cannot mmap shm segment %s (%s)",
sletz's avatar
sletz committed
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
			    registry->id,
			    strerror (errno));
		close (shm_fd);
		return -1;
	}

	close (shm_fd);
	return 0;
}

#elif WIN32

static int
jack_access_registry (jack_shm_info_t *ri)
{
	/* registry must be locked */
	HANDLE shm_fd;
	strncpy (registry_id, "jack-shm-registry", sizeof (registry_id));

	/* try to open an existing segment */
sletz's avatar
sletz committed
888

sletz's avatar
sletz committed
889
890
891
892
893
894
895
896
	if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry_id)) == NULL) {
		int rc = GetLastError();
		if (rc != ERROR_FILE_NOT_FOUND) {
			jack_error ("Cannot open existing shm registry segment (%ld)", rc);
		}
		return rc;
	}

sletz's avatar
sletz committed
897
	if ((ri->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, JACK_SHM_REGISTRY_SIZE)) == NULL) {
sletz's avatar
sletz committed
898
899
900
901
902
903
904
905
		jack_error ("Cannot mmap shm registry segment (%ld)", GetLastError());
		jack_remove_shm (&registry_id);
		CloseHandle (shm_fd);
		return EINVAL;
	}

	/* set up global pointers */
	ri->index = JACK_SHM_REGISTRY_INDEX;
sletz's avatar
sletz committed
906
	jack_shm_header = ri->ptr.attached_at;
sletz's avatar
sletz committed
907
908
909
910
911
912
913
914
915
916
917
918
919
920
	jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);

	//CloseHandle(shm_fd);  // TO CHECK
	return 0;
}

static int
jack_create_registry (jack_shm_info_t *ri)
{
	/* registry must be locked */
	HANDLE shm_fd;

	strncpy (registry_id, "jack-shm-registry", sizeof (registry_id));

sletz's avatar
sletz committed
921
922
923
	if ((shm_fd = CreateFileMapping(INVALID_HANDLE_VALUE,
									0, PAGE_READWRITE,
									0, JACK_SHM_REGISTRY_SIZE,
sletz's avatar
sletz committed
924
925
926
927
928
929
									registry_id)) == NULL || (shm_fd == INVALID_HANDLE_VALUE)) {
		int rc = GetLastError();
		jack_error ("Cannot create shm registry segment (%ld)", rc);
		return rc;
	}

sletz's avatar
sletz committed
930
	if ((ri->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, JACK_SHM_REGISTRY_SIZE)) == NULL) {
sletz's avatar
sletz committed
931
932
933
934
935
936
937
938
		jack_error ("Cannot mmap shm registry segment (%ld)", GetLastError());
		jack_remove_shm (&registry_id);
		CloseHandle (shm_fd);
		return EINVAL;
	}

	/* set up global pointers */
	ri->index = JACK_SHM_REGISTRY_INDEX;
sletz's avatar
sletz committed
939
	jack_shm_header = ri->ptr.attached_at;
sletz's avatar
sletz committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
	jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);

	/* initialize registry contents */
	jack_shm_init_registry ();

	//CloseHandle(shm_fd); // TO CHECK
	return 0;
}

static void
jack_remove_shm (jack_shm_id_t *id)
{
	/* nothing to do */
}

void
jack_release_shm (jack_shm_info_t* si)
{
	/* registry may or may not be locked */
sletz's avatar
sletz committed
959
960
	if (si->ptr.attached_at != NULL) {
		UnmapViewOfFile (si->ptr.attached_at);
sletz's avatar
sletz committed
961
962
963
964
965
966
967
968
969
970
971
	}
}

int
jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
{
	jack_shm_registry_t* registry;
	HANDLE shm_fd;
	int rc = -1;
	char name[SHM_NAME_MAX+1];

sletz's avatar
sletz committed
972
973
974
975
	if (jack_shm_lock_registry () < 0) {
        jack_error ("jack_shm_lock_registry fails...");
        return -1;
    }
sletz's avatar
sletz committed
976
977
978
979
980
981

	if ((registry = jack_get_free_shm_info ()) == NULL) {
		jack_error ("shm registry full");
		goto unlock;
	}

982
	snprintf (name, sizeof (name), "jack-%d-%d", GetUID(), registry->index);
sletz's avatar
sletz committed
983
984
985
986
987
988

	if (strlen (name) >= sizeof (registry->id)) {
		jack_error ("shm segment name too long %s", name);
		goto unlock;
	}

sletz's avatar
sletz committed
989
990
991
	if ((shm_fd = CreateFileMapping(INVALID_HANDLE_VALUE,
									0, PAGE_READWRITE,
									0, size,
sletz's avatar
sletz committed
992
993
994
995
996
997
998
999
1000
									name)) == NULL || (shm_fd == INVALID_HANDLE_VALUE)) {
		int rc = GetLastError();
		jack_error ("Cannot create shm segment (%ld)",rc);
		goto unlock;
	}

	//CloseHandle (shm_fd); // TO CHECK

	registry->size = size;
For faster browsing, not all history is shown. View entire blame