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

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

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