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

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

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