version 1.12, 2025/04/15 21:57:10
|
version 1.18, 2025/05/16 04:25:15
|
Line 24
|
Line 24
|
* along with FreeM. If not, see <https://www.gnu.org/licenses/>. |
* along with FreeM. If not, see <https://www.gnu.org/licenses/>. |
* |
* |
* $Log$ |
* $Log$ |
|
* Revision 1.18 2025/05/16 04:25:15 snw |
|
* Make shm seed NULL on ARM |
|
* |
|
* Revision 1.17 2025/05/12 18:18:00 snw |
|
* Further work on shared memory |
|
* |
|
* Revision 1.16 2025/05/12 14:26:15 snw |
|
* Revert bad change in shared memory manager |
|
* |
|
* Revision 1.15 2025/05/09 19:44:50 snw |
|
* Begin shm rework |
|
* |
|
* Revision 1.14 2025/05/08 14:47:26 snw |
|
* Break everything to begin shared memory rewrite |
|
* |
|
* Revision 1.13 2025/04/16 17:36:12 snw |
|
* Add FreeBSD shm cleanup script |
|
* |
* Revision 1.12 2025/04/15 21:57:10 snw |
* Revision 1.12 2025/04/15 21:57:10 snw |
* Fix SysV IPC bugs on FreeBSD |
* Fix SysV IPC bugs on FreeBSD |
* |
* |
Line 94 void shm_daemon_init(void);
|
Line 112 void shm_daemon_init(void);
|
shm_config_t *shm_config = (shm_config_t *) NULL; |
shm_config_t *shm_config = (shm_config_t *) NULL; |
|
|
#if defined(__FreeBSD__) |
#if defined(__FreeBSD__) |
# define FM_SHM_PERMS 0660 |
# define FM_SHM_PERMS 0777 |
#else |
#else |
# define FM_SHM_PERMS 0770 |
# define FM_SHM_PERMS 0770 |
#endif |
#endif |
Line 104 short shm_init(const size_t seg_size)
|
Line 122 short shm_init(const size_t seg_size)
|
size_t alloc_map_size; |
size_t alloc_map_size; |
long pg_size; |
long pg_size; |
key_t shm_sk; |
key_t shm_sk; |
|
|
|
#if defined(__FreeBSD__) |
|
struct shmid_ds ctl; |
|
#endif |
|
|
shm_sk = ftok (config_file, 5); |
shm_sk = ftok (config_file, 5); |
pg_size = sysconf (_SC_PAGESIZE); |
pg_size = sysconf (_SC_PAGESIZE); |
|
|
Line 112 short shm_init(const size_t seg_size)
|
Line 134 short shm_init(const size_t seg_size)
|
NULLPTRCHK(shm_config,"shm_init"); |
NULLPTRCHK(shm_config,"shm_init"); |
|
|
/* figure out how many pages we can fit in the segment, accounting for header size */ |
/* figure out how many pages we can fit in the segment, accounting for header size */ |
shm_config->pgct = (seg_size / pg_size) - sizeof (shm_hdr_t); |
shm_config->pgct = (seg_size - sizeof (shm_hdr_t)) / pg_size; |
|
|
/* how big will the alloc map be? */ |
/* how big will the alloc map be? */ |
alloc_map_size = shm_config->pgct * sizeof (shm_page_t); |
alloc_map_size = shm_config->pgct * sizeof (shm_page_t); |
Line 130 short shm_init(const size_t seg_size)
|
Line 152 short shm_init(const size_t seg_size)
|
return SHMS_GET_ERR; |
return SHMS_GET_ERR; |
} |
} |
|
|
#if !defined(__arm__) |
|
shm_config->dta = shmat (shm_config->seg_id, NULL, 0); |
shm_config->dta = shmat (shm_config->seg_id, NULL, 0); |
#else |
|
shm_config->dta = shmat (shm_config->seg_id, (void *) 0x1000000, 0); |
|
#endif |
|
|
|
if (shm_config->dta == (void *) -1) { |
if (shm_config->dta == (void *) -1) { |
return SHMS_ATTACH_ERR; |
logprintf (FM_LOG_FATAL, "shm_init: shmat() failed (error code %d [%s])", errno, strerror (errno)); |
} |
} |
/* view the first sizeof (shm_hdr_t) bytes of the data area as an shm_hdr_t */ |
/* view the first sizeof (shm_hdr_t) bytes of the data area as an shm_hdr_t */ |
shm_config->hdr = (shm_hdr_t *) shm_config->dta; |
shm_config->hdr = (shm_hdr_t *) shm_config->dta; |
Line 159 short shm_init(const size_t seg_size)
|
Line 177 short shm_init(const size_t seg_size)
|
daemon_chk = kill (shm_config->hdr->first_process, 0); |
daemon_chk = kill (shm_config->hdr->first_process, 0); |
|
|
if (daemon_chk == -1 && errno == ESRCH) { |
if (daemon_chk == -1 && errno == ESRCH) { |
|
|
logprintf (FM_LOG_WARNING, "shm_init: recovering from crashed daemon pid %ld", shm_config->hdr->first_process); |
logprintf (FM_LOG_WARNING, "shm_init: recovering from crashed daemon pid %ld", shm_config->hdr->first_process); |
|
|
first_process = TRUE; |
first_process = TRUE; |
|
|
shm_daemon_init (); |
shm_daemon_init (); |
|
|
} |
} |
else { |
else { |
|
|
|
|
first_process = FALSE; |
first_process = FALSE; |
|
|
semid_shm = semget (shm_sk, 1, 0); |
semid_shm = semget (shm_sk, 1, 0); |
Line 177 short shm_init(const size_t seg_size)
|
Line 189 short shm_init(const size_t seg_size)
|
logprintf (FM_LOG_FATAL, "shm_init: could not attach to shared memory semaphore [%s]", strerror (errno)); |
logprintf (FM_LOG_FATAL, "shm_init: could not attach to shared memory semaphore [%s]", strerror (errno)); |
} |
} |
|
|
/* we are NOT the initial process. if addresses don't match, re-attach! */ |
shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct)); |
/* (again, borrowed from RSM) */ |
|
if (shm_config->hdr->shmad != shm_config->dta) { |
|
|
|
/* grab the pointers we need */ |
|
void *old_addr = shm_config->dta; |
|
void *new_addr = shm_config->hdr->shmad; |
|
|
|
logprintf (FM_LOG_INFO, "shmmgr: remapping shared memory from virtual address %p to %p", old_addr, new_addr); |
|
|
|
/* detach and reattach */ |
|
if (shmdt (old_addr) == -1) { |
|
logprintf (FM_LOG_FATAL, "shm_init: detach failed during detach/reattach [shmdt error %s]", strerror (errno)); |
|
} |
|
|
|
shm_config->dta = shmat (shm_config->seg_id, new_addr, 0); |
|
|
|
if (shm_config->dta == (void *) -1) { |
|
logprintf (FM_LOG_FATAL, "shm_init: fatal error attaching shared memory segment [shmat error '%s']", strerror (errno)); |
|
} |
|
|
|
shm_config->hdr = (shm_hdr_t *) shm_config->dta; |
|
|
|
/* allocator buffer at the next page-aligned address after the header and allocation map */ |
|
shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct)); |
|
} |
|
else { |
|
shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct)); |
|
} |
|
|
|
} |
} |
|
|
} |
} |