Annotation of freem/src/shmmgr.c, revision 1.13

1.1       snw         1: /*
1.13    ! snw         2:  *   $Id: shmmgr.c,v 1.12 2025/04/15 21:57:10 snw Exp $
1.1       snw         3:  *    shared memory manager
                      4:  *
                      5:  *  
1.3       snw         6:  *   Author: Serena Willis <snw@coherent-logic.com>
1.1       snw         7:  *    Copyright (C) 1998 MUG Deutschland
1.4       snw         8:  *    Copyright (C) 2020, 2025 Coherent Logic Development LLC
1.1       snw         9:  *
                     10:  *
                     11:  *   This file is part of FreeM.
                     12:  *
                     13:  *   FreeM is free software: you can redistribute it and/or modify
                     14:  *   it under the terms of the GNU Affero Public License as published by
                     15:  *   the Free Software Foundation, either version 3 of the License, or
                     16:  *   (at your option) any later version.
                     17:  *
                     18:  *   FreeM is distributed in the hope that it will be useful,
                     19:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21:  *   GNU Affero Public License for more details.
                     22:  *
                     23:  *   You should have received a copy of the GNU Affero Public License
                     24:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
                     25:  *
1.5       snw        26:  *   $Log: shmmgr.c,v $
1.13    ! snw        27:  *   Revision 1.12  2025/04/15 21:57:10  snw
        !            28:  *   Fix SysV IPC bugs on FreeBSD
        !            29:  *
1.12      snw        30:  *   Revision 1.11  2025/04/15 21:08:51  snw
                     31:  *   Add some useful debug output
                     32:  *
1.11      snw        33:  *   Revision 1.10  2025/04/15 19:26:13  snw
                     34:  *   Remove extra whitespace
                     35:  *
1.10      snw        36:  *   Revision 1.9  2025/04/15 16:49:36  snw
                     37:  *   Make use of logprintf throughout codebase
                     38:  *
1.9       snw        39:  *   Revision 1.8  2025/04/14 19:46:18  snw
                     40:  *   Add SHM_REMAP flag to shmat on FreeBSD
                     41:  *
1.8       snw        42:  *   Revision 1.7  2025/04/09 19:52:02  snw
                     43:  *   Eliminate as many warnings as possible while building with -Wall
                     44:  *
1.7       snw        45:  *   Revision 1.6  2025/04/04 19:43:18  snw
                     46:  *   Switch to using environment catalog to determine user and group for environment, and remove -u and -g flags from freem
                     47:  *
1.6       snw        48:  *   Revision 1.5  2025/03/24 02:56:50  snw
                     49:  *   Shared memory compatibility fixes for OS/2
                     50:  *
1.5       snw        51:  *   Revision 1.4  2025/03/09 19:50:47  snw
                     52:  *   Second phase of REUSE compliance and header reformat
                     53:  *
1.4       snw        54:  *
                     55:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     56:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        57:  **/
                     58: 
                     59: #include <stdlib.h>
                     60: #include <stdio.h>
                     61: #include <assert.h>
                     62: #include <string.h>
                     63: #include <unistd.h>
                     64: #include <errno.h>
                     65: #include <signal.h>
                     66: 
                     67: #include "shmmgr.h"
                     68: #include "mpsdef.h"
                     69: #include "locktab.h"
1.9       snw        70: #include "log.h"
1.1       snw        71: 
                     72: #include <sys/types.h>
                     73: #include <sys/ipc.h>
                     74: #include <sys/sem.h>
                     75: 
1.5       snw        76: #if !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(__OS2__)
1.1       snw        77: union semun {
                     78:     int              val;    /* Value for SETVAL */
                     79:     struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
                     80:     unsigned short  *array;  /* Array for GETALL, SETALL */
                     81:     struct seminfo  *__buf;  /* Buffer for IPC_INFO
                     82:                                 (Linux-specific) */
                     83: };
                     84: #endif
                     85: 
                     86: int semid_shm;
                     87: extern int semid_locktab;
                     88: extern int semid_jobtab;
                     89: extern int semid_tp;
                     90: extern int semid_symtab;
                     91: 
                     92: void shm_daemon_init(void);
                     93: 
                     94: shm_config_t *shm_config = (shm_config_t *) NULL;
                     95: 
1.12      snw        96: #if defined(__FreeBSD__)
1.13    ! snw        97: # define FM_SHM_PERMS 0777
1.12      snw        98: #else
                     99: # define FM_SHM_PERMS 0770
                    100: #endif
                    101: 
1.1       snw       102: short shm_init(const size_t seg_size)
                    103: {
                    104:     size_t alloc_map_size;
                    105:     long pg_size;
                    106:     key_t shm_sk;
1.13    ! snw       107:     
        !           108: #if defined(__FreeBSD__)
        !           109:     struct shmid_ds ctl;
        !           110: #endif
        !           111:     
1.1       snw       112:     shm_sk = ftok (config_file, 5);    
                    113:     pg_size = sysconf (_SC_PAGESIZE);
                    114:     
                    115:     shm_config = (shm_config_t *) malloc (sizeof (shm_config_t));
                    116:     NULLPTRCHK(shm_config,"shm_init");    
                    117:     
                    118:     /* figure out how many pages we can fit in the segment, accounting for header size */
                    119:     shm_config->pgct = (seg_size / pg_size) - sizeof (shm_hdr_t);
                    120:     
                    121:     /* how big will the alloc map be? */
                    122:     alloc_map_size = shm_config->pgct * sizeof (shm_page_t);
                    123:     
                    124:     shm_config->segsiz = seg_size + alloc_map_size + pg_size;
                    125:     shm_config->key = ftok (config_file, 1);
                    126:     shm_config->pgsiz = pg_size;
1.12      snw       127:        
                    128:     shm_config->seg_id = shmget (shm_config->key, shm_config->segsiz, FM_SHM_PERMS | IPC_CREAT);
1.1       snw       129:     if (shm_config->seg_id == -1) {
                    130:         if (errno == 22) {
1.9       snw       131:             logprintf (FM_LOG_ERROR, "shm_init:  cannot get shared memory segment of %ld bytes", (unsigned long) shm_config->segsiz);
                    132:             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"); 
1.1       snw       133:         }
                    134:         return SHMS_GET_ERR;
                    135:     }
                    136: 
                    137: #if !defined(__arm__)    
                    138:     shm_config->dta = shmat (shm_config->seg_id, NULL, 0);
                    139: #else
                    140:     shm_config->dta = shmat (shm_config->seg_id, (void *) 0x1000000, 0);
                    141: #endif
                    142:     
                    143:     if (shm_config->dta == (void *) -1) {     
1.13    ! snw       144:         logprintf (FM_LOG_FATAL, "shm_init:  shmat() failed (error code %d [%s])", errno, strerror (errno));
1.1       snw       145:     }
                    146:     /* view the first sizeof (shm_hdr_t) bytes of the data area as an shm_hdr_t */
                    147:     shm_config->hdr = (shm_hdr_t *) shm_config->dta;
                    148:     
                    149:     if (shm_config->hdr->magic != shm_config->key) {
                    150: 
                    151:         /* the shm segment is brand new */
                    152:         first_process = TRUE;
                    153: 
                    154:         shm_daemon_init ();
                    155:         
                    156:     }
                    157:     else {
                    158: 
                    159:         /* this shared mem segment was initialized before */
                    160:         int daemon_chk;
                    161: 
                    162:         /* check if the daemon recorded in the header is actually running */
                    163:         daemon_chk = kill (shm_config->hdr->first_process, 0);
                    164: 
                    165:         if (daemon_chk == -1 && errno == ESRCH) {
                    166: 
1.9       snw       167:             logprintf (FM_LOG_WARNING, "shm_init:  recovering from crashed daemon pid %ld", shm_config->hdr->first_process);
1.1       snw       168: 
                    169:             first_process = TRUE;
                    170: 
                    171:             shm_daemon_init ();
                    172: 
                    173:         }
                    174:         else {
                    175:         
                    176: 
                    177:             first_process = FALSE;
                    178: 
                    179:             semid_shm = semget (shm_sk, 1, 0);
                    180:             if (semid_shm == -1) {
1.9       snw       181:                 logprintf (FM_LOG_FATAL, "shm_init:  could not attach to shared memory semaphore [%s]", strerror (errno));         
1.1       snw       182:             }
                    183:             
                    184:             /* we are NOT the initial process. if addresses don't match, re-attach! */
                    185:             /* (again, borrowed from RSM) */
                    186:             if (shm_config->hdr->shmad != shm_config->dta) {
                    187:                 
                    188:                 /* grab the pointers we need */
                    189:                 void *old_addr = shm_config->dta;
                    190:                 void *new_addr = shm_config->hdr->shmad;
1.11      snw       191: 
1.12      snw       192:                 logprintf (FM_LOG_INFO, "shmmgr:  remapping shared memory from virtual address %p to %p", old_addr, new_addr);
1.1       snw       193:                 
                    194:                 /* detach and reattach */
                    195:                 if (shmdt (old_addr) == -1) {
1.9       snw       196:                     logprintf (FM_LOG_FATAL, "shm_init:  detach failed during detach/reattach [shmdt error %s]", strerror (errno));
1.1       snw       197:                 }
1.8       snw       198: 
1.13    ! snw       199:                 if ((shm_config->dta = shmat (shm_config->seg_id, new_addr, 0)) == -1) {
        !           200:                     switch (errno) {
1.1       snw       201: 
1.13    ! snw       202:                         case EINVAL:
        !           203:                             logprintf (FM_LOG_FATAL, "shm_init:  shmat() failed; no matching shared memory segment exists or shared memory address invalid");
        !           204:                             break;
        !           205: 
        !           206:                         case ENOMEM:
        !           207:                             logprintf (FM_LOG_ERROR, "shm_init:  shmat() failed; specified address cannot be used for mapping");
        !           208:                             exit (1);
        !           209:                             break;
        !           210: 
        !           211:                         case EMFILE:
        !           212:                             logprintf (FM_LOG_FATAL, "shm_init:  shmat() failed; per-process shared memory segment limit reached; check kernel tuning parameters");
        !           213:                             break;
        !           214: 
        !           215:                         default:
        !           216:                             logprintf (FM_LOG_FATAL, "shm_init:  shmat() failed; error code %d [%s]", errno, strerror (errno));
        !           217:                             break;
        !           218:                             
        !           219:                     }
1.1       snw       220:                 }
                    221:                 
                    222:                 shm_config->hdr = (shm_hdr_t *) shm_config->dta;
                    223: 
                    224:                 /* allocator buffer at the next page-aligned address after the header and allocation map */
                    225:                 shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct));
                    226:             }
                    227:             else {
                    228:                 shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct));
                    229:             }
                    230:         
                    231:         }
                    232: 
                    233:     }
                    234:     
                    235:     locktab_init ();
                    236:     
                    237:     assert(shm_address_to_page_num(shm_page_num_to_address(20)) == 20);
                    238:     
                    239: 
                    240:     return TRUE;
                    241: }
                    242: 
                    243: void shm_daemon_init(void)
                    244: {
                    245:     union semun arg;
                    246:     key_t shm_sk;
                    247:     register int i;
                    248: 
                    249:     shm_sk = ftok (config_file, 5);
                    250:     
                    251:     semid_shm = semget (shm_sk, 1, 0660 | IPC_CREAT);
                    252:     if (semid_shm == -1) {
1.9       snw       253:         logprintf (FM_LOG_FATAL, "shm_init:  failed to create shared memory semaphore [%s]", strerror (errno));
1.1       snw       254:     }
                    255:     
                    256:     arg.val = 1;
                    257:     if (semctl (semid_shm, 0, SETVAL, arg) == -1) {
1.9       snw       258:         logprintf (FM_LOG_FATAL, "shm_init:  failed to initialize shared memory semaphore [%s]", strerror (errno));  
1.1       snw       259:     }
                    260:     
                    261:     /* zero out the segment */
                    262:     memset (shm_config->dta, 0, shm_config->segsiz);
                    263:     
                    264:     /* we are the process that created the segment: initialize everything */
                    265:     shm_config->hdr->magic = shm_config->key;
                    266:     shm_config->hdr->first_process = pid;
                    267:     
                    268:     /* store the address we got into the shm_hdr (borrowed from RSM) */
                    269:     shm_config->hdr->shmad = shm_config->dta;
                    270:     shm_config->hdr->maintenance_mode = 0;
                    271:     
                    272:     /* alloc_map comes after the header */
                    273: /*
                    274:     shm_config->alloc_map = (shm_page_t *) (shm_config->dta + sizeof (shm_hdr_t));
                    275: */    
                    276:     shm_config->buf = SHMALIGN(shm_config->dta + (sizeof (shm_hdr_t) * shm_config->pgct));
1.9       snw       277:     logprintf (FM_LOG_INFO, "shm_daemon_init:  allocator buffer aligned at %p (system page size %ld)", shm_config->buf, sysconf (_SC_PAGESIZE));
1.1       snw       278:     
                    279:     for (i = 0; i < shm_config->pgct; i++) {
                    280:         shm_config->hdr->alloc_map[i].is_first = FALSE;
                    281:         shm_config->hdr->alloc_map[i].is_last = FALSE;
                    282:         shm_config->hdr->alloc_map[i].pg_state = PG_FREE;
                    283:     }
                    284:     
                    285: }
                    286: 
                    287: short shm_exit(void)
                    288: {
                    289:     int res;
                    290:     union semun arg;
                    291: 
                    292:     res = shmdt (shm_config->dta);
                    293: 
                    294:     if (res == -1) {
1.9       snw       295:         logprintf (FM_LOG_ERROR, "shm_exit:  failure in shmdt() [%s]", strerror (errno));
1.1       snw       296:         return FALSE;
                    297:     }
                    298:     
                    299:     if (first_process) {
                    300: 
                    301:         res = shmctl (shm_config->seg_id, IPC_RMID, 0);
                    302: 
                    303:         if (res == -1) {
1.9       snw       304:             logprintf (FM_LOG_ERROR, "shm_exit:  failure in shmctl() [%s]", strerror (errno));
1.1       snw       305:             return FALSE;
                    306:         }
                    307: 
                    308:         semctl (semid_shm, 0, IPC_RMID, arg);
                    309:         semctl (semid_locktab, 0, IPC_RMID, arg);
                    310:         semctl (semid_jobtab, 0, IPC_RMID, arg);
                    311:         semctl (semid_tp, 0, IPC_RMID, arg);
                    312:         semctl (semid_symtab, 0, IPC_RMID, arg);
                    313:         
                    314:     }
                    315: 
                    316:     return TRUE;
                    317:     
                    318: }
                    319: 
                    320: short shm_get_sem(void)
                    321: {
                    322:     
                    323:     int tries;
                    324:     struct sembuf s = {0, -1, 0};
                    325:     
                    326:     for (tries = 0; tries < 3; tries++) {
                    327: 
                    328:         if (semop (semid_shm, &s, 1) != -1) {
                    329:             return TRUE;
                    330:         }
                    331: 
                    332:         sleep (1);
                    333: 
                    334:     }
                    335: 
                    336:     return FALSE;
                    337:     
                    338: }
                    339: 
                    340: short shm_release_sem(void)
                    341: {
                    342:     struct sembuf s = {0, 1, 0};
                    343: 
                    344:     if (semop (semid_shm, &s, 1) != -1) {
                    345:         return TRUE;
                    346:     }
                    347: 
                    348:     return FALSE;
                    349: }
                    350: 
                    351: void shm_set_maintenance_mode (const short maintenance_mode)
                    352: {
                    353:     if (shm_get_sem () == TRUE) {
                    354:         shm_config->hdr->maintenance_mode = maintenance_mode;
                    355: 
                    356:         shm_release_sem ();
                    357:     }
                    358: }
                    359: 
                    360: shm_page_t *shm_get_alloc_map_entry(const int page_number)
                    361: {
                    362:     return &(shm_config->hdr->alloc_map[page_number]);
                    363: }
                    364: 
                    365: void *shm_page_num_to_address(const int page_num)
                    366: {
                    367:     return (void *) shm_config->buf + (shm_config->pgsiz * page_num);
                    368: }
                    369: 
                    370: int shm_address_to_page_num(const void *address)
                    371: {
                    372:     unsigned long val = (unsigned long) address - (unsigned long) shm_config->buf;
                    373:     unsigned long new_val = val / shm_config->pgsiz;
                    374:     
                    375:     return (int) new_val;
                    376: }
                    377: 
                    378: void *shm_alloc_pages(const int page_count)
                    379: {
                    380:     
                    381:     register int i;
                    382:     register int j;
                    383: 
                    384:     int candidate_page = 0;
                    385:     int free_pages_gotten = 0;
                    386:     
                    387:     shm_page_t *pg;
                    388: 
                    389:     if (shm_get_sem () == FALSE) {
1.9       snw       390:         logprintf (FM_LOG_FATAL, "shm_alloc_pages:  could not get exclusive access to shared memory");
1.1       snw       391:     }
                    392: 
                    393:     
                    394:     for (i = 0; i < shm_config->pgct; i++) {
                    395: 
                    396:         pg = shm_get_alloc_map_entry (i);
                    397:         NULLPTRCHK(pg,"shm_alloc_pages");
                    398: 
                    399:         free_pages_gotten = 0;
                    400:         
                    401:         if (pg->pg_state == PG_FREE) {
                    402: 
                    403:             candidate_page = i;
                    404:             
                    405:             for (j = i; ((j < (i + page_count)) && (j < shm_config->pgct)); j++) {
                    406:                 pg = shm_get_alloc_map_entry (j);
                    407: 
                    408:                 if (pg->pg_state == PG_FREE) free_pages_gotten++;
                    409:                 
                    410:             }
                    411: 
                    412:             if (free_pages_gotten == page_count) {
                    413: 
                    414:                 for (j = candidate_page; j < (candidate_page + page_count); j++) {
                    415:                     pg = shm_get_alloc_map_entry (j);
                    416: 
                    417:                     pg->pg_state = PG_ALLOC;
                    418:                     pg->pid = pid;
                    419:                     
                    420:                     if (j == candidate_page) {
                    421:                         pg->is_first = TRUE;
                    422:                     }
                    423: 
                    424:                     if (j == candidate_page + (page_count - 1)) {
                    425:                         pg->is_last = TRUE;
                    426:                     }
                    427:                     
                    428:                 }
                    429: 
                    430:                 shm_release_sem ();
                    431:                 
                    432:                 return (void *) shm_config->buf + (shm_config->pgsiz * candidate_page);
                    433:                 
                    434:             }
                    435:             
                    436:         }
                    437: 
                    438:     }
                    439: 
                    440:     shm_release_sem ();
                    441:     
                    442:     return (void *) NULL;
                    443:     
                    444: }
                    445: 
                    446: 
                    447: void *shm_alloc(const size_t bytes)
                    448: {
                    449:     int pages_needed = bytes / shm_config->pgsiz;
                    450:     float extra = bytes % shm_config->pgsiz;
                    451: 
                    452:     if (extra > 0) {
                    453:         pages_needed++;
                    454:     }
                    455: 
                    456:     return shm_alloc_pages (pages_needed);
                    457: }
                    458: 
                    459: void shm_free_page(const int page_number)
                    460: {
                    461:     register int i;
                    462:     shm_page_t *a = shm_get_alloc_map_entry (page_number);
                    463: 
                    464:     if (a->is_first == FALSE) {
1.9       snw       465:         logprintf (FM_LOG_ERROR, "shm_free_page:  attempt to free page in the middle of allocation chain");
1.1       snw       466:         return;
                    467:     }
                    468: 
                    469:     if (a->pg_state == PG_FREE) {
1.9       snw       470:         logprintf (FM_LOG_FATAL, "shm_free_page:  double free attempted in page %d", page_number);
1.1       snw       471:     }
                    472:     
                    473:     if (shm_get_sem () == FALSE) {
1.9       snw       474:         logprintf (FM_LOG_FATAL, "shm_free_page:  could not get exclusive access to shared memory");
1.1       snw       475:     }
                    476: 
                    477:     
                    478:     for (i = page_number; i < shm_config->pgct; i++) {
                    479: 
                    480:         a = shm_get_alloc_map_entry (i);        
                    481:         
                    482:         if (a->is_last) {
                    483:             a->is_first = FALSE;
                    484:             a->pg_state = PG_FREE;
                    485:             a->pid = 0;
                    486:             a->is_last = FALSE;
                    487: 
                    488:             shm_release_sem ();
                    489:             
                    490:             return;
                    491:         }
                    492:         else {
                    493:             a->is_first = FALSE;
                    494:             a->pg_state = PG_FREE;
                    495:             a->pid = 0;
                    496:             a->is_last = FALSE;
                    497:         }
                    498:         
                    499:     }
                    500: 
                    501:     shm_release_sem ();
                    502:    
                    503: }
                    504: 
                    505: void shm_free(const void *addr)
                    506: {
                    507:     shm_free_page (shm_address_to_page_num (addr));
                    508: }
                    509: 
                    510: void shm_dump(void)
                    511: {
                    512: 
                    513:     printf ("SHARED MEMORY CONFIGURATION\r\n");
1.2       snw       514:     printf ("  pgsiz                   %ld\r\n", (unsigned long) shm_config->pgsiz);
1.1       snw       515:     printf ("  pgct                    %d\r\n", shm_config->pgct);
1.7       snw       516:     printf ("  key                     %d\r\n", shm_config->key);
1.1       snw       517:     printf ("  segid                   %d\r\n", shm_config->seg_id);
1.2       snw       518:     printf ("  sizeof shm_page_t       %ld\r\n", (long) sizeof (shm_page_t));
                    519:     printf ("  segsiz                  %ld\r\n", (long) shm_config->segsiz);
1.1       snw       520:     printf ("  shm address             %p\r\n", shm_config->dta);
1.2       snw       521:     printf ("  alloc_map size          %ld\r\n", (unsigned long) sizeof (shm_page_t) * shm_config->pgct);
1.1       snw       522:     printf ("  buf address             %p\r\n", shm_config->buf);
                    523: }
                    524: 
                    525: void shm_dump_pages(void)
                    526: {
                    527: 
                    528:     register int i;
                    529:     shm_page_t *p;
                    530: 
                    531:     printf ("%-10s%-10s%-10s%-10s%-10s\r\n", "PAGE", "PID", "BMHEAD", "BMTAIL", "STATE");
                    532:     
                    533:     for (i = 0; i < shm_config->pgct; i++) {
                    534: 
                    535:         p = shm_get_alloc_map_entry (i);
                    536: 
                    537:         printf ("%-10d%-10d%-10s%-10s%-10s\r\n",
                    538:                 i,
                    539:                 p->pid,
                    540:                 (p->is_first == TRUE) ? "Y" : "N",
                    541:                 (p->is_last == TRUE) ? "Y" : "N",
                    542:                 (p->pg_state == PG_FREE) ? "PG_FREE" : "PG_ALLOC");
                    543:         
                    544:     }
                    545: 
                    546: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>