int Shm::server_initialize_shm (int new_registry) { int rc; jack_d("server_initialize_shm\n"); if (jack_shm_header) return 0; /* already initialized */ if (shm_lock_registry () < 0) { jack_error ("jack_shm_lock_registry fails..."); return -1; } rc = access_registry (®istry_info); if (new_registry) { remove_shm (®istry_id); rc = ENOENT; } switch (rc) { case ENOENT: /* registry does not exist */ rc = create_registry (®istry_info); break; case 0: /* existing registry */ if (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. */ release_shm (®istry_info); remove_shm (®istry_id); if ((rc = create_registry (®istry_info)) != 0) { //jack_error ("incompatible shm registry (%s)", // strerror (errno)); jack_error ("incompatible shm registry"); //#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; } shm_unlock_registry (); return rc; }
void Shm::destroy_shm (jack_shm_info_t* si) { /* must NOT have the registry locked */ if (si->index == JACK_SHM_NULL_INDEX) return; /* segment not allocated */ remove_shm (&jack_shm_registry[si->index].id); release_shm_info (si->index); }
int Shm::cleanup_shm (void) { int i; int destroy; jack_shm_info_t copy; if (shm_lock_registry () < 0) { jack_error ("jack_shm_lock_registry fails..."); return -1; } for (i = 0; i < MAX_SHM_ID; i++) { jack_shm_registry_t* r; r = &jack_shm_registry[i]; memcpy (©, r, sizeof (jack_shm_info_t)); destroy = FALSE; /* ignore unused entries */ if (r->allocator == 0) continue; /* is this my shm segment? */ if (r->allocator == GetPID()) { /* allocated by this process, so unattach and destroy. */ release_shm (©); destroy = TRUE; } else { /* see if allocator still exists */ if (kill (r->allocator, 0)) { if (errno == ESRCH) { /* allocator no longer exists, * so destroy */ destroy = TRUE; } } } if (destroy) { int index = copy.index; if ((index >= 0) && (index < MAX_SHM_ID)) { remove_shm (&jack_shm_registry[index].id); release_shm_entry (index); } r->size = 0; r->allocator = 0; } } shm_unlock_registry (); return TRUE; }
// Returns -1 if 'uidshm' == NULL and shared memory does not exist. // Otherwise does nothing and returns 0. int resolve_ucache(void) { int iscreate = 0; if (uidshm == NULL) { uidshm = attach_shm2("UCACHE_SHMKEY", 3696, sizeof(*uidshm), &iscreate); if (uidshm == NULL) return -1; } if (iscreate) { remove_shm("UCACHE_SHMKEY", 3696, sizeof(*uidshm)); report("Error: miscd is not running!", ""); return -1; } return 0; }