--- freem/src/shmmgr.c 2025/04/14 19:46:18 1.8 +++ freem/src/shmmgr.c 2025/05/12 18:18:00 1.17 @@ -1,5 +1,5 @@ /* - * $Id: shmmgr.c,v 1.8 2025/04/14 19:46:18 snw Exp $ + * $Id: shmmgr.c,v 1.17 2025/05/12 18:18:00 snw Exp $ * shared memory manager * * @@ -24,6 +24,33 @@ * along with FreeM. If not, see . * * $Log: shmmgr.c,v $ + * 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 + * Fix SysV IPC bugs on FreeBSD + * + * Revision 1.11 2025/04/15 21:08:51 snw + * Add some useful debug output + * + * Revision 1.10 2025/04/15 19:26:13 snw + * Remove extra whitespace + * + * Revision 1.9 2025/04/15 16:49:36 snw + * Make use of logprintf throughout codebase + * * Revision 1.8 2025/04/14 19:46:18 snw * Add SHM_REMAP flag to shmat on FreeBSD * @@ -55,6 +82,7 @@ #include "shmmgr.h" #include "mpsdef.h" #include "locktab.h" +#include "log.h" #include #include @@ -80,12 +108,22 @@ void shm_daemon_init(void); shm_config_t *shm_config = (shm_config_t *) NULL; +#if defined(__FreeBSD__) +# define FM_SHM_PERMS 0777 +#else +# define FM_SHM_PERMS 0770 +#endif + short shm_init(const size_t seg_size) { size_t alloc_map_size; long pg_size; key_t shm_sk; - + +#if defined(__FreeBSD__) + struct shmid_ds ctl; +#endif + shm_sk = ftok (config_file, 5); pg_size = sysconf (_SC_PAGESIZE); @@ -93,7 +131,7 @@ short shm_init(const size_t seg_size) NULLPTRCHK(shm_config,"shm_init"); /* 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? */ alloc_map_size = shm_config->pgct * sizeof (shm_page_t); @@ -101,12 +139,12 @@ short shm_init(const size_t seg_size) shm_config->segsiz = seg_size + alloc_map_size + pg_size; shm_config->key = ftok (config_file, 1); shm_config->pgsiz = pg_size; - - shm_config->seg_id = shmget (shm_config->key, shm_config->segsiz, 0770 | IPC_CREAT); + + shm_config->seg_id = shmget (shm_config->key, shm_config->segsiz, FM_SHM_PERMS | IPC_CREAT); if (shm_config->seg_id == -1) { if (errno == 22) { - fprintf (stderr, "shm_init: cannot get shared memory segment of %ld bytes\r\n\r\n", (unsigned long) shm_config->segsiz); - fprintf (stderr, "You may need to tune your kernel parameters, or manually set a smaller shared memory segment size in both the FreeM daemon and each interpreter process by using the `-S` command-line flag.\r\n\r\nPlease refer to the FreeM Platform Notes for your operating system for details.\r\n\r\n"); + logprintf (FM_LOG_ERROR, "shm_init: cannot get shared memory segment of %ld bytes", (unsigned long) shm_config->segsiz); + fprintf (stderr, "\r\nYou may need to tune your kernel parameters, or manually set a smaller shared memory segment size in both the FreeM daemon and each interpreter process by using the `-S` command-line flag.\r\n\r\nPlease refer to the FreeM Platform Notes for your operating system for details.\r\n"); } return SHMS_GET_ERR; } @@ -118,7 +156,7 @@ short shm_init(const size_t seg_size) #endif 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 */ shm_config->hdr = (shm_hdr_t *) shm_config->dta; @@ -140,61 +178,19 @@ short shm_init(const size_t seg_size) daemon_chk = kill (shm_config->hdr->first_process, 0); if (daemon_chk == -1 && errno == ESRCH) { - - fprintf (stderr, "shm_init: recovering from crashed daemon pid %d\r\n", 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; - shm_daemon_init (); - } else { - - first_process = FALSE; semid_shm = semget (shm_sk, 1, 0); if (semid_shm == -1) { - fprintf (stderr, "shm_init: could not attach to shared memory semaphore\r\n"); - exit (1); + 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! */ - /* (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; - - /* detach and reattach */ - if (shmdt (old_addr) == -1) { - fprintf (stderr, "shm_init: detach failed during detach/reattach [shmdt error %s]\r\n", strerror (errno)); - exit (1); - } - -#if defined(__FreeBSD__) - shm_config->dta = shmat (shm_config->seg_id, new_addr, SHM_REMAP); -#else - shm_config->dta = shmat (shm_config->seg_id, new_addr, 0); -#endif - - - - if (shm_config->dta == (void *) -1) { - fprintf (stderr, "shm_init: fatal error attaching shared memory segment [shmat error '%s']\r\n", strerror (errno)); - exit (1); - } - - 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)); - } - + shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct)); } } @@ -217,14 +213,12 @@ void shm_daemon_init(void) semid_shm = semget (shm_sk, 1, 0660 | IPC_CREAT); if (semid_shm == -1) { - fprintf (stderr, "shm_init: failed to create shared memory semaphore\r\n"); - exit (1); + logprintf (FM_LOG_FATAL, "shm_init: failed to create shared memory semaphore [%s]", strerror (errno)); } arg.val = 1; if (semctl (semid_shm, 0, SETVAL, arg) == -1) { - fprintf (stderr, "shm_init: failed to initialize shared memory semaphore\r\n"); - exit (1); + logprintf (FM_LOG_FATAL, "shm_init: failed to initialize shared memory semaphore [%s]", strerror (errno)); } /* zero out the segment */ @@ -243,7 +237,7 @@ void shm_daemon_init(void) shm_config->alloc_map = (shm_page_t *) (shm_config->dta + sizeof (shm_hdr_t)); */ shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct)); - printf ("shm_daemon_init: allocator buffer aligned at %p (system page size %ld)\r\n", shm_config->buf, sysconf (_SC_PAGESIZE)); + logprintf (FM_LOG_INFO, "shm_daemon_init: allocator buffer aligned at %p (system page size %ld)", shm_config->buf, sysconf (_SC_PAGESIZE)); for (i = 0; i < shm_config->pgct; i++) { shm_config->hdr->alloc_map[i].is_first = FALSE; @@ -261,7 +255,7 @@ short shm_exit(void) res = shmdt (shm_config->dta); if (res == -1) { - fprintf (stderr, "shm_exit: failure in shmdt()\r\n"); + logprintf (FM_LOG_ERROR, "shm_exit: failure in shmdt() [%s]", strerror (errno)); return FALSE; } @@ -270,7 +264,7 @@ short shm_exit(void) res = shmctl (shm_config->seg_id, IPC_RMID, 0); if (res == -1) { - fprintf (stderr, "shm_exit: failure in shmctl()\r\n"); + logprintf (FM_LOG_ERROR, "shm_exit: failure in shmctl() [%s]", strerror (errno)); return FALSE; } @@ -356,8 +350,7 @@ void *shm_alloc_pages(const int page_cou shm_page_t *pg; if (shm_get_sem () == FALSE) { - fprintf (stderr, "shm_alloc_pages: could not get exclusive access to shared memory\r\n"); - exit (1); + logprintf (FM_LOG_FATAL, "shm_alloc_pages: could not get exclusive access to shared memory"); } @@ -432,18 +425,16 @@ void shm_free_page(const int page_number shm_page_t *a = shm_get_alloc_map_entry (page_number); if (a->is_first == FALSE) { - fprintf (stderr, "shm_free_page: attempt to free page in the middle of allocation chain\r\n"); + logprintf (FM_LOG_ERROR, "shm_free_page: attempt to free page in the middle of allocation chain"); return; } if (a->pg_state == PG_FREE) { - fprintf (stderr, "shm_free_page: double free attempted in page %d\r\n", page_number); - exit (1); + logprintf (FM_LOG_FATAL, "shm_free_page: double free attempted in page %d", page_number); } if (shm_get_sem () == FALSE) { - fprintf (stderr, "shm_free_page: could not get exclusive access to shared memory\r\n"); - exit (1); + logprintf (FM_LOG_FATAL, "shm_free_page: could not get exclusive access to shared memory"); }