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

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

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