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

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

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