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

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

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